Debian Codegraph @

tests/TrustTests/src/android/trust/test/UserUnlockRequestTest.kt

raw
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 companion object {
83 private const val TAG = "UserUnlockRequestTest"
84 private fun await() = Thread.sleep(250)
85 }
86 }
87
88 class UserUnlockRequestTrustAgent : BaseTrustAgentService() {
89 var onUserRequestedUnlockCallCount: Long = 0
90 private set
91 var onUserMayRequestUnlockCallCount: Long = 0
92 private set
93 var lastCallDismissKeyguard: Boolean = false
94 private set
95
96 override fun onUserRequestedUnlock(dismissKeyguard: Boolean) {
97 Log.i(TAG, "onUserRequestedUnlock($dismissKeyguard)")
98 onUserRequestedUnlockCallCount++
99 lastCallDismissKeyguard = dismissKeyguard
100 }
101
102 override fun onUserMayRequestUnlock() {
103 Log.i(TAG, "onUserMayRequestUnlock")
104 onUserMayRequestUnlockCallCount++
105 }
106
107 companion object {
108 private const val TAG = "UserUnlockRequestTrustAgent"
109 }
110 }