flex_test.dart 4.68 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
          child: new Stack(<Widget>[
20 21 22
            new Positioned(
              top: 10.0,
              left: 10.0,
23
              child: new Column(<Widget>[
24 25 26 27 28 29 30 31 32 33 34 35 36
                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')
                    )
37 38
                  )
                )
39 40 41 42 43
              ])
            )
          ])
        )
      );
44

45 46 47
      tester.tap(tester.findText('X'));
      expect(didReceiveTap, isTrue);
    });
48
  });
49 50 51 52 53 54 55 56 57 58 59 60 61 62

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

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

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

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

      // Column with justifyContent: FlexJustifyContent.collapse
      tester.pumpWidget(new Center(
        child: new Column([
          new Container(width: 100.0, height: 100.0),
          new Container(width: 100.0, height: 150.0)
        ],
          key: flexKey,
          justifyContent: FlexJustifyContent.collapse
        )
      ));
106 107 108
      renderBox = tester.findElementByKey(flexKey).renderObject;
      expect(renderBox.size.width, equals(100.0));
      expect(renderBox.size.height, equals(250.0));
109 110
    });
  });
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152

  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,
          child:  new Column([
            new Container(
              key: childKey,
              width: 100.0,
              height: 100.0
            )], justifyContent: FlexJustifyContent.collapse
          )
        )
      ));

      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,
          child:  new Row([
            new Container(
              key: childKey,
              width: 100.0,
              height: 100.0
            )], justifyContent: FlexJustifyContent.collapse
          )
        )
      ));

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