custom_multi_child_layout_test.dart 5.35 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 7 8 9 10 11
import 'package:flutter/rendering.dart';
import 'package:flutter/widgets.dart';

class TestMultiChildLayoutDelegate extends MultiChildLayoutDelegate {
  BoxConstraints getSizeConstraints;

12
  @override
13
  Size getSize(BoxConstraints constraints) {
14
    if (!RenderObject.debugCheckingIntrinsics)
15
      getSizeConstraints = constraints;
16 17 18 19 20 21
    return new Size(200.0, 300.0);
  }

  Size performLayoutSize;
  Size performLayoutSize0;
  Size performLayoutSize1;
22
  bool performLayoutIsChild;
23

24
  @override
25
  void performLayout(Size size) {
26
    assert(!RenderObject.debugCheckingIntrinsics);
27 28
    expect(() {
      performLayoutSize = size;
29
      BoxConstraints constraints = new BoxConstraints.loose(size);
30 31
      performLayoutSize0 = layoutChild(0, constraints);
      performLayoutSize1 = layoutChild(1, constraints);
32
      performLayoutIsChild = hasChild('fred');
33
    }, returnsNormally);
34
  }
35 36 37

  bool shouldRelayoutCalled = false;
  bool shouldRelayoutValue = false;
38 39

  @override
40
  bool shouldRelayout(_) {
41
    assert(!RenderObject.debugCheckingIntrinsics);
42 43 44
    shouldRelayoutCalled = true;
    return shouldRelayoutValue;
  }
45 46
}

47 48
Widget buildFrame(MultiChildLayoutDelegate delegate) {
  return new Center(
49 50
    child: new CustomMultiChildLayout(
      children: <Widget>[
51 52 53 54 55 56 57 58
        new LayoutId(id: 0, child: new Container(width: 150.0, height: 100.0)),
        new LayoutId(id: 1, child: new Container(width: 100.0, height: 200.0)),
      ],
      delegate: delegate
    )
  );
}

59 60 61 62 63
class PreferredSizeDelegate extends MultiChildLayoutDelegate {
  PreferredSizeDelegate({ this.preferredSize });

  final Size preferredSize;

64
  @override
65 66
  Size getSize(BoxConstraints constraints) => preferredSize;

67
  @override
68 69
  void performLayout(Size size) { }

70
  @override
71 72 73 74
  bool shouldRelayout(PreferredSizeDelegate oldDelegate) {
    return preferredSize != oldDelegate.preferredSize;
  }
}
75

76
void main() {
77
  testWidgets('Control test for CustomMultiChildLayout', (WidgetTester tester) async {
78
    TestMultiChildLayoutDelegate delegate = new TestMultiChildLayoutDelegate();
79
    await tester.pumpWidget(buildFrame(delegate));
80 81 82 83 84 85 86 87 88 89 90 91 92

    expect(delegate.getSizeConstraints.minWidth, 0.0);
    expect(delegate.getSizeConstraints.maxWidth, 800.0);
    expect(delegate.getSizeConstraints.minHeight, 0.0);
    expect(delegate.getSizeConstraints.maxHeight, 600.0);

    expect(delegate.performLayoutSize.width, 200.0);
    expect(delegate.performLayoutSize.height, 300.0);
    expect(delegate.performLayoutSize0.width, 150.0);
    expect(delegate.performLayoutSize0.height, 100.0);
    expect(delegate.performLayoutSize1.width, 100.0);
    expect(delegate.performLayoutSize1.height, 200.0);
    expect(delegate.performLayoutIsChild, false);
93
  });
94

95
  testWidgets('Test MultiChildDelegate shouldRelayout method', (WidgetTester tester) async {
96
    TestMultiChildLayoutDelegate delegate = new TestMultiChildLayoutDelegate();
97
    await tester.pumpWidget(buildFrame(delegate));
98 99 100 101 102 103 104 105

    // Layout happened because the delegate was set.
    expect(delegate.performLayoutSize, isNotNull); // i.e. layout happened
    expect(delegate.shouldRelayoutCalled, isFalse);

    // Layout did not happen because shouldRelayout() returned false.
    delegate = new TestMultiChildLayoutDelegate();
    delegate.shouldRelayoutValue = false;
106
    await tester.pumpWidget(buildFrame(delegate));
107 108 109 110 111 112
    expect(delegate.shouldRelayoutCalled, isTrue);
    expect(delegate.performLayoutSize, isNull);

    // Layout happened because shouldRelayout() returned true.
    delegate = new TestMultiChildLayoutDelegate();
    delegate.shouldRelayoutValue = true;
113
    await tester.pumpWidget(buildFrame(delegate));
114 115
    expect(delegate.shouldRelayoutCalled, isTrue);
    expect(delegate.performLayoutSize, isNotNull);
116 117
  });

118
  testWidgets('Nested CustomMultiChildLayouts', (WidgetTester tester) async {
119
    TestMultiChildLayoutDelegate delegate = new TestMultiChildLayoutDelegate();
120
    await tester.pumpWidget(new Center(
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
      child: new CustomMultiChildLayout(
        children: <Widget>[
          new LayoutId(
            id: 0,
            child: new CustomMultiChildLayout(
              children: <Widget>[
                new LayoutId(id: 0, child: new Container(width: 150.0, height: 100.0)),
                new LayoutId(id: 1, child: new Container(width: 100.0, height: 200.0)),
              ],
              delegate: delegate
            )
          ),
          new LayoutId(id: 1, child: new Container(width: 100.0, height: 200.0)),
        ],
        delegate: delegate
      )
    ));

139
  });
140

141
  testWidgets('Loose constraints', (WidgetTester tester) async {
142
    Key key = new UniqueKey();
143
    await tester.pumpWidget(new Center(
144 145 146 147 148 149 150 151 152 153
      child: new CustomMultiChildLayout(
        key: key,
        delegate: new PreferredSizeDelegate(preferredSize: new Size(300.0, 200.0))
      )
    ));

    RenderBox box = tester.renderObject(find.byKey(key));
    expect(box.size.width, equals(300.0));
    expect(box.size.height, equals(200.0));

154
    await tester.pumpWidget(new Center(
155 156 157 158 159 160 161 162
      child: new CustomMultiChildLayout(
        key: key,
        delegate: new PreferredSizeDelegate(preferredSize: new Size(350.0, 250.0))
      )
    ));

    expect(box.size.width, equals(350.0));
    expect(box.size.height, equals(250.0));
163
  });
164
}