Make decodeImageFromList mockable (#25864)
This commit is contained in:
parent
78f4878fe1
commit
3ee473c587
@ -4,15 +4,24 @@
|
||||
|
||||
import 'dart:async';
|
||||
import 'dart:typed_data';
|
||||
import 'dart:ui' as ui show Image, decodeImageFromList;
|
||||
import 'dart:ui' as ui show Codec, FrameInfo, Image;
|
||||
|
||||
import 'binding.dart';
|
||||
|
||||
/// Creates an image from a list of bytes.
|
||||
///
|
||||
/// This function attempts to interpret the given bytes an image. If successful,
|
||||
/// the returned [Future] resolves to the decoded image. Otherwise, the [Future]
|
||||
/// resolves to null.
|
||||
Future<ui.Image> decodeImageFromList(Uint8List list) {
|
||||
final Completer<ui.Image> completer = Completer<ui.Image>();
|
||||
ui.decodeImageFromList(list, completer.complete);
|
||||
return completer.future;
|
||||
///
|
||||
/// If the image is animated, this returns the first frame. Consider
|
||||
/// [instantiateImageCodec] if support for animated images is necessary.
|
||||
///
|
||||
/// This function differs from [ui.decodeImageFromList] in that it defers to
|
||||
/// [PaintingBinding.instantiateImageCodec], and therefore can be mocked in
|
||||
/// tests.
|
||||
Future<ui.Image> decodeImageFromList(Uint8List bytes) async {
|
||||
final ui.Codec codec = await PaintingBinding.instance.instantiateImageCodec(bytes);
|
||||
final ui.FrameInfo frameInfo = await codec.getNextFrame();
|
||||
return frameInfo.image;
|
||||
}
|
||||
|
@ -6,15 +6,19 @@ import 'dart:typed_data';
|
||||
import 'dart:ui' as ui;
|
||||
|
||||
import 'package:flutter/painting.dart';
|
||||
import '../flutter_test_alternative.dart';
|
||||
|
||||
import '../flutter_test_alternative.dart';
|
||||
import 'binding_test.dart';
|
||||
import 'image_data.dart';
|
||||
|
||||
void main() {
|
||||
final PaintingBindingSpy binding = PaintingBindingSpy();
|
||||
test('Image decoder control test', () async {
|
||||
expect(binding.instantiateImageCodecCalledCount, 0);
|
||||
final ui.Image image = await decodeImageFromList(Uint8List.fromList(kTransparentImage));
|
||||
expect(image, isNotNull);
|
||||
expect(image.width, 1);
|
||||
expect(image.height, 1);
|
||||
expect(binding.instantiateImageCodecCalledCount, 1);
|
||||
});
|
||||
}
|
||||
|
@ -36,6 +36,7 @@ class TestImageProvider extends ImageProvider<TestImageProvider> {
|
||||
}
|
||||
|
||||
Future<void> main() async {
|
||||
AutomatedTestWidgetsFlutterBinding();
|
||||
TestImageProvider.image = await decodeImageFromList(Uint8List.fromList(kTransparentImage));
|
||||
|
||||
testWidgets('DecoratedBox handles loading images', (WidgetTester tester) async {
|
||||
|
@ -14,6 +14,7 @@ import '../rendering/mock_canvas.dart';
|
||||
import 'test_border.dart' show TestBorder;
|
||||
|
||||
Future<void> main() async {
|
||||
AutomatedTestWidgetsFlutterBinding();
|
||||
final ui.Image rawImage = await decodeImageFromList(Uint8List.fromList(kTransparentImage));
|
||||
final ImageProvider image = TestImageProvider(0, 0, image: rawImage);
|
||||
testWidgets('ShapeDecoration.image', (WidgetTester tester) async {
|
||||
|
Loading…
x
Reference in New Issue
Block a user