Showing
3 changed files
with
186 additions
and
5 deletions
| @@ -23,10 +23,11 @@ environment: | @@ -23,10 +23,11 @@ environment: | ||
| 23 | dependencies: | 23 | dependencies: |
| 24 | flutter: | 24 | flutter: |
| 25 | sdk: flutter | 25 | sdk: flutter |
| 26 | - get_test: | ||
| 27 | - path: ../ | ||
| 28 | get: | 26 | get: |
| 29 | - path: ../../../getx | 27 | + git: |
| 28 | + url: git://github.com/jonataslaw/getx.git | ||
| 29 | + path: getx | ||
| 30 | + ref: master | ||
| 30 | 31 | ||
| 31 | 32 | ||
| 32 | # The following adds the Cupertino Icons font to your application. | 33 | # The following adds the Cupertino Icons font to your application. |
| 1 | +import 'dart:async'; | ||
| 2 | +import 'dart:io'; | ||
| 3 | +import 'package:mockito/mockito.dart'; | ||
| 4 | + | ||
| 5 | +/// Copyright 2018 Iiro Krankka | ||
| 6 | +/// Redistribution and use in source and binary forms, with or without | ||
| 7 | +/// modification, are permitted provided that the following conditions are met: | ||
| 8 | +/// 1. Redistributions of source code must retain the above copyright notice, | ||
| 9 | +/// this list of conditions and the following disclaimer. | ||
| 10 | +/// 2. Redistributions in binary form must reproduce the above copyright | ||
| 11 | +/// notice, this list of conditions and the following disclaimer in the | ||
| 12 | +/// documentation and/or other materials provided with the distribution. | ||
| 13 | +/// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
| 14 | +/// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED | ||
| 15 | +/// TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | ||
| 16 | +/// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR | ||
| 17 | +/// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | ||
| 18 | +/// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | ||
| 19 | +/// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, | ||
| 20 | +/// OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||
| 21 | +/// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
| 22 | +/// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE | ||
| 23 | +/// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
| 24 | + | ||
| 25 | +/// Runs [body] in a fresh [Zone] that provides mocked responses for | ||
| 26 | +/// [Image.network] widgets. | ||
| 27 | +/// | ||
| 28 | +/// Behind the scenes, this creates a mocked HTTP client that responds | ||
| 29 | +/// with mocked | ||
| 30 | +/// image data to all HTTP GET requests. | ||
| 31 | +/// | ||
| 32 | +/// This is a workaround needed for widget tests that use network images. | ||
| 33 | +/// Without | ||
| 34 | +/// the mocked HTTP client, any widget tests that pump a widget tree containing | ||
| 35 | +/// [Image.network] widgets will crash. | ||
| 36 | +/// | ||
| 37 | +/// By default, the mocked HTTP client will respond with [_transparentImage]. If | ||
| 38 | +/// provided, it will use [imageBytes] instead. | ||
| 39 | +/// | ||
| 40 | +/// Example usage in a test case: | ||
| 41 | +/// | ||
| 42 | +/// ``` | ||
| 43 | +/// void main() { | ||
| 44 | +/// testWidgets('should not crash', (WidgetTester tester) async { | ||
| 45 | +/// provideMockedNetworkImages(() async { | ||
| 46 | +/// await tester.pumpWidget( | ||
| 47 | +/// MaterialApp( | ||
| 48 | +/// home: Image.network('https://example.com/image.png'), | ||
| 49 | +/// ), | ||
| 50 | +/// ); | ||
| 51 | +/// }); | ||
| 52 | +/// }); | ||
| 53 | +/// } | ||
| 54 | +/// ``` | ||
| 55 | +/// | ||
| 56 | +/// Note that you'll want to add this package to the dev_dependencies instead of | ||
| 57 | +/// the regular dependencies block on your pubspec.yaml. | ||
| 58 | +/// | ||
| 59 | +/// For more context about [Image.network] widgets failing in widget tests, see | ||
| 60 | +/// these issues: | ||
| 61 | +/// | ||
| 62 | +/// * https://github.com/flutter/flutter/issues/13433 | ||
| 63 | +/// * https://github.com/flutter/flutter_markdown/pull/17 | ||
| 64 | +/// | ||
| 65 | +/// The underlying code is taken from the Flutter repo: | ||
| 66 | +/// https://github.com/flutter/flutter/blob/master/dev/manual_tests/test/mock_image_http.dart | ||
| 67 | +R provideMockedNetworkImages<R>(R body(), | ||
| 68 | + {List<int> imageBytes = _transparentImage}) { | ||
| 69 | + return HttpOverrides.runZoned( | ||
| 70 | + body, | ||
| 71 | + createHttpClient: (_) => _createMockImageHttpClient(_, imageBytes), | ||
| 72 | + ); | ||
| 73 | +} | ||
| 74 | + | ||
| 75 | +class MockHttpClient extends Mock implements HttpClient {} | ||
| 76 | + | ||
| 77 | +class MockHttpClientRequest extends Mock implements HttpClientRequest {} | ||
| 78 | + | ||
| 79 | +class MockHttpClientResponse extends Mock implements HttpClientResponse {} | ||
| 80 | + | ||
| 81 | +class MockHttpHeaders extends Mock implements HttpHeaders {} | ||
| 82 | + | ||
| 83 | +// Returns a mock HTTP client that responds with an image to all requests. | ||
| 84 | +MockHttpClient _createMockImageHttpClient( | ||
| 85 | + SecurityContext _, List<int> imageBytes) { | ||
| 86 | + final client = MockHttpClient(); | ||
| 87 | + final request = MockHttpClientRequest(); | ||
| 88 | + final response = MockHttpClientResponse(); | ||
| 89 | + final headers = MockHttpHeaders(); | ||
| 90 | + | ||
| 91 | + when(client.getUrl(any)) | ||
| 92 | + .thenAnswer((_) => Future<HttpClientRequest>.value(request)); | ||
| 93 | + when(request.headers).thenReturn(headers); | ||
| 94 | + when(request.close()) | ||
| 95 | + .thenAnswer((_) => Future<HttpClientResponse>.value(response)); | ||
| 96 | + when(response.contentLength).thenReturn(_transparentImage.length); | ||
| 97 | + when(response.statusCode).thenReturn(HttpStatus.ok); | ||
| 98 | + when(response.listen(any)).thenAnswer((invocation) { | ||
| 99 | + final onData = | ||
| 100 | + invocation.positionalArguments[0] as void Function(List<int>); | ||
| 101 | + final onDone = invocation.namedArguments[#onDone] as void Function(); | ||
| 102 | + final onError = invocation.namedArguments[#onError] as void Function(Object, | ||
| 103 | + [StackTrace]); | ||
| 104 | + final cancelOnError = invocation.namedArguments[#cancelOnError] as bool; | ||
| 105 | + | ||
| 106 | + return Stream<List<int>>.fromIterable(<List<int>>[imageBytes]).listen( | ||
| 107 | + onData, | ||
| 108 | + onDone: onDone, | ||
| 109 | + onError: onError, | ||
| 110 | + cancelOnError: cancelOnError); | ||
| 111 | + }); | ||
| 112 | + | ||
| 113 | + return client; | ||
| 114 | +} | ||
| 115 | + | ||
| 116 | +const List<int> _transparentImage = <int>[ | ||
| 117 | + 0x89, | ||
| 118 | + 0x50, | ||
| 119 | + 0x4E, | ||
| 120 | + 0x47, | ||
| 121 | + 0x0D, | ||
| 122 | + 0x0A, | ||
| 123 | + 0x1A, | ||
| 124 | + 0x0A, | ||
| 125 | + 0x00, | ||
| 126 | + 0x00, | ||
| 127 | + 0x00, | ||
| 128 | + 0x0D, | ||
| 129 | + 0x49, | ||
| 130 | + 0x48, | ||
| 131 | + 0x44, | ||
| 132 | + 0x52, | ||
| 133 | + 0x00, | ||
| 134 | + 0x00, | ||
| 135 | + 0x00, | ||
| 136 | + 0x01, | ||
| 137 | + 0x00, | ||
| 138 | + 0x00, | ||
| 139 | + 0x00, | ||
| 140 | + 0x01, | ||
| 141 | + 0x08, | ||
| 142 | + 0x06, | ||
| 143 | + 0x00, | ||
| 144 | + 0x00, | ||
| 145 | + 0x00, | ||
| 146 | + 0x1F, | ||
| 147 | + 0x15, | ||
| 148 | + 0xC4, | ||
| 149 | + 0x89, | ||
| 150 | + 0x00, | ||
| 151 | + 0x00, | ||
| 152 | + 0x00, | ||
| 153 | + 0x0A, | ||
| 154 | + 0x49, | ||
| 155 | + 0x44, | ||
| 156 | + 0x41, | ||
| 157 | + 0x54, | ||
| 158 | + 0x78, | ||
| 159 | + 0x9C, | ||
| 160 | + 0x63, | ||
| 161 | + 0x00, | ||
| 162 | + 0x01, | ||
| 163 | + 0x00, | ||
| 164 | + 0x00, | ||
| 165 | + 0x05, | ||
| 166 | + 0x00, | ||
| 167 | + 0x01, | ||
| 168 | + 0x0D, | ||
| 169 | + 0x0A, | ||
| 170 | + 0x2D, | ||
| 171 | + 0xB4, | ||
| 172 | + 0x00, | ||
| 173 | + 0x00, | ||
| 174 | + 0x00, | ||
| 175 | + 0x00, | ||
| 176 | + 0x49, | ||
| 177 | + 0x45, | ||
| 178 | + 0x4E, | ||
| 179 | + 0x44, | ||
| 180 | + 0xAE, | ||
| 181 | +]; |
| @@ -10,7 +10,7 @@ environment: | @@ -10,7 +10,7 @@ environment: | ||
| 10 | dependencies: | 10 | dependencies: |
| 11 | flutter: | 11 | flutter: |
| 12 | sdk: flutter | 12 | sdk: flutter |
| 13 | - mockito: ">=3.0.0 <4.0.0" | 13 | + mockito: ">=3.0.0 <5.0.0" |
| 14 | get_navigation: | 14 | get_navigation: |
| 15 | git: | 15 | git: |
| 16 | url: git://github.com/jonataslaw/getx.git | 16 | url: git://github.com/jonataslaw/getx.git |
| @@ -21,7 +21,6 @@ dependencies: | @@ -21,7 +21,6 @@ dependencies: | ||
| 21 | url: git://github.com/jonataslaw/getx.git | 21 | url: git://github.com/jonataslaw/getx.git |
| 22 | path: packages/get_state_manager | 22 | path: packages/get_state_manager |
| 23 | ref: master | 23 | ref: master |
| 24 | - image_test_utils: ">=1.0.0 <2.0.0" | ||
| 25 | flutter_test: | 24 | flutter_test: |
| 26 | sdk: flutter | 25 | sdk: flutter |
| 27 | 26 |
-
Please register or login to post a comment