transform_test.dart 27.8 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
Hixie's avatar
Hixie 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 7
// This file is run as part of a reduced test set in CI on Mac and Windows
// machines.
@Tags(<String>['reduced-test-set'])
8
library;
9

10
import 'dart:math' as math;
11
import 'dart:ui' as ui;
12

13
import 'package:flutter/rendering.dart';
14
import 'package:flutter/widgets.dart';
15
import 'package:flutter_test/flutter_test.dart';
16
import 'package:vector_math/vector_math_64.dart';
17 18

void main() {
19
  testWidgets('Transform origin', (WidgetTester tester) async {
20
    bool didReceiveTap = false;
21
    await tester.pumpWidget(
22
      Directionality(
23
        textDirection: TextDirection.ltr,
24
        child: Stack(
25
          children: <Widget>[
26
            Positioned(
27 28
              top: 100.0,
              left: 100.0,
29
              child: Container(
30 31 32 33
                width: 100.0,
                height: 100.0,
                color: const Color(0xFF0000FF),
              ),
34
            ),
35
            Positioned(
36 37
              top: 100.0,
              left: 100.0,
38
              child: SizedBox(
39 40
                width: 100.0,
                height: 100.0,
41 42
                child: Transform(
                  transform: Matrix4.diagonal3Values(0.5, 0.5, 1.0),
43
                  origin: const Offset(100.0, 50.0),
44
                  child: GestureDetector(
45 46 47
                    onTap: () {
                      didReceiveTap = true;
                    },
48
                    child: Container(
49 50
                      color: const Color(0xFF00FFFF),
                    ),
51 52 53 54
                  ),
                ),
              ),
            ),
55 56
          ],
        ),
57
      ),
58
    );
59

60
    expect(didReceiveTap, isFalse);
61
    await tester.tapAt(const Offset(110.0, 110.0));
62
    expect(didReceiveTap, isFalse);
63
    await tester.tapAt(const Offset(190.0, 150.0));
64
    expect(didReceiveTap, isTrue);
65
  });
Hixie's avatar
Hixie committed
66

67
  testWidgets('Transform alignment', (WidgetTester tester) async {
68
    bool didReceiveTap = false;
69
    await tester.pumpWidget(
70
      Directionality(
71
        textDirection: TextDirection.ltr,
72
        child: Stack(
73
          children: <Widget>[
74
            Positioned(
75 76
              top: 100.0,
              left: 100.0,
77
              child: Container(
78 79 80 81
                width: 100.0,
                height: 100.0,
                color: const Color(0xFF0000FF),
              ),
82
            ),
83
            Positioned(
84 85
              top: 100.0,
              left: 100.0,
86
              child: SizedBox(
87 88
                width: 100.0,
                height: 100.0,
89 90
                child: Transform(
                  transform: Matrix4.diagonal3Values(0.5, 0.5, 1.0),
91
                  alignment: Alignment.centerRight,
92
                  child: GestureDetector(
93 94 95
                    onTap: () {
                      didReceiveTap = true;
                    },
96
                    child: Container(
97 98
                      color: const Color(0xFF00FFFF),
                    ),
99 100 101 102
                  ),
                ),
              ),
            ),
103 104
          ],
        ),
105
      ),
106
    );
Hixie's avatar
Hixie committed
107

108
    expect(didReceiveTap, isFalse);
109
    await tester.tapAt(const Offset(110.0, 110.0));
110
    expect(didReceiveTap, isFalse);
111
    await tester.tapAt(const Offset(190.0, 150.0));
112
    expect(didReceiveTap, isTrue);
Hixie's avatar
Hixie committed
113 114
  });

115
  testWidgets('Transform AlignmentDirectional alignment', (WidgetTester tester) async {
116 117 118
    bool didReceiveTap = false;

    Widget buildFrame(TextDirection textDirection, AlignmentGeometry alignment) {
119
      return Directionality(
120
        textDirection: textDirection,
121
        child: Stack(
122
          children: <Widget>[
123
            Positioned(
124 125
              top: 100.0,
              left: 100.0,
126
              child: Container(
127 128 129 130 131
                width: 100.0,
                height: 100.0,
                color: const Color(0xFF0000FF),
              ),
            ),
132
            Positioned(
133 134
              top: 100.0,
              left: 100.0,
135
              child: SizedBox(
136 137
                width: 100.0,
                height: 100.0,
138 139
                child: Transform(
                  transform: Matrix4.diagonal3Values(0.5, 0.5, 1.0),
140
                  alignment: alignment,
141
                  child: GestureDetector(
142 143 144
                    onTap: () {
                      didReceiveTap = true;
                    },
145
                    child: Container(
146 147 148 149 150 151 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 182 183 184 185
                      color: const Color(0xFF00FFFF),
                    ),
                  ),
                ),
              ),
            ),
          ],
        ),
      );
    }

    await tester.pumpWidget(buildFrame(TextDirection.ltr, AlignmentDirectional.centerEnd));
    didReceiveTap = false;
    await tester.tapAt(const Offset(110.0, 110.0));
    expect(didReceiveTap, isFalse);
    await tester.tapAt(const Offset(190.0, 150.0));
    expect(didReceiveTap, isTrue);

    await tester.pumpWidget(buildFrame(TextDirection.rtl, AlignmentDirectional.centerStart));
    didReceiveTap = false;
    await tester.tapAt(const Offset(110.0, 110.0));
    expect(didReceiveTap, isFalse);
    await tester.tapAt(const Offset(190.0, 150.0));
    expect(didReceiveTap, isTrue);

    await tester.pumpWidget(buildFrame(TextDirection.ltr, AlignmentDirectional.centerStart));
    didReceiveTap = false;
    await tester.tapAt(const Offset(190.0, 150.0));
    expect(didReceiveTap, isFalse);
    await tester.tapAt(const Offset(110.0, 150.0));
    expect(didReceiveTap, isTrue);

    await tester.pumpWidget(buildFrame(TextDirection.rtl, AlignmentDirectional.centerEnd));
    didReceiveTap = false;
    await tester.tapAt(const Offset(190.0, 150.0));
    expect(didReceiveTap, isFalse);
    await tester.tapAt(const Offset(110.0, 150.0));
    expect(didReceiveTap, isTrue);
  });

186
  testWidgets('Transform offset + alignment', (WidgetTester tester) async {
187
    bool didReceiveTap = false;
188
    await tester.pumpWidget(
189
      Directionality(
190
        textDirection: TextDirection.ltr,
191
        child: Stack(
192
          children: <Widget>[
193
            Positioned(
194 195
              top: 100.0,
              left: 100.0,
196
              child: Container(
197 198 199 200 201
                width: 100.0,
                height: 100.0,
                color: const Color(0xFF0000FF),
              ),
            ),
202
            Positioned(
203 204
              top: 100.0,
              left: 100.0,
205
              child: SizedBox(
206 207
                width: 100.0,
                height: 100.0,
208 209
                child: Transform(
                  transform: Matrix4.diagonal3Values(0.5, 0.5, 1.0),
210
                  origin: const Offset(100.0, 0.0),
211
                  alignment: Alignment.centerLeft,
212
                  child: GestureDetector(
213 214 215
                    onTap: () {
                      didReceiveTap = true;
                    },
216
                    child: Container(
217 218 219
                      color: const Color(0xFF00FFFF),
                    ),
                  ),
220 221 222
                ),
              ),
            ),
223
          ],
224
        ),
225 226
      ),
    );
Hixie's avatar
Hixie committed
227

228
    expect(didReceiveTap, isFalse);
229
    await tester.tapAt(const Offset(110.0, 110.0));
230
    expect(didReceiveTap, isFalse);
231
    await tester.tapAt(const Offset(190.0, 150.0));
232
    expect(didReceiveTap, isTrue);
Hixie's avatar
Hixie committed
233
  });
234

235
  testWidgets('Composited transform offset', (WidgetTester tester) async {
236
    await tester.pumpWidget(
237 238
      Center(
        child: SizedBox(
239 240
          width: 400.0,
          height: 300.0,
241 242 243
          child: ClipRect(
            child: Transform(
              transform: Matrix4.diagonal3Values(0.5, 0.5, 1.0),
244
              child: RepaintBoundary(
245
                child: Container(
246
                  color: const Color(0xFF00FF00),
247 248 249 250 251 252 253 254
                ),
              ),
            ),
          ),
        ),
      ),
    );

255
    final List<Layer> layers = tester.layers
256 257 258
      ..retainWhere((Layer layer) => layer is TransformLayer);
    expect(layers.length, 2);
    // The first transform is from the render view.
259
    final TransformLayer layer = layers[1] as TransformLayer;
260
    final Matrix4 transform = layer.transform!;
261
    expect(transform.getTranslation(), equals(Vector3(100.0, 75.0, 0.0)));
262
  });
263

264
  testWidgets('Transform.rotate', (WidgetTester tester) async {
265
    await tester.pumpWidget(
266
      Transform.rotate(
267
        angle: math.pi / 2.0,
268
        child: RepaintBoundary(child: Container()),
269 270 271 272 273 274 275
      ),
    );

    final List<Layer> layers = tester.layers
      ..retainWhere((Layer layer) => layer is TransformLayer);
    expect(layers.length, 2);
    // The first transform is from the render view.
276
    final TransformLayer layer = layers[1] as TransformLayer;
277
    final Matrix4 transform = layer.transform!;
278 279 280 281 282 283 284
    expect(transform.storage, <dynamic>[
      moreOrLessEquals(0.0), 1.0, 0.0, 0.0,
      -1.0, moreOrLessEquals(0.0), 0.0, 0.0,
      0.0, 0.0, 1.0, 0.0,
      700.0, -100.0, 0.0, 1.0,
    ]);
  });
285

286
  testWidgets('applyPaintTransform of Transform in Padding', (WidgetTester tester) async {
287
    await tester.pumpWidget(
288
      Padding(
289 290 291 292 293 294
        padding: const EdgeInsets.only(
          left: 30.0,
          top: 20.0,
          right: 50.0,
          bottom: 70.0,
        ),
295 296
        child: Transform(
          transform: Matrix4.diagonal3Values(2.0, 2.0, 2.0),
297 298 299 300 301 302
          child: const Placeholder(),
        ),
      ),
    );
    expect(tester.getTopLeft(find.byType(Placeholder)), const Offset(30.0, 20.0));
  });
303

304
  testWidgets('Transform.translate', (WidgetTester tester) async {
305
    await tester.pumpWidget(
306
      Transform.translate(
307
        offset: const Offset(100.0, 50.0),
308
        child: RepaintBoundary(child: Container()),
309 310 311 312 313 314 315 316 317 318
      ),
    );

    // This should not cause a transform layer to be inserted.
    final List<Layer> layers = tester.layers
      ..retainWhere((Layer layer) => layer is TransformLayer);
    expect(layers.length, 1); // only the render view
    expect(tester.getTopLeft(find.byType(Container)), const Offset(100.0, 50.0));
  });

319
  testWidgets('Transform.scale', (WidgetTester tester) async {
320
    await tester.pumpWidget(
321
      Transform.scale(
322
        scale: 2.0,
323
        child: RepaintBoundary(child: Container()),
324 325 326 327 328 329 330
      ),
    );

    final List<Layer> layers = tester.layers
      ..retainWhere((Layer layer) => layer is TransformLayer);
    expect(layers.length, 2);
    // The first transform is from the render view.
331
    final TransformLayer layer = layers[1] as TransformLayer;
332
    final Matrix4 transform = layer.transform!;
333 334 335 336 337 338 339 340
    expect(transform.storage, <dynamic>[
      // These are column-major, not row-major.
      2.0, 0.0, 0.0, 0.0,
      0.0, 2.0, 0.0, 0.0,
      0.0, 0.0, 1.0, 0.0,
      -400.0, -300.0, 0.0, 1.0, // it's 1600x1200, centered in an 800x600 square
    ]);
  });
Emmanuel Garcia's avatar
Emmanuel Garcia committed
341

342
  testWidgets('Transform with nan value short-circuits rendering', (WidgetTester tester) async {
343 344 345 346 347 348 349 350 351 352 353
    await tester.pumpWidget(
      Transform(
        transform: Matrix4.identity()
          ..storage[0] = double.nan,
        child: RepaintBoundary(child: Container()),
      ),
    );

    expect(tester.layers, hasLength(1));
  });

354
  testWidgets('Transform with inf value short-circuits rendering', (WidgetTester tester) async {
355 356 357 358 359 360 361 362 363 364 365
    await tester.pumpWidget(
      Transform(
        transform: Matrix4.identity()
          ..storage[0] = double.infinity,
        child: RepaintBoundary(child: Container()),
      ),
    );

    expect(tester.layers, hasLength(1));
  });

366
  testWidgets('Transform with -inf value short-circuits rendering', (WidgetTester tester) async {
367 368 369 370 371 372 373 374 375 376 377
    await tester.pumpWidget(
      Transform(
        transform: Matrix4.identity()
          ..storage[0] = double.negativeInfinity,
        child: RepaintBoundary(child: Container()),
      ),
    );

    expect(tester.layers, hasLength(1));
  });

378
  testWidgets('Transform.rotate does not remove layers due to singular short-circuit', (WidgetTester tester) async {
379 380 381 382 383 384 385 386 387 388
    await tester.pumpWidget(
      Transform.rotate(
        angle: math.pi / 2,
        child: RepaintBoundary(child: Container()),
      ),
    );

    expect(tester.layers, hasLength(3));
  });

389
  testWidgets('Transform.rotate creates nice rotation matrices for 0, 90, 180, 270 degrees', (WidgetTester tester) async {
390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 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 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449
    await tester.pumpWidget(
      Transform.rotate(
        angle: math.pi / 2,
        child: RepaintBoundary(child: Container()),
      ),
    );

    expect(tester.layers[1], isA<TransformLayer>()
      .having((TransformLayer layer) => layer.transform, 'transform', equals(Matrix4.fromList(<double>[
        0.0, -1.0, 0.0, 700.0,
        1.0, 0.0, 0.0, -100.0,
        0.0, 0.0, 1.0, 0.0,
        0.0, 0.0, 0.0, 1.0,
      ])..transpose()))
    );

    await tester.pumpWidget(
      Transform.rotate(
        angle: math.pi,
        child: RepaintBoundary(child: Container()),
      ),
    );

    expect(tester.layers[1], isA<TransformLayer>()
      .having((TransformLayer layer) => layer.transform, 'transform', equals(Matrix4.fromList(<double>[
       -1.0, 0.0, 0.0, 800.0,
        0.0, -1.0, 0.0, 600.0,
        0.0, 0.0, 1.0, 0.0,
        0.0, 0.0, 0.0, 1.0,
      ])..transpose()))
    );

    await tester.pumpWidget(
      Transform.rotate(
        angle: 3 * math.pi / 2,
        child: RepaintBoundary(child: Container()),
      ),
    );

    expect(tester.layers[1], isA<TransformLayer>()
      .having((TransformLayer layer) => layer.transform, 'transform', equals(Matrix4.fromList(<double>[
        0.0, 1.0, 0.0, 100.0,
       -1.0, 0.0, 0.0, 700.0,
        0.0, 0.0, 1.0, 0.0,
        0.0, 0.0, 0.0, 1.0,
      ])..transpose()))
    );

    await tester.pumpWidget(
      Transform.rotate(
        angle: 0,
        child: RepaintBoundary(child: Container()),
      ),
    );

    // No transform layer created
    expect(tester.layers[1], isA<OffsetLayer>());
    expect(tester.layers, hasLength(2));
  });

450
  testWidgets('Transform.scale with 0.0 does not paint child layers', (WidgetTester tester) async {
451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488
    await tester.pumpWidget(
      Transform.scale(
        scale: 0.0,
        child: RepaintBoundary(child: Container()),
      ),
    );

    expect(tester.layers, hasLength(1)); // root transform layer

    await tester.pumpWidget(
      Transform.scale(
        scaleX: 0.0,
        child: RepaintBoundary(child: Container()),
      ),
    );

    expect(tester.layers, hasLength(1));

    await tester.pumpWidget(
      Transform.scale(
        scaleY: 0.0,
        child: RepaintBoundary(child: Container()),
      ),
    );

    expect(tester.layers, hasLength(1));

    await tester.pumpWidget(
      Transform.scale(
        scale: 0.01, // small but non-zero
        child: RepaintBoundary(child: Container()),
      ),
    );

    expect(tester.layers, hasLength(3));
  });


489
  testWidgets('Translated child into translated box - hit test', (WidgetTester tester) async {
490
    final GlobalKey key1 = GlobalKey();
491
    bool pointerDown = false;
Emmanuel Garcia's avatar
Emmanuel Garcia committed
492
    await tester.pumpWidget(
493
      Transform.translate(
Emmanuel Garcia's avatar
Emmanuel Garcia committed
494
        offset: const Offset(100.0, 50.0),
495
        child: Transform.translate(
Emmanuel Garcia's avatar
Emmanuel Garcia committed
496
          offset: const Offset(1000.0, 1000.0),
497
          child: Listener(
Emmanuel Garcia's avatar
Emmanuel Garcia committed
498
            onPointerDown: (PointerDownEvent event) {
499
              pointerDown = true;
Emmanuel Garcia's avatar
Emmanuel Garcia committed
500
            },
501
            child: Container(
Emmanuel Garcia's avatar
Emmanuel Garcia committed
502 503
              key: key1,
              color: const Color(0xFF000000),
504 505 506
            ),
          ),
        ),
Emmanuel Garcia's avatar
Emmanuel Garcia committed
507 508
      ),
    );
509
    expect(pointerDown, isFalse);
Emmanuel Garcia's avatar
Emmanuel Garcia committed
510
    await tester.tap(find.byKey(key1));
511
    expect(pointerDown, isTrue);
Emmanuel Garcia's avatar
Emmanuel Garcia committed
512
  });
513

514
  Widget generateTransform(bool needsCompositing, double angle) {
515 516 517 518 519 520 521 522 523 524 525 526
    final Widget customPaint = CustomPaint(painter: TestRectPainter());
    return Transform(
      transform: MatrixUtils.createCylindricalProjectionTransform(
        radius: 100,
        angle: angle,
        perspective: 0.003,
      ),
      // A RepaintBoundary child forces the Transform to needsCompositing
      child: needsCompositing ? RepaintBoundary(child: customPaint) : customPaint,
    );
  }

527
  testWidgets(
528 529 530
    '3D transform renders the same with or without needsCompositing',
    (WidgetTester tester) async {
      for (double angle = 0; angle <= math.pi/4; angle += 0.01) {
531
        await tester.pumpWidget(RepaintBoundary(child: generateTransform(true, angle)));
532 533
        final RenderBox renderBox = tester.binding.renderView.child!;
        final OffsetLayer layer = renderBox.debugLayer! as OffsetLayer;
534
        final ui.Image imageWithCompositing = await layer.toImage(renderBox.paintBounds);
535
        addTearDown(imageWithCompositing.dispose);
536

537
        await tester.pumpWidget(RepaintBoundary(child: generateTransform(false, angle)));
538 539 540
        await expectLater(find.byType(RepaintBoundary).first, matchesReferenceImage(imageWithCompositing));
      }
    },
541
    skip: isBrowser, // due to https://github.com/flutter/flutter/issues/49857
542
  );
543

544 545 546 547 548
  List<double> extractMatrix(ui.ImageFilter? filter) {
    final List<String> numbers = filter.toString().split('[').last.split(']').first.split(',');
    return numbers.map<double>((String str) => double.parse(str.trim())).toList();
  }

549
  testWidgets('Transform.translate with FilterQuality produces filter layer', (WidgetTester tester) async {
550 551 552 553
    await tester.pumpWidget(
      Transform.translate(
        offset: const Offset(25.0, 25.0),
        filterQuality: FilterQuality.low,
554
        child: const SizedBox(width: 100, height: 100),
555 556 557
      ),
    );
    expect(tester.layers.whereType<ImageFilterLayer>().length, 1);
558 559 560 561 562
    final ImageFilterLayer layer = tester.layers.whereType<ImageFilterLayer>().first;
    expect(extractMatrix(layer.imageFilter), <double>[
      1.0, 0.0, 0.0, 0.0,
      0.0, 1.0, 0.0, 0.0,
      0.0, 0.0, 1.0, 0.0,
563 564
      25.0, 25.0, 0.0, 1.0,
    ]);
565 566
  });

567
  testWidgets('Transform.scale with FilterQuality produces filter layer', (WidgetTester tester) async {
568 569 570 571
    await tester.pumpWidget(
      Transform.scale(
        scale: 3.14159,
        filterQuality: FilterQuality.low,
572
        child: const SizedBox(width: 100, height: 100),
573 574 575
      ),
    );
    expect(tester.layers.whereType<ImageFilterLayer>().length, 1);
576 577 578 579 580
    final ImageFilterLayer layer = tester.layers.whereType<ImageFilterLayer>().first;
    expect(extractMatrix(layer.imageFilter), <double>[
      3.14159, 0.0, 0.0, 0.0,
      0.0, 3.14159, 0.0, 0.0,
      0.0, 0.0, 1.0, 0.0,
581 582
      -856.636, -642.477, 0.0, 1.0,
    ]);
583 584
  });

585
  testWidgets('Transform.rotate with FilterQuality produces filter layer', (WidgetTester tester) async {
586 587 588 589
    await tester.pumpWidget(
      Transform.rotate(
        angle: math.pi / 4,
        filterQuality: FilterQuality.low,
590
        child: const SizedBox(width: 100, height: 100),
591 592 593
      ),
    );
    expect(tester.layers.whereType<ImageFilterLayer>().length, 1);
594 595 596 597 598
    final ImageFilterLayer layer = tester.layers.whereType<ImageFilterLayer>().first;
    expect(extractMatrix(layer.imageFilter), <dynamic>[
      moreOrLessEquals(0.7071067811865476), moreOrLessEquals(0.7071067811865475), 0.0, 0.0,
      moreOrLessEquals(-0.7071067811865475), moreOrLessEquals(0.7071067811865476), 0.0, 0.0,
      0.0, 0.0, 1.0, 0.0,
599 600
      moreOrLessEquals(329.28932188134524), moreOrLessEquals(-194.97474683058329), 0.0, 1.0,
    ]);
601 602
  });

603
  testWidgets('Offset Transform.rotate with FilterQuality produces filter layer', (WidgetTester tester) async {
604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620
    await tester.pumpWidget(
      SizedBox(width: 400, height: 400,
        child: Center(
          child: Transform.rotate(
            angle: math.pi / 4,
            filterQuality: FilterQuality.low,
            child: const SizedBox(width: 100, height: 100),
          ),
        ),
      ),
    );
    expect(tester.layers.whereType<ImageFilterLayer>().length, 1);
    final ImageFilterLayer layer = tester.layers.whereType<ImageFilterLayer>().first;
    expect(extractMatrix(layer.imageFilter), <dynamic>[
      moreOrLessEquals(0.7071067811865476), moreOrLessEquals(0.7071067811865475), 0.0, 0.0,
      moreOrLessEquals(-0.7071067811865475), moreOrLessEquals(0.7071067811865476), 0.0, 0.0,
      0.0, 0.0, 1.0, 0.0,
621
      moreOrLessEquals(329.28932188134524), moreOrLessEquals(-194.97474683058329), 0.0, 1.0,
622
    ]);
623 624
  });

625
  testWidgets('Transform layers update to match child and filterQuality', (WidgetTester tester) async {
626 627 628 629
    await tester.pumpWidget(
      Transform.rotate(
        angle: math.pi / 4,
        filterQuality: FilterQuality.low,
630
        child: const SizedBox(width: 100, height: 100),
631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654
      ),
    );
    expect(tester.layers.whereType<ImageFilterLayer>(), hasLength(1));

    await tester.pumpWidget(
      Transform.rotate(
        angle: math.pi / 4,
        child: const SizedBox(width: 100, height: 100),
      ),
    );
    expect(tester.layers.whereType<ImageFilterLayer>(), isEmpty);

    await tester.pumpWidget(
      Transform.rotate(
        angle: math.pi / 4,
        filterQuality: FilterQuality.low,
      ),
    );
    expect(tester.layers.whereType<ImageFilterLayer>(), isEmpty);

    await tester.pumpWidget(
      Transform.rotate(
        angle: math.pi / 4,
        filterQuality: FilterQuality.low,
655
        child: const SizedBox(width: 100, height: 100),
656 657 658 659 660
      ),
    );
    expect(tester.layers.whereType<ImageFilterLayer>(), hasLength(1));
  });

661
  testWidgets('Transform layers with filterQuality golden', (WidgetTester tester) async {
662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682
    await tester.pumpWidget(
      Directionality(
        textDirection: TextDirection.ltr,
        child: GridView.count(
          crossAxisCount: 3,
          children: <Widget>[
            Transform.rotate(
              angle: math.pi / 6,
              child: Center(child: Container(width: 100, height: 20, color: const Color(0xffffff00))),
            ),
            Transform.scale(
              scale: 1.5,
              child: Center(child: Container(width: 100, height: 20, color: const Color(0xffffff00))),
            ),
            Transform.translate(
              offset: const Offset(20.0, 60.0),
              child: Center(child: Container(width: 100, height: 20, color: const Color(0xffffff00))),
            ),
            Transform.rotate(
              angle: math.pi / 6,
              filterQuality: FilterQuality.low,
683
              child: Center(child: Container(width: 100, height: 20, color: const Color(0xff00ff00))),
684 685 686 687
            ),
            Transform.scale(
              scale: 1.5,
              filterQuality: FilterQuality.low,
688
              child: Center(child: Container(width: 100, height: 20, color: const Color(0xff00ff00))),
689 690 691 692
            ),
            Transform.translate(
              offset: const Offset(20.0, 60.0),
              filterQuality: FilterQuality.low,
693
              child: Center(child: Container(width: 100, height: 20, color: const Color(0xff00ff00))),
694 695 696 697 698 699 700 701 702 703
            ),
          ],
        ),
      ),
    );
    await expectLater(
      find.byType(GridView),
      matchesGoldenFile('transform_golden.BitmapRotate.png'),
    );
  });
704

705
  testWidgets("Transform.scale() does not accept all three 'scale', 'scaleX' and 'scaleY' parameters to be non-null", (WidgetTester tester) async {
706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722
    await expectLater(() {
      tester.pumpWidget(Directionality(
          textDirection: TextDirection.ltr,
          child: Center(
            child: Transform.scale(
              scale: 1.0,
              scaleX: 1.0,
              scaleY: 1.0,
              child: const SizedBox(
                height: 100,
                width: 100,
              ),
            ),
          )));
    }, throwsAssertionError);
  });

723
  testWidgets("Transform.scale() needs at least one of 'scale', 'scaleX' and 'scaleY' to be non-null, otherwise throws AssertionError", (WidgetTester tester) async {
724 725 726 727 728 729 730 731 732 733 734 735 736 737
    await expectLater(() {
      tester.pumpWidget(Directionality(
          textDirection: TextDirection.ltr,
          child: Center(
            child: Transform.scale(
              child: const SizedBox(
                height: 100,
                width: 100,
              ),
            ),
          )));
    }, throwsAssertionError);
  });

738
  testWidgets("Transform.scale() scales widget uniformly with 'scale' parameter", (WidgetTester tester) async {
739 740 741
    const double scale = 1.5;
    const double height = 100;
    const double width = 150;
742 743 744 745 746 747 748
    await tester.pumpWidget(Directionality(
        textDirection: TextDirection.ltr,
        child: SizedBox(
          height: 400,
          width: 400,
          child: Center(
            child: Transform.scale(
749
              scale: scale,
750
              child: Container(
751 752
                height: height,
                width: width,
753 754 755 756 757 758
                decoration: const BoxDecoration(),
              ),
            ),
          ),
        )));

759
    const Size target = Size(width * scale, height * scale);
760

761
    expect(tester.getBottomRight(find.byType(Container)), target.bottomRight(tester.getTopLeft(find.byType(Container))));
762 763
  });

764
  testWidgets("Transform.scale() scales widget according to 'scaleX' and 'scaleY'", (WidgetTester tester) async {
765 766 767 768
    const double scaleX = 1.5;
    const double scaleY = 1.2;
    const double height = 100;
    const double width = 150;
769 770 771 772 773 774 775
    await tester.pumpWidget(Directionality(
        textDirection: TextDirection.ltr,
        child: SizedBox(
          height: 400,
          width: 400,
          child: Center(
            child: Transform.scale(
776 777
              scaleX: scaleX,
              scaleY: scaleY,
778
              child: Container(
779 780
                height: height,
                width: width,
781 782 783 784 785 786
                decoration: const BoxDecoration(),
              ),
            ),
          ),
        )));

787
    const Size target = Size(width * scaleX, height * scaleY);
788

789
    expect(tester.getBottomRight(find.byType(Container)), target.bottomRight(tester.getTopLeft(find.byType(Container))));
790
  });
791

792
  testWidgets(
793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811
    'Transform.flip does flip child correctly',
    (WidgetTester tester) async {
      const Offset topRight = Offset(60, 20);
      const Offset bottomLeft = Offset(20, 60);
      const Offset bottomRight = Offset(60, 60);

      bool tappedRed = false;

      const Widget square = SizedBox.square(dimension: 40);
      final Widget child = Column(
        mainAxisSize: MainAxisSize.min,
        children: <Widget> [
          Row(mainAxisSize: MainAxisSize.min, children: <Widget>[
            GestureDetector(
              onTap: () => tappedRed = true,
              child: const ColoredBox(color: Color(0xffff0000), child: square),
            ),
            const ColoredBox(color: Color(0xff00ff00), child: square),
          ]),
812
          const Row(mainAxisSize: MainAxisSize.min, children: <Widget>[
813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885
            ColoredBox(color: Color(0xff0000ff), child: square),
            ColoredBox(color: Color(0xffeeff00), child: square),
          ]),
        ],
      );

      await tester.pumpWidget(
        Directionality(
          textDirection: TextDirection.ltr,
          child: Align(
            alignment: Alignment.topLeft,
            child: Transform.flip(
              flipX: true,
              child: child,
            ),
          ),
        ),
      );

      await tester.pumpAndSettle();

      await tester.tapAt(topRight);

      expect(tappedRed, isTrue, reason: 'Transform.flip cannot flipX');

      tappedRed = false;

      await tester.pumpWidget(
        Directionality(
          textDirection: TextDirection.ltr,
          child: Align(
            alignment: Alignment.topLeft,
            child: Transform.flip(
              flipY: true,
              child: child,
            ),
          ),
        ),
      );

      await tester.pumpAndSettle();

      await tester.tapAt(bottomLeft);

      expect(tappedRed, isTrue, reason: 'Transform.flip cannot flipY');

      tappedRed = false;

      await tester.pumpWidget(
        Directionality(
          textDirection: TextDirection.ltr,
          child: Align(
            alignment: Alignment.topLeft,
            child: Transform.flip(
              flipX: true,
              flipY: true,
              child: child,
            ),
          ),
        ),
      );

      await tester.pumpAndSettle();

      await tester.tapAt(bottomRight);

      expect(
        tappedRed,
        isTrue,
        reason: 'Transform.flip cannot flipX and flipY together',
      );
    },
  );
886 887 888 889 890 891 892 893 894 895 896 897
}

class TestRectPainter extends CustomPainter {
  @override
  void paint(ui.Canvas canvas, ui.Size size) {
    canvas.drawRect(
      const Offset(200, 200) & const Size(10, 10),
      Paint()..color = const Color(0xFFFF0000),
    );
  }
  @override
  bool shouldRepaint(CustomPainter oldDelegate) => true;
898
}