stack_test.dart 5.62 KB
Newer Older
1
import 'package:flutter/rendering.dart';
2
import 'package:flutter/widgets.dart';
3 4
import 'package:test/test.dart';

Adam Barth's avatar
Adam Barth committed
5
import 'widget_tester.dart';
6 7

void main() {
Hans Muller's avatar
Hans Muller committed
8 9
  test('Can construct an empty Stack', () {
    testWidgets((WidgetTester tester) {
10
      tester.pumpWidget(new Stack(<Widget>[]));
Hans Muller's avatar
Hans Muller committed
11 12 13
    });
  });

14 15
  test('Can construct an empty Centered Stack', () {
    testWidgets((WidgetTester tester) {
16
      tester.pumpWidget(new Center(child: new Stack(<Widget>[])));
17 18 19
    });
  });

20
  test('Can change position data', () {
21 22
    testWidgets((WidgetTester tester) {
      Key key = new Key('container');
23

24
      tester.pumpWidget(
25
        new Stack(<Widget>[
26 27 28 29 30 31 32
          new Positioned(
            left: 10.0,
            child: new Container(
              key: key,
              width: 10.0,
              height: 10.0
            )
33
          )
34 35
        ])
      );
36

37 38 39 40 41 42 43 44 45
      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));
46

47
      tester.pumpWidget(
48
        new Stack(<Widget>[
49 50 51 52 53 54 55
          new Positioned(
            right: 10.0,
            child: new Container(
              key: key,
              width: 10.0,
              height: 10.0
            )
56
          )
57 58
        ])
      );
59

60
      container = tester.findElementByKey(key);
61 62 63 64 65
      parentData = container.renderObject.parentData;
      expect(parentData.top, isNull);
      expect(parentData.right, equals(10.0));
      expect(parentData.bottom, isNull);
      expect(parentData.left, isNull);
66
    });
67
  });
68 69

  test('Can remove parent data', () {
70 71 72
    testWidgets((WidgetTester tester) {
      Key key = new Key('container');
      Container container = new Container(key: key, width: 10.0, height: 10.0);
73

74
      tester.pumpWidget(new Stack(<Widget>[ new Positioned(left: 10.0, child: container) ]));
75
      Element containerElement = tester.findElementByKey(key);
76

77 78 79 80 81 82
      StackParentData parentData;
      parentData = containerElement.renderObject.parentData;
      expect(parentData.top, isNull);
      expect(parentData.right, isNull);
      expect(parentData.bottom, isNull);
      expect(parentData.left, equals(10.0));
83

84
      tester.pumpWidget(new Stack(<Widget>[ container ]));
85
      containerElement = tester.findElementByKey(key);
86

87 88 89 90 91
      parentData = containerElement.renderObject.parentData;
      expect(parentData.top, isNull);
      expect(parentData.right, isNull);
      expect(parentData.bottom, isNull);
      expect(parentData.left, isNull);
92
    });
93
  });
Hans Muller's avatar
Hans Muller committed
94 95 96 97 98 99 100 101

  test('Can align non-positioned children', () {
    testWidgets((WidgetTester tester) {
      Key child0Key = new Key('child0');
      Key child1Key = new Key('child1');

      tester.pumpWidget(
        new Center(
102
          child: new Stack(<Widget>[
Hans Muller's avatar
Hans Muller committed
103 104 105
              new Container(key: child0Key, width: 20.0, height: 20.0),
              new Container(key: child1Key, width: 10.0, height: 10.0)
            ],
106
            alignment: const FractionalOffset(0.5, 0.5)
Hans Muller's avatar
Hans Muller committed
107 108 109 110 111
          )
        )
      );

      Element child0 = tester.findElementByKey(child0Key);
112 113
      final StackParentData child0RenderObjectParentData = child0.renderObject.parentData;
      expect(child0RenderObjectParentData.position, equals(const Point(0.0, 0.0)));
Hans Muller's avatar
Hans Muller committed
114 115

      Element child1 = tester.findElementByKey(child1Key);
116 117
      final StackParentData child1RenderObjectParentData = child1.renderObject.parentData;
      expect(child1RenderObjectParentData.position, equals(const Point(5.0, 5.0)));
Hans Muller's avatar
Hans Muller committed
118 119 120 121 122
    });
  });

  test('Can construct an empty IndexedStack', () {
    testWidgets((WidgetTester tester) {
123
      tester.pumpWidget(new IndexedStack(<Widget>[]));
Hans Muller's avatar
Hans Muller committed
124 125 126
    });
  });

127 128
  test('Can construct an empty Centered IndexedStack', () {
    testWidgets((WidgetTester tester) {
129
      tester.pumpWidget(new Center(child: new IndexedStack(<Widget>[])));
130 131 132
    });
  });

Hans Muller's avatar
Hans Muller committed
133 134 135 136 137 138
  test('Can construct an IndexedStack', () {
    testWidgets((WidgetTester tester) {
      int itemCount = 3;
      List<int> itemsPainted;

      Widget buildFrame(int index) {
139 140
        itemsPainted = <int>[];
        List<Widget> items = new List<Widget>.generate(itemCount, (i) {
141
          return new CustomPaint(child: new Text('$i'), onPaint: (_, __) { itemsPainted.add(i); });
Hans Muller's avatar
Hans Muller committed
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
        });
        return new Center(child: new IndexedStack(items, index: index));
      }

      tester.pumpWidget(buildFrame(0));
      expect(tester.findText('0'), isNotNull);
      expect(tester.findText('1'), isNotNull);
      expect(tester.findText('2'), isNotNull);
      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) {
167 168
        itemsTapped = <int>[];
        List<Widget> items = new List<Widget>.generate(itemCount, (i) {
Hans Muller's avatar
Hans Muller committed
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
          return new GestureDetector(child: new Text('$i'), onTap: () { itemsTapped.add(i); });
        });
        return new Center(child: new IndexedStack(items, key: key, index: index));
      }

      tester.pumpWidget(buildFrame(0));
      expect(itemsTapped, isEmpty);
      tester.tap(tester.findElementByKey(key));
      expect(itemsTapped, [0]);

      tester.pumpWidget(buildFrame(2));
      expect(itemsTapped, isEmpty);
      tester.tap(tester.findElementByKey(key));
      expect(itemsTapped, [2]);
    });
  });

186
}