Unverified Commit 3ee473c5 authored by Ian Hickson's avatar Ian Hickson Committed by GitHub

Make decodeImageFromList mockable (#25864)

parent 78f4878f
......@@ -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 {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment