Skip to content

Commit 2919003

Browse files
authored
fix: Call codec.dispose in tests of engine/src/flutter (flutter#161115)
PR derived from flutter#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]. <!-- Links --> [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
1 parent d59995b commit 2919003

File tree

9 files changed

+54
-11
lines changed

9 files changed

+54
-11
lines changed

engine/src/flutter/lib/web_ui/test/canvaskit/image_golden_test.dart

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ import 'package:ui/ui.dart' as ui;
1414
import 'common.dart';
1515
import 'test_data.dart';
1616

17-
List<TestCodec>? testCodecs;
18-
1917
void main() {
2018
internalBootstrapBrowserTest(() => testMain);
2119
}
@@ -29,6 +27,11 @@ abstract class TestCodec {
2927
Future<ui.Codec> getCodec() async => _cachedCodec ??= await createCodec();
3028

3129
Future<ui.Codec> createCodec();
30+
31+
void dispose() {
32+
_cachedCodec?.dispose();
33+
_cachedCodec = null;
34+
}
3235
}
3336

3437
abstract class TestFileCodec extends TestCodec {
@@ -134,13 +137,10 @@ class BitmapSingleFrameCodec implements ui.Codec {
134137
}
135138

136139
Future<void> testMain() async {
137-
Future<List<TestCodec>> createTestCodecs({
138-
int testTargetWidth = 300,
139-
int testTargetHeight = 300,
140-
}) async {
141-
final HttpFetchResponse listingResponse = await httpFetch('/test_images/');
142-
final List<String> testFiles = (await listingResponse.json() as List<dynamic>).cast<String>();
140+
final HttpFetchResponse listingResponse = await httpFetch('/test_images/');
141+
final List<String> testFiles = (await listingResponse.json() as List<dynamic>).cast<String>();
143142

143+
List<TestCodec> createTestCodecs({int testTargetWidth = 300, int testTargetHeight = 300}) {
144144
// Sanity-check the test file list. If suddenly test files are moved or
145145
// deleted, and the test server returns an empty list, or is missing some
146146
// important test files, we want to know.
@@ -192,8 +192,6 @@ Future<void> testMain() async {
192192
return testCodecs;
193193
}
194194

195-
testCodecs = await createTestCodecs();
196-
197195
group('CanvasKit Images', () {
198196
setUpCanvasKitTest(withImplicitView: true);
199197

@@ -202,7 +200,8 @@ Future<void> testMain() async {
202200
});
203201

204202
group('Codecs', () {
205-
for (final TestCodec testCodec in testCodecs!) {
203+
final List<TestCodec> testCodecs = createTestCodecs();
204+
for (final TestCodec testCodec in testCodecs) {
206205
test('${testCodec.description} can create an image', () async {
207206
try {
208207
final ui.Codec codec = await testCodec.getCodec();
@@ -247,6 +246,9 @@ Future<void> testMain() async {
247246
);
248247
});
249248
}
249+
for (final testCodec in testCodecs) {
250+
testCodec.dispose();
251+
}
250252
});
251253

252254
test('crossOrigin requests cause an error', () async {

engine/src/flutter/lib/web_ui/test/html/drawing/canvas_draw_image_golden_test.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ Future<void> testMain() async {
3939
final Uint8List imageData = base64Decode(base64PngData);
4040
final Codec codec = await instantiateImageCodec(imageData);
4141
final FrameInfo frameInfo = await codec.getNextFrame();
42+
codec.dispose();
4243

4344
const Rect bounds = Rect.fromLTRB(0, 0, 400, 300);
4445
final EnginePictureRecorder recorder = EnginePictureRecorder();

engine/src/flutter/lib/web_ui/test/ui/filters_test.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ Future<void> testMain() async {
3030
expect(codec.frameCount, 1);
3131

3232
final ui.FrameInfo info = await codec.getNextFrame();
33+
codec.dispose();
3334
final ui.Image image = info.image;
3435
expect(image.width, 128);
3536
expect(image.height, 128);

engine/src/flutter/lib/web_ui/test/ui/image_golden_test.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,7 @@ Future<void> testMain() async {
381381
expect(codec.frameCount, 1);
382382

383383
final ui.FrameInfo info = await codec.getNextFrame();
384+
codec.dispose();
384385
return info.image;
385386
});
386387

@@ -393,6 +394,7 @@ Future<void> testMain() async {
393394
expect(codec.frameCount, 1);
394395

395396
final ui.FrameInfo info = await codec.getNextFrame();
397+
codec.dispose();
396398
expect(info.image.width, 3024);
397399
expect(info.image.height, 4032);
398400
});
@@ -424,6 +426,7 @@ Future<void> testMain() async {
424426
await completer.future;
425427

426428
final DomImageBitmap bitmap = await createImageBitmap(image as JSObject);
429+
domWindow.URL.revokeObjectURL(url);
427430

428431
expect(bitmap.width.toDartInt, 150);
429432
expect(bitmap.height.toDartInt, 150);
@@ -471,6 +474,7 @@ Future<void> testMain() async {
471474
height: 150,
472475
transferOwnership: false,
473476
);
477+
domWindow.URL.revokeObjectURL(url);
474478
return uiImage;
475479
});
476480
}
@@ -485,6 +489,7 @@ Future<void> testMain() async {
485489
expect(codec.frameCount, 1);
486490

487491
final ui.FrameInfo info = await codec.getNextFrame();
492+
codec.dispose();
488493
return info.image;
489494
});
490495
}

engine/src/flutter/testing/dart/codec_test.dart

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ void main() {
4949
} else {
5050
expect(e.toString(), contains('Codec failed'));
5151
}
52+
} finally {
53+
codec.dispose();
5254
}
5355
});
5456

@@ -64,6 +66,7 @@ void main() {
6466
frameInfo.image.height,
6567
]);
6668
}
69+
codec.dispose();
6770
expect(
6871
decodedFrameInfos,
6972
equals(<List<int>>[
@@ -88,6 +91,7 @@ void main() {
8891
frameInfo.image.height,
8992
]);
9093
}
94+
codec.dispose();
9195
expect(
9296
decodedFrameInfos,
9397
equals(<List<int>>[
@@ -115,6 +119,7 @@ void main() {
115119
frameInfo.image.height,
116120
]);
117121
}
122+
codec.dispose();
118123
expect(
119124
decodedFrameInfos,
120125
equals(<List<int>>[
@@ -136,6 +141,7 @@ void main() {
136141
} on Exception catch (e) {
137142
expect(e.toString(), contains('Decoded image has been disposed'));
138143
}
144+
codec.dispose();
139145
});
140146

141147
test('Animated gif can reuse across multiple frames', () async {
@@ -153,6 +159,7 @@ void main() {
153159
for (int i = 0; i < 4; i++) {
154160
frameInfo = await codec.getNextFrame();
155161
}
162+
codec.dispose();
156163

157164
final ui.Image image = frameInfo.image;
158165
final ByteData imageData = (await image.toByteData(format: ui.ImageByteFormat.png))!;
@@ -180,6 +187,7 @@ void main() {
180187
for (int i = 0; i < 69; i++) {
181188
frameInfo = await codec.getNextFrame();
182189
}
190+
codec.dispose();
183191

184192
final ui.Image image = frameInfo.image;
185193
final ByteData imageData = (await image.toByteData(format: ui.ImageByteFormat.png))!;
@@ -220,6 +228,7 @@ void main() {
220228
expect(imageData.buffer.asUint8List(), goldenData);
221229
}
222230
}
231+
codec.dispose();
223232
});
224233

225234
test('Animated apng alpha type handling', () async {
@@ -237,6 +246,7 @@ void main() {
237246
image = (await codec.getNextFrame()).image;
238247
imageData = (await image.toByteData())!;
239248
expect(imageData.getUint32(0), 0x99000099);
249+
codec.dispose();
240250
});
241251

242252
test('Animated apng background color restore', () async {
@@ -261,6 +271,7 @@ void main() {
261271
image = (await codec.getNextFrame()).image;
262272
imageData = (await image.toByteData())!;
263273
expect(imageData.getUint32(imageData.lengthInBytes - 4), 0x00000000);
274+
codec.dispose();
264275
});
265276

266277
test('Animated apng frame decode does not crash with invalid destination region', () async {
@@ -277,6 +288,8 @@ void main() {
277288
} else {
278289
expect(e.toString(), contains('Codec failed'));
279290
}
291+
} finally {
292+
codec.dispose();
280293
}
281294
});
282295

@@ -298,6 +311,8 @@ void main() {
298311
} else {
299312
expect(e.toString(), contains('Codec failed'));
300313
}
314+
} finally {
315+
codec.dispose();
301316
}
302317
},
303318
);

engine/src/flutter/testing/dart/fragment_shader_test.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -544,6 +544,7 @@ Future<Image> _createBlueGreenImage() async {
544544
);
545545
final Codec codec = await descriptor.instantiateCodec();
546546
final FrameInfo frame = await codec.getNextFrame();
547+
codec.dispose();
547548
return frame.image;
548549
}
549550

engine/src/flutter/testing/dart/image_descriptor_test.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ void main() {
2222

2323
final Codec codec = await descriptor.instantiateCodec();
2424
expect(codec.frameCount, 1);
25+
codec.dispose();
2526
});
2627

2728
test('basic image descriptor - encoded - square', () async {
@@ -35,6 +36,7 @@ void main() {
3536

3637
final Codec codec = await descriptor.instantiateCodec();
3738
expect(codec.frameCount, 1);
39+
codec.dispose();
3840
});
3941

4042
test('basic image descriptor - encoded - animated', () async {
@@ -49,6 +51,7 @@ void main() {
4951
final Codec codec = await descriptor.instantiateCodec();
5052
expect(codec.frameCount, 4);
5153
expect(codec.repetitionCount, -1);
54+
codec.dispose();
5255
});
5356

5457
test('basic image descriptor - raw', () async {
@@ -68,6 +71,7 @@ void main() {
6871

6972
final Codec codec = await descriptor.instantiateCodec();
7073
expect(codec.frameCount, 1);
74+
codec.dispose();
7175
});
7276

7377
test('HEIC image', () async {
@@ -81,6 +85,7 @@ void main() {
8185

8286
final Codec codec = await descriptor.instantiateCodec();
8387
expect(codec.frameCount, 1);
88+
codec.dispose();
8489
}, skip: !(Platform.isAndroid || Platform.isIOS || Platform.isMacOS || Platform.isWindows));
8590
}
8691

engine/src/flutter/testing/dart/image_dispose_test.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ void main() {
1414
final Uint8List bytes = await _readFile('2x2.png');
1515
final Codec codec = await instantiateImageCodec(bytes);
1616
final FrameInfo frame = await codec.getNextFrame();
17+
codec.dispose();
1718

1819
expect(frame.image.width, 2);
1920
expect(frame.image.height, 2);
@@ -33,6 +34,7 @@ void main() {
3334
final Uint8List bytes = await _readFile('2x2.png');
3435
final Codec codec = await instantiateImageCodec(bytes);
3536
final FrameInfo frame = await codec.getNextFrame();
37+
codec.dispose();
3638

3739
expect(frame.image.width, 2);
3840
expect(frame.image.height, 2);
@@ -71,6 +73,7 @@ void main() {
7173
final Uint8List bytes = await _readFile('2x2.png');
7274
final Codec codec = await instantiateImageCodec(bytes);
7375
final FrameInfo frame = await codec.getNextFrame();
76+
codec.dispose();
7477

7578
final Image handle1 = frame.image.clone();
7679
final Image handle2 = handle1.clone();
@@ -97,6 +100,7 @@ void main() {
97100
final Uint8List bytes = await _readFile('2x2.png');
98101
final Codec codec = await instantiateImageCodec(bytes);
99102
final FrameInfo frame = await codec.getNextFrame();
103+
codec.dispose();
100104

101105
final Image handle1 = frame.image.clone();
102106
final Image handle2 = handle1.clone();
@@ -112,6 +116,7 @@ void main() {
112116

113117
final Codec codec2 = await instantiateImageCodec(bytes);
114118
final FrameInfo frame2 = await codec2.getNextFrame();
119+
codec2.dispose();
115120

116121
expect(frame2.image.isCloneOf(frame.image), false);
117122
});
@@ -120,6 +125,7 @@ void main() {
120125
final Uint8List bytes = await _readFile('2x2.png');
121126
final Codec codec = await instantiateImageCodec(bytes);
122127
final FrameInfo frame = await codec.getNextFrame();
128+
codec.dispose();
123129

124130
expect(frame.image.debugDisposed, false);
125131

engine/src/flutter/testing/dart/image_resize_test.dart

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ void main() {
1515
final Uint8List bytes = await readFile('2x2.png');
1616
final Codec codec = await instantiateImageCodec(bytes);
1717
final FrameInfo frame = await codec.getNextFrame();
18+
codec.dispose();
1819
final int codecHeight = frame.image.height;
1920
final int codecWidth = frame.image.width;
2021
expect(codecHeight, 2);
@@ -25,6 +26,7 @@ void main() {
2526
final Uint8List bytes = await readFile('2x2.png');
2627
final Codec codec = await instantiateImageCodec(bytes, targetHeight: 1);
2728
final FrameInfo frame = await codec.getNextFrame();
29+
codec.dispose();
2830
final int codecHeight = frame.image.height;
2931
final int codecWidth = frame.image.width;
3032
expect(codecHeight, 1);
@@ -35,6 +37,7 @@ void main() {
3537
final Uint8List bytes = await readFile('2x2.png');
3638
final Codec codec = await instantiateImageCodec(bytes, targetWidth: 1);
3739
final FrameInfo frame = await codec.getNextFrame();
40+
codec.dispose();
3841
final int codecHeight = frame.image.height;
3942
final int codecWidth = frame.image.width;
4043
expect(codecHeight, 1);
@@ -45,6 +48,7 @@ void main() {
4548
final Uint8List bytes = await readFile('2x2.png');
4649
final Codec codec = await instantiateImageCodec(bytes, targetWidth: 10);
4750
final FrameInfo frame = await codec.getNextFrame();
51+
codec.dispose();
4852
final int codecHeight = frame.image.height;
4953
final int codecWidth = frame.image.width;
5054
expect(codecHeight, 10);
@@ -55,6 +59,7 @@ void main() {
5559
final Uint8List bytes = await readFile('2x2.png');
5660
final Codec codec = await instantiateImageCodec(bytes, targetWidth: 10, allowUpscaling: false);
5761
final FrameInfo frame = await codec.getNextFrame();
62+
codec.dispose();
5863
final int codecHeight = frame.image.height;
5964
final int codecWidth = frame.image.width;
6065
expect(codecHeight, 2);
@@ -65,6 +70,7 @@ void main() {
6570
final Uint8List bytes = await readFile('2x2.png');
6671
final Codec codec = await instantiateImageCodec(bytes, targetWidth: 10, targetHeight: 1);
6772
final FrameInfo frame = await codec.getNextFrame();
73+
codec.dispose();
6874
final int codecHeight = frame.image.height;
6975
final int codecWidth = frame.image.width;
7076
expect(codecHeight, 1);
@@ -80,6 +86,7 @@ void main() {
8086
allowUpscaling: false,
8187
);
8288
final FrameInfo frame = await codec.getNextFrame();
89+
codec.dispose();
8390
final int codecHeight = frame.image.height;
8491
final int codecWidth = frame.image.width;
8592
expect(codecHeight, 1);

0 commit comments

Comments
 (0)