clip_test.dart 26.1 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
Ian Hickson's avatar
Ian Hickson committed
2 3 4 5 6
// 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/material.dart';
7 8 9
import 'package:flutter/rendering.dart';

import '../rendering/mock_canvas.dart';
10
import 'test_border.dart' show TestBorder;
Ian Hickson's avatar
Ian Hickson committed
11 12 13 14 15 16 17

final List<String> log = <String>[];

class PathClipper extends CustomClipper<Path> {
  @override
  Path getClip(Size size) {
    log.add('getClip');
18
    return Path()
Dan Field's avatar
Dan Field committed
19
      ..addRect(const Rect.fromLTWH(50.0, 50.0, 100.0, 100.0));
Ian Hickson's avatar
Ian Hickson committed
20 21
  }
  @override
22
  bool shouldReclip(PathClipper oldClipper) => false;
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
}

class ValueClipper<T> extends CustomClipper<T> {
  ValueClipper(this.message, this.value);

  final String message;
  final T value;

  @override
  T getClip(Size size) {
    log.add(message);
    return value;
  }

  @override
38
  bool shouldReclip(ValueClipper<T> oldClipper) {
39 40
    return oldClipper.message != message || oldClipper.value != value;
  }
Ian Hickson's avatar
Ian Hickson committed
41 42
}

43
class NotifyClipper<T> extends CustomClipper<T> {
44
  NotifyClipper({required this.clip}) : super(reclip: clip);
45 46 47 48 49 50 51 52 53 54

  final ValueNotifier<T> clip;

  @override
  T getClip(Size size) => clip.value;

  @override
  bool shouldReclip(NotifyClipper<T> oldClipper) => clip != oldClipper.clip;
}

Ian Hickson's avatar
Ian Hickson committed
55
void main() {
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
  testWidgets('ClipRect with a FittedBox child sized to zero works with semantics', (WidgetTester tester) async {
    await tester.pumpWidget(Directionality(
        textDirection: TextDirection.ltr,
        child: ClipRect(
          child: FittedBox(
            child: SizedBox.fromSize(
              size: Size.zero,
              child: Semantics(
                image: true,
                label: 'Image',
              ),
            ),
          ),
        ),
      ),
    );
    expect(find.byType(FittedBox), findsOneWidget);
  });

75
  testWidgets('ClipRect updates clipBehavior in updateRenderObject', (WidgetTester tester) async {
76
    await tester.pumpWidget(const ClipRect());
77 78 79

    final RenderClipRect renderClip = tester.allRenderObjects.whereType<RenderClipRect>().first;

80
    expect(renderClip.clipBehavior, equals(Clip.hardEdge));
81

82
    await tester.pumpWidget(const ClipRect(clipBehavior: Clip.antiAlias));
83

84
    expect(renderClip.clipBehavior, equals(Clip.antiAlias));
85 86
  });

87 88 89 90 91 92
  test('ClipRRect constructs with the right default values', () {
    const ClipRRect clipRRect = ClipRRect();
    expect(clipRRect.clipBehavior, equals(Clip.antiAlias));
    expect(clipRRect.borderRadius, equals(BorderRadius.zero));
  });

93
  testWidgets('ClipRRect updates clipBehavior in updateRenderObject', (WidgetTester tester) async {
94
    await tester.pumpWidget(const ClipRRect());
95 96 97 98 99

    final RenderClipRRect renderClip = tester.allRenderObjects.whereType<RenderClipRRect>().first;

    expect(renderClip.clipBehavior, equals(Clip.antiAlias));

100
    await tester.pumpWidget(const ClipRRect(clipBehavior: Clip.hardEdge));
101 102 103 104 105

    expect(renderClip.clipBehavior, equals(Clip.hardEdge));
  });

  testWidgets('ClipOval updates clipBehavior in updateRenderObject', (WidgetTester tester) async {
106
    await tester.pumpWidget(const ClipOval());
107 108 109 110 111

    final RenderClipOval renderClip = tester.allRenderObjects.whereType<RenderClipOval>().first;

    expect(renderClip.clipBehavior, equals(Clip.antiAlias));

112
    await tester.pumpWidget(const ClipOval(clipBehavior: Clip.hardEdge));
113 114 115 116 117

    expect(renderClip.clipBehavior, equals(Clip.hardEdge));
  });

  testWidgets('ClipPath updates clipBehavior in updateRenderObject', (WidgetTester tester) async {
118
    await tester.pumpWidget(const ClipPath());
119 120 121 122 123

    final RenderClipPath renderClip = tester.allRenderObjects.whereType<RenderClipPath>().first;

    expect(renderClip.clipBehavior, equals(Clip.antiAlias));

124
    await tester.pumpWidget(const ClipPath(clipBehavior: Clip.hardEdge));
125 126 127 128

    expect(renderClip.clipBehavior, equals(Clip.hardEdge));
  });

129 130
  testWidgets('ClipPath', (WidgetTester tester) async {
    await tester.pumpWidget(
131 132 133
      ClipPath(
        clipper: PathClipper(),
        child: GestureDetector(
Ian Hickson's avatar
Ian Hickson committed
134 135
          behavior: HitTestBehavior.opaque,
          onTap: () { log.add('tap'); },
136
        ),
137
      ),
Ian Hickson's avatar
Ian Hickson committed
138
    );
139
    expect(log, equals(<String>['getClip']));
Ian Hickson's avatar
Ian Hickson committed
140

141
    await tester.tapAt(const Offset(10.0, 10.0));
142
    expect(log, equals(<String>['getClip']));
Ian Hickson's avatar
Ian Hickson committed
143 144
    log.clear();

145
    await tester.tapAt(const Offset(100.0, 100.0));
146
    expect(log, equals(<String>['tap']));
Ian Hickson's avatar
Ian Hickson committed
147 148 149
    log.clear();
  });

150 151
  testWidgets('ClipOval', (WidgetTester tester) async {
    await tester.pumpWidget(
152 153
      ClipOval(
        child: GestureDetector(
Ian Hickson's avatar
Ian Hickson committed
154 155
          behavior: HitTestBehavior.opaque,
          onTap: () { log.add('tap'); },
156
        ),
157
      ),
Ian Hickson's avatar
Ian Hickson committed
158
    );
159
    expect(log, equals(<String>[]));
Ian Hickson's avatar
Ian Hickson committed
160

161
    await tester.tapAt(const Offset(10.0, 10.0));
162
    expect(log, equals(<String>[]));
Ian Hickson's avatar
Ian Hickson committed
163 164
    log.clear();

165
    await tester.tapAt(const Offset(400.0, 300.0));
166
    expect(log, equals(<String>['tap']));
Ian Hickson's avatar
Ian Hickson committed
167 168
    log.clear();
  });
169

170 171
  testWidgets('Transparent ClipOval hit test', (WidgetTester tester) async {
    await tester.pumpWidget(
172
      Opacity(
173
        opacity: 0.0,
174 175
        child: ClipOval(
          child: GestureDetector(
176 177
            behavior: HitTestBehavior.opaque,
            onTap: () { log.add('tap'); },
178 179
          ),
        ),
180
      ),
181 182 183
    );
    expect(log, equals(<String>[]));

184
    await tester.tapAt(const Offset(10.0, 10.0));
185 186 187
    expect(log, equals(<String>[]));
    log.clear();

188
    await tester.tapAt(const Offset(400.0, 300.0));
189 190 191 192
    expect(log, equals(<String>['tap']));
    log.clear();
  });

193 194
  testWidgets('ClipRect', (WidgetTester tester) async {
    await tester.pumpWidget(
195
      Align(
196
        alignment: Alignment.topLeft,
197
        child: SizedBox(
198 199
          width: 100.0,
          height: 100.0,
200
          child: ClipRect(
Dan Field's avatar
Dan Field committed
201
            clipper: ValueClipper<Rect>('a', const Rect.fromLTWH(5.0, 5.0, 10.0, 10.0)),
202
            child: GestureDetector(
203 204
              behavior: HitTestBehavior.opaque,
              onTap: () { log.add('tap'); },
205 206 207
            ),
          ),
        ),
208
      ),
209 210 211
    );
    expect(log, equals(<String>['a']));

212
    await tester.tapAt(const Offset(10.0, 10.0));
213 214
    expect(log, equals(<String>['a', 'tap']));

215
    await tester.tapAt(const Offset(100.0, 100.0));
216 217 218
    expect(log, equals(<String>['a', 'tap']));

    await tester.pumpWidget(
219
      Align(
220
        alignment: Alignment.topLeft,
221
        child: SizedBox(
222 223
          width: 100.0,
          height: 100.0,
224
          child: ClipRect(
Dan Field's avatar
Dan Field committed
225
            clipper: ValueClipper<Rect>('a', const Rect.fromLTWH(5.0, 5.0, 10.0, 10.0)),
226
            child: GestureDetector(
227 228
              behavior: HitTestBehavior.opaque,
              onTap: () { log.add('tap'); },
229 230 231
            ),
          ),
        ),
232
      ),
233 234 235 236
    );
    expect(log, equals(<String>['a', 'tap']));

    await tester.pumpWidget(
237
      Align(
238
        alignment: Alignment.topLeft,
239
        child: SizedBox(
240 241
          width: 200.0,
          height: 200.0,
242
          child: ClipRect(
Dan Field's avatar
Dan Field committed
243
            clipper: ValueClipper<Rect>('a', const Rect.fromLTWH(5.0, 5.0, 10.0, 10.0)),
244
            child: GestureDetector(
245 246
              behavior: HitTestBehavior.opaque,
              onTap: () { log.add('tap'); },
247 248 249
            ),
          ),
        ),
250
      ),
251 252 253 254
    );
    expect(log, equals(<String>['a', 'tap', 'a']));

    await tester.pumpWidget(
255
      Align(
256
        alignment: Alignment.topLeft,
257
        child: SizedBox(
258 259
          width: 200.0,
          height: 200.0,
260
          child: ClipRect(
Dan Field's avatar
Dan Field committed
261
            clipper: ValueClipper<Rect>('a', const Rect.fromLTWH(5.0, 5.0, 10.0, 10.0)),
262
            child: GestureDetector(
263 264
              behavior: HitTestBehavior.opaque,
              onTap: () { log.add('tap'); },
265 266 267
            ),
          ),
        ),
268
      ),
269 270 271 272
    );
    expect(log, equals(<String>['a', 'tap', 'a']));

    await tester.pumpWidget(
273
      Align(
274
        alignment: Alignment.topLeft,
275
        child: SizedBox(
276 277
          width: 200.0,
          height: 200.0,
278
          child: ClipRect(
Dan Field's avatar
Dan Field committed
279
            clipper: ValueClipper<Rect>('b', const Rect.fromLTWH(5.0, 5.0, 10.0, 10.0)),
280
            child: GestureDetector(
281 282
              behavior: HitTestBehavior.opaque,
              onTap: () { log.add('tap'); },
283 284 285
            ),
          ),
        ),
286
      ),
287 288 289 290
    );
    expect(log, equals(<String>['a', 'tap', 'a', 'b']));

    await tester.pumpWidget(
291
      Align(
292
        alignment: Alignment.topLeft,
293
        child: SizedBox(
294 295
          width: 200.0,
          height: 200.0,
296
          child: ClipRect(
Dan Field's avatar
Dan Field committed
297
            clipper: ValueClipper<Rect>('c', const Rect.fromLTWH(25.0, 25.0, 10.0, 10.0)),
298
            child: GestureDetector(
299 300
              behavior: HitTestBehavior.opaque,
              onTap: () { log.add('tap'); },
301 302 303
            ),
          ),
        ),
304
      ),
305 306 307
    );
    expect(log, equals(<String>['a', 'tap', 'a', 'b', 'c']));

308
    await tester.tapAt(const Offset(30.0, 30.0));
309 310
    expect(log, equals(<String>['a', 'tap', 'a', 'b', 'c', 'tap']));

311
    await tester.tapAt(const Offset(100.0, 100.0));
312 313 314
    expect(log, equals(<String>['a', 'tap', 'a', 'b', 'c', 'tap']));
  });

315 316 317
  testWidgets('debugPaintSizeEnabled', (WidgetTester tester) async {
    await tester.pumpWidget(
      const ClipRect(
318
        child: Placeholder(),
319 320 321 322
      ),
    );
    expect(tester.renderObject(find.byType(ClipRect)).paint, paints
      ..save()
Dan Field's avatar
Dan Field committed
323
      ..clipRect(rect: const Rect.fromLTRB(0.0, 0.0, 800.0, 600.0))
324 325 326
      ..save()
      ..path() // Placeholder
      ..restore()
327
      ..restore(),
328 329
    );
    debugPaintSizeEnabled = true;
330
    expect(tester.renderObject(find.byType(ClipRect)).debugPaint, paints
Dan Field's avatar
Dan Field committed
331
      ..rect(rect: const Rect.fromLTRB(0.0, 0.0, 800.0, 600.0))
332
      ..paragraph(),
333 334 335
    );
    debugPaintSizeEnabled = false;
  });
336 337 338

  testWidgets('ClipRect painting', (WidgetTester tester) async {
    await tester.pumpWidget(
339 340 341
      Center(
        child: RepaintBoundary(
          child: Container(
342
            color: Colors.white,
343
            child: Padding(
344
              padding: const EdgeInsets.all(100.0),
345
              child: SizedBox(
346 347
                height: 100.0,
                width: 100.0,
348
                child: Transform.rotate(
349
                  angle: 1.0, // radians
350 351
                  child: ClipRect(
                    child: Container(
352
                      color: Colors.red,
353
                      child: Container(
354
                        color: Colors.white,
355 356 357
                        child: RepaintBoundary(
                          child: Center(
                            child: Container(
358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375
                              color: Colors.black,
                              height: 10.0,
                              width: 10.0,
                            ),
                          ),
                        ),
                      ),
                    ),
                  ),
                ),
              ),
            ),
          ),
        ),
      ),
    );
    await expectLater(
      find.byType(RepaintBoundary).first,
376
      matchesGoldenFile('clip.ClipRect.png'),
377
    );
378
  });
379

380 381
  testWidgets('ClipRect save, overlay, and antialiasing', (WidgetTester tester) async {
    await tester.pumpWidget(
382 383
      RepaintBoundary(
        child: Stack(
384 385
          textDirection: TextDirection.ltr,
          children: <Widget>[
386
            Positioned(
387 388 389 390
              top: 0.0,
              left: 0.0,
              width: 100.0,
              height: 100.0,
391 392
              child: ClipRect(
                child: Container(
393 394 395 396 397
                  color: Colors.blue,
                ),
                clipBehavior: Clip.hardEdge,
              ),
            ),
398
            Positioned(
399 400 401 402
              top: 50.0,
              left: 50.0,
              width: 100.0,
              height: 100.0,
403
              child: Transform.rotate(
404
                angle: 1.0,
405
                child: Container(
406 407 408 409 410 411 412 413 414 415
                  color: Colors.red,
                ),
              ),
            ),
          ],
        ),
      ),
    );
    await expectLater(
      find.byType(RepaintBoundary).first,
416
      matchesGoldenFile('clip.ClipRectOverlay.png'),
417
    );
418
  });
419

420 421
  testWidgets('ClipRRect painting', (WidgetTester tester) async {
    await tester.pumpWidget(
422 423 424
      Center(
        child: RepaintBoundary(
          child: Container(
425
            color: Colors.white,
426
            child: Padding(
427
              padding: const EdgeInsets.all(100.0),
428
              child: SizedBox(
429 430
                height: 100.0,
                width: 100.0,
431
                child: Transform.rotate(
432
                  angle: 1.0, // radians
433
                  child: ClipRRect(
434
                    borderRadius: const BorderRadius.only(
435 436 437 438
                      topLeft: Radius.elliptical(10.0, 20.0),
                      topRight: Radius.elliptical(5.0, 30.0),
                      bottomLeft: Radius.elliptical(2.5, 12.0),
                      bottomRight: Radius.elliptical(15.0, 6.0),
439
                    ),
440
                    child: Container(
441
                      color: Colors.red,
442
                      child: Container(
443
                        color: Colors.white,
444 445 446
                        child: RepaintBoundary(
                          child: Center(
                            child: Container(
447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464
                              color: Colors.black,
                              height: 10.0,
                              width: 10.0,
                            ),
                          ),
                        ),
                      ),
                    ),
                  ),
                ),
              ),
            ),
          ),
        ),
      ),
    );
    await expectLater(
      find.byType(RepaintBoundary).first,
465
      matchesGoldenFile('clip.ClipRRect.png'),
466
    );
467
  });
468 469 470

  testWidgets('ClipOval painting', (WidgetTester tester) async {
    await tester.pumpWidget(
471 472 473
      Center(
        child: RepaintBoundary(
          child: Container(
474
            color: Colors.white,
475
            child: Padding(
476
              padding: const EdgeInsets.all(100.0),
477
              child: SizedBox(
478 479
                height: 100.0,
                width: 100.0,
480
                child: Transform.rotate(
481
                  angle: 1.0, // radians
482 483
                  child: ClipOval(
                    child: Container(
484
                      color: Colors.red,
485
                      child: Container(
486
                        color: Colors.white,
487 488 489
                        child: RepaintBoundary(
                          child: Center(
                            child: Container(
490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507
                              color: Colors.black,
                              height: 10.0,
                              width: 10.0,
                            ),
                          ),
                        ),
                      ),
                    ),
                  ),
                ),
              ),
            ),
          ),
        ),
      ),
    );
    await expectLater(
      find.byType(RepaintBoundary).first,
508
      matchesGoldenFile('clip.ClipOval.png'),
509
    );
510
  });
511 512 513

  testWidgets('ClipPath painting', (WidgetTester tester) async {
    await tester.pumpWidget(
514 515 516
      Center(
        child: RepaintBoundary(
          child: Container(
517
            color: Colors.white,
518
            child: Padding(
519
              padding: const EdgeInsets.all(100.0),
520
              child: SizedBox(
521 522
                height: 100.0,
                width: 100.0,
523
                child: Transform.rotate(
524
                  angle: 1.0, // radians
525 526 527 528
                  child: ClipPath(
                    clipper: ShapeBorderClipper(
                      shape: BeveledRectangleBorder(
                        borderRadius: BorderRadius.circular(20.0),
529 530
                      ),
                    ),
531
                    child: Container(
532
                      color: Colors.red,
533
                      child: Container(
534
                        color: Colors.white,
535 536 537
                        child: RepaintBoundary(
                          child: Center(
                            child: Container(
538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555
                              color: Colors.black,
                              height: 10.0,
                              width: 10.0,
                            ),
                          ),
                        ),
                      ),
                    ),
                  ),
                ),
              ),
            ),
          ),
        ),
      ),
    );
    await expectLater(
      find.byType(RepaintBoundary).first,
556
      matchesGoldenFile('clip.ClipPath.png'),
557
    );
558
  });
559

560
  Center genPhysicalModel(Clip clipBehavior) {
561 562 563
    return Center(
      child: RepaintBoundary(
        child: Container(
564
          color: Colors.white,
565
          child: Padding(
566
            padding: const EdgeInsets.all(100.0),
567
            child: SizedBox(
568 569
              height: 100.0,
              width: 100.0,
570
              child: Transform.rotate(
571
                angle: 1.0, // radians
572 573
                child: PhysicalModel(
                  borderRadius: BorderRadius.circular(20.0),
574 575
                  color: Colors.red,
                  clipBehavior: clipBehavior,
576
                  child: Container(
577
                    color: Colors.white,
578 579 580
                    child: RepaintBoundary(
                      child: Center(
                        child: Container(
581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600
                          color: Colors.black,
                          height: 10.0,
                          width: 10.0,
                        ),
                      ),
                    ),
                  ),
                ),
              ),
            ),
          ),
        ),
      ),
    );
  }

  testWidgets('PhysicalModel painting with Clip.antiAlias', (WidgetTester tester) async {
    await tester.pumpWidget(genPhysicalModel(Clip.antiAlias));
    await expectLater(
      find.byType(RepaintBoundary).first,
601
      matchesGoldenFile('clip.PhysicalModel.antiAlias.png'),
602
    );
603
  });
604 605 606 607 608

  testWidgets('PhysicalModel painting with Clip.hardEdge', (WidgetTester tester) async {
    await tester.pumpWidget(genPhysicalModel(Clip.hardEdge));
    await expectLater(
      find.byType(RepaintBoundary).first,
609
      matchesGoldenFile('clip.PhysicalModel.hardEdge.png'),
610
    );
611
  });
612 613 614 615 616 617 618

  // There will be bleeding edges on the rect edges, but there shouldn't be any bleeding edges on the
  // round corners.
  testWidgets('PhysicalModel painting with Clip.antiAliasWithSaveLayer', (WidgetTester tester) async {
    await tester.pumpWidget(genPhysicalModel(Clip.antiAliasWithSaveLayer));
    await expectLater(
      find.byType(RepaintBoundary).first,
619
      matchesGoldenFile('clip.PhysicalModel.antiAliasWithSaveLayer.png'),
620
    );
621
  });
622 623

  testWidgets('Default PhysicalModel painting', (WidgetTester tester) async {
624
    await tester.pumpWidget(
625 626 627
      Center(
        child: RepaintBoundary(
          child: Container(
628
            color: Colors.white,
629
            child: Padding(
630
              padding: const EdgeInsets.all(100.0),
631
              child: SizedBox(
632 633
                height: 100.0,
                width: 100.0,
634
                child: Transform.rotate(
635
                  angle: 1.0, // radians
636 637
                  child: PhysicalModel(
                    borderRadius: BorderRadius.circular(20.0),
638
                    color: Colors.red,
639
                    child: Container(
640
                      color: Colors.white,
641 642 643
                      child: RepaintBoundary(
                        child: Center(
                          child: Container(
644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660
                            color: Colors.black,
                            height: 10.0,
                            width: 10.0,
                          ),
                        ),
                      ),
                    ),
                  ),
                ),
              ),
            ),
          ),
        ),
      ),
    );
    await expectLater(
      find.byType(RepaintBoundary).first,
661
      matchesGoldenFile('clip.PhysicalModel.default.png'),
662
    );
663
  });
664 665

  Center genPhysicalShape(Clip clipBehavior) {
666 667 668
    return Center(
      child: RepaintBoundary(
        child: Container(
669
          color: Colors.white,
670
          child: Padding(
671
            padding: const EdgeInsets.all(100.0),
672
            child: SizedBox(
673 674
              height: 100.0,
              width: 100.0,
675
              child: Transform.rotate(
676
                angle: 1.0, // radians
677 678 679 680
                child: PhysicalShape(
                  clipper: ShapeBorderClipper(
                    shape: BeveledRectangleBorder(
                      borderRadius: BorderRadius.circular(20.0),
681 682 683 684
                    ),
                  ),
                  clipBehavior: clipBehavior,
                  color: Colors.red,
685
                  child: Container(
686
                    color: Colors.white,
687 688 689
                    child: RepaintBoundary(
                      child: Center(
                        child: Container(
690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709
                          color: Colors.black,
                          height: 10.0,
                          width: 10.0,
                        ),
                      ),
                    ),
                  ),
                ),
              ),
            ),
          ),
        ),
      ),
    );
  }

  testWidgets('PhysicalShape painting with Clip.antiAlias', (WidgetTester tester) async {
    await tester.pumpWidget(genPhysicalShape(Clip.antiAlias));
    await expectLater(
      find.byType(RepaintBoundary).first,
710
      matchesGoldenFile('clip.PhysicalShape.antiAlias.png'),
711
    );
712
  });
713 714 715 716 717

  testWidgets('PhysicalShape painting with Clip.hardEdge', (WidgetTester tester) async {
    await tester.pumpWidget(genPhysicalShape(Clip.hardEdge));
    await expectLater(
      find.byType(RepaintBoundary).first,
718
      matchesGoldenFile('clip.PhysicalShape.hardEdge.png'),
719
    );
720
  });
721 722 723 724 725

  testWidgets('PhysicalShape painting with Clip.antiAliasWithSaveLayer', (WidgetTester tester) async {
    await tester.pumpWidget(genPhysicalShape(Clip.antiAliasWithSaveLayer));
    await expectLater(
      find.byType(RepaintBoundary).first,
726
      matchesGoldenFile('clip.PhysicalShape.antiAliasWithSaveLayer.png'),
727
    );
728
  });
729 730 731

  testWidgets('PhysicalShape painting', (WidgetTester tester) async {
    await tester.pumpWidget(
732 733 734
      Center(
        child: RepaintBoundary(
          child: Container(
735
            color: Colors.white,
736
            child: Padding(
737
              padding: const EdgeInsets.all(100.0),
738
              child: SizedBox(
739 740
                height: 100.0,
                width: 100.0,
741
                child: Transform.rotate(
742
                  angle: 1.0, // radians
743 744 745 746
                  child: PhysicalShape(
                    clipper: ShapeBorderClipper(
                      shape: BeveledRectangleBorder(
                        borderRadius: BorderRadius.circular(20.0),
747 748 749
                      ),
                    ),
                    color: Colors.red,
750
                    child: Container(
751
                      color: Colors.white,
752 753 754
                      child: RepaintBoundary(
                        child: Center(
                          child: Container(
755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771
                            color: Colors.black,
                            height: 10.0,
                            width: 10.0,
                          ),
                        ),
                      ),
                    ),
                  ),
                ),
              ),
            ),
          ),
        ),
      ),
    );
    await expectLater(
      find.byType(RepaintBoundary).first,
772
      matchesGoldenFile('clip.PhysicalShape.default.png'),
773
    );
774
  });
775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831

  testWidgets('ClipPath.shape', (WidgetTester tester) async {
    final List<String> logs = <String>[];
    final ShapeBorder shape = TestBorder((String message) { logs.add(message); });
    Widget buildClipPath() {
      return ClipPath.shape(
        shape: shape,
        child: const SizedBox(width: 100.0, height: 100.0),
      );
    }
    final Widget clipPath = buildClipPath();
    // verify that a regular clip works as one would expect
    logs.add('--0');
    await tester.pumpWidget(clipPath);
    // verify that pumping again doesn't recompute the clip
    // even though the widget itself is new (the shape doesn't change identity)
    logs.add('--1');
    await tester.pumpWidget(buildClipPath());
    // verify that ClipPath passes the TextDirection on to its shape
    logs.add('--2');
    await tester.pumpWidget(Directionality(
      textDirection: TextDirection.ltr,
      child: clipPath,
    ));
    // verify that changing the text direction from LTR to RTL has an effect
    // even though the widget itself is identical
    logs.add('--3');
    await tester.pumpWidget(Directionality(
      textDirection: TextDirection.rtl,
      child: clipPath,
    ));
    // verify that pumping again with a text direction has no effect
    logs.add('--4');
    await tester.pumpWidget(Directionality(
      textDirection: TextDirection.rtl,
      child: buildClipPath(),
    ));
    logs.add('--5');
    // verify that changing the text direction and the widget at the same time
    // works as expected
    await tester.pumpWidget(Directionality(
      textDirection: TextDirection.ltr,
      child: clipPath,
    ));
    expect(logs, <String>[
      '--0',
      'getOuterPath Rect.fromLTRB(0.0, 0.0, 800.0, 600.0) null',
      '--1',
      '--2',
      'getOuterPath Rect.fromLTRB(0.0, 0.0, 800.0, 600.0) TextDirection.ltr',
      '--3',
      'getOuterPath Rect.fromLTRB(0.0, 0.0, 800.0, 600.0) TextDirection.rtl',
      '--4',
      '--5',
      'getOuterPath Rect.fromLTRB(0.0, 0.0, 800.0, 600.0) TextDirection.ltr',
    ]);
  });
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

  testWidgets('CustomClipper reclips when notified', (WidgetTester tester) async {
    final ValueNotifier<Rect> clip = ValueNotifier<Rect>(const Rect.fromLTWH(50.0, 50.0, 100.0, 100.0));

    await tester.pumpWidget(
      ClipRect(
        child: const Placeholder(),
        clipper: NotifyClipper<Rect>(clip: clip),
      ),
    );

    expect(tester.renderObject(find.byType(ClipRect)).paint, paints
      ..save()
      ..clipRect(rect: const Rect.fromLTWH(50.0, 50.0, 100.0, 100.0))
      ..save()
      ..path() // Placeholder
      ..restore()
      ..restore(),
    );

    expect(tester.renderObject(find.byType(ClipRect)).debugNeedsPaint, isFalse);
    clip.value = const Rect.fromLTWH(50.0, 50.0, 150.0, 100.0);
    expect(tester.renderObject(find.byType(ClipRect)).debugNeedsPaint, isTrue);

    expect(tester.renderObject(find.byType(ClipRect)).paint, paints
      ..save()
      ..clipRect(rect: const Rect.fromLTWH(50.0, 50.0, 150.0, 100.0))
      ..save()
      ..path() // Placeholder
      ..restore()
      ..restore(),
    );
  });
Ian Hickson's avatar
Ian Hickson committed
865
}