Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions example/android/local.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
sdk.dir=/Users/ironman/Library/Android/sdk
flutter.sdk=/Users/ironman/flutter
sdk.dir=/Users/michaelgobbers/Library/Developer/Xamarin/android-sdk-macosx
flutter.sdk=/Users/michaelgobbers/Library/Developer/flutter
flutter.versionName=1.0.0
flutter.versionCode=1
3 changes: 3 additions & 0 deletions example/ios/Runner/GeneratedPluginRegistrant.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@

#import <Flutter/Flutter.h>

NS_ASSUME_NONNULL_BEGIN

@interface GeneratedPluginRegistrant : NSObject
+ (void)registerWithRegistry:(NSObject<FlutterPluginRegistry>*)registry;
@end

NS_ASSUME_NONNULL_END
#endif /* GeneratedPluginRegistrant_h */
2 changes: 1 addition & 1 deletion example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ description: A new Flutter application.
version: 1.0.0+1

environment:
sdk: ">=2.0.0-dev.68.0 <3.0.0"
sdk: '>=2.12.0 <3.0.0'

dependencies:
flutter:
Expand Down
118 changes: 98 additions & 20 deletions lib/image_test_utils.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'dart:async';
import 'dart:io';

import 'package:mockito/mockito.dart';
import 'package:mocktail/mocktail.dart';

/// Runs [body] in a fresh [Zone] that provides mocked responses for [Image.network] widgets.
///
Expand Down Expand Up @@ -42,47 +42,125 @@ import 'package:mockito/mockito.dart';
///
/// The underlying code is taken from the Flutter repo:
/// https://github.com/flutter/flutter/blob/master/dev/manual_tests/test/mock_image_http.dart
R provideMockedNetworkImages<R>(R body(), {List<int> imageBytes = _transparentImage}) {
R provideMockedNetworkImages<R>(R body(),
{List<int> imageBytes = _transparentImage}) {
return HttpOverrides.runZoned(
body,
createHttpClient: (_) => _createMockImageHttpClient(_, imageBytes),
);
}

class MockHttpClient extends Mock implements HttpClient {}

class MockHttpClientRequest extends Mock implements HttpClientRequest {}

class MockHttpClientResponse extends Mock implements HttpClientResponse {}

class MockHttpHeaders extends Mock implements HttpHeaders {}

// Returns a mock HTTP client that responds with an image to all requests.
MockHttpClient _createMockImageHttpClient(SecurityContext _, List<int> imageBytes) {
MockHttpClient _createMockImageHttpClient(
SecurityContext? _, List<int> imageBytes) {
final MockHttpClient client = MockHttpClient();
final MockHttpClientRequest request = MockHttpClientRequest();
final MockHttpClientResponse response = MockHttpClientResponse();
final MockHttpHeaders headers = MockHttpHeaders();

when(client.getUrl(any)).thenAnswer((_) => Future<HttpClientRequest>.value(request));
when(request.headers).thenReturn(headers);
when(request.close()).thenAnswer((_) => Future<HttpClientResponse>.value(response));
when(response.contentLength).thenReturn(_transparentImage.length);
when(response.statusCode).thenReturn(HttpStatus.ok);
when(response.listen(any)).thenAnswer((Invocation invocation) {
final void Function(List<int>) onData = invocation.positionalArguments[0];
final void Function() onDone = invocation.namedArguments[#onDone];
final void Function(Object, [StackTrace]) onError = invocation.namedArguments[#onError];
final bool cancelOnError = invocation.namedArguments[#cancelOnError];
when(() => client.getUrl(any()))
.thenAnswer((_) => Future<HttpClientRequest>.value(request));
when(() => client.getUrl(any()))
.thenAnswer((_) => Future<HttpClientRequest>.value(request));
when(() => request.headers).thenReturn(headers);
when(() => request.close())
.thenAnswer((_) => Future<HttpClientResponse>.value(response));
when(() => response.contentLength).thenReturn(_transparentImage.length);
when(() => response.compressionState)
.thenReturn(HttpClientResponseCompressionState.notCompressed);
when(() => response.statusCode).thenReturn(HttpStatus.ok);
when(() => response.listen(any(),
onError: any(named: "onError"),
onDone: any(named: "onDone"),
cancelOnError: any(named: "cancelOnError")))
.thenAnswer((Invocation invocation) {
final void Function(List<int>)? onData = invocation.positionalArguments[0];
final void Function()? onDone = invocation.namedArguments[#onDone];
final void Function(Object, [StackTrace?])? onError =
invocation.namedArguments[#onError];
final bool? cancelOnError = invocation.namedArguments[#cancelOnError];

return Stream<List<int>>.fromIterable(<List<int>>[imageBytes])
.listen(onData, onDone: onDone, onError: onError, cancelOnError: cancelOnError);
return Stream<List<int>>.fromIterable(<List<int>>[imageBytes]).listen(
onData,
onDone: onDone,
onError: onError,
cancelOnError: cancelOnError);
});

return client;
}

const List<int> _transparentImage = const <int>[
0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A, 0x00, 0x00, 0x00, 0x0D, 0x49,
0x48, 0x44, 0x52, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x08, 0x06,
0x00, 0x00, 0x00, 0x1F, 0x15, 0xC4, 0x89, 0x00, 0x00, 0x00, 0x0A, 0x49, 0x44,
0x41, 0x54, 0x78, 0x9C, 0x63, 0x00, 0x01, 0x00, 0x00, 0x05, 0x00, 0x01, 0x0D,
0x0A, 0x2D, 0xB4, 0x00, 0x00, 0x00, 0x00, 0x49, 0x45, 0x4E, 0x44, 0xAE,
0x89,
0x50,
0x4E,
0x47,
0x0D,
0x0A,
0x1A,
0x0A,
0x00,
0x00,
0x00,
0x0D,
0x49,
0x48,
0x44,
0x52,
0x00,
0x00,
0x00,
0x01,
0x00,
0x00,
0x00,
0x01,
0x08,
0x06,
0x00,
0x00,
0x00,
0x1F,
0x15,
0xC4,
0x89,
0x00,
0x00,
0x00,
0x0A,
0x49,
0x44,
0x41,
0x54,
0x78,
0x9C,
0x63,
0x00,
0x01,
0x00,
0x00,
0x05,
0x00,
0x01,
0x0D,
0x0A,
0x2D,
0xB4,
0x00,
0x00,
0x00,
0x00,
0x49,
0x45,
0x4E,
0x44,
0xAE,
];
4 changes: 2 additions & 2 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ homepage: https://github.com/roughike/image_test_utils
author: Iiro Krankka <iiro.krankka@gmail.com>

environment:
sdk: '>=2.0.0 <3.0.0'
sdk: '>=2.12.0 <3.0.0'

dependencies:
mockito: ^3.0.0
mocktail: ^0.1.4