From 23d3ca2e2bd7993d4b57076796ddef5fd6beb94d Mon Sep 17 00:00:00 2001 From: utzcoz <43091780+utzcoz@users.noreply.github.com> Date: Fri, 23 Jun 2023 04:33:57 +0800 Subject: [PATCH] [android]: Make Robolectric tests work with SDK 33 (flutter/engine#42965) Make Robolectric tests work with SDK 33. The Robolectric doesn't support create presentation window now, so this CL also adds a custom presentation shadow to hook showing state to make related tests work. [C++, Objective-C, Java style guides]: https://github.com/flutter/engine/blob/main/CONTRIBUTING.md#style --- .../platform/PlatformViewsControllerTest.java | 36 +++++++++++++++++-- .../src/main/resources/robolectric.properties | 2 +- 2 files changed, 35 insertions(+), 3 deletions(-) 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 623b45ad4f9..09781b21ce7 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 @@ -9,6 +9,7 @@ import static org.mockito.ArgumentMatchers.*; import static org.mockito.Mockito.*; import static org.robolectric.Shadows.shadowOf; +import android.app.Presentation; import android.content.Context; import android.content.MutableContextWrapper; import android.content.res.AssetManager; @@ -57,6 +58,7 @@ import org.mockito.ArgumentCaptor; import org.robolectric.annotation.Config; import org.robolectric.annotation.Implementation; import org.robolectric.annotation.Implements; +import org.robolectric.shadows.ShadowDialog; import org.robolectric.shadows.ShadowSurfaceView; @Config(manifest = Config.NONE) @@ -570,7 +572,8 @@ public class PlatformViewsControllerTest { } @Test - @Config(shadows = {ShadowFlutterJNI.class, ShadowPlatformTaskQueue.class}) + @Config( + shadows = {ShadowFlutterJNI.class, ShadowPlatformTaskQueue.class, ShadowPresentation.class}) public void onDetachedFromJNI_clearsPlatformViewContext() { PlatformViewsController platformViewsController = new PlatformViewsController(); @@ -601,7 +604,8 @@ public class PlatformViewsControllerTest { } @Test - @Config(shadows = {ShadowFlutterJNI.class, ShadowPlatformTaskQueue.class}) + @Config( + shadows = {ShadowFlutterJNI.class, ShadowPlatformTaskQueue.class, ShadowPresentation.class}) public void onPreEngineRestart_clearsPlatformViewContext() { PlatformViewsController platformViewsController = new PlatformViewsController(); @@ -1521,6 +1525,34 @@ public class PlatformViewsControllerTest { } } + /** + * The shadow class of {@link Presentation} to simulate Presentation showing logic. + * + *

Robolectric doesn't support VirtualDisplay creating correctly now, so this shadow class is + * used to simulate custom logic for Presentation. + */ + @Implements(Presentation.class) + public static class ShadowPresentation extends ShadowDialog { + private boolean isShowing = false; + + public ShadowPresentation() {} + + @Implementation + protected void show() { + isShowing = true; + } + + @Implementation + protected void dismiss() { + isShowing = false; + } + + @Implementation + protected boolean isShowing() { + return isShowing; + } + } + @Implements(FlutterJNI.class) public static class ShadowFlutterJNI { private static SparseArray replies = new SparseArray<>(); diff --git a/engine/src/flutter/shell/platform/android/test_runner/src/main/resources/robolectric.properties b/engine/src/flutter/shell/platform/android/test_runner/src/main/resources/robolectric.properties index ba72a7e57eb..ebf6f5b5a47 100644 --- a/engine/src/flutter/shell/platform/android/test_runner/src/main/resources/robolectric.properties +++ b/engine/src/flutter/shell/platform/android/test_runner/src/main/resources/robolectric.properties @@ -1,2 +1,2 @@ -sdk=31 +sdk=33 shadows=io.flutter.CustomShadowContextImpl