From 47896834a50a00a350b51907295e8902360124b2 Mon Sep 17 00:00:00 2001 From: ivan_horchakov Date: Tue, 8 Dec 2020 18:45:11 +0200 Subject: [PATCH 1/7] Updated to mockito: ^4.1.1 --- example/ios/Runner/GeneratedPluginRegistrant.h | 3 +++ pubspec.yaml | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/example/ios/Runner/GeneratedPluginRegistrant.h b/example/ios/Runner/GeneratedPluginRegistrant.h index 3b700eb..ed9a5c6 100644 --- a/example/ios/Runner/GeneratedPluginRegistrant.h +++ b/example/ios/Runner/GeneratedPluginRegistrant.h @@ -7,8 +7,11 @@ #import +NS_ASSUME_NONNULL_BEGIN + @interface GeneratedPluginRegistrant : NSObject + (void)registerWithRegistry:(NSObject*)registry; @end +NS_ASSUME_NONNULL_END #endif /* GeneratedPluginRegistrant_h */ diff --git a/pubspec.yaml b/pubspec.yaml index e291ee3..7b34883 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -8,4 +8,4 @@ environment: sdk: '>=2.0.0 <3.0.0' dependencies: - mockito: ^3.0.0 + mockito: ^4.1.1 From 3bf09149d5498292db90942693c561bbf559d1ec Mon Sep 17 00:00:00 2001 From: Michael Gobbers Date: Tue, 1 Jun 2021 11:00:08 +0200 Subject: [PATCH 2/7] null safety migration --- example/android/local.properties | 4 ++-- example/ios/Runner/GeneratedPluginRegistrant.h | 3 +++ example/pubspec.yaml | 2 +- lib/image_test_utils.dart | 12 ++++++------ pubspec.yaml | 4 ++-- 5 files changed, 14 insertions(+), 11 deletions(-) diff --git a/example/android/local.properties b/example/android/local.properties index 02a5a1e..5004041 100644 --- a/example/android/local.properties +++ b/example/android/local.properties @@ -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 \ No newline at end of file diff --git a/example/ios/Runner/GeneratedPluginRegistrant.h b/example/ios/Runner/GeneratedPluginRegistrant.h index 3b700eb..ed9a5c6 100644 --- a/example/ios/Runner/GeneratedPluginRegistrant.h +++ b/example/ios/Runner/GeneratedPluginRegistrant.h @@ -7,8 +7,11 @@ #import +NS_ASSUME_NONNULL_BEGIN + @interface GeneratedPluginRegistrant : NSObject + (void)registerWithRegistry:(NSObject*)registry; @end +NS_ASSUME_NONNULL_END #endif /* GeneratedPluginRegistrant_h */ diff --git a/example/pubspec.yaml b/example/pubspec.yaml index 95e5eb4..f746fad 100644 --- a/example/pubspec.yaml +++ b/example/pubspec.yaml @@ -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: diff --git a/lib/image_test_utils.dart b/lib/image_test_utils.dart index 5fa03ff..e909232 100644 --- a/lib/image_test_utils.dart +++ b/lib/image_test_utils.dart @@ -55,22 +55,22 @@ 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 imageBytes) { +MockHttpClient _createMockImageHttpClient(SecurityContext? _, List imageBytes) { final MockHttpClient client = MockHttpClient(); final MockHttpClientRequest request = MockHttpClientRequest(); final MockHttpClientResponse response = MockHttpClientResponse(); final MockHttpHeaders headers = MockHttpHeaders(); - when(client.getUrl(any)).thenAnswer((_) => Future.value(request)); + when(client.getUrl(any!)).thenAnswer((_) => Future.value(request)); when(request.headers).thenReturn(headers); when(request.close()).thenAnswer((_) => Future.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) 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]; + final void Function(List)? 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>.fromIterable(>[imageBytes]) .listen(onData, onDone: onDone, onError: onError, cancelOnError: cancelOnError); diff --git a/pubspec.yaml b/pubspec.yaml index e291ee3..a9e3bc4 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -5,7 +5,7 @@ homepage: https://github.com/roughike/image_test_utils author: Iiro Krankka environment: - sdk: '>=2.0.0 <3.0.0' + sdk: '>=2.12.0 <3.0.0' dependencies: - mockito: ^3.0.0 + mockito: ^5.0.0-nullsafety.0 From ce2529874de5875d2991ae2513f2ee97665de2b7 Mon Sep 17 00:00:00 2001 From: Michael Gobbers Date: Tue, 6 Jul 2021 16:04:19 +0200 Subject: [PATCH 3/7] introduced mocktail for mocking --- lib/image_test_utils.dart | 14 +++++++------- pubspec.yaml | 2 +- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/lib/image_test_utils.dart b/lib/image_test_utils.dart index e909232..c4bc506 100644 --- a/lib/image_test_utils.dart +++ b/lib/image_test_utils.dart @@ -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. /// @@ -61,12 +61,12 @@ MockHttpClient _createMockImageHttpClient(SecurityContext? _, List imageByt final MockHttpClientResponse response = MockHttpClientResponse(); final MockHttpHeaders headers = MockHttpHeaders(); - when(client.getUrl(any!)).thenAnswer((_) => Future.value(request)); - when(request.headers).thenReturn(headers); - when(request.close()).thenAnswer((_) => Future.value(response)); - when(response.contentLength).thenReturn(_transparentImage.length); - when(response.statusCode).thenReturn(HttpStatus.ok); - when(response.listen(any)).thenAnswer((Invocation invocation) { + when(() => client.getUrl(any())).thenAnswer((_) => Future.value(request)); + when(() => request.headers).thenReturn(headers); + when(() => request.close()).thenAnswer((_) => Future.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)? onData = invocation.positionalArguments[0]; final void Function()? onDone = invocation.namedArguments[#onDone]; final void Function(Object, [StackTrace?])? onError = invocation.namedArguments[#onError]; diff --git a/pubspec.yaml b/pubspec.yaml index a9e3bc4..e1d4731 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -8,4 +8,4 @@ environment: sdk: '>=2.12.0 <3.0.0' dependencies: - mockito: ^5.0.0-nullsafety.0 + mocktail: ^0.1.4 From b3d04954710a12653755398850d9c2925d3af2d1 Mon Sep 17 00:00:00 2001 From: Michael Gobbers Date: Wed, 7 Jul 2021 10:06:06 +0200 Subject: [PATCH 4/7] added support for compression state --- lib/image_test_utils.dart | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/image_test_utils.dart b/lib/image_test_utils.dart index c4bc506..6b4dc54 100644 --- a/lib/image_test_utils.dart +++ b/lib/image_test_utils.dart @@ -61,10 +61,12 @@ MockHttpClient _createMockImageHttpClient(SecurityContext? _, List imageByt final MockHttpClientResponse response = MockHttpClientResponse(); final MockHttpHeaders headers = MockHttpHeaders(); + when(() => client.getUrl(any())).thenAnswer((_) => Future.value(request)); when(() => client.getUrl(any())).thenAnswer((_) => Future.value(request)); when(() => request.headers).thenReturn(headers); when(() => request.close()).thenAnswer((_) => Future.value(response)); when(() => response.contentLength).thenReturn(_transparentImage.length); + when(() => response.compressionState).thenReturn(HttpClientResponseCompressionState.notCompressed); when(() => response.statusCode).thenReturn(HttpStatus.ok); when(() => response.listen(any())).thenAnswer((Invocation invocation) { final void Function(List)? onData = invocation.positionalArguments[0]; From c339a18b4cd2175aaf73e8312988e31c06110d8b Mon Sep 17 00:00:00 2001 From: Michael Gobbers Date: Wed, 7 Jul 2021 10:55:55 +0200 Subject: [PATCH 5/7] changed mock on response.listen to include error and done callback params --- lib/image_test_utils.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/image_test_utils.dart b/lib/image_test_utils.dart index 6b4dc54..0be6e63 100644 --- a/lib/image_test_utils.dart +++ b/lib/image_test_utils.dart @@ -68,7 +68,7 @@ MockHttpClient _createMockImageHttpClient(SecurityContext? _, List imageByt when(() => response.contentLength).thenReturn(_transparentImage.length); when(() => response.compressionState).thenReturn(HttpClientResponseCompressionState.notCompressed); when(() => response.statusCode).thenReturn(HttpStatus.ok); - when(() => response.listen(any())).thenAnswer((Invocation invocation) { + when(() => response.listen(any(), onError: any(), onDone: any())).thenAnswer((Invocation invocation) { final void Function(List)? onData = invocation.positionalArguments[0]; final void Function()? onDone = invocation.namedArguments[#onDone]; final void Function(Object, [StackTrace?])? onError = invocation.namedArguments[#onError]; From e302ec27eeeabd70a17715c1f05e4a7b44638205 Mon Sep 17 00:00:00 2001 From: Michael Gobbers Date: Wed, 7 Jul 2021 11:03:00 +0200 Subject: [PATCH 6/7] fix on named argument matchers --- lib/image_test_utils.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/image_test_utils.dart b/lib/image_test_utils.dart index 0be6e63..7c51238 100644 --- a/lib/image_test_utils.dart +++ b/lib/image_test_utils.dart @@ -68,7 +68,7 @@ MockHttpClient _createMockImageHttpClient(SecurityContext? _, List imageByt when(() => response.contentLength).thenReturn(_transparentImage.length); when(() => response.compressionState).thenReturn(HttpClientResponseCompressionState.notCompressed); when(() => response.statusCode).thenReturn(HttpStatus.ok); - when(() => response.listen(any(), onError: any(), onDone: any())).thenAnswer((Invocation invocation) { + when(() => response.listen(any(), onError: any(named: "onError"), onDone: any(named: "onDone"))).thenAnswer((Invocation invocation) { final void Function(List)? onData = invocation.positionalArguments[0]; final void Function()? onDone = invocation.namedArguments[#onDone]; final void Function(Object, [StackTrace?])? onError = invocation.namedArguments[#onError]; From 6160eccfdfde82459795afa3b1853eb919e1a48d Mon Sep 17 00:00:00 2001 From: Michael Gobbers Date: Wed, 7 Jul 2021 11:04:55 +0200 Subject: [PATCH 7/7] included cancel on error argument. --- lib/image_test_utils.dart | 106 ++++++++++++++++++++++++++++++++------ 1 file changed, 91 insertions(+), 15 deletions(-) diff --git a/lib/image_test_utils.dart b/lib/image_test_utils.dart index 7c51238..bfcd379 100644 --- a/lib/image_test_utils.dart +++ b/lib/image_test_utils.dart @@ -42,7 +42,8 @@ import 'package:mocktail/mocktail.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 body(), {List imageBytes = _transparentImage}) { +R provideMockedNetworkImages(R body(), + {List imageBytes = _transparentImage}) { return HttpOverrides.runZoned( body, createHttpClient: (_) => _createMockImageHttpClient(_, imageBytes), @@ -50,41 +51,116 @@ R provideMockedNetworkImages(R body(), {List imageBytes = _transparentIm } 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 imageBytes) { +MockHttpClient _createMockImageHttpClient( + SecurityContext? _, List imageBytes) { final MockHttpClient client = MockHttpClient(); final MockHttpClientRequest request = MockHttpClientRequest(); final MockHttpClientResponse response = MockHttpClientResponse(); final MockHttpHeaders headers = MockHttpHeaders(); - when(() => client.getUrl(any())).thenAnswer((_) => Future.value(request)); - when(() => client.getUrl(any())).thenAnswer((_) => Future.value(request)); + when(() => client.getUrl(any())) + .thenAnswer((_) => Future.value(request)); + when(() => client.getUrl(any())) + .thenAnswer((_) => Future.value(request)); when(() => request.headers).thenReturn(headers); - when(() => request.close()).thenAnswer((_) => Future.value(response)); + when(() => request.close()) + .thenAnswer((_) => Future.value(response)); when(() => response.contentLength).thenReturn(_transparentImage.length); - when(() => response.compressionState).thenReturn(HttpClientResponseCompressionState.notCompressed); + when(() => response.compressionState) + .thenReturn(HttpClientResponseCompressionState.notCompressed); when(() => response.statusCode).thenReturn(HttpStatus.ok); - when(() => response.listen(any(), onError: any(named: "onError"), onDone: any(named: "onDone"))).thenAnswer((Invocation invocation) { + when(() => response.listen(any(), + onError: any(named: "onError"), + onDone: any(named: "onDone"), + cancelOnError: any(named: "cancelOnError"))) + .thenAnswer((Invocation invocation) { final void Function(List)? onData = invocation.positionalArguments[0]; final void Function()? onDone = invocation.namedArguments[#onDone]; - final void Function(Object, [StackTrace?])? onError = invocation.namedArguments[#onError]; + final void Function(Object, [StackTrace?])? onError = + invocation.namedArguments[#onError]; final bool? cancelOnError = invocation.namedArguments[#cancelOnError]; - return Stream>.fromIterable(>[imageBytes]) - .listen(onData, onDone: onDone, onError: onError, cancelOnError: cancelOnError); + return Stream>.fromIterable(>[imageBytes]).listen( + onData, + onDone: onDone, + onError: onError, + cancelOnError: cancelOnError); }); return client; } const List _transparentImage = const [ - 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, ];