transform_test.dart 16.5 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
import 'dart:math' as math;
6
import 'dart:ui' as ui;
7

8
import 'package:flutter/rendering.dart';
9
import 'package:flutter/widgets.dart';
10
import 'package:flutter_test/flutter_test.dart';
11
import 'package:vector_math/vector_math_64.dart';
12 13

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

55
    expect(didReceiveTap, isFalse);
56
    await tester.tapAt(const Offset(110.0, 110.0));
57
    expect(didReceiveTap, isFalse);
58
    await tester.tapAt(const Offset(190.0, 150.0));
59
    expect(didReceiveTap, isTrue);
60
  });
Hixie's avatar
Hixie committed
61

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

103
    expect(didReceiveTap, isFalse);
104
    await tester.tapAt(const Offset(110.0, 110.0));
105
    expect(didReceiveTap, isFalse);
106
    await tester.tapAt(const Offset(190.0, 150.0));
107
    expect(didReceiveTap, isTrue);
Hixie's avatar
Hixie committed
108 109
  });

110 111 112 113
  testWidgets('Transform AlignmentDirectional alignment', (WidgetTester tester) async {
    bool didReceiveTap = false;

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

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

223
    expect(didReceiveTap, isFalse);
224
    await tester.tapAt(const Offset(110.0, 110.0));
225
    expect(didReceiveTap, isFalse);
226
    await tester.tapAt(const Offset(190.0, 150.0));
227
    expect(didReceiveTap, isTrue);
Hixie's avatar
Hixie committed
228
  });
229 230 231

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

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

  testWidgets('Transform.rotate', (WidgetTester tester) async {
    await tester.pumpWidget(
262
      Transform.rotate(
263
        angle: math.pi / 2.0,
264
        child: Opacity(opacity: 0.5, child: Container()),
265 266 267 268 269 270 271
      ),
    );

    final List<Layer> layers = tester.layers
      ..retainWhere((Layer layer) => layer is TransformLayer);
    expect(layers.length, 2);
    // The first transform is from the render view.
272
    final TransformLayer layer = layers[1] as TransformLayer;
273
    final Matrix4 transform = layer.transform!;
274 275 276 277 278 279 280
    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,
    ]);
  });
281 282 283

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

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

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

  testWidgets('Transform.scale', (WidgetTester tester) async {
    await tester.pumpWidget(
317
      Transform.scale(
318
        scale: 2.0,
319
        child: Opacity(opacity: 0.5, child: Container()),
320 321 322 323 324 325 326
      ),
    );

    final List<Layer> layers = tester.layers
      ..retainWhere((Layer layer) => layer is TransformLayer);
    expect(layers.length, 2);
    // The first transform is from the render view.
327
    final TransformLayer layer = layers[1] as TransformLayer;
328
    final Matrix4 transform = layer.transform!;
329 330 331 332 333 334 335 336
    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
337 338

  testWidgets('Translated child into translated box - hit test', (WidgetTester tester) async {
339
    final GlobalKey key1 = GlobalKey();
Emmanuel Garcia's avatar
Emmanuel Garcia committed
340 341
    bool _pointerDown = false;
    await tester.pumpWidget(
342
      Transform.translate(
Emmanuel Garcia's avatar
Emmanuel Garcia committed
343
        offset: const Offset(100.0, 50.0),
344
        child: Transform.translate(
Emmanuel Garcia's avatar
Emmanuel Garcia committed
345
          offset: const Offset(1000.0, 1000.0),
346
          child: Listener(
Emmanuel Garcia's avatar
Emmanuel Garcia committed
347 348 349
            onPointerDown: (PointerDownEvent event) {
              _pointerDown = true;
            },
350
            child: Container(
Emmanuel Garcia's avatar
Emmanuel Garcia committed
351 352
              key: key1,
              color: const Color(0xFF000000),
353 354 355
            ),
          ),
        ),
Emmanuel Garcia's avatar
Emmanuel Garcia committed
356 357 358 359 360 361
      ),
    );
    expect(_pointerDown, isFalse);
    await tester.tap(find.byKey(key1));
    expect(_pointerDown, isTrue);
  });
362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380

  Widget _generateTransform(bool needsCompositing, double angle) {
    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,
    );
  }

  testWidgets(
    '3D transform renders the same with or without needsCompositing',
    (WidgetTester tester) async {
      for (double angle = 0; angle <= math.pi/4; angle += 0.01) {
        await tester.pumpWidget(RepaintBoundary(child: _generateTransform(true, angle)));
381 382
        final RenderBox renderBox = tester.binding.renderView.child!;
        final OffsetLayer layer = renderBox.debugLayer! as OffsetLayer;
383 384 385 386 387 388 389 390
        final ui.Image imageWithCompositing = await layer.toImage(renderBox.paintBounds);

        await tester.pumpWidget(RepaintBoundary(child: _generateTransform(false, angle)));
        await expectLater(find.byType(RepaintBoundary).first, matchesReferenceImage(imageWithCompositing));
      }
    },
    skip: isBrowser, // due to https://github.com/flutter/flutter/issues/42767
  );
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 450 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 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503

  testWidgets('Transform.translate with FilterQuality produces filter layer', (WidgetTester tester) async {
    await tester.pumpWidget(
      Transform.translate(
        offset: const Offset(25.0, 25.0),
        child: const SizedBox(width: 100, height: 100),
        filterQuality: FilterQuality.low,
      ),
    );
    expect(tester.layers.whereType<ImageFilterLayer>().length, 1);
  });

  testWidgets('Transform.scale with FilterQuality produces filter layer', (WidgetTester tester) async {
    await tester.pumpWidget(
      Transform.scale(
        scale: 3.14159,
        child: const SizedBox(width: 100, height: 100),
        filterQuality: FilterQuality.low,
      ),
    );
    expect(tester.layers.whereType<ImageFilterLayer>().length, 1);
  });

  testWidgets('Transform.rotate with FilterQuality produces filter layer', (WidgetTester tester) async {
    await tester.pumpWidget(
      Transform.rotate(
        angle: math.pi / 4,
        child: const SizedBox(width: 100, height: 100),
        filterQuality: FilterQuality.low,
      ),
    );
    expect(tester.layers.whereType<ImageFilterLayer>().length, 1);
  });

  testWidgets('Transform layers update to match child and filterQuality', (WidgetTester tester) async {
    await tester.pumpWidget(
      Transform.rotate(
        angle: math.pi / 4,
        child: const SizedBox(width: 100, height: 100),
        filterQuality: FilterQuality.low,
      ),
    );
    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,
        child: const SizedBox(width: 100, height: 100),
        filterQuality: FilterQuality.low,
      ),
    );
    expect(tester.layers.whereType<ImageFilterLayer>(), hasLength(1));
  });

  testWidgets('Transform layers with filterQuality golden', (WidgetTester tester) async {
    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,
              child: Center(child: Container(width: 100, height: 20, color: const Color(0xff00ff00))),
              filterQuality: FilterQuality.low,
            ),
            Transform.scale(
              scale: 1.5,
              child: Center(child: Container(width: 100, height: 20, color: const Color(0xff00ff00))),
              filterQuality: FilterQuality.low,
            ),
            Transform.translate(
              offset: const Offset(20.0, 60.0),
              child: Center(child: Container(width: 100, height: 20, color: const Color(0xff00ff00))),
              filterQuality: FilterQuality.low,
            ),
          ],
        ),
      ),
    );
    await expectLater(
      find.byType(GridView),
      matchesGoldenFile('transform_golden.BitmapRotate.png'),
    );
  });
504 505 506 507 508 509 510 511 512 513 514 515
}

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;
516
}