parent_data_test.dart 15 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
Hixie's avatar
Hixie committed
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';
6 7
import 'package:flutter/rendering.dart';
import 'package:flutter/widgets.dart';
8
import 'package:flutter_test/flutter_test.dart';
Adam Barth's avatar
Adam Barth committed
9 10 11 12 13 14

import 'test_widgets.dart';

class TestParentData {
  TestParentData({ this.top, this.right, this.bottom, this.left });

15 16 17 18
  final double? top;
  final double? right;
  final double? bottom;
  final double? left;
Adam Barth's avatar
Adam Barth committed
19 20 21
}

void checkTree(WidgetTester tester, List<TestParentData> expectedParentData) {
22
  final MultiChildRenderObjectElement element = tester.element(
23
    find.byElementPredicate((Element element) => element is MultiChildRenderObjectElement),
24
  );
Adam Barth's avatar
Adam Barth committed
25
  expect(element, isNotNull);
Dan Field's avatar
Dan Field committed
26
  expect(element.renderObject, isA<RenderStack>());
27
  final RenderStack renderObject = element.renderObject as RenderStack;
Adam Barth's avatar
Adam Barth committed
28
  try {
29
    RenderObject? child = renderObject.firstChild;
30
    for (final TestParentData expected in expectedParentData) {
Dan Field's avatar
Dan Field committed
31
      expect(child, isA<RenderDecoratedBox>());
32
      final RenderDecoratedBox decoratedBox = child! as RenderDecoratedBox;
Dan Field's avatar
Dan Field committed
33
      expect(decoratedBox.parentData, isA<StackParentData>());
34
      final StackParentData parentData = decoratedBox.parentData! as StackParentData;
Adam Barth's avatar
Adam Barth committed
35 36 37 38
      expect(parentData.top, equals(expected.top));
      expect(parentData.right, equals(expected.right));
      expect(parentData.bottom, equals(expected.bottom));
      expect(parentData.left, equals(expected.left));
39 40
      final StackParentData? decoratedBoxParentData = decoratedBox.parentData as StackParentData?;
      child = decoratedBoxParentData?.nextSibling;
Adam Barth's avatar
Adam Barth committed
41 42 43
    }
    expect(child, isNull);
  } catch (e) {
44
    debugPrint(renderObject.toStringDeep());
Adam Barth's avatar
Adam Barth committed
45 46 47 48
    rethrow;
  }
}

49
final TestParentData kNonPositioned = TestParentData();
Adam Barth's avatar
Adam Barth committed
50 51

void main() {
52 53
  testWidgets('ParentDataWidget control test', (WidgetTester tester) async {
    await tester.pumpWidget(
54
      const Stack(
55
        textDirection: TextDirection.ltr,
56
        children: <Widget>[
57 58
          DecoratedBox(decoration: kBoxDecorationA),
          Positioned(
59 60
            top: 10.0,
            left: 10.0,
61
            child: DecoratedBox(decoration: kBoxDecorationB),
62
          ),
63
          DecoratedBox(decoration: kBoxDecorationC),
64 65
        ],
      ),
66 67 68 69
    );

    checkTree(tester, <TestParentData>[
      kNonPositioned,
70
      TestParentData(top: 10.0, left: 10.0),
71 72 73
      kNonPositioned,
    ]);

74
    await tester.pumpWidget(
75
      const Stack(
76
        textDirection: TextDirection.ltr,
77
        children: <Widget>[
78
          Positioned(
79 80
            bottom: 5.0,
            right: 7.0,
81
            child: DecoratedBox(decoration: kBoxDecorationA),
82
          ),
83
          Positioned(
84 85
            top: 10.0,
            left: 10.0,
86
            child: DecoratedBox(decoration: kBoxDecorationB),
87
          ),
88
          DecoratedBox(decoration: kBoxDecorationC),
89 90
        ],
      ),
91 92 93
    );

    checkTree(tester, <TestParentData>[
94 95
      TestParentData(bottom: 5.0, right: 7.0),
      TestParentData(top: 10.0, left: 10.0),
96 97 98
      kNonPositioned,
    ]);

99 100 101
    const DecoratedBox kDecoratedBoxA = DecoratedBox(decoration: kBoxDecorationA);
    const DecoratedBox kDecoratedBoxB = DecoratedBox(decoration: kBoxDecorationB);
    const DecoratedBox kDecoratedBoxC = DecoratedBox(decoration: kBoxDecorationC);
102

103
    await tester.pumpWidget(
104
      const Stack(
105
        textDirection: TextDirection.ltr,
106
        children: <Widget>[
107
          Positioned(
108 109
            bottom: 5.0,
            right: 7.0,
110
            child: kDecoratedBoxA,
111
          ),
112
          Positioned(
113 114
            top: 10.0,
            left: 10.0,
115
            child: kDecoratedBoxB,
116 117
          ),
          kDecoratedBoxC,
118 119
        ],
      ),
120 121 122
    );

    checkTree(tester, <TestParentData>[
123 124
      TestParentData(bottom: 5.0, right: 7.0),
      TestParentData(top: 10.0, left: 10.0),
125 126 127
      kNonPositioned,
    ]);

128
    await tester.pumpWidget(
129
      const Stack(
130
        textDirection: TextDirection.ltr,
131
        children: <Widget>[
132
          Positioned(
133 134
            bottom: 6.0,
            right: 8.0,
135
            child: kDecoratedBoxA,
136
          ),
137
          Positioned(
138 139
            left: 10.0,
            right: 10.0,
140
            child: kDecoratedBoxB,
141 142
          ),
          kDecoratedBoxC,
143 144
        ],
      ),
145 146 147
    );

    checkTree(tester, <TestParentData>[
148 149
      TestParentData(bottom: 6.0, right: 8.0),
      TestParentData(left: 10.0, right: 10.0),
150 151 152
      kNonPositioned,
    ]);

153
    await tester.pumpWidget(
154
      Stack(
155
        textDirection: TextDirection.ltr,
156 157
        children: <Widget>[
          kDecoratedBoxA,
158
          Positioned(
159 160
            left: 11.0,
            right: 12.0,
161
            child: Container(child: kDecoratedBoxB),
162 163
          ),
          kDecoratedBoxC,
164 165
        ],
      ),
166 167 168 169
    );

    checkTree(tester, <TestParentData>[
      kNonPositioned,
170
      TestParentData(left: 11.0, right: 12.0),
171 172 173
      kNonPositioned,
    ]);

174
    await tester.pumpWidget(
175
      Stack(
176
        textDirection: TextDirection.ltr,
177 178
        children: <Widget>[
          kDecoratedBoxA,
179
          Positioned(
180
            right: 10.0,
181
            child: Container(child: kDecoratedBoxB),
182
          ),
183 184
          const DummyWidget(
            child: Positioned(
185
              top: 8.0,
186 187 188 189 190
              child: kDecoratedBoxC,
            ),
          ),
        ],
      ),
191 192 193 194
    );

    checkTree(tester, <TestParentData>[
      kNonPositioned,
195 196
      TestParentData(right: 10.0),
      TestParentData(top: 8.0),
197 198
    ]);

199
    await tester.pumpWidget(
200
      const Stack(
201
        textDirection: TextDirection.ltr,
202
        children: <Widget>[
203
          Positioned(
204
            right: 10.0,
205
            child: FlipWidget(left: kDecoratedBoxA, right: kDecoratedBoxB),
206
          ),
207 208
        ],
      ),
209 210 211
    );

    checkTree(tester, <TestParentData>[
212
      TestParentData(right: 10.0),
213 214 215
    ]);

    flipStatefulWidget(tester);
216
    await tester.pump();
217 218

    checkTree(tester, <TestParentData>[
219
      TestParentData(right: 10.0),
220 221
    ]);

222
    await tester.pumpWidget(
223
      const Stack(
224
        textDirection: TextDirection.ltr,
225
        children: <Widget>[
226
          Positioned(
227
            top: 7.0,
228
            child: FlipWidget(left: kDecoratedBoxA, right: kDecoratedBoxB),
229
          ),
230 231
        ],
      ),
232 233 234
    );

    checkTree(tester, <TestParentData>[
235
      TestParentData(top: 7.0),
236 237 238
    ]);

    flipStatefulWidget(tester);
239
    await tester.pump();
240 241

    checkTree(tester, <TestParentData>[
242
      TestParentData(top: 7.0),
243 244
    ]);

245
    await tester.pumpWidget(
246
      const Stack(textDirection: TextDirection.ltr),
247 248 249
    );

    checkTree(tester, <TestParentData>[]);
Adam Barth's avatar
Adam Barth committed
250 251
  });

252 253
  testWidgets('ParentDataWidget conflicting data', (WidgetTester tester) async {
    await tester.pumpWidget(
254
      const Directionality(
255
        textDirection: TextDirection.ltr,
256 257
        child: Stack(
          textDirection: TextDirection.ltr,
258
          children: <Widget>[
259 260 261 262 263 264 265 266
            Positioned(
              top: 5.0,
              bottom: 8.0,
              child: Positioned(
                top: 6.0,
                left: 7.0,
                child: DecoratedBox(decoration: kBoxDecorationB),
              ),
267
            ),
268 269
          ],
        ),
270
      ),
271
    );
272

273 274 275 276
    dynamic exception = tester.takeException();
    expect(exception, isFlutterError);
    expect(
      exception.toString(),
277
      startsWith(
278
        'Incorrect use of ParentDataWidget.\n'
279 280 281 282 283 284 285
        'The following ParentDataWidgets are providing parent data to the same RenderObject:\n'
        '- Positioned(left: 7.0, top: 6.0) (typically placed directly inside a Stack widget)\n'
        '- Positioned(top: 5.0, bottom: 8.0) (typically placed directly inside a Stack widget)\n'
        'However, a RenderObject can only receive parent data from at most one ParentDataWidget.\n'
        'Usually, this indicates that at least one of the offending ParentDataWidgets listed '
        'above is not placed directly inside a compatible ancestor widget.\n'
        'The ownership chain for the RenderObject that received the parent data was:\n'
286
        '  DecoratedBox ← Positioned ← Positioned ← Stack ← Directionality ← ', // End of chain omitted, not relevant for test.
287 288
      ),
    );
289

290
    await tester.pumpWidget(const Stack(textDirection: TextDirection.ltr));
291

292
    checkTree(tester, <TestParentData>[]);
293

294
    await tester.pumpWidget(
295
      const Directionality(
296
        textDirection: TextDirection.ltr,
297
        child: DummyWidget(
298
          child: Row(
299
            children: <Widget>[
300 301 302 303 304 305 306
              Positioned(
                top: 6.0,
                left: 7.0,
                child: DecoratedBox(decoration: kBoxDecorationB),
              ),
            ],
          ),
307 308
        ),
      ),
309
    );
310 311 312 313
    exception = tester.takeException();
    expect(exception, isFlutterError);
    expect(
      exception.toString(),
314
      startsWith(
315
        'Incorrect use of ParentDataWidget.\n'
316 317 318 319 320 321 322
        'The ParentDataWidget Positioned(left: 7.0, top: 6.0) wants to apply ParentData of type '
        'StackParentData to a RenderObject, which has been set up to accept ParentData of '
        'incompatible type FlexParentData.\n'
        'Usually, this means that the Positioned widget has the wrong ancestor RenderObjectWidget. '
        'Typically, Positioned widgets are placed directly inside Stack widgets.\n'
        'The offending Positioned is currently placed inside a Row widget.\n'
        'The ownership chain for the RenderObject that received the incompatible parent data was:\n'
323
        '  DecoratedBox ← Positioned ← Row ← DummyWidget ← Directionality ← ', // End of chain omitted, not relevant for test.
324
      ),
325
    );
326

327
    await tester.pumpWidget(
328
      const Stack(textDirection: TextDirection.ltr),
329
    );
330

331 332
    checkTree(tester, <TestParentData>[]);
  });
333

334
  testWidgets('ParentDataWidget interacts with global keys', (WidgetTester tester) async {
335
    final GlobalKey key = GlobalKey();
336

337
    await tester.pumpWidget(
338
      Stack(
339
        textDirection: TextDirection.ltr,
340
        children: <Widget>[
341
          Positioned(
342 343
            top: 10.0,
            left: 10.0,
344
            child: DecoratedBox(key: key, decoration: kBoxDecorationA),
345 346 347
          ),
        ],
      ),
348 349 350
    );

    checkTree(tester, <TestParentData>[
351
      TestParentData(top: 10.0, left: 10.0),
352 353
    ]);

354
    await tester.pumpWidget(
355
      Stack(
356
        textDirection: TextDirection.ltr,
357
        children: <Widget>[
358
          Positioned(
359 360
            top: 10.0,
            left: 10.0,
361
            child: DecoratedBox(
362
              decoration: kBoxDecorationB,
363
              child: DecoratedBox(key: key, decoration: kBoxDecorationA),
364 365 366 367
            ),
          ),
        ],
      ),
368 369 370
    );

    checkTree(tester, <TestParentData>[
371
      TestParentData(top: 10.0, left: 10.0),
372 373
    ]);

374
    await tester.pumpWidget(
375
      Stack(
376
        textDirection: TextDirection.ltr,
377
        children: <Widget>[
378
          Positioned(
379 380
            top: 10.0,
            left: 10.0,
381
            child: DecoratedBox(key: key, decoration: kBoxDecorationA),
382 383 384
          ),
        ],
      ),
385
    );
386

387
    checkTree(tester, <TestParentData>[
388
      TestParentData(top: 10.0, left: 10.0),
389
    ]);
390
  });
391 392

  testWidgets('Parent data invalid ancestor', (WidgetTester tester) async {
393 394 395 396 397 398 399 400 401 402 403 404 405 406
    await tester.pumpWidget(Directionality(
      textDirection: TextDirection.ltr,
      child: Row(
        children: <Widget>[
          Stack(
            textDirection: TextDirection.ltr,
            children: <Widget>[
              Expanded(
                child: Container(),
              ),
            ],
          ),
        ],
      ),
407 408
    ));

409 410 411 412
    final dynamic exception = tester.takeException();
    expect(exception, isFlutterError);
    expect(
      exception.toString(),
413
      startsWith(
414
        'Incorrect use of ParentDataWidget.\n'
415 416 417 418 419 420 421
        'The ParentDataWidget Expanded(flex: 1) wants to apply ParentData of type '
        'FlexParentData to a RenderObject, which has been set up to accept ParentData of '
        'incompatible type StackParentData.\n'
        'Usually, this means that the Expanded widget has the wrong ancestor RenderObjectWidget. '
        'Typically, Expanded widgets are placed directly inside Flex widgets.\n'
        'The offending Expanded is currently placed inside a Stack widget.\n'
        'The ownership chain for the RenderObject that received the incompatible parent data was:\n'
422
        '  LimitedBox ← Container ← Expanded ← Stack ← Row ← Directionality ← ', // Omitted end of debugCreator chain because it's irrelevant for test.
423 424
      ),
    );
425
  });
426 427 428 429 430 431 432

  testWidgets('ParentDataWidget can be used with different ancestor RenderObjectWidgets', (WidgetTester tester) async {
    await tester.pumpWidget(
      OneAncestorWidget(
        child: Container(),
      ),
    );
433
    DummyParentData parentData = tester.renderObject(find.byType(Container)).parentData! as DummyParentData;
434 435 436 437 438 439 440 441 442 443
    expect(parentData.string, isNull);

    await tester.pumpWidget(
      OneAncestorWidget(
        child: TestParentDataWidget(
          string: 'Foo',
          child: Container(),
        ),
      ),
    );
444
    parentData = tester.renderObject(find.byType(Container)).parentData! as DummyParentData;
445 446 447 448 449 450 451 452 453 454
    expect(parentData.string, 'Foo');

    await tester.pumpWidget(
      AnotherAncestorWidget(
        child: TestParentDataWidget(
          string: 'Bar',
          child: Container(),
        ),
      ),
    );
455
    parentData = tester.renderObject(find.byType(Container)).parentData! as DummyParentData;
456 457 458 459 460 461
    expect(parentData.string, 'Bar');
  });
}

class TestParentDataWidget extends ParentDataWidget<DummyParentData> {
  const TestParentDataWidget({
462
    super.key,
463
    required this.string,
464 465
    required super.child,
  });
466 467 468 469 470 471

  final String string;

  @override
  void applyParentData(RenderObject renderObject) {
    assert(renderObject.parentData is DummyParentData);
472
    final DummyParentData parentData = renderObject.parentData! as DummyParentData;
473 474 475 476 477 478 479 480
    parentData.string = string;
  }

  @override
  Type get debugTypicalAncestorWidgetClass => OneAncestorWidget;
}

class DummyParentData extends ParentData {
481
  String? string;
482 483 484 485
}

class OneAncestorWidget extends SingleChildRenderObjectWidget {
  const OneAncestorWidget({
486 487 488
    super.key,
    required Widget super.child,
  });
489 490 491 492 493 494 495

  @override
  RenderOne createRenderObject(BuildContext context) => RenderOne();
}

class AnotherAncestorWidget extends SingleChildRenderObjectWidget {
  const AnotherAncestorWidget({
496 497 498
    super.key,
    required Widget super.child,
  });
499 500 501 502 503 504 505 506

  @override
  RenderAnother createRenderObject(BuildContext context) => RenderAnother();
}

class RenderOne extends RenderProxyBox {
  @override
  void setupParentData(RenderBox child) {
507
    if (child.parentData is! DummyParentData) {
508
      child.parentData = DummyParentData();
509
    }
510 511 512 513 514 515
  }
}

class RenderAnother extends RenderProxyBox {
  @override
  void setupParentData(RenderBox child) {
516
    if (child.parentData is! DummyParentData) {
517
      child.parentData = DummyParentData();
518
    }
519
  }
Adam Barth's avatar
Adam Barth committed
520
}
521 522

class DummyWidget extends StatelessWidget {
523
  const DummyWidget({ super.key, required this.child });
524 525 526 527 528 529

  final Widget child;

  @override
  Widget build(BuildContext context) => child;
}