-
Notifications
You must be signed in to change notification settings - Fork 214
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into chore/flutter-3.22
- Loading branch information
Showing
20 changed files
with
91 additions
and
89 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
70 changes: 27 additions & 43 deletions
70
flutter_news_example/packages/news_blocks_ui/test/helpers/tolerant_comparator.dart
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 |
---|---|---|
@@ -1,60 +1,44 @@ | ||
// ignore_for_file: avoid_print | ||
|
||
import 'package:flutter/foundation.dart'; | ||
import 'package:flutter_test/flutter_test.dart'; | ||
|
||
/// A comparator for golden tests that allows for a small difference in pixels. | ||
/// | ||
/// This comparator performs a pixel-for-pixel comparison of the decoded PNGs, | ||
/// returning true only if the difference is less than [differenceTolerance]%. | ||
/// In case of failure this comparator will provide output to illustrate the | ||
/// difference. | ||
class TolerantComparator extends LocalFileComparator { | ||
TolerantComparator.from( | ||
super.testFile, { | ||
required LocalFileComparator comparator, | ||
}) : _comparator = comparator; | ||
|
||
/// The difference tolerance that this comparator allows for. | ||
/// | ||
/// If compared files produce less than [differenceTolerance]% difference, | ||
/// then the test is accepted. Otherwise, the test fails. | ||
static const differenceTolerance = .06; | ||
|
||
final LocalFileComparator _comparator; | ||
class _TolerantGoldenFileComparator extends LocalFileComparator { | ||
_TolerantGoldenFileComparator( | ||
super.testFile, | ||
); | ||
|
||
@override | ||
Uri getTestUri(Uri key, int? version) => _comparator.getTestUri(key, version); | ||
/// How much the golden image can differ from the test image. | ||
/// | ||
/// It is expected to be between 0 and 1. | ||
/// Where 0 is no difference (same image) | ||
/// and 1 is the maximum difference (completely different images). | ||
static const double _precisionTolerance = .06; | ||
|
||
@override | ||
Future<bool> compare(Uint8List imageBytes, Uri golden) async { | ||
final result = await GoldenFileComparator.compareLists( | ||
imageBytes, | ||
await getGoldenBytes(golden), | ||
); | ||
if (!result.passed) { | ||
final error = await generateFailureOutput(result, golden, basedir); | ||
if (result.diffPercent >= differenceTolerance) { | ||
throw FlutterError(error); | ||
} else { | ||
print( | ||
'Warning - golden differed less than $differenceTolerance% ' | ||
'(${result.diffPercent}%). Ignoring failure but logging the error.', | ||
); | ||
print(error); | ||
} | ||
|
||
final passed = result.passed || result.diffPercent <= _precisionTolerance; | ||
if (passed) { | ||
result.dispose(); | ||
return true; | ||
} | ||
return true; | ||
|
||
final error = await generateFailureOutput(result, golden, basedir); | ||
result.dispose(); | ||
throw FlutterError(error); | ||
} | ||
} | ||
|
||
/// Sets [TolerantComparator] as the default golden file comparator in tests. | ||
void setUpTolerantComparator() { | ||
final oldComparator = goldenFileComparator as LocalFileComparator; | ||
final newComparator = TolerantComparator.from( | ||
comparator: oldComparator, | ||
Uri.parse('${oldComparator.basedir}test'), | ||
); | ||
expect(oldComparator.basedir, newComparator.basedir); | ||
/// Sets [_TolerantGoldenFileComparator] as the default golden file comparator | ||
/// in tests. | ||
void setUpTolerantComparator(String testPath) { | ||
final oldComparator = goldenFileComparator; | ||
final newComparator = _TolerantGoldenFileComparator(Uri.parse(testPath)); | ||
|
||
goldenFileComparator = newComparator; | ||
|
||
addTearDown(() => goldenFileComparator = oldComparator); | ||
} |
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
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
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