reparent_state_test.dart 10.8 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
import 'package:flutter/material.dart';
Adam Barth's avatar
Adam Barth committed
6
import 'package:flutter_test/flutter_test.dart';
7

8
class StateMarker extends StatefulWidget {
9
  const StateMarker({ Key? key, this.child }) : super(key: key);
10

11
  final Widget? child;
12

13
  @override
14
  StateMarkerState createState() => StateMarkerState();
15 16 17
}

class StateMarkerState extends State<StateMarker> {
18
  String? marker;
19

20
  @override
21
  Widget build(BuildContext context) {
22
    if (widget.child != null)
23
      return widget.child!;
24
    return Container();
25 26 27
  }
}

28
class DeactivateLogger extends StatefulWidget {
29
  const DeactivateLogger({ required Key key, required this.log }) : super(key: key);
30 31 32 33

  final List<String> log;

  @override
34
  DeactivateLoggerState createState() => DeactivateLoggerState();
35 36 37 38 39
}

class DeactivateLoggerState extends State<DeactivateLogger> {
  @override
  void deactivate() {
40
    widget.log.add('deactivate');
41 42 43 44 45
    super.deactivate();
  }

  @override
  Widget build(BuildContext context) {
46
    widget.log.add('build');
47
    return Container();
48 49 50
  }
}

51
void main() {
52
  testWidgets('can reparent state', (WidgetTester tester) async {
53 54
    final GlobalKey left = GlobalKey();
    final GlobalKey right = GlobalKey();
55

56
    const StateMarker grandchild = StateMarker();
57
    await tester.pumpWidget(
58
      Stack(
59
        textDirection: TextDirection.ltr,
60
        children: <Widget>[
61
          Container(
62
            color: Colors.green,
63
            child: StateMarker(key: left),
64
          ),
65
          Container(
66
            color: Colors.green,
67
            child: StateMarker(
68
              key: right,
69 70
              child: grandchild,
            ),
71
          ),
72
        ],
73
      ),
74
    );
75

76
    final StateMarkerState leftState = left.currentState! as StateMarkerState;
Ian Hickson's avatar
Ian Hickson committed
77
    leftState.marker = 'left';
78
    final StateMarkerState rightState = right.currentState! as StateMarkerState;
Ian Hickson's avatar
Ian Hickson committed
79
    rightState.marker = 'right';
80

81
    final StateMarkerState grandchildState = tester.state(find.byWidget(grandchild));
82
    expect(grandchildState, isNotNull);
Ian Hickson's avatar
Ian Hickson committed
83
    grandchildState.marker = 'grandchild';
84

85
    const StateMarker newGrandchild = StateMarker();
86
    await tester.pumpWidget(
87
      Stack(
88
        textDirection: TextDirection.ltr,
89
        children: <Widget>[
90
          Container(
91
            color: Colors.green,
92
            child: StateMarker(
93
              key: right,
94 95
              child: newGrandchild,
            ),
96
          ),
97
          Container(
98
            color: Colors.green,
99
            child: StateMarker(key: left),
100
          ),
101
        ],
102
      ),
103 104 105
    );

    expect(left.currentState, equals(leftState));
Ian Hickson's avatar
Ian Hickson committed
106
    expect(leftState.marker, equals('left'));
107
    expect(right.currentState, equals(rightState));
Ian Hickson's avatar
Ian Hickson committed
108
    expect(rightState.marker, equals('right'));
109

110
    final StateMarkerState newGrandchildState = tester.state(find.byWidget(newGrandchild));
111 112
    expect(newGrandchildState, isNotNull);
    expect(newGrandchildState, equals(grandchildState));
Ian Hickson's avatar
Ian Hickson committed
113
    expect(newGrandchildState.marker, equals('grandchild'));
114

115
    await tester.pumpWidget(
116 117
      Center(
        child: Container(
118
          color: Colors.green,
119
          child: StateMarker(
120
            key: left,
121 122 123
            child: Container(),
          ),
        ),
124
      ),
125
    );
126

127
    expect(left.currentState, equals(leftState));
Ian Hickson's avatar
Ian Hickson committed
128
    expect(leftState.marker, equals('left'));
129
    expect(right.currentState, isNull);
130 131
  });

132
  testWidgets('can reparent state with multichild widgets', (WidgetTester tester) async {
133 134
    final GlobalKey left = GlobalKey();
    final GlobalKey right = GlobalKey();
135

136
    const StateMarker grandchild = StateMarker();
137
    await tester.pumpWidget(
138
      Stack(
139
        textDirection: TextDirection.ltr,
140
        children: <Widget>[
141 142
          StateMarker(key: left),
          StateMarker(
143
            key: right,
144 145 146
            child: grandchild,
          ),
        ],
147
      ),
148
    );
149

150
    final StateMarkerState leftState = left.currentState! as StateMarkerState;
Ian Hickson's avatar
Ian Hickson committed
151
    leftState.marker = 'left';
152
    final StateMarkerState rightState = right.currentState! as StateMarkerState;
Ian Hickson's avatar
Ian Hickson committed
153
    rightState.marker = 'right';
154

155
    final StateMarkerState grandchildState = tester.state(find.byWidget(grandchild));
156
    expect(grandchildState, isNotNull);
Ian Hickson's avatar
Ian Hickson committed
157
    grandchildState.marker = 'grandchild';
158

159
    const StateMarker newGrandchild = StateMarker();
160
    await tester.pumpWidget(
161
      Stack(
162
        textDirection: TextDirection.ltr,
163
        children: <Widget>[
164
          StateMarker(
165
            key: right,
166
            child: newGrandchild,
167
          ),
168 169
          StateMarker(key: left),
        ],
170
      ),
171 172 173
    );

    expect(left.currentState, equals(leftState));
Ian Hickson's avatar
Ian Hickson committed
174
    expect(leftState.marker, equals('left'));
175
    expect(right.currentState, equals(rightState));
Ian Hickson's avatar
Ian Hickson committed
176
    expect(rightState.marker, equals('right'));
177

178
    final StateMarkerState newGrandchildState = tester.state(find.byWidget(newGrandchild));
179 180
    expect(newGrandchildState, isNotNull);
    expect(newGrandchildState, equals(grandchildState));
Ian Hickson's avatar
Ian Hickson committed
181
    expect(newGrandchildState.marker, equals('grandchild'));
182

183
    await tester.pumpWidget(
184 185
      Center(
        child: Container(
186
          color: Colors.green,
187
          child: StateMarker(
188
            key: left,
189 190 191
            child: Container(),
          ),
        ),
192
      ),
193
    );
194

195
    expect(left.currentState, equals(leftState));
Ian Hickson's avatar
Ian Hickson committed
196
    expect(leftState.marker, equals('left'));
197
    expect(right.currentState, isNull);
198 199
  });

200
  testWidgets('can with scrollable list', (WidgetTester tester) async {
201
    final GlobalKey key = GlobalKey();
202

203
    await tester.pumpWidget(StateMarker(key: key));
204

205
    final StateMarkerState keyState = key.currentState! as StateMarkerState;
Ian Hickson's avatar
Ian Hickson committed
206
    keyState.marker = 'marked';
207

208
    await tester.pumpWidget(
209
      Directionality(
210
        textDirection: TextDirection.ltr,
211
        child: ListView(
212 213
          itemExtent: 100.0,
          children: <Widget>[
214
            SizedBox(
215 216
              key: const Key('container'),
              height: 100.0,
217
              child: StateMarker(key: key),
218 219
            ),
          ],
220
        ),
221 222
      ),
    );
223

224
    expect(key.currentState, equals(keyState));
Ian Hickson's avatar
Ian Hickson committed
225
    expect(keyState.marker, equals('marked'));
226

227
    await tester.pumpWidget(StateMarker(key: key));
228

229
    expect(key.currentState, equals(keyState));
Ian Hickson's avatar
Ian Hickson committed
230
    expect(keyState.marker, equals('marked'));
231
  });
232

233
  testWidgets('Reparent during update children', (WidgetTester tester) async {
234
    final GlobalKey key = GlobalKey();
235

236
    await tester.pumpWidget(Stack(
237
      textDirection: TextDirection.ltr,
238
      children: <Widget>[
239
        StateMarker(key: key),
240
        const SizedBox(width: 100.0, height: 100.0),
241
      ],
242 243
    ));

244
    final StateMarkerState keyState = key.currentState!as StateMarkerState;
Ian Hickson's avatar
Ian Hickson committed
245
    keyState.marker = 'marked';
246

247
    await tester.pumpWidget(Stack(
248
      textDirection: TextDirection.ltr,
249
      children: <Widget>[
250
        const SizedBox(width: 100.0, height: 100.0),
251
        StateMarker(key: key),
252
      ],
253 254 255
    ));

    expect(key.currentState, equals(keyState));
Ian Hickson's avatar
Ian Hickson committed
256
    expect(keyState.marker, equals('marked'));
257

258
    await tester.pumpWidget(Stack(
259
      textDirection: TextDirection.ltr,
260
      children: <Widget>[
261
        StateMarker(key: key),
262
        const SizedBox(width: 100.0, height: 100.0),
263
      ],
264 265 266
    ));

    expect(key.currentState, equals(keyState));
Ian Hickson's avatar
Ian Hickson committed
267
    expect(keyState.marker, equals('marked'));
268
  });
269

270
  testWidgets('Reparent to child during update children', (WidgetTester tester) async {
271
    final GlobalKey key = GlobalKey();
272

273
    await tester.pumpWidget(Stack(
274
      textDirection: TextDirection.ltr,
275
      children: <Widget>[
276
        const SizedBox(width: 100.0, height: 100.0),
277
        StateMarker(key: key),
278
        const SizedBox(width: 100.0, height: 100.0),
279
      ],
280 281
    ));

282
    final StateMarkerState keyState = key.currentState! as StateMarkerState;
Ian Hickson's avatar
Ian Hickson committed
283
    keyState.marker = 'marked';
284

285
    await tester.pumpWidget(Stack(
286
      textDirection: TextDirection.ltr,
287
      children: <Widget>[
288
        SizedBox(width: 100.0, height: 100.0, child: StateMarker(key: key)),
289
        const SizedBox(width: 100.0, height: 100.0),
290
      ],
291 292 293
    ));

    expect(key.currentState, equals(keyState));
Ian Hickson's avatar
Ian Hickson committed
294
    expect(keyState.marker, equals('marked'));
295

296
    await tester.pumpWidget(Stack(
297
      textDirection: TextDirection.ltr,
298
      children: <Widget>[
299
        const SizedBox(width: 100.0, height: 100.0),
300
        StateMarker(key: key),
301
        const SizedBox(width: 100.0, height: 100.0),
302
      ],
303 304 305
    ));

    expect(key.currentState, equals(keyState));
Ian Hickson's avatar
Ian Hickson committed
306
    expect(keyState.marker, equals('marked'));
307

308
    await tester.pumpWidget(Stack(
309
      textDirection: TextDirection.ltr,
310
      children: <Widget>[
311
        const SizedBox(width: 100.0, height: 100.0),
312
        SizedBox(width: 100.0, height: 100.0, child: StateMarker(key: key)),
313
      ],
314 315 316
    ));

    expect(key.currentState, equals(keyState));
Ian Hickson's avatar
Ian Hickson committed
317
    expect(keyState.marker, equals('marked'));
318

319
    await tester.pumpWidget(Stack(
320
      textDirection: TextDirection.ltr,
321
      children: <Widget>[
322
        const SizedBox(width: 100.0, height: 100.0),
323
        StateMarker(key: key),
324
        const SizedBox(width: 100.0, height: 100.0),
325
      ],
326 327 328
    ));

    expect(key.currentState, equals(keyState));
Ian Hickson's avatar
Ian Hickson committed
329
    expect(keyState.marker, equals('marked'));
330
  });
331

332
  testWidgets('Deactivate implies build', (WidgetTester tester) async {
333
    final GlobalKey key = GlobalKey();
334
    final List<String> log = <String>[];
335
    final DeactivateLogger logger = DeactivateLogger(key: key, log: log);
336

337
    await tester.pumpWidget(
338
      Container(key: UniqueKey(), child: logger),
339
    );
340

341
    expect(log, equals(<String>['build']));
342

343
    await tester.pumpWidget(
344
      Container(key: UniqueKey(), child: logger),
345
    );
346

347
    expect(log, equals(<String>['build', 'deactivate', 'build']));
348
    log.clear();
349

350
    await tester.pump();
351
    expect(log, isEmpty);
352
  });
353 354

  testWidgets('Reparenting with multiple moves', (WidgetTester tester) async {
355 356 357
    final GlobalKey key1 = GlobalKey();
    final GlobalKey key2 = GlobalKey();
    final GlobalKey key3 = GlobalKey();
358 359

    await tester.pumpWidget(
360
      Row(
361
        textDirection: TextDirection.ltr,
362
        children: <Widget>[
363
          StateMarker(
364
            key: key1,
365
            child: StateMarker(
366
              key: key2,
367
              child: StateMarker(
368
                key: key3,
369 370 371 372 373
                child: StateMarker(child: Container(width: 100.0)),
              ),
            ),
          ),
        ],
374
      ),
375 376 377
    );

    await tester.pumpWidget(
378
      Row(
379
        textDirection: TextDirection.ltr,
380
        children: <Widget>[
381
          StateMarker(
382
            key: key2,
383
            child: StateMarker(child: Container(width: 100.0)),
384
          ),
385
          StateMarker(
386
            key: key1,
387
            child: StateMarker(
388
              key: key3,
389 390
              child: StateMarker(child: Container(width: 100.0)),
            ),
391
          ),
392
        ],
393
      ),
394 395
    );
  });
396
}