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 package android.trust.test 17 18 import android.app.trust.TrustManager 19 import android.content.Context 20 import android.trust.BaseTrustAgentService 21 import android.trust.TrustTestActivity 22 import android.trust.test.lib.ScreenLockRule 23 import android.trust.test.lib.TrustAgentRule 24 import android.util.Log 25 import androidx.test.core.app.ApplicationProvider.getApplicationContext 26 import androidx.test.ext.junit.rules.ActivityScenarioRule 27 import androidx.test.ext.junit.runners.AndroidJUnit4 28 import com.google.common.truth.Truth.assertThat 29 import org.junit.Rule 30 import org.junit.Test 31 import org.junit.rules.RuleChain 32 import org.junit.runner.RunWith 33 34 /** 35 * Test for the user unlock triggers. 36 * 37 * atest TrustTests:UserUnlockRequestTest 38 */ 39 @RunWith(AndroidJUnit4::class) 40 class UserUnlockRequestTest { 41 private val context = getApplicationContext<Context>() 42 private val trustManager = context.getSystemService(TrustManager::class.java) as TrustManager 43 private val userId = context.userId 44 private val activityScenarioRule = ActivityScenarioRule(TrustTestActivity::class.java) 45 private val trustAgentRule = TrustAgentRule<UserUnlockRequestTrustAgent>() 46 47 @get:Rule 48 val rule: RuleChain = RuleChain 49 .outerRule(activityScenarioRule) 50 .around(ScreenLockRule()) 51 .around(trustAgentRule) 52 53 @Test 54 fun reportUserRequestedUnlock_propagatesToAgent() { 55 val oldCount = trustAgentRule.agent.onUserRequestedUnlockCallCount 56 trustManager.reportUserRequestedUnlock(userId, false) 57 await() 58 59 assertThat(trustAgentRule.agent.onUserRequestedUnlockCallCount) 60 .isEqualTo(oldCount + 1) 61 } 62 63 @Test 64 fun reportUserRequestedUnlock_propagatesToAgentWithDismissKeyguard() { 65 trustManager.reportUserRequestedUnlock(userId, true) 66 await() 67 68 assertThat(trustAgentRule.agent.lastCallDismissKeyguard) 69 .isTrue() 70 } 71 72 @Test 73 fun reportUserMayRequestUnlock_propagatesToAgent() { 74 val oldCount = trustAgentRule.agent.onUserMayRequestUnlockCallCount 75 trustManager.reportUserMayRequestUnlock(userId) 76 await() 77 78 assertThat(trustAgentRule.agent.onUserMayRequestUnlockCallCount) 79 .isEqualTo(oldCount + 1) 80 } 81 82 @Test 83 fun reportUserMayRequestUnlock_differentUserId_doesNotPropagateToAgent() { 84 val oldCount = trustAgentRule.agent.onUserMayRequestUnlockCallCount 85 trustManager.reportUserMayRequestUnlock(userId + 1) 86 await() 87 88 assertThat(trustAgentRule.agent.onUserMayRequestUnlockCallCount) 89 .isEqualTo(oldCount) 90 } 91 92 companion object { 93 private const val TAG = "UserUnlockRequestTest" 94 private fun await() = Thread.sleep(250) 95 } 96 } 97 98 class UserUnlockRequestTrustAgent : BaseTrustAgentService() { 99 var onUserRequestedUnlockCallCount: Long = 0 100 private set 101 var onUserMayRequestUnlockCallCount: Long = 0 102 private set 103 var lastCallDismissKeyguard: Boolean = false 104 private set 105 106 override fun onUserRequestedUnlock(dismissKeyguard: Boolean) { 107 Log.i(TAG, "onUserRequestedUnlock($dismissKeyguard)") 108 onUserRequestedUnlockCallCount++ 109 lastCallDismissKeyguard = dismissKeyguard 110 } 111 112 override fun onUserMayRequestUnlock() { 113 Log.i(TAG, "onUserMayRequestUnlock") 114 onUserMayRequestUnlockCallCount++ 115 } 116 117 companion object { 118 private const val TAG = "UserUnlockRequestTrustAgent" 119 } 120 } 121