parent_data_test.dart 8.85 KB
Newer Older
Hixie's avatar
Hixie committed
1 2 3 4
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

Adam Barth's avatar
Adam Barth committed
5
import 'package:flutter_test/flutter_test.dart';
6 7
import 'package:flutter/rendering.dart';
import 'package:flutter/widgets.dart';
Adam Barth's avatar
Adam Barth committed
8 9 10 11 12 13 14 15 16 17 18 19 20

import 'test_widgets.dart';

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

  final double top;
  final double right;
  final double bottom;
  final double left;
}

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

final TestParentData kNonPositioned = new TestParentData();

void main() {
51
  testWidgets('ParentDataWidget control test', (WidgetTester tester) async {
52

53
    await tester.pumpWidget(
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
      new Stack(
        children: <Widget>[
          new DecoratedBox(decoration: kBoxDecorationA),
          new Positioned(
            top: 10.0,
            left: 10.0,
            child: new DecoratedBox(decoration: kBoxDecorationB)
          ),
          new DecoratedBox(decoration: kBoxDecorationC),
        ]
      )
    );

    checkTree(tester, <TestParentData>[
      kNonPositioned,
      new TestParentData(top: 10.0, left: 10.0),
      kNonPositioned,
    ]);

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

    checkTree(tester, <TestParentData>[
      new TestParentData(bottom: 5.0, right: 7.0),
      new TestParentData(top: 10.0, left: 10.0),
      kNonPositioned,
    ]);

97 98 99
    final DecoratedBox kDecoratedBoxA = new DecoratedBox(decoration: kBoxDecorationA);
    final DecoratedBox kDecoratedBoxB = new DecoratedBox(decoration: kBoxDecorationB);
    final DecoratedBox kDecoratedBoxC = new DecoratedBox(decoration: kBoxDecorationC);
100

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

    checkTree(tester, <TestParentData>[
      new TestParentData(bottom: 5.0, right: 7.0),
      new TestParentData(top: 10.0, left: 10.0),
      kNonPositioned,
    ]);

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

    checkTree(tester, <TestParentData>[
      new TestParentData(bottom: 6.0, right: 8.0),
      new TestParentData(left: 10.0, right: 10.0),
      kNonPositioned,
    ]);

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

    checkTree(tester, <TestParentData>[
      kNonPositioned,
      new TestParentData(left: 11.0, right: 12.0),
      kNonPositioned,
    ]);

169
    await tester.pumpWidget(
170 171 172 173 174 175 176 177 178 179 180
      new Stack(
        children: <Widget>[
          kDecoratedBoxA,
          new Positioned(
            right: 10.0,
            child: new Container(child: kDecoratedBoxB)
          ),
          new Container(
            child: new Positioned(
              top: 8.0,
              child: kDecoratedBoxC
181
            )
182 183 184 185 186 187 188 189 190 191 192
          )
        ]
      )
    );

    checkTree(tester, <TestParentData>[
      kNonPositioned,
      new TestParentData(right: 10.0),
      new TestParentData(top: 8.0),
    ]);

193
    await tester.pumpWidget(
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
      new Stack(
        children: <Widget>[
          new Positioned(
            right: 10.0,
            child: new FlipWidget(left: kDecoratedBoxA, right: kDecoratedBoxB)
          ),
        ]
      )
    );

    checkTree(tester, <TestParentData>[
      new TestParentData(right: 10.0),
    ]);

    flipStatefulWidget(tester);
209
    await tester.pump();
210 211 212 213 214

    checkTree(tester, <TestParentData>[
      new TestParentData(right: 10.0),
    ]);

215
    await tester.pumpWidget(
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
      new Stack(
        children: <Widget>[
          new Positioned(
            top: 7.0,
            child: new FlipWidget(left: kDecoratedBoxA, right: kDecoratedBoxB)
          ),
        ]
      )
    );

    checkTree(tester, <TestParentData>[
      new TestParentData(top: 7.0),
    ]);

    flipStatefulWidget(tester);
231
    await tester.pump();
232 233 234 235 236

    checkTree(tester, <TestParentData>[
      new TestParentData(top: 7.0),
    ]);

237
    await tester.pumpWidget(
238 239 240 241
      new Stack()
    );

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

244 245
  testWidgets('ParentDataWidget conflicting data', (WidgetTester tester) async {
    await tester.pumpWidget(
246 247 248 249 250 251 252 253 254
      new Stack(
        children: <Widget>[
          new Positioned(
            top: 5.0,
            bottom: 8.0,
            child: new Positioned(
              top: 6.0,
              left: 7.0,
              child: new DecoratedBox(decoration: kBoxDecorationB)
255
            )
256
          )
257 258 259
        ]
      )
    );
260
    expect(tester.takeException(), isFlutterError);
261

262
    await tester.pumpWidget(new Stack());
263

264
    checkTree(tester, <TestParentData>[]);
265

266
    await tester.pumpWidget(
267
      new Container(
268
        child: new Row(
269 270
          children: <Widget>[
            new Positioned(
271 272 273
              top: 6.0,
              left: 7.0,
              child: new DecoratedBox(decoration: kBoxDecorationB)
274 275 276
            )
          ]
        )
277 278
      )
    );
279
    expect(tester.takeException(), isFlutterError);
280

281
    await tester.pumpWidget(
282 283
      new Stack()
    );
284

285 286
    checkTree(tester, <TestParentData>[]);
  });
287

288
  testWidgets('ParentDataWidget interacts with global keys', (WidgetTester tester) async {
289
    final GlobalKey key = new GlobalKey();
290

291
    await tester.pumpWidget(
292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
      new Stack(
        children: <Widget>[
          new Positioned(
            top: 10.0,
            left: 10.0,
            child: new DecoratedBox(key: key, decoration: kBoxDecorationA)
          )
        ]
      )
    );

    checkTree(tester, <TestParentData>[
      new TestParentData(top: 10.0, left: 10.0),
    ]);

307
    await tester.pumpWidget(
308 309 310 311 312 313 314
      new Stack(
        children: <Widget>[
          new Positioned(
            top: 10.0,
            left: 10.0,
            child: new DecoratedBox(
              decoration: kBoxDecorationB,
315 316
              child: new DecoratedBox(key: key, decoration: kBoxDecorationA)
            )
317 318 319 320 321 322 323 324 325
          )
        ]
      )
    );

    checkTree(tester, <TestParentData>[
      new TestParentData(top: 10.0, left: 10.0),
    ]);

326
    await tester.pumpWidget(
327 328 329 330 331 332 333 334 335 336
      new Stack(
        children: <Widget>[
          new Positioned(
            top: 10.0,
            left: 10.0,
            child: new DecoratedBox(key: key, decoration: kBoxDecorationA)
          )
        ]
      )
    );
337

338 339 340
    checkTree(tester, <TestParentData>[
      new TestParentData(top: 10.0, left: 10.0),
    ]);
341
  });
342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357

  testWidgets('Parent data invalid ancestor', (WidgetTester tester) async {
    await tester.pumpWidget(new Row(
      children: <Widget>[
        new Stack(
          children: <Widget>[
            new Expanded(
              child: new Container()
            ),
          ],
        ),
      ],
    ));

    expect(tester.takeException(), isFlutterError);
  });
Adam Barth's avatar
Adam Barth committed
358
}