Ian Hickson | 449f4a6 | 2019-11-27 15:04:02 -0800 | [diff] [blame] | 1 | // Copyright 2014 The Flutter Authors. All rights reserved. |
Todd Volkert | 65079ad | 2018-05-03 07:39:41 -0700 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
Kate Lovett | 9e3e44e | 2019-04-19 11:32:16 -0700 | [diff] [blame] | 4 | |
Todd Volkert | 65079ad | 2018-05-03 07:39:41 -0700 | [diff] [blame] | 5 | import 'dart:async'; |
Kate Lovett | b1ca7f4 | 2019-11-15 13:04:59 -0800 | [diff] [blame] | 6 | import 'dart:io' as io; |
Kate Lovett | 1374a41 | 2019-11-27 11:29:53 -0800 | [diff] [blame] | 7 | import 'dart:math' as math; |
Todd Volkert | 65079ad | 2018-05-03 07:39:41 -0700 | [diff] [blame] | 8 | import 'dart:typed_data'; |
| 9 | |
Todd Volkert | 65079ad | 2018-05-03 07:39:41 -0700 | [diff] [blame] | 10 | import 'package:file/file.dart'; |
| 11 | import 'package:file/local.dart'; |
| 12 | import 'package:flutter_test/flutter_test.dart'; |
| 13 | import 'package:meta/meta.dart'; |
Kate Lovett | 616794f | 2019-07-28 12:26:06 -0700 | [diff] [blame] | 14 | import 'package:platform/platform.dart'; |
Todd Volkert | 65079ad | 2018-05-03 07:39:41 -0700 | [diff] [blame] | 15 | |
Kate Lovett | 616794f | 2019-07-28 12:26:06 -0700 | [diff] [blame] | 16 | import 'package:flutter_goldens_client/skia_client.dart'; |
Kate Lovett | 616794f | 2019-07-28 12:26:06 -0700 | [diff] [blame] | 17 | export 'package:flutter_goldens_client/skia_client.dart'; |
Kate Lovett | 7e542fc | 2019-04-19 14:33:49 -0700 | [diff] [blame] | 18 | |
Kate Lovett | 7bc0203 | 2019-10-25 15:05:21 -0700 | [diff] [blame] | 19 | // If you are here trying to figure out how to use golden files in the Flutter |
| 20 | // repo itself, consider reading this wiki page: |
| 21 | // https://github.com/flutter/flutter/wiki/Writing-a-golden-file-test-for-package%3Aflutter |
| 22 | |
| 23 | const String _kFlutterRootKey = 'FLUTTER_ROOT'; |
| 24 | |
Todd Volkert | 65079ad | 2018-05-03 07:39:41 -0700 | [diff] [blame] | 25 | /// Main method that can be used in a `flutter_test_config.dart` file to set |
| 26 | /// [goldenFileComparator] to an instance of [FlutterGoldenFileComparator] that |
Kate Lovett | 616794f | 2019-07-28 12:26:06 -0700 | [diff] [blame] | 27 | /// works for the current test. _Which_ FlutterGoldenFileComparator is |
| 28 | /// instantiated is based on the current testing environment. |
Todd Volkert | 65079ad | 2018-05-03 07:39:41 -0700 | [diff] [blame] | 29 | Future<void> main(FutureOr<void> testMain()) async { |
Kate Lovett | 616794f | 2019-07-28 12:26:06 -0700 | [diff] [blame] | 30 | const Platform platform = LocalPlatform(); |
Kate Lovett | 7bc0203 | 2019-10-25 15:05:21 -0700 | [diff] [blame] | 31 | if (FlutterSkiaGoldFileComparator.isAvailableForEnvironment(platform)) { |
| 32 | goldenFileComparator = await FlutterSkiaGoldFileComparator.fromDefaultComparator(platform); |
| 33 | } else if (FlutterPreSubmitFileComparator.isAvailableForEnvironment(platform)) { |
| 34 | goldenFileComparator = await FlutterPreSubmitFileComparator.fromDefaultComparator(platform); |
Kate Lovett | b1ca7f4 | 2019-11-15 13:04:59 -0800 | [diff] [blame] | 35 | } else if (FlutterSkippingGoldenFileComparator.isAvailableForEnvironment(platform)) { |
| 36 | goldenFileComparator = FlutterSkippingGoldenFileComparator.fromDefaultComparator( |
| 37 | 'Golden file testing is unavailable on LUCI and some Cirrus shards.' |
| 38 | ); |
Kate Lovett | 7bc0203 | 2019-10-25 15:05:21 -0700 | [diff] [blame] | 39 | } else { |
| 40 | goldenFileComparator = await FlutterLocalFileComparator.fromDefaultComparator(platform); |
Kate Lovett | 616794f | 2019-07-28 12:26:06 -0700 | [diff] [blame] | 41 | } |
Kate Lovett | b1ca7f4 | 2019-11-15 13:04:59 -0800 | [diff] [blame] | 42 | |
Todd Volkert | 65079ad | 2018-05-03 07:39:41 -0700 | [diff] [blame] | 43 | await testMain(); |
| 44 | } |
| 45 | |
Kate Lovett | 616794f | 2019-07-28 12:26:06 -0700 | [diff] [blame] | 46 | /// Abstract base class golden file comparator specific to the `flutter/flutter` |
| 47 | /// repository. |
Kate Lovett | 7bc0203 | 2019-10-25 15:05:21 -0700 | [diff] [blame] | 48 | /// |
| 49 | /// Golden file testing for the `flutter/flutter` repository is handled by three |
| 50 | /// different [FlutterGoldenFileComparator]s, depending on the current testing |
| 51 | /// environment. |
| 52 | /// |
| 53 | /// * The [FlutterSkiaGoldFileComparator] is utilized during post-submit |
| 54 | /// testing, after a pull request has landed on the master branch. This |
| 55 | /// comparator uses the [SkiaGoldClient] and the `goldctl` tool to upload |
| 56 | /// tests to the [Flutter Gold dashboard](https://flutter-gold.skia.org). |
| 57 | /// Flutter Gold manages the master golden files for the `flutter/flutter` |
| 58 | /// repository. |
| 59 | /// |
| 60 | /// * The [FlutterPreSubmitFileComparator] is utilized in pre-submit testing, |
| 61 | /// before a pull request can land on the master branch. This comparator |
| 62 | /// uses the [SkiaGoldClient] to request the baseline images kept by the |
| 63 | /// [Flutter Gold dashboard](https://flutter-gold.skia.org). It then |
| 64 | /// compares the current test image to the baseline images using the |
| 65 | /// standard [GoldenFileComparator.compareLists] to detect any pixel |
| 66 | /// difference. The [SkiaGoldClient] is also used here to check the active |
| 67 | /// ignores from the dashboard, in order to allow intended changes to pass |
| 68 | /// tests. |
| 69 | /// |
| 70 | /// * The [FlutterLocalFileComparator] is used for any other tests run outside |
| 71 | /// of the above conditions. Similar to the |
| 72 | /// [FlutterPreSubmitFileComparator], this comparator will use the |
| 73 | /// [SkiaGoldClient] to request baseline images from |
| 74 | /// [Flutter Gold](https://flutter-gold.skia.org) and compares for the |
| 75 | /// current test image. If a difference is detected, this comparator will |
| 76 | /// generate failure output illustrating the found difference. If a baseline |
| 77 | /// is not found for a given test image, it will consider it a new test and |
| 78 | /// output the new image for verification. |
| 79 | /// The [FlutterSkippingGoldenFileComparator] is utilized to skip tests outside |
| 80 | /// of the appropriate environments. Currently, tests executing in post-submit |
| 81 | /// on the LUCI build environment are skipped, as post-submit checks are done |
Kate Lovett | b1ca7f4 | 2019-11-15 13:04:59 -0800 | [diff] [blame] | 82 | /// on Cirrus. This comparator is also used when an internet connection is |
| 83 | /// unavailable. |
Kate Lovett | 616794f | 2019-07-28 12:26:06 -0700 | [diff] [blame] | 84 | abstract class FlutterGoldenFileComparator extends GoldenFileComparator { |
| 85 | /// Creates a [FlutterGoldenFileComparator] that will resolve golden file |
Kate Lovett | 7bc0203 | 2019-10-25 15:05:21 -0700 | [diff] [blame] | 86 | /// URIs relative to the specified [basedir], and retrieve golden baselines |
| 87 | /// using the [skiaClient]. The [basedir] is used for writing and accessing |
| 88 | /// information and files for interacting with the [skiaClient]. When testing |
| 89 | /// locally, the [basedir] will also contain any diffs from failed tests, or |
| 90 | /// goldens generated from newly introduced tests. |
Kate Lovett | 616794f | 2019-07-28 12:26:06 -0700 | [diff] [blame] | 91 | /// |
Kate Lovett | 7bc0203 | 2019-10-25 15:05:21 -0700 | [diff] [blame] | 92 | /// The [fs] and [platform] parameters are useful in tests, where the default |
| 93 | /// file system and platform can be replaced by mock instances. |
Kate Lovett | 616794f | 2019-07-28 12:26:06 -0700 | [diff] [blame] | 94 | @visibleForTesting |
| 95 | FlutterGoldenFileComparator( |
Kate Lovett | 7bc0203 | 2019-10-25 15:05:21 -0700 | [diff] [blame] | 96 | this.basedir, |
| 97 | this.skiaClient, { |
Kate Lovett | 616794f | 2019-07-28 12:26:06 -0700 | [diff] [blame] | 98 | this.fs = const LocalFileSystem(), |
| 99 | this.platform = const LocalPlatform(), |
| 100 | }) : assert(basedir != null), |
Kate Lovett | 7bc0203 | 2019-10-25 15:05:21 -0700 | [diff] [blame] | 101 | assert(skiaClient != null), |
Kate Lovett | 616794f | 2019-07-28 12:26:06 -0700 | [diff] [blame] | 102 | assert(fs != null), |
| 103 | assert(platform != null); |
| 104 | |
| 105 | /// The directory to which golden file URIs will be resolved in [compare] and |
Kate Lovett | 7bc0203 | 2019-10-25 15:05:21 -0700 | [diff] [blame] | 106 | /// [update], cannot be null. |
Kate Lovett | 616794f | 2019-07-28 12:26:06 -0700 | [diff] [blame] | 107 | final Uri basedir; |
| 108 | |
Kate Lovett | 7bc0203 | 2019-10-25 15:05:21 -0700 | [diff] [blame] | 109 | /// A client for uploading image tests and making baseline requests to the |
| 110 | /// Flutter Gold Dashboard, cannot be null. |
| 111 | final SkiaGoldClient skiaClient; |
| 112 | |
Kate Lovett | 616794f | 2019-07-28 12:26:06 -0700 | [diff] [blame] | 113 | /// The file system used to perform file access. |
| 114 | @visibleForTesting |
| 115 | final FileSystem fs; |
| 116 | |
| 117 | /// A wrapper for the [dart:io.Platform] API. |
| 118 | @visibleForTesting |
| 119 | final Platform platform; |
| 120 | |
| 121 | @override |
| 122 | Future<void> update(Uri golden, Uint8List imageBytes) async { |
| 123 | final File goldenFile = getGoldenFile(golden); |
| 124 | await goldenFile.parent.create(recursive: true); |
| 125 | await goldenFile.writeAsBytes(imageBytes, flush: true); |
| 126 | } |
| 127 | |
Kate Lovett | 7bc0203 | 2019-10-25 15:05:21 -0700 | [diff] [blame] | 128 | @override |
| 129 | Uri getTestUri(Uri key, int version) => key; |
| 130 | |
Kate Lovett | 616794f | 2019-07-28 12:26:06 -0700 | [diff] [blame] | 131 | /// Calculate the appropriate basedir for the current test context. |
Kate Lovett | 9011cec | 2019-12-04 15:43:36 -0800 | [diff] [blame] | 132 | /// |
| 133 | /// The optional [suffix] argument is used by the |
| 134 | /// [FlutterSkiaGoldFileComparator] and the [FlutterPreSubmitFileComparator]. |
| 135 | /// These [FlutterGoldenFileComparators] randomize their base directories to |
| 136 | /// maintain thread safety while using the `goldctl` tool. |
Kate Lovett | 616794f | 2019-07-28 12:26:06 -0700 | [diff] [blame] | 137 | @protected |
| 138 | @visibleForTesting |
Kate Lovett | 1c15cd8 | 2020-02-05 11:03:02 -0800 | [diff] [blame^] | 139 | static Directory getBaseDirectory( |
| 140 | LocalFileComparator defaultComparator, |
| 141 | Platform platform, { |
| 142 | String suffix = '', |
| 143 | bool local = false, |
| 144 | }) { |
Kate Lovett | 7bc0203 | 2019-10-25 15:05:21 -0700 | [diff] [blame] | 145 | const FileSystem fs = LocalFileSystem(); |
| 146 | final Directory flutterRoot = fs.directory(platform.environment[_kFlutterRootKey]); |
Kate Lovett | 1c15cd8 | 2020-02-05 11:03:02 -0800 | [diff] [blame^] | 147 | Directory comparisonRoot; |
| 148 | |
| 149 | if (!local) { |
| 150 | comparisonRoot = fs.systemTempDirectory.childDirectory( |
| 151 | 'skia_goldens$suffix' |
| 152 | ); |
| 153 | } else { |
| 154 | comparisonRoot = flutterRoot.childDirectory( |
| 155 | fs.path.join( |
| 156 | 'bin', |
| 157 | 'cache', |
| 158 | 'pkg', |
| 159 | 'skia_goldens', |
| 160 | ) |
| 161 | ); |
| 162 | } |
| 163 | |
Kate Lovett | 616794f | 2019-07-28 12:26:06 -0700 | [diff] [blame] | 164 | final Directory testDirectory = fs.directory(defaultComparator.basedir); |
Kate Lovett | 7bc0203 | 2019-10-25 15:05:21 -0700 | [diff] [blame] | 165 | final String testDirectoryRelativePath = fs.path.relative( |
| 166 | testDirectory.path, |
| 167 | from: flutterRoot.path, |
| 168 | ); |
| 169 | return comparisonRoot.childDirectory(testDirectoryRelativePath); |
Kate Lovett | 616794f | 2019-07-28 12:26:06 -0700 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | /// Returns the golden [File] identified by the given [Uri]. |
| 173 | @protected |
| 174 | File getGoldenFile(Uri uri) { |
Kate Lovett | 616794f | 2019-07-28 12:26:06 -0700 | [diff] [blame] | 175 | final File goldenFile = fs.directory(basedir).childFile(fs.file(uri).path); |
Kate Lovett | 616794f | 2019-07-28 12:26:06 -0700 | [diff] [blame] | 176 | return goldenFile; |
| 177 | } |
Kate Lovett | 616794f | 2019-07-28 12:26:06 -0700 | [diff] [blame] | 178 | |
Greg Spencer | a60bf8e | 2019-11-22 08:43:55 -0800 | [diff] [blame] | 179 | /// Prepends the golden URL with the library name that encloses the current |
Kate Lovett | 7bc0203 | 2019-10-25 15:05:21 -0700 | [diff] [blame] | 180 | /// test. |
| 181 | Uri _addPrefix(Uri golden) { |
| 182 | final String prefix = basedir.pathSegments[basedir.pathSegments.length - 2]; |
| 183 | return Uri.parse(prefix + '.' + golden.toString()); |
Todd Volkert | 65079ad | 2018-05-03 07:39:41 -0700 | [diff] [blame] | 184 | } |
Kate Lovett | 616794f | 2019-07-28 12:26:06 -0700 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | /// A [FlutterGoldenFileComparator] for testing golden images with Skia Gold. |
| 188 | /// |
| 189 | /// For testing across all platforms, the [SkiaGoldClient] is used to upload |
| 190 | /// images for framework-related golden tests and process results. Currently |
| 191 | /// these tests are designed to be run post-submit on Cirrus CI, informed by the |
| 192 | /// environment. |
| 193 | /// |
| 194 | /// See also: |
| 195 | /// |
| 196 | /// * [GoldenFileComparator], the abstract class that |
| 197 | /// [FlutterGoldenFileComparator] implements. |
Kate Lovett | 7bc0203 | 2019-10-25 15:05:21 -0700 | [diff] [blame] | 198 | /// * [FlutterPreSubmitFileComparator], another |
| 199 | /// [FlutterGoldenFileComparator] that tests golden images before changes are |
| 200 | /// merged into the master branch. |
| 201 | /// * [FlutterLocalFileComparator], another |
| 202 | /// [FlutterGoldenFileComparator] that tests golden images locally on your |
| 203 | /// current machine. |
Kate Lovett | 616794f | 2019-07-28 12:26:06 -0700 | [diff] [blame] | 204 | class FlutterSkiaGoldFileComparator extends FlutterGoldenFileComparator { |
| 205 | /// Creates a [FlutterSkiaGoldFileComparator] that will test golden file |
| 206 | /// images against Skia Gold. |
| 207 | /// |
Kate Lovett | 7bc0203 | 2019-10-25 15:05:21 -0700 | [diff] [blame] | 208 | /// The [fs] and [platform] parameters are useful in tests, where the default |
| 209 | /// file system and platform can be replaced by mock instances. |
Kate Lovett | 616794f | 2019-07-28 12:26:06 -0700 | [diff] [blame] | 210 | FlutterSkiaGoldFileComparator( |
| 211 | final Uri basedir, |
Kate Lovett | 7bc0203 | 2019-10-25 15:05:21 -0700 | [diff] [blame] | 212 | final SkiaGoldClient skiaClient, { |
| 213 | final FileSystem fs = const LocalFileSystem(), |
| 214 | final Platform platform = const LocalPlatform(), |
Kate Lovett | 616794f | 2019-07-28 12:26:06 -0700 | [diff] [blame] | 215 | }) : super( |
| 216 | basedir, |
Kate Lovett | 7bc0203 | 2019-10-25 15:05:21 -0700 | [diff] [blame] | 217 | skiaClient, |
Kate Lovett | 616794f | 2019-07-28 12:26:06 -0700 | [diff] [blame] | 218 | fs: fs, |
| 219 | platform: platform, |
| 220 | ); |
| 221 | |
Kate Lovett | 616794f | 2019-07-28 12:26:06 -0700 | [diff] [blame] | 222 | /// Creates a new [FlutterSkiaGoldFileComparator] that mirrors the relative |
| 223 | /// path resolution of the default [goldenFileComparator]. |
| 224 | /// |
| 225 | /// The [goldens] and [defaultComparator] parameters are visible for testing |
| 226 | /// purposes only. |
Kate Lovett | 7bc0203 | 2019-10-25 15:05:21 -0700 | [diff] [blame] | 227 | static Future<FlutterSkiaGoldFileComparator> fromDefaultComparator( |
| 228 | final Platform platform, { |
Kate Lovett | 616794f | 2019-07-28 12:26:06 -0700 | [diff] [blame] | 229 | SkiaGoldClient goldens, |
| 230 | LocalFileComparator defaultComparator, |
| 231 | }) async { |
Kate Lovett | 8df0d65 | 2019-10-21 16:45:56 -0700 | [diff] [blame] | 232 | |
Alexandre Ardhuin | 1f3ff5c | 2019-11-21 17:46:37 +0100 | [diff] [blame] | 233 | defaultComparator ??= goldenFileComparator as LocalFileComparator; |
Kate Lovett | 7bc0203 | 2019-10-25 15:05:21 -0700 | [diff] [blame] | 234 | final Directory baseDirectory = FlutterGoldenFileComparator.getBaseDirectory( |
| 235 | defaultComparator, |
| 236 | platform, |
Kate Lovett | 1374a41 | 2019-11-27 11:29:53 -0800 | [diff] [blame] | 237 | suffix: '${math.Random().nextInt(10000)}', |
Kate Lovett | 7bc0203 | 2019-10-25 15:05:21 -0700 | [diff] [blame] | 238 | ); |
Kate Lovett | 9011cec | 2019-12-04 15:43:36 -0800 | [diff] [blame] | 239 | baseDirectory.createSync(recursive: true); |
Kate Lovett | 7bc0203 | 2019-10-25 15:05:21 -0700 | [diff] [blame] | 240 | |
| 241 | goldens ??= SkiaGoldClient(baseDirectory); |
| 242 | await goldens.auth(); |
Kate Lovett | 616794f | 2019-07-28 12:26:06 -0700 | [diff] [blame] | 243 | await goldens.imgtestInit(); |
| 244 | return FlutterSkiaGoldFileComparator(baseDirectory.uri, goldens); |
Todd Volkert | 65079ad | 2018-05-03 07:39:41 -0700 | [diff] [blame] | 245 | } |
Kate Lovett | eb0b179 | 2019-07-12 12:23:04 -0700 | [diff] [blame] | 246 | |
Kate Lovett | 616794f | 2019-07-28 12:26:06 -0700 | [diff] [blame] | 247 | @override |
| 248 | Future<bool> compare(Uint8List imageBytes, Uri golden) async { |
| 249 | golden = _addPrefix(golden); |
| 250 | await update(golden, imageBytes); |
Kate Lovett | 3a3939a | 2019-10-21 17:31:54 -0700 | [diff] [blame] | 251 | final File goldenFile = getGoldenFile(golden); |
Kate Lovett | 616794f | 2019-07-28 12:26:06 -0700 | [diff] [blame] | 252 | |
Kate Lovett | 7bc0203 | 2019-10-25 15:05:21 -0700 | [diff] [blame] | 253 | return skiaClient.imgtestAdd(golden.path, goldenFile); |
| 254 | } |
Kate Lovett | 3a3939a | 2019-10-21 17:31:54 -0700 | [diff] [blame] | 255 | |
Kate Lovett | 616794f | 2019-07-28 12:26:06 -0700 | [diff] [blame] | 256 | /// Decides based on the current environment whether goldens tests should be |
| 257 | /// performed against Skia Gold. |
Kate Lovett | 7bc0203 | 2019-10-25 15:05:21 -0700 | [diff] [blame] | 258 | static bool isAvailableForEnvironment(Platform platform) { |
Kate Lovett | 616794f | 2019-07-28 12:26:06 -0700 | [diff] [blame] | 259 | final String cirrusPR = platform.environment['CIRRUS_PR'] ?? ''; |
| 260 | final String cirrusBranch = platform.environment['CIRRUS_BRANCH'] ?? ''; |
Kate Lovett | 26d09f1 | 2019-10-31 11:44:07 -0700 | [diff] [blame] | 261 | return platform.environment.containsKey('CIRRUS_CI') |
Kate Lovett | 616794f | 2019-07-28 12:26:06 -0700 | [diff] [blame] | 262 | && cirrusPR.isEmpty |
| 263 | && cirrusBranch == 'master' |
Kate Lovett | 26d09f1 | 2019-10-31 11:44:07 -0700 | [diff] [blame] | 264 | && platform.environment.containsKey('GOLD_SERVICE_ACCOUNT'); |
Kate Lovett | 616794f | 2019-07-28 12:26:06 -0700 | [diff] [blame] | 265 | } |
Kate Lovett | 7bc0203 | 2019-10-25 15:05:21 -0700 | [diff] [blame] | 266 | } |
Kate Lovett | 3a3939a | 2019-10-21 17:31:54 -0700 | [diff] [blame] | 267 | |
Kate Lovett | 7bc0203 | 2019-10-25 15:05:21 -0700 | [diff] [blame] | 268 | /// A [FlutterGoldenFileComparator] for testing golden images before changes are |
| 269 | /// merged into the master branch. |
| 270 | /// |
| 271 | /// This comparator utilizes the [SkiaGoldClient] to request baseline images for |
| 272 | /// the given device under test for comparison. This comparator is only |
| 273 | /// initialized during pre-submit testing on Cirrus CI. |
| 274 | /// |
| 275 | /// See also: |
| 276 | /// |
| 277 | /// * [GoldenFileComparator], the abstract class that |
| 278 | /// [FlutterGoldenFileComparator] implements. |
| 279 | /// * [FlutterSkiaGoldFileComparator], another |
| 280 | /// [FlutterGoldenFileComparator] that uploads tests to the Skia Gold |
| 281 | /// dashboard. |
| 282 | /// * [FlutterLocalFileComparator], another |
| 283 | /// [FlutterGoldenFileComparator] that tests golden images locally on your |
| 284 | /// current machine. |
| 285 | class FlutterPreSubmitFileComparator extends FlutterGoldenFileComparator { |
| 286 | /// Creates a [FlutterPreSubmitFileComparator] that will test golden file |
| 287 | /// images against baselines requested from Flutter Gold. |
| 288 | /// |
| 289 | /// The [fs] and [platform] parameters are useful in tests, where the default |
| 290 | /// file system and platform can be replaced by mock instances. |
| 291 | FlutterPreSubmitFileComparator( |
| 292 | final Uri basedir, |
| 293 | final SkiaGoldClient skiaClient, { |
| 294 | final FileSystem fs = const LocalFileSystem(), |
| 295 | final Platform platform = const LocalPlatform(), |
| 296 | }) : super( |
| 297 | basedir, |
| 298 | skiaClient, |
| 299 | fs: fs, |
| 300 | platform: platform, |
| 301 | ); |
| 302 | |
| 303 | /// Creates a new [FlutterPreSubmitFileComparator] that mirrors the |
| 304 | /// relative path resolution of the default [goldenFileComparator]. |
| 305 | /// |
| 306 | /// The [goldens] and [defaultComparator] parameters are visible for testing |
| 307 | /// purposes only. |
| 308 | static Future<FlutterGoldenFileComparator> fromDefaultComparator( |
| 309 | final Platform platform, { |
| 310 | SkiaGoldClient goldens, |
| 311 | LocalFileComparator defaultComparator, |
Kate Lovett | 84aa29c | 2020-01-09 08:08:03 -0800 | [diff] [blame] | 312 | final Directory testBasedir, |
Kate Lovett | 7bc0203 | 2019-10-25 15:05:21 -0700 | [diff] [blame] | 313 | }) async { |
| 314 | |
Alexandre Ardhuin | 1f3ff5c | 2019-11-21 17:46:37 +0100 | [diff] [blame] | 315 | defaultComparator ??= goldenFileComparator as LocalFileComparator; |
Kate Lovett | 84aa29c | 2020-01-09 08:08:03 -0800 | [diff] [blame] | 316 | final Directory baseDirectory = testBasedir ?? FlutterGoldenFileComparator.getBaseDirectory( |
Kate Lovett | 7bc0203 | 2019-10-25 15:05:21 -0700 | [diff] [blame] | 317 | defaultComparator, |
| 318 | platform, |
Kate Lovett | 9011cec | 2019-12-04 15:43:36 -0800 | [diff] [blame] | 319 | suffix: '${math.Random().nextInt(10000)}', |
Kate Lovett | 7bc0203 | 2019-10-25 15:05:21 -0700 | [diff] [blame] | 320 | ); |
Kate Lovett | 84aa29c | 2020-01-09 08:08:03 -0800 | [diff] [blame] | 321 | |
| 322 | if (!baseDirectory.existsSync()) |
| 323 | baseDirectory.createSync(recursive: true); |
Kate Lovett | 7bc0203 | 2019-10-25 15:05:21 -0700 | [diff] [blame] | 324 | |
| 325 | goldens ??= SkiaGoldClient(baseDirectory); |
Kate Lovett | 84aa29c | 2020-01-09 08:08:03 -0800 | [diff] [blame] | 326 | |
| 327 | final bool hasWritePermission = !platform.environment['GOLD_SERVICE_ACCOUNT'].startsWith('ENCRYPTED'); |
| 328 | if (hasWritePermission) { |
| 329 | await goldens.auth(); |
| 330 | await goldens.tryjobInit(); |
| 331 | return _AuthorizedFlutterPreSubmitComparator( |
| 332 | baseDirectory.uri, |
| 333 | goldens, |
| 334 | platform: platform, |
| 335 | ); |
| 336 | } |
| 337 | |
| 338 | goldens.emptyAuth(); |
| 339 | return _UnauthorizedFlutterPreSubmitComparator( |
| 340 | baseDirectory.uri, |
| 341 | goldens, |
| 342 | platform: platform, |
| 343 | ); |
Kate Lovett | 7bc0203 | 2019-10-25 15:05:21 -0700 | [diff] [blame] | 344 | } |
| 345 | |
| 346 | @override |
| 347 | Future<bool> compare(Uint8List imageBytes, Uri golden) async { |
Kate Lovett | 84aa29c | 2020-01-09 08:08:03 -0800 | [diff] [blame] | 348 | assert( |
| 349 | false, |
| 350 | 'The FlutterPreSubmitFileComparator has been used to execute a golden ' |
| 351 | 'file test; this should never happen. Presubmit golden file testing ' |
| 352 | 'should be executed by either the _AuthorizedFlutterPreSubmitComparator ' |
| 353 | 'or the _UnauthorizedFlutterPreSubmitComparator based on contributor ' |
| 354 | 'permissions.' |
| 355 | ); |
| 356 | return false; |
| 357 | } |
| 358 | |
| 359 | /// Decides based on the current environment whether goldens tests should be |
| 360 | /// performed as pre-submit tests with Skia Gold. |
| 361 | static bool isAvailableForEnvironment(Platform platform) { |
| 362 | final String cirrusPR = platform.environment['CIRRUS_PR'] ?? ''; |
| 363 | return platform.environment.containsKey('CIRRUS_CI') |
| 364 | && cirrusPR.isNotEmpty |
| 365 | && platform.environment.containsKey('GOLD_SERVICE_ACCOUNT'); |
| 366 | } |
| 367 | } |
| 368 | |
| 369 | class _AuthorizedFlutterPreSubmitComparator extends FlutterPreSubmitFileComparator { |
| 370 | _AuthorizedFlutterPreSubmitComparator( |
| 371 | final Uri basedir, |
| 372 | final SkiaGoldClient skiaClient, { |
| 373 | final FileSystem fs = const LocalFileSystem(), |
| 374 | final Platform platform = const LocalPlatform(), |
| 375 | }) : super( |
| 376 | basedir, |
| 377 | skiaClient, |
| 378 | fs: fs, |
| 379 | platform: platform, |
| 380 | ); |
| 381 | |
| 382 | @override |
| 383 | Future<bool> compare(Uint8List imageBytes, Uri golden) async { |
Kate Lovett | 7bc0203 | 2019-10-25 15:05:21 -0700 | [diff] [blame] | 384 | golden = _addPrefix(golden); |
Kate Lovett | 9011cec | 2019-12-04 15:43:36 -0800 | [diff] [blame] | 385 | await update(golden, imageBytes); |
| 386 | final File goldenFile = getGoldenFile(golden); |
Kate Lovett | 7bc0203 | 2019-10-25 15:05:21 -0700 | [diff] [blame] | 387 | |
Kate Lovett | 9011cec | 2019-12-04 15:43:36 -0800 | [diff] [blame] | 388 | return skiaClient.tryjobAdd(golden.path, goldenFile); |
Kate Lovett | 7bc0203 | 2019-10-25 15:05:21 -0700 | [diff] [blame] | 389 | } |
Kate Lovett | 84aa29c | 2020-01-09 08:08:03 -0800 | [diff] [blame] | 390 | } |
Kate Lovett | 7bc0203 | 2019-10-25 15:05:21 -0700 | [diff] [blame] | 391 | |
Kate Lovett | 84aa29c | 2020-01-09 08:08:03 -0800 | [diff] [blame] | 392 | class _UnauthorizedFlutterPreSubmitComparator extends FlutterPreSubmitFileComparator { |
| 393 | _UnauthorizedFlutterPreSubmitComparator( |
| 394 | final Uri basedir, |
| 395 | final SkiaGoldClient skiaClient, { |
| 396 | final FileSystem fs = const LocalFileSystem(), |
| 397 | final Platform platform = const LocalPlatform(), |
| 398 | }) : super( |
| 399 | basedir, |
| 400 | skiaClient, |
| 401 | fs: fs, |
| 402 | platform: platform, |
| 403 | ); |
| 404 | |
| 405 | @override |
| 406 | Future<bool> compare(Uint8List imageBytes, Uri golden) async { |
| 407 | golden = _addPrefix(golden); |
| 408 | await update(golden, imageBytes); |
| 409 | final File goldenFile = getGoldenFile(golden); |
| 410 | |
| 411 | // Check for match to existing baseline. |
| 412 | if (await skiaClient.imgtestCheck(golden.path, goldenFile)) |
| 413 | return true; |
| 414 | |
| 415 | // We do not have a matching image, so we need to check a few things |
| 416 | // manually. We wait until this point to do this work so request traffic |
| 417 | // low. |
| 418 | skiaClient.getExpectations(); |
| 419 | final String testName = skiaClient.cleanTestName(golden.path); |
| 420 | final List<String> testExpectations = skiaClient.expectations[testName]; |
| 421 | if (testExpectations == null) { |
| 422 | // This is a new test. |
| 423 | print('No expectations provided by Skia Gold for test: $golden. ' |
| 424 | 'This may be a new test. If this is an unexpected result, check ' |
| 425 | 'https://flutter-gold.skia.org.\n' |
| 426 | ); |
| 427 | return true; |
| 428 | } |
| 429 | |
| 430 | // Contributors without the proper permissions to execute a tryjob can make |
| 431 | // a golden file change through Gold's ignore feature instead. |
| 432 | final bool ignoreResult = await skiaClient.testIsIgnoredForPullRequest( |
| 433 | platform.environment['CIRRUS_PR'] ?? '', |
| 434 | golden.path, |
| 435 | ); |
| 436 | // If true, this is an intended change. |
| 437 | return ignoreResult; |
Kate Lovett | 26d09f1 | 2019-10-31 11:44:07 -0700 | [diff] [blame] | 438 | } |
| 439 | } |
| 440 | |
| 441 | /// A [FlutterGoldenFileComparator] for controlling post-submit testing |
| 442 | /// conditions that do not execute golden file tests. |
| 443 | /// |
| 444 | /// Currently, this comparator is used in post-submit checks on LUCI and with |
Kate Lovett | b1ca7f4 | 2019-11-15 13:04:59 -0800 | [diff] [blame] | 445 | /// some Cirrus shards that do not run framework tests. This comparator is also |
| 446 | /// used when an internet connection is not available for contacting Gold. |
Kate Lovett | 26d09f1 | 2019-10-31 11:44:07 -0700 | [diff] [blame] | 447 | /// |
| 448 | /// See also: |
| 449 | /// |
| 450 | /// * [FlutterGoldensRepositoryFileComparator], another |
| 451 | /// [FlutterGoldenFileComparator] that tests golden images using the |
| 452 | /// flutter/goldens repository. |
| 453 | /// * [FlutterSkiaGoldFileComparator], another [FlutterGoldenFileComparator] |
| 454 | /// that tests golden images through Skia Gold. |
| 455 | /// * [FlutterPreSubmitFileComparator], another |
| 456 | /// [FlutterGoldenFileComparator] that tests golden images before changes are |
| 457 | /// merged into the master branch. |
| 458 | /// * [FlutterLocalFileComparator], another |
| 459 | /// [FlutterGoldenFileComparator] that tests golden images locally on your |
| 460 | /// current machine. |
| 461 | class FlutterSkippingGoldenFileComparator extends FlutterGoldenFileComparator { |
| 462 | /// Creates a [FlutterSkippingGoldenFileComparator] that will skip tests that |
| 463 | /// are not in the right environment for golden file testing. |
| 464 | FlutterSkippingGoldenFileComparator( |
| 465 | final Uri basedir, |
| 466 | final SkiaGoldClient skiaClient, |
Kate Lovett | b1ca7f4 | 2019-11-15 13:04:59 -0800 | [diff] [blame] | 467 | this.reason, |
| 468 | ) : assert(reason != null), |
| 469 | super(basedir, skiaClient); |
| 470 | |
| 471 | /// Describes the reason for using the [FlutterSkippingGoldenFileComparator]. |
| 472 | /// |
| 473 | /// Cannot be null. |
| 474 | final String reason; |
Kate Lovett | 26d09f1 | 2019-10-31 11:44:07 -0700 | [diff] [blame] | 475 | |
| 476 | /// Creates a new [FlutterSkippingGoldenFileComparator] that mirrors the |
| 477 | /// relative path resolution of the default [goldenFileComparator]. |
Kate Lovett | b1ca7f4 | 2019-11-15 13:04:59 -0800 | [diff] [blame] | 478 | static FlutterSkippingGoldenFileComparator fromDefaultComparator( |
| 479 | String reason, { |
Kate Lovett | 26d09f1 | 2019-10-31 11:44:07 -0700 | [diff] [blame] | 480 | LocalFileComparator defaultComparator, |
| 481 | }) { |
Alexandre Ardhuin | 1f3ff5c | 2019-11-21 17:46:37 +0100 | [diff] [blame] | 482 | defaultComparator ??= goldenFileComparator as LocalFileComparator; |
Kate Lovett | 26d09f1 | 2019-10-31 11:44:07 -0700 | [diff] [blame] | 483 | const FileSystem fs = LocalFileSystem(); |
| 484 | final Uri basedir = defaultComparator.basedir; |
| 485 | final SkiaGoldClient skiaClient = SkiaGoldClient(fs.directory(basedir)); |
Kate Lovett | b1ca7f4 | 2019-11-15 13:04:59 -0800 | [diff] [blame] | 486 | return FlutterSkippingGoldenFileComparator(basedir, skiaClient, reason); |
Kate Lovett | 26d09f1 | 2019-10-31 11:44:07 -0700 | [diff] [blame] | 487 | } |
| 488 | |
| 489 | @override |
| 490 | Future<bool> compare(Uint8List imageBytes, Uri golden) async { |
| 491 | print( |
Kate Lovett | b1ca7f4 | 2019-11-15 13:04:59 -0800 | [diff] [blame] | 492 | 'Skipping "$golden" test : $reason' |
Kate Lovett | 26d09f1 | 2019-10-31 11:44:07 -0700 | [diff] [blame] | 493 | ); |
| 494 | return true; |
| 495 | } |
| 496 | |
| 497 | @override |
| 498 | Future<void> update(Uri golden, Uint8List imageBytes) => null; |
| 499 | |
| 500 | /// Decides based on the current environment whether this comparator should be |
| 501 | /// used. |
| 502 | static bool isAvailableForEnvironment(Platform platform) { |
Kate Lovett | 84aa29c | 2020-01-09 08:08:03 -0800 | [diff] [blame] | 503 | return platform.environment.containsKey('SWARMING_TASK_ID') |
| 504 | || platform.environment.containsKey('CIRRUS_CI'); |
Kate Lovett | 3a3939a | 2019-10-21 17:31:54 -0700 | [diff] [blame] | 505 | } |
Kate Lovett | 616794f | 2019-07-28 12:26:06 -0700 | [diff] [blame] | 506 | } |
| 507 | |
Kate Lovett | 7bc0203 | 2019-10-25 15:05:21 -0700 | [diff] [blame] | 508 | /// A [FlutterGoldenFileComparator] for testing golden images locally on your |
| 509 | /// current machine. |
| 510 | /// |
| 511 | /// This comparator utilizes the [SkiaGoldClient] to request baseline images for |
Kate Lovett | 26d09f1 | 2019-10-31 11:44:07 -0700 | [diff] [blame] | 512 | /// the given device under test for comparison. This comparator is initialized |
| 513 | /// when conditions for all other [FlutterGoldenFileComparators] have not been |
| 514 | /// met, see the `isAvailableForEnvironment` method for each one listed below. |
| 515 | /// |
| 516 | /// The [FlutterLocalFileComparator] is intended to run on local machines and |
| 517 | /// serve as a smoke test during development. As such, it will not be able to |
| 518 | /// detect unintended changes on environments other than the currently executing |
| 519 | /// machine, until they are tested using the [FlutterPreSubmitFileComparator]. |
Kate Lovett | 7bc0203 | 2019-10-25 15:05:21 -0700 | [diff] [blame] | 520 | /// |
| 521 | /// See also: |
| 522 | /// |
| 523 | /// * [GoldenFileComparator], the abstract class that |
| 524 | /// [FlutterGoldenFileComparator] implements. |
| 525 | /// * [FlutterSkiaGoldFileComparator], another |
| 526 | /// [FlutterGoldenFileComparator] that uploads tests to the Skia Gold |
| 527 | /// dashboard. |
| 528 | /// * [FlutterPreSubmitFileComparator], another |
| 529 | /// [FlutterGoldenFileComparator] that tests golden images before changes are |
| 530 | /// merged into the master branch. |
Kate Lovett | 26d09f1 | 2019-10-31 11:44:07 -0700 | [diff] [blame] | 531 | /// * [FlutterSkippingGoldenFileComparator], another |
| 532 | /// [FlutterGoldenFileComparator] that controls post-submit testing |
| 533 | /// conditions that do not execute golden file tests. |
Kate Lovett | 7bc0203 | 2019-10-25 15:05:21 -0700 | [diff] [blame] | 534 | class FlutterLocalFileComparator extends FlutterGoldenFileComparator with LocalComparisonOutput { |
| 535 | /// Creates a [FlutterLocalFileComparator] that will test golden file |
| 536 | /// images against baselines requested from Flutter Gold. |
| 537 | /// |
| 538 | /// The [fs] and [platform] parameters are useful in tests, where the default |
| 539 | /// file system and platform can be replaced by mock instances. |
| 540 | FlutterLocalFileComparator( |
| 541 | final Uri basedir, |
| 542 | final SkiaGoldClient skiaClient, { |
| 543 | final FileSystem fs = const LocalFileSystem(), |
| 544 | final Platform platform = const LocalPlatform(), |
| 545 | }) : super( |
| 546 | basedir, |
| 547 | skiaClient, |
| 548 | fs: fs, |
| 549 | platform: platform, |
| 550 | ); |
| 551 | |
| 552 | /// Creates a new [FlutterLocalFileComparator] that mirrors the |
| 553 | /// relative path resolution of the default [goldenFileComparator]. |
| 554 | /// |
Kate Lovett | b1ca7f4 | 2019-11-15 13:04:59 -0800 | [diff] [blame] | 555 | /// The [goldens], [defaultComparator], and [baseDirectory] parameters are |
| 556 | /// visible for testing purposes only. |
Kate Lovett | 7bc0203 | 2019-10-25 15:05:21 -0700 | [diff] [blame] | 557 | static Future<FlutterGoldenFileComparator> fromDefaultComparator( |
| 558 | final Platform platform, { |
| 559 | SkiaGoldClient goldens, |
| 560 | LocalFileComparator defaultComparator, |
Kate Lovett | b1ca7f4 | 2019-11-15 13:04:59 -0800 | [diff] [blame] | 561 | Directory baseDirectory, |
Kate Lovett | 7bc0203 | 2019-10-25 15:05:21 -0700 | [diff] [blame] | 562 | }) async { |
Alexandre Ardhuin | 1f3ff5c | 2019-11-21 17:46:37 +0100 | [diff] [blame] | 563 | defaultComparator ??= goldenFileComparator as LocalFileComparator; |
Kate Lovett | b1ca7f4 | 2019-11-15 13:04:59 -0800 | [diff] [blame] | 564 | baseDirectory ??= FlutterGoldenFileComparator.getBaseDirectory( |
Kate Lovett | 7bc0203 | 2019-10-25 15:05:21 -0700 | [diff] [blame] | 565 | defaultComparator, |
| 566 | platform, |
Kate Lovett | 1c15cd8 | 2020-02-05 11:03:02 -0800 | [diff] [blame^] | 567 | local: true, |
Kate Lovett | 7bc0203 | 2019-10-25 15:05:21 -0700 | [diff] [blame] | 568 | ); |
| 569 | |
| 570 | if(!baseDirectory.existsSync()) { |
| 571 | baseDirectory.createSync(recursive: true); |
| 572 | } |
| 573 | |
| 574 | goldens ??= SkiaGoldClient(baseDirectory); |
Kate Lovett | b1ca7f4 | 2019-11-15 13:04:59 -0800 | [diff] [blame] | 575 | |
| 576 | try { |
| 577 | await goldens.getExpectations(); |
| 578 | } on io.OSError catch (_) { |
| 579 | return FlutterSkippingGoldenFileComparator( |
| 580 | baseDirectory.uri, |
| 581 | goldens, |
Kate Lovett | 6397c02 | 2020-01-10 14:43:01 -0800 | [diff] [blame] | 582 | 'OSError occurred, could not reach Gold. ' |
| 583 | 'Switching to FlutterSkippingGoldenFileComparator.', |
| 584 | ); |
| 585 | } on io.SocketException catch (_) { |
| 586 | return FlutterSkippingGoldenFileComparator( |
| 587 | baseDirectory.uri, |
| 588 | goldens, |
| 589 | 'SocketException occurred, could not reach Gold. ' |
| 590 | 'Switching to FlutterSkippingGoldenFileComparator.', |
Kate Lovett | b1ca7f4 | 2019-11-15 13:04:59 -0800 | [diff] [blame] | 591 | ); |
| 592 | } |
Kate Lovett | 7bc0203 | 2019-10-25 15:05:21 -0700 | [diff] [blame] | 593 | |
| 594 | return FlutterLocalFileComparator(baseDirectory.uri, goldens); |
| 595 | } |
| 596 | |
| 597 | @override |
| 598 | Future<bool> compare(Uint8List imageBytes, Uri golden) async { |
| 599 | golden = _addPrefix(golden); |
| 600 | final String testName = skiaClient.cleanTestName(golden.path); |
| 601 | final List<String> testExpectations = skiaClient.expectations[testName]; |
| 602 | if (testExpectations == null) { |
| 603 | // There is no baseline for this test |
| 604 | print('No expectations provided by Skia Gold for test: $golden. ' |
| 605 | 'This may be a new test. If this is an unexpected result, check ' |
| 606 | 'https://flutter-gold.skia.org.\n' |
| 607 | 'Validate image output found at $basedir' |
| 608 | ); |
| 609 | update(golden, imageBytes); |
| 610 | return true; |
| 611 | } |
| 612 | |
| 613 | ComparisonResult result; |
| 614 | final Map<String, ComparisonResult> failureDiffs = <String, ComparisonResult>{}; |
Alexandre Ardhuin | 4f9b6cf | 2020-01-07 16:32:04 +0100 | [diff] [blame] | 615 | for (final String expectation in testExpectations) { |
Kate Lovett | 7bc0203 | 2019-10-25 15:05:21 -0700 | [diff] [blame] | 616 | final List<int> goldenBytes = await skiaClient.getImageBytes(expectation); |
| 617 | |
| 618 | result = GoldenFileComparator.compareLists( |
| 619 | imageBytes, |
| 620 | goldenBytes, |
| 621 | ); |
| 622 | |
| 623 | if (result.passed) { |
| 624 | return true; |
| 625 | } |
| 626 | failureDiffs[expectation] = result; |
| 627 | } |
Kate Lovett | dfbbfcd | 2019-11-07 13:01:32 -0800 | [diff] [blame] | 628 | |
Alexandre Ardhuin | 4f9b6cf | 2020-01-07 16:32:04 +0100 | [diff] [blame] | 629 | for (final MapEntry<String, ComparisonResult> entry in failureDiffs.entries) { |
Kate Lovett | dfbbfcd | 2019-11-07 13:01:32 -0800 | [diff] [blame] | 630 | if (await skiaClient.isValidDigestForExpectation(entry.key, golden.path)) |
| 631 | generateFailureOutput(entry.value, golden, basedir, key: entry.key); |
| 632 | } |
Kate Lovett | 7bc0203 | 2019-10-25 15:05:21 -0700 | [diff] [blame] | 633 | return false; |
| 634 | } |
| 635 | } |