custom_one_child_layout_test.dart 3.52 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';
Adam Barth's avatar
Adam Barth committed
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
import 'package:flutter/widgets.dart';
import 'package:test/test.dart';

class TestOneChildLayoutDelegate extends OneChildLayoutDelegate {
  BoxConstraints constraintsFromGetSize;
  BoxConstraints constraintsFromGetConstraintsForChild;
  Size sizeFromGetPositionForChild;
  Size childSizeFromGetPositionForChild;

  Size getSize(BoxConstraints constraints) {
    constraintsFromGetSize = constraints;
    return new Size(200.0, 300.0);
  }

  BoxConstraints getConstraintsForChild(BoxConstraints constraints) {
    constraintsFromGetConstraintsForChild = constraints;
    return new BoxConstraints(
      minWidth: 100.0,
      maxWidth: 150.0,
      minHeight: 200.0,
      maxHeight: 400.0
    );
  }

30
  Offset getPositionForChild(Size size, Size childSize) {
Adam Barth's avatar
Adam Barth committed
31 32
    sizeFromGetPositionForChild = size;
    childSizeFromGetPositionForChild = childSize;
33
    return Offset.zero;
Adam Barth's avatar
Adam Barth committed
34
  }
35 36 37 38 39 40 41 42 43 44 45

  bool shouldRelayoutCalled = false;
  bool shouldRelayoutValue = false;
  bool shouldRelayout(_) {
    shouldRelayoutCalled = true;
    return shouldRelayoutValue;
  }
}

Widget buildFrame(delegate) {
  return new Center(child: new CustomOneChildLayout(delegate: delegate, child: new Container()));
Adam Barth's avatar
Adam Barth committed
46 47 48 49 50 51
}

void main() {
  test('Control test for CustomOneChildLayout', () {
    testWidgets((WidgetTester tester) {
      TestOneChildLayoutDelegate delegate = new TestOneChildLayoutDelegate();
52
      tester.pumpWidget(buildFrame(delegate));
Adam Barth's avatar
Adam Barth committed
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70

      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);
    });
  });
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96

  test('Test OneChildDelegate shouldRelayout method', () {
    testWidgets((WidgetTester tester) {
      TestOneChildLayoutDelegate delegate = new TestOneChildLayoutDelegate();
      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.
      delegate = new TestOneChildLayoutDelegate();
      delegate.shouldRelayoutValue = false;
      tester.pumpWidget(buildFrame(delegate));
      expect(delegate.shouldRelayoutCalled, isTrue);
      expect(delegate.constraintsFromGetConstraintsForChild, isNull);

      // Layout happened because shouldRelayout() returned true.
      delegate = new TestOneChildLayoutDelegate();
      delegate.shouldRelayoutValue = true;
      tester.pumpWidget(buildFrame(delegate));
      expect(delegate.shouldRelayoutCalled, isTrue);
      expect(delegate.constraintsFromGetConstraintsForChild, isNotNull);
    });
  });

Adam Barth's avatar
Adam Barth committed
97
}