Skip to content
This repository has been archived by the owner on Feb 22, 2023. It is now read-only.

[google_maps] Add initial google_maps tests #1409

Merged
merged 12 commits into from
Mar 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .cirrus.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ task:
matrix:
PLUGIN_SHARDING: "--shardIndex 0 --shardCount 2"
PLUGIN_SHARDING: "--shardIndex 1 --shardCount 2"
MAPS_API_KEY: ENCRYPTED[596a9f6bca436694625ac50851dc5da6b4d34cba8025f7db5bc9465142e8cd44e15f69e3507787753accebfc4910d550]
create_device_script:
echo no | avdmanager -v create avd -n test -k "system-images;android-21;default;armeabi-v7a"
start_emulator_background_script:
Expand Down Expand Up @@ -63,6 +64,7 @@ task:
PLUGIN_SHARDING: "--shardIndex 1 --shardCount 4"
PLUGIN_SHARDING: "--shardIndex 2 --shardCount 4"
PLUGIN_SHARDING: "--shardIndex 3 --shardCount 4"
SIMCTL_CHILD_MAPS_API_KEY: ENCRYPTED[596a9f6bca436694625ac50851dc5da6b4d34cba8025f7db5bc9465142e8cd44e15f69e3507787753accebfc4910d550]
setup_script:
- brew update
- brew install libimobiledevice
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,12 @@ public void onMethodCall(MethodCall call, MethodChannel.Result result) {
markersController.changeMarkers((List<Object>) markersToChange);
Object markerIdsToRemove = call.argument("markerIdsToRemove");
markersController.removeMarkers((List<Object>) markerIdsToRemove);
result.success(null);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this a bugfix? if so be clear about it in the PR description, changelog, etc (probably worth considering splitting this to a separate PR)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated the PR description.

break;
}
case "map#isCompassEnabled":
{
result.success(googleMap.getUiSettings().isCompassEnabled());
break;
}
default:
Expand Down
4 changes: 4 additions & 0 deletions packages/google_maps_flutter/example/android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ android {
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}

defaultConfig {
manifestPlaceholders = [mapsApiKey: "$System.env.MAPS_API_KEY"]
}

buildTypes {
release {
// TODO: Add your own signing config for the release build.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<!-- Update this value to your google maps api key. -->
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="YOUR KEY HERE" />
android:value="${mapsApiKey}" />
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a comment saying this should be replaced(for those running locally)

<activity
android:name=".MainActivity"
android:launchMode="singleTop"
Expand Down
10 changes: 7 additions & 3 deletions packages/google_maps_flutter/example/ios/Runner/AppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
- (BOOL)application:(UIApplication*)application
didFinishLaunchingWithOptions:(NSDictionary*)launchOptions {
// Provide the GoogleMaps API key.
[GMSServices provideAPIKey:@"YOUR KEY HERE"];
NSString* mapsApiKey = [[NSProcessInfo processInfo] environment][@"MAPS_API_KEY"];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might be confusing for users, what do you think about still using the key "YOUR KEY HERE" if the env variable is not set?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm thinking of making it so that in the future for local runs we require the api key to be set via env var. That way we do not need to edit these files, and flutter run will automatically pick them up. But for short term, i'm cool with making this be "YOUR KEY HERE".

if ([mapsApiKey length] == 0) {
mapsApiKey = @"YOUR KEY HERE";
}
[GMSServices provideAPIKey:mapsApiKey];

// Register Flutter plugins.
[GeneratedPluginRegistrant registerWithRegistry:self];
Expand Down
6 changes: 3 additions & 3 deletions packages/google_maps_flutter/example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ dependencies:
cupertino_icons: ^0.1.0

dev_dependencies:
flutter_test:
sdk: flutter

google_maps_flutter:
path: ../
flutter_driver:
sdk: flutter
test: ^1.6.0

# For information on the generic Dart part of this file, see the
# following page: https://www.dartlang.org/tools/pub/pubspec
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright 2019, the Chromium project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

import 'package:flutter/services.dart';

/// Inspect Google Maps state using the platform SDK.
///
/// This class is primarily used for testing. The methods on this
/// class should call "getters" on the GoogleMap object or equivalent
/// on the platform side.
class GoogleMapInspector {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: add a doc comment explaining the role of this class.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm confused about why this is its own class. Why not put methods on the GoogleMapController?

GoogleMapInspector(this._channel);

final MethodChannel _channel;

Future<bool> isCompassEnabled() async {
return await _channel.invokeMethod<bool>('map#isCompassEnabled');
}
}
60 changes: 60 additions & 0 deletions packages/google_maps_flutter/example/test_driver/google_maps.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// Copyright 2019, the Chromium project authors. Please see the AUTHORS file
// for details. 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:async';

import 'package:flutter/widgets.dart';
import 'package:flutter_driver/driver_extension.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';

import 'google_map_inspector.dart';
import 'test_widgets.dart';

const CameraPosition _kInitialCameraPosition =
CameraPosition(target: LatLng(0, 0));

void main() {
final Completer<String> allTestsCompleter = Completer<String>();
enableFlutterDriverExtension(handler: (_) => allTestsCompleter.future);

tearDownAll(() => allTestsCompleter.complete(null));

test('testCompassToggle', () async {
final Completer<GoogleMapInspector> inspectorCompleter =
Completer<GoogleMapInspector>();

await pumpWidget(Directionality(
textDirection: TextDirection.ltr,
child: GoogleMap(
initialCameraPosition: _kInitialCameraPosition,
compassEnabled: false,
onMapCreated: (GoogleMapController controller) {
final GoogleMapInspector inspector =
// ignore: invalid_use_of_visible_for_testing_member
GoogleMapInspector(controller.channel);
inspectorCompleter.complete(inspector);
},
),
));

final GoogleMapInspector inspector = await inspectorCompleter.future;
bool compassEnabled = await inspector.isCompassEnabled();
expect(compassEnabled, false);

await pumpWidget(Directionality(
textDirection: TextDirection.ltr,
child: GoogleMap(
initialCameraPosition: _kInitialCameraPosition,
compassEnabled: true,
onMapCreated: (GoogleMapController controller) {
fail("OnMapCreated should get called only once.");
},
),
));

compassEnabled = await inspector.isCompassEnabled();
expect(compassEnabled, true);
});
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Copyright 2019, the Chromium project authors. Please see the AUTHORS file
// for details. 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:async';

import 'package:flutter_driver/flutter_driver.dart';

Future<void> main() async {
final FlutterDriver driver = await FlutterDriver.connect();
await driver.requestData(null, timeout: const Duration(minutes: 1));
driver.close();
}
12 changes: 12 additions & 0 deletions packages/google_maps_flutter/example/test_driver/test_widgets.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Copyright 2019, the Chromium project authors. Please see the AUTHORS file
// for details. 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:async';

import 'package:flutter/widgets.dart';

Future<void> pumpWidget(Widget widget) {
runApp(widget);
return WidgetsBinding.instance.endOfFrame;
}
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,9 @@ - (void)onMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
[_markersController removeMarkerIds:markerIdsToRemove];
}
result(nil);
} else if ([call.method isEqualToString:@"map#isCompassEnabled"]) {
NSNumber* isCompassEnabled = @(_mapView.settings.compassButton);
result(isCompassEnabled);
} else {
result(FlutterMethodNotImplemented);
}
Expand Down
18 changes: 9 additions & 9 deletions packages/google_maps_flutter/lib/src/controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,11 @@ part of google_maps_flutter;
/// Controller for a single GoogleMap instance running on the host platform.
class GoogleMapController {
GoogleMapController._(
MethodChannel channel,
this.channel,
CameraPosition initialCameraPosition,
this._googleMapState,
) : assert(channel != null),
_channel = channel {
_channel.setMethodCallHandler(_handleMethodCall);
) : assert(channel != null) {
channel.setMethodCallHandler(_handleMethodCall);
}

static Future<GoogleMapController> init(
Expand All @@ -34,7 +33,8 @@ class GoogleMapController {
);
}

final MethodChannel _channel;
@visibleForTesting
final MethodChannel channel;

final _GoogleMapState _googleMapState;

Expand Down Expand Up @@ -79,7 +79,7 @@ class GoogleMapController {
// TODO(amirh): remove this on when the invokeMethod update makes it to stable Flutter.
// https://github.com/flutter/flutter/issues/26431
// ignore: strong_mode_implicit_dynamic_method
await _channel.invokeMethod(
await channel.invokeMethod(
'map#update',
<String, dynamic>{
'options': optionsUpdate,
Expand All @@ -98,7 +98,7 @@ class GoogleMapController {
// TODO(amirh): remove this on when the invokeMethod update makes it to stable Flutter.
// https://github.com/flutter/flutter/issues/26431
// ignore: strong_mode_implicit_dynamic_method
await _channel.invokeMethod(
await channel.invokeMethod(
'markers#update',
markerUpdates._toMap(),
);
Expand All @@ -112,7 +112,7 @@ class GoogleMapController {
// TODO(amirh): remove this on when the invokeMethod update makes it to stable Flutter.
// https://github.com/flutter/flutter/issues/26431
// ignore: strong_mode_implicit_dynamic_method
await _channel.invokeMethod('camera#animate', <String, dynamic>{
await channel.invokeMethod('camera#animate', <String, dynamic>{
'cameraUpdate': cameraUpdate._toJson(),
});
}
Expand All @@ -125,7 +125,7 @@ class GoogleMapController {
// TODO(amirh): remove this on when the invokeMethod update makes it to stable Flutter.
// https://github.com/flutter/flutter/issues/26431
// ignore: strong_mode_implicit_dynamic_method
await _channel.invokeMethod('camera#move', <String, dynamic>{
await channel.invokeMethod('camera#move', <String, dynamic>{
'cameraUpdate': cameraUpdate._toJson(),
});
}
Expand Down
6 changes: 6 additions & 0 deletions packages/google_maps_flutter/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ dev_dependencies:
flutter_test:
sdk: flutter

# TODO(iskakaushik): The following dependencies can be removed once
# https://github.com/dart-lang/pub/issues/2101 is resolved.
flutter_driver:
sdk: flutter
test: ^1.6.0

flutter:
plugin:
androidPackage: io.flutter.plugins.googlemaps
Expand Down