box_constraints_test.dart 5.53 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4 5
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'package:flutter/rendering.dart';
Dan Field's avatar
Dan Field committed
6
import 'package:flutter_test/flutter_test.dart';
7 8 9 10

void main() {
  test('BoxConstraints toString', () {
    expect(const BoxConstraints.expand().toString(), contains('biggest'));
11 12
    expect(const BoxConstraints().toString(), contains('unconstrained'));
    expect(const BoxConstraints.tightFor(width: 50.0).toString(), contains('w=50'));
13 14 15
  });

  test('BoxConstraints copyWith', () {
16
    const BoxConstraints constraints = BoxConstraints(
17 18 19
      minWidth: 3.0,
      maxWidth: 7.0,
      minHeight: 11.0,
20
      maxHeight: 17.0,
21 22 23 24 25 26 27
    );
    BoxConstraints copy = constraints.copyWith();
    expect(copy, equals(constraints));
    copy = constraints.copyWith(
      minWidth: 13.0,
      maxWidth: 17.0,
      minHeight: 111.0,
28
      maxHeight: 117.0,
29 30 31 32 33 34 35 36 37 38
    );
    expect(copy.minWidth, 13.0);
    expect(copy.maxWidth, 17.0);
    expect(copy.minHeight, 111.0);
    expect(copy.maxHeight, 117.0);
    expect(copy, isNot(equals(constraints)));
    expect(copy.hashCode, isNot(equals(constraints.hashCode)));
  });

  test('BoxConstraints operators', () {
39
    const BoxConstraints constraints = BoxConstraints(
40 41 42
      minWidth: 3.0,
      maxWidth: 7.0,
      minHeight: 11.0,
43
      maxHeight: 17.0,
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
    );
    BoxConstraints copy = constraints * 2.0;
    expect(copy.minWidth, 6.0);
    expect(copy.maxWidth, 14.0);
    expect(copy.minHeight, 22.0);
    expect(copy.maxHeight, 34.0);
    expect(copy / 2.0, equals(constraints));
    copy = constraints ~/ 2.0;
    expect(copy.minWidth, 1.0);
    expect(copy.maxWidth, 3.0);
    expect(copy.minHeight, 5.0);
    expect(copy.maxHeight, 8.0);
    copy = constraints % 3.0;
    expect(copy.minWidth, 0.0);
    expect(copy.maxWidth, 1.0);
    expect(copy.minHeight, 2.0);
    expect(copy.maxHeight, 2.0);
  });

  test('BoxConstraints lerp', () {
    expect(BoxConstraints.lerp(null, null, 0.5), isNull);
65
    const BoxConstraints constraints = BoxConstraints(
66 67 68
      minWidth: 3.0,
      maxWidth: 7.0,
      minHeight: 11.0,
69
      maxHeight: 17.0,
70
    );
71
    BoxConstraints copy = BoxConstraints.lerp(null, constraints, 0.5)!;
72 73 74 75
    expect(copy.minWidth, moreOrLessEquals(1.5));
    expect(copy.maxWidth, moreOrLessEquals(3.5));
    expect(copy.minHeight, moreOrLessEquals(5.5));
    expect(copy.maxHeight, moreOrLessEquals(8.5));
76
    copy = BoxConstraints.lerp(constraints, null, 0.5)!;
77 78 79 80
    expect(copy.minWidth, moreOrLessEquals(1.5));
    expect(copy.maxWidth, moreOrLessEquals(3.5));
    expect(copy.minHeight, moreOrLessEquals(5.5));
    expect(copy.maxHeight, moreOrLessEquals(8.5));
81
    copy = BoxConstraints.lerp(const BoxConstraints(
82 83 84
      minWidth: 13.0,
      maxWidth: 17.0,
      minHeight: 111.0,
85
      maxHeight: 117.0,
86
    ), constraints, 0.2)!;
87 88 89 90
    expect(copy.minWidth, moreOrLessEquals(11.0));
    expect(copy.maxWidth, moreOrLessEquals(15.0));
    expect(copy.minHeight, moreOrLessEquals(91.0));
    expect(copy.maxHeight, moreOrLessEquals(97.0));
91 92
  });

93 94 95 96 97 98
  test('BoxConstraints.lerp identical a,b', () {
    expect(BoxConstraints.lerp(null, null, 0), null);
    const BoxConstraints constraints = BoxConstraints();
    expect(identical(BoxConstraints.lerp(constraints, constraints, 0.5), constraints), true);
  });

99
  test('BoxConstraints lerp with unbounded width', () {
100
    const BoxConstraints constraints1 = BoxConstraints(
101
      minWidth: double.infinity,
102 103 104
      minHeight: 10.0,
      maxHeight: 20.0,
    );
105
    const BoxConstraints constraints2 = BoxConstraints(
106
      minWidth: double.infinity,
107 108 109
      minHeight: 20.0,
      maxHeight: 30.0,
    );
110
    const BoxConstraints constraints3 = BoxConstraints(
111
      minWidth: double.infinity,
112 113 114 115 116 117 118
      minHeight: 15.0,
      maxHeight: 25.0,
    );
    expect(BoxConstraints.lerp(constraints1, constraints2, 0.5), constraints3);
  });

  test('BoxConstraints lerp with unbounded height', () {
119
    const BoxConstraints constraints1 = BoxConstraints(
120 121
      minWidth: 10.0,
      maxWidth: 20.0,
122
      minHeight: double.infinity,
123
    );
124
    const BoxConstraints constraints2 = BoxConstraints(
125 126
      minWidth: 20.0,
      maxWidth: 30.0,
127
      minHeight: double.infinity,
128
    );
129
    const BoxConstraints constraints3 = BoxConstraints(
130 131
      minWidth: 15.0,
      maxWidth: 25.0,
132
      minHeight: double.infinity,
133 134 135 136 137
    );
    expect(BoxConstraints.lerp(constraints1, constraints2, 0.5), constraints3);
  });

  test('BoxConstraints lerp from bounded to unbounded', () {
138
    const BoxConstraints constraints1 = BoxConstraints(
139 140
      minWidth: double.infinity,
      minHeight: double.infinity,
141
    );
142
    const BoxConstraints constraints2 = BoxConstraints(
143 144
      minWidth: 20.0,
      maxWidth: 30.0,
145
      minHeight: double.infinity,
146
    );
147
    const BoxConstraints constraints3 = BoxConstraints(
148
      minWidth: double.infinity,
149 150 151
      minHeight: 20.0,
      maxHeight: 30.0,
    );
Dan Field's avatar
Dan Field committed
152 153 154
    expect(() => BoxConstraints.lerp(constraints1, constraints2, 0.5), throwsAssertionError);
    expect(() => BoxConstraints.lerp(constraints1, constraints3, 0.5), throwsAssertionError);
    expect(() => BoxConstraints.lerp(constraints2, constraints3, 0.5), throwsAssertionError);
155 156
  });

157
  test('BoxConstraints normalize', () {
158
    const BoxConstraints constraints = BoxConstraints(
159 160 161
      minWidth: 3.0,
      maxWidth: 2.0,
      minHeight: 11.0,
162
      maxHeight: 18.0,
163
    );
164
    final BoxConstraints copy = constraints.normalize();
165 166 167 168 169 170
    expect(copy.minWidth, 3.0);
    expect(copy.maxWidth, 3.0);
    expect(copy.minHeight, 11.0);
    expect(copy.maxHeight, 18.0);
  });
}