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.
This commit is contained in:
parent
84068857ff
commit
1153371a56
@ -99,14 +99,14 @@ class NetworkImage extends image_provider.ImageProvider<image_provider.NetworkIm
|
|||||||
static final HttpClient _sharedHttpClient = HttpClient()..autoUncompress = false;
|
static final HttpClient _sharedHttpClient = HttpClient()..autoUncompress = false;
|
||||||
|
|
||||||
static HttpClient get _httpClient {
|
static HttpClient get _httpClient {
|
||||||
HttpClient client = _sharedHttpClient;
|
HttpClient? client;
|
||||||
assert(() {
|
assert(() {
|
||||||
if (debugNetworkImageHttpClientProvider != null) {
|
if (debugNetworkImageHttpClientProvider != null) {
|
||||||
client = debugNetworkImageHttpClientProvider!();
|
client = debugNetworkImageHttpClientProvider!();
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}());
|
}());
|
||||||
return client;
|
return client ?? _sharedHttpClient;
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<ui.Codec> _loadAsync(
|
Future<ui.Codec> _loadAsync(
|
||||||
|
@ -16,6 +16,7 @@ import '../rendering/rendering_tester.dart';
|
|||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
TestRenderingFlutterBinding.ensureInitialized();
|
TestRenderingFlutterBinding.ensureInitialized();
|
||||||
|
HttpOverrides.global = _FakeHttpOverrides();
|
||||||
|
|
||||||
Future<Codec> basicDecoder(ImmutableBuffer buffer, {int? cacheWidth, int? cacheHeight, bool? allowUpscaling}) {
|
Future<Codec> basicDecoder(ImmutableBuffer buffer, {int? cacheWidth, int? cacheHeight, bool? allowUpscaling}) {
|
||||||
return PaintingBinding.instance.instantiateImageCodecFromBuffer(buffer, cacheWidth: cacheWidth, cacheHeight: cacheHeight, allowUpscaling: allowUpscaling ?? false);
|
return PaintingBinding.instance.instantiateImageCodecFromBuffer(buffer, cacheWidth: cacheWidth, cacheHeight: cacheHeight, allowUpscaling: allowUpscaling ?? false);
|
||||||
@ -24,12 +25,14 @@ void main() {
|
|||||||
late _FakeHttpClient httpClient;
|
late _FakeHttpClient httpClient;
|
||||||
|
|
||||||
setUp(() {
|
setUp(() {
|
||||||
|
_FakeHttpOverrides.createHttpClientCalls = 0;
|
||||||
httpClient = _FakeHttpClient();
|
httpClient = _FakeHttpClient();
|
||||||
debugNetworkImageHttpClientProvider = () => httpClient;
|
debugNetworkImageHttpClientProvider = () => httpClient;
|
||||||
});
|
});
|
||||||
|
|
||||||
tearDown(() {
|
tearDown(() {
|
||||||
debugNetworkImageHttpClientProvider = null;
|
debugNetworkImageHttpClientProvider = null;
|
||||||
|
expect(_FakeHttpOverrides.createHttpClientCalls, 0);
|
||||||
PaintingBinding.instance.imageCache.clear();
|
PaintingBinding.instance.imageCache.clear();
|
||||||
PaintingBinding.instance.imageCache.clearLiveImages();
|
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 {
|
class _FakeHttpClient extends Fake implements HttpClient {
|
||||||
final _FakeHttpClientRequest request = _FakeHttpClientRequest();
|
final _FakeHttpClientRequest request = _FakeHttpClientRequest();
|
||||||
Object? thrownError;
|
Object? thrownError;
|
||||||
@ -224,6 +243,7 @@ class _FakeHttpClient extends Fake implements HttpClient {
|
|||||||
return request;
|
return request;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class _FakeHttpClientRequest extends Fake implements HttpClientRequest {
|
class _FakeHttpClientRequest extends Fake implements HttpClientRequest {
|
||||||
final _FakeHttpClientResponse response = _FakeHttpClientResponse();
|
final _FakeHttpClientResponse response = _FakeHttpClientResponse();
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user