multichild_test.dart 9.66 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';
8 9 10 11 12 13
import 'package:test/test.dart';

import 'test_widgets.dart';

void checkTree(WidgetTester tester, List<BoxDecoration> expectedDecorations) {
  MultiChildRenderObjectElement element =
14
      tester.findElement((Element element) => element is MultiChildRenderObjectElement);
15 16 17 18 19 20 21 22 23
  expect(element, isNotNull);
  expect(element.renderObject is RenderStack, isTrue);
  RenderStack renderObject = element.renderObject;
  try {
    RenderObject child = renderObject.firstChild;
    for (BoxDecoration decoration in expectedDecorations) {
      expect(child is RenderDecoratedBox, isTrue);
      RenderDecoratedBox decoratedBox = child;
      expect(decoratedBox.decoration, equals(decoration));
24 25
      final StackParentData decoratedBoxParentData = decoratedBox.parentData;
      child = decoratedBoxParentData.nextSibling;
26 27 28 29 30 31 32 33 34 35
    }
    expect(child, isNull);
  } catch (e) {
    print(renderObject.toStringDeep());
    rethrow;
  }
}

void main() {
  test('MultiChildRenderObjectElement control test', () {
36 37 38
    testWidgets((WidgetTester tester) {

      tester.pumpWidget(
39 40 41 42 43 44 45
        new Stack(
          children: <Widget>[
            new DecoratedBox(decoration: kBoxDecorationA),
            new DecoratedBox(decoration: kBoxDecorationB),
            new DecoratedBox(decoration: kBoxDecorationC),
          ]
        )
46 47
      );

48
      checkTree(tester, <BoxDecoration>[kBoxDecorationA, kBoxDecorationB, kBoxDecorationC]);
49 50

      tester.pumpWidget(
51 52 53 54 55 56
        new Stack(
          children: <Widget>[
            new DecoratedBox(decoration: kBoxDecorationA),
            new DecoratedBox(decoration: kBoxDecorationC),
          ]
        )
57 58
      );

59
      checkTree(tester, <BoxDecoration>[kBoxDecorationA, kBoxDecorationC]);
60 61

      tester.pumpWidget(
62 63 64 65 66 67 68
        new Stack(
          children: <Widget>[
            new DecoratedBox(decoration: kBoxDecorationA),
            new DecoratedBox(key: new Key('b'), decoration: kBoxDecorationB),
            new DecoratedBox(decoration: kBoxDecorationC),
          ]
        )
69 70
      );

71
      checkTree(tester, <BoxDecoration>[kBoxDecorationA, kBoxDecorationB, kBoxDecorationC]);
72 73

      tester.pumpWidget(
74 75 76 77 78 79 80
        new Stack(
          children: <Widget>[
            new DecoratedBox(key: new Key('b'), decoration: kBoxDecorationB),
            new DecoratedBox(decoration: kBoxDecorationC),
            new DecoratedBox(key: new Key('a'), decoration: kBoxDecorationA),
          ]
        )
81 82
      );

83
      checkTree(tester, <BoxDecoration>[kBoxDecorationB, kBoxDecorationC, kBoxDecorationA]);
84 85

      tester.pumpWidget(
86 87 88 89 90 91 92
        new Stack(
          children: <Widget>[
            new DecoratedBox(key: new Key('a'), decoration: kBoxDecorationA),
            new DecoratedBox(decoration: kBoxDecorationC),
            new DecoratedBox(key: new Key('b'), decoration: kBoxDecorationB),
          ]
        )
93 94
      );

95
      checkTree(tester, <BoxDecoration>[kBoxDecorationA, kBoxDecorationC, kBoxDecorationB]);
96 97

      tester.pumpWidget(
98 99 100 101 102
        new Stack(
          children: <Widget>[
            new DecoratedBox(decoration: kBoxDecorationC),
          ]
        )
103 104
      );

105
      checkTree(tester, <BoxDecoration>[kBoxDecorationC]);
106 107

      tester.pumpWidget(
108
        new Stack()
109 110
      );

111
      checkTree(tester, <BoxDecoration>[]);
112 113

    });
114 115 116
  });

  test('MultiChildRenderObjectElement with stateless components', () {
117 118 119
    testWidgets((WidgetTester tester) {

      tester.pumpWidget(
120 121 122 123 124 125 126
        new Stack(
          children: <Widget>[
            new DecoratedBox(decoration: kBoxDecorationA),
            new DecoratedBox(decoration: kBoxDecorationB),
            new DecoratedBox(decoration: kBoxDecorationC),
          ]
        )
127 128
      );

129
      checkTree(tester, <BoxDecoration>[kBoxDecorationA, kBoxDecorationB, kBoxDecorationC]);
130 131

      tester.pumpWidget(
132 133 134 135 136 137 138 139 140
        new Stack(
          children: <Widget>[
            new DecoratedBox(decoration: kBoxDecorationA),
            new Container(
              child: new DecoratedBox(decoration: kBoxDecorationB)
            ),
            new DecoratedBox(decoration: kBoxDecorationC),
          ]
        )
141 142
      );

143
      checkTree(tester, <BoxDecoration>[kBoxDecorationA, kBoxDecorationB, kBoxDecorationC]);
144 145

      tester.pumpWidget(
146 147 148 149 150 151 152 153 154 155 156
        new Stack(
          children: <Widget>[
            new DecoratedBox(decoration: kBoxDecorationA),
            new Container(
              child: new Container(
                child: new DecoratedBox(decoration: kBoxDecorationB)
              )
            ),
            new DecoratedBox(decoration: kBoxDecorationC),
          ]
        )
157 158
      );

159
      checkTree(tester, <BoxDecoration>[kBoxDecorationA, kBoxDecorationB, kBoxDecorationC]);
160 161

      tester.pumpWidget(
162 163 164 165 166 167 168 169 170 171 172 173 174
        new Stack(
          children: <Widget>[
            new Container(
              child: new Container(
                child: new DecoratedBox(decoration: kBoxDecorationB)
              )
            ),
            new Container(
              child: new DecoratedBox(decoration: kBoxDecorationA)
            ),
            new DecoratedBox(decoration: kBoxDecorationC),
          ]
        )
175 176
      );

177
      checkTree(tester, <BoxDecoration>[kBoxDecorationB, kBoxDecorationA, kBoxDecorationC]);
178 179

      tester.pumpWidget(
180 181 182 183 184 185 186 187 188 189 190
        new Stack(
          children: <Widget>[
            new Container(
              child: new DecoratedBox(decoration: kBoxDecorationB)
            ),
            new Container(
              child: new DecoratedBox(decoration: kBoxDecorationA)
            ),
            new DecoratedBox(decoration: kBoxDecorationC),
          ]
        )
191 192
      );

193
      checkTree(tester, <BoxDecoration>[kBoxDecorationB, kBoxDecorationA, kBoxDecorationC]);
194 195

      tester.pumpWidget(
196 197 198 199 200 201 202 203 204 205 206 207
        new Stack(
          children: <Widget>[
            new Container(
              key: new Key('b'),
              child: new DecoratedBox(decoration: kBoxDecorationB)
            ),
            new Container(
              key: new Key('a'),
              child: new DecoratedBox(decoration: kBoxDecorationA)
            ),
          ]
        )
208 209
      );

210
      checkTree(tester, <BoxDecoration>[kBoxDecorationB, kBoxDecorationA]);
211 212

      tester.pumpWidget(
213 214 215 216 217 218 219 220 221 222 223 224
        new Stack(
          children: <Widget>[
            new Container(
              key: new Key('a'),
              child: new DecoratedBox(decoration: kBoxDecorationA)
            ),
            new Container(
              key: new Key('b'),
              child: new DecoratedBox(decoration: kBoxDecorationB)
            ),
          ]
        )
225 226
      );

227
      checkTree(tester, <BoxDecoration>[kBoxDecorationA, kBoxDecorationB]);
228 229

      tester.pumpWidget(
230
        new Stack()
231 232
      );

233
      checkTree(tester, <BoxDecoration>[]);
234
    });
235 236 237
  });

  test('MultiChildRenderObjectElement with stateful components', () {
238 239
    testWidgets((WidgetTester tester) {
      tester.pumpWidget(
240 241 242 243 244 245
        new Stack(
          children: <Widget>[
            new DecoratedBox(decoration: kBoxDecorationA),
            new DecoratedBox(decoration: kBoxDecorationB),
          ]
        )
246 247
      );

248
      checkTree(tester, <BoxDecoration>[kBoxDecorationA, kBoxDecorationB]);
249 250

      tester.pumpWidget(
251 252 253 254 255 256 257 258 259
        new Stack(
          children: <Widget>[
            new FlipComponent(
              left: new DecoratedBox(decoration: kBoxDecorationA),
              right: new DecoratedBox(decoration: kBoxDecorationB)
            ),
            new DecoratedBox(decoration: kBoxDecorationC),
          ]
        )
260 261
      );

262
      checkTree(tester, <BoxDecoration>[kBoxDecorationA, kBoxDecorationC]);
263 264 265 266

      flipStatefulComponent(tester);
      tester.pump();

267
      checkTree(tester, <BoxDecoration>[kBoxDecorationB, kBoxDecorationC]);
268 269

      tester.pumpWidget(
270 271 272 273 274 275 276 277
        new Stack(
          children: <Widget>[
            new FlipComponent(
              left: new DecoratedBox(decoration: kBoxDecorationA),
              right: new DecoratedBox(decoration: kBoxDecorationB)
            ),
          ]
        )
278 279
      );

280
      checkTree(tester, <BoxDecoration>[kBoxDecorationB]);
281 282 283 284

      flipStatefulComponent(tester);
      tester.pump();

285
      checkTree(tester, <BoxDecoration>[kBoxDecorationA]);
286 287

      tester.pumpWidget(
288 289 290 291 292 293 294 295 296
        new Stack(
          children: <Widget>[
            new FlipComponent(
              key: new Key('flip'),
              left: new DecoratedBox(decoration: kBoxDecorationA),
              right: new DecoratedBox(decoration: kBoxDecorationB)
            ),
          ]
        )
297 298 299
      );

      tester.pumpWidget(
300 301 302 303 304 305 306 307 308 309
        new Stack(
          children: <Widget>[
            new DecoratedBox(key: new Key('c'), decoration: kBoxDecorationC),
            new FlipComponent(
              key: new Key('flip'),
              left: new DecoratedBox(decoration: kBoxDecorationA),
              right: new DecoratedBox(decoration: kBoxDecorationB)
            ),
          ]
        )
310 311
      );

312
      checkTree(tester, <BoxDecoration>[kBoxDecorationC, kBoxDecorationA]);
313 314 315 316

      flipStatefulComponent(tester);
      tester.pump();

317
      checkTree(tester, <BoxDecoration>[kBoxDecorationC, kBoxDecorationB]);
318 319

      tester.pumpWidget(
320 321 322 323 324 325 326 327 328 329
        new Stack(
          children: <Widget>[
            new FlipComponent(
              key: new Key('flip'),
              left: new DecoratedBox(decoration: kBoxDecorationA),
              right: new DecoratedBox(decoration: kBoxDecorationB)
            ),
            new DecoratedBox(key: new Key('c'), decoration: kBoxDecorationC),
          ]
        )
330 331
      );

332
      checkTree(tester, <BoxDecoration>[kBoxDecorationB, kBoxDecorationC]);
333
    });
334 335
  });
}