From 29190035139b66fbc6fb4666e9ebc79d886f5c21 Mon Sep 17 00:00:00 2001 From: Koji Wakamiya Date: Fri, 24 Jan 2025 00:13:57 +0900 Subject: [PATCH] fix: Call codec.dispose in tests of `engine/src/flutter` (#161115) PR derived from https://github.com/flutter/flutter/pull/159945. Added `codec.dispose()` for directories under `engine/src/flutter`. ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [x] I signed the [CLA]. - [ ] I listed at least one issue that this PR fixes in the description above. - [ ] I updated/added relevant documentation (doc comments with `///`). - [x] I added new tests to check the change I am making, or this PR is [test-exempt]. - [x] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [x] All existing and new tests are passing. If you need help, consider asking for advice on the #hackers-new channel on [Discord]. [Contributor Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview [Tree Hygiene]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md [test-exempt]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests [Flutter Style Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md [Features we expect every widget to implement]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement [CLA]: https://cla.developers.google.com/ [flutter/tests]: https://github.com/flutter/tests [breaking change policy]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes [Discord]: https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md [Data Driven Fixes]: https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md --- .../test/canvaskit/image_golden_test.dart | 24 ++++++++++--------- .../canvas_draw_image_golden_test.dart | 1 + .../lib/web_ui/test/ui/filters_test.dart | 1 + .../lib/web_ui/test/ui/image_golden_test.dart | 5 ++++ .../src/flutter/testing/dart/codec_test.dart | 15 ++++++++++++ .../testing/dart/fragment_shader_test.dart | 1 + .../testing/dart/image_descriptor_test.dart | 5 ++++ .../testing/dart/image_dispose_test.dart | 6 +++++ .../testing/dart/image_resize_test.dart | 7 ++++++ 9 files changed, 54 insertions(+), 11 deletions(-) diff --git a/engine/src/flutter/lib/web_ui/test/canvaskit/image_golden_test.dart b/engine/src/flutter/lib/web_ui/test/canvaskit/image_golden_test.dart index 659c84e6a1..27faf8ec34 100644 --- a/engine/src/flutter/lib/web_ui/test/canvaskit/image_golden_test.dart +++ b/engine/src/flutter/lib/web_ui/test/canvaskit/image_golden_test.dart @@ -14,8 +14,6 @@ import 'package:ui/ui.dart' as ui; import 'common.dart'; import 'test_data.dart'; -List? testCodecs; - void main() { internalBootstrapBrowserTest(() => testMain); } @@ -29,6 +27,11 @@ abstract class TestCodec { Future getCodec() async => _cachedCodec ??= await createCodec(); Future createCodec(); + + void dispose() { + _cachedCodec?.dispose(); + _cachedCodec = null; + } } abstract class TestFileCodec extends TestCodec { @@ -134,13 +137,10 @@ class BitmapSingleFrameCodec implements ui.Codec { } Future testMain() async { - Future> createTestCodecs({ - int testTargetWidth = 300, - int testTargetHeight = 300, - }) async { - final HttpFetchResponse listingResponse = await httpFetch('/test_images/'); - final List testFiles = (await listingResponse.json() as List).cast(); + final HttpFetchResponse listingResponse = await httpFetch('/test_images/'); + final List testFiles = (await listingResponse.json() as List).cast(); + List createTestCodecs({int testTargetWidth = 300, int testTargetHeight = 300}) { // Sanity-check the test file list. If suddenly test files are moved or // deleted, and the test server returns an empty list, or is missing some // important test files, we want to know. @@ -192,8 +192,6 @@ Future testMain() async { return testCodecs; } - testCodecs = await createTestCodecs(); - group('CanvasKit Images', () { setUpCanvasKitTest(withImplicitView: true); @@ -202,7 +200,8 @@ Future testMain() async { }); group('Codecs', () { - for (final TestCodec testCodec in testCodecs!) { + final List testCodecs = createTestCodecs(); + for (final TestCodec testCodec in testCodecs) { test('${testCodec.description} can create an image', () async { try { final ui.Codec codec = await testCodec.getCodec(); @@ -247,6 +246,9 @@ Future testMain() async { ); }); } + for (final testCodec in testCodecs) { + testCodec.dispose(); + } }); test('crossOrigin requests cause an error', () async { diff --git a/engine/src/flutter/lib/web_ui/test/html/drawing/canvas_draw_image_golden_test.dart b/engine/src/flutter/lib/web_ui/test/html/drawing/canvas_draw_image_golden_test.dart index 64ef6a1903..6b778e6640 100644 --- a/engine/src/flutter/lib/web_ui/test/html/drawing/canvas_draw_image_golden_test.dart +++ b/engine/src/flutter/lib/web_ui/test/html/drawing/canvas_draw_image_golden_test.dart @@ -39,6 +39,7 @@ Future testMain() async { final Uint8List imageData = base64Decode(base64PngData); final Codec codec = await instantiateImageCodec(imageData); final FrameInfo frameInfo = await codec.getNextFrame(); + codec.dispose(); const Rect bounds = Rect.fromLTRB(0, 0, 400, 300); final EnginePictureRecorder recorder = EnginePictureRecorder(); diff --git a/engine/src/flutter/lib/web_ui/test/ui/filters_test.dart b/engine/src/flutter/lib/web_ui/test/ui/filters_test.dart index 8b9a69d7b9..d86982d982 100644 --- a/engine/src/flutter/lib/web_ui/test/ui/filters_test.dart +++ b/engine/src/flutter/lib/web_ui/test/ui/filters_test.dart @@ -30,6 +30,7 @@ Future testMain() async { expect(codec.frameCount, 1); final ui.FrameInfo info = await codec.getNextFrame(); + codec.dispose(); final ui.Image image = info.image; expect(image.width, 128); expect(image.height, 128); diff --git a/engine/src/flutter/lib/web_ui/test/ui/image_golden_test.dart b/engine/src/flutter/lib/web_ui/test/ui/image_golden_test.dart index 1a81b1ef84..26517ec710 100644 --- a/engine/src/flutter/lib/web_ui/test/ui/image_golden_test.dart +++ b/engine/src/flutter/lib/web_ui/test/ui/image_golden_test.dart @@ -381,6 +381,7 @@ Future testMain() async { expect(codec.frameCount, 1); final ui.FrameInfo info = await codec.getNextFrame(); + codec.dispose(); return info.image; }); @@ -393,6 +394,7 @@ Future testMain() async { expect(codec.frameCount, 1); final ui.FrameInfo info = await codec.getNextFrame(); + codec.dispose(); expect(info.image.width, 3024); expect(info.image.height, 4032); }); @@ -424,6 +426,7 @@ Future testMain() async { await completer.future; final DomImageBitmap bitmap = await createImageBitmap(image as JSObject); + domWindow.URL.revokeObjectURL(url); expect(bitmap.width.toDartInt, 150); expect(bitmap.height.toDartInt, 150); @@ -471,6 +474,7 @@ Future testMain() async { height: 150, transferOwnership: false, ); + domWindow.URL.revokeObjectURL(url); return uiImage; }); } @@ -485,6 +489,7 @@ Future testMain() async { expect(codec.frameCount, 1); final ui.FrameInfo info = await codec.getNextFrame(); + codec.dispose(); return info.image; }); } diff --git a/engine/src/flutter/testing/dart/codec_test.dart b/engine/src/flutter/testing/dart/codec_test.dart index 71c496b65e..fe0a7acdf9 100644 --- a/engine/src/flutter/testing/dart/codec_test.dart +++ b/engine/src/flutter/testing/dart/codec_test.dart @@ -49,6 +49,8 @@ void main() { } else { expect(e.toString(), contains('Codec failed')); } + } finally { + codec.dispose(); } }); @@ -64,6 +66,7 @@ void main() { frameInfo.image.height, ]); } + codec.dispose(); expect( decodedFrameInfos, equals(>[ @@ -88,6 +91,7 @@ void main() { frameInfo.image.height, ]); } + codec.dispose(); expect( decodedFrameInfos, equals(>[ @@ -115,6 +119,7 @@ void main() { frameInfo.image.height, ]); } + codec.dispose(); expect( decodedFrameInfos, equals(>[ @@ -136,6 +141,7 @@ void main() { } on Exception catch (e) { expect(e.toString(), contains('Decoded image has been disposed')); } + codec.dispose(); }); test('Animated gif can reuse across multiple frames', () async { @@ -153,6 +159,7 @@ void main() { for (int i = 0; i < 4; i++) { frameInfo = await codec.getNextFrame(); } + codec.dispose(); final ui.Image image = frameInfo.image; final ByteData imageData = (await image.toByteData(format: ui.ImageByteFormat.png))!; @@ -180,6 +187,7 @@ void main() { for (int i = 0; i < 69; i++) { frameInfo = await codec.getNextFrame(); } + codec.dispose(); final ui.Image image = frameInfo.image; final ByteData imageData = (await image.toByteData(format: ui.ImageByteFormat.png))!; @@ -220,6 +228,7 @@ void main() { expect(imageData.buffer.asUint8List(), goldenData); } } + codec.dispose(); }); test('Animated apng alpha type handling', () async { @@ -237,6 +246,7 @@ void main() { image = (await codec.getNextFrame()).image; imageData = (await image.toByteData())!; expect(imageData.getUint32(0), 0x99000099); + codec.dispose(); }); test('Animated apng background color restore', () async { @@ -261,6 +271,7 @@ void main() { image = (await codec.getNextFrame()).image; imageData = (await image.toByteData())!; expect(imageData.getUint32(imageData.lengthInBytes - 4), 0x00000000); + codec.dispose(); }); test('Animated apng frame decode does not crash with invalid destination region', () async { @@ -277,6 +288,8 @@ void main() { } else { expect(e.toString(), contains('Codec failed')); } + } finally { + codec.dispose(); } }); @@ -298,6 +311,8 @@ void main() { } else { expect(e.toString(), contains('Codec failed')); } + } finally { + codec.dispose(); } }, ); diff --git a/engine/src/flutter/testing/dart/fragment_shader_test.dart b/engine/src/flutter/testing/dart/fragment_shader_test.dart index 4a76a09c1d..0bfffbf515 100644 --- a/engine/src/flutter/testing/dart/fragment_shader_test.dart +++ b/engine/src/flutter/testing/dart/fragment_shader_test.dart @@ -544,6 +544,7 @@ Future _createBlueGreenImage() async { ); final Codec codec = await descriptor.instantiateCodec(); final FrameInfo frame = await codec.getNextFrame(); + codec.dispose(); return frame.image; } diff --git a/engine/src/flutter/testing/dart/image_descriptor_test.dart b/engine/src/flutter/testing/dart/image_descriptor_test.dart index 2e9ba876b9..7fcf0520fa 100644 --- a/engine/src/flutter/testing/dart/image_descriptor_test.dart +++ b/engine/src/flutter/testing/dart/image_descriptor_test.dart @@ -22,6 +22,7 @@ void main() { final Codec codec = await descriptor.instantiateCodec(); expect(codec.frameCount, 1); + codec.dispose(); }); test('basic image descriptor - encoded - square', () async { @@ -35,6 +36,7 @@ void main() { final Codec codec = await descriptor.instantiateCodec(); expect(codec.frameCount, 1); + codec.dispose(); }); test('basic image descriptor - encoded - animated', () async { @@ -49,6 +51,7 @@ void main() { final Codec codec = await descriptor.instantiateCodec(); expect(codec.frameCount, 4); expect(codec.repetitionCount, -1); + codec.dispose(); }); test('basic image descriptor - raw', () async { @@ -68,6 +71,7 @@ void main() { final Codec codec = await descriptor.instantiateCodec(); expect(codec.frameCount, 1); + codec.dispose(); }); test('HEIC image', () async { @@ -81,6 +85,7 @@ void main() { final Codec codec = await descriptor.instantiateCodec(); expect(codec.frameCount, 1); + codec.dispose(); }, skip: !(Platform.isAndroid || Platform.isIOS || Platform.isMacOS || Platform.isWindows)); } diff --git a/engine/src/flutter/testing/dart/image_dispose_test.dart b/engine/src/flutter/testing/dart/image_dispose_test.dart index c013d1bc8f..c788487f7b 100644 --- a/engine/src/flutter/testing/dart/image_dispose_test.dart +++ b/engine/src/flutter/testing/dart/image_dispose_test.dart @@ -14,6 +14,7 @@ void main() { final Uint8List bytes = await _readFile('2x2.png'); final Codec codec = await instantiateImageCodec(bytes); final FrameInfo frame = await codec.getNextFrame(); + codec.dispose(); expect(frame.image.width, 2); expect(frame.image.height, 2); @@ -33,6 +34,7 @@ void main() { final Uint8List bytes = await _readFile('2x2.png'); final Codec codec = await instantiateImageCodec(bytes); final FrameInfo frame = await codec.getNextFrame(); + codec.dispose(); expect(frame.image.width, 2); expect(frame.image.height, 2); @@ -71,6 +73,7 @@ void main() { final Uint8List bytes = await _readFile('2x2.png'); final Codec codec = await instantiateImageCodec(bytes); final FrameInfo frame = await codec.getNextFrame(); + codec.dispose(); final Image handle1 = frame.image.clone(); final Image handle2 = handle1.clone(); @@ -97,6 +100,7 @@ void main() { final Uint8List bytes = await _readFile('2x2.png'); final Codec codec = await instantiateImageCodec(bytes); final FrameInfo frame = await codec.getNextFrame(); + codec.dispose(); final Image handle1 = frame.image.clone(); final Image handle2 = handle1.clone(); @@ -112,6 +116,7 @@ void main() { final Codec codec2 = await instantiateImageCodec(bytes); final FrameInfo frame2 = await codec2.getNextFrame(); + codec2.dispose(); expect(frame2.image.isCloneOf(frame.image), false); }); @@ -120,6 +125,7 @@ void main() { final Uint8List bytes = await _readFile('2x2.png'); final Codec codec = await instantiateImageCodec(bytes); final FrameInfo frame = await codec.getNextFrame(); + codec.dispose(); expect(frame.image.debugDisposed, false); diff --git a/engine/src/flutter/testing/dart/image_resize_test.dart b/engine/src/flutter/testing/dart/image_resize_test.dart index 262966668e..1049ba6bd0 100644 --- a/engine/src/flutter/testing/dart/image_resize_test.dart +++ b/engine/src/flutter/testing/dart/image_resize_test.dart @@ -15,6 +15,7 @@ void main() { final Uint8List bytes = await readFile('2x2.png'); final Codec codec = await instantiateImageCodec(bytes); final FrameInfo frame = await codec.getNextFrame(); + codec.dispose(); final int codecHeight = frame.image.height; final int codecWidth = frame.image.width; expect(codecHeight, 2); @@ -25,6 +26,7 @@ void main() { final Uint8List bytes = await readFile('2x2.png'); final Codec codec = await instantiateImageCodec(bytes, targetHeight: 1); final FrameInfo frame = await codec.getNextFrame(); + codec.dispose(); final int codecHeight = frame.image.height; final int codecWidth = frame.image.width; expect(codecHeight, 1); @@ -35,6 +37,7 @@ void main() { final Uint8List bytes = await readFile('2x2.png'); final Codec codec = await instantiateImageCodec(bytes, targetWidth: 1); final FrameInfo frame = await codec.getNextFrame(); + codec.dispose(); final int codecHeight = frame.image.height; final int codecWidth = frame.image.width; expect(codecHeight, 1); @@ -45,6 +48,7 @@ void main() { final Uint8List bytes = await readFile('2x2.png'); final Codec codec = await instantiateImageCodec(bytes, targetWidth: 10); final FrameInfo frame = await codec.getNextFrame(); + codec.dispose(); final int codecHeight = frame.image.height; final int codecWidth = frame.image.width; expect(codecHeight, 10); @@ -55,6 +59,7 @@ void main() { final Uint8List bytes = await readFile('2x2.png'); final Codec codec = await instantiateImageCodec(bytes, targetWidth: 10, allowUpscaling: false); final FrameInfo frame = await codec.getNextFrame(); + codec.dispose(); final int codecHeight = frame.image.height; final int codecWidth = frame.image.width; expect(codecHeight, 2); @@ -65,6 +70,7 @@ void main() { final Uint8List bytes = await readFile('2x2.png'); final Codec codec = await instantiateImageCodec(bytes, targetWidth: 10, targetHeight: 1); final FrameInfo frame = await codec.getNextFrame(); + codec.dispose(); final int codecHeight = frame.image.height; final int codecWidth = frame.image.width; expect(codecHeight, 1); @@ -80,6 +86,7 @@ void main() { allowUpscaling: false, ); final FrameInfo frame = await codec.getNextFrame(); + codec.dispose(); final int codecHeight = frame.image.height; final int codecWidth = frame.image.width; expect(codecHeight, 1);