@@ -562,6 +562,61 @@ public void onEndFrame__revertsFlutterSurface() {
562562 assertEquals (flutterView .getRenderSurface ().getClass ().getSimpleName (), "FlutterSurfaceView" );
563563 }
564564
565+ @ Test
566+ @ Config (shadows = {ShadowFlutterSurfaceView .class , ShadowFlutterJNI .class })
567+ public void detach__destroysOverlaySurfaces () {
568+ final PlatformViewsController platformViewsController = new PlatformViewsController ();
569+
570+ final int platformViewId = 0 ;
571+ assertNull (platformViewsController .getPlatformViewById (platformViewId ));
572+
573+ final PlatformViewFactory viewFactory = mock (PlatformViewFactory .class );
574+ final PlatformView platformView = mock (PlatformView .class );
575+ when (platformView .getView ()).thenReturn (mock (View .class ));
576+ when (viewFactory .create (any (), eq (platformViewId ), any ())).thenReturn (platformView );
577+
578+ platformViewsController .getRegistry ().registerViewFactory ("testType" , viewFactory );
579+
580+ final FlutterJNI jni = new FlutterJNI ();
581+ jni .attachToNative (false );
582+ attach (jni , platformViewsController );
583+
584+ jni .onFirstFrame ();
585+
586+ // Simulate create call from the framework.
587+ createPlatformView (jni , platformViewsController , platformViewId , "testType" );
588+
589+ // Produce a frame that displays a platform view and an overlay surface.
590+ platformViewsController .onBeginFrame ();
591+ platformViewsController .onDisplayPlatformView (
592+ platformViewId ,
593+ /* x=*/ 0 ,
594+ /* y=*/ 0 ,
595+ /* width=*/ 10 ,
596+ /* height=*/ 10 ,
597+ /* viewWidth=*/ 10 ,
598+ /* viewHeight=*/ 10 ,
599+ /* mutatorsStack=*/ new FlutterMutatorsStack ());
600+
601+ final FlutterImageView overlayImageView = mock (FlutterImageView .class );
602+ when (overlayImageView .acquireLatestImage ()).thenReturn (true );
603+
604+ final FlutterOverlaySurface overlaySurface =
605+ platformViewsController .createOverlaySurface (overlayImageView );
606+ // This is OK.
607+ platformViewsController .onDisplayOverlaySurface (
608+ overlaySurface .getId (), /* x=*/ 0 , /* y=*/ 0 , /* width=*/ 10 , /* height=*/ 10 );
609+
610+ platformViewsController .detach ();
611+
612+ assertThrows (
613+ IllegalStateException .class ,
614+ () -> {
615+ platformViewsController .onDisplayOverlaySurface (
616+ overlaySurface .getId (), /* x=*/ 0 , /* y=*/ 0 , /* width=*/ 10 , /* height=*/ 10 );
617+ });
618+ }
619+
565620 @ Test
566621 public void checkInputConnectionProxy__falseIfViewIsNull () {
567622 final PlatformViewsController platformViewsController = new PlatformViewsController ();
0 commit comments