From 70b18ec7275fbf8bbf723f30119940674fce2478 Mon Sep 17 00:00:00 2001 From: Matan Lurey Date: Fri, 10 May 2024 15:15:23 -0700 Subject: [PATCH] Write `dart:ui` golden-file tests testing `FilterQuality.*` (flutter/engine#52687) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Work towards https://github.com/flutter/flutter/issues/147259. Most of this is just me understanding how `FilterQuality.*` feeds into the rest of the engine, and updating our testing documentation so we all understand how to re-run these tests in the future. Here is an example output of both Impeller and non-Impeller output of `image_filter_test`: Screenshot 2024-05-08 at 2 25 53 PM --- engine/src/flutter/testing/dart/README.md | 42 +++++++++++++ .../testing/dart/image_filter_test.dart | 62 +++++++++++++++++++ engine/src/flutter/testing/run_tests.py | 3 +- 3 files changed, 106 insertions(+), 1 deletion(-) create mode 100644 engine/src/flutter/testing/dart/README.md diff --git a/engine/src/flutter/testing/dart/README.md b/engine/src/flutter/testing/dart/README.md new file mode 100644 index 0000000000..23340ada82 --- /dev/null +++ b/engine/src/flutter/testing/dart/README.md @@ -0,0 +1,42 @@ +# Dart UI Tests + +The tests in this directory are written in Dart and run on the Flutter engine, +typically testing functionality that is not easily tested with C++ tests or +is best tested at a higher level (i.e. importing `dart:ui`). + +## Running the tests + +The simplest way to run these tests is using [`run_tests.py`][]: + +```sh +./testing/run_tests.py --type=dart +``` + +Or, to run a specific test, provide the base file name as `--dart-filter`: + +```sh +./testing/run_tests.py --type=dart --dart-filter=image_filter_test.dart +``` + +[`run_tests.py`]: ../run_tests.py + +Note that the tests are _compiled_ as Dart kernel files, and not run from +source, if any changes are made to the tests, you will need to recompile them +before running the tests: + +```sh +# At the time of this writing, 'et build' did not work with Dart targets. +# Instead, use the following command to build the tests. + +ninja -C ../out/host_debug_unopt_arm64 compile_image_filter_test.dart +``` + +To view the outputted golden files locally, you'll need to open the generated +output folder, which will vary based on both your current target architecture +and the test suite you're running. + +For example, for the above test on `host_debug_unopt_arm64`: + +```sh +open ../out/host_debug_unopt_arm64/gen/skia_gold_image_filter_test.dart_iplr +``` diff --git a/engine/src/flutter/testing/dart/image_filter_test.dart b/engine/src/flutter/testing/dart/image_filter_test.dart index 44ffece1b7..100d198947 100644 --- a/engine/src/flutter/testing/dart/image_filter_test.dart +++ b/engine/src/flutter/testing/dart/image_filter_test.dart @@ -2,11 +2,13 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. +import 'dart:async'; import 'dart:typed_data'; import 'dart:ui'; import 'package:litetest/litetest.dart'; +import 'goldens.dart'; import 'impeller_enabled.dart'; const Color red = Color(0xFFAA0000); @@ -297,4 +299,64 @@ void main() { ), ); }); + + // Tests that FilterQuality. produces the expected golden file. + group('ImageFilter|FilterQuality', () async { + final ImageComparer comparer = await ImageComparer.create(); + + /// Draw a red-green checkerboard pattern with 1x1 squares (pixels). + Future drawCheckerboard({ + int width = 100, + int height = 100, + }) async { + final Completer completer = Completer(); + final Uint32List pixels = Uint32List.fromList( + List.generate(width * height, (int index) { + final int x = index % width; + final int y = index ~/ width; + return (x % 2 == y % 2) ? red.value : green.value; + }), + ); + decodeImageFromPixels( + Uint8List.view(pixels.buffer), + width, + height, + PixelFormat.rgba8888, + completer.complete, + ); + return completer.future; + } + + final Future redGreenCheckerboard = drawCheckerboard(); + + /// Return the [image] shrunk and then scaled. + Future shrinkAndScaleImage( + Image image, + FilterQuality quality, { + double factorDown = 0.25, + double factorUp = 10, + }) async { + Future scale(Image image, double factor) async { + final Paint paint = Paint()..filterQuality = quality; + final PictureRecorder recorder = PictureRecorder(); + final Canvas canvas = Canvas(recorder); + + final Rect input = Rect.fromLTWH(0, 0, image.width.toDouble(), image.height.toDouble()); + final Rect output = Rect.fromLTWH(0, 0, input.width * factor, input.height * factor); + + canvas.drawImageRect(image, input, output, paint); + final Picture picture = recorder.endRecording(); + return picture.toImage(output.width.toInt(), output.height.toInt()); + } + + final Image shrunk = await scale(image, factorDown); + return scale(shrunk, factorUp); + } + + test('Scaling a checkerboard of 1x1 red-green pixels with FilterQuality.none', () async { + final Image base = await redGreenCheckerboard; + final Image scaled = await shrinkAndScaleImage(base, FilterQuality.none); + await comparer.addGoldenImage(scaled, 'dart_ui_filter_quality_none_scale_1x1_red_green_checkerboard.png'); + }); + }); } diff --git a/engine/src/flutter/testing/run_tests.py b/engine/src/flutter/testing/run_tests.py index 1fbcd31f84..c2d87e05d4 100755 --- a/engine/src/flutter/testing/run_tests.py +++ b/engine/src/flutter/testing/run_tests.py @@ -1156,7 +1156,8 @@ Flutter Wiki page on the subject: https://github.com/flutter/flutter/wiki/Testin '--dart-filter', type=str, default='', - help='A list of Dart test scripts to run in flutter_tester.' + help='A list of Dart test script base file names to run in ' + 'flutter_tester (example: "image_filter_test.dart").' ) parser.add_argument( '--dart-host-filter',