Skip to content

Commit

Permalink
Replace http stubbing with an in-memory TileProvider in tests
Browse files Browse the repository at this point in the history
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<HttpClientRequest>'
  • Loading branch information
rorystephenson committed Jun 11, 2023
1 parent 164e3d3 commit 4c18441
Show file tree
Hide file tree
Showing 8 changed files with 14 additions and 78 deletions.
3 changes: 0 additions & 3 deletions test/flutter_map_controller_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
3 changes: 0 additions & 3 deletions test/flutter_map_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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>[
Marker(
Expand Down
3 changes: 0 additions & 3 deletions test/layer/circle_layer_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down
3 changes: 0 additions & 3 deletions test/layer/marker_layer_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down
3 changes: 0 additions & 3 deletions test/layer/polygon_layer_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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 = <Polygon>[
for (int i = 0; i < 1; ++i)
Expand Down
3 changes: 0 additions & 3 deletions test/layer/polyline_layer_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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 = <Polyline>[
for (int i = 0; i < 10; i++)
Expand Down
60 changes: 0 additions & 60 deletions test/test_utils/mocks.dart

This file was deleted.

14 changes: 14 additions & 0 deletions test/test_utils/test_app.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:flutter_map/flutter_map.dart';
import 'package:latlong2/latlong.dart';
Expand Down Expand Up @@ -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),
Expand All @@ -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<Object> getImage(
TileCoordinates coordinates, TileLayer options) =>
MemoryImage(base64Decode(_whiteTile));
}

0 comments on commit 4c18441

Please sign in to comment.