Skip to content

Commit

Permalink
Prepare images for tests individually to enable clean up of cache. (#…
Browse files Browse the repository at this point in the history
…149693)
  • Loading branch information
polina-c authored Jun 7, 2024
1 parent 1fdb40e commit 35a4933
Showing 1 changed file with 20 additions and 12 deletions.
32 changes: 20 additions & 12 deletions packages/flutter/test/widgets/box_decoration_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import 'dart:ui' as ui show Image;
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:leak_tracker_flutter_testing/leak_tracker_flutter_testing.dart';

import '../image_data.dart';

Expand All @@ -18,7 +17,13 @@ class TestImageProvider extends ImageProvider<TestImageProvider> {

final Future<void> future;

static late ui.Image image;
static final List<ui.Image> _images = <ui.Image>[];

static Future<void> prepareImages(int count) async {
for (int i = 0; i < count; i++) {
_images.add(await decodeImageFromList(Uint8List.fromList(kTransparentImage)));
}
}

@override
Future<TestImageProvider> obtainKey(ImageConfiguration configuration) {
Expand All @@ -27,20 +32,25 @@ class TestImageProvider extends ImageProvider<TestImageProvider> {

@override
ImageStreamCompleter loadImage(TestImageProvider key, ImageDecoderCallback decode) {
assert(_images.isNotEmpty, 'ask for more images in `prepareImages`');
final ui.Image image = _images.last;
_images.removeLast();

return OneFrameImageStreamCompleter(
future.then<ImageInfo>((void value) => ImageInfo(image: image)),
future.then<ImageInfo>((void value) {
final ImageInfo result = ImageInfo(image: image);
return result;
}),
);
}
}

Future<void> main() async {
AutomatedTestWidgetsFlutterBinding();
TestImageProvider.image = await decodeImageFromList(Uint8List.fromList(kTransparentImage));
await TestImageProvider.prepareImages(2);

testWidgets('DecoratedBox handles loading images',
// TODO(polina-c): dispose ImageStreamCompleterHandle, https://github.com/flutter/flutter/issues/145599 [leaks-to-clean]
experimentalLeakTesting: LeakTesting.settings.withIgnoredAll(),
(WidgetTester tester) async {
testWidgets('DecoratedBox handles loading images', (WidgetTester tester) async {
addTearDown(imageCache.clear);
final GlobalKey key = GlobalKey();
final Completer<void> completer = Completer<void>();
await tester.pumpWidget(
Expand All @@ -63,10 +73,8 @@ Future<void> main() async {
expect(tester.binding.hasScheduledFrame, isFalse);
});

testWidgets('Moving a DecoratedBox',
// TODO(polina-c): dispose ImageStreamCompleterHandle, https://github.com/flutter/flutter/issues/145599 [leaks-to-clean]
experimentalLeakTesting: LeakTesting.settings.withIgnoredAll(),
(WidgetTester tester) async {
testWidgets('Moving a DecoratedBox', (WidgetTester tester) async {
addTearDown(imageCache.clear);
final Completer<void> completer = Completer<void>();
final Widget subtree = KeyedSubtree(
key: GlobalKey(),
Expand Down

0 comments on commit 35a4933

Please sign in to comment.