|
1 | 1 | import 'dart:async';
|
2 |
| -import 'dart:io'; |
3 | 2 |
|
4 |
| -import 'package:golden_toolkit/golden_toolkit.dart'; |
| 3 | +import 'package:flutter/foundation.dart'; |
| 4 | +import 'package:flutter_test/flutter_test.dart'; |
5 | 5 |
|
6 |
| -// All tests in this folder and its subfolders will have the configuration defined here |
| 6 | +const _kGoldenTestsThreshold = 10 / 100; // 1% tolerance |
7 | 7 |
|
8 | 8 | Future<void> testExecutable(FutureOr<void> Function() testMain) async {
|
9 |
| - return GoldenToolkit.runWithConfiguration( |
10 |
| - () async { |
11 |
| - await loadAppFonts(); |
12 |
| - await testMain(); |
13 |
| - }, |
14 |
| - config: GoldenToolkitConfiguration( |
15 |
| - // Currently, goldens are not generated/validated in CI for this repo. We have settled on the goldens for this package |
16 |
| - // being captured/validated by developers running on MacOSX. We may revisit this in the future if there is a reason to invest |
17 |
| - // in more sophistication |
18 |
| - skipGoldenAssertion: () => !Platform.isMacOS, |
19 |
| - enableRealShadows: true, |
20 |
| - ), |
21 |
| - ); |
| 9 | + if (goldenFileComparator is LocalFileComparator) { |
| 10 | + final testUrl = (goldenFileComparator as LocalFileComparator).basedir; |
| 11 | + |
| 12 | + goldenFileComparator = LocalFileComparatorWithThreshold( |
| 13 | + Uri.parse('$testUrl/test. dart'), |
| 14 | + _kGoldenTestsThreshold, |
| 15 | + ); |
| 16 | + } else { |
| 17 | + throw Exception( |
| 18 | + 'Expected goldenFileComparator to be of type' |
| 19 | + 'LocalFileComparator' |
| 20 | + 'but it is of type ${goldenFileComparator.runtimeType}`', |
| 21 | + ); |
| 22 | + } |
| 23 | + await testMain(); |
| 24 | +} |
| 25 | + |
| 26 | +/// Works just like [LocalFileComparator] but includes a [threshold] that, when |
| 27 | +/// exceeded, marks the test as a failure. |
| 28 | +class LocalFileComparatorWithThreshold extends LocalFileComparator { |
| 29 | + /// Threshold above which tests will be marked as failing. |
| 30 | + /// Ranges from 0 to 1, both inclusive. |
| 31 | + final double threshold; |
| 32 | + |
| 33 | + LocalFileComparatorWithThreshold(Uri testFile, this.threshold) |
| 34 | + : assert(threshold >= 0 && threshold <= 1), |
| 35 | + super(testFile); |
| 36 | + |
| 37 | + /// Copy of [LocalFileComparator]'s [compare] method, except for the fact that |
| 38 | + /// it checks if the [ComparisonResult.diffPercent] is not greater than |
| 39 | + /// [threshold] to decide whether this test is successful or a failure. |
| 40 | + @override |
| 41 | + Future<bool> compare(Uint8List imageBytes, Uri golden) async { |
| 42 | + final result = await GoldenFileComparator.compareLists( |
| 43 | + imageBytes, |
| 44 | + await getGoldenBytes(golden), |
| 45 | + ); |
| 46 | + |
| 47 | + if (!result.passed && result.diffPercent <= threshold) { |
| 48 | + // print( |
| 49 | + // 'A difference of ${result.diffPercent * 100}% was found, but it is ' |
| 50 | + // 'acceptable since it is not greater than the threshold of ' |
| 51 | + // '${threshold * 100}%', |
| 52 | + // ); |
| 53 | + |
| 54 | + return true; |
| 55 | + } |
| 56 | + |
| 57 | + if (!result.passed) { |
| 58 | + final error = await generateFailureOutput(result, golden, basedir); |
| 59 | + throw FlutterError(error); |
| 60 | + } |
| 61 | + return result.passed; |
| 62 | + } |
22 | 63 | }
|
0 commit comments