parent_data_test.dart 8.69 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));
38
      child = (decoratedBox.parentData as StackParentData).nextSibling;
Adam Barth's avatar
Adam Barth committed
39 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
    }
    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', () {
65
    testWidgets((WidgetTester tester) {
Adam Barth's avatar
Adam Barth committed
66

67
      tester.pumpWidget(
68
        new Stack(<Widget>[
69 70 71 72
          new DecoratedBox(decoration: kBoxDecorationA),
          new Positioned(
            top: 10.0,
            left: 10.0,
Adam Barth's avatar
Adam Barth committed
73
            child: new DecoratedBox(decoration: kBoxDecorationB)
74 75 76 77
          ),
          new DecoratedBox(decoration: kBoxDecorationC),
        ])
      );
Adam Barth's avatar
Adam Barth committed
78

79
      checkTree(tester, <TestParentData>[
80 81 82 83 84 85
        kNonPositioned,
        new TestParentData(top: 10.0, left: 10.0),
        kNonPositioned,
      ]);

      tester.pumpWidget(
86
        new Stack(<Widget>[
87 88 89 90 91 92 93 94 95 96 97 98 99
          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),
        ])
      );
Adam Barth's avatar
Adam Barth committed
100

101
      checkTree(tester, <TestParentData>[
102 103 104 105
        new TestParentData(bottom: 5.0, right: 7.0),
        new TestParentData(top: 10.0, left: 10.0),
        kNonPositioned,
      ]);
Adam Barth's avatar
Adam Barth committed
106

107 108 109
      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
110

111
      tester.pumpWidget(
112
        new Stack(<Widget>[
Adam Barth's avatar
Adam Barth committed
113
          new Positioned(
114 115 116 117 118 119 120 121 122 123 124 125 126
            bottom: 5.0,
            right: 7.0,
            child: kDecoratedBoxA
          ),
          new Positioned(
            top: 10.0,
            left: 10.0,
            child: kDecoratedBoxB
          ),
          kDecoratedBoxC,
        ])
      );

127
      checkTree(tester, <TestParentData>[
128 129 130 131 132 133
        new TestParentData(bottom: 5.0, right: 7.0),
        new TestParentData(top: 10.0, left: 10.0),
        kNonPositioned,
      ]);

      tester.pumpWidget(
134
        new Stack(<Widget>[
135 136 137 138 139 140 141 142 143 144 145 146 147 148
          new Positioned(
            bottom: 6.0,
            right: 8.0,
            child: kDecoratedBoxA
          ),
          new Positioned(
            left: 10.0,
            right: 10.0,
            child: kDecoratedBoxB
          ),
          kDecoratedBoxC,
        ])
      );

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

      tester.pumpWidget(
156
        new Stack(<Widget>[
157 158 159 160 161 162 163 164 165 166
          kDecoratedBoxA,
          new Positioned(
            left: 11.0,
            right: 12.0,
            child: new Container(child: kDecoratedBoxB)
          ),
          kDecoratedBoxC,
        ])
      );

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

      tester.pumpWidget(
174
        new Stack(<Widget>[
175 176 177 178 179 180 181 182 183 184
          kDecoratedBoxA,
          new Positioned(
            right: 10.0,
            child: new Container(child: kDecoratedBoxB)
          ),
          new Container(
            child: new Positioned(
              top: 8.0,
              child: kDecoratedBoxC
            )
Adam Barth's avatar
Adam Barth committed
185 186
          )
        ])
187
      );
Adam Barth's avatar
Adam Barth committed
188

189
      checkTree(tester, <TestParentData>[
190 191 192 193 194 195
        kNonPositioned,
        new TestParentData(right: 10.0),
        new TestParentData(top: 8.0),
      ]);

      tester.pumpWidget(
196
        new Stack(<Widget>[
197 198 199 200 201 202 203
          new Positioned(
            right: 10.0,
            child: new FlipComponent(left: kDecoratedBoxA, right: kDecoratedBoxB)
          ),
        ])
      );

204
      checkTree(tester, <TestParentData>[
205 206
        new TestParentData(right: 10.0),
      ]);
Adam Barth's avatar
Adam Barth committed
207

208 209
      flipStatefulComponent(tester);
      tester.pump();
Adam Barth's avatar
Adam Barth committed
210

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

      tester.pumpWidget(
216
        new Stack(<Widget>[
217 218 219 220 221 222 223
          new Positioned(
            top: 7.0,
            child: new FlipComponent(left: kDecoratedBoxA, right: kDecoratedBoxB)
          ),
        ])
      );

224
      checkTree(tester, <TestParentData>[
225 226 227 228 229 230
        new TestParentData(top: 7.0),
      ]);

      flipStatefulComponent(tester);
      tester.pump();

231
      checkTree(tester, <TestParentData>[
232 233 234 235
        new TestParentData(top: 7.0),
      ]);

      tester.pumpWidget(
236
        new Stack(<Widget>[])
237 238
      );

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

243 244 245 246 247
  test('ParentDataWidget conflicting data', () {
    testWidgets((WidgetTester tester) {
      expect(cachedException, isNull);

      tester.pumpWidget(
248
        new Stack(<Widget>[
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
          new Positioned(
            top: 5.0,
            bottom: 8.0,
            child: new Positioned(
              top: 6.0,
              left: 7.0,
              child: new DecoratedBox(decoration: kBoxDecorationB)
            )
          )
        ])
      );

      expect(cachedException, isNotNull);
      cachedException = null;

264
      tester.pumpWidget(new Stack(<Widget>[]));
265

266
      checkTree(tester, <TestParentData>[]);
267 268 269 270
      expect(cachedException, isNull);

      tester.pumpWidget(
        new Container(
271
          child: new Flex(<Widget>[
272 273 274 275 276 277 278 279 280 281 282 283 284
            new Positioned(
              top: 6.0,
              left: 7.0,
              child: new DecoratedBox(decoration: kBoxDecorationB)
            )
          ])
        )
      );

      expect(cachedException, isNotNull);
      cachedException = null;

      tester.pumpWidget(
285
        new Stack(<Widget>[])
286 287
      );

288
      checkTree(tester, <TestParentData>[]);
289 290
    });
  });
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341

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

      tester.pumpWidget(
        new Stack(<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),
      ]);

      tester.pumpWidget(
        new Stack(<Widget>[
          new Positioned(
            top: 10.0,
            left: 10.0,
            child: new DecoratedBox(
              decoration: kBoxDecorationB,
              child: new DecoratedBox(key: key, decoration: kBoxDecorationA)
            )
          )
        ])
      );

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

      tester.pumpWidget(
        new Stack(<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),
      ]);
    });
  });
Adam Barth's avatar
Adam Barth committed
342
}