Todd Volkert | e19db89 | 2018-04-30 10:35:56 -0700 | [diff] [blame] | 1 | // 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 | |
| 5 | import 'dart:async'; |
| 6 | import 'dart:io' as io; |
| 7 | import 'dart:typed_data'; |
| 8 | |
| 9 | import 'package:file/memory.dart'; |
| 10 | import 'package:test/test.dart' as test_package; |
| 11 | import 'package:test/test.dart' hide test; |
| 12 | |
| 13 | import 'package:flutter_test/flutter_test.dart' show goldenFileComparator, LocalFileComparator; |
| 14 | |
Todd Volkert | 00aac68 | 2018-07-27 08:44:39 -0700 | [diff] [blame^] | 15 | const List<int> _kExpectedBytes = const <int>[1, 2, 3]; |
Todd Volkert | e19db89 | 2018-04-30 10:35:56 -0700 | [diff] [blame] | 16 | |
| 17 | void 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 Volkert | 65079ad | 2018-05-03 07:39:41 -0700 | [diff] [blame] | 27 | /// 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 Volkert | e19db89 | 2018-04-30 10:35:56 -0700 | [diff] [blame] | 37 | 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 Hickson | 6599271 | 2018-06-19 17:22:56 -0700 | [diff] [blame] | 62 | expect(goldenFileComparator, const isInstanceOf<LocalFileComparator>()); |
Todd Volkert | e19db89 | 2018-04-30 10:35:56 -0700 | [diff] [blame] | 63 | 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 Volkert | 65079ad | 2018-05-03 07:39:41 -0700 | [diff] [blame] | 72 | comparator = new LocalFileComparator(fs.file(fix('/golden_test.dart')).uri, pathStyle: fs.path.style); |
Todd Volkert | e19db89 | 2018-04-30 10:35:56 -0700 | [diff] [blame] | 73 | }); |
| 74 | |
| 75 | test('calculates basedir correctly', () { |
Todd Volkert | 65079ad | 2018-05-03 07:39:41 -0700 | [diff] [blame] | 76 | 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 Volkert | e19db89 | 2018-04-30 10:35:56 -0700 | [diff] [blame] | 79 | }); |
| 80 | |
Todd Volkert | be09a20 | 2018-05-04 10:31:53 -0700 | [diff] [blame] | 81 | 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 Volkert | e19db89 | 2018-04-30 10:35:56 -0700 | [diff] [blame] | 86 | group('compare', () { |
| 87 | Future<bool> doComparison([String golden = 'golden.png']) { |
Todd Volkert | 65079ad | 2018-05-03 07:39:41 -0700 | [diff] [blame] | 88 | final Uri uri = fs.file(fix(golden)).uri; |
Todd Volkert | e19db89 | 2018-04-30 10:35:56 -0700 | [diff] [blame] | 89 | 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 Volkert | 65079ad | 2018-05-03 07:39:41 -0700 | [diff] [blame] | 97 | fs.file(fix('/golden.png')).writeAsBytesSync(_kExpectedBytes); |
Todd Volkert | e19db89 | 2018-04-30 10:35:56 -0700 | [diff] [blame] | 98 | final bool success = await doComparison(); |
| 99 | expect(success, isTrue); |
| 100 | }); |
| 101 | |
| 102 | test('when golden file is in subfolder of test', () async { |
Todd Volkert | 65079ad | 2018-05-03 07:39:41 -0700 | [diff] [blame] | 103 | fs.file(fix('/sub/foo.png')) |
Todd Volkert | e19db89 | 2018-04-30 10:35:56 -0700 | [diff] [blame] | 104 | ..createSync(recursive: true) |
| 105 | ..writeAsBytesSync(_kExpectedBytes); |
| 106 | final bool success = await doComparison('sub/foo.png'); |
| 107 | expect(success, isTrue); |
| 108 | }); |
Todd Volkert | be09a20 | 2018-05-04 10:31:53 -0700 | [diff] [blame] | 109 | |
| 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 Volkert | e19db89 | 2018-04-30 10:35:56 -0700 | [diff] [blame] | 131 | }); |
| 132 | |
| 133 | group('fails', () { |
| 134 | test('when golden file does not exist', () async { |
| 135 | final Future<bool> comparison = doComparison(); |
Ian Hickson | 6599271 | 2018-06-19 17:22:56 -0700 | [diff] [blame] | 136 | expect(comparison, throwsA(const isInstanceOf<TestFailure>())); |
Todd Volkert | e19db89 | 2018-04-30 10:35:56 -0700 | [diff] [blame] | 137 | }); |
| 138 | |
| 139 | test('when golden bytes are leading subset of image bytes', () async { |
Todd Volkert | 65079ad | 2018-05-03 07:39:41 -0700 | [diff] [blame] | 140 | fs.file(fix('/golden.png')).writeAsBytesSync(<int>[1, 2]); |
Todd Volkert | e19db89 | 2018-04-30 10:35:56 -0700 | [diff] [blame] | 141 | expect(await doComparison(), isFalse); |
| 142 | }); |
| 143 | |
| 144 | test('when golden bytes are leading superset of image bytes', () async { |
Todd Volkert | 65079ad | 2018-05-03 07:39:41 -0700 | [diff] [blame] | 145 | fs.file(fix('/golden.png')).writeAsBytesSync(<int>[1, 2, 3, 4]); |
Todd Volkert | e19db89 | 2018-04-30 10:35:56 -0700 | [diff] [blame] | 146 | expect(await doComparison(), isFalse); |
| 147 | }); |
| 148 | |
| 149 | test('when golden bytes are trailing subset of image bytes', () async { |
Todd Volkert | 65079ad | 2018-05-03 07:39:41 -0700 | [diff] [blame] | 150 | fs.file(fix('/golden.png')).writeAsBytesSync(<int>[2, 3]); |
Todd Volkert | e19db89 | 2018-04-30 10:35:56 -0700 | [diff] [blame] | 151 | expect(await doComparison(), isFalse); |
| 152 | }); |
| 153 | |
| 154 | test('when golden bytes are trailing superset of image bytes', () async { |
Todd Volkert | 65079ad | 2018-05-03 07:39:41 -0700 | [diff] [blame] | 155 | fs.file(fix('/golden.png')).writeAsBytesSync(<int>[0, 1, 2, 3]); |
Todd Volkert | e19db89 | 2018-04-30 10:35:56 -0700 | [diff] [blame] | 156 | expect(await doComparison(), isFalse); |
| 157 | }); |
| 158 | |
| 159 | test('when golden bytes are disjoint from image bytes', () async { |
Todd Volkert | 65079ad | 2018-05-03 07:39:41 -0700 | [diff] [blame] | 160 | fs.file(fix('/golden.png')).writeAsBytesSync(<int>[4, 5, 6]); |
Todd Volkert | e19db89 | 2018-04-30 10:35:56 -0700 | [diff] [blame] | 161 | expect(await doComparison(), isFalse); |
| 162 | }); |
| 163 | |
| 164 | test('when golden bytes are empty', () async { |
Todd Volkert | 65079ad | 2018-05-03 07:39:41 -0700 | [diff] [blame] | 165 | fs.file(fix('/golden.png')).writeAsBytesSync(<int>[]); |
Todd Volkert | e19db89 | 2018-04-30 10:35:56 -0700 | [diff] [blame] | 166 | expect(await doComparison(), isFalse); |
| 167 | }); |
| 168 | }); |
| 169 | }); |
| 170 | |
| 171 | group('update', () { |
| 172 | test('updates existing file', () async { |
Todd Volkert | 65079ad | 2018-05-03 07:39:41 -0700 | [diff] [blame] | 173 | fs.file(fix('/golden.png')).writeAsBytesSync(_kExpectedBytes); |
Todd Volkert | 00aac68 | 2018-07-27 08:44:39 -0700 | [diff] [blame^] | 174 | const List<int> newBytes = const <int>[11, 12, 13]; |
Todd Volkert | 65079ad | 2018-05-03 07:39:41 -0700 | [diff] [blame] | 175 | await comparator.update(fs.file('golden.png').uri, new Uint8List.fromList(newBytes)); |
| 176 | expect(fs.file(fix('/golden.png')).readAsBytesSync(), newBytes); |
Todd Volkert | e19db89 | 2018-04-30 10:35:56 -0700 | [diff] [blame] | 177 | }); |
| 178 | |
| 179 | test('creates non-existent file', () async { |
Todd Volkert | 65079ad | 2018-05-03 07:39:41 -0700 | [diff] [blame] | 180 | expect(fs.file(fix('/foo.png')).existsSync(), isFalse); |
Todd Volkert | 00aac68 | 2018-07-27 08:44:39 -0700 | [diff] [blame^] | 181 | const List<int> newBytes = const <int>[11, 12, 13]; |
Todd Volkert | 65079ad | 2018-05-03 07:39:41 -0700 | [diff] [blame] | 182 | 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 Volkert | e19db89 | 2018-04-30 10:35:56 -0700 | [diff] [blame] | 185 | }); |
| 186 | }); |
| 187 | }); |
| 188 | } |