Repository: flutter
Issue: IntegrationTestWidgetsFlutterBinding takeScreenshot gets stuck for GoogleMaps
Labels: platform-android,framework,a: platform-views,p: maps,package,f: integration_test,has reproducible steps,P2,found in release: 3.10,found in release: 3.11,team-android,triaged-android

### Is there an existing issue for this?

- [X] I have searched the [existing issues](https://github.com/flutter/flutter/issues)
- [X] I have read the [guide to filing a bug](https://flutter.dev/docs/resources/bug-reports)

### Steps to reproduce

1. Go to flutter/packages google_maps_flutter [example](https://github.com/flutter/packages/tree/main/packages/google_maps_flutter/google_maps_flutter/example) and open it in Android Studio.
2. Use Flutter 3.10.1 • channel stable
3. Go to `app/src/main/AndroidManifest.xml` and provide a Google Api Key at `com.google.android.geo.API_KEY`.
4. Go to `test_driver/integration_test.dart` and add the following code:

```
import 'dart:io';

import 'package:integration_test/integration_test_driver_extended.dart';

Future<void> main() async {
  return integrationDriver(
    onScreenshot: (String name, List<int> image,
        [Map<String, Object?>? args]) async {
      final File img = File('screenshots/$name.png');
      img.writeAsBytesSync(image);
      // Return false if the screenshot is invalid.
      return true;
    },
  );
}
```
5. Create a `screenshots` folder under project root folder.
6. Go to `integration_test/google_map_test.dart` and add `takeScreenshot` to `testCompassToggle` test.

```
testWidgets('testCompassToggle', (WidgetTester tester) async {
    final Key key = GlobalKey();
    final Completer<int> mapIdCompleter = Completer<int>();
    await tester.pumpWidget(Directionality(
      textDirection: TextDirection.ltr,
      child: GoogleMap(
        key: key,
        initialCameraPosition: _kInitialCameraPosition,
        compassEnabled: false,
        onMapCreated: (GoogleMapController controller) {
          mapIdCompleter.complete(controller.mapId);
        },
      ),
    ));

    final int mapId = await mapIdCompleter.future;
    final GoogleMapsInspectorPlatform inspector =
        GoogleMapsInspectorPlatform.instance!;
    bool compassEnabled = await inspector.isCompassEnabled(mapId: mapId);
    expect(compassEnabled, false);

    await tester.pumpWidget(Directionality(
      textDirection: TextDirection.ltr,
      child: GoogleMap(
        key: key,
        initialCameraPosition: _kInitialCameraPosition,
        onMapCreated: (GoogleMapController controller) {
          fail('OnMapCreated should get called only once.');
        },
      ),
    ));

    compassEnabled = await inspector.isCompassEnabled(mapId: mapId);
    expect(compassEnabled, true);

    // Added take screenshot <<<<
    await binding.convertFlutterSurfaceToImage();

    await tester.pumpAndSettle();

    await binding.takeScreenshot("myscreenshot");
  });
```
7. Run the test from project root folder with the command: `flutter drive --driver=test_driver/integration_test.dart --target=integration_test/google_maps_test.dart`



### Expected results

On `testCompassToggle` test run the screenshot `myscreenshot.png` should be generated inside `screenshots` folder.

### Actual results

The `await binding.takeScreenshot("myscreenshot");` method call hangs indefinitely and the test doesn't finish. A screenshot isn't generated. The issue is reproducing on both real device and emulator.

### Code sample

I've presented in the steps the code samples and the steps needed to reproduce the issue in "Steps to Reproduce" section.

### Screenshots or Video

<details>
<summary>Screenshots / Video demonstration</summary>

[Upload media here]

</details>


### Logs

```console
ivan@Ivans-Mac-mini example % flutter drive --driver=test_driver/integration_test.dart --target=integration_test/google_map_test.dart
Target file "integration_test/google_map_test.dart" not found.
ivan@Ivans-Mac-mini example % flutter drive --driver=test_driver/integration_test.dart --target=integration_test/google_maps_test.dart 
^C%                                                                                                                            ivan@Ivans-Mac-mini example % clear
ivan@Ivans-Mac-mini example % flutter drive --driver=test_driver/integration_test.dart --target=integration_test/google_maps_test.dart
Resolving dependencies... 
  collection 1.17.1 (1.17.2 available)
  espresso 0.2.1 (0.3.0+5 available)
  file 6.1.4 (7.0.0 available)
  matcher 0.12.15 (0.12.16 available)
  material_color_utilities 0.2.0 (0.5.0 available)
  source_span 1.9.1 (1.10.0 available)
  test_api 0.5.1 (0.6.0 available)
  vm_service 11.3.0 (11.6.0 available)
Got dependencies!
Connected devices:
Pixel 4a (mobile)           • 0A281JEC208152 • android-arm64  • Android 13 (API 33)
sdk gphone64 arm64 (mobile) • emulator-5554  • android-arm64  • Android 13 (API 33) (emulator)
sdk gphone64 arm64 (mobile) • emulator-5556  • android-arm64  • Android 13 (API 33) (emulator)
macOS (desktop)             • macos          • darwin-arm64   • macOS 13.3.1 22E772610a darwin-arm64
Chrome (web)                • chrome         • web-javascript • Google Chrome 113.0.5672.126

No wireless devices were found.

[1]: Pixel 4a (0A281JEC208152)
[2]: sdk gphone64 arm64 (emulator-5554)
[3]: sdk gphone64 arm64 (emulator-5556)
[4]: macOS (macos)
[5]: Chrome (chrome)
Please choose one (or "q" to quit): 1
Running Gradle task 'assembleDebug'...                              4.6s
✓  Built build/app/outputs/flutter-apk/app-debug.apk.
Installing build/app/outputs/flutter-apk/app-debug.apk...           4.2s
I/oglemapsexample(14942): Compiler allocated 4413KB to compile void android.view.ViewRootImpl.performTraversals()
VMServiceFlutterDriver: Connecting to Flutter application at http://127.0.0.1:64996/-mPzezvXQ7k=/
VMServiceFlutterDriver: Isolate found with number: 743515918418887
VMServiceFlutterDriver: Isolate is paused at start.
VMServiceFlutterDriver: Attempting to resume isolate
I/flutter (14942): 00:00 +0: testCompassToggle
VMServiceFlutterDriver: Connected to Flutter application.
W/Parcel  (14942): Expecting binder but got null!
D/MapsInitializer(14942): preferredRenderer: null
D/zzcb    (14942): preferredRenderer: null
I/zzcb    (14942): Making Creator dynamically
I/DynamiteModule(14942): Considering local module com.google.android.gms.maps_dynamite:0 and remote module
com.google.android.gms.maps_dynamite:203115000
I/DynamiteModule(14942): Selected remote version of com.google.android.gms.maps_dynamite, version >= 203115000
V/DynamiteModule(14942): Dynamite loader version >= 2, using loadModule2NoCrashUtils
W/System  (14942): ClassLoader referenced unknown path: 
D/nativeloader(14942): Configuring classloader-namespace for other apk . target_sdk_version=33, uses_libraries=,
library_path=/data/app/~~65Pju9QF0s1RXZ-XSNKHwA==/com.google.android.gms-bqiUoiBUMa-cOy08aUZk7Q==/lib/arm64:/data/app/~~65Pju9Q
F0s1RXZ-XSNKHwA==/com.google.android.gms-bqiUoiBUMa-cOy08aUZk7Q==/base.apk!/lib/arm64-v8a,
permitted_path=/data:/mnt/expand:/data/user/0/com.google.android.gms
I/DynamiteLoaderV2Impl(14942): [71] com.google.android.gms.maps_dynamite
I/Google Maps Android API(14942): Google Play services client version: 12451000
D/CompatibilityChangeReporter(14942): Compat change id reported: 183155436; UID 10266; state: DISABLED
I/SnapshotHandler(14942): Unable to retrieve flag snapshot for
com.google.android.gms.maps#io.flutter.plugins.googlemapsexample, using defaults.
I/Google Maps Android API(14942): Google Play services package version: 231613044
I/Google Maps Android API(14942): Google Play services maps renderer version(legacy): 203115000
D/MapsInitializer(14942): loadedRenderer: LEGACY
D/zzcb    (14942): preferredRenderer: null
I/Google Maps Android API(14942): Google Play services package version: 231613044
I/Google Maps Android API(14942): Google Play services maps renderer version(legacy): 203115000
D/CompatibilityChangeReporter(14942): Compat change id reported: 210923482; UID 10266; state: DISABLED
D/CompatibilityChangeReporter(14942): Compat change id reported: 37756858; UID 10266; state: ENABLED
D/CompatibilityChangeReporter(14942): Compat change id reported: 171228096; UID 10266; state: ENABLED
I/PlatformViewsController(14942): Using hybrid composition for platform view: 0
E/GoogleMapController(14942): Cannot enable MyLocation layer as location permissions are not granted
I/bb      (14942): Successfully registered with Phenotype.
D/TrafficStats(14942): tagSocket(163) with statsTag=0x30001101, statsUid=-1
D/TrafficStats(14942): tagSocket(192) with statsTag=0x30001101, statsUid=-1
D/TrafficStats(14942): tagSocket(201) with statsTag=0x30001101, statsUid=-1
D/TrafficStats(14942): tagSocket(208) with statsTag=0x30001101, statsUid=-1
W/DynamiteModule(14942): Local module descriptor class for com.google.android.gms.googlecertificates not found.
I/DynamiteModule(14942): Considering local module com.google.android.gms.googlecertificates:0 and remote module
com.google.android.gms.googlecertificates:7
I/DynamiteModule(14942): Selected remote version of com.google.android.gms.googlecertificates, version >= 7
I/DynamiteLoaderV2Impl(14942): [71] com.google.android.gms.googlecertificates
I/SnapshotHandler(14942): Unable to retrieve flag snapshot for
com.google.android.libraries.consentverifier#io.flutter.plugins.googlemapsexample, using defaults.
W/MobStoreFlagStore(14942): Unable to update local snapshot for
com.google.android.libraries.consentverifier#io.flutter.plugins.googlemapsexample, may result in stale flags.
W/MobStoreFlagStore(14942): java.util.concurrent.ExecutionException: java.lang.SecurityException: GoogleCertificatesRslt: not
allowed: pkg=io.flutter.plugins.googlemapsexample, sha256=[9ec0a203d6c494daf12a14ea7db1980252b60c3a1c1780953ee049d160875e56],
atk=false, ver=231613044.true (go/gsrlt)
W/MobStoreFlagStore(14942):     at ajh.s(:com.google.android.gms.dynamite_mapsdynamite@231613044@23.16.13 (190400-0):3)
W/MobStoreFlagStore(14942):     at ajh.get(:com.google.android.gms.dynamite_mapsdynamite@231613044@23.16.13 (190400-0):2)
W/MobStoreFlagStore(14942):     at akr.g(:com.google.android.gms.dynamite_mapsdynamite@231613044@23.16.13 (190400-0):2)
W/MobStoreFlagStore(14942):     at yp.c(:com.google.android.gms.dynamite_mapsdynamite@231613044@23.16.13 (190400-0):1)
W/MobStoreFlagStore(14942):     at yr.run(:com.google.android.gms.dynamite_mapsdynamite@231613044@23.16.13 (190400-0):0)
W/MobStoreFlagStore(14942):     at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:463)
W/MobStoreFlagStore(14942):     at java.util.concurrent.FutureTask.run(FutureTask.java:264)
W/MobStoreFlagStore(14942):     at
java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:307)
W/MobStoreFlagStore(14942):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1137)
W/MobStoreFlagStore(14942):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:637)
W/MobStoreFlagStore(14942):     at java.lang.Thread.run(Thread.java:1012)
W/MobStoreFlagStore(14942): Caused by: java.lang.SecurityException: GoogleCertificatesRslt: not allowed:
pkg=io.flutter.plugins.googlemapsexample, sha256=[9ec0a203d6c494daf12a14ea7db1980252b60c3a1c1780953ee049d160875e56], atk=false,
ver=231613044.true (go/gsrlt)
W/MobStoreFlagStore(14942):     at android.os.Parcel.createExceptionOrNull(Parcel.java:3011)
W/MobStoreFlagStore(14942):     at android.os.Parcel.createException(Parcel.java:2995)
W/MobStoreFlagStore(14942):     at android.os.Parcel.readException(Parcel.java:2978)
W/MobStoreFlagStore(14942):     at android.os.Parcel.readException(Parcel.java:2920)
W/MobStoreFlagStore(14942):     at fe.c(:com.google.android.gms.dynamite_mapsdynamite@231613044@23.16.13 (190400-0):2)
W/MobStoreFlagStore(14942):     at rs.a(:com.google.android.gms.dynamite_mapsdynamite@231613044@23.16.13 (190400-0):4)
W/MobStoreFlagStore(14942):     at jk.e(:com.google.android.gms.dynamite_mapsdynamite@231613044@23.16.13 (190400-0):2)
W/MobStoreFlagStore(14942):     at ki.t(:com.google.android.gms.dynamite_mapsdynamite@231613044@23.16.13 (190400-0):2)
W/MobStoreFlagStore(14942):     at ki.u(:com.google.android.gms.dynamite_mapsdynamite@231613044@23.16.13 (190400-0):3)
W/MobStoreFlagStore(14942):     at ki.e(:com.google.android.gms.dynamite_mapsdynamite@231613044@23.16.13 (190400-0):2)
W/MobStoreFlagStore(14942):     at km.handleMessage(:com.google.android.gms.dynamite_mapsdynamite@231613044@23.16.13
(190400-0):74)
W/MobStoreFlagStore(14942):     at android.os.Handler.dispatchMessage(Handler.java:102)
W/MobStoreFlagStore(14942):     at android.os.Looper.loopOnce(Looper.java:201)
W/MobStoreFlagStore(14942):     at android.os.Looper.loop(Looper.java:288)
W/MobStoreFlagStore(14942):     at android.os.HandlerThread.run(HandlerThread.java:67)
VMServiceFlutterDriver: request_data message is taking a long time to complete...


```

### Flutter Doctor output

<details><summary>Doctor output</summary>

```console
Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, 3.10.1, on macOS 13.3.1 22E772610a darwin-arm64, locale en-DE)
[✓] Android toolchain - develop for Android devices (Android SDK version 33.0.1)
[✓] Xcode - develop for iOS and macOS (Xcode 14.2)
[✓] Chrome - develop for the web
[✓] Android Studio (version 2022.1)
[✓] IntelliJ IDEA Community Edition (version 2023.1)
[✓] VS Code (version 1.76.2)
[✓] Connected device (5 available)
[✓] Network resources

• No issues found!

```

</details>