custom_multi_child_layout_test.dart 6.31 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 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
class NotifierLayoutDelegate extends MultiChildLayoutDelegate {
  NotifierLayoutDelegate(this.size) : super(relayout: size);

  final ValueNotifier<Size> size;

  @override
  Size getSize(BoxConstraints constraints) => size.value;

  @override
  void performLayout(Size size) { }

  @override
  bool shouldRelayout(NotifierLayoutDelegate oldDelegate) {
    return size != oldDelegate.size;
  }
}

93
void main() {
94
  testWidgets('Control test for CustomMultiChildLayout', (WidgetTester tester) async {
95
    final TestMultiChildLayoutDelegate delegate = TestMultiChildLayoutDelegate();
96
    await tester.pumpWidget(buildFrame(delegate));
97 98 99 100 101 102 103 104 105 106 107 108 109

    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);
110
  });
111

112
  testWidgets('Test MultiChildDelegate shouldRelayout method', (WidgetTester tester) async {
113
    TestMultiChildLayoutDelegate delegate = TestMultiChildLayoutDelegate();
114
    await tester.pumpWidget(buildFrame(delegate));
115 116 117 118 119 120

    // 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.
121
    delegate = TestMultiChildLayoutDelegate();
122
    delegate.shouldRelayoutValue = false;
123
    await tester.pumpWidget(buildFrame(delegate));
124 125 126 127
    expect(delegate.shouldRelayoutCalled, isTrue);
    expect(delegate.performLayoutSize, isNull);

    // Layout happened because shouldRelayout() returned true.
128
    delegate = TestMultiChildLayoutDelegate();
129
    delegate.shouldRelayoutValue = true;
130
    await tester.pumpWidget(buildFrame(delegate));
131 132
    expect(delegate.shouldRelayoutCalled, isTrue);
    expect(delegate.performLayoutSize, isNotNull);
133 134
  });

135
  testWidgets('Nested CustomMultiChildLayouts', (WidgetTester tester) async {
136 137 138
    final TestMultiChildLayoutDelegate delegate = TestMultiChildLayoutDelegate();
    await tester.pumpWidget(Center(
      child: CustomMultiChildLayout(
139
        children: <Widget>[
140
          LayoutId(
141
            id: 0,
142
            child: CustomMultiChildLayout(
143
              children: <Widget>[
144 145
                LayoutId(id: 0, child: Container(width: 150.0, height: 100.0)),
                LayoutId(id: 1, child: Container(width: 100.0, height: 200.0)),
146
              ],
147 148
              delegate: delegate,
            ),
149
          ),
150
          LayoutId(id: 1, child: Container(width: 100.0, height: 200.0)),
151
        ],
152 153
        delegate: delegate,
      ),
154 155
    ));

156
  });
157

158
  testWidgets('Loose constraints', (WidgetTester tester) async {
159 160 161
    final Key key = UniqueKey();
    await tester.pumpWidget(Center(
      child: CustomMultiChildLayout(
162
        key: key,
163 164
        delegate: PreferredSizeDelegate(preferredSize: const Size(300.0, 200.0)),
      ),
165 166
    ));

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

171 172
    await tester.pumpWidget(Center(
      child: CustomMultiChildLayout(
173
        key: key,
174 175
        delegate: PreferredSizeDelegate(preferredSize: const Size(350.0, 250.0)),
      ),
176 177 178 179
    ));

    expect(box.size.width, equals(350.0));
    expect(box.size.height, equals(250.0));
180
  });
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201

  testWidgets('Can use listener for relayout', (WidgetTester tester) async {
    final ValueNotifier<Size> size = ValueNotifier<Size>(const Size(100.0, 200.0));

    await tester.pumpWidget(
      Center(
        child: CustomMultiChildLayout(
          delegate: NotifierLayoutDelegate(size),
        ),
      ),
    );

    RenderBox box = tester.renderObject(find.byType(CustomMultiChildLayout));
    expect(box.size, equals(const Size(100.0, 200.0)));

    size.value = const Size(150.0, 240.0);
    await tester.pump();

    box = tester.renderObject(find.byType(CustomMultiChildLayout));
    expect(box.size, equals(const Size(150.0, 240.0)));
  });
202
}