gradient_test.dart 21.9 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
Ian Hickson's avatar
Ian Hickson committed
4

5 6
// @dart = 2.8

7
import 'dart:math' as math;
8

9
import 'package:flutter/widgets.dart';
10 11 12 13 14
import 'package:flutter_test/flutter_test.dart';
import 'package:flutter/painting.dart';

void main() {
  test('LinearGradient scale test', () {
15
    const LinearGradient testGradient = LinearGradient(
16
      begin: Alignment.bottomRight,
17 18 19 20 21
      end: Alignment(0.7, 1.0),
      colors: <Color>[
        Color(0x00FFFFFF),
        Color(0x11777777),
        Color(0x44444444),
22 23 24 25 26
      ],
    );
    final LinearGradient actual = LinearGradient.lerp(null, testGradient, 0.25);

    expect(actual, const LinearGradient(
27
      begin: Alignment.bottomRight,
28 29 30 31 32
      end: Alignment(0.7, 1.0),
      colors: <Color>[
        Color(0x00FFFFFF),
        Color(0x04777777),
        Color(0x11444444),
33 34 35 36 37
      ],
    ));
  });

  test('LinearGradient lerp test', () {
38
    const LinearGradient testGradient1 = LinearGradient(
39 40
      begin: Alignment.topLeft,
      end: Alignment.bottomLeft,
41 42 43
      colors: <Color>[
        Color(0x33333333),
        Color(0x66666666),
44 45
      ],
    );
46
    const LinearGradient testGradient2 = LinearGradient(
47 48
      begin: Alignment.topRight,
      end: Alignment.topLeft,
49 50 51
      colors: <Color>[
        Color(0x44444444),
        Color(0x88888888),
52 53 54
      ],
    );

55
    final LinearGradient actual = LinearGradient.lerp(testGradient1, testGradient2, 0.5);
56
    expect(actual, const LinearGradient(
57 58 59 60 61
      begin: Alignment(0.0, -1.0),
      end: Alignment(-1.0, 0.0),
      colors: <Color>[
        Color(0x3B3B3B3B),
        Color(0x77777777),
62
      ],
63
      stops: <double>[0, 1],
64 65 66
    ));
  });

67
  test('LinearGradient lerp test with stops', () {
68
    const LinearGradient testGradient1 = LinearGradient(
69 70
      begin: Alignment.topLeft,
      end: Alignment.bottomLeft,
71 72 73
      colors: <Color>[
        Color(0x33333333),
        Color(0x66666666),
74
      ],
75
      stops: <double>[
76 77 78 79
        0.0,
        0.5,
      ],
    );
80
    const LinearGradient testGradient2 = LinearGradient(
81 82
      begin: Alignment.topRight,
      end: Alignment.topLeft,
83 84 85
      colors: <Color>[
        Color(0x44444444),
        Color(0x88888888),
86
      ],
87
      stops: <double>[
88 89 90 91 92 93 94
        0.5,
        1.0,
      ],
    );

    final LinearGradient actual = LinearGradient.lerp(testGradient1, testGradient2, 0.5);
    expect(actual, const LinearGradient(
95 96 97 98
      begin: Alignment(0.0, -1.0),
      end: Alignment(-1.0, 0.0),
      colors: <Color>[
        Color(0x3B3B3B3B),
99
        Color(0x55555555),
100
        Color(0x77777777),
101
      ],
102
      stops: <double>[
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 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
        0.0,
        0.5,
        1.0,
      ],
    ));
  });

  test('LinearGradient lerp test with unequal number of colors', () {
    const LinearGradient testGradient1 = LinearGradient(
      colors: <Color>[
        Color(0x22222222),
        Color(0x66666666),
      ],
    );
    const LinearGradient testGradient2 = LinearGradient(
      colors: <Color>[
        Color(0x44444444),
        Color(0x66666666),
        Color(0x88888888),
      ],
    );

    final LinearGradient actual = LinearGradient.lerp(testGradient1, testGradient2, 0.5);
    expect(actual, const LinearGradient(
      colors: <Color>[
        Color(0x33333333),
        Color(0x55555555),
        Color(0x77777777),
      ],
      stops: <double>[
        0.0,
        0.5,
        1.0,
      ],
    ));
  });

  test('LinearGradient lerp test with stops and unequal number of colors', () {
    const LinearGradient testGradient1 = LinearGradient(
      colors: <Color>[
        Color(0x33333333),
        Color(0x66666666),
      ],
      stops: <double>[
        0.0,
        0.5,
      ],
    );
    const LinearGradient testGradient2 = LinearGradient(
      colors: <Color>[
        Color(0x44444444),
        Color(0x48484848),
        Color(0x88888888),
      ],
      stops: <double>[
        0.5,
        0.7,
        1.0,
      ],
    );

    final LinearGradient actual = LinearGradient.lerp(testGradient1, testGradient2, 0.5);
    expect(actual, const LinearGradient(
      colors: <Color>[
        Color(0x3B3B3B3B),
        Color(0x55555555),
        Color(0x57575757),
        Color(0x77777777),
      ],
      stops: <double>[
        0.0,
        0.5,
        0.7,
        1.0,
177 178 179 180
      ],
    ));
  });

181 182 183
  test('LinearGradient toString', () {
    expect(
      const LinearGradient(
184 185
        begin: Alignment.topLeft,
        end: Alignment.bottomLeft,
186 187 188
        colors: <Color>[
          Color(0x33333333),
          Color(0x66666666),
189 190 191
        ],
      ).toString(),
      equals(
192
        'LinearGradient(topLeft, bottomLeft, [Color(0x33333333), Color(0x66666666)], null, TileMode.clamp)',
193 194 195 196
      ),
    );
  });

197
  test('LinearGradient with AlignmentDirectional', () {
198 199 200
    expect(
      () {
        return const LinearGradient(
201
          begin: AlignmentDirectional.topStart,
202
          colors: <Color>[ Color(0xFFFFFFFF), Color(0xFFFFFFFF) ],
Dan Field's avatar
Dan Field committed
203
        ).createShader(const Rect.fromLTWH(0.0, 0.0, 100.0, 100.0));
204 205 206 207 208 209
      },
      throwsAssertionError,
    );
    expect(
      () {
        return const LinearGradient(
210
          begin: AlignmentDirectional.topStart,
211
          colors: <Color>[ Color(0xFFFFFFFF), Color(0xFFFFFFFF) ],
Dan Field's avatar
Dan Field committed
212
        ).createShader(const Rect.fromLTWH(0.0, 0.0, 100.0, 100.0), textDirection: TextDirection.rtl);
213 214 215 216 217 218
      },
      returnsNormally,
    );
    expect(
      () {
        return const LinearGradient(
219
          begin: AlignmentDirectional.topStart,
220
          colors: <Color>[ Color(0xFFFFFFFF), Color(0xFFFFFFFF) ],
Dan Field's avatar
Dan Field committed
221
        ).createShader(const Rect.fromLTWH(0.0, 0.0, 100.0, 100.0), textDirection: TextDirection.ltr);
222 223 224 225 226 227
      },
      returnsNormally,
    );
    expect(
      () {
        return const LinearGradient(
228
          begin: Alignment.topLeft,
229
          colors: <Color>[ Color(0xFFFFFFFF), Color(0xFFFFFFFF) ],
Dan Field's avatar
Dan Field committed
230
        ).createShader(const Rect.fromLTWH(0.0, 0.0, 100.0, 100.0));
231 232 233 234 235
      },
      returnsNormally,
    );
  });

236
  test('RadialGradient with AlignmentDirectional', () {
237 238 239
    expect(
      () {
        return const RadialGradient(
240
          center: AlignmentDirectional.topStart,
241
          colors: <Color>[ Color(0xFFFFFFFF), Color(0xFFFFFFFF) ],
Dan Field's avatar
Dan Field committed
242
        ).createShader(const Rect.fromLTWH(0.0, 0.0, 100.0, 100.0));
243 244 245
      },
      throwsAssertionError,
    );
246

247 248 249
    expect(
      () {
        return const RadialGradient(
250
          center: AlignmentDirectional.topStart,
251
          colors: <Color>[ Color(0xFFFFFFFF), Color(0xFFFFFFFF) ],
Dan Field's avatar
Dan Field committed
252
        ).createShader(const Rect.fromLTWH(0.0, 0.0, 100.0, 100.0), textDirection: TextDirection.rtl);
253 254 255 256 257 258
      },
      returnsNormally,
    );
    expect(
      () {
        return const RadialGradient(
259
          center: AlignmentDirectional.topStart,
260
          colors: <Color>[ Color(0xFFFFFFFF), Color(0xFFFFFFFF) ],
Dan Field's avatar
Dan Field committed
261
        ).createShader(const Rect.fromLTWH(0.0, 0.0, 100.0, 100.0), textDirection: TextDirection.ltr);
262 263 264 265 266 267
      },
      returnsNormally,
    );
    expect(
      () {
        return const RadialGradient(
268
          center: Alignment.topLeft,
269
          colors: <Color>[ Color(0xFFFFFFFF), Color(0xFFFFFFFF) ],
Dan Field's avatar
Dan Field committed
270
        ).createShader(const Rect.fromLTWH(0.0, 0.0, 100.0, 100.0));
271 272 273 274
      },
      returnsNormally,
    );
  });
275 276

  test('RadialGradient lerp test', () {
277
    const RadialGradient testGradient1 = RadialGradient(
278 279
      center: Alignment.topLeft,
      radius: 20.0,
280 281 282
      colors: <Color>[
        Color(0x33333333),
        Color(0x66666666),
283 284
      ],
    );
285
    const RadialGradient testGradient2 = RadialGradient(
286 287
      center: Alignment.topRight,
      radius: 10.0,
288 289 290
      colors: <Color>[
        Color(0x44444444),
        Color(0x88888888),
291 292 293 294 295
      ],
    );

    final RadialGradient actual = RadialGradient.lerp(testGradient1, testGradient2, 0.5);
    expect(actual, const RadialGradient(
296
      center: Alignment(0.0, -1.0),
297
      radius: 15.0,
298 299 300
      colors: <Color>[
        Color(0x3B3B3B3B),
        Color(0x77777777),
301
      ],
302 303 304 305
      stops: <double>[
        0.0,
        1.0,
      ],
306 307 308
    ));
  });

309
  test('RadialGradient lerp test with stops', () {
310
    const RadialGradient testGradient1 = RadialGradient(
311 312
      center: Alignment.topLeft,
      radius: 20.0,
313 314 315
      colors: <Color>[
        Color(0x33333333),
        Color(0x66666666),
316
      ],
317
      stops: <double>[
318 319 320 321
        0.0,
        0.5,
      ],
    );
322
    const RadialGradient testGradient2 = RadialGradient(
323 324
      center: Alignment.topRight,
      radius: 10.0,
325 326 327
      colors: <Color>[
        Color(0x44444444),
        Color(0x88888888),
328
      ],
329
      stops: <double>[
330 331 332 333 334 335
        0.5,
        1.0,
      ],
    );

    final RadialGradient actual = RadialGradient.lerp(testGradient1, testGradient2, 0.5);
336

337
    expect(actual.focal, isNull);
338

339
    expect(actual, const RadialGradient(
340
      center: Alignment(0.0, -1.0),
341
      radius: 15.0,
342 343
      colors: <Color>[
        Color(0x3B3B3B3B),
344 345 346 347 348 349
        Color(0x55555555),
        Color(0x77777777),
      ],
      stops: <double>[
        0.0,
        0.5,
350
        1.0,
351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 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
      ],
    ));
  });

  test('RadialGradient lerp test with unequal number of colors', () {
    const RadialGradient testGradient1 = RadialGradient(
      colors: <Color>[
        Color(0x22222222),
        Color(0x66666666),
      ],
    );
    const RadialGradient testGradient2 = RadialGradient(
      colors: <Color>[
        Color(0x44444444),
        Color(0x66666666),
        Color(0x88888888),
      ],
    );

    final RadialGradient actual = RadialGradient.lerp(testGradient1, testGradient2, 0.5);
    expect(actual, const RadialGradient(
      colors: <Color>[
        Color(0x33333333),
        Color(0x55555555),
        Color(0x77777777),
      ],
      stops: <double>[
        0.0,
        0.5,
        1.0,
      ],
    ));
  });

  test('RadialGradient lerp test with stops and unequal number of colors', () {
    const RadialGradient testGradient1 = RadialGradient(
      colors: <Color>[
        Color(0x33333333),
        Color(0x66666666),
      ],
      stops: <double>[
        0.0,
        0.5,
      ],
    );
    const RadialGradient testGradient2 = RadialGradient(
      colors: <Color>[
        Color(0x44444444),
        Color(0x48484848),
        Color(0x88888888),
      ],
      stops: <double>[
        0.5,
        0.7,
        1.0,
      ],
    );

    final RadialGradient actual = RadialGradient.lerp(testGradient1, testGradient2, 0.5);
    expect(actual, const RadialGradient(
      colors: <Color>[
        Color(0x3B3B3B3B),
        Color(0x55555555),
        Color(0x57575757),
415
        Color(0x77777777),
416
      ],
417
      stops: <double>[
418 419 420 421
        0.0,
        0.5,
        0.7,
        1.0,
422 423 424 425
      ],
    ));
  });

426
  test('RadialGradient lerp test with focal', () {
427
    const RadialGradient testGradient1 = RadialGradient(
428 429 430 431
      center: Alignment.topLeft,
      focal: Alignment.centerLeft,
      radius: 20.0,
      focalRadius: 10.0,
432 433 434
      colors: <Color>[
        Color(0x33333333),
        Color(0x66666666),
435 436
      ],
    );
437
    const RadialGradient testGradient2 = RadialGradient(
438 439 440 441
      center: Alignment.topRight,
      focal: Alignment.centerRight,
      radius: 10.0,
      focalRadius: 5.0,
442 443 444
      colors: <Color>[
        Color(0x44444444),
        Color(0x88888888),
445 446
      ],
    );
447
    const RadialGradient testGradient3 = RadialGradient(
448 449
      center: Alignment.topRight,
      radius: 10.0,
450 451 452
      colors: <Color>[
        Color(0x44444444),
        Color(0x88888888),
453 454 455 456 457
      ],
    );

    final RadialGradient actual = RadialGradient.lerp(testGradient1, testGradient2, 0.5);
    expect(actual, const RadialGradient(
458 459
      center: Alignment(0.0, -1.0),
      focal: Alignment(0.0, 0.0),
460 461
      radius: 15.0,
      focalRadius: 7.5,
462 463 464
      colors: <Color>[
        Color(0x3B3B3B3B),
        Color(0x77777777),
465
      ],
466 467 468 469
      stops: <double>[
        0.0,
        1.0,
      ],
470 471 472 473
    ));

    final RadialGradient actual2 = RadialGradient.lerp(testGradient1, testGradient3, 0.5);
    expect(actual2, const RadialGradient(
474 475
      center: Alignment(0.0, -1.0),
      focal: Alignment(-0.5, 0.0),
476 477
      radius: 15.0,
      focalRadius: 5.0,
478 479 480
      colors: <Color>[
        Color(0x3B3B3B3B),
        Color(0x77777777),
481
      ],
482 483 484 485
      stops: <double>[
        0.0,
        1.0,
      ],
486 487
    ));
  });
488

489
  test('SweepGradient lerp test', () {
490
    const SweepGradient testGradient1 = SweepGradient(
491 492 493
      center: Alignment.topLeft,
      startAngle: 0.0,
      endAngle: math.pi / 2,
494 495 496
      colors: <Color>[
        Color(0x33333333),
        Color(0x66666666),
497 498
      ],
    );
499
    const SweepGradient testGradient2 = SweepGradient(
500 501 502
      center: Alignment.topRight,
      startAngle: math.pi / 2,
      endAngle: math.pi,
503 504 505
      colors: <Color>[
        Color(0x44444444),
        Color(0x88888888),
506 507 508 509 510
      ],
    );

    final SweepGradient actual = SweepGradient.lerp(testGradient1, testGradient2, 0.5);
    expect(actual, const SweepGradient(
511
      center: Alignment(0.0, -1.0),
512 513
      startAngle: math.pi / 4,
      endAngle: math.pi * 3/4,
514 515 516
      colors: <Color>[
        Color(0x3B3B3B3B),
        Color(0x77777777),
517
      ],
518 519 520 521
      stops: <double>[
        0.0,
        1.0,
      ],
522 523 524 525
    ));
  });

  test('SweepGradient lerp test with stops', () {
526
    const SweepGradient testGradient1 = SweepGradient(
527 528 529
      center: Alignment.topLeft,
      startAngle: 0.0,
      endAngle: math.pi / 2,
530 531 532
      colors: <Color>[
        Color(0x33333333),
        Color(0x66666666),
533
      ],
534
      stops: <double>[
535 536 537 538
        0.0,
        0.5,
      ],
    );
539
    const SweepGradient testGradient2 = SweepGradient(
540 541 542
      center: Alignment.topRight,
      startAngle: math.pi / 2,
      endAngle:  math.pi,
543 544 545
      colors: <Color>[
        Color(0x44444444),
        Color(0x88888888),
546
      ],
547
      stops: <double>[
548 549 550 551 552 553 554
        0.5,
        1.0,
      ],
    );

    final SweepGradient actual = SweepGradient.lerp(testGradient1, testGradient2, 0.5);
    expect(actual, const SweepGradient(
555
      center: Alignment(0.0, -1.0),
556 557
      startAngle: math.pi / 4,
      endAngle: math.pi * 3/4,
558 559
      colors: <Color>[
        Color(0x3B3B3B3B),
560
        Color(0x55555555),
561
        Color(0x77777777),
562
      ],
563
      stops: <double>[
564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637
        0.0,
        0.5,
        1.0,
      ],
    ));
  });

  test('SweepGradient lerp test with unequal number of colors', () {
    const SweepGradient testGradient1 = SweepGradient(
      colors: <Color>[
        Color(0x22222222),
        Color(0x66666666),
      ],
    );
    const SweepGradient testGradient2 = SweepGradient(
      colors: <Color>[
        Color(0x44444444),
        Color(0x66666666),
        Color(0x88888888),
      ],
    );

    final SweepGradient actual = SweepGradient.lerp(testGradient1, testGradient2, 0.5);
    expect(actual, const SweepGradient(
      colors: <Color>[
        Color(0x33333333),
        Color(0x55555555),
        Color(0x77777777),
      ],
      stops: <double>[
        0.0,
        0.5,
        1.0,
      ],
    ));
  });

  test('SweepGradient lerp test with stops and unequal number of colors', () {
    const SweepGradient testGradient1 = SweepGradient(
      colors: <Color>[
        Color(0x33333333),
        Color(0x66666666),
      ],
      stops: <double>[
        0.0,
        0.5,
      ],
    );
    const SweepGradient testGradient2 = SweepGradient(
      colors: <Color>[
        Color(0x44444444),
        Color(0x48484848),
        Color(0x88888888),
      ],
      stops: <double>[
        0.5,
        0.7,
        1.0,
      ],
    );

    final SweepGradient actual = SweepGradient.lerp(testGradient1, testGradient2, 0.5);
    expect(actual, const SweepGradient(
      colors: <Color>[
        Color(0x3B3B3B3B),
        Color(0x55555555),
        Color(0x57575757),
        Color(0x77777777),
      ],
      stops: <double>[
        0.0,
        0.5,
        0.7,
        1.0,
638 639 640 641 642
      ],
    ));
  });

  test('SweepGradient scale test)', () {
643
    const SweepGradient testGradient = SweepGradient(
644 645 646
      center: Alignment.topLeft,
      startAngle: 0.0,
      endAngle: math.pi / 2,
647 648 649
      colors: <Color>[
        Color(0xff333333),
        Color(0xff666666),
650 651
      ],
    );
652

653
    final SweepGradient actual = testGradient.scale(0.5);
654

655 656 657 658
    expect(actual, const SweepGradient(
      center: Alignment.topLeft,
      startAngle: 0.0,
      endAngle: math.pi / 2,
659 660 661
      colors: <Color>[
        Color(0x80333333),
        Color(0x80666666),
662 663 664 665
      ],
    ));
  });

666
  test('Gradient lerp test (with RadialGradient)', () {
667
    const RadialGradient testGradient1 = RadialGradient(
668 669
      center: Alignment.topLeft,
      radius: 20.0,
670 671 672
      colors: <Color>[
        Color(0x33333333),
        Color(0x66666666),
673
      ],
674 675 676 677
      stops: <double>[
        0.0,
        1.0,
      ],
678
    );
679 680
    const RadialGradient testGradient2 = RadialGradient(
      center: Alignment(0.0, -1.0),
681
      radius: 15.0,
682 683 684
      colors: <Color>[
        Color(0x3B3B3B3B),
        Color(0x77777777),
685
      ],
686 687 688 689
      stops: <double>[
        0.0,
        1.0,
      ],
690
    );
691
    const RadialGradient testGradient3 = RadialGradient(
692 693
      center: Alignment.topRight,
      radius: 10.0,
694 695 696
      colors: <Color>[
        Color(0x44444444),
        Color(0x88888888),
697
      ],
698 699 700 701
      stops: <double>[
        0.0,
        1.0,
      ],
702 703 704 705 706 707 708 709 710 711 712
    );

    expect(Gradient.lerp(testGradient1, testGradient3, 0.0), testGradient1);
    expect(Gradient.lerp(testGradient1, testGradient3, 0.5), testGradient2);
    expect(Gradient.lerp(testGradient1, testGradient3, 1.0), testGradient3);
    expect(Gradient.lerp(testGradient3, testGradient1, 0.0), testGradient3);
    expect(Gradient.lerp(testGradient3, testGradient1, 0.5), testGradient2);
    expect(Gradient.lerp(testGradient3, testGradient1, 1.0), testGradient1);
  });

  test('Gradient lerp test (LinearGradient to RadialGradient)', () {
713
    const LinearGradient testGradient1 = LinearGradient(
714 715
      begin: Alignment.topLeft,
      end: Alignment.bottomRight,
716 717 718
      colors: <Color>[
        Color(0x33333333),
        Color(0x66666666),
719 720
      ],
    );
721
    const RadialGradient testGradient2 = RadialGradient(
722 723
      center: Alignment.center,
      radius: 20.0,
724 725 726
      colors: <Color>[
        Color(0x44444444),
        Color(0x88888888),
727 728 729 730 731 732 733
      ],
    );

    expect(Gradient.lerp(testGradient1, testGradient2, 0.0), testGradient1);
    expect(Gradient.lerp(testGradient1, testGradient2, 1.0), testGradient2);
    expect(Gradient.lerp(testGradient1, testGradient2, 0.5), testGradient2.scale(0.0));
  });
734 735

  test('Gradients can handle missing stops and report mismatched stops', () {
736 737 738 739 740
    const LinearGradient test1a = LinearGradient(
      colors: <Color>[
        Color(0x11111111),
        Color(0x22222222),
        Color(0x33333333),
741 742
      ],
    );
743 744 745 746 747
    const RadialGradient test1b = RadialGradient(
      colors: <Color>[
        Color(0x11111111),
        Color(0x22222222),
        Color(0x33333333),
748 749
      ],
    );
750 751 752 753 754
    const LinearGradient test2a = LinearGradient(
      colors: <Color>[
        Color(0x11111111),
        Color(0x22222222),
        Color(0x33333333),
755
      ],
756
      stops: <double>[0.0, 1.0],
757
    );
758 759 760 761 762
    const RadialGradient test2b = RadialGradient(
      colors: <Color>[
        Color(0x11111111),
        Color(0x22222222),
        Color(0x33333333),
763
      ],
764
      stops: <double>[0.0, 1.0],
765
    );
Dan Field's avatar
Dan Field committed
766
    const Rect rect = Rect.fromLTWH(1.0, 2.0, 3.0, 4.0);
767 768 769 770 771
    expect(test1a.createShader(rect), isNotNull);
    expect(test1b.createShader(rect), isNotNull);
    expect(() { test2a.createShader(rect); }, throwsArgumentError);
    expect(() { test2b.createShader(rect); }, throwsArgumentError);
  });
772 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

  group('Transforms', () {
    const List<Color> colors = <Color>[Color(0xFFFFFFFF), Color(0xFF000088)];
    const Rect rect = Rect.fromLTWH(0.0, 0.0, 300.0, 400.0);
    const List<Gradient> gradients45 = <Gradient>[
      LinearGradient(colors: colors, transform: GradientRotation(math.pi/4)),
      // A radial gradient won't be interesting to rotate unless the center is changed.
      RadialGradient(colors: colors, center: Alignment.topCenter, transform: GradientRotation(math.pi/4)),
      SweepGradient(colors: colors, transform: GradientRotation(math.pi/4)),
    ];
    const List<Gradient> gradients90 = <Gradient>[
      LinearGradient(colors: colors, transform: GradientRotation(math.pi/2)),
      // A radial gradient won't be interesting to rotate unless the center is changed.
      RadialGradient(colors: colors, center: Alignment.topCenter, transform: GradientRotation(math.pi/2)),
      SweepGradient(colors: colors, transform: GradientRotation(math.pi/2)),
    ];

    const Map<Type, String> gradientSnakeCase = <Type, String> {
      LinearGradient: 'linear_gradient',
      RadialGradient: 'radial_gradient',
      SweepGradient: 'sweep_gradient',
    };

    Future<void> runTest(WidgetTester tester, Gradient gradient, double degrees) async {
      final String goldenName = '${gradientSnakeCase[gradient.runtimeType]}_$degrees.png';
      final Shader shader = gradient.createShader(
        rect,
      );
      final Key painterKey = UniqueKey();
      await tester.pumpWidget(Center(
        child: SizedBox.fromSize(
          size: rect.size,
          child: RepaintBoundary(
            key: painterKey,
            child: CustomPaint(
              painter: GradientPainter(shader, rect)
            ),
          ),
        ),
      ));
812 813 814 815
      await expectLater(
        find.byKey(painterKey),
        matchesGoldenFile(goldenName),
      );
816 817
    }

818
    group('Gradients - 45 degrees', () {
819
      for (final Gradient gradient in gradients45) {
820 821
        testWidgets('$gradient', (WidgetTester tester) async {
          await runTest(tester, gradient, 45);
822
        }, skip: isBrowser); // https://github.com/flutter/flutter/issues/41389
823
      }
824
    });
825

826
    group('Gradients - 90 degrees', () {
827
      for (final Gradient gradient in gradients90) {
828 829
        testWidgets('$gradient', (WidgetTester tester) async {
          await runTest(tester, gradient, 90);
830
        }, skip: isBrowser); // https://github.com/flutter/flutter/issues/41389
831
      }
832
    });
833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849
  });
}

class GradientPainter extends CustomPainter {
  const GradientPainter(this.shader, this.rect);

  final Shader shader;
  final Rect rect;

  @override
  void paint(Canvas canvas, Size size) {
    canvas.drawRect(rect, Paint()..shader = shader);
  }

  @override
  bool shouldRepaint(CustomPainter oldDelegate) => true;

850
}