From 147b03c6fb57b423908f27249f97a524fb389beb Mon Sep 17 00:00:00 2001 From: Rory Stephenson Date: Sun, 11 Jun 2023 12:05:30 +0200 Subject: [PATCH] Replace http stubbing with an in-memory TileProvider in tests This stops the following message from being spammed in tests which was caused by a problem with the http mocking: type 'Null' is not a subtype of type 'Future' --- test/flutter_map_controller_test.dart | 3 -- test/flutter_map_test.dart | 3 -- test/layer/circle_layer_test.dart | 3 -- test/layer/marker_layer_test.dart | 3 -- test/layer/polygon_layer_test.dart | 3 -- test/layer/polyline_layer_test.dart | 3 -- test/test_utils/mocks.dart | 60 --------------------------- test/test_utils/test_app.dart | 14 +++++++ 8 files changed, 14 insertions(+), 78 deletions(-) delete mode 100644 test/test_utils/mocks.dart diff --git a/test/flutter_map_controller_test.dart b/test/flutter_map_controller_test.dart index da9aae6b1..63ade0c31 100644 --- a/test/flutter_map_controller_test.dart +++ b/test/flutter_map_controller_test.dart @@ -2,12 +2,9 @@ import 'package:flutter_map/flutter_map.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:latlong2/latlong.dart'; -import 'test_utils/mocks.dart'; import 'test_utils/test_app.dart'; void main() { - setupMocks(); - testWidgets('test fit bounds methods', (tester) async { final controller = MapController(); final bounds = LatLngBounds( diff --git a/test/flutter_map_test.dart b/test/flutter_map_test.dart index a321cdc98..f2e99635a 100644 --- a/test/flutter_map_test.dart +++ b/test/flutter_map_test.dart @@ -3,12 +3,9 @@ import 'package:flutter_map/plugin_api.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:latlong2/latlong.dart'; -import 'test_utils/mocks.dart'; import 'test_utils/test_app.dart'; void main() { - setupMocks(); - testWidgets('flutter_map', (tester) async { final markers = [ Marker( diff --git a/test/layer/circle_layer_test.dart b/test/layer/circle_layer_test.dart index e69ba0906..ac8194dfc 100644 --- a/test/layer/circle_layer_test.dart +++ b/test/layer/circle_layer_test.dart @@ -3,12 +3,9 @@ import 'package:flutter_map/flutter_map.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:latlong2/latlong.dart'; -import '../test_utils/mocks.dart'; import '../test_utils/test_app.dart'; void main() { - setupMocks(); - testWidgets('test circle marker key', (tester) async { const key = Key('c-1'); diff --git a/test/layer/marker_layer_test.dart b/test/layer/marker_layer_test.dart index db0fb1eef..825b5294a 100644 --- a/test/layer/marker_layer_test.dart +++ b/test/layer/marker_layer_test.dart @@ -3,12 +3,9 @@ import 'package:flutter_map/flutter_map.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:latlong2/latlong.dart'; -import '../test_utils/mocks.dart'; import '../test_utils/test_app.dart'; void main() { - setupMocks(); - testWidgets('test marker key', (tester) async { const key = Key('m-1'); diff --git a/test/layer/polygon_layer_test.dart b/test/layer/polygon_layer_test.dart index ef5e46c78..ac55b4684 100644 --- a/test/layer/polygon_layer_test.dart +++ b/test/layer/polygon_layer_test.dart @@ -3,12 +3,9 @@ import 'package:flutter_map/flutter_map.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:latlong2/latlong.dart'; -import '../test_utils/mocks.dart'; import '../test_utils/test_app.dart'; void main() { - setupMocks(); - testWidgets('test polygon layer', (tester) async { final polygons = [ for (int i = 0; i < 1; ++i) diff --git a/test/layer/polyline_layer_test.dart b/test/layer/polyline_layer_test.dart index a42816161..9fdf43f44 100644 --- a/test/layer/polyline_layer_test.dart +++ b/test/layer/polyline_layer_test.dart @@ -3,12 +3,9 @@ import 'package:flutter_map/flutter_map.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:latlong2/latlong.dart'; -import '../test_utils/mocks.dart'; import '../test_utils/test_app.dart'; void main() { - setupMocks(); - testWidgets('test polyline layer', (tester) async { final polylines = [ for (int i = 0; i < 10; i++) diff --git a/test/test_utils/mocks.dart b/test/test_utils/mocks.dart deleted file mode 100644 index 8a25202e3..000000000 --- a/test/test_utils/mocks.dart +++ /dev/null @@ -1,60 +0,0 @@ -import 'dart:async'; -import 'dart:io'; - -import 'package:flutter_test/flutter_test.dart'; -import 'package:mocktail/mocktail.dart'; - -class MockHttpClientResponse extends Mock implements HttpClientResponse { - final _stream = readFile(); - - @override - int get statusCode => HttpStatus.ok; - - @override - int get contentLength => File('test/res/map.png').lengthSync(); - - @override - HttpClientResponseCompressionState get compressionState => - HttpClientResponseCompressionState.notCompressed; - - @override - StreamSubscription> listen(void Function(List event)? onData, - {Function? onError, void Function()? onDone, bool? cancelOnError}) { - return _stream.listen( - onData, - onError: onError, - onDone: onDone, - cancelOnError: cancelOnError, - ); - } - - static Stream> readFile() => File('test/res/map.png').openRead(); -} - -class MockHttpHeaders extends Mock implements HttpHeaders {} - -class MockHttpClientRequest extends Mock implements HttpClientRequest { - @override - HttpHeaders get headers => MockHttpHeaders(); - - @override - Future close() => Future.value(MockHttpClientResponse()); -} - -class MockClient extends Mock implements HttpClient { - @override - Future getUrl(Uri url) { - return Future.value(MockHttpClientRequest()); - } -} - -class MockHttpOverrides extends HttpOverrides { - @override - HttpClient createHttpClient(SecurityContext? securityContext) => MockClient(); -} - -void setupMocks() { - setUpAll(() { - HttpOverrides.global = MockHttpOverrides(); - }); -} diff --git a/test/test_utils/test_app.dart b/test/test_utils/test_app.dart index 5945ff855..4448dbca4 100644 --- a/test/test_utils/test_app.dart +++ b/test/test_utils/test_app.dart @@ -1,3 +1,5 @@ +import 'dart:convert'; + import 'package:flutter/material.dart'; import 'package:flutter_map/flutter_map.dart'; import 'package:latlong2/latlong.dart'; @@ -36,6 +38,7 @@ class TestApp extends StatelessWidget { children: [ TileLayer( urlTemplate: 'https://tile.openstreetmap.org/{z}/{x}/{y}.png', + tileProvider: TestTileProvider(), ), if (polylines.isNotEmpty) PolylineLayer(polylines: polylines), if (polygons.isNotEmpty) PolygonLayer(polygons: polygons), @@ -49,3 +52,14 @@ class TestApp extends StatelessWidget { ); } } + +class TestTileProvider extends TileProvider { + // Base 64 encoded 256x256 white tile. + static const _whiteTile = + 'iVBORw0KGgoAAAANSUhEUgAAAQAAAAEAAQMAAABmvDolAAAAAXNSR0IB2cksfwAAAAlwSFlzAAALEwAACxMBAJqcGAAAAANQTFRF////p8QbyAAAAB9JREFUeJztwQENAAAAwqD3T20ON6AAAAAAAAAAAL4NIQAAAfFnIe4AAAAASUVORK5CYII='; + + @override + ImageProvider getImage( + TileCoordinates coordinates, TileLayer options) => + MemoryImage(base64Decode(_whiteTile)); +}