|
| 1 | +import 'dart:io'; |
| 2 | +import 'dart:typed_data'; |
| 3 | + |
| 4 | +import 'package:flutter/foundation.dart'; |
| 5 | +import 'package:flutter_test/flutter_test.dart'; |
| 6 | +import 'package:path/path.dart'; |
| 7 | + |
| 8 | +/// A matcher that expects given content to match the golden file referenced |
| 9 | +/// by [key], allowing up to [maxPixelMismatchCount] different pixels before |
| 10 | +/// considering the test to be a failure. |
| 11 | +/// |
| 12 | +/// Typically, the [key] is expected to be a relative file path from the given |
| 13 | +/// test file, to the golden file, e.g., "goldens/my-golden-name.png". |
| 14 | +/// |
| 15 | +/// This matcher can be used by calling it in `expectLater()`, e.g., |
| 16 | +/// |
| 17 | +/// await expectLater( |
| 18 | +/// find.byType(MaterialApp), |
| 19 | +/// matchesGoldenFileWithPixelAllowance("goldens/my-golden-name.png", 20), |
| 20 | +/// ); |
| 21 | +/// |
| 22 | +/// Typically, Flutter's golden system describes mismatches in terms of percentages. |
| 23 | +/// But percentages are difficult to depend upon. Sometimes a relatively large percentage |
| 24 | +/// doesn't matter, and sometimes a tiny percentage is critical. When it comes to ignoring |
| 25 | +/// irrelevant mismatches, it's often more convenient to work in terms of pixels. This |
| 26 | +/// matcher lets developers specify a maximum pixel mismatch count, instead of relying on |
| 27 | +/// percentage differences across the entire golden image. |
| 28 | +MatchesGoldenFile matchesGoldenFileWithPixelAllowance(Object key, int maxPixelMismatchCount, {int? version}) { |
| 29 | + if (key is Uri) { |
| 30 | + return MatchesGoldenFileWithPixelAllowance(key, maxPixelMismatchCount, version); |
| 31 | + } else if (key is String) { |
| 32 | + return MatchesGoldenFileWithPixelAllowance.forStringPath(key, maxPixelMismatchCount, version); |
| 33 | + } |
| 34 | + throw ArgumentError('Unexpected type for golden file: ${key.runtimeType}'); |
| 35 | +} |
| 36 | + |
| 37 | +/// A special version of [MatchesGoldenFile] that allows a specified number of |
| 38 | +/// pixels to be different between golden files before considering the test to |
| 39 | +/// be a failure. |
| 40 | +/// |
| 41 | +/// Typically, this matcher is expected to be created by calling |
| 42 | +/// [matchesGoldenFileWithPixelAllowance]. |
| 43 | +class MatchesGoldenFileWithPixelAllowance extends MatchesGoldenFile { |
| 44 | + /// Creates a [MatchesGoldenFileWithPixelAllowance] that looks for a golden |
| 45 | + /// file at the relative path within the [key] URI. |
| 46 | + /// |
| 47 | + /// The [key] URI should be a relative path from the executing test's |
| 48 | + /// directory to the golden file, e.g., "goldens/my-golden-name.png". |
| 49 | + MatchesGoldenFileWithPixelAllowance(super.key, this._maxPixelMismatchCount, [super.version]); |
| 50 | + |
| 51 | + /// Creates a [MatchesGoldenFileWithPixelAllowance] that looks for a golden |
| 52 | + /// file at the relative [path]. |
| 53 | + /// |
| 54 | + /// The [path] should be relative to the executing test's directory, e.g., |
| 55 | + /// "goldens/my-golden-name.png". |
| 56 | + MatchesGoldenFileWithPixelAllowance.forStringPath(String path, this._maxPixelMismatchCount, [int? version]) |
| 57 | + : super.forStringPath(path, version); |
| 58 | + |
| 59 | + final int _maxPixelMismatchCount; |
| 60 | + |
| 61 | + @override |
| 62 | + Future<String?> matchAsync(dynamic item) async { |
| 63 | + // Cache the current goldenFileComparator so we can restore |
| 64 | + // it after the test. |
| 65 | + final originalComparator = goldenFileComparator; |
| 66 | + |
| 67 | + try { |
| 68 | + goldenFileComparator = PixelDiffGoldenComparator( |
| 69 | + (goldenFileComparator as LocalFileComparator).basedir.path, |
| 70 | + pixelCount: _maxPixelMismatchCount, |
| 71 | + ); |
| 72 | + |
| 73 | + return await super.matchAsync(item); |
| 74 | + } finally { |
| 75 | + goldenFileComparator = originalComparator; |
| 76 | + } |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +/// A golden file comparator that allows a specified number of pixels |
| 81 | +/// to be different between the golden image file and the test image file, and |
| 82 | +/// still pass. |
| 83 | +class PixelDiffGoldenComparator extends LocalFileComparator { |
| 84 | + PixelDiffGoldenComparator( |
| 85 | + String testBaseDirectory, { |
| 86 | + required int pixelCount, |
| 87 | + }) : _testBaseDirectory = testBaseDirectory, |
| 88 | + _maxPixelMismatchCount = pixelCount, |
| 89 | + super(Uri.parse(testBaseDirectory)); |
| 90 | + |
| 91 | + @override |
| 92 | + Uri get basedir => Uri.parse(_testBaseDirectory); |
| 93 | + |
| 94 | + /// The file system path to the directory that holds the currently executing |
| 95 | + /// Dart test file. |
| 96 | + final String _testBaseDirectory; |
| 97 | + |
| 98 | + /// The maximum number of mismatched pixels for which this pixel test |
| 99 | + /// is considered a success/pass. |
| 100 | + final int _maxPixelMismatchCount; |
| 101 | + |
| 102 | + @override |
| 103 | + Future<bool> compare(Uint8List imageBytes, Uri golden) async { |
| 104 | + // Note: the incoming `golden` Uri is a partial path from the currently |
| 105 | + // executing test directory to the golden file, e.g., "goldens/my-test.png". |
| 106 | + final result = await GoldenFileComparator.compareLists( |
| 107 | + imageBytes, |
| 108 | + await getGoldenBytes(golden), |
| 109 | + ); |
| 110 | + |
| 111 | + if (result.passed) { |
| 112 | + return true; |
| 113 | + } |
| 114 | + |
| 115 | + final diffImage = result.diffs!.entries.first.value; |
| 116 | + final pixelCount = diffImage.width * diffImage.height; |
| 117 | + final pixelMismatchCount = pixelCount * result.diffPercent; |
| 118 | + |
| 119 | + if (pixelMismatchCount <= _maxPixelMismatchCount) { |
| 120 | + return true; |
| 121 | + } |
| 122 | + |
| 123 | + // Paint the golden diffs and images to failure files. |
| 124 | + await generateFailureOutput(result, golden, basedir); |
| 125 | + throw FlutterError( |
| 126 | + "Pixel test failed. ${result.diffPercent.toStringAsFixed(2)}% diff, $pixelMismatchCount pixel count diff (max allowed pixel mismatch count is $_maxPixelMismatchCount)"); |
| 127 | + } |
| 128 | + |
| 129 | + @override |
| 130 | + @protected |
| 131 | + Future<List<int>> getGoldenBytes(Uri golden) async { |
| 132 | + final File goldenFile = _getGoldenFile(golden); |
| 133 | + if (!goldenFile.existsSync()) { |
| 134 | + fail('Could not be compared against non-existent file: "$golden"'); |
| 135 | + } |
| 136 | + final List<int> goldenBytes = await goldenFile.readAsBytes(); |
| 137 | + return goldenBytes; |
| 138 | + } |
| 139 | + |
| 140 | + File _getGoldenFile(Uri golden) => File(join(_testBaseDirectory, fromUri(golden.path))); |
| 141 | +} |
0 commit comments