Skip to content

Commit

Permalink
[google_maps_flutter] Fix integration tests (flutter#6350)
Browse files Browse the repository at this point in the history
Some integration tests behave slightly differently on Android when using
Hybrid Composition, and need to wait for a change to become true rather
than it being immediately true.

This replicates the changes from `google_maps_flutter_android` to the
app-facing copy of those integration tests.

Fixes tree breakage from publishing
flutter#6334
  • Loading branch information
stuartmorgan authored and Adam Harwood committed Nov 3, 2022
1 parent a714f20 commit 2149398
Showing 1 changed file with 35 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,28 @@ void main() {
IntegrationTestWidgetsFlutterBinding.ensureInitialized();
GoogleMapsFlutterPlatform.instance.enableDebugInspection();

// Repeatedly checks an asynchronous value against a test condition, waiting
// one frame between each check, returing the value if it passes the predicate
// before [maxTries] is reached.
//
// Returns null if the predicate is never satisfied.
//
// This is useful for cases where the Maps SDK has some internally
// asynchronous operation that we don't have visibility into (e.g., native UI
// animations).
Future<T?> waitForValueMatchingPredicate<T>(WidgetTester tester,
Future<T> Function() getValue, bool Function(T) predicate,
{int maxTries = 100}) async {
for (int i = 0; i < maxTries; i++) {
final T value = await getValue();
if (predicate(value)) {
return value;
}
await tester.pump();
}
return null;
}

testWidgets('testCompassToggle', (WidgetTester tester) async {
final Key key = GlobalKey();
final Completer<int> mapIdCompleter = Completer<int>();
Expand Down Expand Up @@ -481,12 +503,13 @@ void main() {
final GoogleMapController mapController =
await mapControllerCompleter.future;

// Wait for the visible region to be non-zero.
final LatLngBounds firstVisibleRegion =
await mapController.getVisibleRegion();

expect(firstVisibleRegion, isNotNull);
expect(firstVisibleRegion.southwest, isNotNull);
expect(firstVisibleRegion.northeast, isNotNull);
await waitForValueMatchingPredicate<LatLngBounds>(
tester,
() => mapController.getVisibleRegion(),
(LatLngBounds bounds) => bounds != zeroLatLngBounds) ??
zeroLatLngBounds;
expect(firstVisibleRegion, isNot(zeroLatLngBounds));
expect(firstVisibleRegion.contains(_kInitialMapCenter), isTrue);

Expand Down Expand Up @@ -517,9 +540,6 @@ void main() {
final LatLngBounds secondVisibleRegion =
await mapController.getVisibleRegion();

expect(secondVisibleRegion, isNotNull);
expect(secondVisibleRegion.southwest, isNotNull);
expect(secondVisibleRegion.northeast, isNotNull);
expect(secondVisibleRegion, isNot(zeroLatLngBounds));

expect(firstVisibleRegion, isNot(secondVisibleRegion));
Expand Down Expand Up @@ -906,7 +926,13 @@ void main() {
expect(iwVisibleStatus, false);

await controller.showMarkerInfoWindow(marker.markerId);
iwVisibleStatus = await controller.isMarkerInfoWindowShown(marker.markerId);
// The Maps SDK doesn't always return true for whether it is shown
// immediately after showing it, so wait for it to report as shown.
iwVisibleStatus = await waitForValueMatchingPredicate<bool>(
tester,
() => controller.isMarkerInfoWindowShown(marker.markerId),
(bool visible) => visible) ??
false;
expect(iwVisibleStatus, true);

await controller.hideMarkerInfoWindow(marker.markerId);
Expand Down

0 comments on commit 2149398

Please sign in to comment.