aspect_ratio_test.dart 6.66 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.

5 6
// @dart = 2.8

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

11
import 'rendering_tester.dart';
12 13

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

17 18 19 20 21 22 23 24 25 26 27 28
    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);

29 30 31 32
    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);
33 34
  });

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

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

50 51 52 53
    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);
54 55
  });

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

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

74 75 76 77
    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);
78 79
  });

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

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

98 99 100 101
    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);
102
  });
Ian Hickson's avatar
Ian Hickson committed
103 104

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

114
    final List<FlutterErrorDetails> errors = <FlutterErrorDetails>[];
115
    layout(box, onErrors: () {
116
      errors.addAll(renderer.takeAllFlutterErrorDetails());
117
    });
118
    expect(errors, hasLength(2));
Dan Field's avatar
Dan Field committed
119
    expect(errors.first.exception, isFlutterError);
120 121 122 123 124 125
    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'
126
      "   doesn't know how much size to consume.\n"
127
    );
128
    // The second error message is a generic message generated by the Dart VM. Not worth testing.
Ian Hickson's avatar
Ian Hickson committed
129 130 131 132 133
  });

  test('RenderAspectRatio: Sizing', () {
    RenderConstrainedOverflowBox outside;
    RenderAspectRatio inside;
134 135
    layout(outside = RenderConstrainedOverflowBox(
      child: inside = RenderAspectRatio(aspectRatio: 1.0),
Ian Hickson's avatar
Ian Hickson committed
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
    ));
    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));

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

    outside.maxWidth = 90.0;
158
    outside.maxHeight = double.infinity;
Ian Hickson's avatar
Ian Hickson committed
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
    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));

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

    outside.maxWidth = 90.0;
180
    outside.maxHeight = double.infinity;
Ian Hickson's avatar
Ian Hickson committed
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
    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));

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

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