Skip to content

Commit 301ab86

Browse files
authored
[google_maps_flutter_web] Fail fast and deduplicate map setup in integration tests (#13092)
Split out from #13059. Integration tests have no default timeout, so when a map fails to initialize on CI, the test hangs until `flutter drive` gives up after 20 minutes with an opaque `DriverError` and no browser state in the log. Also, tests that construct multiple `GoogleMapController` instances reused `mapId = 33930`, which collides in `platformViewRegistry.registerViewFactory` so `controller.widget` renders the first test's `<div>`. - Add `pumpMap` and `pumpPlatformMap` helpers that bound map creation to 30s and dump the Maps SDK / DOM `<div>` state on timeout. - Add `getNextMapId()` so each test controller registers a unique platform view factory. - Set a 2-minute per-test timeout in the real-map suites and a 5-minute driver timeout in `test_driver/integration_test.dart`. - Fix `marker_clustering_test` waiting for `clusters.isNotEmpty` instead of `clusters.isEmpty` after removing all clusters. Part of flutter/flutter#193452
1 parent 759473e commit 301ab86

7 files changed

Lines changed: 185 additions & 116 deletions

File tree

‎packages/google_maps_flutter/google_maps_flutter_web/example/latest/integration_test/google_maps_controller_test.dart‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import 'package:integration_test/integration_test.dart';
1515
import 'package:mockito/annotations.dart';
1616
import 'package:mockito/mockito.dart';
1717
import 'google_maps_controller_test.mocks.dart';
18+
import 'resources/pump_map.dart';
1819

1920
// This value is used when comparing long~num, like
2021
// LatLng values.
@@ -38,18 +39,19 @@ void main() {
3839
IntegrationTestWidgetsFlutterBinding.ensureInitialized();
3940

4041
group('GoogleMapController', () {
41-
var mapId = 33930;
42+
late int mapId;
4243
late GoogleMapController controller;
4344
late StreamController<MapEvent<Object?>> stream;
4445

45-
// Creates a controller with the default mapId and stream controller, and any `options` needed.
46+
// Creates a controller with a fresh mapId and the test's stream controller.
4647
GoogleMapController createController({
4748
CameraPosition initialCameraPosition = const CameraPosition(target: LatLng(0, 0)),
4849
MapObjects mapObjects = const MapObjects(),
4950
MapConfiguration mapConfiguration = const MapConfiguration(),
5051
}) {
52+
mapId = getNextMapId();
5153
return GoogleMapController(
52-
mapId: ++mapId,
54+
mapId: mapId,
5355
streamController: stream,
5456
widgetConfiguration: MapWidgetConfiguration(
5557
initialCameraPosition: initialCameraPosition,

‎packages/google_maps_flutter/google_maps_flutter_web/example/latest/integration_test/google_maps_plugin_test.dart‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import 'package:mockito/mockito.dart';
1717

1818
@GenerateNiceMocks(<MockSpec<dynamic>>[MockSpec<GoogleMapController>()])
1919
import 'google_maps_plugin_test.mocks.dart';
20+
import 'resources/pump_map.dart';
2021

2122
/// Test GoogleMapsPlugin
2223
void main() {
@@ -65,8 +66,12 @@ void main() {
6566
});
6667

6768
group('buildView', () {
68-
const testMapId = 33930;
6969
const initialCameraPosition = CameraPosition(target: LatLng(0, 0));
70+
late int testMapId;
71+
72+
setUp(() {
73+
testMapId = getNextMapId();
74+
});
7075

7176
testWidgets('returns an HtmlElementView and caches the controller for later', (
7277
WidgetTester tester,

‎packages/google_maps_flutter/google_maps_flutter_web/example/latest/integration_test/marker_clustering_test.dart‎

Lines changed: 44 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,13 @@ import 'package:google_maps_flutter_web/src/google_maps_inspector_web.dart';
1313
import 'package:google_maps_flutter_web/src/marker_clustering.dart';
1414
import 'package:integration_test/integration_test.dart';
1515

16+
import 'resources/pump_map.dart';
17+
1618
void main() {
17-
IntegrationTestWidgetsFlutterBinding.ensureInitialized();
19+
// Fails any test that hangs, instead of letting the driver time out.
20+
IntegrationTestWidgetsFlutterBinding.ensureInitialized().defaultTestTimeout = const Timeout(
21+
Duration(minutes: 2),
22+
);
1823

1924
GoogleMapsFlutterPlatform.instance.enableDebugInspection();
2025
final GoogleMapsFlutterPlatform plugin = GoogleMapsFlutterPlatform.instance;
@@ -25,7 +30,7 @@ void main() {
2530

2631
group('MarkersController', () {
2732
testWidgets('Marker clustering', (WidgetTester tester) async {
28-
const testMapId = 33930;
33+
final int testMapId = getNextMapId();
2934
const clusterManagerId = ClusterManagerId('cluster 1');
3035

3136
final clusterManagers = <ClusterManager>{
@@ -46,22 +51,20 @@ void main() {
4651
),
4752
};
4853

49-
final mapIdCompleter = Completer<int>();
50-
51-
await _pumpMap(
54+
final int mapId = await pumpPlatformMap(
5255
tester,
53-
plugin.buildViewWithConfiguration(
54-
testMapId,
55-
(int id) => mapIdCompleter.complete(id),
56-
widgetConfiguration: const MapWidgetConfiguration(
57-
initialCameraPosition: initialCameraPosition,
58-
textDirection: TextDirection.ltr,
56+
(void Function(int) onPlatformViewCreated) => _wrapMap(
57+
plugin.buildViewWithConfiguration(
58+
testMapId,
59+
onPlatformViewCreated,
60+
widgetConfiguration: const MapWidgetConfiguration(
61+
initialCameraPosition: initialCameraPosition,
62+
textDirection: TextDirection.ltr,
63+
),
64+
mapObjects: MapObjects(clusterManagers: clusterManagers, markers: initialMarkers),
5965
),
60-
mapObjects: MapObjects(clusterManagers: clusterManagers, markers: initialMarkers),
6166
),
6267
);
63-
64-
final int mapId = await mapIdCompleter.future;
6568
expect(mapId, equals(testMapId));
6669

6770
final List<Cluster> clusters =
@@ -86,7 +89,7 @@ void main() {
8689
await waitForValueMatchingPredicate<List<Cluster>>(
8790
tester,
8891
() async => inspector.getClusters(mapId: mapId, clusterManagerId: clusterManagerId),
89-
(List<Cluster> clusters) => clusters.isNotEmpty,
92+
(List<Cluster> clusters) => clusters.isEmpty,
9093
) ??
9194
<Cluster>[];
9295

@@ -119,32 +122,35 @@ void main() {
119122
),
120123
};
121124

122-
const testMapId = 33931;
125+
final int testMapId = getNextMapId();
123126
final events = StreamController<ClusteringEvent>();
124-
await _pumpMap(
127+
await pumpPlatformMap(
125128
tester,
126-
plugin.buildViewWithConfiguration(
127-
testMapId,
128-
(int id) async {
129-
final StreamSubscription<ClusteringEvent>? subscription =
130-
(inspector as GoogleMapsInspectorWeb)
131-
.getClusteringEvents(mapId: testMapId, clusterManagerId: clusterManagerId)
132-
?.listen(events.add);
133-
134-
await plugin.updateMarkers(
135-
MarkerUpdates.from(initialMarkers, markersCluster1),
136-
mapId: testMapId,
137-
);
138-
139-
await Future<void>.delayed(const Duration(seconds: 1));
140-
await subscription?.cancel();
141-
await events.close();
142-
},
143-
widgetConfiguration: const MapWidgetConfiguration(
144-
initialCameraPosition: initialCameraPosition,
145-
textDirection: TextDirection.ltr,
129+
(void Function(int) onPlatformViewCreated) => _wrapMap(
130+
plugin.buildViewWithConfiguration(
131+
testMapId,
132+
(int id) async {
133+
onPlatformViewCreated(id);
134+
final StreamSubscription<ClusteringEvent>? subscription =
135+
(inspector as GoogleMapsInspectorWeb)
136+
.getClusteringEvents(mapId: testMapId, clusterManagerId: clusterManagerId)
137+
?.listen(events.add);
138+
139+
await plugin.updateMarkers(
140+
MarkerUpdates.from(initialMarkers, markersCluster1),
141+
mapId: testMapId,
142+
);
143+
144+
await Future<void>.delayed(const Duration(seconds: 1));
145+
await subscription?.cancel();
146+
await events.close();
147+
},
148+
widgetConfiguration: const MapWidgetConfiguration(
149+
initialCameraPosition: initialCameraPosition,
150+
textDirection: TextDirection.ltr,
151+
),
152+
mapObjects: MapObjects(clusterManagers: clusterManagers, markers: initialMarkers),
146153
),
147-
mapObjects: MapObjects(clusterManagers: clusterManagers, markers: initialMarkers),
148154
),
149155
);
150156

@@ -212,12 +218,6 @@ Marker _copyMarkerWithClusterManagerId(Marker marker, ClusterManagerId? clusterM
212218
);
213219
}
214220

215-
/// Pumps a [map] widget in [tester] of a certain [size], then waits until it settles.
216-
Future<void> _pumpMap(WidgetTester tester, Widget map, [Size size = const Size.square(200)]) async {
217-
await tester.pumpWidget(_wrapMap(map, size));
218-
await tester.pumpAndSettle();
219-
}
220-
221221
/// Wraps a [map] in a bunch of widgets so it renders in all platforms.
222222
///
223223
/// An optional [size] can be passed.

‎packages/google_maps_flutter/google_maps_flutter_web/example/latest/integration_test/overlays_test.dart‎

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import 'package:web/web.dart';
1919

2020
@GenerateNiceMocks(<MockSpec<dynamic>>[MockSpec<TileProvider>()])
2121
import 'overlays_test.mocks.dart';
22+
import 'resources/pump_map.dart';
2223

2324
MockTileProvider neverTileProvider() {
2425
final tileProvider = MockTileProvider();
@@ -27,7 +28,10 @@ MockTileProvider neverTileProvider() {
2728
}
2829

2930
void main() {
30-
IntegrationTestWidgetsFlutterBinding.ensureInitialized();
31+
// Fails any test that hangs, instead of letting the driver time out.
32+
IntegrationTestWidgetsFlutterBinding.ensureInitialized().defaultTestTimeout = const Timeout(
33+
Duration(minutes: 2),
34+
);
3135

3236
group('TileOverlaysController', () {
3337
late TileOverlaysController controller;
@@ -137,28 +141,22 @@ void main() {
137141
});
138142

139143
testWidgets('clearTileCache', (WidgetTester tester) async {
140-
final controllerCompleter = Completer<GoogleMapController>();
141-
await tester.pumpWidget(
142-
MaterialApp(
144+
final GoogleMapController controller = await pumpMap(
145+
tester,
146+
(void Function(GoogleMapController) onMapCreated) => MaterialApp(
143147
home: Scaffold(
144148
body: GoogleMap(
145149
initialCameraPosition: const CameraPosition(
146150
target: LatLng(43.3078, -5.6958),
147151
zoom: 14,
148152
),
149153
tileOverlays: <TileOverlay>{...tileOverlays.take(2)},
150-
onMapCreated: (GoogleMapController value) {
151-
controllerCompleter.complete(value);
152-
addTearDown(() => value.dispose());
153-
},
154+
onMapCreated: onMapCreated,
154155
),
155156
),
156157
),
157158
);
158-
159-
// This is needed to kick-off the rendering of the JS Map flutter widget
160-
await tester.pump();
161-
final GoogleMapController controller = await controllerCompleter.future;
159+
addTearDown(controller.dispose);
162160

163161
await tester.pump();
164162
verify(tileProviders[0].getTile(any, any, any));

0 commit comments

Comments
 (0)