stack_test.dart 8.44 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 'package:test/test.dart';

10
import '../rendering/rendering_tester.dart';
11 12

void main() {
Hans Muller's avatar
Hans Muller committed
13 14
  test('Can construct an empty Stack', () {
    testWidgets((WidgetTester tester) {
15
      tester.pumpWidget(new Stack());
Hans Muller's avatar
Hans Muller committed
16 17 18
    });
  });

19 20
  test('Can construct an empty Centered Stack', () {
    testWidgets((WidgetTester tester) {
21
      tester.pumpWidget(new Center(child: new Stack()));
22 23 24
    });
  });

25
  test('Can change position data', () {
26
    testElementTree((ElementTreeTester tester) {
27
      Key key = new Key('container');
28

29
      tester.pumpWidget(
30 31 32 33 34 35 36 37 38
        new Stack(
          children: <Widget>[
            new Positioned(
              left: 10.0,
              child: new Container(
                key: key,
                width: 10.0,
                height: 10.0
              )
39
            )
40 41
          ]
        )
42
      );
43

44 45 46 47 48 49 50 51 52
      Element container;
      StackParentData parentData;

      container = tester.findElementByKey(key);
      parentData = container.renderObject.parentData;
      expect(parentData.top, isNull);
      expect(parentData.right, isNull);
      expect(parentData.bottom, isNull);
      expect(parentData.left, equals(10.0));
53 54
      expect(parentData.width, isNull);
      expect(parentData.height, isNull);
55

56
      tester.pumpWidget(
57 58 59 60 61 62 63 64 65
        new Stack(
          children: <Widget>[
            new Positioned(
              right: 10.0,
              child: new Container(
                key: key,
                width: 10.0,
                height: 10.0
              )
66
            )
67 68
          ]
        )
69
      );
70

71
      container = tester.findElementByKey(key);
72 73 74 75 76
      parentData = container.renderObject.parentData;
      expect(parentData.top, isNull);
      expect(parentData.right, equals(10.0));
      expect(parentData.bottom, isNull);
      expect(parentData.left, isNull);
77 78
      expect(parentData.width, isNull);
      expect(parentData.height, isNull);
79
    });
80
  });
81 82

  test('Can remove parent data', () {
83
    testElementTree((ElementTreeTester tester) {
84 85
      Key key = new Key('container');
      Container container = new Container(key: key, width: 10.0, height: 10.0);
86

87
      tester.pumpWidget(new Stack(children: <Widget>[ new Positioned(left: 10.0, child: container) ]));
88
      Element containerElement = tester.findElementByKey(key);
89

90 91 92 93 94 95
      StackParentData parentData;
      parentData = containerElement.renderObject.parentData;
      expect(parentData.top, isNull);
      expect(parentData.right, isNull);
      expect(parentData.bottom, isNull);
      expect(parentData.left, equals(10.0));
96 97
      expect(parentData.width, isNull);
      expect(parentData.height, isNull);
98

99
      tester.pumpWidget(new Stack(children: <Widget>[ container ]));
100
      containerElement = tester.findElementByKey(key);
101

102 103 104 105 106
      parentData = containerElement.renderObject.parentData;
      expect(parentData.top, isNull);
      expect(parentData.right, isNull);
      expect(parentData.bottom, isNull);
      expect(parentData.left, isNull);
107 108
      expect(parentData.width, isNull);
      expect(parentData.height, isNull);
109
    });
110
  });
Hans Muller's avatar
Hans Muller committed
111 112

  test('Can align non-positioned children', () {
113
    testElementTree((ElementTreeTester tester) {
Hans Muller's avatar
Hans Muller committed
114 115 116 117 118
      Key child0Key = new Key('child0');
      Key child1Key = new Key('child1');

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

      Element child0 = tester.findElementByKey(child0Key);
130
      final StackParentData child0RenderObjectParentData = child0.renderObject.parentData;
131
      expect(child0RenderObjectParentData.offset, equals(const Offset(0.0, 0.0)));
Hans Muller's avatar
Hans Muller committed
132 133

      Element child1 = tester.findElementByKey(child1Key);
134
      final StackParentData child1RenderObjectParentData = child1.renderObject.parentData;
135
      expect(child1RenderObjectParentData.offset, equals(const Offset(5.0, 5.0)));
Hans Muller's avatar
Hans Muller committed
136 137 138 139 140
    });
  });

  test('Can construct an empty IndexedStack', () {
    testWidgets((WidgetTester tester) {
141
      tester.pumpWidget(new IndexedStack());
Hans Muller's avatar
Hans Muller committed
142 143 144
    });
  });

145 146
  test('Can construct an empty Centered IndexedStack', () {
    testWidgets((WidgetTester tester) {
147
      tester.pumpWidget(new Center(child: new IndexedStack()));
148 149 150
    });
  });

Hans Muller's avatar
Hans Muller committed
151 152 153 154 155 156
  test('Can construct an IndexedStack', () {
    testWidgets((WidgetTester tester) {
      int itemCount = 3;
      List<int> itemsPainted;

      Widget buildFrame(int index) {
157
        itemsPainted = <int>[];
158
        List<Widget> items = new List<Widget>.generate(itemCount, (int i) {
159 160 161 162 163 164
          return new CustomPaint(
            child: new Text('$i'),
            painter: new TestCallbackPainter(
              onPaint: () { itemsPainted.add(i); }
            )
          );
Hans Muller's avatar
Hans Muller committed
165
        });
166
        return new Center(child: new IndexedStack(children: items, index: index));
Hans Muller's avatar
Hans Muller committed
167 168 169
      }

      tester.pumpWidget(buildFrame(0));
170 171 172
      expect(tester, hasWidget(find.text('0')));
      expect(tester, hasWidget(find.text('1')));
      expect(tester, hasWidget(find.text('2')));
Hans Muller's avatar
Hans Muller committed
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
      expect(itemsPainted, equals([0]));

      tester.pumpWidget(buildFrame(1));
      expect(itemsPainted, equals([1]));

      tester.pumpWidget(buildFrame(2));
      expect(itemsPainted, equals([2]));
    });
  });

  test('Can hit test an IndexedStack', () {
    testWidgets((WidgetTester tester) {
      Key key = new Key('indexedStack');
      int itemCount = 3;
      List<int> itemsTapped;

      Widget buildFrame(int index) {
190
        itemsTapped = <int>[];
191
        List<Widget> items = new List<Widget>.generate(itemCount, (int i) {
Hans Muller's avatar
Hans Muller committed
192 193
          return new GestureDetector(child: new Text('$i'), onTap: () { itemsTapped.add(i); });
        });
194
        return new Center(child: new IndexedStack(children: items, key: key, index: index));
Hans Muller's avatar
Hans Muller committed
195 196 197 198
      }

      tester.pumpWidget(buildFrame(0));
      expect(itemsTapped, isEmpty);
199
      tester.tap(find.byKey(key));
Hans Muller's avatar
Hans Muller committed
200 201 202 203
      expect(itemsTapped, [0]);

      tester.pumpWidget(buildFrame(2));
      expect(itemsTapped, isEmpty);
204
      tester.tap(find.byKey(key));
Hans Muller's avatar
Hans Muller committed
205 206 207 208
      expect(itemsTapped, [2]);
    });
  });

209
  test('Can set width and height', () {
210
    testElementTree((ElementTreeTester tester) {
211 212 213 214 215 216 217
      Key key = new Key('container');

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

      tester.pumpWidget(
218 219 220 221 222 223 224 225 226 227
        new Stack(
          children: <Widget>[
            new Positioned(
              left: 10.0,
              width: 11.0,
              height: 12.0,
              child: new DecoratedBox(key: key, decoration: kBoxDecoration)
            )
          ]
        )
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
      );

      Element box;
      RenderBox renderBox;
      StackParentData parentData;

      box = tester.findElementByKey(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));
243 244
      expect(parentData.offset.dx, equals(10.0));
      expect(parentData.offset.dy, equals(0.0));
245 246 247 248
      expect(renderBox.size.width, equals(11.0));
      expect(renderBox.size.height, equals(12.0));

      tester.pumpWidget(
249 250 251 252 253 254 255 256 257 258
        new Stack(
          children: <Widget>[
            new Positioned(
              right: 10.0,
              width: 11.0,
              height: 12.0,
              child: new DecoratedBox(key: key, decoration: kBoxDecoration)
            )
          ]
        )
259 260 261 262 263 264 265 266 267 268 269
      );

      box = tester.findElementByKey(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));
270 271
      expect(parentData.offset.dx, equals(779.0));
      expect(parentData.offset.dy, equals(0.0));
272 273 274 275 276
      expect(renderBox.size.width, equals(11.0));
      expect(renderBox.size.height, equals(12.0));
    });
  });

277
}