constraints_test.dart 2.57 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';
6
import 'package:flutter_test/flutter_test.dart';
7 8 9 10

import 'rendering_tester.dart';

void main() {
11 12
  TestRenderingFlutterBinding.ensureInitialized();

Ian Hickson's avatar
Ian Hickson committed
13
  test('RenderFractionallySizedBox constraints', () {
14
    RenderBox root, leaf, test;
15 16 17 18
    root = RenderPositionedBox(
      child: RenderConstrainedBox(
        additionalConstraints: BoxConstraints.tight(const Size(200.0, 200.0)),
        child: test = RenderFractionallySizedOverflowBox(
19 20
          widthFactor: 2.0,
          heightFactor: 0.5,
21
          child: leaf = RenderConstrainedBox(
22
            additionalConstraints: const BoxConstraints.expand(),
23 24 25
          ),
        ),
      ),
26 27 28 29 30 31 32 33 34
    );
    layout(root);
    expect(root.size.width, equals(800.0));
    expect(root.size.height, equals(600.0));
    expect(test.size.width, equals(200.0));
    expect(test.size.height, equals(200.0));
    expect(leaf.size.width, equals(400.0));
    expect(leaf.size.height, equals(100.0));
  });
35

Ian Hickson's avatar
Ian Hickson committed
36
  test('BoxConstraints with NaN', () {
37 38 39 40
    String result;

    result = 'no exception';
    try {
41
      const BoxConstraints constraints = BoxConstraints(minWidth: double.nan, maxWidth: double.nan, minHeight: 2.0, maxHeight: double.nan);
42
      assert(constraints.debugAssertIsValid());
43 44 45 46 47
    } on FlutterError catch (e) {
      result = '$e';
    }
    expect(result, equals(
      'BoxConstraints has NaN values in minWidth, maxWidth, and maxHeight.\n'
48
      'The offending constraints were:\n'
49
      '  BoxConstraints(NaN<=w<=NaN, 2.0<=h<=NaN; NOT NORMALIZED)',
50 51 52 53
    ));

    result = 'no exception';
    try {
54
      const BoxConstraints constraints = BoxConstraints(minHeight: double.nan);
55
      assert(constraints.debugAssertIsValid());
56 57 58 59 60
    } on FlutterError catch (e) {
      result = '$e';
    }
    expect(result, equals(
      'BoxConstraints has a NaN value in minHeight.\n'
61
      'The offending constraints were:\n'
62
      '  BoxConstraints(0.0<=w<=Infinity, NaN<=h<=Infinity; NOT NORMALIZED)',
63 64 65 66
    ));

    result = 'no exception';
    try {
67
      const BoxConstraints constraints = BoxConstraints(minHeight: double.nan, maxWidth: 0.0/0.0);
68
      assert(constraints.debugAssertIsValid());
69 70 71 72 73
    } on FlutterError catch (e) {
      result = '$e';
    }
    expect(result, equals(
      'BoxConstraints has NaN values in maxWidth and minHeight.\n'
74
      'The offending constraints were:\n'
75
      '  BoxConstraints(0.0<=w<=NaN, NaN<=h<=Infinity; NOT NORMALIZED)',
76 77
    ));
  });
78
}