CTS: regression test for overlaying IME picker
Bug: 181660568
Test: atest InputMethodPickerTest
Change-Id: Id17ff13bebe88ac8ff07efe0155fe7acc3470c97
diff --git a/tests/framework/base/windowmanager/util/src/android/server/wm/WindowManagerStateHelper.java b/tests/framework/base/windowmanager/util/src/android/server/wm/WindowManagerStateHelper.java
index 56a98d0..a70f507 100644
--- a/tests/framework/base/windowmanager/util/src/android/server/wm/WindowManagerStateHelper.java
+++ b/tests/framework/base/windowmanager/util/src/android/server/wm/WindowManagerStateHelper.java
@@ -305,7 +305,7 @@
}, windowName + "'s surface is disappeared");
}
- void waitAndAssertWindowSurfaceShown(String windowName, boolean shown) {
+ public void waitAndAssertWindowSurfaceShown(String windowName, boolean shown) {
assertTrue(
waitForWithAmState(state -> state.isWindowSurfaceShown(windowName) == shown,
windowName + "'s isWindowSurfaceShown to return " + shown));
diff --git a/tests/inputmethod/Android.bp b/tests/inputmethod/Android.bp
index 90a8c2f..4902be2 100644
--- a/tests/inputmethod/Android.bp
+++ b/tests/inputmethod/Android.bp
@@ -27,9 +27,11 @@
compile_multilib: "both",
libs: ["android.test.runner"],
static_libs: [
+ "androidx.test.ext.junit",
"androidx.test.rules",
"androidx.test.uiautomator_uiautomator",
"compatibility-device-util-axt",
+ "cts-wm-util",
"ctstestrunner-axt",
"CtsMockInputMethodLib",
"CtsMockSpellCheckerLib",
diff --git a/tests/inputmethod/src/android/view/inputmethod/cts/InputMethodPickerTest.java b/tests/inputmethod/src/android/view/inputmethod/cts/InputMethodPickerTest.java
new file mode 100644
index 0000000..3064c30
--- /dev/null
+++ b/tests/inputmethod/src/android/view/inputmethod/cts/InputMethodPickerTest.java
@@ -0,0 +1,103 @@
+/*
+ * Copyright (C) 2021 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package android.view.inputmethod.cts;
+
+import static android.content.Intent.ACTION_CLOSE_SYSTEM_DIALOGS;
+import static android.content.Intent.FLAG_RECEIVER_FOREGROUND;
+import static android.content.pm.PackageManager.FEATURE_INPUT_METHODS;
+import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;
+import static android.view.inputmethod.cts.util.TestUtils.waitOnMainUntil;
+
+import static androidx.test.platform.app.InstrumentationRegistry.getInstrumentation;
+
+import static org.junit.Assume.assumeTrue;
+
+import android.content.Intent;
+import android.platform.test.annotations.AppModeFull;
+import android.platform.test.annotations.SecurityTest;
+import android.server.wm.ActivityManagerTestBase;
+import android.view.View;
+import android.view.inputmethod.InputMethodManager;
+import android.view.inputmethod.cts.util.TestActivity;
+import android.widget.LinearLayout;
+
+import androidx.test.ext.junit.runners.AndroidJUnit4;
+import androidx.test.filters.MediumTest;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+import java.util.concurrent.TimeUnit;
+
+@MediumTest
+@RunWith(AndroidJUnit4.class)
+public class InputMethodPickerTest extends ActivityManagerTestBase {
+
+ private static final long TIMEOUT = TimeUnit.SECONDS.toMillis(5);
+
+ private InputMethodManager mImManager;
+
+ @Before
+ public void setUp() throws Exception {
+ super.setUp();
+ mImManager = mContext.getSystemService(InputMethodManager.class);
+
+ closeSystemDialogsAndWait();
+ }
+
+ @After
+ public void tearDown() throws Exception {
+ closeSystemDialogsAndWait();
+ }
+
+ @AppModeFull(reason = "Instant apps cannot rely on ACTION_CLOSE_SYSTEM_DIALOGS")
+ @SecurityTest(minPatchLevel = "unknown")
+ @Test
+ public void testInputMethodPicker_hidesUntrustedOverlays() throws Exception {
+ assumeTrue(mContext.getPackageManager().hasSystemFeature(FEATURE_INPUT_METHODS));
+ TestActivity testActivity = TestActivity.startSync(activity -> {
+ final View view = new View(activity);
+ view.setLayoutParams(new LinearLayout.LayoutParams(MATCH_PARENT, MATCH_PARENT));
+ return view;
+ });
+
+ // Test setup: Show overlay and verify that it worked.
+ getInstrumentation().runOnMainSync(() -> {
+ testActivity.showOverlayWindow();
+ });
+ mWmState.waitAndAssertWindowSurfaceShown(TestActivity.OVERLAY_WINDOW_NAME, true);
+
+ // Test setup: Show the IME picker and verify that it worked.
+ getInstrumentation().runOnMainSync(() -> {
+ mImManager.showInputMethodPicker();
+ });
+ waitOnMainUntil(() -> mImManager.isInputMethodPickerShown(), TIMEOUT,
+ "Test setup failed: InputMethod picker should be shown");
+
+ // Actual Test: Make sure the IME picker hides app overlays.
+ mWmState.waitAndAssertWindowSurfaceShown(TestActivity.OVERLAY_WINDOW_NAME, false);
+ }
+
+ private void closeSystemDialogsAndWait() throws Exception {
+ mContext.sendBroadcast(
+ new Intent(ACTION_CLOSE_SYSTEM_DIALOGS).setFlags(FLAG_RECEIVER_FOREGROUND));
+ waitOnMainUntil(() -> !mImManager.isInputMethodPickerShown(), TIMEOUT,
+ "Test assertion failed: InputMethod picker should be closed but isn't");
+ }
+}
diff --git a/tests/inputmethod/src/android/view/inputmethod/cts/util/TestActivity.java b/tests/inputmethod/src/android/view/inputmethod/cts/util/TestActivity.java
index 398e7b6..78d7835 100644
--- a/tests/inputmethod/src/android/view/inputmethod/cts/util/TestActivity.java
+++ b/tests/inputmethod/src/android/view/inputmethod/cts/util/TestActivity.java
@@ -16,14 +16,20 @@
package android.view.inputmethod.cts.util;
+import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;
+import static android.view.WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
import static android.view.WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED;
+import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
import android.app.Activity;
+import android.content.Context;
import android.content.Intent;
+import android.graphics.PixelFormat;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
+import android.widget.TextView;
import androidx.annotation.AnyThread;
import androidx.annotation.NonNull;
@@ -36,6 +42,7 @@
public class TestActivity extends Activity {
+ public static final String OVERLAY_WINDOW_NAME = "TestActivity.APP_OVERLAY_WINDOW";
private static final AtomicReference<Function<TestActivity, View>> sInitializer =
new AtomicReference<>();
@@ -45,6 +52,8 @@
private long mOnBackPressedCallCount;
+ private TextView mOverlayView;
+
/**
* Controls how {@link #onBackPressed()} behaves.
*
@@ -82,6 +91,16 @@
setContentView(mInitializer.apply(this));
}
+ @Override
+ protected void onDestroy() {
+ super.onDestroy();
+ if (mOverlayView != null) {
+ mOverlayView.getContext()
+ .getSystemService(WindowManager.class).removeView(mOverlayView);
+ mOverlayView = null;
+ }
+ }
+
/**
* {@inheritDoc}
*/
@@ -94,6 +113,23 @@
super.onBackPressed();
}
+ public void showOverlayWindow() {
+ if (mOverlayView != null) {
+ throw new IllegalStateException("can only show one overlay at a time.");
+ }
+ Context overlayContext = getApplicationContext().createWindowContext(getDisplay(),
+ TYPE_APPLICATION_OVERLAY, null);
+ mOverlayView = new TextView(overlayContext);
+ WindowManager.LayoutParams params =
+ new WindowManager.LayoutParams(MATCH_PARENT, MATCH_PARENT,
+ TYPE_APPLICATION_OVERLAY, FLAG_NOT_FOCUSABLE, PixelFormat.TRANSLUCENT);
+ params.setTitle(OVERLAY_WINDOW_NAME);
+ mOverlayView.setLayoutParams(params);
+ mOverlayView.setText("IME CTS TestActivity OverlayView");
+ mOverlayView.setBackgroundColor(0x77FFFF00);
+ overlayContext.getSystemService(WindowManager.class).addView(mOverlayView, params);
+ }
+
/**
* Launches {@link TestActivity} with the given initialization logic for content view.
*