|
| 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 'dart:ui' as ui; |
| 7 | + |
| 8 | +import 'package:flutter/foundation.dart'; |
| 9 | +import 'package:flutter/material.dart'; |
| 10 | +import 'package:flutter_test/flutter_test.dart'; |
| 11 | +import 'package:google_maps_flutter/google_maps_flutter.dart'; |
| 12 | +import 'package:google_maps_flutter_platform_interface/google_maps_flutter_platform_interface.dart'; |
| 13 | +import 'package:integration_test/integration_test.dart'; |
| 14 | + |
| 15 | +import 'shared.dart'; |
| 16 | + |
| 17 | +/// Integration Tests for the Tiles feature. These also use the [GoogleMapsInspectorPlatform]. |
| 18 | +void main() { |
| 19 | + IntegrationTestWidgetsFlutterBinding.ensureInitialized(); |
| 20 | + runTests(); |
| 21 | +} |
| 22 | + |
| 23 | +void runTests() { |
| 24 | + GoogleMapsFlutterPlatform.instance.enableDebugInspection(); |
| 25 | + |
| 26 | + final GoogleMapsInspectorPlatform inspector = |
| 27 | + GoogleMapsInspectorPlatform.instance!; |
| 28 | + |
| 29 | + group('Tiles', () { |
| 30 | + testWidgets( |
| 31 | + 'set tileOverlay correctly', |
| 32 | + (WidgetTester tester) async { |
| 33 | + final Completer<int> mapIdCompleter = Completer<int>(); |
| 34 | + final TileOverlay tileOverlay1 = TileOverlay( |
| 35 | + tileOverlayId: const TileOverlayId('tile_overlay_1'), |
| 36 | + tileProvider: _DebugTileProvider(), |
| 37 | + zIndex: 2, |
| 38 | + transparency: 0.2, |
| 39 | + ); |
| 40 | + |
| 41 | + final TileOverlay tileOverlay2 = TileOverlay( |
| 42 | + tileOverlayId: const TileOverlayId('tile_overlay_2'), |
| 43 | + tileProvider: _DebugTileProvider(), |
| 44 | + zIndex: 1, |
| 45 | + visible: false, |
| 46 | + transparency: 0.3, |
| 47 | + fadeIn: false, |
| 48 | + ); |
| 49 | + await tester.pumpWidget( |
| 50 | + Directionality( |
| 51 | + textDirection: TextDirection.ltr, |
| 52 | + child: GoogleMap( |
| 53 | + initialCameraPosition: kInitialCameraPosition, |
| 54 | + tileOverlays: <TileOverlay>{tileOverlay1, tileOverlay2}, |
| 55 | + onMapCreated: (GoogleMapController controller) { |
| 56 | + mapIdCompleter.complete(controller.mapId); |
| 57 | + }, |
| 58 | + ), |
| 59 | + ), |
| 60 | + ); |
| 61 | + await tester.pumpAndSettle(const Duration(seconds: 3)); |
| 62 | + |
| 63 | + final int mapId = await mapIdCompleter.future; |
| 64 | + |
| 65 | + final TileOverlay tileOverlayInfo1 = (await inspector |
| 66 | + .getTileOverlayInfo(tileOverlay1.mapsId, mapId: mapId))!; |
| 67 | + final TileOverlay tileOverlayInfo2 = (await inspector |
| 68 | + .getTileOverlayInfo(tileOverlay2.mapsId, mapId: mapId))!; |
| 69 | + |
| 70 | + expect(tileOverlayInfo1.visible, isTrue); |
| 71 | + expect(tileOverlayInfo1.fadeIn, isTrue); |
| 72 | + expect(tileOverlayInfo1.transparency, |
| 73 | + moreOrLessEquals(0.2, epsilon: 0.001)); |
| 74 | + expect(tileOverlayInfo1.zIndex, 2); |
| 75 | + |
| 76 | + expect(tileOverlayInfo2.visible, isFalse); |
| 77 | + expect(tileOverlayInfo2.fadeIn, isFalse); |
| 78 | + expect(tileOverlayInfo2.transparency, |
| 79 | + moreOrLessEquals(0.3, epsilon: 0.001)); |
| 80 | + expect(tileOverlayInfo2.zIndex, 1); |
| 81 | + }, |
| 82 | + ); |
| 83 | + |
| 84 | + testWidgets( |
| 85 | + 'update tileOverlays correctly', |
| 86 | + (WidgetTester tester) async { |
| 87 | + final Completer<int> mapIdCompleter = Completer<int>(); |
| 88 | + final Key key = GlobalKey(); |
| 89 | + final TileOverlay tileOverlay1 = TileOverlay( |
| 90 | + tileOverlayId: const TileOverlayId('tile_overlay_1'), |
| 91 | + tileProvider: _DebugTileProvider(), |
| 92 | + zIndex: 2, |
| 93 | + transparency: 0.2, |
| 94 | + ); |
| 95 | + |
| 96 | + final TileOverlay tileOverlay2 = TileOverlay( |
| 97 | + tileOverlayId: const TileOverlayId('tile_overlay_2'), |
| 98 | + tileProvider: _DebugTileProvider(), |
| 99 | + zIndex: 3, |
| 100 | + transparency: 0.5, |
| 101 | + ); |
| 102 | + await tester.pumpWidget( |
| 103 | + Directionality( |
| 104 | + textDirection: TextDirection.ltr, |
| 105 | + child: GoogleMap( |
| 106 | + key: key, |
| 107 | + initialCameraPosition: kInitialCameraPosition, |
| 108 | + tileOverlays: <TileOverlay>{tileOverlay1, tileOverlay2}, |
| 109 | + onMapCreated: (GoogleMapController controller) { |
| 110 | + mapIdCompleter.complete(controller.mapId); |
| 111 | + }, |
| 112 | + ), |
| 113 | + ), |
| 114 | + ); |
| 115 | + |
| 116 | + final int mapId = await mapIdCompleter.future; |
| 117 | + |
| 118 | + final TileOverlay tileOverlay1New = TileOverlay( |
| 119 | + tileOverlayId: const TileOverlayId('tile_overlay_1'), |
| 120 | + tileProvider: _DebugTileProvider(), |
| 121 | + zIndex: 1, |
| 122 | + visible: false, |
| 123 | + transparency: 0.3, |
| 124 | + fadeIn: false, |
| 125 | + ); |
| 126 | + |
| 127 | + await tester.pumpWidget( |
| 128 | + Directionality( |
| 129 | + textDirection: TextDirection.ltr, |
| 130 | + child: GoogleMap( |
| 131 | + key: key, |
| 132 | + initialCameraPosition: kInitialCameraPosition, |
| 133 | + tileOverlays: <TileOverlay>{tileOverlay1New}, |
| 134 | + onMapCreated: (GoogleMapController controller) { |
| 135 | + fail('update: OnMapCreated should get called only once.'); |
| 136 | + }, |
| 137 | + ), |
| 138 | + ), |
| 139 | + ); |
| 140 | + |
| 141 | + await tester.pumpAndSettle(const Duration(seconds: 3)); |
| 142 | + |
| 143 | + final TileOverlay tileOverlayInfo1 = (await inspector |
| 144 | + .getTileOverlayInfo(tileOverlay1.mapsId, mapId: mapId))!; |
| 145 | + final TileOverlay? tileOverlayInfo2 = await inspector |
| 146 | + .getTileOverlayInfo(tileOverlay2.mapsId, mapId: mapId); |
| 147 | + |
| 148 | + expect(tileOverlayInfo1.visible, isFalse); |
| 149 | + expect(tileOverlayInfo1.fadeIn, isFalse); |
| 150 | + expect(tileOverlayInfo1.transparency, |
| 151 | + moreOrLessEquals(0.3, epsilon: 0.001)); |
| 152 | + expect(tileOverlayInfo1.zIndex, 1); |
| 153 | + |
| 154 | + expect(tileOverlayInfo2, isNull); |
| 155 | + }, |
| 156 | + ); |
| 157 | + |
| 158 | + testWidgets( |
| 159 | + 'remove tileOverlays correctly', |
| 160 | + (WidgetTester tester) async { |
| 161 | + final Completer<int> mapIdCompleter = Completer<int>(); |
| 162 | + final Key key = GlobalKey(); |
| 163 | + final TileOverlay tileOverlay1 = TileOverlay( |
| 164 | + tileOverlayId: const TileOverlayId('tile_overlay_1'), |
| 165 | + tileProvider: _DebugTileProvider(), |
| 166 | + zIndex: 2, |
| 167 | + transparency: 0.2, |
| 168 | + ); |
| 169 | + |
| 170 | + await tester.pumpWidget( |
| 171 | + Directionality( |
| 172 | + textDirection: TextDirection.ltr, |
| 173 | + child: GoogleMap( |
| 174 | + key: key, |
| 175 | + initialCameraPosition: kInitialCameraPosition, |
| 176 | + tileOverlays: <TileOverlay>{tileOverlay1}, |
| 177 | + onMapCreated: (GoogleMapController controller) { |
| 178 | + mapIdCompleter.complete(controller.mapId); |
| 179 | + }, |
| 180 | + ), |
| 181 | + ), |
| 182 | + ); |
| 183 | + |
| 184 | + final int mapId = await mapIdCompleter.future; |
| 185 | + |
| 186 | + await tester.pumpWidget( |
| 187 | + Directionality( |
| 188 | + textDirection: TextDirection.ltr, |
| 189 | + child: GoogleMap( |
| 190 | + key: key, |
| 191 | + initialCameraPosition: kInitialCameraPosition, |
| 192 | + onMapCreated: (GoogleMapController controller) { |
| 193 | + fail('OnMapCreated should get called only once.'); |
| 194 | + }, |
| 195 | + ), |
| 196 | + ), |
| 197 | + ); |
| 198 | + |
| 199 | + await tester.pumpAndSettle(const Duration(seconds: 3)); |
| 200 | + final TileOverlay? tileOverlayInfo1 = await inspector |
| 201 | + .getTileOverlayInfo(tileOverlay1.mapsId, mapId: mapId); |
| 202 | + |
| 203 | + expect(tileOverlayInfo1, isNull); |
| 204 | + }, |
| 205 | + ); |
| 206 | + }, skip: isWeb /* Tiles not supported on the web */); |
| 207 | +} |
| 208 | + |
| 209 | +class _DebugTileProvider implements TileProvider { |
| 210 | + _DebugTileProvider() { |
| 211 | + boxPaint.isAntiAlias = true; |
| 212 | + boxPaint.color = Colors.blue; |
| 213 | + boxPaint.strokeWidth = 2.0; |
| 214 | + boxPaint.style = PaintingStyle.stroke; |
| 215 | + } |
| 216 | + |
| 217 | + static const int width = 100; |
| 218 | + static const int height = 100; |
| 219 | + static final Paint boxPaint = Paint(); |
| 220 | + static const TextStyle textStyle = TextStyle( |
| 221 | + color: Colors.red, |
| 222 | + fontSize: 20, |
| 223 | + ); |
| 224 | + |
| 225 | + @override |
| 226 | + Future<Tile> getTile(int x, int y, int? zoom) async { |
| 227 | + final ui.PictureRecorder recorder = ui.PictureRecorder(); |
| 228 | + final Canvas canvas = Canvas(recorder); |
| 229 | + final TextSpan textSpan = TextSpan( |
| 230 | + text: '$x,$y', |
| 231 | + style: textStyle, |
| 232 | + ); |
| 233 | + final TextPainter textPainter = TextPainter( |
| 234 | + text: textSpan, |
| 235 | + textDirection: TextDirection.ltr, |
| 236 | + ); |
| 237 | + textPainter.layout( |
| 238 | + maxWidth: width.toDouble(), |
| 239 | + ); |
| 240 | + textPainter.paint(canvas, Offset.zero); |
| 241 | + canvas.drawRect( |
| 242 | + Rect.fromLTRB(0, 0, width.toDouble(), width.toDouble()), boxPaint); |
| 243 | + final ui.Picture picture = recorder.endRecording(); |
| 244 | + final Uint8List byteData = await picture |
| 245 | + .toImage(width, height) |
| 246 | + .then((ui.Image image) => |
| 247 | + image.toByteData(format: ui.ImageByteFormat.png)) |
| 248 | + .then((ByteData? byteData) => byteData!.buffer.asUint8List()); |
| 249 | + return Tile(width, height, byteData); |
| 250 | + } |
| 251 | +} |
0 commit comments