parent_data_test.dart 9.3 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 21 22
import 'package:test/test.dart';

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) {
  MultiChildRenderObjectElement element =
23
      tester.findElement((Element element) => element is MultiChildRenderObjectElement);
Adam Barth's avatar
Adam Barth committed
24 25 26 27 28 29 30 31 32 33 34 35 36 37
  expect(element, isNotNull);
  expect(element.renderObject is RenderStack, isTrue);
  RenderStack renderObject = element.renderObject;
  try {
    RenderObject child = renderObject.firstChild;
    for (TestParentData expected in expectedParentData) {
      expect(child is RenderDecoratedBox, isTrue);
      RenderDecoratedBox decoratedBox = child;
      expect(decoratedBox.parentData is StackParentData, isTrue);
      StackParentData parentData = decoratedBox.parentData;
      expect(parentData.top, equals(expected.top));
      expect(parentData.right, equals(expected.right));
      expect(parentData.bottom, equals(expected.bottom));
      expect(parentData.left, equals(expected.left));
Hixie's avatar
Hixie committed
38 39
      StackParentData decoratedBoxParentData = decoratedBox.parentData;
      child = decoratedBoxParentData.nextSibling;
Adam Barth's avatar
Adam Barth committed
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
    }
    expect(child, isNull);
  } catch (e) {
    print(renderObject.toStringDeep());
    rethrow;
  }
}

final TestParentData kNonPositioned = new TestParentData();

void main() {
  dynamic cachedException;

  setUp(() {
    assert(cachedException == null);
    debugWidgetsExceptionHandler = (String context, dynamic exception, StackTrace stack) {
      cachedException = exception;
    };
  });

  tearDown(() {
    cachedException = null;
    debugWidgetsExceptionHandler = null;
  });

  test('ParentDataWidget control test', () {
66
    testWidgets((WidgetTester tester) {
Adam Barth's avatar
Adam Barth committed
67

68
      tester.pumpWidget(
69 70 71 72 73 74 75 76 77 78 79
        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),
          ]
        )
80
      );
Adam Barth's avatar
Adam Barth committed
81

82
      checkTree(tester, <TestParentData>[
83 84 85 86 87 88
        kNonPositioned,
        new TestParentData(top: 10.0, left: 10.0),
        kNonPositioned,
      ]);

      tester.pumpWidget(
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
        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),
          ]
        )
104
      );
Adam Barth's avatar
Adam Barth committed
105

106
      checkTree(tester, <TestParentData>[
107 108 109 110
        new TestParentData(bottom: 5.0, right: 7.0),
        new TestParentData(top: 10.0, left: 10.0),
        kNonPositioned,
      ]);
Adam Barth's avatar
Adam Barth committed
111

112 113 114
      DecoratedBox kDecoratedBoxA = new DecoratedBox(decoration: kBoxDecorationA);
      DecoratedBox kDecoratedBoxB = new DecoratedBox(decoration: kBoxDecorationB);
      DecoratedBox kDecoratedBoxC = new DecoratedBox(decoration: kBoxDecorationC);
Adam Barth's avatar
Adam Barth committed
115

116
      tester.pumpWidget(
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
        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,
          ]
        )
132 133
      );

134
      checkTree(tester, <TestParentData>[
135 136 137 138 139 140
        new TestParentData(bottom: 5.0, right: 7.0),
        new TestParentData(top: 10.0, left: 10.0),
        kNonPositioned,
      ]);

      tester.pumpWidget(
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
        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,
          ]
        )
156 157
      );

158
      checkTree(tester, <TestParentData>[
159 160 161 162 163 164
        new TestParentData(bottom: 6.0, right: 8.0),
        new TestParentData(left: 10.0, right: 10.0),
        kNonPositioned,
      ]);

      tester.pumpWidget(
165 166 167 168 169 170 171 172 173 174 175
        new Stack(
          children: <Widget>[
            kDecoratedBoxA,
            new Positioned(
              left: 11.0,
              right: 12.0,
              child: new Container(child: kDecoratedBoxB)
            ),
            kDecoratedBoxC,
          ]
        )
176 177
      );

178
      checkTree(tester, <TestParentData>[
179 180 181 182 183 184
        kNonPositioned,
        new TestParentData(left: 11.0, right: 12.0),
        kNonPositioned,
      ]);

      tester.pumpWidget(
185 186 187 188 189 190 191 192 193 194 195 196
        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
              )
197
            )
198 199
          ]
        )
200
      );
Adam Barth's avatar
Adam Barth committed
201

202
      checkTree(tester, <TestParentData>[
203 204 205 206 207 208
        kNonPositioned,
        new TestParentData(right: 10.0),
        new TestParentData(top: 8.0),
      ]);

      tester.pumpWidget(
209 210 211 212
        new Stack(
          children: <Widget>[
            new Positioned(
              right: 10.0,
213
              child: new FlipWidget(left: kDecoratedBoxA, right: kDecoratedBoxB)
214 215 216
            ),
          ]
        )
217 218
      );

219
      checkTree(tester, <TestParentData>[
220 221
        new TestParentData(right: 10.0),
      ]);
Adam Barth's avatar
Adam Barth committed
222

223
      flipStatefulWidget(tester);
224
      tester.pump();
Adam Barth's avatar
Adam Barth committed
225

226
      checkTree(tester, <TestParentData>[
227 228 229 230
        new TestParentData(right: 10.0),
      ]);

      tester.pumpWidget(
231 232 233 234
        new Stack(
          children: <Widget>[
            new Positioned(
              top: 7.0,
235
              child: new FlipWidget(left: kDecoratedBoxA, right: kDecoratedBoxB)
236 237 238
            ),
          ]
        )
239 240
      );

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

245
      flipStatefulWidget(tester);
246 247
      tester.pump();

248
      checkTree(tester, <TestParentData>[
249 250 251 252
        new TestParentData(top: 7.0),
      ]);

      tester.pumpWidget(
253
        new Stack()
254 255
      );

256
      checkTree(tester, <TestParentData>[]);
257
    });
Adam Barth's avatar
Adam Barth committed
258 259
  });

260 261 262 263 264
  test('ParentDataWidget conflicting data', () {
    testWidgets((WidgetTester tester) {
      expect(cachedException, isNull);

      tester.pumpWidget(
265 266 267 268 269 270 271 272 273 274
        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)
              )
275
            )
276 277
          ]
        )
278 279 280 281 282
      );

      expect(cachedException, isNotNull);
      cachedException = null;

283
      tester.pumpWidget(new Stack());
284

285
      checkTree(tester, <TestParentData>[]);
286 287 288 289
      expect(cachedException, isNull);

      tester.pumpWidget(
        new Container(
290 291 292 293 294 295 296 297 298
          child: new Flex(
            children: <Widget>[
              new Positioned(
                top: 6.0,
                left: 7.0,
                child: new DecoratedBox(decoration: kBoxDecorationB)
              )
            ]
          )
299 300 301 302 303 304 305
        )
      );

      expect(cachedException, isNotNull);
      cachedException = null;

      tester.pumpWidget(
306
        new Stack()
307 308
      );

309
      checkTree(tester, <TestParentData>[]);
310 311
    });
  });
312 313 314 315 316 317

  test('ParentDataWidget interacts with global keys', () {
    testWidgets((WidgetTester tester) {
      GlobalKey key = new GlobalKey();

      tester.pumpWidget(
318 319 320 321 322 323 324 325 326
        new Stack(
          children: <Widget>[
            new Positioned(
              top: 10.0,
              left: 10.0,
              child: new DecoratedBox(key: key, decoration: kBoxDecorationA)
            )
          ]
        )
327 328 329 330 331 332 333
      );

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

      tester.pumpWidget(
334 335 336 337 338 339 340 341 342
        new Stack(
          children: <Widget>[
            new Positioned(
              top: 10.0,
              left: 10.0,
              child: new DecoratedBox(
                decoration: kBoxDecorationB,
                child: new DecoratedBox(key: key, decoration: kBoxDecorationA)
              )
343
            )
344 345
          ]
        )
346 347 348 349 350 351 352
      );

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

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

      checkTree(tester, <TestParentData>[
        new TestParentData(top: 10.0, left: 10.0),
      ]);
    });
  });
Adam Barth's avatar
Adam Barth committed
369
}