From 2232f767bdd6bd71080cebb2c80dfb9bdb74b989 Mon Sep 17 00:00:00 2001 From: Jason Simmons Date: Wed, 20 Jul 2022 13:03:04 -0700 Subject: [PATCH] Use uncompressed image size as the basis for the fuzzy pixel matching threshold in the Skia Gold scenario tests (flutter/engine#34431) --- .../dev/flutter/scenariosui/ScreenshotUtil.java | 14 +++++++++----- .../bin/android_integration_tests.dart | 3 ++- .../bin/utils/screenshot_transformer.dart | 12 +++++++++--- .../skia_gold_client/lib/skia_gold_client.dart | 4 ++-- 4 files changed, 22 insertions(+), 11 deletions(-) diff --git a/engine/src/flutter/testing/scenario_app/android/app/src/androidTest/java/dev/flutter/scenariosui/ScreenshotUtil.java b/engine/src/flutter/testing/scenario_app/android/app/src/androidTest/java/dev/flutter/scenariosui/ScreenshotUtil.java index 10272513d0..dd3319df59 100644 --- a/engine/src/flutter/testing/scenario_app/android/app/src/androidTest/java/dev/flutter/scenariosui/ScreenshotUtil.java +++ b/engine/src/flutter/testing/scenario_app/android/app/src/androidTest/java/dev/flutter/scenariosui/ScreenshotUtil.java @@ -36,11 +36,13 @@ public class ScreenshotUtil { out = socket.getOutputStream(); } - synchronized void writeFile(String name, byte[] fileContent) throws IOException { - final ByteBuffer buffer = ByteBuffer.allocate(name.length() + fileContent.length + 8); + synchronized void writeFile(String name, byte[] fileContent, int pixelCount) + throws IOException { + final ByteBuffer buffer = ByteBuffer.allocate(name.length() + fileContent.length + 12); // See ScreenshotBlobTransformer#bind in screenshot_transformer.dart for consumer side. buffer.putInt(name.length()); buffer.putInt(fileContent.length); + buffer.putInt(pixelCount); buffer.put(name.getBytes()); buffer.put(fileContent); final byte[] bytes = buffer.array(); @@ -92,12 +94,13 @@ public class ScreenshotUtil { * @param filename The file name. * @param fileContent The file content. */ - public static synchronized void writeFile(@NonNull String filename, @NonNull byte[] fileContent) { + public static synchronized void writeFile( + @NonNull String filename, @NonNull byte[] fileContent, int pixelCount) { if (executor != null && conn != null) { executor.execute( () -> { try { - conn.writeFile(filename, fileContent); + conn.writeFile(filename, fileContent, pixelCount); } catch (IOException e) { throw new RuntimeException(e); } @@ -123,8 +126,9 @@ public class ScreenshotUtil { if (bitmap == null) { throw new RuntimeException("failed to capture screenshot"); } + int pixelCount = bitmap.getWidth() * bitmap.getHeight(); final ByteArrayOutputStream out = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.PNG, 100, out); - ScreenshotUtil.writeFile(captureName, out.toByteArray()); + ScreenshotUtil.writeFile(captureName, out.toByteArray(), pixelCount); } } diff --git a/engine/src/flutter/testing/scenario_app/bin/android_integration_tests.dart b/engine/src/flutter/testing/scenario_app/bin/android_integration_tests.dart index 614030ea29..221525516c 100644 --- a/engine/src/flutter/testing/scenario_app/bin/android_integration_tests.dart +++ b/engine/src/flutter/testing/scenario_app/bin/android_integration_tests.dart @@ -76,7 +76,8 @@ void main(List args) async { log('wrote ${goldenFile.absolute.path}'); if (isSkiaGoldClientAvailable) { final Future comparison = skiaGoldClient! - .addImg(fileName, goldenFile, screenshotSize: fileContent.lengthInBytes) + .addImg(fileName, goldenFile, + screenshotSize: screenshot.pixelCount) .catchError((dynamic err) { panic(['skia gold comparison failed: ${err.toString()}']); }); diff --git a/engine/src/flutter/testing/scenario_app/bin/utils/screenshot_transformer.dart b/engine/src/flutter/testing/scenario_app/bin/utils/screenshot_transformer.dart index 9b8df01805..d29f01cca2 100644 --- a/engine/src/flutter/testing/scenario_app/bin/utils/screenshot_transformer.dart +++ b/engine/src/flutter/testing/scenario_app/bin/utils/screenshot_transformer.dart @@ -8,13 +8,16 @@ import 'dart:typed_data'; /// A screenshot from the Android emulator. class Screenshot { - Screenshot(this.filename, this.fileContent); + Screenshot(this.filename, this.fileContent, this.pixelCount); /// The name of the screenshot. final String filename; /// The binary content of the screenshot. final Uint8List fileContent; + + /// The number of pixels in the screenshot. + final int pixelCount; } /// Takes the input stream and transforms it into [Screenshot]s. @@ -28,7 +31,7 @@ class ScreenshotBlobTransformer extends StreamTransformerBase 0); assert(fcontentLen > 0); + assert(pixelCount > 0); if (pending.length < off + fnameLen) { continue; @@ -60,7 +66,7 @@ class ScreenshotBlobTransformer extends StreamTransformerBase addImg( String testName, File goldenFile, { - double differentPixelsRate = 0.1, + double differentPixelsRate = 0.01, int pixelColorDelta = 0, required int screenshotSize, }) async {