flutter_goldens_test.dart 5.95 KB
Newer Older
1 2 3 4
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5
import 'dart:io' as io;
6 7 8 9 10 11 12 13 14 15 16
import 'dart:typed_data';

import 'package:file/file.dart';
import 'package:file/memory.dart';
import 'package:flutter_goldens/flutter_goldens.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mockito/mockito.dart';
import 'package:platform/platform.dart';
import 'package:process/process.dart';

const String _kFlutterRoot = '/flutter';
17 18 19 20
const String _kRepositoryRoot = '$_kFlutterRoot/bin/cache/pkg/goldens';
const String _kVersionFile = '$_kFlutterRoot/bin/internal/goldens.version';
const String _kGoldensVersion = '123456abcdef';

21 22 23 24 25
void main() {
  MemoryFileSystem fs;
  FakePlatform platform;
  MockProcessManager process;

26
  setUp(() {
27
    fs = MemoryFileSystem();
28
    platform = FakePlatform(environment: <String, String>{'FLUTTER_ROOT': _kFlutterRoot});
29
    process = MockProcessManager();
30 31 32 33
    fs.directory(_kFlutterRoot).createSync(recursive: true);
    fs.directory(_kRepositoryRoot).createSync(recursive: true);
    fs.file(_kVersionFile).createSync(recursive: true);
    fs.file(_kVersionFile).writeAsStringSync(_kGoldensVersion);
34 35
  });

36 37
  group('GoldensClient', () {
    GoldensClient goldens;
38 39

    setUp(() {
40
      goldens = GoldensClient(
41 42 43 44 45 46
        fs: fs,
        platform: platform,
        process: process,
      );
    });

47 48 49 50 51 52 53 54 55 56 57 58 59
    group('prepare', () {
      test('performs minimal work if versions match', () async {
        when(process.run(any, workingDirectory: anyNamed('workingDirectory')))
            .thenAnswer((_) => Future<io.ProcessResult>.value(io.ProcessResult(123, 0, _kGoldensVersion, '')));
        await goldens.prepare();

        // Verify that we only spawned `git rev-parse HEAD`
        final VerificationResult verifyProcessRun =
            verify(process.run(captureAny, workingDirectory: captureAnyNamed('workingDirectory')));
        verifyProcessRun.called(1);
        expect(verifyProcessRun.captured.first, <String>['git', 'rev-parse', 'HEAD']);
        expect(verifyProcessRun.captured.last, _kRepositoryRoot);
      });
60 61 62 63 64 65 66 67
    });
  });

  group('FlutterGoldenFileComparator', () {
    MemoryFileSystem fs;
    FlutterGoldenFileComparator comparator;

    setUp(() {
68
      fs = MemoryFileSystem();
69 70
      final Directory flutterRoot = fs.directory('/path/to/flutter')..createSync(recursive: true);
      final Directory goldensRoot = flutterRoot.childDirectory('bin/cache/goldens')..createSync(recursive: true);
71
      final Directory testDirectory = goldensRoot.childDirectory('test/foo/bar')..createSync(recursive: true);
72
      comparator = FlutterGoldenFileComparator(testDirectory.uri, fs: fs);
73 74 75 76
    });

    group('fromDefaultComparator', () {
      test('calculates the basedir correctly', () async {
77 78 79 80 81 82 83 84 85 86 87
        final MockGoldensClient goldens = MockGoldensClient();
        final MockLocalFileComparator defaultComparator = MockLocalFileComparator();
        final Directory flutterRoot = fs.directory('/foo')..createSync(recursive: true);
        final Directory goldensRoot = flutterRoot.childDirectory('bar')..createSync(recursive: true);
        when(goldens.fs).thenReturn(fs);
        when(goldens.flutterRoot).thenReturn(flutterRoot);
        when(goldens.repositoryRoot).thenReturn(goldensRoot);
        when(defaultComparator.basedir).thenReturn(flutterRoot.childDirectory('baz').uri);
        comparator = await FlutterGoldenFileComparator.fromDefaultComparator(
            goldens: goldens, defaultComparator: defaultComparator);
        expect(comparator.basedir, fs.directory('/foo/bar/baz').uri);
88
      });
89 90 91
    });

    group('compare', () {
92 93 94 95 96 97 98
      test('throws if golden file is not found', () async {
        try {
          await comparator.compare(Uint8List.fromList(<int>[1, 2, 3]), Uri.parse('test.png'));
          fail('TestFailure expected but not thrown');
        } on TestFailure catch (error) {
          expect(error.message, contains('Could not be compared against non-existent file'));
        }
99 100
      });

101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
      test('returns false if golden bytes do not match', () async {
        final File goldenFile = fs.file('/path/to/flutter/bin/cache/goldens/test/foo/bar/test.png')
          ..createSync(recursive: true);
        goldenFile.writeAsBytesSync(<int>[4, 5, 6], flush: true);
        final bool result = await comparator.compare(Uint8List.fromList(<int>[1, 2, 3]), Uri.parse('test.png'));
        expect(result, isFalse);
      });

      test('returns true if golden bytes match', () async {
        final File goldenFile = fs.file('/path/to/flutter/bin/cache/goldens/test/foo/bar/test.png')
          ..createSync(recursive: true);
        goldenFile.writeAsBytesSync(<int>[1, 2, 3], flush: true);
        final bool result = await comparator.compare(Uint8List.fromList(<int>[1, 2, 3]), Uri.parse('test.png'));
        expect(result, isTrue);
      });
116 117 118 119 120 121
    });

    group('update', () {
      test('creates golden file if it does not already exist', () async {
        final File goldenFile = fs.file('/path/to/flutter/bin/cache/goldens/test/foo/bar/test.png');
        expect(goldenFile.existsSync(), isFalse);
122
        await comparator.update(Uri.parse('test.png'), Uint8List.fromList(<int>[1, 2, 3]));
123 124 125 126 127 128 129 130
        expect(goldenFile.existsSync(), isTrue);
        expect(goldenFile.readAsBytesSync(), <int>[1, 2, 3]);
      });

      test('overwrites golden bytes if golden file already exist', () async {
        final File goldenFile = fs.file('/path/to/flutter/bin/cache/goldens/test/foo/bar/test.png')
          ..createSync(recursive: true);
        goldenFile.writeAsBytesSync(<int>[4, 5, 6], flush: true);
131
        await comparator.update(Uri.parse('test.png'), Uint8List.fromList(<int>[1, 2, 3]));
132 133 134 135 136 137 138
        expect(goldenFile.readAsBytesSync(), <int>[1, 2, 3]);
      });
    });
  });
}

class MockProcessManager extends Mock implements ProcessManager {}
139
class MockGoldensClient extends Mock implements GoldensClient {}
140
class MockLocalFileComparator extends Mock implements LocalFileComparator {}