@@ -136,6 +136,11 @@ Future<void> checkTermsAndConditionsAcceptance(
136136 );
137137
138138 await $.pumpAndSettle ();
139+ // Force wait a bit for the dialog to appear.
140+ await $.tester.runAsync (
141+ () => Future .delayed (const Duration (milliseconds: 250 )),
142+ );
143+
139144 // Tap accept or cancel.
140145 if (Platform .isAndroid) {
141146 await $.native .tap (Selector (text: "Got It" ));
@@ -188,37 +193,17 @@ Future<GoogleNavigationViewController> startNavigation(
188193 void Function (CameraPosition )? onCameraStartedFollowingLocation,
189194 void Function (CameraPosition )? onCameraStoppedFollowingLocation,
190195}) async {
191- final ControllerCompleter <GoogleNavigationViewController >
192- controllerCompleter = ControllerCompleter ();
193-
194- await checkLocationDialogAndTosAcceptance ($);
195-
196- final Key key = GlobalKey ();
197- await pumpNavigationView (
198- $,
199- GoogleMapsNavigationView (
200- key: key,
201- onViewCreated: (GoogleNavigationViewController viewController) {
202- controllerCompleter.complete (viewController);
203- },
204- onCameraMoveStarted: onCameraMoveStarted,
205- onCameraMove: onCameraMove,
206- onCameraIdle: onCameraIdle,
207- onCameraStartedFollowingLocation: onCameraStartedFollowingLocation,
208- onCameraStoppedFollowingLocation: onCameraStoppedFollowingLocation,
209- ),
210- );
211-
212196 final GoogleNavigationViewController controller =
213- await controllerCompleter.future;
214-
215- await GoogleMapsNavigator .initializeNavigationSession ();
216- await $.pumpAndSettle ();
217-
218- await GoogleMapsNavigator .simulator.setUserLocation (
219- const LatLng (latitude: startLocationLat, longitude: startLocationLng),
220- );
221- await $.pumpAndSettle (timeout: const Duration (seconds: 1 ));
197+ await startNavigationWithoutDestination (
198+ $,
199+ initializeNavigation: true ,
200+ simulateLocation: true ,
201+ onCameraMoveStarted: onCameraMoveStarted,
202+ onCameraMove: onCameraMove,
203+ onCameraIdle: onCameraIdle,
204+ onCameraStartedFollowingLocation: onCameraStartedFollowingLocation,
205+ onCameraStoppedFollowingLocation: onCameraStoppedFollowingLocation,
206+ );
222207
223208 /// Set Destination.
224209 final Destinations destinations = Destinations (
@@ -236,11 +221,9 @@ Future<GoogleNavigationViewController> startNavigation(
236221 final NavigationRouteStatus status =
237222 await GoogleMapsNavigator .setDestinations (destinations);
238223 expect (status, NavigationRouteStatus .statusOk);
239- await $.pumpAndSettle ();
240224
241225 /// Start guidance.
242226 await GoogleMapsNavigator .startGuidance ();
243- await $.pumpAndSettle ();
244227
245228 expect (await GoogleMapsNavigator .isGuidanceRunning (), true );
246229
@@ -350,9 +333,13 @@ Future<GoogleNavigationViewController> startNavigationWithoutDestination(
350333 void Function (bool )? onNavigationUIEnabledChanged,
351334 void Function (String )? onPolygonClicked,
352335 void Function (String )? onPolylineClicked,
336+ void Function (CameraPosition , bool )? onCameraMoveStarted,
337+ void Function (CameraPosition )? onCameraMove,
338+ void Function (CameraPosition )? onCameraIdle,
339+ void Function (CameraPosition )? onCameraStartedFollowingLocation,
340+ void Function (CameraPosition )? onCameraStoppedFollowingLocation,
353341 void Function (NavigationViewRecenterButtonClickedEvent )?
354342 onRecenterButtonClicked,
355- void Function (CameraPosition )? onCameraIdle,
356343}) async {
357344 final Completer <GoogleNavigationViewController > controllerCompleter =
358345 Completer <GoogleNavigationViewController >();
@@ -383,24 +370,30 @@ Future<GoogleNavigationViewController> startNavigationWithoutDestination(
383370 onPolygonClicked: onPolygonClicked,
384371 onPolylineClicked: onPolylineClicked,
385372 onRecenterButtonClicked: onRecenterButtonClicked,
373+ onCameraMoveStarted: onCameraMoveStarted,
374+ onCameraMove: onCameraMove,
386375 onCameraIdle: onCameraIdle,
376+ onCameraStartedFollowingLocation: onCameraStartedFollowingLocation,
377+ onCameraStoppedFollowingLocation: onCameraStoppedFollowingLocation,
387378 ),
388379 );
389380
390381 final GoogleNavigationViewController controller =
391382 await controllerCompleter.future;
392- await $.pumpAndSettle ();
393383
394384 if (initializeNavigation) {
395385 await GoogleMapsNavigator .initializeNavigationSession ();
396- await $.pumpAndSettle ();
397386 }
398387
399388 if (simulateLocation) {
400- await GoogleMapsNavigator .simulator.setUserLocation (
401- const LatLng (latitude: startLocationLat, longitude: startLocationLng),
389+ const double tolerance = 0.001 ;
390+ await setSimulatedUserLocationWithCheck (
391+ $,
392+ controller,
393+ startLocationLat,
394+ startLocationLng,
395+ tolerance,
402396 );
403- await $.pumpAndSettle (timeout: const Duration (seconds: 1 ));
404397 }
405398
406399 return controller;
@@ -432,8 +425,6 @@ Future<GoogleMapViewController> startMapView(
432425 final ControllerCompleter <GoogleMapViewController > controllerCompleter =
433426 ControllerCompleter ();
434427
435- //await checkLocationDialogAndTosAcceptance($);
436-
437428 final Key key = GlobalKey ();
438429 await pumpMapView (
439430 $,
@@ -485,11 +476,58 @@ Future<Value?> waitForValueMatchingPredicate<Value>(
485476 if (predicate (currentValue)) {
486477 return currentValue;
487478 }
488- await $.pump (Duration (milliseconds: delayMs));
479+ await $.tester.runAsync (
480+ () => Future .delayed (Duration (milliseconds: delayMs)),
481+ );
489482 }
490483 return null ;
491484}
492485
486+ /// Sets the simulated user location to [startLat] , [startLng] and checks that
487+ /// the location was set correctly within the given [tolerance] .
488+ Future <void > setSimulatedUserLocationWithCheck (
489+ PatrolIntegrationTester $,
490+ GoogleNavigationViewController viewController,
491+ double startLat,
492+ double startLng,
493+ double tolerance,
494+ ) async {
495+ // Simulate location
496+ await GoogleMapsNavigator .simulator.setUserLocation (
497+ LatLng (latitude: startLat, longitude: startLng),
498+ );
499+
500+ // Wait a bit for the location to be set, as this always seems to take a bit
501+ // of time on both Android and iOS platforms.
502+ await $.tester.runAsync (
503+ () => Future .delayed (const Duration (milliseconds: 500 )),
504+ );
505+
506+ final LatLng ? currentLocation = await waitForValueMatchingPredicate <LatLng ?>(
507+ $,
508+ viewController.getMyLocation,
509+ (LatLng ? location) {
510+ if (location == null ) return false ;
511+
512+ bool isCloseTo (double a, double b) {
513+ var diff = a - b;
514+ if (diff < 0 ) diff = - diff;
515+ return diff <= tolerance;
516+ }
517+
518+ return isCloseTo (location.latitude, startLat) &&
519+ isCloseTo (location.longitude, startLng);
520+ },
521+ // Total time before giving up: 100 * 200ms = 20s
522+ maxTries: 100 ,
523+ delayMs: 200 ,
524+ );
525+
526+ expect (currentLocation, isNotNull);
527+ expect (currentLocation? .latitude, closeTo (startLat, tolerance));
528+ expect (currentLocation? .longitude, closeTo (startLng, tolerance));
529+ }
530+
493531// Convert a Color to an integer.
494532int ? colorToInt (Color ? color) {
495533 if (color == null ) {
0 commit comments