flex_test.dart 5.1 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 10 11
import 'package:test/test.dart';

void main() {
  test('Can hit test flex children of stacks', () {
12 13 14 15 16 17 18
    testWidgets((WidgetTester tester) {
      bool didReceiveTap = false;
      tester.pumpWidget(
        new Container(
          decoration: const BoxDecoration(
            backgroundColor: const Color(0xFF00FF00)
          ),
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
          child: new Stack(
            children: <Widget>[
              new Positioned(
                top: 10.0,
                left: 10.0,
                child: new Column(
                  children: <Widget>[
                    new GestureDetector(
                      onTap: () {
                        didReceiveTap = true;
                      },
                      child: new Container(
                        decoration: const BoxDecoration(
                          backgroundColor: const Color(0xFF0000FF)
                        ),
                        width: 100.0,
                        height: 100.0,
                        child: new Center(
                          child: new Text('X')
                        )
                      )
40
                    )
41
                  ]
42
                )
43 44 45
              )
            ]
          )
46 47
        )
      );
48

49 50 51
      tester.tap(tester.findText('X'));
      expect(didReceiveTap, isTrue);
    });
52
  });
53 54 55 56 57 58 59

  test('Row, Column and FlexJustifyContent.collapse', () {
    final Key flexKey = new Key('flexKey');

    // Row without justifyContent: FlexJustifyContent.collapse
    testWidgets((WidgetTester tester) {
      tester.pumpWidget(new Center(
60 61 62 63 64
        child: new Row(
          children: <Widget>[
            new Container(width: 10.0, height: 100.0),
            new Container(width: 30.0, height: 100.0)
          ],
65 66 67
          key: flexKey
        )
      ));
68 69 70
      RenderBox renderBox = tester.findElementByKey(flexKey).renderObject;
      expect(renderBox.size.width, equals(800.0));
      expect(renderBox.size.height, equals(100.0));
71 72 73

      // Row with justifyContent: FlexJustifyContent.collapse
      tester.pumpWidget(new Center(
74 75 76 77 78
        child: new Row(
          children: <Widget>[
            new Container(width: 10.0, height: 100.0),
            new Container(width: 30.0, height: 100.0)
          ],
79 80 81 82
          key: flexKey,
          justifyContent: FlexJustifyContent.collapse
        )
      ));
83 84 85
      renderBox = tester.findElementByKey(flexKey).renderObject;
      expect(renderBox.size.width, equals(40.0));
      expect(renderBox.size.height, equals(100.0));
86 87 88 89 90
    });

    // Column without justifyContent: FlexJustifyContent.collapse
    testWidgets((WidgetTester tester) {
      tester.pumpWidget(new Center(
91 92 93 94 95
        child: new Column(
          children: <Widget>[
            new Container(width: 100.0, height: 100.0),
            new Container(width: 100.0, height: 150.0)
          ],
96 97 98
          key: flexKey
        )
      ));
99 100 101
      RenderBox renderBox = tester.findElementByKey(flexKey).renderObject;
      expect(renderBox.size.width, equals(100.0));
      expect(renderBox.size.height, equals(600.0));
102 103 104

      // Column with justifyContent: FlexJustifyContent.collapse
      tester.pumpWidget(new Center(
105 106 107 108 109
        child: new Column(
          children: <Widget>[
            new Container(width: 100.0, height: 100.0),
            new Container(width: 100.0, height: 150.0)
          ],
110 111 112 113
          key: flexKey,
          justifyContent: FlexJustifyContent.collapse
        )
      ));
114 115 116
      renderBox = tester.findElementByKey(flexKey).renderObject;
      expect(renderBox.size.width, equals(100.0));
      expect(renderBox.size.height, equals(250.0));
117 118
    });
  });
119 120 121 122 123 124 125 126 127

  test('Can layout at zero size', () {
    final Key childKey = new Key('childKey');

    testWidgets((WidgetTester tester) {
      tester.pumpWidget(new Center(
        child: new Container(
          width: 0.0,
          height: 0.0,
128 129 130 131 132 133 134 135 136
          child:  new Column(
            children: <Widget>[
              new Container(
                key: childKey,
                width: 100.0,
                height: 100.0
              )
            ],
            justifyContent: FlexJustifyContent.collapse
137 138 139 140 141 142 143 144 145 146 147 148
          )
        )
      ));

      RenderBox renderBox = tester.findElementByKey(childKey).renderObject;
      expect(renderBox.size.width, equals(0.0));
      expect(renderBox.size.height, equals(100.0));

      tester.pumpWidget(new Center(
        child: new Container(
          width: 0.0,
          height: 0.0,
149 150 151 152 153 154 155 156 157
          child:  new Row(
            children: <Widget>[
              new Container(
                key: childKey,
                width: 100.0,
                height: 100.0
              )
            ],
            justifyContent: FlexJustifyContent.collapse
158 159 160 161 162 163 164 165 166
          )
        )
      ));

      renderBox = tester.findElementByKey(childKey).renderObject;
      expect(renderBox.size.width, equals(100.0));
      expect(renderBox.size.height, equals(0.0));
    });
  });
167
}