multichild_test.dart 9.27 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

import 'test_widgets.dart';

void checkTree(WidgetTester tester, List<BoxDecoration> expectedDecorations) {
12
  MultiChildRenderObjectElement element = tester.element(find.byElementPredicate(
13 14
    (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
    }
    expect(child, isNull);
  } catch (e) {
    print(renderObject.toStringDeep());
    rethrow;
  }
}

void main() {
35
  testWidgets('MultiChildRenderObjectElement control test', (WidgetTester tester) async {
36

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

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

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

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

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

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

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

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

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

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

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

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

106
    await tester.pumpWidget(
107 108 109 110 111
      new Stack()
    );

    checkTree(tester, <BoxDecoration>[]);

112 113
  });

114
  testWidgets('MultiChildRenderObjectElement with stateless widgets', (WidgetTester tester) async {
115

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

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

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

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

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

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

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

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

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

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

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

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

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

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

226
    await tester.pumpWidget(
227 228 229 230
      new Stack()
    );

    checkTree(tester, <BoxDecoration>[]);
231 232
  });

233 234
  testWidgets('MultiChildRenderObjectElement with stateful widgets', (WidgetTester tester) async {
    await tester.pumpWidget(
235 236 237 238 239 240 241 242 243 244
      new Stack(
        children: <Widget>[
          new DecoratedBox(decoration: kBoxDecorationA),
          new DecoratedBox(decoration: kBoxDecorationB),
        ]
      )
    );

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

245
    await tester.pumpWidget(
246 247 248 249 250 251 252 253 254 255 256 257 258 259
      new Stack(
        children: <Widget>[
          new FlipWidget(
            left: new DecoratedBox(decoration: kBoxDecorationA),
            right: new DecoratedBox(decoration: kBoxDecorationB)
          ),
          new DecoratedBox(decoration: kBoxDecorationC),
        ]
      )
    );

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

    flipStatefulWidget(tester);
260
    await tester.pump();
261 262 263

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

264
    await tester.pumpWidget(
265 266 267 268 269 270 271 272 273 274 275 276 277
      new Stack(
        children: <Widget>[
          new FlipWidget(
            left: new DecoratedBox(decoration: kBoxDecorationA),
            right: new DecoratedBox(decoration: kBoxDecorationB)
          ),
        ]
      )
    );

    checkTree(tester, <BoxDecoration>[kBoxDecorationB]);

    flipStatefulWidget(tester);
278
    await tester.pump();
279 280 281

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

282
    await tester.pumpWidget(
283 284 285
      new Stack(
        children: <Widget>[
          new FlipWidget(
286
            key: const Key('flip'),
287 288 289 290 291 292 293
            left: new DecoratedBox(decoration: kBoxDecorationA),
            right: new DecoratedBox(decoration: kBoxDecorationB)
          ),
        ]
      )
    );

294
    await tester.pumpWidget(
295 296
      new Stack(
        children: <Widget>[
297
          new DecoratedBox(key: const Key('c'), decoration: kBoxDecorationC),
298
          new FlipWidget(
299
            key: const Key('flip'),
300 301 302 303 304 305 306 307 308 309
            left: new DecoratedBox(decoration: kBoxDecorationA),
            right: new DecoratedBox(decoration: kBoxDecorationB)
          ),
        ]
      )
    );

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

    flipStatefulWidget(tester);
310
    await tester.pump();
311 312 313

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

314
    await tester.pumpWidget(
315 316 317
      new Stack(
        children: <Widget>[
          new FlipWidget(
318
            key: const Key('flip'),
319 320 321
            left: new DecoratedBox(decoration: kBoxDecorationA),
            right: new DecoratedBox(decoration: kBoxDecorationB)
          ),
322
          new DecoratedBox(key: const Key('c'), decoration: kBoxDecorationC),
323 324 325 326 327
        ]
      )
    );

    checkTree(tester, <BoxDecoration>[kBoxDecorationB, kBoxDecorationC]);
328 329
  });
}