custom_multi_child_layout_test.dart 5.29 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
    return const Size(200.0, 300.0);
17 18 19 20 21
  }

  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
      final BoxConstraints constraints = 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
Widget buildFrame(MultiChildLayoutDelegate delegate) {
48 49
  return Center(
    child: CustomMultiChildLayout(
50
      children: <Widget>[
51 52
        LayoutId(id: 0, child: Container(width: 150.0, height: 100.0)),
        LayoutId(id: 1, child: Container(width: 100.0, height: 200.0)),
53
      ],
54 55
      delegate: delegate,
    ),
56 57 58
  );
}

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
    final TestMultiChildLayoutDelegate delegate = 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 = TestMultiChildLayoutDelegate();
97
    await tester.pumpWidget(buildFrame(delegate));
98 99 100 101 102 103

    // 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.
104
    delegate = TestMultiChildLayoutDelegate();
105
    delegate.shouldRelayoutValue = false;
106
    await tester.pumpWidget(buildFrame(delegate));
107 108 109 110
    expect(delegate.shouldRelayoutCalled, isTrue);
    expect(delegate.performLayoutSize, isNull);

    // Layout happened because shouldRelayout() returned true.
111
    delegate = TestMultiChildLayoutDelegate();
112
    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 120 121
    final TestMultiChildLayoutDelegate delegate = TestMultiChildLayoutDelegate();
    await tester.pumpWidget(Center(
      child: CustomMultiChildLayout(
122
        children: <Widget>[
123
          LayoutId(
124
            id: 0,
125
            child: CustomMultiChildLayout(
126
              children: <Widget>[
127 128
                LayoutId(id: 0, child: Container(width: 150.0, height: 100.0)),
                LayoutId(id: 1, child: Container(width: 100.0, height: 200.0)),
129
              ],
130 131
              delegate: delegate,
            ),
132
          ),
133
          LayoutId(id: 1, child: Container(width: 100.0, height: 200.0)),
134
        ],
135 136
        delegate: delegate,
      ),
137 138
    ));

139
  });
140

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

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

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

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