custom_one_child_layout_test.dart 3.86 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';
Adam Barth's avatar
Adam Barth committed
7 8 9
import 'package:flutter/widgets.dart';
import 'package:test/test.dart';

10
class TestSingleChildLayoutDelegate extends SingleChildLayoutDelegate {
Adam Barth's avatar
Adam Barth committed
11 12 13 14 15
  BoxConstraints constraintsFromGetSize;
  BoxConstraints constraintsFromGetConstraintsForChild;
  Size sizeFromGetPositionForChild;
  Size childSizeFromGetPositionForChild;

16
  @override
Adam Barth's avatar
Adam Barth committed
17
  Size getSize(BoxConstraints constraints) {
18
    if (!RenderObject.debugCheckingIntrinsics)
19
      constraintsFromGetSize = constraints;
Adam Barth's avatar
Adam Barth committed
20 21 22
    return new Size(200.0, 300.0);
  }

23
  @override
Adam Barth's avatar
Adam Barth committed
24
  BoxConstraints getConstraintsForChild(BoxConstraints constraints) {
25
    assert(!RenderObject.debugCheckingIntrinsics);
Adam Barth's avatar
Adam Barth committed
26 27 28 29 30 31 32 33 34
    constraintsFromGetConstraintsForChild = constraints;
    return new BoxConstraints(
      minWidth: 100.0,
      maxWidth: 150.0,
      minHeight: 200.0,
      maxHeight: 400.0
    );
  }

35
  @override
36
  Offset getPositionForChild(Size size, Size childSize) {
37
    assert(!RenderObject.debugCheckingIntrinsics);
Adam Barth's avatar
Adam Barth committed
38 39
    sizeFromGetPositionForChild = size;
    childSizeFromGetPositionForChild = childSize;
40
    return Offset.zero;
Adam Barth's avatar
Adam Barth committed
41
  }
42 43 44

  bool shouldRelayoutCalled = false;
  bool shouldRelayoutValue = false;
45 46

  @override
47
  bool shouldRelayout(_) {
48
    assert(!RenderObject.debugCheckingIntrinsics);
49 50 51 52 53
    shouldRelayoutCalled = true;
    return shouldRelayoutValue;
  }
}

54 55
Widget buildFrame(SingleChildLayoutDelegate delegate) {
  return new Center(child: new CustomSingleChildLayout(delegate: delegate, child: new Container()));
Adam Barth's avatar
Adam Barth committed
56 57 58
}

void main() {
59
  test('Control test for CustomSingleChildLayout', () {
Adam Barth's avatar
Adam Barth committed
60
    testWidgets((WidgetTester tester) {
61
      TestSingleChildLayoutDelegate delegate = new TestSingleChildLayoutDelegate();
62
      tester.pumpWidget(buildFrame(delegate));
Adam Barth's avatar
Adam Barth committed
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80

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

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

      expect(delegate.sizeFromGetPositionForChild.width, 200.0);
      expect(delegate.sizeFromGetPositionForChild.height, 300.0);

      expect(delegate.childSizeFromGetPositionForChild.width, 150.0);
      expect(delegate.childSizeFromGetPositionForChild.height, 400.0);
    });
  });
81

82
  test('Test SingleChildDelegate shouldRelayout method', () {
83
    testWidgets((WidgetTester tester) {
84
      TestSingleChildLayoutDelegate delegate = new TestSingleChildLayoutDelegate();
85 86 87 88 89 90 91
      tester.pumpWidget(buildFrame(delegate));

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

      // Layout did not happen because shouldRelayout() returned false.
92
      delegate = new TestSingleChildLayoutDelegate();
93 94 95 96 97 98
      delegate.shouldRelayoutValue = false;
      tester.pumpWidget(buildFrame(delegate));
      expect(delegate.shouldRelayoutCalled, isTrue);
      expect(delegate.constraintsFromGetConstraintsForChild, isNull);

      // Layout happened because shouldRelayout() returned true.
99
      delegate = new TestSingleChildLayoutDelegate();
100 101 102 103 104 105 106
      delegate.shouldRelayoutValue = true;
      tester.pumpWidget(buildFrame(delegate));
      expect(delegate.shouldRelayoutCalled, isTrue);
      expect(delegate.constraintsFromGetConstraintsForChild, isNotNull);
    });
  });

Adam Barth's avatar
Adam Barth committed
107
}