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

Ian Hickson's avatar
Ian Hickson committed
5
import 'package:flutter/foundation.dart';
6
import 'package:flutter/rendering.dart';
Dan Field's avatar
Dan Field committed
7 8
import 'package:flutter_test/flutter_test.dart';

9
import 'rendering_tester.dart';
10 11

void main() {
Ian Hickson's avatar
Ian Hickson committed
12
  test('RenderAspectRatio: Intrinsic sizing 2.0', () {
13
    final RenderAspectRatio box = RenderAspectRatio(aspectRatio: 2.0);
14

15 16 17 18 19 20 21 22 23 24 25 26
    expect(box.getMinIntrinsicWidth(200.0), 400.0);
    expect(box.getMinIntrinsicWidth(400.0), 800.0);

    expect(box.getMaxIntrinsicWidth(200.0), 400.0);
    expect(box.getMaxIntrinsicWidth(400.0), 800.0);

    expect(box.getMinIntrinsicHeight(200.0), 100.0);
    expect(box.getMinIntrinsicHeight(400.0), 200.0);

    expect(box.getMaxIntrinsicHeight(200.0), 100.0);
    expect(box.getMaxIntrinsicHeight(400.0), 200.0);

27 28 29 30
    expect(box.getMinIntrinsicWidth(double.infinity), 0.0);
    expect(box.getMaxIntrinsicWidth(double.infinity), 0.0);
    expect(box.getMinIntrinsicHeight(double.infinity), 0.0);
    expect(box.getMaxIntrinsicHeight(double.infinity), 0.0);
31 32
  });

Ian Hickson's avatar
Ian Hickson committed
33
  test('RenderAspectRatio: Intrinsic sizing 0.5', () {
34
    final RenderAspectRatio box = RenderAspectRatio(aspectRatio: 0.5);
35 36 37 38 39 40 41 42 43 44 45 46 47

    expect(box.getMinIntrinsicWidth(200.0), 100.0);
    expect(box.getMinIntrinsicWidth(400.0), 200.0);

    expect(box.getMaxIntrinsicWidth(200.0), 100.0);
    expect(box.getMaxIntrinsicWidth(400.0), 200.0);

    expect(box.getMinIntrinsicHeight(200.0), 400.0);
    expect(box.getMinIntrinsicHeight(400.0), 800.0);

    expect(box.getMaxIntrinsicHeight(200.0), 400.0);
    expect(box.getMaxIntrinsicHeight(400.0), 800.0);

48 49 50 51
    expect(box.getMinIntrinsicWidth(double.infinity), 0.0);
    expect(box.getMaxIntrinsicWidth(double.infinity), 0.0);
    expect(box.getMinIntrinsicHeight(double.infinity), 0.0);
    expect(box.getMaxIntrinsicHeight(double.infinity), 0.0);
52 53
  });

Ian Hickson's avatar
Ian Hickson committed
54
  test('RenderAspectRatio: Intrinsic sizing 2.0', () {
55
    final RenderAspectRatio box = RenderAspectRatio(
56
      aspectRatio: 2.0,
57
      child: RenderSizedBox(const Size(90.0, 70.0)),
58 59 60 61 62 63 64 65 66 67 68 69 70 71
    );

    expect(box.getMinIntrinsicWidth(200.0), 400.0);
    expect(box.getMinIntrinsicWidth(400.0), 800.0);

    expect(box.getMaxIntrinsicWidth(200.0), 400.0);
    expect(box.getMaxIntrinsicWidth(400.0), 800.0);

    expect(box.getMinIntrinsicHeight(200.0), 100.0);
    expect(box.getMinIntrinsicHeight(400.0), 200.0);

    expect(box.getMaxIntrinsicHeight(200.0), 100.0);
    expect(box.getMaxIntrinsicHeight(400.0), 200.0);

72 73 74 75
    expect(box.getMinIntrinsicWidth(double.infinity), 90.0);
    expect(box.getMaxIntrinsicWidth(double.infinity), 90.0);
    expect(box.getMinIntrinsicHeight(double.infinity), 70.0);
    expect(box.getMaxIntrinsicHeight(double.infinity), 70.0);
76 77
  });

Ian Hickson's avatar
Ian Hickson committed
78
  test('RenderAspectRatio: Intrinsic sizing 0.5', () {
79
    final RenderAspectRatio box = RenderAspectRatio(
80
      aspectRatio: 0.5,
81
      child: RenderSizedBox(const Size(90.0, 70.0)),
82 83 84 85 86 87 88 89 90 91 92 93 94 95
    );

    expect(box.getMinIntrinsicWidth(200.0), 100.0);
    expect(box.getMinIntrinsicWidth(400.0), 200.0);

    expect(box.getMaxIntrinsicWidth(200.0), 100.0);
    expect(box.getMaxIntrinsicWidth(400.0), 200.0);

    expect(box.getMinIntrinsicHeight(200.0), 400.0);
    expect(box.getMinIntrinsicHeight(400.0), 800.0);

    expect(box.getMaxIntrinsicHeight(200.0), 400.0);
    expect(box.getMaxIntrinsicHeight(400.0), 800.0);

96 97 98 99
    expect(box.getMinIntrinsicWidth(double.infinity), 90.0);
    expect(box.getMaxIntrinsicWidth(double.infinity), 90.0);
    expect(box.getMinIntrinsicHeight(double.infinity), 70.0);
    expect(box.getMaxIntrinsicHeight(double.infinity), 70.0);
100
  });
Ian Hickson's avatar
Ian Hickson committed
101 102

  test('RenderAspectRatio: Unbounded', () {
103
    final RenderBox box = RenderConstrainedOverflowBox(
104 105
      maxWidth: double.infinity,
      maxHeight: double.infinity,
106
      child: RenderAspectRatio(
Ian Hickson's avatar
Ian Hickson committed
107
        aspectRatio: 0.5,
108
        child: RenderSizedBox(const Size(90.0, 70.0)),
Ian Hickson's avatar
Ian Hickson committed
109 110
      ),
    );
111

112
    final List<FlutterErrorDetails> errors = <FlutterErrorDetails>[];
113
    layout(box, onErrors: () {
114
      errors.addAll(renderer.takeAllFlutterErrorDetails());
115
    });
116
    expect(errors, hasLength(2));
Dan Field's avatar
Dan Field committed
117
    expect(errors.first.exception, isFlutterError);
118 119 120 121 122 123
    expect(errors.first.exception.toStringDeep(),
      'FlutterError\n'
      '   RenderAspectRatio has unbounded constraints.\n'
      '   This RenderAspectRatio was given an aspect ratio of 0.5 but was\n'
      '   given both unbounded width and unbounded height constraints.\n'
      '   Because both constraints were unbounded, this render object\n'
124
      "   doesn't know how much size to consume.\n"
125
    );
126
    // The second error message is a generic message generated by the Dart VM. Not worth testing.
Ian Hickson's avatar
Ian Hickson committed
127 128 129 130 131
  });

  test('RenderAspectRatio: Sizing', () {
    RenderConstrainedOverflowBox outside;
    RenderAspectRatio inside;
132 133
    layout(outside = RenderConstrainedOverflowBox(
      child: inside = RenderAspectRatio(aspectRatio: 1.0),
Ian Hickson's avatar
Ian Hickson committed
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
    ));
    pumpFrame();
    expect(inside.size, const Size(800.0, 600.0));
    outside.minWidth = 0.0;
    outside.minHeight = 0.0;

    outside.maxWidth = 100.0;
    outside.maxHeight = 90.0;
    pumpFrame();
    expect(inside.size, const Size(90.0, 90.0));

    outside.maxWidth = 90.0;
    outside.maxHeight = 100.0;
    pumpFrame();
    expect(inside.size, const Size(90.0, 90.0));

150
    outside.maxWidth = double.infinity;
Ian Hickson's avatar
Ian Hickson committed
151 152 153 154 155
    outside.maxHeight = 90.0;
    pumpFrame();
    expect(inside.size, const Size(90.0, 90.0));

    outside.maxWidth = 90.0;
156
    outside.maxHeight = double.infinity;
Ian Hickson's avatar
Ian Hickson committed
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
    pumpFrame();
    expect(inside.size, const Size(90.0, 90.0));

    inside.aspectRatio = 2.0;

    outside.maxWidth = 100.0;
    outside.maxHeight = 90.0;
    pumpFrame();
    expect(inside.size, const Size(100.0, 50.0));

    outside.maxWidth = 90.0;
    outside.maxHeight = 100.0;
    pumpFrame();
    expect(inside.size, const Size(90.0, 45.0));

172
    outside.maxWidth = double.infinity;
Ian Hickson's avatar
Ian Hickson committed
173 174 175 176 177
    outside.maxHeight = 90.0;
    pumpFrame();
    expect(inside.size, const Size(180.0, 90.0));

    outside.maxWidth = 90.0;
178
    outside.maxHeight = double.infinity;
Ian Hickson's avatar
Ian Hickson committed
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
    pumpFrame();
    expect(inside.size, const Size(90.0, 45.0));

    outside.minWidth = 80.0;
    outside.minHeight = 80.0;

    outside.maxWidth = 100.0;
    outside.maxHeight = 90.0;
    pumpFrame();
    expect(inside.size, const Size(100.0, 80.0));

    outside.maxWidth = 90.0;
    outside.maxHeight = 100.0;
    pumpFrame();
    expect(inside.size, const Size(90.0, 80.0));

195
    outside.maxWidth = double.infinity;
Ian Hickson's avatar
Ian Hickson committed
196 197 198 199 200
    outside.maxHeight = 90.0;
    pumpFrame();
    expect(inside.size, const Size(180.0, 90.0));

    outside.maxWidth = 90.0;
201
    outside.maxHeight = double.infinity;
Ian Hickson's avatar
Ian Hickson committed
202 203 204
    pumpFrame();
    expect(inside.size, const Size(90.0, 80.0));
  });
205
}