From bfc6604163344f9e6336d7a34997485e186d2919 Mon Sep 17 00:00:00 2001 From: Sergey Nikolaienkov Date: Mon, 28 Dec 2020 10:29:04 +0000 Subject: [PATCH] Do not click on TVs in PipKeyboardTest Implement an alternative way of showing/hiding IME in PipKeyboardTest on TV - via "re-launching" the ImeActivity with a particular action. The original way - calling UiObject2.click() on the EditText does not work well on TVs, because the touch events injected by the Instrumentation to emulate the click force TV into the touch mode. Bug: 176413558 Test: atest WMShellFlickerTests:PipKeyboardTest Change-Id: Id3462eba548c8ed639ba44437db573928348c1e6 --- .../wm/shell/flicker/CommonConstants.kt | 4 +++ .../wm/shell/flicker/helpers/BaseAppHelper.kt | 3 +- .../wm/shell/flicker/helpers/ImeAppHelper.kt | 28 ++++++++++++---- .../test-apps/flickerapp/AndroidManifest.xml | 1 + .../wm/shell/flicker/testapp/ImeActivity.java | 33 +++++++++++++++++++ 5 files changed, 61 insertions(+), 8 deletions(-) diff --git a/libs/WindowManager/Shell/tests/flicker/src/com/android/wm/shell/flicker/CommonConstants.kt b/libs/WindowManager/Shell/tests/flicker/src/com/android/wm/shell/flicker/CommonConstants.kt index 5125a3972cf43..796c8c4a6ad08 100644 --- a/libs/WindowManager/Shell/tests/flicker/src/com/android/wm/shell/flicker/CommonConstants.kt +++ b/libs/WindowManager/Shell/tests/flicker/src/com/android/wm/shell/flicker/CommonConstants.kt @@ -31,6 +31,10 @@ const val TEST_APP_PIP_MENU_ACTION_CLEAR = "Clear" // Test App > Ime Activity const val TEST_APP_IME_ACTIVITY_LABEL = "ImeApp" +const val TEST_APP_IME_ACTIVITY_ACTION_OPEN_IME = + "com.android.wm.shell.flicker.testapp.action.OPEN_IME" +const val TEST_APP_IME_ACTIVITY_ACTION_CLOSE_IME = + "com.android.wm.shell.flicker.testapp.action.CLOSE_IME" // Test App > Test Activity const val TEST_APP_FIXED_ACTIVITY_LABEL = "FixedApp" diff --git a/libs/WindowManager/Shell/tests/flicker/src/com/android/wm/shell/flicker/helpers/BaseAppHelper.kt b/libs/WindowManager/Shell/tests/flicker/src/com/android/wm/shell/flicker/helpers/BaseAppHelper.kt index 6fd1df3b3f308..f8efd0e9c7613 100644 --- a/libs/WindowManager/Shell/tests/flicker/src/com/android/wm/shell/flicker/helpers/BaseAppHelper.kt +++ b/libs/WindowManager/Shell/tests/flicker/src/com/android/wm/shell/flicker/helpers/BaseAppHelper.kt @@ -62,8 +62,9 @@ abstract class BaseAppHelper( val ui: UiObject2? get() = uiDevice.findObject(appSelector) - fun launchViaIntent(stringExtras: Map = mapOf()) { + fun launchViaIntent(action: String? = null, stringExtras: Map = mapOf()) { val intent = openAppIntent + intent.action = action stringExtras.forEach() { intent.putExtra(it.key, it.value) } diff --git a/libs/WindowManager/Shell/tests/flicker/src/com/android/wm/shell/flicker/helpers/ImeAppHelper.kt b/libs/WindowManager/Shell/tests/flicker/src/com/android/wm/shell/flicker/helpers/ImeAppHelper.kt index c546a4d180276..eb20d73cdcd1e 100644 --- a/libs/WindowManager/Shell/tests/flicker/src/com/android/wm/shell/flicker/helpers/ImeAppHelper.kt +++ b/libs/WindowManager/Shell/tests/flicker/src/com/android/wm/shell/flicker/helpers/ImeAppHelper.kt @@ -20,6 +20,8 @@ import android.app.Instrumentation import androidx.test.uiautomator.By import androidx.test.uiautomator.Until import com.android.server.wm.flicker.helpers.FIND_TIMEOUT +import com.android.wm.shell.flicker.TEST_APP_IME_ACTIVITY_ACTION_CLOSE_IME +import com.android.wm.shell.flicker.TEST_APP_IME_ACTIVITY_ACTION_OPEN_IME import com.android.wm.shell.flicker.TEST_APP_IME_ACTIVITY_LABEL import com.android.wm.shell.flicker.testapp.Components import org.junit.Assert @@ -32,15 +34,27 @@ open class ImeAppHelper( Components.ImeActivity() ) { fun openIME() { - val editText = uiDevice.wait( - Until.findObject(By.res(getPackage(), "plain_text_input")), - FIND_TIMEOUT) - Assert.assertNotNull("Text field not found, this usually happens when the device " + - "was left in an unknown state (e.g. in split screen)", editText) - editText.click() + if (!isTelevision) { + val editText = uiDevice.wait( + Until.findObject(By.res(getPackage(), "plain_text_input")), + FIND_TIMEOUT) + Assert.assertNotNull("Text field not found, this usually happens when the device " + + "was left in an unknown state (e.g. in split screen)", editText) + editText.click() + } else { + // If we do the same thing as above - editText.click() - on TV, that's going to force TV + // into the touch mode. We really don't want that. + launchViaIntent(action = TEST_APP_IME_ACTIVITY_ACTION_OPEN_IME) + } } fun closeIME() { - uiDevice.pressBack() + if (!isTelevision) { + uiDevice.pressBack() + } else { + // While pressing the back button should close the IME on TV as well, it may also lead + // to the app closing. So let's instead just ask the app to close the IME. + launchViaIntent(action = TEST_APP_IME_ACTIVITY_ACTION_CLOSE_IME) + } } } \ No newline at end of file diff --git a/libs/WindowManager/Shell/tests/flicker/test-apps/flickerapp/AndroidManifest.xml b/libs/WindowManager/Shell/tests/flicker/test-apps/flickerapp/AndroidManifest.xml index 28ed3431db62e..5549330df7667 100644 --- a/libs/WindowManager/Shell/tests/flicker/test-apps/flickerapp/AndroidManifest.xml +++ b/libs/WindowManager/Shell/tests/flicker/test-apps/flickerapp/AndroidManifest.xml @@ -53,6 +53,7 @@ diff --git a/libs/WindowManager/Shell/tests/flicker/test-apps/flickerapp/src/com/android/wm/shell/flicker/testapp/ImeActivity.java b/libs/WindowManager/Shell/tests/flicker/test-apps/flickerapp/src/com/android/wm/shell/flicker/testapp/ImeActivity.java index 856728715c1cd..59c64a1345abc 100644 --- a/libs/WindowManager/Shell/tests/flicker/test-apps/flickerapp/src/com/android/wm/shell/flicker/testapp/ImeActivity.java +++ b/libs/WindowManager/Shell/tests/flicker/test-apps/flickerapp/src/com/android/wm/shell/flicker/testapp/ImeActivity.java @@ -17,10 +17,21 @@ package com.android.wm.shell.flicker.testapp; import android.app.Activity; +import android.content.Intent; import android.os.Bundle; +import android.view.View; import android.view.WindowManager; +import android.view.inputmethod.InputMethodManager; public class ImeActivity extends Activity { + private static final String ACTION_OPEN_IME = + "com.android.wm.shell.flicker.testapp.action.OPEN_IME"; + private static final String ACTION_CLOSE_IME = + "com.android.wm.shell.flicker.testapp.action.CLOSE_IME"; + + private InputMethodManager mImm; + private View mEditText; + @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -29,5 +40,27 @@ public class ImeActivity extends Activity { .LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES; getWindow().setAttributes(p); setContentView(R.layout.activity_ime); + + mEditText = findViewById(R.id.plain_text_input); + mImm = getSystemService(InputMethodManager.class); + + handleIntent(getIntent()); + } + + @Override + protected void onNewIntent(Intent intent) { + super.onNewIntent(intent); + handleIntent(intent); + } + + private void handleIntent(Intent intent) { + final String action = intent.getAction(); + if (ACTION_OPEN_IME.equals(action)) { + mEditText.requestFocus(); + mImm.showSoftInput(mEditText, InputMethodManager.SHOW_FORCED); + } else if (ACTION_CLOSE_IME.equals(action)) { + mImm.hideSoftInputFromWindow(mEditText.getWindowToken(), 0); + mEditText.clearFocus(); + } } }