multichild_test.dart 9.95 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
  final MultiChildRenderObjectElement element = tester.element(find.byElementPredicate(
13 14
    (Element element) => element is MultiChildRenderObjectElement
  ));
15 16
  expect(element, isNotNull);
  expect(element.renderObject is RenderStack, isTrue);
17
  final RenderStack renderObject = element.renderObject;
18 19 20 21
  try {
    RenderObject child = renderObject.firstChild;
    for (BoxDecoration decoration in expectedDecorations) {
      expect(child is RenderDecoratedBox, isTrue);
22
      final RenderDecoratedBox decoratedBox = child;
23
      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
      Stack(
39
        textDirection: TextDirection.ltr,
40
        children: const <Widget>[
41 42 43
          DecoratedBox(decoration: kBoxDecorationA),
          DecoratedBox(decoration: kBoxDecorationB),
          DecoratedBox(decoration: kBoxDecorationC),
44 45
        ],
      ),
46 47 48 49
    );

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

50
    await tester.pumpWidget(
51
      Stack(
52
        textDirection: TextDirection.ltr,
53
        children: const <Widget>[
54 55
          DecoratedBox(decoration: kBoxDecorationA),
          DecoratedBox(decoration: kBoxDecorationC),
56 57
        ],
      ),
58 59 60 61
    );

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

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

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

75
    await tester.pumpWidget(
76
      Stack(
77
        textDirection: TextDirection.ltr,
78
        children: const <Widget>[
79 80 81
          DecoratedBox(key: Key('b'), decoration: kBoxDecorationB),
          DecoratedBox(decoration: kBoxDecorationC),
          DecoratedBox(key: Key('a'), decoration: kBoxDecorationA),
82 83
        ],
      ),
84 85 86 87
    );

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

88
    await tester.pumpWidget(
89
      Stack(
90
        textDirection: TextDirection.ltr,
91
        children: const <Widget>[
92 93 94
          DecoratedBox(key: Key('a'), decoration: kBoxDecorationA),
          DecoratedBox(decoration: kBoxDecorationC),
          DecoratedBox(key: Key('b'), decoration: kBoxDecorationB),
95 96
        ],
      ),
97 98 99 100
    );

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

101
    await tester.pumpWidget(
102
      Stack(
103
        textDirection: TextDirection.ltr,
104
        children: const <Widget>[
105
          DecoratedBox(decoration: kBoxDecorationC),
106 107
        ],
      ),
108 109 110 111
    );

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

112
    await tester.pumpWidget(
113
      Stack(textDirection: TextDirection.ltr)
114 115 116 117
    );

    checkTree(tester, <BoxDecoration>[]);

118 119
  });

120
  testWidgets('MultiChildRenderObjectElement with stateless widgets', (WidgetTester tester) async {
121

122
    await tester.pumpWidget(
123
      Stack(
124
        textDirection: TextDirection.ltr,
125
        children: const <Widget>[
126 127 128
          DecoratedBox(decoration: kBoxDecorationA),
          DecoratedBox(decoration: kBoxDecorationB),
          DecoratedBox(decoration: kBoxDecorationC),
129 130
        ],
      ),
131 132 133 134
    );

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

135
    await tester.pumpWidget(
136
      Stack(
137
        textDirection: TextDirection.ltr,
138
        children: <Widget>[
139
          const DecoratedBox(decoration: kBoxDecorationA),
140
          Container(
141
            child: const DecoratedBox(decoration: kBoxDecorationB)
142
          ),
143
          const DecoratedBox(decoration: kBoxDecorationC),
144 145
        ],
      ),
146 147 148 149
    );

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

150
    await tester.pumpWidget(
151
      Stack(
152
        textDirection: TextDirection.ltr,
153
        children: <Widget>[
154
          const DecoratedBox(decoration: kBoxDecorationA),
155 156
          Container(
            child: Container(
157
              child: const DecoratedBox(decoration: kBoxDecorationB),
158
            ),
159
          ),
160
          const DecoratedBox(decoration: kBoxDecorationC),
161 162
        ],
      ),
163 164 165 166
    );

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

167
    await tester.pumpWidget(
168
      Stack(
169
        textDirection: TextDirection.ltr,
170
        children: <Widget>[
171 172
          Container(
            child: Container(
173
              child: const DecoratedBox(decoration: kBoxDecorationB),
174
            ),
175
          ),
176
          Container(
177
            child: const DecoratedBox(decoration: kBoxDecorationA),
178
          ),
179
          const DecoratedBox(decoration: kBoxDecorationC),
180 181
        ],
      ),
182 183 184 185
    );

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

186
    await tester.pumpWidget(
187
      Stack(
188
        textDirection: TextDirection.ltr,
189
        children: <Widget>[
190
          Container(
191
            child: const DecoratedBox(decoration: kBoxDecorationB),
192
          ),
193
          Container(
194
            child: const DecoratedBox(decoration: kBoxDecorationA),
195
          ),
196
          const DecoratedBox(decoration: kBoxDecorationC),
197 198
        ],
      ),
199 200 201 202
    );

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

203
    await tester.pumpWidget(
204
      Stack(
205
        textDirection: TextDirection.ltr,
206
        children: <Widget>[
207
          Container(
208
            key: const Key('b'),
209
            child: const DecoratedBox(decoration: kBoxDecorationB),
210
          ),
211
          Container(
212
            key: const Key('a'),
213
            child: const DecoratedBox(decoration: kBoxDecorationA),
214
          ),
215 216
        ],
      ),
217 218 219 220
    );

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

221
    await tester.pumpWidget(
222
      Stack(
223
        textDirection: TextDirection.ltr,
224
        children: <Widget>[
225
          Container(
226
            key: const Key('a'),
227
            child: const DecoratedBox(decoration: kBoxDecorationA),
228
          ),
229
          Container(
230
            key: const Key('b'),
231
            child: const DecoratedBox(decoration: kBoxDecorationB),
232
          ),
233 234
        ],
      ),
235 236 237 238
    );

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

239
    await tester.pumpWidget(
240
      Stack(textDirection: TextDirection.ltr)
241 242 243
    );

    checkTree(tester, <BoxDecoration>[]);
244 245
  });

246 247
  testWidgets('MultiChildRenderObjectElement with stateful widgets', (WidgetTester tester) async {
    await tester.pumpWidget(
248
      Stack(
249
        textDirection: TextDirection.ltr,
250
        children: const <Widget>[
251 252
          DecoratedBox(decoration: kBoxDecorationA),
          DecoratedBox(decoration: kBoxDecorationB),
253 254
        ],
      ),
255 256 257 258
    );

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

259
    await tester.pumpWidget(
260
      Stack(
261
        textDirection: TextDirection.ltr,
262
        children: const <Widget>[
263 264 265
          FlipWidget(
            left: DecoratedBox(decoration: kBoxDecorationA),
            right: DecoratedBox(decoration: kBoxDecorationB),
266
          ),
267
          DecoratedBox(decoration: kBoxDecorationC),
268 269
        ],
      ),
270 271 272 273 274
    );

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

    flipStatefulWidget(tester);
275
    await tester.pump();
276 277 278

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

279
    await tester.pumpWidget(
280
      Stack(
281
        textDirection: TextDirection.ltr,
282
        children: const <Widget>[
283 284 285
          FlipWidget(
            left: DecoratedBox(decoration: kBoxDecorationA),
            right: DecoratedBox(decoration: kBoxDecorationB),
286
          ),
287 288
        ],
      ),
289 290 291 292 293
    );

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

    flipStatefulWidget(tester);
294
    await tester.pump();
295 296 297

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

298
    await tester.pumpWidget(
299
      Stack(
300
        textDirection: TextDirection.ltr,
301
        children: const <Widget>[
302 303 304 305
          FlipWidget(
            key: Key('flip'),
            left: DecoratedBox(decoration: kBoxDecorationA),
            right: DecoratedBox(decoration: kBoxDecorationB),
306
          ),
307 308
        ],
      ),
309 310
    );

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

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

    flipStatefulWidget(tester);
328
    await tester.pump();
329 330 331

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

332
    await tester.pumpWidget(
333
      Stack(
334
        textDirection: TextDirection.ltr,
335
        children: const <Widget>[
336 337 338 339
          FlipWidget(
            key: Key('flip'),
            left: DecoratedBox(decoration: kBoxDecorationA),
            right: DecoratedBox(decoration: kBoxDecorationB),
340
          ),
341
          DecoratedBox(key: Key('c'), decoration: kBoxDecorationC),
342 343
        ],
      ),
344 345 346
    );

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