Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 7fc2817a authored by Dave McCloskey's avatar Dave McCloskey Committed by Android (Google) Code Review
Browse files

Merge "Refactor TrustTests waiting into common function." into tm-dev

parents f26d0418 00ea00b0
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -41,7 +41,7 @@ abstract class BaseTrustAgentService : TrustAgentService() {
        private const val TAG = "BaseTrustAgentService"

        fun instance(serviceClass: KClass<out BaseTrustAgentService>): BaseTrustAgentService? {
            return instances[serviceClass]!!
            return instances[serviceClass]
        }
    }
}
+0 −1
Original line number Diff line number Diff line
@@ -57,7 +57,6 @@ class LockUserTest {

    companion object {
        private const val TAG = "LockUserTest"
        private fun await() = Thread.sleep(250)
    }
}

+3 −29
Original line number Diff line number Diff line
@@ -22,7 +22,6 @@ import android.content.Context
import android.util.Log
import android.view.WindowManagerGlobal
import androidx.test.core.app.ApplicationProvider.getApplicationContext
import com.google.common.truth.Truth.assertThat
import org.junit.rules.TestRule
import org.junit.runner.Description
import org.junit.runners.model.Statement
@@ -53,37 +52,12 @@ class LockStateTrackingRule : TestRule {
    }

    fun assertLocked() {
        val maxWaits = 50
        var waitCount = 0

        // First verify we get the call in LockState via TrustListener
        while ((lockState.locked == false) && waitCount < maxWaits) {
            Log.i(TAG, "phone still unlocked (TrustListener), wait 50ms more ($waitCount)")
            Thread.sleep(50)
            waitCount++
        }
        assertThat(lockState.locked).isTrue()

        // TODO(b/225231929): refactor checks into one loop and re-use for assertUnlocked
        // Then verify we get the window manager locked
        while (!windowManager.isKeyguardLocked && waitCount < maxWaits) {
            Log.i(TAG, "phone still unlocked (WindowManager), wait 50ms more ($waitCount)")
            Thread.sleep(50)
            waitCount++
        }
        assertThat(windowManager.isKeyguardLocked).isTrue()
        wait("un-locked per TrustListener") { lockState.locked == true }
        wait("keyguard lock") { windowManager.isKeyguardLocked }
    }

    fun assertUnlocked() {
        val maxWaits = 50
        var waitCount = 0

        while ((lockState.locked == true) && waitCount < maxWaits) {
            Log.i(TAG, "phone still unlocked, wait 50ms more ($waitCount)")
            Thread.sleep(50)
            waitCount++
        }
        assertThat(lockState.locked).isFalse()
        wait("locked per TrustListener") { lockState.locked == false }
    }

    inner class Listener : TrustListener {
+13 −29
Original line number Diff line number Diff line
@@ -42,7 +42,7 @@ class ScreenLockRule : TestRule {
    override fun apply(base: Statement, description: Description) = object : Statement() {
        override fun evaluate() {
            verifyNoScreenLockAlreadySet()
            verifyKeyguardDismissed()
            dismissKeyguard()
            setScreenLock()
            setLockOnPowerButton()

@@ -51,7 +51,7 @@ class ScreenLockRule : TestRule {
            } finally {
                removeScreenLock()
                revertLockOnPowerButton()
                verifyKeyguardDismissed()
                dismissKeyguard()
            }
        }
    }
@@ -62,30 +62,21 @@ class ScreenLockRule : TestRule {
                .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)")
    fun dismissKeyguard() {
        wait("keyguard dismissed") { count ->
            windowManager.dismissKeyguard(null, null)

            // Sometimes, bouncer gets shown due to a race, so we have to put display to sleep
            // and wake it back up to get it to go away
            if (waitCount >= 10 && waitCount % 5 == 0) {
                Log.i(TAG, "Escalation: attempting screen off/on to get rid of bouncer (+500ms)")
            if (count >= 10 && count % 5 == 0) {
                Log.i(TAG, "Escalation: attempting screen off/on to get rid of bouncer")
                uiDevice.sleep()
                Thread.sleep(250)
                uiDevice.wakeUp()
                Thread.sleep(250)
            }

            Thread.sleep(50)
            waitCount++
            !windowManager.isKeyguardLocked
        }
        assertWithMessage("Keyguard should be unlocked")
                .that(windowManager.isKeyguardLocked)
                .isFalse()
    }

    private fun setScreenLock() {
@@ -94,9 +85,7 @@ class ScreenLockRule : TestRule {
                LockscreenCredential.createNone(),
                context.userId
        )
        assertWithMessage("Screen Lock should now be set")
                .that(lockPatternUtils.isSecure(context.userId))
                .isTrue()
        wait("screen lock set") { lockPatternUtils.isSecure(context.userId) }
        Log.i(TAG, "Device PIN set to $PIN")
    }

@@ -110,21 +99,16 @@ class ScreenLockRule : TestRule {
                LockscreenCredential.createNone(),
                LockscreenCredential.createPin(PIN),
                context.userId)
        Thread.sleep(100)
        Log.i(TAG, "Removing screen lock")
        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()
        wait("screen lock un-set") {
            lockPatternUtils.isLockScreenDisabled(context.userId)
        }
        wait("screen lock insecure") { !lockPatternUtils.isSecure(context.userId) }
    }

    private fun revertLockOnPowerButton() {
+3 −10
Original line number Diff line number Diff line
@@ -50,7 +50,6 @@ class TrustAgentRule<T : BaseTrustAgentService>(
            verifyTrustServiceRunning()
            unlockDeviceWithCredential()
            enableTrustAgent()
            waitForEnablement()

            try {
                verifyAgentIsRunning()
@@ -80,15 +79,10 @@ class TrustAgentRule<T : BaseTrustAgentService>(
        lockPatternUtils.setEnabledTrustAgents(agents, userId)
    }

    private fun waitForEnablement() {
        Log.d(TAG, "Waiting for $WAIT_TIME ms")
        Thread.sleep(WAIT_TIME)
        Log.d(TAG, "Done waiting")
    }

    private fun verifyAgentIsRunning() {
        assertWithMessage("${serviceClass.simpleName} should be running")
            .that(BaseTrustAgentService.instance(serviceClass)).isNotNull()
        wait("${serviceClass.simpleName} to be running") {
            BaseTrustAgentService.instance(serviceClass) != null
        }
    }

    private fun disableTrustAgent() {
@@ -112,6 +106,5 @@ class TrustAgentRule<T : BaseTrustAgentService>(
            TrustAgentRule(T::class)

        private const val TAG = "TrustAgentRule"
        private val WAIT_TIME = 1000L
    }
}
Loading