intrinsic_width_test.dart 16.8 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1 2 3 4
// Copyright 2015 The Chromium Authors. All rights reserved.
// 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';
Hixie's avatar
Hixie committed
6 7
import 'package:test/test.dart';

8
import 'rendering_tester.dart';
Hixie's avatar
Hixie committed
9

10
// before using this, consider using RenderSizedBox from rendering_tester.dart
Hixie's avatar
Hixie committed
11 12 13 14 15
class RenderTestBox extends RenderBox {
  RenderTestBox(this._intrinsicDimensions);

  final BoxConstraints _intrinsicDimensions;

16
  @override
17
  double computeMinIntrinsicWidth(double height) {
18
    return _intrinsicDimensions.minWidth;
Hixie's avatar
Hixie committed
19 20
  }

21
  @override
22
  double computeMaxIntrinsicWidth(double height) {
23
    return _intrinsicDimensions.maxWidth;
Hixie's avatar
Hixie committed
24 25
  }

26
  @override
27
  double computeMinIntrinsicHeight(double width) {
28
    return _intrinsicDimensions.minHeight;
Hixie's avatar
Hixie committed
29 30
  }

31
  @override
32
  double computeMaxIntrinsicHeight(double width) {
33
    return _intrinsicDimensions.maxHeight;
Hixie's avatar
Hixie committed
34 35
  }

36
  @override
Hixie's avatar
Hixie committed
37
  bool get sizedByParent => true;
38 39

  @override
Hixie's avatar
Hixie committed
40 41 42 43 44 45 46 47
  void performResize() {
    size = constraints.constrain(new Size(_intrinsicDimensions.minWidth + (_intrinsicDimensions.maxWidth-_intrinsicDimensions.minWidth) / 2.0,
                                          _intrinsicDimensions.minHeight + (_intrinsicDimensions.maxHeight-_intrinsicDimensions.minHeight) / 2.0));
  }
}

void main() {
  test('Shrink-wrapping width', () {
48 49
    final RenderBox child = new RenderTestBox(const BoxConstraints(minWidth: 10.0, maxWidth: 100.0, minHeight: 20.0, maxHeight: 200.0));
    final RenderBox parent = new RenderIntrinsicWidth(child: child);
Hixie's avatar
Hixie committed
50
    layout(parent,
51
      constraints: const BoxConstraints(
52 53 54 55 56 57
        minWidth: 5.0,
        minHeight: 8.0,
        maxWidth: 500.0,
        maxHeight: 800.0
      )
    );
Hixie's avatar
Hixie committed
58 59
    expect(parent.size.width, equals(100.0));
    expect(parent.size.height, equals(110.0));
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79

    expect(parent.getMinIntrinsicWidth(0.0), equals(100.0));
    expect(parent.getMaxIntrinsicWidth(0.0), equals(100.0));
    expect(parent.getMinIntrinsicHeight(0.0), equals(20.0));
    expect(parent.getMaxIntrinsicHeight(0.0), equals(200.0));

    expect(parent.getMinIntrinsicWidth(10.0), equals(100.0));
    expect(parent.getMaxIntrinsicWidth(10.0), equals(100.0));
    expect(parent.getMinIntrinsicHeight(10.0), equals(20.0));
    expect(parent.getMaxIntrinsicHeight(10.0), equals(200.0));

    expect(parent.getMinIntrinsicWidth(80.0), equals(100.0));
    expect(parent.getMaxIntrinsicWidth(80.0), equals(100.0));
    expect(parent.getMinIntrinsicHeight(80.0), equals(20.0));
    expect(parent.getMaxIntrinsicHeight(80.0), equals(200.0));

    expect(parent.getMinIntrinsicWidth(double.INFINITY), equals(100.0));
    expect(parent.getMaxIntrinsicWidth(double.INFINITY), equals(100.0));
    expect(parent.getMinIntrinsicHeight(double.INFINITY), equals(20.0));
    expect(parent.getMaxIntrinsicHeight(double.INFINITY), equals(200.0));
Hixie's avatar
Hixie committed
80 81
  });

Ian Hickson's avatar
Ian Hickson committed
82
  test('IntrinsicWidth without a child', () {
83
    final RenderBox parent = new RenderIntrinsicWidth();
Ian Hickson's avatar
Ian Hickson committed
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
    layout(parent,
      constraints: const BoxConstraints(
        minWidth: 5.0,
        minHeight: 8.0,
        maxWidth: 500.0,
        maxHeight: 800.0
      )
    );
    expect(parent.size.width, equals(5.0));
    expect(parent.size.height, equals(8.0));

    expect(parent.getMinIntrinsicWidth(0.0), equals(0.0));
    expect(parent.getMaxIntrinsicWidth(0.0), equals(0.0));
    expect(parent.getMinIntrinsicHeight(0.0), equals(0.0));
    expect(parent.getMaxIntrinsicHeight(0.0), equals(0.0));

    expect(parent.getMinIntrinsicWidth(10.0), equals(0.0));
    expect(parent.getMaxIntrinsicWidth(10.0), equals(0.0));
    expect(parent.getMinIntrinsicHeight(10.0), equals(0.0));
    expect(parent.getMaxIntrinsicHeight(10.0), equals(0.0));

    expect(parent.getMinIntrinsicWidth(80.0), equals(0.0));
    expect(parent.getMaxIntrinsicWidth(80.0), equals(0.0));
    expect(parent.getMinIntrinsicHeight(80.0), equals(0.0));
    expect(parent.getMaxIntrinsicHeight(80.0), equals(0.0));

    expect(parent.getMinIntrinsicWidth(double.INFINITY), equals(0.0));
    expect(parent.getMaxIntrinsicWidth(double.INFINITY), equals(0.0));
    expect(parent.getMinIntrinsicHeight(double.INFINITY), equals(0.0));
    expect(parent.getMaxIntrinsicHeight(double.INFINITY), equals(0.0));
  });

116
  test('Shrink-wrapping width (stepped width)', () {
117 118
    final RenderBox child = new RenderTestBox(const BoxConstraints(minWidth: 10.0, maxWidth: 100.0, minHeight: 20.0, maxHeight: 200.0));
    final RenderBox parent = new RenderIntrinsicWidth(child: child, stepWidth: 47.0);
119
    layout(parent,
120
      constraints: const BoxConstraints(
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
        minWidth: 5.0,
        minHeight: 8.0,
        maxWidth: 500.0,
        maxHeight: 800.0
      )
    );
    expect(parent.size.width, equals(3.0 * 47.0));
    expect(parent.size.height, equals(110.0));

    expect(parent.getMinIntrinsicWidth(0.0), equals(3.0 * 47.0));
    expect(parent.getMaxIntrinsicWidth(0.0), equals(3.0 * 47.0));
    expect(parent.getMinIntrinsicHeight(0.0), equals(20.0));
    expect(parent.getMaxIntrinsicHeight(0.0), equals(200.0));

    expect(parent.getMinIntrinsicWidth(10.0), equals(3.0 * 47.0));
    expect(parent.getMaxIntrinsicWidth(10.0), equals(3.0 * 47.0));
    expect(parent.getMinIntrinsicHeight(10.0), equals(20.0));
    expect(parent.getMaxIntrinsicHeight(10.0), equals(200.0));

    expect(parent.getMinIntrinsicWidth(80.0), equals(3.0 * 47.0));
    expect(parent.getMaxIntrinsicWidth(80.0), equals(3.0 * 47.0));
    expect(parent.getMinIntrinsicHeight(80.0), equals(20.0));
    expect(parent.getMaxIntrinsicHeight(80.0), equals(200.0));

    expect(parent.getMinIntrinsicWidth(double.INFINITY), equals(3.0 * 47.0));
    expect(parent.getMaxIntrinsicWidth(double.INFINITY), equals(3.0 * 47.0));
    expect(parent.getMinIntrinsicHeight(double.INFINITY), equals(20.0));
    expect(parent.getMaxIntrinsicHeight(double.INFINITY), equals(200.0));
  });

  test('Shrink-wrapping width (stepped height)', () {
152 153
    final RenderBox child = new RenderTestBox(const BoxConstraints(minWidth: 10.0, maxWidth: 100.0, minHeight: 20.0, maxHeight: 200.0));
    final RenderBox parent = new RenderIntrinsicWidth(child: child, stepHeight: 47.0);
154
    layout(parent,
155
      constraints: const BoxConstraints(
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
        minWidth: 5.0,
        minHeight: 8.0,
        maxWidth: 500.0,
        maxHeight: 800.0
      )
    );
    expect(parent.size.width, equals(100.0));
    expect(parent.size.height, equals(235.0));

    expect(parent.getMinIntrinsicWidth(0.0), equals(100.0));
    expect(parent.getMaxIntrinsicWidth(0.0), equals(100.0));
    expect(parent.getMinIntrinsicHeight(0.0), equals(1.0 * 47.0));
    expect(parent.getMaxIntrinsicHeight(0.0), equals(5.0 * 47.0));

    expect(parent.getMinIntrinsicWidth(10.0), equals(100.0));
    expect(parent.getMaxIntrinsicWidth(10.0), equals(100.0));
    expect(parent.getMinIntrinsicHeight(10.0), equals(1.0 * 47.0));
    expect(parent.getMaxIntrinsicHeight(10.0), equals(5.0 * 47.0));

    expect(parent.getMinIntrinsicWidth(80.0), equals(100.0));
    expect(parent.getMaxIntrinsicWidth(80.0), equals(100.0));
    expect(parent.getMinIntrinsicHeight(80.0), equals(1.0 * 47.0));
    expect(parent.getMaxIntrinsicHeight(80.0), equals(5.0 * 47.0));

    expect(parent.getMinIntrinsicWidth(double.INFINITY), equals(100.0));
    expect(parent.getMaxIntrinsicWidth(double.INFINITY), equals(100.0));
    expect(parent.getMinIntrinsicHeight(double.INFINITY), equals(1.0 * 47.0));
    expect(parent.getMaxIntrinsicHeight(double.INFINITY), equals(5.0 * 47.0));
  });

  test('Shrink-wrapping width (stepped everything)', () {
187 188
    final RenderBox child = new RenderTestBox(const BoxConstraints(minWidth: 10.0, maxWidth: 100.0, minHeight: 20.0, maxHeight: 200.0));
    final RenderBox parent = new RenderIntrinsicWidth(child: child, stepHeight: 47.0, stepWidth: 37.0);
189
    layout(parent,
190
      constraints: const BoxConstraints(
191 192 193 194 195 196 197 198
        minWidth: 5.0,
        minHeight: 8.0,
        maxWidth: 500.0,
        maxHeight: 800.0
      )
    );
    expect(parent.size.width, equals(3.0 * 37.0));
    expect(parent.size.height, equals(235.0));
Hixie's avatar
Hixie committed
199

200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
    expect(parent.getMinIntrinsicWidth(0.0), equals(3.0 * 37.0));
    expect(parent.getMaxIntrinsicWidth(0.0), equals(3.0 * 37.0));
    expect(parent.getMinIntrinsicHeight(0.0), equals(1.0 * 47.0));
    expect(parent.getMaxIntrinsicHeight(0.0), equals(5.0 * 47.0));

    expect(parent.getMinIntrinsicWidth(10.0), equals(3.0 * 37.0));
    expect(parent.getMaxIntrinsicWidth(10.0), equals(3.0 * 37.0));
    expect(parent.getMinIntrinsicHeight(10.0), equals(1.0 * 47.0));
    expect(parent.getMaxIntrinsicHeight(10.0), equals(5.0 * 47.0));

    expect(parent.getMinIntrinsicWidth(80.0), equals(3.0 * 37.0));
    expect(parent.getMaxIntrinsicWidth(80.0), equals(3.0 * 37.0));
    expect(parent.getMinIntrinsicHeight(80.0), equals(1.0 * 47.0));
    expect(parent.getMaxIntrinsicHeight(80.0), equals(5.0 * 47.0));

    expect(parent.getMinIntrinsicWidth(double.INFINITY), equals(3.0 * 37.0));
    expect(parent.getMaxIntrinsicWidth(double.INFINITY), equals(3.0 * 37.0));
    expect(parent.getMinIntrinsicHeight(double.INFINITY), equals(1.0 * 47.0));
    expect(parent.getMaxIntrinsicHeight(double.INFINITY), equals(5.0 * 47.0));
  });

  test('Shrink-wrapping height', () {
222 223
    final RenderBox child = new RenderTestBox(const BoxConstraints(minWidth: 10.0, maxWidth: 100.0, minHeight: 20.0, maxHeight: 200.0));
    final RenderBox parent = new RenderIntrinsicHeight(child: child);
Hixie's avatar
Hixie committed
224
    layout(parent,
225
      constraints: const BoxConstraints(
226 227 228 229 230 231
        minWidth: 5.0,
        minHeight: 8.0,
        maxWidth: 500.0,
        maxHeight: 800.0
      )
    );
Hixie's avatar
Hixie committed
232 233
    expect(parent.size.width, equals(55.0));
    expect(parent.size.height, equals(200.0));
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255

    expect(parent.getMinIntrinsicWidth(0.0), equals(10.0));
    expect(parent.getMaxIntrinsicWidth(0.0), equals(100.0));
    expect(parent.getMinIntrinsicHeight(0.0), equals(200.0));
    expect(parent.getMaxIntrinsicHeight(0.0), equals(200.0));

    expect(parent.getMinIntrinsicWidth(10.0), equals(10.0));
    expect(parent.getMaxIntrinsicWidth(10.0), equals(100.0));
    expect(parent.getMinIntrinsicHeight(10.0), equals(200.0));
    expect(parent.getMaxIntrinsicHeight(10.0), equals(200.0));

    expect(parent.getMinIntrinsicWidth(80.0), equals(10.0));
    expect(parent.getMaxIntrinsicWidth(80.0), equals(100.0));
    expect(parent.getMinIntrinsicHeight(80.0), equals(200.0));
    expect(parent.getMaxIntrinsicHeight(80.0), equals(200.0));

    expect(parent.getMinIntrinsicWidth(double.INFINITY), equals(10.0));
    expect(parent.getMaxIntrinsicWidth(double.INFINITY), equals(100.0));
    expect(parent.getMinIntrinsicHeight(double.INFINITY), equals(200.0));
    expect(parent.getMaxIntrinsicHeight(double.INFINITY), equals(200.0));
  });

Ian Hickson's avatar
Ian Hickson committed
256
  test('IntrinsicHeight without a child', () {
257
    final RenderBox parent = new RenderIntrinsicHeight();
Ian Hickson's avatar
Ian Hickson committed
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
    layout(parent,
      constraints: const BoxConstraints(
        minWidth: 5.0,
        minHeight: 8.0,
        maxWidth: 500.0,
        maxHeight: 800.0
      )
    );
    expect(parent.size.width, equals(5.0));
    expect(parent.size.height, equals(8.0));

    expect(parent.getMinIntrinsicWidth(0.0), equals(0.0));
    expect(parent.getMaxIntrinsicWidth(0.0), equals(0.0));
    expect(parent.getMinIntrinsicHeight(0.0), equals(0.0));
    expect(parent.getMaxIntrinsicHeight(0.0), equals(0.0));

    expect(parent.getMinIntrinsicWidth(10.0), equals(0.0));
    expect(parent.getMaxIntrinsicWidth(10.0), equals(0.0));
    expect(parent.getMinIntrinsicHeight(10.0), equals(0.0));
    expect(parent.getMaxIntrinsicHeight(10.0), equals(0.0));

    expect(parent.getMinIntrinsicWidth(80.0), equals(0.0));
    expect(parent.getMaxIntrinsicWidth(80.0), equals(0.0));
    expect(parent.getMinIntrinsicHeight(80.0), equals(0.0));
    expect(parent.getMaxIntrinsicHeight(80.0), equals(0.0));

    expect(parent.getMinIntrinsicWidth(double.INFINITY), equals(0.0));
    expect(parent.getMaxIntrinsicWidth(double.INFINITY), equals(0.0));
    expect(parent.getMinIntrinsicHeight(double.INFINITY), equals(0.0));
    expect(parent.getMaxIntrinsicHeight(double.INFINITY), equals(0.0));
  });

290
  test('Padding and boring intrinsics', () {
291
    final RenderBox box = new RenderPadding(
292
      padding: const EdgeInsets.all(15.0),
293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318
      child: new RenderSizedBox(const Size(20.0, 20.0))
    );

    expect(box.getMinIntrinsicWidth(0.0), 50.0);
    expect(box.getMaxIntrinsicWidth(0.0), 50.0);
    expect(box.getMinIntrinsicHeight(0.0), 50.0);
    expect(box.getMaxIntrinsicHeight(0.0), 50.0);

    expect(box.getMinIntrinsicWidth(10.0), 50.0);
    expect(box.getMaxIntrinsicWidth(10.0), 50.0);
    expect(box.getMinIntrinsicHeight(10.0), 50.0);
    expect(box.getMaxIntrinsicHeight(10.0), 50.0);

    expect(box.getMinIntrinsicWidth(80.0), 50.0);
    expect(box.getMaxIntrinsicWidth(80.0), 50.0);
    expect(box.getMinIntrinsicHeight(80.0), 50.0);
    expect(box.getMaxIntrinsicHeight(80.0), 50.0);

    expect(box.getMinIntrinsicWidth(double.INFINITY), 50.0);
    expect(box.getMaxIntrinsicWidth(double.INFINITY), 50.0);
    expect(box.getMinIntrinsicHeight(double.INFINITY), 50.0);
    expect(box.getMaxIntrinsicHeight(double.INFINITY), 50.0);

    // also a smoke test:
    layout(
      box,
319
      constraints: const BoxConstraints(
320 321 322 323 324 325 326 327 328
        minWidth: 10.0,
        minHeight: 10.0,
        maxWidth: 10.0,
        maxHeight: 10.0
      )
    );
  });

  test('Padding and interesting intrinsics', () {
329
    final RenderBox box = new RenderPadding(
330
      padding: const EdgeInsets.all(15.0),
331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
      child: new RenderAspectRatio(aspectRatio: 1.0)
    );

    expect(box.getMinIntrinsicWidth(0.0), 30.0);
    expect(box.getMaxIntrinsicWidth(0.0), 30.0);
    expect(box.getMinIntrinsicHeight(0.0), 30.0);
    expect(box.getMaxIntrinsicHeight(0.0), 30.0);

    expect(box.getMinIntrinsicWidth(10.0), 30.0);
    expect(box.getMaxIntrinsicWidth(10.0), 30.0);
    expect(box.getMinIntrinsicHeight(10.0), 30.0);
    expect(box.getMaxIntrinsicHeight(10.0), 30.0);

    expect(box.getMinIntrinsicWidth(80.0), 80.0);
    expect(box.getMaxIntrinsicWidth(80.0), 80.0);
    expect(box.getMinIntrinsicHeight(80.0), 80.0);
    expect(box.getMaxIntrinsicHeight(80.0), 80.0);

    expect(box.getMinIntrinsicWidth(double.INFINITY), 30.0);
    expect(box.getMaxIntrinsicWidth(double.INFINITY), 30.0);
    expect(box.getMinIntrinsicHeight(double.INFINITY), 30.0);
    expect(box.getMaxIntrinsicHeight(double.INFINITY), 30.0);

    // also a smoke test:
    layout(
      box,
357
      constraints: const BoxConstraints(
358 359 360 361 362 363
        minWidth: 10.0,
        minHeight: 10.0,
        maxWidth: 10.0,
        maxHeight: 10.0
      )
    );
Hixie's avatar
Hixie committed
364
  });
365 366

  test('Padding and boring intrinsics', () {
367
    final RenderBox box = new RenderPadding(
368
      padding: const EdgeInsets.all(15.0),
369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394
      child: new RenderSizedBox(const Size(20.0, 20.0))
    );

    expect(box.getMinIntrinsicWidth(0.0), 50.0);
    expect(box.getMaxIntrinsicWidth(0.0), 50.0);
    expect(box.getMinIntrinsicHeight(0.0), 50.0);
    expect(box.getMaxIntrinsicHeight(0.0), 50.0);

    expect(box.getMinIntrinsicWidth(10.0), 50.0);
    expect(box.getMaxIntrinsicWidth(10.0), 50.0);
    expect(box.getMinIntrinsicHeight(10.0), 50.0);
    expect(box.getMaxIntrinsicHeight(10.0), 50.0);

    expect(box.getMinIntrinsicWidth(80.0), 50.0);
    expect(box.getMaxIntrinsicWidth(80.0), 50.0);
    expect(box.getMinIntrinsicHeight(80.0), 50.0);
    expect(box.getMaxIntrinsicHeight(80.0), 50.0);

    expect(box.getMinIntrinsicWidth(double.INFINITY), 50.0);
    expect(box.getMaxIntrinsicWidth(double.INFINITY), 50.0);
    expect(box.getMinIntrinsicHeight(double.INFINITY), 50.0);
    expect(box.getMaxIntrinsicHeight(double.INFINITY), 50.0);

    // also a smoke test:
    layout(
      box,
395
      constraints: const BoxConstraints(
396 397 398 399 400 401 402 403 404
        minWidth: 10.0,
        minHeight: 10.0,
        maxWidth: 10.0,
        maxHeight: 10.0
      )
    );
  });

  test('Padding and interesting intrinsics', () {
405
    final RenderBox box = new RenderPadding(
406
      padding: const EdgeInsets.all(15.0),
407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432
      child: new RenderAspectRatio(aspectRatio: 1.0)
    );

    expect(box.getMinIntrinsicWidth(0.0), 30.0);
    expect(box.getMaxIntrinsicWidth(0.0), 30.0);
    expect(box.getMinIntrinsicHeight(0.0), 30.0);
    expect(box.getMaxIntrinsicHeight(0.0), 30.0);

    expect(box.getMinIntrinsicWidth(10.0), 30.0);
    expect(box.getMaxIntrinsicWidth(10.0), 30.0);
    expect(box.getMinIntrinsicHeight(10.0), 30.0);
    expect(box.getMaxIntrinsicHeight(10.0), 30.0);

    expect(box.getMinIntrinsicWidth(80.0), 80.0);
    expect(box.getMaxIntrinsicWidth(80.0), 80.0);
    expect(box.getMinIntrinsicHeight(80.0), 80.0);
    expect(box.getMaxIntrinsicHeight(80.0), 80.0);

    expect(box.getMinIntrinsicWidth(double.INFINITY), 30.0);
    expect(box.getMaxIntrinsicWidth(double.INFINITY), 30.0);
    expect(box.getMinIntrinsicHeight(double.INFINITY), 30.0);
    expect(box.getMaxIntrinsicHeight(double.INFINITY), 30.0);

    // also a smoke test:
    layout(
      box,
433
      constraints: const BoxConstraints(
434 435 436 437 438 439 440
        minWidth: 10.0,
        minHeight: 10.0,
        maxWidth: 10.0,
        maxHeight: 10.0
      )
    );
  });
Hixie's avatar
Hixie committed
441
}