-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
326 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import 'dart:async'; | ||
|
||
import 'package:flutter_test/flutter_test.dart'; | ||
|
||
import 'util/local_file_comparator_with_threshold.dart'; | ||
|
||
/// Customise your threshold here | ||
/// For example, the error threshold here is 0.5% | ||
/// Golden tests will pass if the pixel difference is equal to or below 0.5% | ||
const _kGoldenTestsThreshold = 0.5 / 100; | ||
|
||
Future<void> testExecutable(FutureOr<void> Function() testMain) async { | ||
if (goldenFileComparator is LocalFileComparator) { | ||
final testUrl = (goldenFileComparator as LocalFileComparator).basedir; | ||
|
||
goldenFileComparator = LocalFileComparatorWithThreshold( | ||
// flutter_test's LocalFileComparator expects the test's URI to be passed | ||
// as an argument, but it only uses it to parse the baseDir in order to | ||
// obtain the directory where the golden tests will be placed. | ||
// As such, we use the default `testUrl`, which is only the `baseDir` and | ||
// append a generically named `test.dart` so that the `baseDir` is | ||
// properly extracted. | ||
Uri.parse('$testUrl/test.dart'), | ||
_kGoldenTestsThreshold, | ||
); | ||
} else { | ||
throw Exception( | ||
'Expected `goldenFileComparator` to be of type `LocalFileComparator`, ' | ||
'but it is of type `${goldenFileComparator.runtimeType}`', | ||
); | ||
} | ||
|
||
await testMain(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import 'package:flutter/foundation.dart'; | ||
import 'package:flutter_test/flutter_test.dart'; | ||
|
||
// source: https://stackoverflow.com/a/73048760/14052058 | ||
|
||
/// Works just like [LocalFileComparator] but includes a [threshold] that, when | ||
/// exceeded, marks the test as a failure. | ||
class LocalFileComparatorWithThreshold extends LocalFileComparator { | ||
/// Threshold above which tests will be marked as failing. | ||
/// Ranges from 0 to 1, both inclusive. | ||
final double threshold; | ||
|
||
LocalFileComparatorWithThreshold(super.testFile, this.threshold) | ||
: assert(threshold >= 0 && threshold <= 1); | ||
|
||
/// Copy of [LocalFileComparator]'s [compare] method, except for the fact that | ||
/// it checks if the [ComparisonResult.diffPercent] is not greater than | ||
/// [threshold] to decide whether this test is successful or a failure. | ||
@override | ||
Future<bool> compare(Uint8List imageBytes, Uri golden) async { | ||
final result = await GoldenFileComparator.compareLists( | ||
imageBytes, | ||
await getGoldenBytes(golden), | ||
); | ||
|
||
if (!result.passed && result.diffPercent <= threshold) { | ||
debugPrint( | ||
'A difference of ${result.diffPercent * 100}% was found, but it is ' | ||
'acceptable since it is not greater than the threshold of ' | ||
'${threshold * 100}%', | ||
); | ||
|
||
return true; | ||
} | ||
|
||
if (!result.passed) { | ||
final error = await generateFailureOutput(result, golden, basedir); | ||
throw FlutterError(error); | ||
} | ||
return result.passed; | ||
} | ||
} |
Oops, something went wrong.