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

5
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();

13
  test('RenderPositionedBox expands', () {
14 15
    final RenderConstrainedBox sizer = RenderConstrainedBox(
      additionalConstraints: BoxConstraints.tight(const Size(100.0, 100.0)),
16
      child: RenderDecoratedBox(decoration: const BoxDecoration()),
17
    );
18 19
    final RenderPositionedBox positioner = RenderPositionedBox(child: sizer);
    layout(positioner, constraints: BoxConstraints.loose(const Size(200.0, 200.0)));
20

21 22
    expect(positioner.size.width, equals(200.0), reason: 'positioner width');
    expect(positioner.size.height, equals(200.0), reason: 'positioner height');
23 24 25
  });

  test('RenderPositionedBox shrink wraps', () {
26 27
    final RenderConstrainedBox sizer = RenderConstrainedBox(
      additionalConstraints: BoxConstraints.tight(const Size(100.0, 100.0)),
28
      child: RenderDecoratedBox(decoration: const BoxDecoration()),
29
    );
30 31
    final RenderPositionedBox positioner = RenderPositionedBox(child: sizer, widthFactor: 1.0);
    layout(positioner, constraints: BoxConstraints.loose(const Size(200.0, 200.0)));
32

33 34
    expect(positioner.size.width, equals(100.0), reason: 'positioner width');
    expect(positioner.size.height, equals(200.0), reason: 'positioner height');
35

36 37
    positioner.widthFactor = null;
    positioner.heightFactor = 1.0;
Hixie's avatar
Hixie committed
38
    pumpFrame();
39

40 41
    expect(positioner.size.width, equals(200.0), reason: 'positioner width');
    expect(positioner.size.height, equals(100.0), reason: 'positioner height');
42

43
    positioner.widthFactor = 1.0;
Hixie's avatar
Hixie committed
44
    pumpFrame();
45

46 47
    expect(positioner.size.width, equals(100.0), reason: 'positioner width');
    expect(positioner.size.height, equals(100.0), reason: 'positioner height');
48
  });
49 50

  test('RenderPositionedBox width and height factors', () {
51 52
    final RenderConstrainedBox sizer = RenderConstrainedBox(
      additionalConstraints: BoxConstraints.tight(const Size(100.0, 100.0)),
53
      child: RenderDecoratedBox(decoration: const BoxDecoration()),
54
    );
55 56
    final RenderPositionedBox positioner = RenderPositionedBox(child: sizer, widthFactor: 1.0, heightFactor: 0.0);
    layout(positioner, constraints: BoxConstraints.loose(const Size(200.0, 200.0)));
57

58 59 60 61
    expect(positioner.computeMinIntrinsicWidth(200), equals(100.0));
    expect(positioner.computeMaxIntrinsicWidth(200), equals(100.0));
    expect(positioner.computeMinIntrinsicHeight(200), equals(0));
    expect(positioner.computeMaxIntrinsicHeight(200), equals(0));
62 63 64 65 66 67 68
    expect(positioner.size.width, equals(100.0));
    expect(positioner.size.height, equals(0.0));

    positioner.widthFactor = 0.5;
    positioner.heightFactor = 0.5;
    pumpFrame();

69 70 71 72
    expect(positioner.computeMinIntrinsicWidth(200), equals(50.0));
    expect(positioner.computeMaxIntrinsicWidth(200), equals(50.0));
    expect(positioner.computeMinIntrinsicHeight(200), equals(50.0));
    expect(positioner.computeMaxIntrinsicHeight(200), equals(50.0));
73 74 75 76 77 78 79
    expect(positioner.size.width, equals(50.0));
    expect(positioner.size.height, equals(50.0));

    positioner.widthFactor = null;
    positioner.heightFactor = null;
    pumpFrame();

80 81 82 83
    expect(positioner.computeMinIntrinsicWidth(200), equals(100.0));
    expect(positioner.computeMaxIntrinsicWidth(200), equals(100.0));
    expect(positioner.computeMinIntrinsicHeight(200), equals(100.0));
    expect(positioner.computeMaxIntrinsicHeight(200), equals(100.0));
84 85
    expect(positioner.size.width, equals(200.0));
    expect(positioner.size.height, equals(200.0));
86
  });
87
}