reparent_state_with_layout_builder_test.dart 4.51 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5 6
import 'dart:ui' as ui show window;

7
import 'package:flutter/material.dart';
8
import 'package:flutter_test/flutter_test.dart';
9 10 11 12

// This is a regression test for https://github.com/flutter/flutter/issues/5840.

class Bar extends StatefulWidget {
13
  const Bar({ Key? key }) : super(key: key);
14
  @override
15
  BarState createState() => BarState();
16 17 18
}

class BarState extends State<Bar> {
19
  final GlobalKey _fooKey = GlobalKey();
20 21 22 23 24 25 26 27 28 29 30 31

  bool _mode = false;

  void trigger() {
    setState(() {
      _mode = !_mode;
    });
  }

  @override
  Widget build(BuildContext context) {
    if (_mode) {
32 33
      return SizedBox(
        child: LayoutBuilder(
34
          builder: (BuildContext context, BoxConstraints constraints) {
35
            return StatefulCreationCounter(key: _fooKey);
36
          },
37 38 39
        ),
      );
    } else {
40
      return LayoutBuilder(
41
        builder: (BuildContext context, BoxConstraints constraints) {
42
          return StatefulCreationCounter(key: _fooKey);
43
        },
44 45 46 47 48 49
      );
    }
  }
}

class StatefulCreationCounter extends StatefulWidget {
50
  const StatefulCreationCounter({ Key? key }) : super(key: key);
51 52

  @override
53
  StatefulCreationCounterState createState() => StatefulCreationCounterState();
54 55 56 57 58 59 60 61 62 63 64 65
}

class StatefulCreationCounterState extends State<StatefulCreationCounter> {
  static int creationCount = 0;

  @override
  void initState() {
    super.initState();
    creationCount += 1;
  }

  @override
66
  Widget build(BuildContext context) => Container();
67 68 69
}

void main() {
70
  testWidgets('reparent state with layout builder', (WidgetTester tester) async {
71
    expect(StatefulCreationCounterState.creationCount, 0);
72
    await tester.pumpWidget(const Bar());
73
    expect(StatefulCreationCounterState.creationCount, 1);
74
    final BarState s = tester.state<BarState>(find.byType(Bar));
75 76 77 78
    s.trigger();
    await tester.pump();
    expect(StatefulCreationCounterState.creationCount, 1);
  });
79

80
  testWidgets('Clean then reparent with dependencies', (WidgetTester tester) async {
81
    int layoutBuilderBuildCount = 0;
82

83 84 85
    late StateSetter keyedSetState;
    late StateSetter layoutBuilderSetState;
    late StateSetter childSetState;
86

87 88
    final GlobalKey key = GlobalKey();
    final Widget keyedWidget = StatefulBuilder(
89 90 91 92
      key: key,
      builder: (BuildContext context, StateSetter setState) {
        keyedSetState = setState;
        MediaQuery.of(context);
93
        return Container();
94 95 96 97
      },
    );

    Widget layoutBuilderChild = keyedWidget;
98
    Widget deepChild = Container();
99

100 101 102
    await tester.pumpWidget(MediaQuery(
      data: MediaQueryData.fromWindow(ui.window),
      child: Column(
103
        children: <Widget>[
104
          StatefulBuilder(builder: (BuildContext context, StateSetter setState) {
105
            layoutBuilderSetState = setState;
106
            return LayoutBuilder(
107
              builder: (BuildContext context, BoxConstraints constraints) {
108 109
                layoutBuilderBuildCount += 1;
                return layoutBuilderChild; // initially keyedWidget above, but then a new Container
110 111 112
              },
            );
          }),
113 114 115 116 117 118
          Container(
            child: Container(
              child: Container(
                child: Container(
                  child: Container(
                    child: Container(
119 120 121 122 123 124
                      child: StatefulBuilder(
                        builder: (BuildContext context, StateSetter setState) {
                          childSetState = setState;
                          return deepChild; // initially a Container, but then the keyedWidget above
                        },
                      ),
125 126 127 128 129 130 131 132 133 134 135
                    ),
                  ),
                ),
              ),
            ),
          ),
        ],
      ),
    ));
    expect(layoutBuilderBuildCount, 1);

136
    keyedSetState(() { /* Change nothing but add the element to the dirty list. */ });
137 138

    childSetState(() {
139 140
      // The deep child builds in the initial build phase. It takes the child
      // from the LayoutBuilder before the LayoutBuilder has a chance to build.
141 142 143 144
      deepChild = keyedWidget;
    });

    layoutBuilderSetState(() {
145 146
      // The layout builder will build in a separate build scope. This delays
      // the removal of the keyed child until this build scope.
147
      layoutBuilderChild = Container();
148 149 150 151 152 153
    });

    // The essential part of this test is that this call to pump doesn't throw.
    await tester.pump();
    expect(layoutBuilderBuildCount, 2);
  });
154
}