blob: 2f20a37a161040eab442ef71f43c4edf93c38ae6 [file] [log] [blame]
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'dart:typed_data';
import 'package:flutter/services.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
void main() {
final _FakePlatformViewsController fakePlatformViewsController =
_FakePlatformViewsController();
setUpAll(() {
SystemChannels.platform_views.setMockMethodCallHandler(
fakePlatformViewsController.fakePlatformViewsMethodHandler);
});
setUp(() {
fakePlatformViewsController.reset();
});
testWidgets('Initial camera position', (WidgetTester tester) async {
await tester.pumpWidget(
const Directionality(
textDirection: TextDirection.ltr,
child: GoogleMap(
initialCameraPosition: CameraPosition(target: LatLng(10.0, 15.0)),
),
),
);
final FakePlatformGoogleMap platformGoogleMap =
fakePlatformViewsController.lastCreatedView;
expect(platformGoogleMap.cameraPosition,
const CameraPosition(target: LatLng(10.0, 15.0)));
});
testWidgets('Initial camera position change is a no-op',
(WidgetTester tester) async {
await tester.pumpWidget(
const Directionality(
textDirection: TextDirection.ltr,
child: GoogleMap(
initialCameraPosition: CameraPosition(target: LatLng(10.0, 15.0)),
),
),
);
await tester.pumpWidget(
const Directionality(
textDirection: TextDirection.ltr,
child: GoogleMap(
initialCameraPosition: CameraPosition(target: LatLng(10.0, 16.0)),
),
),
);
final FakePlatformGoogleMap platformGoogleMap =
fakePlatformViewsController.lastCreatedView;
expect(platformGoogleMap.cameraPosition,
const CameraPosition(target: LatLng(10.0, 15.0)));
});
testWidgets('Can update compassEnabled', (WidgetTester tester) async {
await tester.pumpWidget(
const Directionality(
textDirection: TextDirection.ltr,
child: GoogleMap(
initialCameraPosition: CameraPosition(target: LatLng(10.0, 15.0)),
compassEnabled: false,
),
),
);
final FakePlatformGoogleMap platformGoogleMap =
fakePlatformViewsController.lastCreatedView;
expect(platformGoogleMap.compassEnabled, false);
await tester.pumpWidget(
const Directionality(
textDirection: TextDirection.ltr,
child: GoogleMap(
initialCameraPosition: CameraPosition(target: LatLng(10.0, 15.0)),
compassEnabled: true,
),
),
);
expect(platformGoogleMap.compassEnabled, true);
});
testWidgets('Can update cameraTargetBounds', (WidgetTester tester) async {
await tester.pumpWidget(
Directionality(
textDirection: TextDirection.ltr,
child: GoogleMap(
initialCameraPosition:
const CameraPosition(target: LatLng(10.0, 15.0)),
cameraTargetBounds: CameraTargetBounds(
LatLngBounds(
southwest: const LatLng(10.0, 20.0),
northeast: const LatLng(30.0, 40.0),
),
),
),
),
);
final FakePlatformGoogleMap platformGoogleMap =
fakePlatformViewsController.lastCreatedView;
expect(
platformGoogleMap.cameraTargetBounds,
CameraTargetBounds(
LatLngBounds(
southwest: const LatLng(10.0, 20.0),
northeast: const LatLng(30.0, 40.0),
),
));
await tester.pumpWidget(
Directionality(
textDirection: TextDirection.ltr,
child: GoogleMap(
initialCameraPosition:
const CameraPosition(target: LatLng(10.0, 15.0)),
cameraTargetBounds: CameraTargetBounds(
LatLngBounds(
southwest: const LatLng(16.0, 20.0),
northeast: const LatLng(30.0, 40.0),
),
),
),
),
);
expect(
platformGoogleMap.cameraTargetBounds,
CameraTargetBounds(
LatLngBounds(
southwest: const LatLng(16.0, 20.0),
northeast: const LatLng(30.0, 40.0),
),
));
});
testWidgets('Can update mapType', (WidgetTester tester) async {
await tester.pumpWidget(
const Directionality(
textDirection: TextDirection.ltr,
child: GoogleMap(
initialCameraPosition: CameraPosition(target: LatLng(10.0, 15.0)),
mapType: MapType.hybrid,
),
),
);
final FakePlatformGoogleMap platformGoogleMap =
fakePlatformViewsController.lastCreatedView;
expect(platformGoogleMap.mapType, MapType.hybrid);
await tester.pumpWidget(
const Directionality(
textDirection: TextDirection.ltr,
child: GoogleMap(
initialCameraPosition: CameraPosition(target: LatLng(10.0, 15.0)),
mapType: MapType.satellite,
),
),
);
expect(platformGoogleMap.mapType, MapType.satellite);
});
testWidgets('Can update minMaxZoom', (WidgetTester tester) async {
await tester.pumpWidget(
const Directionality(
textDirection: TextDirection.ltr,
child: GoogleMap(
initialCameraPosition: CameraPosition(target: LatLng(10.0, 15.0)),
minMaxZoomPreference: MinMaxZoomPreference(1.0, 3.0),
),
),
);
final FakePlatformGoogleMap platformGoogleMap =
fakePlatformViewsController.lastCreatedView;
expect(platformGoogleMap.minMaxZoomPreference,
const MinMaxZoomPreference(1.0, 3.0));
await tester.pumpWidget(
const Directionality(
textDirection: TextDirection.ltr,
child: GoogleMap(
initialCameraPosition: CameraPosition(target: LatLng(10.0, 15.0)),
minMaxZoomPreference: MinMaxZoomPreference.unbounded,
),
),
);
expect(
platformGoogleMap.minMaxZoomPreference, MinMaxZoomPreference.unbounded);
});
testWidgets('Can update rotateGesturesEnabled', (WidgetTester tester) async {
await tester.pumpWidget(
const Directionality(
textDirection: TextDirection.ltr,
child: GoogleMap(
initialCameraPosition: CameraPosition(target: LatLng(10.0, 15.0)),
rotateGesturesEnabled: false,
),
),
);
final FakePlatformGoogleMap platformGoogleMap =
fakePlatformViewsController.lastCreatedView;
expect(platformGoogleMap.rotateGesturesEnabled, false);
await tester.pumpWidget(
const Directionality(
textDirection: TextDirection.ltr,
child: GoogleMap(
initialCameraPosition: CameraPosition(target: LatLng(10.0, 15.0)),
rotateGesturesEnabled: true,
),
),
);
expect(platformGoogleMap.rotateGesturesEnabled, true);
});
testWidgets('Can update scrollGesturesEnabled', (WidgetTester tester) async {
await tester.pumpWidget(
const Directionality(
textDirection: TextDirection.ltr,
child: GoogleMap(
initialCameraPosition: CameraPosition(target: LatLng(10.0, 15.0)),
scrollGesturesEnabled: false,
),
),
);
final FakePlatformGoogleMap platformGoogleMap =
fakePlatformViewsController.lastCreatedView;
expect(platformGoogleMap.scrollGesturesEnabled, false);
await tester.pumpWidget(
const Directionality(
textDirection: TextDirection.ltr,
child: GoogleMap(
initialCameraPosition: CameraPosition(target: LatLng(10.0, 15.0)),
scrollGesturesEnabled: true,
),
),
);
expect(platformGoogleMap.scrollGesturesEnabled, true);
});
testWidgets('Can update tiltGesturesEnabled', (WidgetTester tester) async {
await tester.pumpWidget(
const Directionality(
textDirection: TextDirection.ltr,
child: GoogleMap(
initialCameraPosition: CameraPosition(target: LatLng(10.0, 15.0)),
tiltGesturesEnabled: false,
),
),
);
final FakePlatformGoogleMap platformGoogleMap =
fakePlatformViewsController.lastCreatedView;
expect(platformGoogleMap.tiltGesturesEnabled, false);
await tester.pumpWidget(
const Directionality(
textDirection: TextDirection.ltr,
child: GoogleMap(
initialCameraPosition: CameraPosition(target: LatLng(10.0, 15.0)),
tiltGesturesEnabled: true,
),
),
);
expect(platformGoogleMap.tiltGesturesEnabled, true);
});
testWidgets('Can update trackCameraPosition', (WidgetTester tester) async {
await tester.pumpWidget(
const Directionality(
textDirection: TextDirection.ltr,
child: GoogleMap(
initialCameraPosition: CameraPosition(target: LatLng(10.0, 15.0)),
trackCameraPosition: false,
),
),
);
final FakePlatformGoogleMap platformGoogleMap =
fakePlatformViewsController.lastCreatedView;
expect(platformGoogleMap.trackCameraPosition, false);
await tester.pumpWidget(
const Directionality(
textDirection: TextDirection.ltr,
child: GoogleMap(
initialCameraPosition: CameraPosition(target: LatLng(10.0, 15.0)),
trackCameraPosition: true,
),
),
);
expect(platformGoogleMap.trackCameraPosition, true);
});
testWidgets('Can update zoomGesturesEnabled', (WidgetTester tester) async {
await tester.pumpWidget(
const Directionality(
textDirection: TextDirection.ltr,
child: GoogleMap(
initialCameraPosition: CameraPosition(target: LatLng(10.0, 15.0)),
zoomGesturesEnabled: false,
),
),
);
final FakePlatformGoogleMap platformGoogleMap =
fakePlatformViewsController.lastCreatedView;
expect(platformGoogleMap.zoomGesturesEnabled, false);
await tester.pumpWidget(
const Directionality(
textDirection: TextDirection.ltr,
child: GoogleMap(
initialCameraPosition: CameraPosition(target: LatLng(10.0, 15.0)),
zoomGesturesEnabled: true,
),
),
);
expect(platformGoogleMap.zoomGesturesEnabled, true);
});
testWidgets('Can update myLocationEnabled', (WidgetTester tester) async {
await tester.pumpWidget(
const Directionality(
textDirection: TextDirection.ltr,
child: GoogleMap(
initialCameraPosition: CameraPosition(target: LatLng(10.0, 15.0)),
myLocationEnabled: false,
),
),
);
final FakePlatformGoogleMap platformGoogleMap =
fakePlatformViewsController.lastCreatedView;
expect(platformGoogleMap.myLocationEnabled, false);
await tester.pumpWidget(
const Directionality(
textDirection: TextDirection.ltr,
child: GoogleMap(
initialCameraPosition: CameraPosition(target: LatLng(10.0, 15.0)),
myLocationEnabled: true,
),
),
);
expect(platformGoogleMap.myLocationEnabled, true);
});
}
class FakePlatformGoogleMap {
FakePlatformGoogleMap(int id, Map<dynamic, dynamic> params) {
cameraPosition = CameraPosition.fromMap(params['initialCameraPosition']);
channel = MethodChannel(
'plugins.flutter.io/google_maps_$id', const StandardMethodCodec());
channel.setMockMethodCallHandler(onMethodCall);
updateOptions(params['options']);
}
MethodChannel channel;
CameraPosition cameraPosition;
bool compassEnabled;
CameraTargetBounds cameraTargetBounds;
MapType mapType;
MinMaxZoomPreference minMaxZoomPreference;
bool rotateGesturesEnabled;
bool scrollGesturesEnabled;
bool tiltGesturesEnabled;
bool zoomGesturesEnabled;
bool trackCameraPosition;
bool myLocationEnabled;
Future<dynamic> onMethodCall(MethodCall call) {
switch (call.method) {
case 'map#update':
updateOptions(call.arguments['options']);
return Future<void>.sync(() {});
}
return Future<void>.sync(() {});
}
void updateOptions(Map<dynamic, dynamic> options) {
if (options.containsKey('compassEnabled')) {
compassEnabled = options['compassEnabled'];
}
if (options.containsKey('cameraTargetBounds')) {
final List<dynamic> boundsList = options['cameraTargetBounds'];
cameraTargetBounds = boundsList[0] == null
? CameraTargetBounds.unbounded
: CameraTargetBounds(LatLngBounds.fromList(boundsList[0]));
}
if (options.containsKey('mapType')) {
mapType = MapType.values[options['mapType']];
}
if (options.containsKey('minMaxZoomPreference')) {
final List<dynamic> minMaxZoomList = options['minMaxZoomPreference'];
minMaxZoomPreference =
MinMaxZoomPreference(minMaxZoomList[0], minMaxZoomList[1]);
}
if (options.containsKey('rotateGesturesEnabled')) {
rotateGesturesEnabled = options['rotateGesturesEnabled'];
}
if (options.containsKey('scrollGesturesEnabled')) {
scrollGesturesEnabled = options['scrollGesturesEnabled'];
}
if (options.containsKey('tiltGesturesEnabled')) {
tiltGesturesEnabled = options['tiltGesturesEnabled'];
}
if (options.containsKey('trackCameraPosition')) {
trackCameraPosition = options['trackCameraPosition'];
}
if (options.containsKey('zoomGesturesEnabled')) {
zoomGesturesEnabled = options['zoomGesturesEnabled'];
}
if (options.containsKey('myLocationEnabled')) {
myLocationEnabled = options['myLocationEnabled'];
}
}
}
class _FakePlatformViewsController {
FakePlatformGoogleMap lastCreatedView;
Future<dynamic> fakePlatformViewsMethodHandler(MethodCall call) {
switch (call.method) {
case 'create':
final Map<dynamic, dynamic> args = call.arguments;
final Map<dynamic, dynamic> params = _decodeParams(args['params']);
lastCreatedView = FakePlatformGoogleMap(
args['id'],
params,
);
return Future<int>.sync(() => 1);
default:
return Future<void>.sync(() {});
}
}
void reset() {
lastCreatedView = null;
}
}
Map<dynamic, dynamic> _decodeParams(Uint8List paramsMessage) {
final ByteBuffer buffer = paramsMessage.buffer;
final ByteData messageBytes = buffer.asByteData(
paramsMessage.offsetInBytes,
paramsMessage.lengthInBytes,
);
return const StandardMessageCodec().decodeMessage(messageBytes);
}