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

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

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

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

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

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

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

    final LinearGradient actual = LinearGradient.lerp(testGradient1, testGradient2, 0.5);
    expect(actual, const LinearGradient(
93 94 95 96
      begin: Alignment(0.0, -1.0),
      end: Alignment(-1.0, 0.0),
      colors: <Color>[
        Color(0x3B3B3B3B),
97
        Color(0x55555555),
98
        Color(0x77777777),
99
      ],
100
      stops: <double>[
101 102 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
        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,
175 176 177 178
      ],
    ));
  });

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

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

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

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

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

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

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

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

335
    expect(actual.focal, isNull);
336

337
    expect(actual, const RadialGradient(
338
      center: Alignment(0.0, -1.0),
339
      radius: 15.0,
340 341
      colors: <Color>[
        Color(0x3B3B3B3B),
342 343 344 345 346 347
        Color(0x55555555),
        Color(0x77777777),
      ],
      stops: <double>[
        0.0,
        0.5,
348
        1.0,
349 350 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
      ],
    ));
  });

  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),
413
        Color(0x77777777),
414
      ],
415
      stops: <double>[
416 417 418 419
        0.0,
        0.5,
        0.7,
        1.0,
420 421 422 423
      ],
    ));
  });

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

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

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

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

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

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

    final SweepGradient actual = SweepGradient.lerp(testGradient1, testGradient2, 0.5);
    expect(actual, const SweepGradient(
553
      center: Alignment(0.0, -1.0),
554 555
      startAngle: math.pi / 4,
      endAngle: math.pi * 3/4,
556 557
      colors: <Color>[
        Color(0x3B3B3B3B),
558
        Color(0x55555555),
559
        Color(0x77777777),
560
      ],
561
      stops: <double>[
562 563 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
        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,
636 637 638 639 640
      ],
    ));
  });

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

651
    final SweepGradient actual = testGradient.scale(0.5);
652

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

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

    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)', () {
711
    const LinearGradient testGradient1 = LinearGradient(
712 713
      begin: Alignment.topLeft,
      end: Alignment.bottomRight,
714 715 716
      colors: <Color>[
        Color(0x33333333),
        Color(0x66666666),
717 718
      ],
    );
719
    const RadialGradient testGradient2 = RadialGradient(
720 721
      center: Alignment.center,
      radius: 20.0,
722 723 724
      colors: <Color>[
        Color(0x44444444),
        Color(0x88888888),
725 726 727 728 729 730 731
      ],
    );

    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));
  });
732 733

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

  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)
            ),
          ),
        ),
      ));
810 811 812 813
      await expectLater(
        find.byKey(painterKey),
        matchesGoldenFile(goldenName),
      );
814 815
    }

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

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

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;

848
}