fitted_box_test.dart 18.3 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
Adam Barth's avatar
Adam Barth 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 6
// @dart = 2.8

Adam Barth's avatar
Adam Barth committed
7 8 9 10 11 12
import 'package:flutter_test/flutter_test.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter/widgets.dart';

void main() {
  testWidgets('Can size according to aspect ratio', (WidgetTester tester) async {
13 14
    final Key outside = UniqueKey();
    final Key inside = UniqueKey();
Adam Barth's avatar
Adam Barth committed
15 16

    await tester.pumpWidget(
17 18
      Center(
        child: Container(
Adam Barth's avatar
Adam Barth committed
19
          width: 200.0,
20
          child: FittedBox(
Adam Barth's avatar
Adam Barth committed
21
            key: outside,
22
            child: Container(
Adam Barth's avatar
Adam Barth committed
23 24 25
              key: inside,
              width: 100.0,
              height: 50.0,
26 27 28
            ),
          ),
        ),
29
      ),
Adam Barth's avatar
Adam Barth committed
30 31
    );

32
    final RenderBox outsideBox = tester.firstRenderObject(find.byKey(outside));
Adam Barth's avatar
Adam Barth committed
33 34 35
    expect(outsideBox.size.width, 200.0);
    expect(outsideBox.size.height, 100.0);

36
    final RenderBox insideBox = tester.firstRenderObject(find.byKey(inside));
Adam Barth's avatar
Adam Barth committed
37 38 39
    expect(insideBox.size.width, 100.0);
    expect(insideBox.size.height, 50.0);

40 41
    final Offset insidePoint = insideBox.localToGlobal(const Offset(100.0, 50.0));
    final Offset outsidePoint = outsideBox.localToGlobal(const Offset(200.0, 100.0));
Adam Barth's avatar
Adam Barth committed
42

43
    expect(outsidePoint, equals(const Offset(500.0, 350.0)));
Adam Barth's avatar
Adam Barth committed
44 45 46 47
    expect(insidePoint, equals(outsidePoint));
  });

  testWidgets('Can contain child', (WidgetTester tester) async {
48 49
    final Key outside = UniqueKey();
    final Key inside = UniqueKey();
Adam Barth's avatar
Adam Barth committed
50 51

    await tester.pumpWidget(
52 53
      Center(
        child: Container(
Adam Barth's avatar
Adam Barth committed
54 55
          width: 200.0,
          height: 200.0,
56
          child: FittedBox(
Adam Barth's avatar
Adam Barth committed
57
            key: outside,
58
            child: Container(
Adam Barth's avatar
Adam Barth committed
59 60 61
              key: inside,
              width: 100.0,
              height: 50.0,
62 63 64
            ),
          ),
        ),
65
      ),
Adam Barth's avatar
Adam Barth committed
66 67
    );

68
    final RenderBox outsideBox = tester.firstRenderObject(find.byKey(outside));
Adam Barth's avatar
Adam Barth committed
69 70 71
    expect(outsideBox.size.width, 200.0);
    expect(outsideBox.size.height, 200.0);

72
    final RenderBox insideBox = tester.firstRenderObject(find.byKey(inside));
Adam Barth's avatar
Adam Barth committed
73 74 75
    expect(insideBox.size.width, 100.0);
    expect(insideBox.size.height, 50.0);

76 77
    final Offset insidePoint = insideBox.localToGlobal(const Offset(100.0, 0.0));
    final Offset outsidePoint = outsideBox.localToGlobal(const Offset(200.0, 50.0));
Adam Barth's avatar
Adam Barth committed
78 79 80 81

    expect(insidePoint, equals(outsidePoint));
  });

82
  testWidgets('Child can cover', (WidgetTester tester) async {
83 84
    final Key outside = UniqueKey();
    final Key inside = UniqueKey();
Adam Barth's avatar
Adam Barth committed
85 86

    await tester.pumpWidget(
87 88
      Center(
        child: Container(
Adam Barth's avatar
Adam Barth committed
89 90
          width: 200.0,
          height: 200.0,
91
          child: FittedBox(
Adam Barth's avatar
Adam Barth committed
92
            key: outside,
93
            fit: BoxFit.cover,
94
            child: Container(
Adam Barth's avatar
Adam Barth committed
95 96 97
              key: inside,
              width: 100.0,
              height: 50.0,
98 99 100
            ),
          ),
        ),
101
      ),
Adam Barth's avatar
Adam Barth committed
102 103
    );

104
    final RenderBox outsideBox = tester.firstRenderObject(find.byKey(outside));
Adam Barth's avatar
Adam Barth committed
105 106 107
    expect(outsideBox.size.width, 200.0);
    expect(outsideBox.size.height, 200.0);

108
    final RenderBox insideBox = tester.firstRenderObject(find.byKey(inside));
Adam Barth's avatar
Adam Barth committed
109 110 111
    expect(insideBox.size.width, 100.0);
    expect(insideBox.size.height, 50.0);

112 113
    final Offset insidePoint = insideBox.localToGlobal(const Offset(50.0, 25.0));
    final Offset outsidePoint = outsideBox.localToGlobal(const Offset(100.0, 100.0));
Adam Barth's avatar
Adam Barth committed
114 115 116

    expect(insidePoint, equals(outsidePoint));
  });
Ian Hickson's avatar
Ian Hickson committed
117 118

  testWidgets('FittedBox with no child', (WidgetTester tester) async {
119
    final Key key = UniqueKey();
Ian Hickson's avatar
Ian Hickson committed
120
    await tester.pumpWidget(
121 122
      Center(
        child: FittedBox(
Ian Hickson's avatar
Ian Hickson committed
123 124 125 126 127 128 129 130 131 132 133 134
          key: key,
          fit: BoxFit.cover,
        ),
      ),
    );

    final RenderBox box = tester.firstRenderObject(find.byKey(key));
    expect(box.size.width, 0.0);
    expect(box.size.height, 0.0);
  });

  testWidgets('Child can be aligned multiple ways in a row', (WidgetTester tester) async {
135 136
    final Key outside = UniqueKey();
    final Key inside = UniqueKey();
Ian Hickson's avatar
Ian Hickson committed
137 138 139 140

    { // align RTL

      await tester.pumpWidget(
141
        Directionality(
Ian Hickson's avatar
Ian Hickson committed
142
          textDirection: TextDirection.rtl,
143 144
          child: Center(
            child: Container(
Ian Hickson's avatar
Ian Hickson committed
145 146
              width: 100.0,
              height: 100.0,
147
              child: FittedBox(
Ian Hickson's avatar
Ian Hickson committed
148 149 150
                key: outside,
                fit: BoxFit.scaleDown,
                alignment: AlignmentDirectional.bottomEnd,
151
                child: Container(
Ian Hickson's avatar
Ian Hickson committed
152 153 154 155 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
                  key: inside,
                  width: 10.0,
                  height: 10.0,
                ),
              ),
            ),
          ),
        ),
      );

      final RenderBox outsideBox = tester.firstRenderObject(find.byKey(outside));
      expect(outsideBox.size.width, 100.0);
      expect(outsideBox.size.height, 100.0);

      final RenderBox insideBox = tester.firstRenderObject(find.byKey(inside));
      expect(insideBox.size.width, 10.0);
      expect(insideBox.size.height, 10.0);

      final Offset insideTopLeft = insideBox.localToGlobal(const Offset(0.0, 0.0));
      final Offset outsideTopLeft = outsideBox.localToGlobal(const Offset(0.0, 90.0));
      final Offset insideBottomRight = insideBox.localToGlobal(const Offset(10.0, 10.0));
      final Offset outsideBottomRight = outsideBox.localToGlobal(const Offset(10.0, 100.0));

      expect(insideTopLeft, equals(outsideTopLeft));
      expect(insideBottomRight, equals(outsideBottomRight));
    }

    { // change direction

      await tester.pumpWidget(
182
        Directionality(
Ian Hickson's avatar
Ian Hickson committed
183
          textDirection: TextDirection.ltr,
184 185
          child: Center(
            child: Container(
Ian Hickson's avatar
Ian Hickson committed
186 187
              width: 100.0,
              height: 100.0,
188
              child: FittedBox(
Ian Hickson's avatar
Ian Hickson committed
189 190 191
                key: outside,
                fit: BoxFit.scaleDown,
                alignment: AlignmentDirectional.bottomEnd,
192
                child: Container(
Ian Hickson's avatar
Ian Hickson committed
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
                  key: inside,
                  width: 10.0,
                  height: 10.0,
                ),
              ),
            ),
          ),
        ),
      );

      final RenderBox outsideBox = tester.firstRenderObject(find.byKey(outside));
      expect(outsideBox.size.width, 100.0);
      expect(outsideBox.size.height, 100.0);

      final RenderBox insideBox = tester.firstRenderObject(find.byKey(inside));
      expect(insideBox.size.width, 10.0);
      expect(insideBox.size.height, 10.0);

      final Offset insideTopLeft = insideBox.localToGlobal(const Offset(0.0, 0.0));
      final Offset outsideTopLeft = outsideBox.localToGlobal(const Offset(90.0, 90.0));
      final Offset insideBottomRight = insideBox.localToGlobal(const Offset(10.0, 10.0));
      final Offset outsideBottomRight = outsideBox.localToGlobal(const Offset(100.0, 100.0));

      expect(insideTopLeft, equals(outsideTopLeft));
      expect(insideBottomRight, equals(outsideBottomRight));
    }

    { // change alignment

      await tester.pumpWidget(
223
        Directionality(
Ian Hickson's avatar
Ian Hickson committed
224
          textDirection: TextDirection.ltr,
225 226
          child: Center(
            child: Container(
Ian Hickson's avatar
Ian Hickson committed
227 228
              width: 100.0,
              height: 100.0,
229
              child: FittedBox(
Ian Hickson's avatar
Ian Hickson committed
230 231 232
                key: outside,
                fit: BoxFit.scaleDown,
                alignment: AlignmentDirectional.center,
233
                child: Container(
Ian Hickson's avatar
Ian Hickson committed
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
                  key: inside,
                  width: 10.0,
                  height: 10.0,
                ),
              ),
            ),
          ),
        ),
      );

      final RenderBox outsideBox = tester.firstRenderObject(find.byKey(outside));
      expect(outsideBox.size.width, 100.0);
      expect(outsideBox.size.height, 100.0);

      final RenderBox insideBox = tester.firstRenderObject(find.byKey(inside));
      expect(insideBox.size.width, 10.0);
      expect(insideBox.size.height, 10.0);

      final Offset insideTopLeft = insideBox.localToGlobal(const Offset(0.0, 0.0));
      final Offset outsideTopLeft = outsideBox.localToGlobal(const Offset(45.0, 45.0));
      final Offset insideBottomRight = insideBox.localToGlobal(const Offset(10.0, 10.0));
      final Offset outsideBottomRight = outsideBox.localToGlobal(const Offset(55.0, 55.0));

      expect(insideTopLeft, equals(outsideTopLeft));
      expect(insideBottomRight, equals(outsideBottomRight));
    }

    { // change size

      await tester.pumpWidget(
264
        Directionality(
Ian Hickson's avatar
Ian Hickson committed
265
          textDirection: TextDirection.ltr,
266 267
          child: Center(
            child: Container(
Ian Hickson's avatar
Ian Hickson committed
268 269
              width: 100.0,
              height: 100.0,
270
              child: FittedBox(
Ian Hickson's avatar
Ian Hickson committed
271 272 273
                key: outside,
                fit: BoxFit.scaleDown,
                alignment: AlignmentDirectional.center,
274
                child: Container(
Ian Hickson's avatar
Ian Hickson committed
275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
                  key: inside,
                  width: 30.0,
                  height: 10.0,
                ),
              ),
            ),
          ),
        ),
      );

      final RenderBox outsideBox = tester.firstRenderObject(find.byKey(outside));
      expect(outsideBox.size.width, 100.0);
      expect(outsideBox.size.height, 100.0);

      final RenderBox insideBox = tester.firstRenderObject(find.byKey(inside));
      expect(insideBox.size.width, 30.0);
      expect(insideBox.size.height, 10.0);

      final Offset insideTopLeft = insideBox.localToGlobal(const Offset(0.0, 0.0));
      final Offset outsideTopLeft = outsideBox.localToGlobal(const Offset(35.0, 45.0));
      final Offset insideBottomRight = insideBox.localToGlobal(const Offset(30.0, 10.0));
      final Offset outsideBottomRight = outsideBox.localToGlobal(const Offset(65.0, 55.0));

      expect(insideTopLeft, equals(outsideTopLeft));
      expect(insideBottomRight, equals(outsideBottomRight));
    }

    { // change fit

      await tester.pumpWidget(
305
        Directionality(
Ian Hickson's avatar
Ian Hickson committed
306
          textDirection: TextDirection.ltr,
307 308
          child: Center(
            child: Container(
Ian Hickson's avatar
Ian Hickson committed
309 310
              width: 100.0,
              height: 100.0,
311
              child: FittedBox(
Ian Hickson's avatar
Ian Hickson committed
312 313 314
                key: outside,
                fit: BoxFit.fill,
                alignment: AlignmentDirectional.center,
315
                child: Container(
Ian Hickson's avatar
Ian Hickson committed
316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
                  key: inside,
                  width: 30.0,
                  height: 10.0,
                ),
              ),
            ),
          ),
        ),
      );

      final RenderBox outsideBox = tester.firstRenderObject(find.byKey(outside));
      expect(outsideBox.size.width, 100.0);
      expect(outsideBox.size.height, 100.0);

      final RenderBox insideBox = tester.firstRenderObject(find.byKey(inside));
      expect(insideBox.size.width, 30.0);
      expect(insideBox.size.height, 10.0);

      final Offset insideTopLeft = insideBox.localToGlobal(const Offset(0.0, 0.0));
      final Offset outsideTopLeft = outsideBox.localToGlobal(const Offset(0.0, 0.0));
      final Offset insideBottomRight = insideBox.localToGlobal(const Offset(30.0, 10.0));
      final Offset outsideBottomRight = outsideBox.localToGlobal(const Offset(100.0, 100.0));

      expect(insideTopLeft, equals(outsideTopLeft));
      expect(insideBottomRight, equals(outsideBottomRight));
    }
  });
343 344 345 346

  testWidgets('FittedBox layers - contain', (WidgetTester tester) async {
    await tester.pumpWidget(
      const Center(
347
        child: SizedBox(
348 349
          width: 100.0,
          height: 10.0,
350
          child: FittedBox(
351
            fit: BoxFit.contain,
352
            child: SizedBox(
353 354
              width: 50.0,
              height: 50.0,
355 356
              child: RepaintBoundary(
                child: Placeholder(),
357 358 359 360 361 362 363 364 365 366 367 368
              ),
            ),
          ),
        ),
      ),
    );
    expect(getLayers(), <Type>[TransformLayer, TransformLayer, OffsetLayer]);
  });

  testWidgets('FittedBox layers - cover - horizontal', (WidgetTester tester) async {
    await tester.pumpWidget(
      const Center(
369
        child: SizedBox(
370 371
          width: 100.0,
          height: 10.0,
372
          child: FittedBox(
373
            fit: BoxFit.cover,
374
            clipBehavior: Clip.hardEdge,
375
            child: SizedBox(
376 377
              width: 10.0,
              height: 50.0,
378 379
              child: RepaintBoundary(
                child: Placeholder(),
380 381 382 383 384 385 386 387 388 389 390 391
              ),
            ),
          ),
        ),
      ),
    );
    expect(getLayers(), <Type>[TransformLayer, ClipRectLayer, TransformLayer, OffsetLayer]);
  });

  testWidgets('FittedBox layers - cover - vertical', (WidgetTester tester) async {
    await tester.pumpWidget(
      const Center(
392
        child: SizedBox(
393 394
          width: 10.0,
          height: 100.0,
395
          child: FittedBox(
396
            fit: BoxFit.cover,
397
            clipBehavior: Clip.hardEdge,
398
            child: SizedBox(
399 400
              width: 50.0,
              height: 10.0,
401 402
              child: RepaintBoundary(
                child: Placeholder(),
403 404 405 406 407 408 409 410 411 412 413
              ),
            ),
          ),
        ),
      ),
    );
    expect(getLayers(), <Type>[TransformLayer, ClipRectLayer, TransformLayer, OffsetLayer]);
  });

  testWidgets('FittedBox layers - none - clip', (WidgetTester tester) async {
    final List<double> values = <double>[10.0, 50.0, 100.0];
414 415 416 417
    for (final double a in values) {
      for (final double b in values) {
        for (final double c in values) {
          for (final double d in values) {
418
            await tester.pumpWidget(
419 420
              Center(
                child: SizedBox(
421 422
                  width: a,
                  height: b,
423
                  child: FittedBox(
424
                    fit: BoxFit.none,
425
                    clipBehavior: Clip.hardEdge,
426
                    child: SizedBox(
427 428 429
                      width: c,
                      height: d,
                      child: const RepaintBoundary(
430
                        child: Placeholder(),
431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446
                      ),
                    ),
                  ),
                ),
              ),
            );
            if (a < c || b < d) {
              expect(getLayers(), <Type>[TransformLayer, ClipRectLayer, OffsetLayer]);
            } else {
              expect(getLayers(), <Type>[TransformLayer, OffsetLayer]);
            }
          }
        }
      }
    }
  });
Emmanuel Garcia's avatar
Emmanuel Garcia committed
447 448

  testWidgets('Big child into small fitted box - hit testing', (WidgetTester tester) async {
449
    final GlobalKey key1 = GlobalKey();
Emmanuel Garcia's avatar
Emmanuel Garcia committed
450 451
    bool _pointerDown = false;
    await tester.pumpWidget(
452 453
      Center(
        child: SizedBox(
Emmanuel Garcia's avatar
Emmanuel Garcia committed
454 455
          width: 100.0,
          height: 100.0,
456
          child: FittedBox(
Emmanuel Garcia's avatar
Emmanuel Garcia committed
457 458
            fit: BoxFit.contain,
            alignment: FractionalOffset.center,
459
            child: SizedBox(
Emmanuel Garcia's avatar
Emmanuel Garcia committed
460 461
              width: 1000.0,
              height: 1000.0,
462
              child: Listener(
Emmanuel Garcia's avatar
Emmanuel Garcia committed
463 464 465
                onPointerDown: (PointerDownEvent event) {
                  _pointerDown = true;
                },
466
                child: Container(
Emmanuel Garcia's avatar
Emmanuel Garcia committed
467 468
                  key: key1,
                  color: const Color(0xFF000000),
469 470 471 472 473
                ),
              ),
            ),
          ),
        ),
474
      ),
Emmanuel Garcia's avatar
Emmanuel Garcia committed
475 476 477 478 479
    );
    expect(_pointerDown, isFalse);
    await tester.tap(find.byKey(key1));
    expect(_pointerDown, isTrue);
  });
480 481 482 483 484 485 486 487 488

  testWidgets('Can set and update clipBehavior', (WidgetTester tester) async {
    await tester.pumpWidget(FittedBox(fit: BoxFit.none, child: Container()));
    final RenderFittedBox renderObject = tester.allRenderObjects.whereType<RenderFittedBox>().first;
    expect(renderObject.clipBehavior, equals(Clip.hardEdge));

    await tester.pumpWidget(FittedBox(fit: BoxFit.none, child: Container(), clipBehavior: Clip.antiAlias));
    expect(renderObject.clipBehavior, equals(Clip.antiAlias));
  });
489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593

  testWidgets('BoxFit.scaleDown matches size of child', (WidgetTester tester) async {
    final Key outside = UniqueKey();
    final Key inside = UniqueKey();

    // Does not scale up when child is smaller than constraints

    await tester.pumpWidget(
      Center(
        child: Container(
          width: 200.0,
          child: FittedBox(
            key: outside,
            fit: BoxFit.scaleDown,
            child: Container(
              key: inside,
              width: 100.0,
              height: 50.0,
            ),
          ),
        ),
      ),
    );

    final RenderBox outsideBox = tester.firstRenderObject(find.byKey(outside));
    final RenderBox insideBox = tester.firstRenderObject(find.byKey(inside));

    expect(outsideBox.size.width, 200.0);
    expect(outsideBox.size.height, 50.0);

    Offset outsidePoint = outsideBox.localToGlobal(Offset.zero);
    Offset insidePoint = insideBox.localToGlobal(Offset.zero);
    expect(insidePoint - outsidePoint, equals(const Offset(50.0, 0.0)));

    // Scales down when child is bigger than constraints

    await tester.pumpWidget(
      Center(
        child: Container(
          width: 200.0,
          child: FittedBox(
            key: outside,
            fit: BoxFit.scaleDown,
            child: Container(
              key: inside,
              width: 400.0,
              height: 200.0,
            ),
          ),
        ),
      ),
    );

    expect(outsideBox.size.width, 200.0);
    expect(outsideBox.size.height, 100.0);

    outsidePoint = outsideBox.localToGlobal(Offset.zero);
    insidePoint = insideBox.localToGlobal(Offset.zero);

    expect(insidePoint - outsidePoint, equals(Offset.zero));
  });

  testWidgets('Switching to and from BoxFit.scaleDown causes relayout', (WidgetTester tester) async {
    final Key outside = UniqueKey();

    final Widget scaleDownWidget = Center(
      child: Container(
        width: 200.0,
        child: FittedBox(
          key: outside,
          fit: BoxFit.scaleDown,
          child: Container(
            width: 100.0,
            height: 50.0,
          ),
        ),
      ),
    );

    final Widget coverWidget = Center(
      child: Container(
        width: 200.0,
        child: FittedBox(
          key: outside,
          child: Container(
            width: 100.0,
            height: 50.0,
          ),
        ),
      ),
    );

    await tester.pumpWidget(scaleDownWidget);

    final RenderBox outsideBox = tester.firstRenderObject(find.byKey(outside));
    expect(outsideBox.size.height, 50.0);

    await tester.pumpWidget(coverWidget);

    expect(outsideBox.size.height, 100.0);

    await tester.pumpWidget(scaleDownWidget);

    expect(outsideBox.size.height, 50.0);
  });
594 595 596 597 598 599
}

List<Type> getLayers() {
  final List<Type> layers = <Type>[];
  Layer layer = RendererBinding.instance.renderView.debugLayer;
  while (layer is ContainerLayer) {
600
    final ContainerLayer container = layer as ContainerLayer;
601 602 603 604 605
    layers.add(container.runtimeType);
    expect(container.firstChild, same(container.lastChild));
    layer = container.firstChild;
  }
  return layers;
Adam Barth's avatar
Adam Barth committed
606
}