Enable lockUserTest

Code such that after lockUserTest is run, we assert that the keyguard
was successfully dismissed so that the device can return to it's pretest
state. All tests run in <5s now.

Test: atest TrustTests --iterations
Bug: 221155933
Change-Id: I20bf7671f8deeb6c70a977e7c2fc7cbc5395fe4b
diff --git a/tests/TrustTests/AndroidManifest.xml b/tests/TrustTests/AndroidManifest.xml
index c94152d..68bc1f69 100644
--- a/tests/TrustTests/AndroidManifest.xml
+++ b/tests/TrustTests/AndroidManifest.xml
@@ -23,7 +23,9 @@
     <uses-permission android:name="android.permission.BIND_DEVICE_ADMIN" />
     <uses-permission android:name="android.permission.CONTROL_KEYGUARD" />
     <uses-permission android:name="android.permission.DEVICE_POWER" />
+    <uses-permission android:name="android.permission.DISABLE_KEYGUARD" />
     <uses-permission android:name="android.permission.INTERACT_ACROSS_USERS_FULL" />
+    <uses-permission android:name="android.permission.MANAGE_USERS" />
     <uses-permission android:name="android.permission.PROVIDE_TRUST_AGENT" />
     <uses-permission android:name="android.permission.TRUST_LISTENER" />
 
diff --git a/tests/TrustTests/src/android/trust/test/LockUserTest.kt b/tests/TrustTests/src/android/trust/test/LockUserTest.kt
index 83fc28f..8f200a6 100644
--- a/tests/TrustTests/src/android/trust/test/LockUserTest.kt
+++ b/tests/TrustTests/src/android/trust/test/LockUserTest.kt
@@ -25,7 +25,6 @@
 import androidx.test.ext.junit.rules.ActivityScenarioRule
 import androidx.test.ext.junit.runners.AndroidJUnit4
 import com.google.common.truth.Truth.assertThat
-import org.junit.Ignore
 import org.junit.Rule
 import org.junit.Test
 import org.junit.rules.RuleChain
@@ -49,7 +48,6 @@
         .around(lockStateTrackingRule)
         .around(trustAgentRule)
 
-    @Ignore("Causes issues with subsequent tests") // TODO: Enable test
     @Test
     fun lockUser_locksTheDevice() {
         Log.i(TAG, "Locking user")
diff --git a/tests/TrustTests/src/android/trust/test/lib/ScreenLockRule.kt b/tests/TrustTests/src/android/trust/test/lib/ScreenLockRule.kt
index bc100ba..006525d 100644
--- a/tests/TrustTests/src/android/trust/test/lib/ScreenLockRule.kt
+++ b/tests/TrustTests/src/android/trust/test/lib/ScreenLockRule.kt
@@ -20,6 +20,8 @@
 import android.util.Log
 import android.view.WindowManagerGlobal
 import androidx.test.core.app.ApplicationProvider.getApplicationContext
+import androidx.test.platform.app.InstrumentationRegistry.getInstrumentation
+import androidx.test.uiautomator.UiDevice
 import com.android.internal.widget.LockPatternUtils
 import com.android.internal.widget.LockscreenCredential
 import com.google.common.truth.Truth.assertWithMessage
@@ -32,6 +34,7 @@
  */
 class ScreenLockRule : TestRule {
     private val context: Context = getApplicationContext()
+    private val uiDevice = UiDevice.getInstance(getInstrumentation())
     private val windowManager = WindowManagerGlobal.getWindowManagerService()
     private val lockPatternUtils = LockPatternUtils(context)
     private var instantLockSavedValue = false
@@ -48,19 +51,21 @@
             } finally {
                 removeScreenLock()
                 revertLockOnPowerButton()
+                verifyKeyguardDismissed()
             }
         }
     }
 
     private fun verifyNoScreenLockAlreadySet() {
         assertWithMessage("Screen Lock must not already be set on device")
-            .that(lockPatternUtils.isSecure(context.userId))
-            .isFalse()
+                .that(lockPatternUtils.isSecure(context.userId))
+                .isFalse()
     }
 
     private fun verifyKeyguardDismissed() {
         val maxWaits = 30
         var waitCount = 0
+
         while (windowManager.isKeyguardLocked && waitCount < maxWaits) {
             Log.i(TAG, "Keyguard still showing; attempting to dismiss and wait 50ms ($waitCount)")
             windowManager.dismissKeyguard(null, null)
@@ -68,19 +73,19 @@
             waitCount++
         }
         assertWithMessage("Keyguard should be unlocked")
-            .that(windowManager.isKeyguardLocked)
-            .isFalse()
+                .that(windowManager.isKeyguardLocked)
+                .isFalse()
     }
 
     private fun setScreenLock() {
         lockPatternUtils.setLockCredential(
-            LockscreenCredential.createPin(PIN),
-            LockscreenCredential.createNone(),
-            context.userId
+                LockscreenCredential.createPin(PIN),
+                LockscreenCredential.createNone(),
+                context.userId
         )
         assertWithMessage("Screen Lock should now be set")
-            .that(lockPatternUtils.isSecure(context.userId))
-            .isTrue()
+                .that(lockPatternUtils.isSecure(context.userId))
+                .isTrue()
         Log.i(TAG, "Device PIN set to $PIN")
     }
 
@@ -90,14 +95,25 @@
     }
 
     private fun removeScreenLock() {
-        lockPatternUtils.setLockCredential(
-            LockscreenCredential.createNone(),
-            LockscreenCredential.createPin(PIN),
-            context.userId
-        )
-        Log.i(TAG, "Device PIN cleared; waiting 50 ms then dismissing Keyguard")
-        Thread.sleep(50)
-        windowManager.dismissKeyguard(null, null)
+        var lockCredentialUnset = lockPatternUtils.setLockCredential(
+                LockscreenCredential.createNone(),
+                LockscreenCredential.createPin(PIN),
+                context.userId)
+        Thread.sleep(100)
+        assertWithMessage("Lock screen credential should be unset")
+                .that(lockCredentialUnset)
+                .isTrue()
+
+        lockPatternUtils.setLockScreenDisabled(true, context.userId)
+        Thread.sleep(100)
+        assertWithMessage("Lockscreen needs to be disabled")
+                .that(lockPatternUtils.isLockScreenDisabled(context.userId))
+                .isTrue()
+
+        // this is here because somehow it helps the keyguard not get stuck
+        uiDevice.sleep()
+        Thread.sleep(500) // delay added to avoid initiating camera by double clicking power
+        uiDevice.wakeUp()
     }
 
     private fun revertLockOnPowerButton() {