fitted_box_test.dart 18.2 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 5 6 7 8 9 10
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

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 {
11 12
    final Key outside = UniqueKey();
    final Key inside = UniqueKey();
Adam Barth's avatar
Adam Barth committed
13 14

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

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

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

38 39
    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
40

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

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

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

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

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

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

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

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

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

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

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

110 111
    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
112 113 114

    expect(insidePoint, equals(outsidePoint));
  });
Ian Hickson's avatar
Ian Hickson committed
115 116

  testWidgets('FittedBox with no child', (WidgetTester tester) async {
117
    final Key key = UniqueKey();
Ian Hickson's avatar
Ian Hickson committed
118
    await tester.pumpWidget(
119 120
      Center(
        child: FittedBox(
Ian Hickson's avatar
Ian Hickson committed
121 122 123 124 125 126 127 128 129 130 131 132
          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 {
133 134
    final Key outside = UniqueKey();
    final Key inside = UniqueKey();
Ian Hickson's avatar
Ian Hickson committed
135 136 137 138

    { // align RTL

      await tester.pumpWidget(
139
        Directionality(
Ian Hickson's avatar
Ian Hickson committed
140
          textDirection: TextDirection.rtl,
141
          child: Center(
142
            child: SizedBox(
Ian Hickson's avatar
Ian Hickson committed
143 144
              width: 100.0,
              height: 100.0,
145
              child: FittedBox(
Ian Hickson's avatar
Ian Hickson committed
146 147 148
                key: outside,
                fit: BoxFit.scaleDown,
                alignment: AlignmentDirectional.bottomEnd,
149
                child: SizedBox(
Ian Hickson's avatar
Ian Hickson committed
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
                  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);

168
      final Offset insideTopLeft = insideBox.localToGlobal(Offset.zero);
Ian Hickson's avatar
Ian Hickson committed
169 170 171 172 173 174 175 176 177 178 179
      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(
180
        Directionality(
Ian Hickson's avatar
Ian Hickson committed
181
          textDirection: TextDirection.ltr,
182
          child: Center(
183
            child: SizedBox(
Ian Hickson's avatar
Ian Hickson committed
184 185
              width: 100.0,
              height: 100.0,
186
              child: FittedBox(
Ian Hickson's avatar
Ian Hickson committed
187 188 189
                key: outside,
                fit: BoxFit.scaleDown,
                alignment: AlignmentDirectional.bottomEnd,
190
                child: SizedBox(
Ian Hickson's avatar
Ian Hickson committed
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
                  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);

209
      final Offset insideTopLeft = insideBox.localToGlobal(Offset.zero);
Ian Hickson's avatar
Ian Hickson committed
210 211 212 213 214 215 216 217 218 219 220
      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(
221
        Directionality(
Ian Hickson's avatar
Ian Hickson committed
222
          textDirection: TextDirection.ltr,
223
          child: Center(
224
            child: SizedBox(
Ian Hickson's avatar
Ian Hickson committed
225 226
              width: 100.0,
              height: 100.0,
227
              child: FittedBox(
Ian Hickson's avatar
Ian Hickson committed
228 229 230
                key: outside,
                fit: BoxFit.scaleDown,
                alignment: AlignmentDirectional.center,
231
                child: SizedBox(
Ian Hickson's avatar
Ian Hickson committed
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
                  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);

250
      final Offset insideTopLeft = insideBox.localToGlobal(Offset.zero);
Ian Hickson's avatar
Ian Hickson committed
251 252 253 254 255 256 257 258 259 260 261
      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(
262
        Directionality(
Ian Hickson's avatar
Ian Hickson committed
263
          textDirection: TextDirection.ltr,
264
          child: Center(
265
            child: SizedBox(
Ian Hickson's avatar
Ian Hickson committed
266 267
              width: 100.0,
              height: 100.0,
268
              child: FittedBox(
Ian Hickson's avatar
Ian Hickson committed
269 270 271
                key: outside,
                fit: BoxFit.scaleDown,
                alignment: AlignmentDirectional.center,
272
                child: SizedBox(
Ian Hickson's avatar
Ian Hickson committed
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
                  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);

291
      final Offset insideTopLeft = insideBox.localToGlobal(Offset.zero);
Ian Hickson's avatar
Ian Hickson committed
292 293 294 295 296 297 298 299 300 301 302
      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(
303
        Directionality(
Ian Hickson's avatar
Ian Hickson committed
304
          textDirection: TextDirection.ltr,
305
          child: Center(
306
            child: SizedBox(
Ian Hickson's avatar
Ian Hickson committed
307 308
              width: 100.0,
              height: 100.0,
309
              child: FittedBox(
Ian Hickson's avatar
Ian Hickson committed
310 311 312
                key: outside,
                fit: BoxFit.fill,
                alignment: AlignmentDirectional.center,
313
                child: SizedBox(
Ian Hickson's avatar
Ian Hickson committed
314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
                  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);

332 333
      final Offset insideTopLeft = insideBox.localToGlobal(Offset.zero);
      final Offset outsideTopLeft = outsideBox.localToGlobal(Offset.zero);
Ian Hickson's avatar
Ian Hickson committed
334 335 336 337 338 339 340
      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));
    }
  });
341 342 343 344

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

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

  testWidgets('FittedBox layers - cover - vertical', (WidgetTester tester) async {
    await tester.pumpWidget(
      const Center(
390
        child: SizedBox(
391 392
          width: 10.0,
          height: 100.0,
393
          child: FittedBox(
394
            fit: BoxFit.cover,
395
            clipBehavior: Clip.hardEdge,
396
            child: SizedBox(
397 398
              width: 50.0,
              height: 10.0,
399 400
              child: RepaintBoundary(
                child: Placeholder(),
401 402 403 404 405 406 407 408 409 410 411
              ),
            ),
          ),
        ),
      ),
    );
    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];
412 413 414 415
    for (final double a in values) {
      for (final double b in values) {
        for (final double c in values) {
          for (final double d in values) {
416
            await tester.pumpWidget(
417 418
              Center(
                child: SizedBox(
419 420
                  width: a,
                  height: b,
421
                  child: FittedBox(
422
                    fit: BoxFit.none,
423
                    clipBehavior: Clip.hardEdge,
424
                    child: SizedBox(
425 426 427
                      width: c,
                      height: d,
                      child: const RepaintBoundary(
428
                        child: Placeholder(),
429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444
                      ),
                    ),
                  ),
                ),
              ),
            );
            if (a < c || b < d) {
              expect(getLayers(), <Type>[TransformLayer, ClipRectLayer, OffsetLayer]);
            } else {
              expect(getLayers(), <Type>[TransformLayer, OffsetLayer]);
            }
          }
        }
      }
    }
  });
Emmanuel Garcia's avatar
Emmanuel Garcia committed
445 446

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

  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;
482
    expect(renderObject.clipBehavior, equals(Clip.none));
483 484 485 486

    await tester.pumpWidget(FittedBox(fit: BoxFit.none, child: Container(), clipBehavior: Clip.antiAlias));
    expect(renderObject.clipBehavior, equals(Clip.antiAlias));
  });
487 488 489 490 491 492 493 494 495

  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(
496
        child: SizedBox(
497 498 499 500
          width: 200.0,
          child: FittedBox(
            key: outside,
            fit: BoxFit.scaleDown,
501
            child: SizedBox(
502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524
              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(
525
        child: SizedBox(
526 527 528 529
          width: 200.0,
          child: FittedBox(
            key: outside,
            fit: BoxFit.scaleDown,
530
            child: SizedBox(
531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552
              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(
553
      child: SizedBox(
554 555 556 557
        width: 200.0,
        child: FittedBox(
          key: outside,
          fit: BoxFit.scaleDown,
558
          child: const SizedBox(
559 560 561 562 563 564 565 566
            width: 100.0,
            height: 50.0,
          ),
        ),
      ),
    );

    final Widget coverWidget = Center(
567
      child: SizedBox(
568 569 570
        width: 200.0,
        child: FittedBox(
          key: outside,
571
          child: const SizedBox(
572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591
            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);
  });
592 593 594 595
}

List<Type> getLayers() {
  final List<Type> layers = <Type>[];
596 597
  Layer? container = RendererBinding.instance!.renderView.debugLayer;
  while (container is ContainerLayer) {
598 599
    layers.add(container.runtimeType);
    expect(container.firstChild, same(container.lastChild));
600
    container = container.firstChild;
601 602
  }
  return layers;
Adam Barth's avatar
Adam Barth committed
603
}