Unverified Commit 1153371a authored by Greg Price's avatar Greg Price Committed by GitHub

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.
parent 84068857
......@@ -99,14 +99,14 @@ class NetworkImage extends image_provider.ImageProvider<image_provider.NetworkIm
static final HttpClient _sharedHttpClient = HttpClient()..autoUncompress = false;
static HttpClient get _httpClient {
HttpClient client = _sharedHttpClient;
HttpClient? client;
assert(() {
if (debugNetworkImageHttpClientProvider != null) {
client = debugNetworkImageHttpClientProvider!();
}
return true;
}());
return client;
return client ?? _sharedHttpClient;
}
Future<ui.Codec> _loadAsync(
......
......@@ -16,6 +16,7 @@ import '../rendering/rendering_tester.dart';
void main() {
TestRenderingFlutterBinding.ensureInitialized();
HttpOverrides.global = _FakeHttpOverrides();
Future<Codec> basicDecoder(ImmutableBuffer buffer, {int? cacheWidth, int? cacheHeight, bool? allowUpscaling}) {
return PaintingBinding.instance.instantiateImageCodecFromBuffer(buffer, cacheWidth: cacheWidth, cacheHeight: cacheHeight, allowUpscaling: allowUpscaling ?? false);
......@@ -24,12 +25,14 @@ void main() {
late _FakeHttpClient httpClient;
setUp(() {
_FakeHttpOverrides.createHttpClientCalls = 0;
httpClient = _FakeHttpClient();
debugNetworkImageHttpClientProvider = () => httpClient;
});
tearDown(() {
debugNetworkImageHttpClientProvider = null;
expect(_FakeHttpOverrides.createHttpClientCalls, 0);
PaintingBinding.instance.imageCache.clear();
PaintingBinding.instance.imageCache.clearLiveImages();
});
......@@ -212,6 +215,22 @@ void main() {
});
}
/// Override `HttpClient()` to throw an error.
///
/// This ensures that these tests never cause a call to the [HttpClient]
/// constructor.
///
/// Regression test for <https://github.com/flutter/flutter/issues/129532>.
class _FakeHttpOverrides extends HttpOverrides {
static int createHttpClientCalls = 0;
@override
HttpClient createHttpClient(SecurityContext? context) {
createHttpClientCalls++;
throw Exception('This test tried to create an HttpClient.');
}
}
class _FakeHttpClient extends Fake implements HttpClient {
final _FakeHttpClientRequest request = _FakeHttpClientRequest();
Object? thrownError;
......@@ -224,6 +243,7 @@ class _FakeHttpClient extends Fake implements HttpClient {
return request;
}
}
class _FakeHttpClientRequest extends Fake implements HttpClientRequest {
final _FakeHttpClientResponse response = _FakeHttpClientResponse();
......
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