Add tests for IME window visibility when setShowWhenLocked

To verify IME window can visible when actvitiy#setShowWhenLocked or
"android:setShowWhenLocked=true" in AndroidManifest.xml,

Added 2 tests in KeyguardLockedTests as below to make sure it works as expected:
1) testShowWhenLockedAttrImeActivityAndShowSoftInput:
   Launch "android:setShowWhenLocked=true" activity & calling showSoftInput
   to verify IME window is visible.

2) testShowWhenLockedImeActivityAndShowSoftInput:
   Launch activity with Activity#setShowWhenLocked set as true & calling
   showSoftInput to verify IME window is visible.

Bug: 119629545
Test: atest KeyguardLockedTests
Change-Id: I1b589bd884395d40395ea87b4fbc9ca04d9ded85
diff --git a/tests/framework/base/activitymanager/AndroidManifest.xml b/tests/framework/base/activitymanager/AndroidManifest.xml
index 8b55608..fcfd54a 100644
--- a/tests/framework/base/activitymanager/AndroidManifest.xml
+++ b/tests/framework/base/activitymanager/AndroidManifest.xml
@@ -103,6 +103,8 @@
         <activity android:name="android.server.am.ActivityManagerMultiDisplayTests$ImeTestActivity2" />
         <activity android:name="android.server.am.ActivityManagerMultiDisplayTests$ImeTestActivityWithBrokenContextWrapper" />
 
+        <activity android:name="android.server.am.KeyguardLockedTests$ShowWhenLockedImeActivity" />
+
         <activity android:name="android.server.am.lifecycle.ActivityStarterTests$StandardActivity"
                   android:exported="true" />
 
diff --git a/tests/framework/base/activitymanager/app/AndroidManifest.xml b/tests/framework/base/activitymanager/app/AndroidManifest.xml
index 3ec88ab..89164f5 100755
--- a/tests/framework/base/activitymanager/app/AndroidManifest.xml
+++ b/tests/framework/base/activitymanager/app/AndroidManifest.xml
@@ -393,6 +393,10 @@
         <activity android:name=".NoInheritShowWhenLockedAttrActivity"
                   android:exported="true" />
 
+        <activity android:name=".ShowWhenLockedAttrImeActivity"
+                  android:showWhenLocked="true"
+                  android:exported="true" />
+
         <activity android:name=".ToastActivity"
                   android:exported="true"/>
 
diff --git a/tests/framework/base/activitymanager/app/src/android/server/am/Components.java b/tests/framework/base/activitymanager/app/src/android/server/am/Components.java
index cc0fd44..a3c63b7 100644
--- a/tests/framework/base/activitymanager/app/src/android/server/am/Components.java
+++ b/tests/framework/base/activitymanager/app/src/android/server/am/Components.java
@@ -84,6 +84,8 @@
             component("ResumeWhilePausingActivity");
     public static final ComponentName SHOW_WHEN_LOCKED_ACTIVITY =
             component("ShowWhenLockedActivity");
+    public static final ComponentName SHOW_WHEN_LOCKED_ATTR_IME_ACTIVITY =
+            component("ShowWhenLockedAttrImeActivity");
     public static final ComponentName SHOW_WHEN_LOCKED_ATTR_ACTIVITY =
             component("ShowWhenLockedAttrActivity");
     public static final ComponentName SHOW_WHEN_LOCKED_ATTR_REMOVE_ATTR_ACTIVITY =
diff --git a/tests/framework/base/activitymanager/app/src/android/server/am/ShowWhenLockedAttrImeActivity.java b/tests/framework/base/activitymanager/app/src/android/server/am/ShowWhenLockedAttrImeActivity.java
new file mode 100644
index 0000000..7bae38b
--- /dev/null
+++ b/tests/framework/base/activitymanager/app/src/android/server/am/ShowWhenLockedAttrImeActivity.java
@@ -0,0 +1,40 @@
+/*
+ * Copyright (C) 2019 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.server.am;
+
+import static android.view.WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE;
+
+import android.os.Bundle;
+import android.widget.EditText;
+import android.widget.LinearLayout;
+
+public class ShowWhenLockedAttrImeActivity extends AbstractLifecycleLogActivity {
+
+    @Override
+    protected void onCreate(Bundle icicle) {
+        super.onCreate(icicle);
+
+        final EditText editText = new EditText(this);
+        final LinearLayout layout = new LinearLayout(this);
+        layout.setOrientation(LinearLayout.VERTICAL);
+        layout.addView(editText);
+        setContentView(layout);
+
+        getWindow().setSoftInputMode(SOFT_INPUT_STATE_ALWAYS_VISIBLE);
+        editText.requestFocus();
+    }
+}
diff --git a/tests/framework/base/activitymanager/src/android/server/am/KeyguardLockedTests.java b/tests/framework/base/activitymanager/src/android/server/am/KeyguardLockedTests.java
index 7daccc8..28fe737 100644
--- a/tests/framework/base/activitymanager/src/android/server/am/KeyguardLockedTests.java
+++ b/tests/framework/base/activitymanager/src/android/server/am/KeyguardLockedTests.java
@@ -25,20 +25,40 @@
 import static android.server.am.Components.PipActivity.EXTRA_ENTER_PIP;
 import static android.server.am.Components.PipActivity.EXTRA_SHOW_OVER_KEYGUARD;
 import static android.server.am.Components.SHOW_WHEN_LOCKED_ACTIVITY;
+import static android.server.am.Components.SHOW_WHEN_LOCKED_ATTR_IME_ACTIVITY;
 import static android.server.am.Components.TURN_SCREEN_ON_ATTR_DISMISS_KEYGUARD_ACTIVITY;
 import static android.server.am.UiDeviceUtils.pressBackButton;
 import static android.view.Display.DEFAULT_DISPLAY;
+import static android.view.WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE;
+
+import static com.android.cts.mockime.ImeEventStreamTestUtils.expectEvent;
 
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertTrue;
 import static org.junit.Assume.assumeTrue;
 
+import android.app.Activity;
 import android.app.KeyguardManager;
 import android.content.ComponentName;
+import android.os.Bundle;
+import android.os.SystemClock;
+import android.view.inputmethod.EditorInfo;
+import android.view.inputmethod.InputMethodManager;
+import android.widget.EditText;
+import android.widget.LinearLayout;
+
+import android.support.test.InstrumentationRegistry;
+
+import com.android.cts.mockime.ImeEvent;
+import com.android.cts.mockime.ImeEventStream;
+import com.android.cts.mockime.ImeSettings;
+import com.android.cts.mockime.MockImeSession;
 
 import org.junit.Before;
 import org.junit.Test;
 
+import java.util.concurrent.TimeUnit;
+
 /**
  * Build/Install/Run:
  *     atest CtsActivityManagerDeviceTestCases:KeyguardLockedTests
@@ -275,6 +295,73 @@
         }
     }
 
+    @Test
+    public void testShowWhenLockedAttrImeActivityAndShowSoftInput() throws Exception {
+        try (final LockScreenSession lockScreenSession = new LockScreenSession();
+             // Leverage MockImeSession to ensure at least an IME exists as default.
+             final MockImeSession mockImeSession = MockImeSession.create(mContext,
+                     InstrumentationRegistry.getInstrumentation().getUiAutomation(),
+                     new ImeSettings.Builder())) {
+            lockScreenSession.setLockCredential().gotoKeyguard();
+            mAmWmState.assertKeyguardShowingAndNotOccluded();
+            launchActivity(SHOW_WHEN_LOCKED_ATTR_IME_ACTIVITY);
+            mAmWmState.computeState(SHOW_WHEN_LOCKED_ATTR_IME_ACTIVITY);
+            mAmWmState.assertVisibility(SHOW_WHEN_LOCKED_ATTR_IME_ACTIVITY, true);
+
+            // Make sure the activity has been called showSoftInput & IME window is visible.
+            final ImeEventStream stream = mockImeSession.openEventStream();
+            expectEvent(stream, event -> "showSoftInput".equals(event.getEventName()),
+                    TimeUnit.SECONDS.toMillis(5) /* eventTimeout */);
+            // Assert the IME is shown on the expected display.
+            mAmWmState.waitAndAssertImeWindowShownOnDisplay(DEFAULT_DISPLAY);
+        }
+    }
+
+    @Test
+    public void testShowWhenLockedImeActivityAndShowSoftInput() throws Exception {
+        try (final LockScreenSession lockScreenSession = new LockScreenSession();
+             final TestActivitySession<ShowWhenLockedImeActivity> imeTestActivitySession = new
+                     TestActivitySession<>();
+             // Leverage MockImeSession to ensure at least an IME exists as default.
+             final MockImeSession mockImeSession = MockImeSession.create(mContext,
+                     InstrumentationRegistry.getInstrumentation().getUiAutomation(),
+                     new ImeSettings.Builder())) {
+            lockScreenSession.setLockCredential().gotoKeyguard();
+            mAmWmState.assertKeyguardShowingAndNotOccluded();
+            imeTestActivitySession.launchTestActivityOnDisplaySync(ShowWhenLockedImeActivity.class,
+                    DEFAULT_DISPLAY);
+
+            // Make sure the activity has been called showSoftInput & IME window is visible.
+            final ImeEventStream stream = mockImeSession.openEventStream();
+            expectEvent(stream, event -> "showSoftInput".equals(event.getEventName()),
+                    TimeUnit.SECONDS.toMillis(5) /* eventTimeout */);
+            // Assert the IME is shown on the expected display.
+            mAmWmState.waitAndAssertImeWindowShownOnDisplay(DEFAULT_DISPLAY);
+        }
+    }
+
+    public static class ShowWhenLockedImeActivity extends Activity {
+
+        @Override
+        protected void onCreate(Bundle icicle) {
+            super.onCreate(icicle);
+            final EditText editText = new EditText(this);
+            // Set private IME option for editorMatcher to identify which TextView received
+            // onStartInput event.
+            editText.setPrivateImeOptions(
+                    getClass().getName() + "/" + Long.toString(SystemClock.elapsedRealtimeNanos()));
+            final LinearLayout layout = new LinearLayout(this);
+            layout.setOrientation(LinearLayout.VERTICAL);
+            layout.addView(editText);
+            setContentView(layout);
+
+            // Set showWhenLocked as true & request focus for showing soft input.
+            setShowWhenLocked(true);
+            getWindow().setSoftInputMode(SOFT_INPUT_STATE_ALWAYS_VISIBLE);
+            editText.requestFocus();
+        }
+    }
+
     /**
      * Waits until the given activity has entered picture-in-picture mode (allowing for the
      * subsequent animation to start).