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

5 6
// See also dev/automated_tests/flutter_test/flutter_gold_test.dart

7
import 'dart:async';
8
import 'dart:convert';
9
import 'dart:io' hide Directory;
10
import 'dart:typed_data';
11
import 'dart:ui' show hashValues, hashList;
12 13 14

import 'package:file/file.dart';
import 'package:file/memory.dart';
15
import 'package:flutter/foundation.dart';
16 17 18 19 20
import 'package:flutter_goldens/flutter_goldens.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:platform/platform.dart';
import 'package:process/process.dart';

21 22
import 'json_templates.dart';

23
const String _kFlutterRoot = '/flutter';
24 25 26 27 28 29 30 31

// 1x1 transparent pixel
const List<int> _kTestPngBytes =
<int>[137, 80, 78, 71, 13, 10, 26, 10, 0, 0, 0, 13, 73, 72, 68, 82, 0, 0, 0,
  1, 0, 0, 0, 1, 8, 6, 0, 0, 0, 31, 21, 196, 137, 0, 0, 0, 11, 73, 68, 65, 84,
  120, 1, 99, 97, 0, 2, 0, 0, 25, 0, 5, 144, 240, 54, 245, 0, 0, 0, 0, 73, 69,
  78, 68, 174, 66, 96, 130];

32
void main() {
33 34 35 36
  late MemoryFileSystem fs;
  late FakePlatform platform;
  late FakeProcessManager process;
  late FakeHttpClient fakeHttpClient;
37

38
  setUp(() {
39
    fs = MemoryFileSystem();
40 41 42 43
    platform = FakePlatform(
      environment: <String, String>{'FLUTTER_ROOT': _kFlutterRoot},
      operatingSystem: 'macos'
    );
44 45
    process = FakeProcessManager();
    fakeHttpClient = FakeHttpClient();
46
    fs.directory(_kFlutterRoot).createSync(recursive: true);
47 48
  });

49
  group('SkiaGoldClient', () {
50 51
    late SkiaGoldClient skiaClient;
    late Directory workDirectory;
52 53

    setUp(() {
54
      workDirectory = fs.directory('/workDirectory')
55 56 57
        ..createSync(recursive: true);
      skiaClient = SkiaGoldClient(
        workDirectory,
58
        fs: fs,
59
        process: process,
60
        platform: platform,
61
        httpClient: fakeHttpClient,
62 63 64
      );
    });

65
    test('auth performs minimal work if already authorized', () async {
66
      final File authFile = fs.file('/workDirectory/temp/auth_opt.json')
67
        ..createSync(recursive: true);
68
      authFile.writeAsStringSync(authTemplate());
69
      process.fallbackProcessResult = ProcessResult(123, 0, '', '');
70 71
      await skiaClient.auth();

72
      expect(process.workingDirectories, isEmpty);
73 74
    });

75 76 77 78 79 80 81 82 83 84
    test('gsutil is checked when authorization file is present', () async {
      final File authFile = fs.file('/workDirectory/temp/auth_opt.json')
        ..createSync(recursive: true);
      authFile.writeAsStringSync(authTemplate(gsutil: true));
      expect(
        await skiaClient.clientIsAuthorized(),
        isFalse,
      );
    });

85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
    test('throws for error state from auth', () async {
      platform = FakePlatform(
        environment: <String, String>{
          'FLUTTER_ROOT': _kFlutterRoot,
          'GOLD_SERVICE_ACCOUNT' : 'Service Account',
          'GOLDCTL' : 'goldctl',
        },
        operatingSystem: 'macos'
      );

      skiaClient = SkiaGoldClient(
        workDirectory,
        fs: fs,
        process: process,
        platform: platform,
100
        httpClient: fakeHttpClient,
101 102
      );

103
      process.fallbackProcessResult = ProcessResult(123, 1, 'Fallback failure', 'Fallback failure');
104 105

      expect(
106
        skiaClient.auth(),
107 108 109 110
        throwsException,
      );
    });

111
    test('throws for error state from init', () {
112 113 114 115 116 117 118 119 120 121 122 123 124
      platform = FakePlatform(
        environment: <String, String>{
          'FLUTTER_ROOT': _kFlutterRoot,
          'GOLDCTL' : 'goldctl',
        },
        operatingSystem: 'macos'
      );

      skiaClient = SkiaGoldClient(
        workDirectory,
        fs: fs,
        process: process,
        platform: platform,
125
        httpClient: fakeHttpClient,
126 127
      );

128
      const RunInvocation gitInvocation = RunInvocation(
129
        <String>['git', 'rev-parse', 'HEAD'],
130 131 132
        '/flutter',
      );
      const RunInvocation goldctlInvocation = RunInvocation(
133 134 135 136 137 138 139 140 141 142
        <String>[
          'goldctl',
          'imgtest', 'init',
          '--instance', 'flutter',
          '--work-dir', '/workDirectory/temp',
          '--commit', '12345678',
          '--keys-file', '/workDirectory/keys.json',
          '--failure-file', '/workDirectory/failures.json',
          '--passfail',
        ],
143 144 145
        null,
      );
      process.processResults[gitInvocation] = ProcessResult(12345678, 0, '12345678', '');
146 147
      process.processResults[goldctlInvocation] = ProcessResult(123, 1, 'Expected failure', 'Expected failure');
      process.fallbackProcessResult = ProcessResult(123, 1, 'Fallback failure', 'Fallback failure');
148 149

      expect(
150
        skiaClient.imgtestInit(),
151 152
        throwsException,
      );
153 154
    });

155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
    test('Only calls init once', () async {
      platform = FakePlatform(
        environment: <String, String>{
          'FLUTTER_ROOT': _kFlutterRoot,
          'GOLDCTL' : 'goldctl',
        },
        operatingSystem: 'macos'
      );

      skiaClient = SkiaGoldClient(
        workDirectory,
        fs: fs,
        process: process,
        platform: platform,
        httpClient: fakeHttpClient,
      );

      const RunInvocation gitInvocation = RunInvocation(
        <String>['git', 'rev-parse', 'HEAD'],
        '/flutter',
      );
      const RunInvocation goldctlInvocation = RunInvocation(
        <String>[
          'goldctl',
          'imgtest', 'init',
          '--instance', 'flutter',
          '--work-dir', '/workDirectory/temp',
          '--commit', '1234',
          '--keys-file', '/workDirectory/keys.json',
          '--failure-file', '/workDirectory/failures.json',
          '--passfail',
        ],
        null,
      );
      process.processResults[gitInvocation] = ProcessResult(1234, 0, '1234', '');
      process.processResults[goldctlInvocation] = ProcessResult(5678, 0, '5678', '');
      process.fallbackProcessResult = ProcessResult(123, 1, 'Fallback failure', 'Fallback failure');

      // First call
      await skiaClient.imgtestInit();

      // Remove fake process result.
      // If the init call is executed again, the fallback process will throw.
      process.processResults.remove(goldctlInvocation);

      // Second call
      await skiaClient.imgtestInit();
    });

    test('Only calls tryjob init once', () async {
      platform = FakePlatform(
        environment: <String, String>{
          'FLUTTER_ROOT': _kFlutterRoot,
          'GOLDCTL' : 'goldctl',
          'SWARMING_TASK_ID' : '4ae997b50dfd4d11',
          'LOGDOG_STREAM_PREFIX' : 'buildbucket/cr-buildbucket.appspot.com/8885996262141582672',
          'GOLD_TRYJOB' : 'refs/pull/49815/head',
        },
        operatingSystem: 'macos'
      );

      skiaClient = SkiaGoldClient(
        workDirectory,
        fs: fs,
        process: process,
        platform: platform,
        httpClient: fakeHttpClient,
      );

      const RunInvocation gitInvocation = RunInvocation(
        <String>['git', 'rev-parse', 'HEAD'],
        '/flutter',
      );
      const RunInvocation goldctlInvocation = RunInvocation(
        <String>[
          'goldctl',
          'imgtest', 'init',
          '--instance', 'flutter',
          '--work-dir', '/workDirectory/temp',
          '--commit', '1234',
          '--keys-file', '/workDirectory/keys.json',
          '--failure-file', '/workDirectory/failures.json',
          '--passfail',
          '--crs', 'github',
          '--patchset_id', '1234',
          '--changelist', '49815',
          '--cis', 'buildbucket',
          '--jobid', '8885996262141582672',
        ],
        null,
      );
      process.processResults[gitInvocation] = ProcessResult(1234, 0, '1234', '');
      process.processResults[goldctlInvocation] = ProcessResult(5678, 0, '5678', '');
      process.fallbackProcessResult = ProcessResult(123, 1, 'Fallback failure', 'Fallback failure');

      // First call
      await skiaClient.tryjobInit();

      // Remove fake process result.
      // If the init call is executed again, the fallback process will throw.
      process.processResults.remove(goldctlInvocation);

      // Second call
      await skiaClient.tryjobInit();
    });

261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
    test('throws for error state from imgtestAdd', () {
      final File goldenFile = fs.file('/workDirectory/temp/golden_file_test.png')
        ..createSync(recursive: true);
      platform = FakePlatform(
          environment: <String, String>{
            'FLUTTER_ROOT': _kFlutterRoot,
            'GOLDCTL' : 'goldctl',
          },
          operatingSystem: 'macos'
      );

      skiaClient = SkiaGoldClient(
        workDirectory,
        fs: fs,
        process: process,
        platform: platform,
        httpClient: fakeHttpClient,
      );

      const RunInvocation goldctlInvocation = RunInvocation(
        <String>[
          'goldctl',
          'imgtest', 'add',
          '--work-dir', '/workDirectory/temp',
          '--test-name', 'golden_file_test',
          '--png-file', '/workDirectory/temp/golden_file_test.png',
          '--passfail',
        ],
        null,
      );
291 292
      process.processResults[goldctlInvocation] = ProcessResult(123, 1, 'Expected failure', 'Expected failure');
      process.fallbackProcessResult = ProcessResult(123, 1, 'Fallback failure', 'Fallback failure');
293 294 295 296 297 298 299

      expect(
        skiaClient.imgtestAdd('golden_file_test', goldenFile),
        throwsException,
      );
    });

300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316
    test('correctly inits tryjob for luci', () async {
      platform = FakePlatform(
        environment: <String, String>{
          'FLUTTER_ROOT': _kFlutterRoot,
          'GOLDCTL' : 'goldctl',
          'SWARMING_TASK_ID' : '4ae997b50dfd4d11',
          'LOGDOG_STREAM_PREFIX' : 'buildbucket/cr-buildbucket.appspot.com/8885996262141582672',
          'GOLD_TRYJOB' : 'refs/pull/49815/head',
        },
        operatingSystem: 'macos'
      );

      skiaClient = SkiaGoldClient(
        workDirectory,
        fs: fs,
        process: process,
        platform: platform,
317
        httpClient: fakeHttpClient,
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
      );

      final List<String> ciArguments = skiaClient.getCIArguments();

      expect(
        ciArguments,
        equals(
          <String>[
            '--changelist', '49815',
            '--cis', 'buildbucket',
            '--jobid', '8885996262141582672',
          ],
        ),
      );
    });

334
    test('Creates traceID correctly', () async {
335 336 337 338 339 340 341 342 343 344 345
      String traceID;
      platform = FakePlatform(
        environment: <String, String>{
          'FLUTTER_ROOT': _kFlutterRoot,
          'GOLDCTL' : 'goldctl',
          'SWARMING_TASK_ID' : '4ae997b50dfd4d11',
          'LOGDOG_STREAM_PREFIX' : 'buildbucket/cr-buildbucket.appspot.com/8885996262141582672',
          'GOLD_TRYJOB' : 'refs/pull/49815/head',
        },
        operatingSystem: 'linux'
      );
346

347 348 349 350 351
      skiaClient = SkiaGoldClient(
        workDirectory,
        fs: fs,
        process: process,
        platform: platform,
352
        httpClient: fakeHttpClient,
353
      );
354

355
      traceID = skiaClient.getTraceID('flutter.golden.1');
356 357
      expect(
        traceID,
358
        equals('ae18c7a6aa48e0685525dfe8fdf79003'),
359
      );
360

361 362 363 364 365 366 367 368 369 370 371 372
      // Browser
      platform = FakePlatform(
        environment: <String, String>{
          'FLUTTER_ROOT': _kFlutterRoot,
          'GOLDCTL' : 'goldctl',
          'SWARMING_TASK_ID' : '4ae997b50dfd4d11',
          'LOGDOG_STREAM_PREFIX' : 'buildbucket/cr-buildbucket.appspot.com/8885996262141582672',
          'GOLD_TRYJOB' : 'refs/pull/49815/head',
          'FLUTTER_TEST_BROWSER' : 'chrome',
        },
        operatingSystem: 'linux'
      );
373

374 375 376 377 378
      skiaClient = SkiaGoldClient(
        workDirectory,
        fs: fs,
        process: process,
        platform: platform,
379
        httpClient: fakeHttpClient,
380
      );
381 382

      traceID = skiaClient.getTraceID('flutter.golden.1');
383 384
      expect(
        traceID,
385
        equals('e9d5c296c48e7126808520e9cc191243'),
386
      );
387

388 389 390 391 392 393 394 395 396 397 398 399 400
      // Locally - should defer to luci traceID
      platform = FakePlatform(
        environment: <String, String>{
          'FLUTTER_ROOT': _kFlutterRoot,
        },
        operatingSystem: 'macos'
      );

      skiaClient = SkiaGoldClient(
        workDirectory,
        fs: fs,
        process: process,
        platform: platform,
401
        httpClient: fakeHttpClient,
402
      );
403 404

      traceID = skiaClient.getTraceID('flutter.golden.1');
405 406
      expect(
        traceID,
407
        equals('9968695b9ae78cdb77cbb2be621ca2d6'),
408 409 410 411
      );
    });

    group('Request Handling', () {
412
      const String expectation = '55109a4bed52acc780530f7a9aeff6c0';
413 414 415 416 417

      test('image bytes are processed properly', () async {
        final Uri imageUrl = Uri.parse(
          'https://flutter-gold.skia.org/img/images/$expectation.png'
        );
418 419
        final FakeHttpClientRequest fakeImageRequest = FakeHttpClientRequest();
        final FakeHttpImageResponse fakeImageResponse = FakeHttpImageResponse(
420 421
          imageResponseTemplate()
        );
422 423 424

        fakeHttpClient.request = fakeImageRequest;
        fakeImageRequest.response = fakeImageResponse;
425 426 427

        final List<int> masterBytes = await skiaClient.getImageBytes(expectation);

428
        expect(fakeHttpClient.lastUri, imageUrl);
429 430
        expect(masterBytes, equals(_kTestPngBytes));
      });
431 432
    });
  });
433

434
  group('FlutterGoldenFileComparator', () {
435
    late FlutterGoldenFileComparator comparator;
436 437

    setUp(() {
438 439
      final Directory basedir = fs.directory('flutter/test/library/')
        ..createSync(recursive: true);
440
      comparator = FlutterPostSubmitFileComparator(
441
        basedir.uri,
442
        FakeSkiaGoldClient(),
443 444
        fs: fs,
        platform: platform,
445 446 447
      );
    });

448
    test('calculates the basedir correctly from defaultComparator for local testing', () async {
449
      final FakeLocalFileComparator defaultComparator = FakeLocalFileComparator();
450 451
      final Directory flutterRoot = fs.directory(platform.environment['FLUTTER_ROOT'])
        ..createSync(recursive: true);
452
      defaultComparator.basedir = flutterRoot.childDirectory('baz').uri;
453 454 455 456 457 458 459 460 461 462 463 464 465 466 467

      final Directory basedir = FlutterGoldenFileComparator.getBaseDirectory(
        defaultComparator,
        platform,
      );
      expect(
        basedir.uri,
        fs.directory('/flutter/bin/cache/pkg/skia_goldens/baz').uri,
      );
    });

    test('ignores version number', () {
      final Uri key = comparator.getTestUri(Uri.parse('foo.png'), 1);
      expect(key, Uri.parse('foo.png'));
    });
468

469
    group('Post-Submit', () {
470
      late FakeSkiaGoldClient fakeSkiaClient;
471 472

      setUp(() {
473
        fakeSkiaClient = FakeSkiaGoldClient();
474
        final Directory basedir = fs.directory('flutter/test/library/')
475
          ..createSync(recursive: true);
476
        comparator = FlutterPostSubmitFileComparator(
477
          basedir.uri,
478
          fakeSkiaClient,
479 480 481
          fs: fs,
          platform: platform,
        );
482
      });
483

484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503
      test('asserts .png format', () async {
        await expectLater(
          () async {
            return comparator.compare(
              Uint8List.fromList(_kTestPngBytes),
              Uri.parse('flutter.golden_test.1'),
            );
          },
          throwsA(
            isA<AssertionError>().having((AssertionError error) => error.toString(),
              'description',
              contains(
                'Golden files in the Flutter framework must end with the file '
                'extension .png.'
              ),
            ),
          ),
        );
      });

504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521
      test('calls init during compare', () {
        expect(fakeSkiaClient.initCalls, 0);
        comparator.compare(
          Uint8List.fromList(_kTestPngBytes),
          Uri.parse('flutter.golden_test.1.png'),
        );
        expect(fakeSkiaClient.initCalls, 1);
      });

      test('does not call init in during construction', () {
        expect(fakeSkiaClient.initCalls, 0);
        FlutterPostSubmitFileComparator.fromDefaultComparator(
          platform,
          goldens: fakeSkiaClient,
        );
        expect(fakeSkiaClient.initCalls, 0);
      });

522
      group('correctly determines testing environment', () {
523
        test('returns true for configured Luci', () {
524 525 526 527 528 529 530 531 532 533 534 535 536 537
          platform = FakePlatform(
            environment: <String, String>{
              'FLUTTER_ROOT': _kFlutterRoot,
              'SWARMING_TASK_ID' : '12345678990',
              'GOLDCTL' : 'goldctl',
            },
            operatingSystem: 'macos'
          );
          expect(
            FlutterPostSubmitFileComparator.isAvailableForEnvironment(platform),
            isTrue,
          );
        });

538
        test('returns false - GOLDCTL not present', () {
539 540 541
          platform = FakePlatform(
            environment: <String, String>{
              'FLUTTER_ROOT': _kFlutterRoot,
542
              'SWARMING_TASK_ID' : '12345678990',
543 544 545 546
            },
            operatingSystem: 'macos'
          );
          expect(
547
            FlutterPostSubmitFileComparator.isAvailableForEnvironment(platform),
548 549 550 551
            isFalse,
          );
        });

552
        test('returns false - GOLD_TRYJOB active', () {
553 554 555
          platform = FakePlatform(
            environment: <String, String>{
              'FLUTTER_ROOT': _kFlutterRoot,
556 557 558
              'SWARMING_TASK_ID' : '12345678990',
              'GOLDCTL' : 'goldctl',
              'GOLD_TRYJOB' : 'git/ref/12345/head'
559 560 561 562
            },
            operatingSystem: 'macos'
          );
          expect(
563
            FlutterPostSubmitFileComparator.isAvailableForEnvironment(platform),
564 565 566 567
            isFalse,
          );
        });

568
        test('returns false - on Cirrus', () {
569 570 571 572 573
          platform = FakePlatform(
            environment: <String, String>{
              'FLUTTER_ROOT': _kFlutterRoot,
              'CIRRUS_CI': 'true',
              'CIRRUS_PR': '',
574
              'CIRRUS_BRANCH': 'master',
575 576 577 578 579
              'GOLD_SERVICE_ACCOUNT': 'service account...'
            },
            operatingSystem: 'macos'
          );
          expect(
580
            FlutterPostSubmitFileComparator.isAvailableForEnvironment(platform),
581 582 583
            isFalse,
          );
        });
584
      });
585
    });
586

587
    group('Pre-Submit', () {
588 589 590 591 592 593 594 595 596 597 598 599 600 601
      late FakeSkiaGoldClient fakeSkiaClient;

      setUp(() {
        fakeSkiaClient = FakeSkiaGoldClient();
        final Directory basedir = fs.directory('flutter/test/library/')
          ..createSync(recursive: true);
        comparator = FlutterPreSubmitFileComparator(
          basedir.uri,
          fakeSkiaClient,
          fs: fs,
          platform: platform,
        );
      });

602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621
      test('asserts .png format', () async {
        await expectLater(
          () async {
            return comparator.compare(
              Uint8List.fromList(_kTestPngBytes),
              Uri.parse('flutter.golden_test.1'),
            );
          },
          throwsA(
            isA<AssertionError>().having((AssertionError error) => error.toString(),
              'description',
              contains(
                'Golden files in the Flutter framework must end with the file '
                'extension .png.'
              ),
            ),
          ),
        );
      });

622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639
      test('calls init during compare', () {
        expect(fakeSkiaClient.tryInitCalls, 0);
        comparator.compare(
          Uint8List.fromList(_kTestPngBytes),
          Uri.parse('flutter.golden_test.1.png'),
        );
        expect(fakeSkiaClient.tryInitCalls, 1);
      });

      test('does not call init in during construction', () {
        expect(fakeSkiaClient.tryInitCalls, 0);
        FlutterPostSubmitFileComparator.fromDefaultComparator(
          platform,
          goldens: fakeSkiaClient,
        );
        expect(fakeSkiaClient.tryInitCalls, 0);
      });

640
      group('correctly determines testing environment', () {
641
        test('returns true for Luci', () {
642 643 644
          platform = FakePlatform(
            environment: <String, String>{
              'FLUTTER_ROOT': _kFlutterRoot,
645 646 647
              'SWARMING_TASK_ID' : '12345678990',
              'GOLDCTL' : 'goldctl',
              'GOLD_TRYJOB' : 'git/ref/12345/head'
648 649 650 651 652 653 654 655
            },
            operatingSystem: 'macos'
          );
          expect(
            FlutterPreSubmitFileComparator.isAvailableForEnvironment(platform),
            isTrue,
          );
        });
656

657
        test('returns false - not on Luci', () {
658 659 660 661 662 663 664 665
          platform = FakePlatform(
            environment: <String, String>{
              'FLUTTER_ROOT': _kFlutterRoot,
            },
            operatingSystem: 'macos'
          );
          expect(
            FlutterPreSubmitFileComparator.isAvailableForEnvironment(platform),
666
            isFalse,
667 668 669
          );
        });

670
        test('returns false - GOLDCTL missing', () {
671 672 673
          platform = FakePlatform(
            environment: <String, String>{
              'FLUTTER_ROOT': _kFlutterRoot,
674 675
              'SWARMING_TASK_ID' : '12345678990',
              'GOLD_TRYJOB' : 'git/ref/12345/head'
676 677 678 679 680 681 682 683 684
            },
            operatingSystem: 'macos'
          );
          expect(
            FlutterPreSubmitFileComparator.isAvailableForEnvironment(platform),
            isFalse,
          );
        });

685
        test('returns false - GOLD_TRYJOB missing', () {
686 687 688
          platform = FakePlatform(
            environment: <String, String>{
              'FLUTTER_ROOT': _kFlutterRoot,
689 690
              'SWARMING_TASK_ID' : '12345678990',
              'GOLDCTL' : 'goldctl',
691 692 693 694 695 696 697 698 699
            },
            operatingSystem: 'macos'
          );
          expect(
            FlutterPreSubmitFileComparator.isAvailableForEnvironment(platform),
            isFalse,
          );
        });

700
        test('returns false - on Cirrus', () {
701 702 703
          platform = FakePlatform(
            environment: <String, String>{
              'FLUTTER_ROOT': _kFlutterRoot,
704 705 706 707
              'CIRRUS_CI': 'true',
              'CIRRUS_PR': '',
              'CIRRUS_BRANCH': 'master',
              'GOLD_SERVICE_ACCOUNT': 'service account...'
708 709 710 711
            },
            operatingSystem: 'macos'
          );
          expect(
712
            FlutterPostSubmitFileComparator.isAvailableForEnvironment(platform),
713 714 715
            isFalse,
          );
        });
716
      });
717
    });
718

719 720 721 722 723 724 725 726 727
    group('Skipping', () {
      group('correctly determines testing environment', () {
        test('returns true on Cirrus builds', () {
          platform = FakePlatform(
            environment: <String, String>{
              'FLUTTER_ROOT': _kFlutterRoot,
              'CIRRUS_CI' : 'yep',
            },
            operatingSystem: 'macos'
728
          );
729
          expect(
730
            FlutterSkippingFileComparator.isAvailableForEnvironment(platform),
731
            isTrue,
732
          );
733 734
        });

735
        test('returns true on irrelevant LUCI builds', () {
736 737 738
          platform = FakePlatform(
            environment: <String, String>{
              'FLUTTER_ROOT': _kFlutterRoot,
739
              'SWARMING_TASK_ID' : '1234567890',
740 741 742 743
            },
            operatingSystem: 'macos'
          );
          expect(
744
            FlutterSkippingFileComparator.isAvailableForEnvironment(platform),
745 746 747
            isTrue,
          );
        });
748

749 750 751 752 753 754 755 756
        test('returns false - no CI', () {
          platform = FakePlatform(
            environment: <String, String>{
              'FLUTTER_ROOT': _kFlutterRoot,
            },
            operatingSystem: 'macos'
          );
          expect(
757
            FlutterSkippingFileComparator.isAvailableForEnvironment(
758 759 760 761
              platform),
            isFalse,
          );
        });
762 763 764
      });
    });

765
    group('Local', () {
766
      late FlutterLocalFileComparator comparator;
767
      final FakeSkiaGoldClient fakeSkiaClient = FakeSkiaGoldClient();
768

769 770 771 772 773
      setUp(() async {
        final Directory basedir = fs.directory('flutter/test/library/')
          ..createSync(recursive: true);
        comparator = FlutterLocalFileComparator(
          basedir.uri,
774
          fakeSkiaClient,
775 776 777 778 779 780 781
          fs: fs,
          platform: FakePlatform(
            environment: <String, String>{'FLUTTER_ROOT': _kFlutterRoot},
            operatingSystem: 'macos'
          ),
        );

782 783 784 785
        const String hash = '55109a4bed52acc780530f7a9aeff6c0';
        fakeSkiaClient.expectationForTestValues['flutter.golden_test.1'] = hash;
        fakeSkiaClient.imageBytesValues[hash] =_kTestPngBytes;
        fakeSkiaClient.cleanTestNameValues['library.flutter.golden_test.1.png'] = 'flutter.golden_test.1';
786 787
      });

788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807
      test('asserts .png format', () async {
        await expectLater(
          () async {
            return comparator.compare(
              Uint8List.fromList(_kTestPngBytes),
              Uri.parse('flutter.golden_test.1'),
            );
          },
          throwsA(
            isA<AssertionError>().having((AssertionError error) => error.toString(),
              'description',
              contains(
                'Golden files in the Flutter framework must end with the file '
                'extension .png.'
              ),
            ),
          ),
        );
      });

808 809 810 811 812 813 814 815 816 817
      test('passes when bytes match', () async {
        expect(
          await comparator.compare(
            Uint8List.fromList(_kTestPngBytes),
            Uri.parse('flutter.golden_test.1.png'),
          ),
          isTrue,
        );
      });

818
      test('returns FlutterSkippingGoldenFileComparator when network connection is unavailable', () async {
819 820 821 822 823
        final FakeDirectory fakeDirectory = FakeDirectory();
        fakeDirectory.existsSyncValue = true;
        fakeDirectory.uri = Uri.parse('/flutter');

        fakeSkiaClient.getExpectationForTestThrowable = const OSError("Can't reach Gold");
824 825 826

        FlutterGoldenFileComparator comparator = await FlutterLocalFileComparator.fromDefaultComparator(
          platform,
827 828
          goldens: fakeSkiaClient,
          baseDirectory: fakeDirectory,
829
        );
830
        expect(comparator.runtimeType, FlutterSkippingFileComparator);
831

832 833
        fakeSkiaClient.getExpectationForTestThrowable =  const SocketException("Can't reach Gold");

834
        comparator = await FlutterLocalFileComparator.fromDefaultComparator(
835
          platform,
836 837
          goldens: fakeSkiaClient,
          baseDirectory: fakeDirectory,
838
        );
839
        expect(comparator.runtimeType, FlutterSkippingFileComparator);
840 841
        // reset property or it will carry on to other tests
        fakeSkiaClient.getExpectationForTestThrowable = null;
842
      });
843
    });
844 845 846
  });
}

847 848 849 850 851
@immutable
class RunInvocation {
  const RunInvocation(this.command, this.workingDirectory);

  final List<String> command;
852
  final String? workingDirectory;
853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880

  @override
  int get hashCode => hashValues(hashList(command), workingDirectory);

  bool _commandEquals(List<String> other) {
    if (other == command) {
      return true;
    }
    if (other.length != command.length) {
      return false;
    }
    for (int index = 0; index < other.length; index += 1) {
      if (other[index] != command[index]) {
        return false;
      }
    }
    return true;
  }

  @override
  bool operator ==(Object other) {
    if (other.runtimeType != runtimeType) {
      return false;
    }
    return other is RunInvocation
        && _commandEquals(other.command)
        && other.workingDirectory == workingDirectory;
  }
881

882 883 884
  @override
  String toString() => '$command ($workingDirectory)';
}
885

886 887
class FakeProcessManager extends Fake implements ProcessManager {
  Map<RunInvocation, ProcessResult> processResults = <RunInvocation, ProcessResult>{};
888

889 890
  /// Used if [processResults] does not contain a matching invocation.
  ProcessResult? fallbackProcessResult;
891

892
  final List<String?> workingDirectories = <String?>[];
893

894 895
  @override
  Future<ProcessResult> run(
896 897 898
    List<Object> command, {
    String? workingDirectory,
    Map<String, String>? environment,
899 900
    bool includeParentEnvironment = true,
    bool runInShell = false,
901 902
    Encoding? stdoutEncoding = systemEncoding,
    Encoding? stderrEncoding = systemEncoding,
903 904
  }) async {
    workingDirectories.add(workingDirectory);
905
    final ProcessResult? result = processResults[RunInvocation(command.cast<String>(), workingDirectory)];
906
    if (result == null && fallbackProcessResult == null) {
907
      printOnFailure('ProcessManager.run was called with $command ($workingDirectory) unexpectedly - $processResults.');
908
      fail('See above.');
909
    }
910
    return result ?? fallbackProcessResult!;
911 912 913
  }
}

914
// See also dev/automated_tests/flutter_test/flutter_gold_test.dart
915 916
class FakeSkiaGoldClient extends Fake implements SkiaGoldClient {
  Map<String, String> expectationForTestValues = <String, String>{};
917
  Exception? getExpectationForTestThrowable;
918 919 920
  @override
  Future<String> getExpectationForTest(String testName) async {
    if (getExpectationForTestThrowable != null) {
921
      throw getExpectationForTestThrowable!;
922
    }
923
    return expectationForTestValues[testName] ?? '';
924 925
  }

926 927 928 929 930 931 932 933 934 935 936 937 938 939 940
  @override
  Future<void> auth() async {}

  int initCalls = 0;
  @override
  Future<void> imgtestInit() async => initCalls += 1;
  @override
  Future<bool> imgtestAdd(String testName, File goldenFile) async => true;

  int tryInitCalls = 0;
  @override
  Future<void> tryjobInit() async => tryInitCalls += 1;
  @override
  Future<bool> tryjobAdd(String testName, File goldenFile) async => true;

941 942
  Map<String, List<int>> imageBytesValues = <String, List<int>>{};
  @override
943
  Future<List<int>> getImageBytes(String imageHash) async => imageBytesValues[imageHash]!;
944 945 946

  Map<String, String> cleanTestNameValues = <String, String>{};
  @override
947
  String cleanTestName(String fileName) => cleanTestNameValues[fileName] ?? '';
948 949 950 951
}

class FakeLocalFileComparator extends Fake implements LocalFileComparator {
  @override
952
  late Uri basedir;
953 954 955
}

class FakeDirectory extends Fake implements Directory {
956
  late bool existsSyncValue;
957 958 959 960
  @override
  bool existsSync() => existsSyncValue;

  @override
961
  late Uri uri;
962 963 964
}

class FakeHttpClient extends Fake implements HttpClient {
965 966
  late Uri lastUri;
  late FakeHttpClientRequest request;
967 968 969 970 971 972 973 974 975

  @override
  Future<HttpClientRequest> getUrl(Uri url) async {
    lastUri = url;
    return request;
  }
}

class FakeHttpClientRequest extends Fake implements HttpClientRequest {
976
  late FakeHttpImageResponse response;
977 978 979 980 981 982

  @override
  Future<HttpClientResponse> close() async {
    return response;
  }
}
983

984 985
class FakeHttpClientResponse extends Fake implements HttpClientResponse {
  FakeHttpClientResponse(this.response);
986

987
  final List<int> response;
988 989

  @override
990
  StreamSubscription<List<int>> listen(
991
    void Function(List<int> event)? onData, {
992
      Function? onError,
993
      void Function()? onDone,
994
      bool? cancelOnError,
995
    }) {
996
    return Stream<List<int>>.fromFuture(Future<List<int>>.value(response))
997 998 999 1000
      .listen(onData, onError: onError, onDone: onDone, cancelOnError: cancelOnError);
  }
}

1001 1002
class FakeHttpImageResponse extends Fake implements HttpClientResponse {
  FakeHttpImageResponse(this.response);
1003 1004 1005 1006

  final List<List<int>> response;

  @override
1007
  Future<void> forEach(void Function(List<int> element) action) async {
1008 1009 1010
    response.forEach(action);
  }
}