Write dart:ui golden-file tests testing FilterQuality.* (flutter/engine#52687)

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`:

<img width="485" alt="Screenshot 2024-05-08 at 2 25 53 PM" src="https://github.com/flutter/engine/assets/168174/58e6a4f5-98e2-4b9e-b181-4ff613ad4d29">
This commit is contained in:
Matan Lurey
2024-05-10 15:15:23 -07:00
committed by GitHub
parent fa49f1794a
commit 70b18ec727
3 changed files with 106 additions and 1 deletions

View File

@@ -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
```

View File

@@ -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.<value> 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<Image> drawCheckerboard({
int width = 100,
int height = 100,
}) async {
final Completer<Image> completer = Completer<Image>();
final Uint32List pixels = Uint32List.fromList(
List<int>.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<Image> redGreenCheckerboard = drawCheckerboard();
/// Return the [image] shrunk and then scaled.
Future<Image> shrinkAndScaleImage(
Image image,
FilterQuality quality, {
double factorDown = 0.25,
double factorUp = 10,
}) async {
Future<Image> 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');
});
});
}

View File

@@ -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',