[android]: Make Robolectric tests work with SDK 33 (#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
diff --git a/shell/platform/android/test/io/flutter/plugin/platform/PlatformViewsControllerTest.java b/shell/platform/android/test/io/flutter/plugin/platform/PlatformViewsControllerTest.java
index 623b45a..09781b2 100644
--- a/shell/platform/android/test/io/flutter/plugin/platform/PlatformViewsControllerTest.java
+++ b/shell/platform/android/test/io/flutter/plugin/platform/PlatformViewsControllerTest.java
@@ -9,6 +9,7 @@
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.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 @@
}
@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 @@
}
@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 @@
}
}
+ /**
+ * The shadow class of {@link Presentation} to simulate Presentation showing logic.
+ *
+ * <p>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<ByteBuffer> replies = new SparseArray<>();
diff --git a/shell/platform/android/test_runner/src/main/resources/robolectric.properties b/shell/platform/android/test_runner/src/main/resources/robolectric.properties
index ba72a7e..ebf6f5b 100644
--- a/shell/platform/android/test_runner/src/main/resources/robolectric.properties
+++ b/shell/platform/android/test_runner/src/main/resources/robolectric.properties
@@ -1,2 +1,2 @@
-sdk=31
+sdk=33
shadows=io.flutter.CustomShadowContextImpl