1 /* 2 * Copyright (C) 2022 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package android.trust.test 18 19 import android.app.trust.TrustManager 20 import android.content.Context 21 import android.service.trust.TrustAgentService.FLAG_GRANT_TRUST_TEMPORARY_AND_RENEWABLE 22 import android.trust.BaseTrustAgentService 23 import android.trust.TrustTestActivity 24 import android.trust.test.lib.LockStateTrackingRule 25 import android.trust.test.lib.ScreenLockRule 26 import android.trust.test.lib.TestTrustListener 27 import android.trust.test.lib.TrustAgentRule 28 import androidx.test.core.app.ApplicationProvider.getApplicationContext 29 import androidx.test.ext.junit.rules.ActivityScenarioRule 30 import androidx.test.ext.junit.runners.AndroidJUnit4 31 import androidx.test.platform.app.InstrumentationRegistry.getInstrumentation 32 import androidx.test.uiautomator.UiDevice 33 import com.google.common.truth.Truth.assertThat 34 import org.junit.After 35 import org.junit.Before 36 import org.junit.Rule 37 import org.junit.Test 38 import org.junit.rules.RuleChain 39 import org.junit.runner.RunWith 40 41 /** 42 * Test for testing isActiveUnlockRunning. 43 * 44 * atest TrustTests:IsActiveUnlockRunningTest 45 */ 46 @RunWith(AndroidJUnit4::class) 47 class IsActiveUnlockRunningTest { 48 private val uiDevice = UiDevice.getInstance(getInstrumentation()) 49 private val context: Context = getApplicationContext() 50 private val userId = context.userId 51 private val trustManager = context.getSystemService(TrustManager::class.java) as TrustManager 52 private val activityScenarioRule = ActivityScenarioRule(TrustTestActivity::class.java) 53 private val lockStateTrackingRule = LockStateTrackingRule() 54 private val trustAgentRule = TrustAgentRule<IsActiveUnlockRunningTrustAgent>() 55 56 private val listener = object : TestTrustListener() { 57 var isRunning = false 58 private set 59 60 override fun onIsActiveUnlockRunningChanged(isRunning: Boolean, userId: Int) { 61 this.isRunning = isRunning 62 } 63 } 64 65 @get:Rule 66 val rule: RuleChain = RuleChain 67 .outerRule(activityScenarioRule) 68 .around(ScreenLockRule()) 69 .around(lockStateTrackingRule) 70 .around(trustAgentRule) 71 72 @Before 73 fun manageTrust() { 74 trustAgentRule.agent.setManagingTrust(true) 75 trustManager.registerTrustListener(listener) 76 } 77 78 @After 79 fun unregisterListener() { 80 trustManager.unregisterTrustListener(listener) 81 } 82 83 @Test 84 fun defaultState_isActiveUnlockRunningIsFalse() { 85 assertThat(trustManager.isActiveUnlockRunning(userId)).isFalse() 86 assertThat(listener.isRunning).isFalse() 87 } 88 89 @Test 90 fun grantTrustLockedDevice_isActiveUnlockRunningIsFalse() { 91 uiDevice.sleep() 92 lockStateTrackingRule.assertLocked() 93 94 uiDevice.wakeUp() 95 trustAgentRule.agent.grantTrust( 96 GRANT_MESSAGE, 0, FLAG_GRANT_TRUST_TEMPORARY_AND_RENEWABLE) {} 97 98 assertThat(trustManager.isActiveUnlockRunning(userId)).isFalse() 99 assertThat(listener.isRunning).isFalse() 100 } 101 102 @Test 103 fun grantTrustUnlockedDevice_isActiveUnlockRunningIsTrueWhileLocked() { 104 trustAgentRule.agent.grantTrust( 105 GRANT_MESSAGE, 0, FLAG_GRANT_TRUST_TEMPORARY_AND_RENEWABLE) {} 106 uiDevice.sleep() 107 108 lockStateTrackingRule.assertLocked() 109 110 assertThat(trustManager.isActiveUnlockRunning(userId)).isTrue() 111 assertThat(listener.isRunning).isTrue() 112 } 113 114 @Test 115 fun trustRevoked_isActiveUnlockRunningIsFalse() { 116 trustAgentRule.agent.grantTrust( 117 GRANT_MESSAGE, 0, FLAG_GRANT_TRUST_TEMPORARY_AND_RENEWABLE) {} 118 119 trustAgentRule.agent.revokeTrust() 120 121 assertThat(trustManager.isActiveUnlockRunning(userId)).isFalse() 122 assertThat(listener.isRunning).isFalse() 123 } 124 125 companion object { 126 private const val GRANT_MESSAGE = "granted by test" 127 private fun await(millis: Long) = Thread.sleep(millis) 128 } 129 } 130 131 class IsActiveUnlockRunningTrustAgent : BaseTrustAgentService() 132