Fix CTS testKeyguardLockAndUnlock fail

WindowManager failed to find the keyguard window because
cmd is injected by shell command and the shell command
only take current window as the event window. But
topDisplayFocusedStack changes when window focus changed.
Fix is to send a tap event to default display
to ensure focus switch to default display

Test: run CtsKeystoreTestCases testKeyguardLockAndUnlock

bug: 172972737

[RESTRICT AUTOMERGE]
Signed-off-by: wznqmp <sajjad.mosharraf@gm.com>
Change-Id: Ia988e4b434e8b5867b079fe7b9ff7cd14699b606
diff --git a/tests/tests/keystore/src/android/server/am/ActivityManagerTestBase.java b/tests/tests/keystore/src/android/server/am/ActivityManagerTestBase.java
index 9ebba74..c227295 100644
--- a/tests/tests/keystore/src/android/server/am/ActivityManagerTestBase.java
+++ b/tests/tests/keystore/src/android/server/am/ActivityManagerTestBase.java
@@ -32,6 +32,9 @@
 import static android.server.am.UiDeviceUtils.pressUnlockButton;
 import static android.server.am.UiDeviceUtils.pressWakeupButton;
 import static android.server.am.UiDeviceUtils.waitForDeviceIdle;
+import static androidx.test.InstrumentationRegistry.getInstrumentation;
+import static android.view.Display.DEFAULT_DISPLAY;
+
 
 import android.accessibilityservice.AccessibilityService;
 import android.app.ActivityManager;
@@ -39,6 +42,8 @@
 import android.content.ComponentName;
 import android.content.Context;
 import android.os.SystemClock;
+import android.view.InputDevice;
+import android.view.MotionEvent;
 
 import androidx.test.InstrumentationRegistry;
 
@@ -256,6 +261,23 @@
         return false;
     }
 
+    protected void tapOnDisplay(int x, int y, int displayId) {
+        final long downTime = SystemClock.uptimeMillis();
+        injectMotion(downTime, downTime, MotionEvent.ACTION_DOWN, x, y, displayId);
+
+        final long upTime = SystemClock.uptimeMillis();
+        injectMotion(downTime, upTime, MotionEvent.ACTION_UP, x, y, displayId);
+    }
+
+    private static void injectMotion(long downTime, long eventTime, int action,
+            int x, int y, int displayId) {
+        final MotionEvent event = MotionEvent.obtain(downTime, eventTime, action,
+                x, y, 0 /* metaState */);
+        event.setSource(InputDevice.SOURCE_TOUCHSCREEN);
+        event.setDisplayId(displayId);
+        getInstrumentation().getUiAutomation().injectInputEvent(event, true /* sync */);
+    }
+
     protected class LockScreenSession implements AutoCloseable {
         private static final boolean DEBUG = false;
 
@@ -276,6 +298,7 @@
         }
 
         public LockScreenSession enterAndConfirmLockCredential() {
+            tapOnDisplay(10, 10, DEFAULT_DISPLAY);
             waitForDeviceIdle(3000);
 
             runCommandAndPrintOutput("input text " + LOCK_CREDENTIAL);