|
| 1 | +// Copyright 2013 The Flutter Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +import 'dart:async'; |
| 6 | +import 'package:flutter/widgets.dart' |
| 7 | + show Directionality, SizedBox, TextDirection; |
| 8 | +import 'package:flutter_test/flutter_test.dart'; |
| 9 | +import 'package:google_maps/google_maps.dart' as gmaps; |
| 10 | +import 'package:google_maps_flutter_platform_interface/google_maps_flutter_platform_interface.dart' |
| 11 | + show |
| 12 | + CameraPosition, |
| 13 | + LatLng, |
| 14 | + MapConfiguration, |
| 15 | + MapEvent, |
| 16 | + MapWidgetConfiguration; |
| 17 | +import 'package:google_maps_flutter_web/google_maps_flutter_web.dart' |
| 18 | + show GoogleMapController; |
| 19 | +import 'package:integration_test/integration_test.dart'; |
| 20 | + |
| 21 | +void main() { |
| 22 | + IntegrationTestWidgetsFlutterBinding.ensureInitialized(); |
| 23 | + |
| 24 | + MapWidgetConfiguration cfg() => const MapWidgetConfiguration( |
| 25 | + initialCameraPosition: CameraPosition(target: LatLng(0, 0), zoom: 1), |
| 26 | + textDirection: TextDirection.ltr, |
| 27 | + ); |
| 28 | + |
| 29 | + testWidgets('cloudMapId present => mapId set & styles omitted', ( |
| 30 | + WidgetTester tester, |
| 31 | + ) async { |
| 32 | + const MapConfiguration testMapConfig = MapConfiguration( |
| 33 | + mapId: 'test-cloud-map-id', |
| 34 | + ); |
| 35 | + |
| 36 | + await tester.pumpWidget( |
| 37 | + const Directionality(textDirection: TextDirection.ltr, child: SizedBox()), |
| 38 | + ); |
| 39 | + |
| 40 | + final StreamController<MapEvent<Object?>> stream = |
| 41 | + StreamController<MapEvent<Object?>>(); |
| 42 | + addTearDown(() { |
| 43 | + // Stream is closed by controller.dispose() |
| 44 | + }); |
| 45 | + |
| 46 | + gmaps.MapOptions? captured; |
| 47 | + |
| 48 | + final GoogleMapController controller = GoogleMapController( |
| 49 | + mapId: 1, // Internal controller ID |
| 50 | + streamController: stream, |
| 51 | + widgetConfiguration: cfg(), |
| 52 | + mapConfiguration: testMapConfig, // cloudMapId is set here |
| 53 | + ); |
| 54 | + |
| 55 | + controller.debugSetOverrides( |
| 56 | + setOptions: (gmaps.MapOptions options) { |
| 57 | + captured = options; |
| 58 | + }, |
| 59 | + ); |
| 60 | + |
| 61 | + final List<gmaps.MapTypeStyle> styles = <gmaps.MapTypeStyle>[ |
| 62 | + gmaps.MapTypeStyle() |
| 63 | + ..featureType = 'road' |
| 64 | + ..elementType = 'geometry', |
| 65 | + ]; |
| 66 | + |
| 67 | + controller.updateStyles(styles); |
| 68 | + |
| 69 | + await tester.pump(); |
| 70 | + |
| 71 | + expect(captured, isNotNull); |
| 72 | + expect(captured!.mapId, testMapConfig.mapId); |
| 73 | + expect( |
| 74 | + captured!.styles == null || captured!.styles!.isEmpty, |
| 75 | + isTrue, |
| 76 | + reason: 'When cloudMapId is set, styles must not be applied.', |
| 77 | + ); |
| 78 | + |
| 79 | + controller.dispose(); |
| 80 | + }); |
| 81 | + |
| 82 | + testWidgets('no cloudMapId => styles applied', (WidgetTester tester) async { |
| 83 | + await tester.pumpWidget( |
| 84 | + const Directionality(textDirection: TextDirection.ltr, child: SizedBox()), |
| 85 | + ); |
| 86 | + |
| 87 | + final StreamController<MapEvent<Object?>> stream = |
| 88 | + StreamController<MapEvent<Object?>>(); |
| 89 | + addTearDown(() { |
| 90 | + // Stream is closed by controller.dispose() |
| 91 | + }); |
| 92 | + |
| 93 | + gmaps.MapOptions? captured; |
| 94 | + final GoogleMapController controller = GoogleMapController( |
| 95 | + mapId: 2, // Internal controller ID |
| 96 | + streamController: stream, |
| 97 | + widgetConfiguration: cfg(), |
| 98 | + ); |
| 99 | + |
| 100 | + controller.debugSetOverrides( |
| 101 | + setOptions: (gmaps.MapOptions options) { |
| 102 | + captured = options; |
| 103 | + }, |
| 104 | + ); |
| 105 | + |
| 106 | + final List<gmaps.MapTypeStyle> styles = <gmaps.MapTypeStyle>[ |
| 107 | + gmaps.MapTypeStyle() |
| 108 | + ..featureType = 'poi' |
| 109 | + ..elementType = 'labels', |
| 110 | + ]; |
| 111 | + |
| 112 | + controller.updateStyles(styles); |
| 113 | + |
| 114 | + await tester.pump(); |
| 115 | + |
| 116 | + expect(captured, isNotNull); |
| 117 | + expect( |
| 118 | + captured!.mapId, |
| 119 | + anyOf(isNull, isEmpty), |
| 120 | + reason: 'mapId should be empty/null when no Cloud Map is used.', |
| 121 | + ); |
| 122 | + expect(captured!.styles, isNotNull); |
| 123 | + expect( |
| 124 | + captured!.styles, |
| 125 | + isNotEmpty, |
| 126 | + reason: 'When cloudMapId is null, styles should be applied.', |
| 127 | + ); |
| 128 | + |
| 129 | + controller.dispose(); |
| 130 | + }); |
| 131 | +} |
0 commit comments