Skip to content

Commit d6aaa0d

Browse files
authored
fix: camera events on mapview (#292)
1 parent 8bc03c1 commit d6aaa0d

File tree

5 files changed

+183
-125
lines changed

5 files changed

+183
-125
lines changed

example/integration_test/shared.dart

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -292,31 +292,35 @@ Future<GoogleMapViewController> getMapViewControllerForTestMapType(
292292
onMyLocationButtonClicked: onMyLocationButtonClicked,
293293
onMyLocationClicked: onMyLocationClicked,
294294
onPolygonClicked: onPolygonClicked,
295-
onPolylineClicked: onPolygonClicked,
295+
onPolylineClicked: onPolylineClicked,
296+
onCameraIdle: onCameraIdle,
296297
); // Instantiate a regular map.
297298
break;
298299

299300
/// Set up navigation map.
300301
case TestMapType.navigationView:
301-
viewController = await startNavigationWithoutDestination($,
302-
initializeNavigation: initializeNavigation,
303-
simulateLocation: simulateLocation,
304-
onMarkerClicked: onMarkerClicked,
305-
onCircleClicked: onCircleClicked,
306-
onMapClicked: onMapClicked,
307-
onMapLongClicked: onMapLongClicked,
308-
onMarkerDrag: onMarkerDrag,
309-
onMarkerDragEnd: onMarkerDragEnd,
310-
onMarkerDragStart: onMarkerDragStart,
311-
onMarkerInfoWindowClicked: onMarkerInfoWindowClicked,
312-
onMarkerInfoWindowClosed: onMarkerInfoWindowClosed,
313-
onMarkerInfoWindowLongClicked: onMarkerInfoWindowLongClicked,
314-
onMyLocationButtonClicked: onMyLocationButtonClicked,
315-
onMyLocationClicked: onMyLocationClicked,
316-
onPolygonClicked: onPolygonClicked,
317-
onPolylineClicked: onPolygonClicked,
318-
onRecenterButtonClicked:
319-
onRecenterButtonClicked); // Instantiate a navigation map.
302+
viewController = await startNavigationWithoutDestination(
303+
$,
304+
initializeNavigation: initializeNavigation,
305+
simulateLocation: simulateLocation,
306+
onMarkerClicked: onMarkerClicked,
307+
onCircleClicked: onCircleClicked,
308+
onMapClicked: onMapClicked,
309+
onMapLongClicked: onMapLongClicked,
310+
onMarkerDrag: onMarkerDrag,
311+
onMarkerDragEnd: onMarkerDragEnd,
312+
onMarkerDragStart: onMarkerDragStart,
313+
onMarkerInfoWindowClicked: onMarkerInfoWindowClicked,
314+
onMarkerInfoWindowClosed: onMarkerInfoWindowClosed,
315+
onMarkerInfoWindowLongClicked: onMarkerInfoWindowLongClicked,
316+
onMyLocationButtonClicked: onMyLocationButtonClicked,
317+
onMyLocationClicked: onMyLocationClicked,
318+
onNavigationUIEnabledChanged: onNavigationUIEnabledChanged,
319+
onPolygonClicked: onPolygonClicked,
320+
onPolylineClicked: onPolylineClicked,
321+
onRecenterButtonClicked: onRecenterButtonClicked,
322+
onCameraIdle: onCameraIdle,
323+
); // Instantiate a navigation map.
320324
break;
321325
}
322326
return viewController;
@@ -377,8 +381,9 @@ Future<GoogleNavigationViewController> startNavigationWithoutDestination(
377381
onMyLocationClicked: onMyLocationClicked,
378382
onNavigationUIEnabledChanged: onNavigationUIEnabledChanged,
379383
onPolygonClicked: onPolygonClicked,
380-
onPolylineClicked: onPolygonClicked,
384+
onPolylineClicked: onPolylineClicked,
381385
onRecenterButtonClicked: onRecenterButtonClicked,
386+
onCameraIdle: onCameraIdle,
382387
),
383388
);
384389

@@ -451,8 +456,9 @@ Future<GoogleMapViewController> startMapView(
451456
onMyLocationButtonClicked: onMyLocationButtonClicked,
452457
onMyLocationClicked: onMyLocationClicked,
453458
onPolygonClicked: onPolygonClicked,
454-
onPolylineClicked: onPolygonClicked,
459+
onPolylineClicked: onPolylineClicked,
455460
onRecenterButtonClicked: onRecenterButtonClicked,
461+
onCameraIdle: onCameraIdle,
456462
),
457463
);
458464

example/integration_test/t05_camera_test.dart

Lines changed: 80 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import 'package:flutter/material.dart';
2626
import 'shared.dart';
2727

2828
void main() {
29+
final mapTypeVariants = getMapTypeVariants();
2930
CameraPosition expectedPosition = const CameraPosition();
3031
Completer<void> cameraMoveStartedCompleter = Completer<void>();
3132
Completer<void> cameraMoveCompleter = Completer<void>();
@@ -36,6 +37,7 @@ void main() {
3637
bool? followingMyLocationActive;
3738
CameraPosition? startedFollowingMyLocationPosition;
3839
CameraPosition? stoppedFollowingMyLocationPosition;
40+
const double latLngTestThreshold = 0.03;
3941

4042
/// Define the camera event callback functions.
4143
void onCameraMoveStarted(CameraPosition position, bool gesture) {
@@ -119,10 +121,10 @@ void main() {
119121
bool checkCoordinatesMatch(CameraPosition receivedPosition) {
120122
return (receivedPosition.target.latitude - expectedPosition.target.latitude)
121123
.abs() <=
122-
0.02 &&
124+
latLngTestThreshold &&
123125
(receivedPosition.target.longitude - expectedPosition.target.longitude)
124126
.abs() <=
125-
0.02;
127+
latLngTestThreshold;
126128
}
127129

128130
/// Wait for cameraMoveStarted, cameraMove and cameraIdle events.
@@ -139,8 +141,10 @@ void main() {
139141
/// Check the camera coordinates match each other within tolerance.
140142
void checkCameraCoordinatesMatch(
141143
CameraPosition received, CameraPosition expected) {
142-
expect(received.target.latitude, closeTo(expected.target.latitude, 0.02));
143-
expect(received.target.longitude, closeTo(expected.target.longitude, 0.02));
144+
expect(received.target.latitude,
145+
closeTo(expected.target.latitude, latLngTestThreshold));
146+
expect(received.target.longitude,
147+
closeTo(expected.target.longitude, latLngTestThreshold));
144148
}
145149

146150
patrol('Test camera modes', (PatrolIntegrationTester $) async {
@@ -455,17 +459,44 @@ void main() {
455459
patrol(
456460
'Test moveCamera() and animateCamera() with various options',
457461
(PatrolIntegrationTester $) async {
458-
/// Initialize navigation with the event listener functions.
459-
final GoogleNavigationViewController controller = await startNavigation(
462+
const double startLat = startLocationLat + 1;
463+
const double startLng = startLocationLng + 1;
464+
const LatLng target =
465+
LatLng(latitude: startLat + 1, longitude: startLng + 1);
466+
467+
final CameraUpdate start =
468+
CameraUpdate.newCameraPosition(const CameraPosition(
469+
target: LatLng(
470+
latitude: startLat,
471+
longitude: startLng,
472+
),
473+
zoom: 9,
474+
));
475+
476+
/// Initialize view with the event listener functions.
477+
final GoogleMapViewController viewController =
478+
await getMapViewControllerForTestMapType(
460479
$,
480+
testMapType: mapTypeVariants.currentValue!,
461481
onCameraIdle: onCameraIdle,
462482
);
463-
await GoogleMapsNavigator.stopGuidance();
483+
// Move camera back to the start.
484+
Future<void> moveCameraToStart() async {
485+
resetCameraEventCompleters();
486+
await viewController.moveCamera(start);
487+
await cameraIdleCompleter.future.timeout(const Duration(seconds: 10),
488+
onTimeout: () {
489+
fail('Future timed out');
490+
});
491+
}
492+
493+
// Move camera to the start position.
494+
await moveCameraToStart();
464495

465496
// Create a wrapper for moveCamera() that waits until the move is finished.
466497
Future<void> moveCamera(CameraUpdate update) async {
467498
resetCameraEventCompleters();
468-
await controller.moveCamera(update);
499+
await viewController.moveCamera(update);
469500
await cameraIdleCompleter.future.timeout(const Duration(seconds: 10),
470501
onTimeout: () {
471502
fail('Future timed out');
@@ -484,7 +515,7 @@ void main() {
484515
}
485516

486517
// Animate camera to the set position with reduced duration.
487-
await controller.animateCamera(
518+
await viewController.animateCamera(
488519
update,
489520
duration: const Duration(milliseconds: 50),
490521
onFinished: onFinished,
@@ -503,30 +534,6 @@ void main() {
503534
animateCamera
504535
];
505536

506-
const double startLat = startLocationLat + 1;
507-
const double startLng = startLocationLng + 1;
508-
const LatLng target =
509-
LatLng(latitude: startLat + 1, longitude: startLng + 1);
510-
511-
final CameraUpdate start =
512-
CameraUpdate.newCameraPosition(const CameraPosition(
513-
target: LatLng(
514-
latitude: startLat,
515-
longitude: startLng,
516-
),
517-
zoom: 9,
518-
));
519-
520-
// Move camera back to the start.
521-
Future<void> moveCameraToStart() async {
522-
resetCameraEventCompleters();
523-
await controller.moveCamera(start);
524-
await cameraIdleCompleter.future.timeout(const Duration(seconds: 10),
525-
onTimeout: () {
526-
fail('Future timed out');
527-
});
528-
}
529-
530537
final CameraUpdate updateCameraPosition =
531538
CameraUpdate.newCameraPosition(const CameraPosition(
532539
bearing: 5,
@@ -562,9 +569,9 @@ void main() {
562569

563570
// Test that the camera target matches the provided target.
564571
expect(cameraIdlePosition.target.latitude,
565-
closeTo(updateNewLatLng.latLng!.latitude, 0.02));
572+
closeTo(updateNewLatLng.latLng!.latitude, latLngTestThreshold));
566573
expect(cameraIdlePosition.target.longitude,
567-
closeTo(updateNewLatLng.latLng!.longitude, 0.02));
574+
closeTo(updateNewLatLng.latLng!.longitude, latLngTestThreshold));
568575

569576
// Test that the other values haven't changed
570577
expect(cameraIdlePosition.bearing,
@@ -591,10 +598,14 @@ void main() {
591598
await cameraMethod(updateLatLngBounds);
592599

593600
// Test that the camera target matches the centre of the LatLngBounds.
594-
expect(cameraIdlePosition.target.latitude,
595-
closeTo(updateLatLngBounds.bounds!.center.latitude, 0.02));
596-
expect(cameraIdlePosition.target.longitude,
597-
closeTo(updateLatLngBounds.bounds!.center.longitude, 0.02));
601+
expect(
602+
cameraIdlePosition.target.latitude,
603+
closeTo(updateLatLngBounds.bounds!.center.latitude,
604+
latLngTestThreshold));
605+
expect(
606+
cameraIdlePosition.target.longitude,
607+
closeTo(updateLatLngBounds.bounds!.center.longitude,
608+
latLngTestThreshold));
598609

599610
// Test that the other values, excluding zoom, haven't changed.
600611
expect(cameraIdlePosition.bearing,
@@ -614,10 +625,10 @@ void main() {
614625
await cameraMethod(updateLatLngZoom);
615626

616627
// Test that the camera target and zoom match the provided target and zoom.
617-
expect(
618-
cameraIdlePosition.target.latitude, closeTo(target.latitude, 0.02));
628+
expect(cameraIdlePosition.target.latitude,
629+
closeTo(target.latitude, latLngTestThreshold));
619630
expect(cameraIdlePosition.target.longitude,
620-
closeTo(target.longitude, 0.02));
631+
closeTo(target.longitude, latLngTestThreshold));
621632
expect(cameraIdlePosition.zoom, closeTo(updateLatLngZoom.zoom!, 0.1));
622633

623634
// Test that the the other values haven't changed
@@ -664,9 +675,9 @@ void main() {
664675

665676
// Test that the [focus] parameter caused the camera position to change.
666677
expect(cameraIdlePosition.target.latitude,
667-
isNot(closeTo(target.latitude, 0.02)));
678+
isNot(closeTo(target.latitude, latLngTestThreshold)));
668679
expect(cameraIdlePosition.target.longitude,
669-
isNot(closeTo(target.longitude, 0.02)));
680+
isNot(closeTo(target.longitude, latLngTestThreshold)));
670681

671682
// Test that the zoom has changed to the specified value.
672683
expect(
@@ -700,10 +711,14 @@ void main() {
700711
// Test that the other values haven't changed.
701712
expect(cameraIdlePosition.bearing,
702713
closeTo(start.cameraPosition!.bearing, 0.1));
703-
expect(cameraIdlePosition.target.latitude,
704-
closeTo(start.cameraPosition!.target.latitude, 0.02));
705-
expect(cameraIdlePosition.target.longitude,
706-
closeTo(start.cameraPosition!.target.longitude, 0.02));
714+
expect(
715+
cameraIdlePosition.target.latitude,
716+
closeTo(
717+
start.cameraPosition!.target.latitude, latLngTestThreshold));
718+
expect(
719+
cameraIdlePosition.target.longitude,
720+
closeTo(
721+
start.cameraPosition!.target.longitude, latLngTestThreshold));
707722
expect(
708723
cameraIdlePosition.tilt, closeTo(start.cameraPosition!.tilt, 0.1));
709724

@@ -726,10 +741,14 @@ void main() {
726741
// Test that the target and camera tilt haven't changed.
727742
expect(cameraIdlePosition.bearing,
728743
closeTo(start.cameraPosition!.bearing, 0.1));
729-
expect(cameraIdlePosition.target.latitude,
730-
closeTo(start.cameraPosition!.target.latitude, 0.02));
731-
expect(cameraIdlePosition.target.longitude,
732-
closeTo(start.cameraPosition!.target.longitude, 0.02));
744+
expect(
745+
cameraIdlePosition.target.latitude,
746+
closeTo(
747+
start.cameraPosition!.target.latitude, latLngTestThreshold));
748+
expect(
749+
cameraIdlePosition.target.longitude,
750+
closeTo(
751+
start.cameraPosition!.target.longitude, latLngTestThreshold));
733752
expect(
734753
cameraIdlePosition.tilt, closeTo(start.cameraPosition!.tilt, 0.1));
735754

@@ -749,19 +768,20 @@ void main() {
749768
// Test that the target and camera tilt haven't changed.
750769
expect(cameraIdlePosition.bearing,
751770
closeTo(start.cameraPosition!.bearing, 0.1));
752-
expect(cameraIdlePosition.target.latitude,
753-
closeTo(start.cameraPosition!.target.latitude, 0.02));
754-
expect(cameraIdlePosition.target.longitude,
755-
closeTo(start.cameraPosition!.target.longitude, 0.02));
771+
expect(
772+
cameraIdlePosition.target.latitude,
773+
closeTo(
774+
start.cameraPosition!.target.latitude, latLngTestThreshold));
775+
expect(
776+
cameraIdlePosition.target.longitude,
777+
closeTo(
778+
start.cameraPosition!.target.longitude, latLngTestThreshold));
756779
expect(
757780
cameraIdlePosition.tilt, closeTo(start.cameraPosition!.tilt, 0.1));
758781

759782
await moveCameraToStart();
760783
}
761784
},
762-
// TODO(jokerttu): Fix flaky tests on Android. Sometimes camera location is
763-
// read before camera is animated to the new location causing the test to
764-
// fail.
765-
skip: Platform.isAndroid,
785+
variant: mapTypeVariants,
766786
);
767787
}

0 commit comments

Comments
 (0)