multichild_test.dart 8.72 KB
Newer Older
1 2
import 'package:flutter/rendering.dart';
import 'package:flutter/widgets.dart';
3 4 5 6 7 8 9
import 'package:test/test.dart';

import 'test_widgets.dart';
import 'widget_tester.dart';

void checkTree(WidgetTester tester, List<BoxDecoration> expectedDecorations) {
  MultiChildRenderObjectElement element =
10
      tester.findElement((Element element) => element is MultiChildRenderObjectElement);
11 12 13 14 15 16 17 18 19
  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));
20 21
      final StackParentData decoratedBoxParentData = decoratedBox.parentData;
      child = decoratedBoxParentData.nextSibling;
22 23 24 25 26 27 28 29 30 31
    }
    expect(child, isNull);
  } catch (e) {
    print(renderObject.toStringDeep());
    rethrow;
  }
}

void main() {
  test('MultiChildRenderObjectElement control test', () {
32 33 34
    testWidgets((WidgetTester tester) {

      tester.pumpWidget(
35
        new Stack(<Widget>[
36 37 38 39 40 41
          new DecoratedBox(decoration: kBoxDecorationA),
          new DecoratedBox(decoration: kBoxDecorationB),
          new DecoratedBox(decoration: kBoxDecorationC),
        ])
      );

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

      tester.pumpWidget(
45
        new Stack(<Widget>[
46 47 48 49 50
          new DecoratedBox(decoration: kBoxDecorationA),
          new DecoratedBox(decoration: kBoxDecorationC),
        ])
      );

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

      tester.pumpWidget(
54
        new Stack(<Widget>[
55 56 57 58 59 60
          new DecoratedBox(decoration: kBoxDecorationA),
          new DecoratedBox(key: new Key('b'), decoration: kBoxDecorationB),
          new DecoratedBox(decoration: kBoxDecorationC),
        ])
      );

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

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

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

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

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

      tester.pumpWidget(
84
        new Stack(<Widget>[
85 86 87 88
          new DecoratedBox(decoration: kBoxDecorationC),
        ])
      );

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

      tester.pumpWidget(
92
        new Stack(<Widget>[])
93 94
      );

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

    });
98 99 100
  });

  test('MultiChildRenderObjectElement with stateless components', () {
101 102 103
    testWidgets((WidgetTester tester) {

      tester.pumpWidget(
104
        new Stack(<Widget>[
105 106 107 108 109 110
          new DecoratedBox(decoration: kBoxDecorationA),
          new DecoratedBox(decoration: kBoxDecorationB),
          new DecoratedBox(decoration: kBoxDecorationC),
        ])
      );

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

      tester.pumpWidget(
114
        new Stack(<Widget>[
115 116
          new DecoratedBox(decoration: kBoxDecorationA),
          new Container(
117
            child: new DecoratedBox(decoration: kBoxDecorationB)
118 119 120 121 122
          ),
          new DecoratedBox(decoration: kBoxDecorationC),
        ])
      );

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

      tester.pumpWidget(
126
        new Stack(<Widget>[
127 128 129 130 131 132 133 134 135 136
          new DecoratedBox(decoration: kBoxDecorationA),
          new Container(
            child: new Container(
              child: new DecoratedBox(decoration: kBoxDecorationB)
            )
          ),
          new DecoratedBox(decoration: kBoxDecorationC),
        ])
      );

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

      tester.pumpWidget(
140
        new Stack(<Widget>[
141 142 143 144 145 146 147 148 149 150 151 152
          new Container(
            child: new Container(
              child: new DecoratedBox(decoration: kBoxDecorationB)
            )
          ),
          new Container(
            child: new DecoratedBox(decoration: kBoxDecorationA)
          ),
          new DecoratedBox(decoration: kBoxDecorationC),
        ])
      );

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

      tester.pumpWidget(
156
        new Stack(<Widget>[
157
          new Container(
158
            child: new DecoratedBox(decoration: kBoxDecorationB)
159 160 161 162 163 164 165 166
          ),
          new Container(
            child: new DecoratedBox(decoration: kBoxDecorationA)
          ),
          new DecoratedBox(decoration: kBoxDecorationC),
        ])
      );

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

      tester.pumpWidget(
170
        new Stack(<Widget>[
171 172 173 174 175 176 177 178 179 180 181
          new Container(
            key: new Key('b'),
            child: new DecoratedBox(decoration: kBoxDecorationB)
          ),
          new Container(
            key: new Key('a'),
            child: new DecoratedBox(decoration: kBoxDecorationA)
          ),
        ])
      );

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

      tester.pumpWidget(
185
        new Stack(<Widget>[
186 187 188 189 190 191 192 193 194 195 196
          new Container(
            key: new Key('a'),
            child: new DecoratedBox(decoration: kBoxDecorationA)
          ),
          new Container(
            key: new Key('b'),
            child: new DecoratedBox(decoration: kBoxDecorationB)
          ),
        ])
      );

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

      tester.pumpWidget(
200
        new Stack(<Widget>[])
201 202
      );

203
      checkTree(tester, <BoxDecoration>[]);
204
    });
205 206 207
  });

  test('MultiChildRenderObjectElement with stateful components', () {
208 209
    testWidgets((WidgetTester tester) {
      tester.pumpWidget(
210
        new Stack(<Widget>[
211 212 213 214 215
          new DecoratedBox(decoration: kBoxDecorationA),
          new DecoratedBox(decoration: kBoxDecorationB),
        ])
      );

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

      tester.pumpWidget(
219
        new Stack(<Widget>[
220 221 222 223 224 225 226 227
          new FlipComponent(
            left: new DecoratedBox(decoration: kBoxDecorationA),
            right: new DecoratedBox(decoration: kBoxDecorationB)
          ),
          new DecoratedBox(decoration: kBoxDecorationC),
        ])
      );

228
      checkTree(tester, <BoxDecoration>[kBoxDecorationA, kBoxDecorationC]);
229 230 231 232

      flipStatefulComponent(tester);
      tester.pump();

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

      tester.pumpWidget(
236
        new Stack(<Widget>[
237 238 239 240 241 242 243
          new FlipComponent(
            left: new DecoratedBox(decoration: kBoxDecorationA),
            right: new DecoratedBox(decoration: kBoxDecorationB)
          ),
        ])
      );

244
      checkTree(tester, <BoxDecoration>[kBoxDecorationB]);
245 246 247 248

      flipStatefulComponent(tester);
      tester.pump();

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

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

      tester.pumpWidget(
262
        new Stack(<Widget>[
263 264 265 266 267 268 269 270 271
          new DecoratedBox(key: new Key('c'), decoration: kBoxDecorationC),
          new FlipComponent(
            key: new Key('flip'),
            left: new DecoratedBox(decoration: kBoxDecorationA),
            right: new DecoratedBox(decoration: kBoxDecorationB)
          ),
        ])
      );

272
      checkTree(tester, <BoxDecoration>[kBoxDecorationC, kBoxDecorationA]);
273 274 275 276

      flipStatefulComponent(tester);
      tester.pump();

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

      tester.pumpWidget(
280
        new Stack(<Widget>[
281 282 283 284 285 286 287 288 289
          new FlipComponent(
            key: new Key('flip'),
            left: new DecoratedBox(decoration: kBoxDecorationA),
            right: new DecoratedBox(decoration: kBoxDecorationB)
          ),
          new DecoratedBox(key: new Key('c'), decoration: kBoxDecorationC),
        ])
      );

290
      checkTree(tester, <BoxDecoration>[kBoxDecorationB, kBoxDecorationC]);
291
    });
292 293
  });
}