@@ -22,6 +22,28 @@ void main() {
22
22
IntegrationTestWidgetsFlutterBinding .ensureInitialized ();
23
23
GoogleMapsFlutterPlatform .instance.enableDebugInspection ();
24
24
25
+ // Repeatedly checks an asynchronous value against a test condition, waiting
26
+ // one frame between each check, returing the value if it passes the predicate
27
+ // before [maxTries] is reached.
28
+ //
29
+ // Returns null if the predicate is never satisfied.
30
+ //
31
+ // This is useful for cases where the Maps SDK has some internally
32
+ // asynchronous operation that we don't have visibility into (e.g., native UI
33
+ // animations).
34
+ Future <T ?> waitForValueMatchingPredicate <T >(WidgetTester tester,
35
+ Future <T > Function () getValue, bool Function (T ) predicate,
36
+ {int maxTries = 100 }) async {
37
+ for (int i = 0 ; i < maxTries; i++ ) {
38
+ final T value = await getValue ();
39
+ if (predicate (value)) {
40
+ return value;
41
+ }
42
+ await tester.pump ();
43
+ }
44
+ return null ;
45
+ }
46
+
25
47
testWidgets ('testCompassToggle' , (WidgetTester tester) async {
26
48
final Key key = GlobalKey ();
27
49
final Completer <int > mapIdCompleter = Completer <int >();
@@ -481,12 +503,13 @@ void main() {
481
503
final GoogleMapController mapController =
482
504
await mapControllerCompleter.future;
483
505
506
+ // Wait for the visible region to be non-zero.
484
507
final LatLngBounds firstVisibleRegion =
485
- await mapController. getVisibleRegion ();
486
-
487
- expect (firstVisibleRegion, isNotNull);
488
- expect (firstVisibleRegion.southwest, isNotNull);
489
- expect (firstVisibleRegion.northeast, isNotNull) ;
508
+ await waitForValueMatchingPredicate < LatLngBounds >(
509
+ tester,
510
+ () => mapController. getVisibleRegion (),
511
+ ( LatLngBounds bounds) => bounds != zeroLatLngBounds) ??
512
+ zeroLatLngBounds ;
490
513
expect (firstVisibleRegion, isNot (zeroLatLngBounds));
491
514
expect (firstVisibleRegion.contains (_kInitialMapCenter), isTrue);
492
515
@@ -517,9 +540,6 @@ void main() {
517
540
final LatLngBounds secondVisibleRegion =
518
541
await mapController.getVisibleRegion ();
519
542
520
- expect (secondVisibleRegion, isNotNull);
521
- expect (secondVisibleRegion.southwest, isNotNull);
522
- expect (secondVisibleRegion.northeast, isNotNull);
523
543
expect (secondVisibleRegion, isNot (zeroLatLngBounds));
524
544
525
545
expect (firstVisibleRegion, isNot (secondVisibleRegion));
@@ -906,7 +926,13 @@ void main() {
906
926
expect (iwVisibleStatus, false );
907
927
908
928
await controller.showMarkerInfoWindow (marker.markerId);
909
- iwVisibleStatus = await controller.isMarkerInfoWindowShown (marker.markerId);
929
+ // The Maps SDK doesn't always return true for whether it is shown
930
+ // immediately after showing it, so wait for it to report as shown.
931
+ iwVisibleStatus = await waitForValueMatchingPredicate <bool >(
932
+ tester,
933
+ () => controller.isMarkerInfoWindowShown (marker.markerId),
934
+ (bool visible) => visible) ??
935
+ false ;
910
936
expect (iwVisibleStatus, true );
911
937
912
938
await controller.hideMarkerInfoWindow (marker.markerId);
0 commit comments