Unverified Commit 26271223 authored by Dan Field's avatar Dan Field Committed by GitHub

Throw when trying to load an empty file (#50325)

parent f118b638
...@@ -746,7 +746,7 @@ class FileImage extends ImageProvider<FileImage> { ...@@ -746,7 +746,7 @@ class FileImage extends ImageProvider<FileImage> {
final Uint8List bytes = await file.readAsBytes(); final Uint8List bytes = await file.readAsBytes();
if (bytes.lengthInBytes == 0) if (bytes.lengthInBytes == 0)
return null; throw StateError('$file is empty and cannot be loaded as an image.');
return await decode(bytes); return await decode(bytes);
} }
......
...@@ -7,6 +7,7 @@ import 'dart:io'; ...@@ -7,6 +7,7 @@ import 'dart:io';
import 'dart:math' as math; import 'dart:math' as math;
import 'dart:typed_data'; import 'dart:typed_data';
import 'package:file/memory.dart';
import 'package:flutter/foundation.dart'; import 'package:flutter/foundation.dart';
import 'package:flutter/painting.dart'; import 'package:flutter/painting.dart';
import 'package:flutter_test/flutter_test.dart'; import 'package:flutter_test/flutter_test.dart';
...@@ -22,11 +23,20 @@ void main() { ...@@ -22,11 +23,20 @@ void main() {
return PaintingBinding.instance.instantiateImageCodec(bytes, cacheWidth: cacheWidth, cacheHeight: cacheHeight); return PaintingBinding.instance.instantiateImageCodec(bytes, cacheWidth: cacheWidth, cacheHeight: cacheHeight);
}; };
group(ImageProvider, () { setUpAll(() {
setUpAll(() { TestRenderingFlutterBinding(); // initializes the imageCache
TestRenderingFlutterBinding(); // initializes the imageCache });
});
FlutterExceptionHandler oldError;
setUp(() {
oldError = FlutterError.onError;
});
tearDown(() {
FlutterError.onError = oldError;
});
group('ImageProvider', () {
group('Image cache', () { group('Image cache', () {
tearDown(() { tearDown(() {
imageCache.clear(); imageCache.clear();
...@@ -147,6 +157,21 @@ void main() { ...@@ -147,6 +157,21 @@ void main() {
expect(uncaught, false); expect(uncaught, false);
}); });
test('File image with empty file throws expected error - (image cache)', () async {
final Completer<StateError> error = Completer<StateError>();
FlutterError.onError = (FlutterErrorDetails details) {
print(details.exception);
error.complete(details.exception as StateError);
};
final MemoryFileSystem fs = MemoryFileSystem();
final File file = fs.file('/empty.png')..createSync(recursive: true);
final FileImage provider = FileImage(file);
provider.resolve(ImageConfiguration.empty);
expect(await error.future, isStateError);
});
group(NetworkImage, () { group(NetworkImage, () {
MockHttpClient httpClient; MockHttpClient httpClient;
...@@ -359,6 +384,20 @@ void main() { ...@@ -359,6 +384,20 @@ void main() {
resizeImage.load(await resizeImage.obtainKey(ImageConfiguration.empty), decode); resizeImage.load(await resizeImage.obtainKey(ImageConfiguration.empty), decode);
}); });
test('File image with empty file throws expected error (load)', () async {
final Completer<StateError> error = Completer<StateError>();
FlutterError.onError = (FlutterErrorDetails details) {
error.complete(details.exception as StateError);
};
final MemoryFileSystem fs = MemoryFileSystem();
final File file = fs.file('/empty.png')..createSync(recursive: true);
final FileImage provider = FileImage(file);
expect(provider.load(provider, null), isA<MultiFrameImageStreamCompleter>());
expect(await error.future, isStateError);
});
} }
Future<Size> _resolveAndGetSize(ImageProvider imageProvider, Future<Size> _resolveAndGetSize(ImageProvider imageProvider,
......
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