stack_test.dart 9.59 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
import 'package:flutter/rendering.dart';
7
import 'package:flutter/widgets.dart';
8

9
import '../rendering/rendering_tester.dart';
10

11 12 13 14 15 16 17 18 19
class TestPaintingContext implements PaintingContext {
  final List<Invocation> invocations = <Invocation>[];

  @override
    void noSuchMethod(Invocation invocation) {
      invocations.add(invocation);
    }
}

20
void main() {
21 22
  testWidgets('Can construct an empty Stack', (WidgetTester tester) async {
    await tester.pumpWidget(new Stack());
Hans Muller's avatar
Hans Muller committed
23 24
  });

25 26
  testWidgets('Can construct an empty Centered Stack', (WidgetTester tester) async {
    await tester.pumpWidget(new Center(child: new Stack()));
27 28
  });

29
  testWidgets('Can change position data', (WidgetTester tester) async {
30 31
    Key key = new Key('container');

32
    await tester.pumpWidget(
33 34 35 36 37 38 39 40
      new Stack(
        children: <Widget>[
          new Positioned(
            left: 10.0,
            child: new Container(
              key: key,
              width: 10.0,
              height: 10.0
41
            )
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
          )
        ]
      )
    );

    Element container;
    StackParentData parentData;

    container = tester.element(find.byKey(key));
    parentData = container.renderObject.parentData;
    expect(parentData.top, isNull);
    expect(parentData.right, isNull);
    expect(parentData.bottom, isNull);
    expect(parentData.left, equals(10.0));
    expect(parentData.width, isNull);
    expect(parentData.height, isNull);

59
    await tester.pumpWidget(
60 61 62 63 64 65 66 67
      new Stack(
        children: <Widget>[
          new Positioned(
            right: 10.0,
            child: new Container(
              key: key,
              width: 10.0,
              height: 10.0
68
            )
69 70 71 72 73 74 75 76 77 78 79 80 81
          )
        ]
      )
    );

    container = tester.element(find.byKey(key));
    parentData = container.renderObject.parentData;
    expect(parentData.top, isNull);
    expect(parentData.right, equals(10.0));
    expect(parentData.bottom, isNull);
    expect(parentData.left, isNull);
    expect(parentData.width, isNull);
    expect(parentData.height, isNull);
82
  });
83

84
  testWidgets('Can remove parent data', (WidgetTester tester) async {
85 86 87
    Key key = new Key('container');
    Container container = new Container(key: key, width: 10.0, height: 10.0);

88
    await tester.pumpWidget(new Stack(children: <Widget>[ new Positioned(left: 10.0, child: container) ]));
89 90 91 92 93 94 95 96 97 98 99
    Element containerElement = tester.element(find.byKey(key));

    StackParentData parentData;
    parentData = containerElement.renderObject.parentData;
    expect(parentData.top, isNull);
    expect(parentData.right, isNull);
    expect(parentData.bottom, isNull);
    expect(parentData.left, equals(10.0));
    expect(parentData.width, isNull);
    expect(parentData.height, isNull);

100
    await tester.pumpWidget(new Stack(children: <Widget>[ container ]));
101 102 103 104 105 106 107 108 109
    containerElement = tester.element(find.byKey(key));

    parentData = containerElement.renderObject.parentData;
    expect(parentData.top, isNull);
    expect(parentData.right, isNull);
    expect(parentData.bottom, isNull);
    expect(parentData.left, isNull);
    expect(parentData.width, isNull);
    expect(parentData.height, isNull);
110
  });
Hans Muller's avatar
Hans Muller committed
111

112
  testWidgets('Can align non-positioned children', (WidgetTester tester) async {
113 114 115
    Key child0Key = new Key('child0');
    Key child1Key = new Key('child1');

116
    await tester.pumpWidget(
117 118 119 120 121 122 123
      new Center(
        child: new Stack(
          children: <Widget>[
            new Container(key: child0Key, width: 20.0, height: 20.0),
            new Container(key: child1Key, width: 10.0, height: 10.0)
          ],
          alignment: const FractionalOffset(0.5, 0.5)
Hans Muller's avatar
Hans Muller committed
124
        )
125 126
      )
    );
Hans Muller's avatar
Hans Muller committed
127

128 129 130
    Element child0 = tester.element(find.byKey(child0Key));
    final StackParentData child0RenderObjectParentData = child0.renderObject.parentData;
    expect(child0RenderObjectParentData.offset, equals(const Offset(0.0, 0.0)));
Hans Muller's avatar
Hans Muller committed
131

132 133 134
    Element child1 = tester.element(find.byKey(child1Key));
    final StackParentData child1RenderObjectParentData = child1.renderObject.parentData;
    expect(child1RenderObjectParentData.offset, equals(const Offset(5.0, 5.0)));
Hans Muller's avatar
Hans Muller committed
135 136
  });

137 138
  testWidgets('Can construct an empty IndexedStack', (WidgetTester tester) async {
    await tester.pumpWidget(new IndexedStack());
Hans Muller's avatar
Hans Muller committed
139 140
  });

141 142
  testWidgets('Can construct an empty Centered IndexedStack', (WidgetTester tester) async {
    await tester.pumpWidget(new Center(child: new IndexedStack()));
143 144
  });

145
  testWidgets('Can construct an IndexedStack', (WidgetTester tester) async {
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
    int itemCount = 3;
    List<int> itemsPainted;

    Widget buildFrame(int index) {
      itemsPainted = <int>[];
      List<Widget> items = new List<Widget>.generate(itemCount, (int i) {
        return new CustomPaint(
          child: new Text('$i'),
          painter: new TestCallbackPainter(
            onPaint: () { itemsPainted.add(i); }
          )
        );
      });
      return new Center(child: new IndexedStack(children: items, index: index));
    }

162
    await tester.pumpWidget(buildFrame(0));
163 164 165
    expect(find.text('0'), findsOneWidget);
    expect(find.text('1'), findsOneWidget);
    expect(find.text('2'), findsOneWidget);
166
    expect(itemsPainted, equals(<int>[0]));
167

168
    await tester.pumpWidget(buildFrame(1));
169
    expect(itemsPainted, equals(<int>[1]));
170

171
    await tester.pumpWidget(buildFrame(2));
172
    expect(itemsPainted, equals(<int>[2]));
Hans Muller's avatar
Hans Muller committed
173 174
  });

175
  testWidgets('Can hit test an IndexedStack', (WidgetTester tester) async {
176 177 178 179 180 181 182 183 184 185 186 187
    Key key = new Key('indexedStack');
    int itemCount = 3;
    List<int> itemsTapped;

    Widget buildFrame(int index) {
      itemsTapped = <int>[];
      List<Widget> items = new List<Widget>.generate(itemCount, (int i) {
        return new GestureDetector(child: new Text('$i'), onTap: () { itemsTapped.add(i); });
      });
      return new Center(child: new IndexedStack(children: items, key: key, index: index));
    }

188
    await tester.pumpWidget(buildFrame(0));
189
    expect(itemsTapped, isEmpty);
190
    await tester.tap(find.byKey(key));
191
    expect(itemsTapped, <int>[0]);
192

193
    await tester.pumpWidget(buildFrame(2));
194
    expect(itemsTapped, isEmpty);
195
    await tester.tap(find.byKey(key));
196
    expect(itemsTapped, <int>[2]);
Hans Muller's avatar
Hans Muller committed
197 198
  });

199
  testWidgets('Can set width and height', (WidgetTester tester) async {
200 201 202 203 204 205
    Key key = new Key('container');

    BoxDecoration kBoxDecoration = new BoxDecoration(
      backgroundColor: new Color(0xFF00FF00)
    );

206
    await tester.pumpWidget(
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
      new Stack(
        children: <Widget>[
          new Positioned(
            left: 10.0,
            width: 11.0,
            height: 12.0,
            child: new DecoratedBox(key: key, decoration: kBoxDecoration)
          )
        ]
      )
    );

    Element box;
    RenderBox renderBox;
    StackParentData parentData;

    box = tester.element(find.byKey(key));
    renderBox = box.renderObject;
    parentData = renderBox.parentData;
    expect(parentData.top, isNull);
    expect(parentData.right, isNull);
    expect(parentData.bottom, isNull);
    expect(parentData.left, equals(10.0));
    expect(parentData.width, equals(11.0));
    expect(parentData.height, equals(12.0));
    expect(parentData.offset.dx, equals(10.0));
    expect(parentData.offset.dy, equals(0.0));
    expect(renderBox.size.width, equals(11.0));
    expect(renderBox.size.height, equals(12.0));

237
    await tester.pumpWidget(
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
      new Stack(
        children: <Widget>[
          new Positioned(
            right: 10.0,
            width: 11.0,
            height: 12.0,
            child: new DecoratedBox(key: key, decoration: kBoxDecoration)
          )
        ]
      )
    );

    box = tester.element(find.byKey(key));
    renderBox = box.renderObject;
    parentData = renderBox.parentData;
    expect(parentData.top, isNull);
    expect(parentData.right, equals(10.0));
    expect(parentData.bottom, isNull);
    expect(parentData.left, isNull);
    expect(parentData.width, equals(11.0));
    expect(parentData.height, equals(12.0));
    expect(parentData.offset.dx, equals(779.0));
    expect(parentData.offset.dy, equals(0.0));
    expect(renderBox.size.width, equals(11.0));
    expect(renderBox.size.height, equals(12.0));
263 264
  });

265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 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
  testWidgets('Stack clip test', (WidgetTester tester) async {
    await tester.pumpWidget(
      new Center(
        child: new Stack(
          children: <Widget>[
            new Container(
              width: 100.0,
              height: 100.0
            ),
            new Positioned(
              top: 0.0,
              left: 0.0,
              child: new Container(
                width: 200.0,
                height: 200.0
              )
            )
          ]
        )
      )
    );

    RenderBox box = tester.renderObject(find.byType(Stack));
    TestPaintingContext context = new TestPaintingContext();
    box.paint(context, Offset.zero);
    expect(context.invocations.first.memberName, equals(#pushClipRect));

    await tester.pumpWidget(
      new Center(
        child: new Stack(
          overflow: Overflow.visible,
          children: <Widget>[
            new Container(
              width: 100.0,
              height: 100.0
            ),
            new Positioned(
              top: 0.0,
              left: 0.0,
              child: new Container(
                width: 200.0,
                height: 200.0
              )
            )
          ]
        )
      )
    );

    box = tester.renderObject(find.byType(Stack));
    context = new TestPaintingContext();
    box.paint(context, Offset.zero);
    expect(context.invocations.first.memberName, equals(#paintChild));
  });
319
}