Unverified Commit 6ec90142 authored by Kate Lovett's avatar Kate Lovett Committed by GitHub

Expose the diff from ComparisonResult (#77014)

parent 347e77d9
...@@ -170,11 +170,15 @@ class LocalComparisonOutput { ...@@ -170,11 +170,15 @@ class LocalComparisonOutput {
/// [test] and [master] image bytes provided. /// [test] and [master] image bytes provided.
Future<ComparisonResult> compareLists(List<int>? test, List<int>? master) async { Future<ComparisonResult> compareLists(List<int>? test, List<int>? master) async {
if (identical(test, master)) if (identical(test, master))
return ComparisonResult(passed: true); return ComparisonResult(
passed: true,
diffPercent: 0.0,
);
if (test == null || master == null || test.isEmpty || master.isEmpty) { if (test == null || master == null || test.isEmpty || master.isEmpty) {
return ComparisonResult( return ComparisonResult(
passed: false, passed: false,
diffPercent: 1.0,
error: 'Pixel test failed, null image provided.', error: 'Pixel test failed, null image provided.',
); );
} }
...@@ -195,6 +199,7 @@ Future<ComparisonResult> compareLists(List<int>? test, List<int>? master) async ...@@ -195,6 +199,7 @@ Future<ComparisonResult> compareLists(List<int>? test, List<int>? master) async
if (width != masterImage.width || height != masterImage.height) { if (width != masterImage.width || height != masterImage.height) {
return ComparisonResult( return ComparisonResult(
passed: false, passed: false,
diffPercent: 1.0,
error: 'Pixel test failed, image sizes do not match.\n' error: 'Pixel test failed, image sizes do not match.\n'
'Master Image: ${masterImage.width} X ${masterImage.height}\n' 'Master Image: ${masterImage.width} X ${masterImage.height}\n'
'Test Image: ${testImage.width} X ${testImage.height}', 'Test Image: ${testImage.width} X ${testImage.height}',
...@@ -240,10 +245,12 @@ Future<ComparisonResult> compareLists(List<int>? test, List<int>? master) async ...@@ -240,10 +245,12 @@ Future<ComparisonResult> compareLists(List<int>? test, List<int>? master) async
} }
if (pixelDiffCount > 0) { if (pixelDiffCount > 0) {
final double diffPercent = pixelDiffCount / totalPixels;
return ComparisonResult( return ComparisonResult(
passed: false, passed: false,
diffPercent: diffPercent,
error: 'Pixel test failed, ' error: 'Pixel test failed, '
'${((pixelDiffCount/totalPixels) * 100).toStringAsFixed(2)}% ' '${(diffPercent * 100).toStringAsFixed(2)}% '
'diff detected.', 'diff detected.',
diffs: <String, Image>{ diffs: <String, Image>{
'masterImage' : masterImage, 'masterImage' : masterImage,
...@@ -253,7 +260,7 @@ Future<ComparisonResult> compareLists(List<int>? test, List<int>? master) async ...@@ -253,7 +260,7 @@ Future<ComparisonResult> compareLists(List<int>? test, List<int>? master) async
}, },
); );
} }
return ComparisonResult(passed: true); return ComparisonResult(passed: true, diffPercent: 0.0);
} }
/// Inverts [imageBytes], returning a new [ByteData] object. /// Inverts [imageBytes], returning a new [ByteData] object.
......
...@@ -319,6 +319,7 @@ class ComparisonResult { ...@@ -319,6 +319,7 @@ class ComparisonResult {
/// Creates a new [ComparisonResult] for the current test. /// Creates a new [ComparisonResult] for the current test.
ComparisonResult({ ComparisonResult({
required this.passed, required this.passed,
required this.diffPercent,
this.error, this.error,
this.diffs, this.diffs,
}); });
...@@ -335,4 +336,7 @@ class ComparisonResult { ...@@ -335,4 +336,7 @@ class ComparisonResult {
/// values in the execution of the pixel test. /// values in the execution of the pixel test.
// TODO(jonahwilliams): fix type signature when image is updated to support web. // TODO(jonahwilliams): fix type signature when image is updated to support web.
final Map<String, Object>? diffs; final Map<String, Object>? diffs;
/// The calculated percentage of pixel difference between two images.
final double diffPercent;
} }
...@@ -104,7 +104,7 @@ void main() { ...@@ -104,7 +104,7 @@ void main() {
test('throws if local output is not awaited', () { test('throws if local output is not awaited', () {
try { try {
comparator.generateFailureOutput( comparator.generateFailureOutput(
ComparisonResult(passed: false), ComparisonResult(passed: false, diffPercent: 1.0),
Uri.parse('foo_test.dart'), Uri.parse('foo_test.dart'),
Uri.parse('/foo/bar/'), Uri.parse('/foo/bar/'),
); );
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment