Dismiss keyguard when turning on screen.

Dismiss the keyguard when turning on the screen so that the test app can
become TOP when its Activity is active.

Bug: 286716632
Test: atest CtsJobSchedulerTestCases:ExpeditedJobTest
Test: atest CtsJobSchedulerTestCases:JobThrottlingTest
Test: atest CtsJobSchedulerTestCases:UserInitiatedJobTest
Change-Id: I5a734d8b17a6d0b70d95b9b9e199a50f087d622d
diff --git a/tests/JobScheduler/src/android/jobscheduler/cts/ExpeditedJobTest.java b/tests/JobScheduler/src/android/jobscheduler/cts/ExpeditedJobTest.java
index 00b9da2..7b93266 100644
--- a/tests/JobScheduler/src/android/jobscheduler/cts/ExpeditedJobTest.java
+++ b/tests/JobScheduler/src/android/jobscheduler/cts/ExpeditedJobTest.java
@@ -16,6 +16,7 @@
 
 package android.jobscheduler.cts;
 
+import static android.jobscheduler.cts.JobThrottlingTest.setScreenState;
 import static android.jobscheduler.cts.JobThrottlingTest.setTestPackageStandbyBucket;
 import static android.jobscheduler.cts.TestAppInterface.TEST_APP_PACKAGE;
 
@@ -33,7 +34,6 @@
 import androidx.test.uiautomator.UiDevice;
 
 import com.android.compatibility.common.util.AppOpsUtils;
-import com.android.compatibility.common.util.ScreenUtils;
 
 import org.junit.After;
 import org.junit.Before;
@@ -74,7 +74,7 @@
     public void testJobUidState() throws Exception {
         // Turn screen off so any lingering activity close processing from previous tests
         // don't affect this one.
-        ScreenUtils.setScreenOn(false);
+        setScreenState(mUiDevice, false);
         mTestAppInterface.scheduleJob(Map.of(
                 TestJobSchedulerReceiver.EXTRA_AS_EXPEDITED, true,
                 TestJobSchedulerReceiver.EXTRA_REQUEST_JOB_UID_STATE, true
@@ -93,7 +93,7 @@
     public void testTopEJUnlimited() throws Exception {
         final int standardConcurrency = 64;
         final int numEjs = standardConcurrency + 1;
-        ScreenUtils.setScreenOn(true);
+        setScreenState(mUiDevice, true);
         mTestAppInterface.startAndKeepTestActivity(true);
         for (int i = 0; i < numEjs; ++i) {
             mTestAppInterface.scheduleJob(
diff --git a/tests/JobScheduler/src/android/jobscheduler/cts/JobThrottlingTest.java b/tests/JobScheduler/src/android/jobscheduler/cts/JobThrottlingTest.java
index c6f547c..1e5d5bf 100644
--- a/tests/JobScheduler/src/android/jobscheduler/cts/JobThrottlingTest.java
+++ b/tests/JobScheduler/src/android/jobscheduler/cts/JobThrottlingTest.java
@@ -1461,11 +1461,19 @@
      * Set the screen state.
      */
     private void setScreenState(boolean on) throws Exception {
+        setScreenState(mUiDevice, on);
+    }
+
+    static void setScreenState(UiDevice uiDevice, boolean on) throws Exception {
+        PowerManager powerManager =
+                InstrumentationRegistry.getContext().getSystemService(PowerManager.class);
         if (on) {
-            mUiDevice.executeShellCommand("input keyevent KEYCODE_WAKEUP");
-            mUiDevice.executeShellCommand("wm dismiss-keyguard");
+            uiDevice.executeShellCommand("input keyevent KEYCODE_WAKEUP");
+            uiDevice.executeShellCommand("wm dismiss-keyguard");
+            waitUntil("Device not interactive", () -> powerManager.isInteractive());
         } else {
-            mUiDevice.executeShellCommand("input keyevent KEYCODE_SLEEP");
+            uiDevice.executeShellCommand("input keyevent KEYCODE_SLEEP");
+            waitUntil("Device still interactive", () -> !powerManager.isInteractive());
         }
         // Wait a little bit to make sure the screen state has changed.
         Thread.sleep(4_000);
diff --git a/tests/JobScheduler/src/android/jobscheduler/cts/UserInitiatedJobTest.java b/tests/JobScheduler/src/android/jobscheduler/cts/UserInitiatedJobTest.java
index c81c8b2..d5eb783 100644
--- a/tests/JobScheduler/src/android/jobscheduler/cts/UserInitiatedJobTest.java
+++ b/tests/JobScheduler/src/android/jobscheduler/cts/UserInitiatedJobTest.java
@@ -17,6 +17,7 @@
 package android.jobscheduler.cts;
 
 import static android.app.job.JobInfo.NETWORK_TYPE_ANY;
+import static android.jobscheduler.cts.JobThrottlingTest.setScreenState;
 import static android.jobscheduler.cts.JobThrottlingTest.setTestPackageStandbyBucket;
 import static android.jobscheduler.cts.TestAppInterface.TEST_APP_PACKAGE;
 
@@ -46,7 +47,6 @@
 import androidx.test.uiautomator.UiDevice;
 
 import com.android.compatibility.common.util.CallbackAsserter;
-import com.android.compatibility.common.util.ScreenUtils;
 import com.android.compatibility.common.util.SystemUtil;
 
 import org.junit.After;
@@ -142,7 +142,7 @@
     public void testTopUiUnlimited() throws Exception {
         final int standardConcurrency = 64;
         final int numUijs = standardConcurrency + 1;
-        ScreenUtils.setScreenOn(true);
+        setScreenState(mUiDevice, true);
         mTestAppInterface.startAndKeepTestActivity(true);
         for (int i = 0; i < numUijs; ++i) {
             mTestAppInterface.scheduleJob(
@@ -205,6 +205,7 @@
             mNetworkingHelper.setAllNetworksEnabled(true);
             assertFalse(mTestAppInterface.awaitJobStart(DEFAULT_WAIT_TIMEOUT_MS));
 
+            setScreenState(mUiDevice, true);
             mTestAppInterface.startAndKeepTestActivity(true);
             assertTrue(mTestAppInterface.awaitJobStart(DEFAULT_WAIT_TIMEOUT_MS));
         }
@@ -253,7 +254,7 @@
     /** Test that UI jobs of restricted apps will be stopped after the app leaves the TOP state. */
     @Test
     public void testRestrictedTopToBg() throws Exception {
-        ScreenUtils.setScreenOn(true);
+        setScreenState(mUiDevice, true);
         mTestAppInterface.setTestPackageRestricted(true);
         mTestAppInterface.startAndKeepTestActivity(true);
         mTestAppInterface.scheduleJob(
@@ -338,7 +339,7 @@
     public void testSchedulingBg() throws Exception {
         // Close the activity and turn the screen off so the app isn't considered TOP.
         mTestAppInterface.closeActivity();
-        ScreenUtils.setScreenOn(false);
+        setScreenState(mUiDevice, false);
         mTestAppInterface.scheduleJob(
                 Map.of(TestJobSchedulerReceiver.EXTRA_AS_USER_INITIATED, true),
                 Map.of(TestJobSchedulerReceiver.EXTRA_REQUIRED_NETWORK_TYPE, NETWORK_TYPE_ANY));
@@ -351,7 +352,7 @@
     public void testSchedulingEj() throws Exception {
         // Close the activity and turn the screen off so the app isn't considered TOP.
         mTestAppInterface.closeActivity();
-        ScreenUtils.setScreenOn(false);
+        setScreenState(mUiDevice, false);
 
         final int jobIdEj = JOB_ID;
         final int jobIdUij = JOB_ID + 1;
@@ -374,7 +375,7 @@
     /** Test that UI jobs can be scheduled directly from an FGS that was started in TOP state. */
     @Test
     public void testSchedulingFgs_approved() throws Exception {
-        ScreenUtils.setScreenOn(true);
+        setScreenState(mUiDevice, true);
         mTestAppInterface.startAndKeepTestActivity(true);
         mTestAppInterface.startFgs();
         mTestAppInterface.closeActivity(true);
@@ -409,7 +410,7 @@
     /** Test that UI jobs can be scheduled directly from the TOP state. */
     @Test
     public void testSchedulingTop() throws Exception {
-        ScreenUtils.setScreenOn(true);
+        setScreenState(mUiDevice, true);
         mTestAppInterface.startAndKeepTestActivity(true);
         mTestAppInterface.scheduleJob(
                 Map.of(TestJobSchedulerReceiver.EXTRA_AS_USER_INITIATED, true),
@@ -424,7 +425,7 @@
         int firstJobId = JOB_ID;
         int secondJobId = firstJobId + 1;
 
-        ScreenUtils.setScreenOn(true);
+        setScreenState(mUiDevice, true);
         mTestAppInterface.startAndKeepTestActivity(true);
         mTestAppInterface.scheduleJob(
                 Map.of(