blob: e93945b1e60a45a0c172b00ab9b66e9a4c4739d2 [file] [log] [blame]
Todd Volkerte19db892018-04-30 10:35:56 -07001// Copyright 2018 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5import 'dart:async';
6import 'dart:io' as io;
7import 'dart:typed_data';
8
9import 'package:file/memory.dart';
10import 'package:test/test.dart' as test_package;
11import 'package:test/test.dart' hide test;
12
13import 'package:flutter_test/flutter_test.dart' show goldenFileComparator, LocalFileComparator;
14
Todd Volkert00aac682018-07-27 08:44:39 -070015const List<int> _kExpectedBytes = const <int>[1, 2, 3];
Todd Volkerte19db892018-04-30 10:35:56 -070016
17void main() {
18 MemoryFileSystem fs;
19
20 setUp(() {
21 final FileSystemStyle style = io.Platform.isWindows
22 ? FileSystemStyle.windows
23 : FileSystemStyle.posix;
24 fs = new MemoryFileSystem(style: style);
25 });
26
Todd Volkert65079ad2018-05-03 07:39:41 -070027 /// Converts posix-style paths to the style associated with [fs].
28 ///
29 /// This allows us to deal in posix-style paths in the tests.
30 String fix(String path) {
31 if (path.startsWith('/')) {
32 path = '${fs.style.drive}$path';
33 }
34 return path.replaceAll('/', fs.path.separator);
35 }
36
Todd Volkerte19db892018-04-30 10:35:56 -070037 void test(String description, FutureOr<void> body()) {
38 test_package.test(description, () {
39 return io.IOOverrides.runZoned(
40 body,
41 createDirectory: (String path) => fs.directory(path),
42 createFile: (String path) => fs.file(path),
43 createLink: (String path) => fs.link(path),
44 getCurrentDirectory: () => fs.currentDirectory,
45 setCurrentDirectory: (String path) => fs.currentDirectory = path,
46 getSystemTempDirectory: () => fs.systemTempDirectory,
47 stat: (String path) => fs.stat(path),
48 statSync: (String path) => fs.statSync(path),
49 fseIdentical: (String p1, String p2) => fs.identical(p1, p2),
50 fseIdenticalSync: (String p1, String p2) => fs.identicalSync(p1, p2),
51 fseGetType: (String path, bool followLinks) => fs.type(path, followLinks: followLinks),
52 fseGetTypeSync: (String path, bool followLinks) => fs.typeSync(path, followLinks: followLinks),
53 fsWatch: (String a, int b, bool c) => throw new UnsupportedError('unsupported'),
54 fsWatchIsSupported: () => fs.isWatchSupported,
55 );
56 });
57 }
58
59 group('goldenFileComparator', () {
60 test('is initialized by test framework', () {
61 expect(goldenFileComparator, isNotNull);
Ian Hickson65992712018-06-19 17:22:56 -070062 expect(goldenFileComparator, const isInstanceOf<LocalFileComparator>());
Todd Volkerte19db892018-04-30 10:35:56 -070063 final LocalFileComparator comparator = goldenFileComparator;
64 expect(comparator.basedir.path, contains('flutter_test'));
65 });
66 });
67
68 group('LocalFileComparator', () {
69 LocalFileComparator comparator;
70
71 setUp(() {
Todd Volkert65079ad2018-05-03 07:39:41 -070072 comparator = new LocalFileComparator(fs.file(fix('/golden_test.dart')).uri, pathStyle: fs.path.style);
Todd Volkerte19db892018-04-30 10:35:56 -070073 });
74
75 test('calculates basedir correctly', () {
Todd Volkert65079ad2018-05-03 07:39:41 -070076 expect(comparator.basedir, fs.file(fix('/')).uri);
77 comparator = new LocalFileComparator(fs.file(fix('/foo/bar/golden_test.dart')).uri, pathStyle: fs.path.style);
78 expect(comparator.basedir, fs.directory(fix('/foo/bar/')).uri);
Todd Volkerte19db892018-04-30 10:35:56 -070079 });
80
Todd Volkertbe09a202018-05-04 10:31:53 -070081 test('can be instantiated with uri that represents file in same folder', () {
82 comparator = new LocalFileComparator(Uri.parse('foo_test.dart'), pathStyle: fs.path.style);
83 expect(comparator.basedir, Uri.parse('./'));
84 });
85
Todd Volkerte19db892018-04-30 10:35:56 -070086 group('compare', () {
87 Future<bool> doComparison([String golden = 'golden.png']) {
Todd Volkert65079ad2018-05-03 07:39:41 -070088 final Uri uri = fs.file(fix(golden)).uri;
Todd Volkerte19db892018-04-30 10:35:56 -070089 return comparator.compare(
90 new Uint8List.fromList(_kExpectedBytes),
91 uri,
92 );
93 }
94
95 group('succeeds', () {
96 test('when golden file is in same folder as test', () async {
Todd Volkert65079ad2018-05-03 07:39:41 -070097 fs.file(fix('/golden.png')).writeAsBytesSync(_kExpectedBytes);
Todd Volkerte19db892018-04-30 10:35:56 -070098 final bool success = await doComparison();
99 expect(success, isTrue);
100 });
101
102 test('when golden file is in subfolder of test', () async {
Todd Volkert65079ad2018-05-03 07:39:41 -0700103 fs.file(fix('/sub/foo.png'))
Todd Volkerte19db892018-04-30 10:35:56 -0700104 ..createSync(recursive: true)
105 ..writeAsBytesSync(_kExpectedBytes);
106 final bool success = await doComparison('sub/foo.png');
107 expect(success, isTrue);
108 });
Todd Volkertbe09a202018-05-04 10:31:53 -0700109
110 group('when comparator instantiated with uri that represents file in same folder', () {
111 test('and golden file is in same folder as test', () async {
112 fs.file(fix('/foo/bar/golden.png'))
113 ..createSync(recursive: true)
114 ..writeAsBytesSync(_kExpectedBytes);
115 fs.currentDirectory = fix('/foo/bar');
116 comparator = new LocalFileComparator(Uri.parse('local_test.dart'), pathStyle: fs.path.style);
117 final bool success = await doComparison('golden.png');
118 expect(success, isTrue);
119 });
120
121 test('and golden file is in subfolder of test', () async {
122 fs.file(fix('/foo/bar/baz/golden.png'))
123 ..createSync(recursive: true)
124 ..writeAsBytesSync(_kExpectedBytes);
125 fs.currentDirectory = fix('/foo/bar');
126 comparator = new LocalFileComparator(Uri.parse('local_test.dart'), pathStyle: fs.path.style);
127 final bool success = await doComparison('baz/golden.png');
128 expect(success, isTrue);
129 });
130 });
Todd Volkerte19db892018-04-30 10:35:56 -0700131 });
132
133 group('fails', () {
134 test('when golden file does not exist', () async {
135 final Future<bool> comparison = doComparison();
Ian Hickson65992712018-06-19 17:22:56 -0700136 expect(comparison, throwsA(const isInstanceOf<TestFailure>()));
Todd Volkerte19db892018-04-30 10:35:56 -0700137 });
138
139 test('when golden bytes are leading subset of image bytes', () async {
Todd Volkert65079ad2018-05-03 07:39:41 -0700140 fs.file(fix('/golden.png')).writeAsBytesSync(<int>[1, 2]);
Todd Volkerte19db892018-04-30 10:35:56 -0700141 expect(await doComparison(), isFalse);
142 });
143
144 test('when golden bytes are leading superset of image bytes', () async {
Todd Volkert65079ad2018-05-03 07:39:41 -0700145 fs.file(fix('/golden.png')).writeAsBytesSync(<int>[1, 2, 3, 4]);
Todd Volkerte19db892018-04-30 10:35:56 -0700146 expect(await doComparison(), isFalse);
147 });
148
149 test('when golden bytes are trailing subset of image bytes', () async {
Todd Volkert65079ad2018-05-03 07:39:41 -0700150 fs.file(fix('/golden.png')).writeAsBytesSync(<int>[2, 3]);
Todd Volkerte19db892018-04-30 10:35:56 -0700151 expect(await doComparison(), isFalse);
152 });
153
154 test('when golden bytes are trailing superset of image bytes', () async {
Todd Volkert65079ad2018-05-03 07:39:41 -0700155 fs.file(fix('/golden.png')).writeAsBytesSync(<int>[0, 1, 2, 3]);
Todd Volkerte19db892018-04-30 10:35:56 -0700156 expect(await doComparison(), isFalse);
157 });
158
159 test('when golden bytes are disjoint from image bytes', () async {
Todd Volkert65079ad2018-05-03 07:39:41 -0700160 fs.file(fix('/golden.png')).writeAsBytesSync(<int>[4, 5, 6]);
Todd Volkerte19db892018-04-30 10:35:56 -0700161 expect(await doComparison(), isFalse);
162 });
163
164 test('when golden bytes are empty', () async {
Todd Volkert65079ad2018-05-03 07:39:41 -0700165 fs.file(fix('/golden.png')).writeAsBytesSync(<int>[]);
Todd Volkerte19db892018-04-30 10:35:56 -0700166 expect(await doComparison(), isFalse);
167 });
168 });
169 });
170
171 group('update', () {
172 test('updates existing file', () async {
Todd Volkert65079ad2018-05-03 07:39:41 -0700173 fs.file(fix('/golden.png')).writeAsBytesSync(_kExpectedBytes);
Todd Volkert00aac682018-07-27 08:44:39 -0700174 const List<int> newBytes = const <int>[11, 12, 13];
Todd Volkert65079ad2018-05-03 07:39:41 -0700175 await comparator.update(fs.file('golden.png').uri, new Uint8List.fromList(newBytes));
176 expect(fs.file(fix('/golden.png')).readAsBytesSync(), newBytes);
Todd Volkerte19db892018-04-30 10:35:56 -0700177 });
178
179 test('creates non-existent file', () async {
Todd Volkert65079ad2018-05-03 07:39:41 -0700180 expect(fs.file(fix('/foo.png')).existsSync(), isFalse);
Todd Volkert00aac682018-07-27 08:44:39 -0700181 const List<int> newBytes = const <int>[11, 12, 13];
Todd Volkert65079ad2018-05-03 07:39:41 -0700182 await comparator.update(fs.file('foo.png').uri, new Uint8List.fromList(newBytes));
183 expect(fs.file(fix('/foo.png')).existsSync(), isTrue);
184 expect(fs.file(fix('/foo.png')).readAsBytesSync(), newBytes);
Todd Volkerte19db892018-04-30 10:35:56 -0700185 });
186 });
187 });
188}