Skip to content

Commit 1153371

Browse files
authored
Fix NetworkImage causing spurious warning in tests (#129537)
Fixes #129532. This ensures that when a test properly uses `debugNetworkImageHttpClientProvider` to tell `NetworkImage` to use a fake `HttpClient`, we don't go ahead and try to instantiate `HttpClient` anyway and generate a misleading warning.
1 parent 8406885 commit 1153371

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

packages/flutter/lib/src/painting/_network_image_io.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,14 +99,14 @@ class NetworkImage extends image_provider.ImageProvider<image_provider.NetworkIm
9999
static final HttpClient _sharedHttpClient = HttpClient()..autoUncompress = false;
100100

101101
static HttpClient get _httpClient {
102-
HttpClient client = _sharedHttpClient;
102+
HttpClient? client;
103103
assert(() {
104104
if (debugNetworkImageHttpClientProvider != null) {
105105
client = debugNetworkImageHttpClientProvider!();
106106
}
107107
return true;
108108
}());
109-
return client;
109+
return client ?? _sharedHttpClient;
110110
}
111111

112112
Future<ui.Codec> _loadAsync(

packages/flutter/test/painting/image_provider_network_image_test.dart

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import '../rendering/rendering_tester.dart';
1616

1717
void main() {
1818
TestRenderingFlutterBinding.ensureInitialized();
19+
HttpOverrides.global = _FakeHttpOverrides();
1920

2021
Future<Codec> basicDecoder(ImmutableBuffer buffer, {int? cacheWidth, int? cacheHeight, bool? allowUpscaling}) {
2122
return PaintingBinding.instance.instantiateImageCodecFromBuffer(buffer, cacheWidth: cacheWidth, cacheHeight: cacheHeight, allowUpscaling: allowUpscaling ?? false);
@@ -24,12 +25,14 @@ void main() {
2425
late _FakeHttpClient httpClient;
2526

2627
setUp(() {
28+
_FakeHttpOverrides.createHttpClientCalls = 0;
2729
httpClient = _FakeHttpClient();
2830
debugNetworkImageHttpClientProvider = () => httpClient;
2931
});
3032

3133
tearDown(() {
3234
debugNetworkImageHttpClientProvider = null;
35+
expect(_FakeHttpOverrides.createHttpClientCalls, 0);
3336
PaintingBinding.instance.imageCache.clear();
3437
PaintingBinding.instance.imageCache.clearLiveImages();
3538
});
@@ -212,6 +215,22 @@ void main() {
212215
});
213216
}
214217

218+
/// Override `HttpClient()` to throw an error.
219+
///
220+
/// This ensures that these tests never cause a call to the [HttpClient]
221+
/// constructor.
222+
///
223+
/// Regression test for <https://github.com/flutter/flutter/issues/129532>.
224+
class _FakeHttpOverrides extends HttpOverrides {
225+
static int createHttpClientCalls = 0;
226+
227+
@override
228+
HttpClient createHttpClient(SecurityContext? context) {
229+
createHttpClientCalls++;
230+
throw Exception('This test tried to create an HttpClient.');
231+
}
232+
}
233+
215234
class _FakeHttpClient extends Fake implements HttpClient {
216235
final _FakeHttpClientRequest request = _FakeHttpClientRequest();
217236
Object? thrownError;
@@ -224,6 +243,7 @@ class _FakeHttpClient extends Fake implements HttpClient {
224243
return request;
225244
}
226245
}
246+
227247
class _FakeHttpClientRequest extends Fake implements HttpClientRequest {
228248
final _FakeHttpClientResponse response = _FakeHttpClientResponse();
229249

0 commit comments

Comments
 (0)