From 6ec9014298140490eb697745fa98f5a67036cb17 Mon Sep 17 00:00:00 2001 From: Kate Lovett Date: Tue, 2 Mar 2021 12:09:03 -0600 Subject: [PATCH] Expose the diff from ComparisonResult (#77014) --- packages/flutter_test/lib/src/_goldens_io.dart | 13 ++++++++++--- packages/flutter_test/lib/src/goldens.dart | 4 ++++ packages/flutter_test/test/goldens_test.dart | 2 +- 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/packages/flutter_test/lib/src/_goldens_io.dart b/packages/flutter_test/lib/src/_goldens_io.dart index dda3880f13..db12c02439 100644 --- a/packages/flutter_test/lib/src/_goldens_io.dart +++ b/packages/flutter_test/lib/src/_goldens_io.dart @@ -170,11 +170,15 @@ class LocalComparisonOutput { /// [test] and [master] image bytes provided. Future compareLists(List? test, List? master) async { if (identical(test, master)) - return ComparisonResult(passed: true); + return ComparisonResult( + passed: true, + diffPercent: 0.0, + ); if (test == null || master == null || test.isEmpty || master.isEmpty) { return ComparisonResult( passed: false, + diffPercent: 1.0, error: 'Pixel test failed, null image provided.', ); } @@ -195,6 +199,7 @@ Future compareLists(List? test, List? master) async if (width != masterImage.width || height != masterImage.height) { return ComparisonResult( passed: false, + diffPercent: 1.0, error: 'Pixel test failed, image sizes do not match.\n' 'Master Image: ${masterImage.width} X ${masterImage.height}\n' 'Test Image: ${testImage.width} X ${testImage.height}', @@ -240,10 +245,12 @@ Future compareLists(List? test, List? master) async } if (pixelDiffCount > 0) { + final double diffPercent = pixelDiffCount / totalPixels; return ComparisonResult( passed: false, + diffPercent: diffPercent, error: 'Pixel test failed, ' - '${((pixelDiffCount/totalPixels) * 100).toStringAsFixed(2)}% ' + '${(diffPercent * 100).toStringAsFixed(2)}% ' 'diff detected.', diffs: { 'masterImage' : masterImage, @@ -253,7 +260,7 @@ Future compareLists(List? test, List? master) async }, ); } - return ComparisonResult(passed: true); + return ComparisonResult(passed: true, diffPercent: 0.0); } /// Inverts [imageBytes], returning a new [ByteData] object. diff --git a/packages/flutter_test/lib/src/goldens.dart b/packages/flutter_test/lib/src/goldens.dart index 1f17e3a7dc..0143a71d7c 100644 --- a/packages/flutter_test/lib/src/goldens.dart +++ b/packages/flutter_test/lib/src/goldens.dart @@ -319,6 +319,7 @@ class ComparisonResult { /// Creates a new [ComparisonResult] for the current test. ComparisonResult({ required this.passed, + required this.diffPercent, this.error, this.diffs, }); @@ -335,4 +336,7 @@ class ComparisonResult { /// values in the execution of the pixel test. // TODO(jonahwilliams): fix type signature when image is updated to support web. final Map? diffs; + + /// The calculated percentage of pixel difference between two images. + final double diffPercent; } diff --git a/packages/flutter_test/test/goldens_test.dart b/packages/flutter_test/test/goldens_test.dart index 6ed20e058a..9502d54057 100644 --- a/packages/flutter_test/test/goldens_test.dart +++ b/packages/flutter_test/test/goldens_test.dart @@ -104,7 +104,7 @@ void main() { test('throws if local output is not awaited', () { try { comparator.generateFailureOutput( - ComparisonResult(passed: false), + ComparisonResult(passed: false, diffPercent: 1.0), Uri.parse('foo_test.dart'), Uri.parse('/foo/bar/'), );