From b181ae8f637badc0096d0bec1ee14f6dbf04794f Mon Sep 17 00:00:00 2001 From: Gary Qian Date: Thu, 23 Apr 2020 15:32:20 -0700 Subject: [PATCH] Hand off presentation properly in VirtualDisplayController.resize() (flutter/engine#17897) --- .../platform/VirtualDisplayController.java | 13 ++++++++--- .../platform/PlatformViewsControllerTest.java | 22 +++++++++++++++++++ 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/engine/src/flutter/shell/platform/android/io/flutter/plugin/platform/VirtualDisplayController.java b/engine/src/flutter/shell/platform/android/io/flutter/plugin/platform/VirtualDisplayController.java index 7c4b6bec8a..ec037ee680 100644 --- a/engine/src/flutter/shell/platform/android/io/flutter/plugin/platform/VirtualDisplayController.java +++ b/engine/src/flutter/shell/platform/android/io/flutter/plugin/platform/VirtualDisplayController.java @@ -16,6 +16,7 @@ import android.view.Surface; import android.view.View; import android.view.ViewTreeObserver; import androidx.annotation.NonNull; +import androidx.annotation.VisibleForTesting; import io.flutter.view.TextureRegistry; @TargetApi(Build.VERSION_CODES.KITKAT_WATCH) @@ -62,7 +63,7 @@ class VirtualDisplayController { private final TextureRegistry.SurfaceTextureEntry textureEntry; private final OnFocusChangeListener focusChangeListener; private VirtualDisplay virtualDisplay; - private SingleViewPresentation presentation; + @VisibleForTesting SingleViewPresentation presentation; private Surface surface; private VirtualDisplayController( @@ -145,7 +146,11 @@ class VirtualDisplayController { public void onViewDetachedFromWindow(View v) {} }); - presentation = + // Create a new SingleViewPresentation and show() it before we cancel() the existing + // presentation. Calling show() and cancel() in this order fixes + // https://github.com/flutter/flutter/issues/26345 and maintains seamless transition + // of the contents of the presentation. + SingleViewPresentation newPresentation = new SingleViewPresentation( context, virtualDisplay.getDisplay(), @@ -153,7 +158,9 @@ class VirtualDisplayController { presentationState, focusChangeListener, isFocused); - presentation.show(); + newPresentation.show(); + presentation.cancel(); + presentation = newPresentation; } public void dispose() { diff --git a/engine/src/flutter/shell/platform/android/test/io/flutter/plugin/platform/PlatformViewsControllerTest.java b/engine/src/flutter/shell/platform/android/test/io/flutter/plugin/platform/PlatformViewsControllerTest.java index 28c2d3fe86..8b2dfce5fb 100644 --- a/engine/src/flutter/shell/platform/android/test/io/flutter/plugin/platform/PlatformViewsControllerTest.java +++ b/engine/src/flutter/shell/platform/android/test/io/flutter/plugin/platform/PlatformViewsControllerTest.java @@ -57,4 +57,26 @@ public class PlatformViewsControllerTest { verify(fakeVdController2, times(1)).onFlutterViewAttached(eq(fakeFlutterView)); verify(fakeVdController2, times(1)).onFlutterViewDetached(); } + + @Test + public void itCancelsOldPresentationOnResize() { + // Setup test structure. + // Create a fake View that represents the View that renders a Flutter UI. + View fakeFlutterView = new View(RuntimeEnvironment.systemContext); + + // Create fake VirtualDisplayControllers. This requires internal knowledge of + // PlatformViewsController. We know that all PlatformViewsController does is + // forward view attachment/detachment calls to it's VirtualDisplayControllers. + // + // TODO(mattcarroll): once PlatformViewsController is refactored into testable + // pieces, remove this test and avoid verifying private behavior. + VirtualDisplayController fakeVdController1 = mock(VirtualDisplayController.class); + + SingleViewPresentation presentation = fakeVdController1.presentation; + + fakeVdController1.resize(10, 10, null); + + assertEquals(fakeVdController1.presentation != presentation, true); + assertEquals(presentation.isShowing(), false); + } }