• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2023 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 com.android.launcher3.util
18 
19 import android.content.Context
20 import android.content.Intent
21 import android.os.Process
22 import android.os.UserManager
23 import androidx.test.ext.junit.runners.AndroidJUnit4
24 import androidx.test.filters.SmallTest
25 import com.google.common.truth.Truth.assertThat
26 import org.junit.Before
27 import org.junit.Test
28 import org.junit.runner.RunWith
29 import org.mockito.Mock
30 import org.mockito.Mockito.verify
31 import org.mockito.Mockito.verifyZeroInteractions
32 import org.mockito.Mockito.`when`
33 import org.mockito.MockitoAnnotations
34 
35 /** Unit tests for {@link LockedUserUtil} */
36 @SmallTest
37 @RunWith(AndroidJUnit4::class)
38 class LockedUserStateTest {
39 
40     @Mock lateinit var userManager: UserManager
41     @Mock lateinit var context: Context
42 
43     @Before
setupnull44     fun setup() {
45         MockitoAnnotations.initMocks(this)
46         `when`(context.getSystemService(UserManager::class.java)).thenReturn(userManager)
47     }
48 
49     @Test
runOnUserUnlocked_runs_action_immediately_if_already_unlockednull50     fun runOnUserUnlocked_runs_action_immediately_if_already_unlocked() {
51         `when`(userManager.isUserUnlocked(Process.myUserHandle())).thenReturn(true)
52         LockedUserState.INSTANCE.initializeForTesting(LockedUserState(context))
53         val action: Runnable = mock()
54 
55         LockedUserState.get(context).runOnUserUnlocked(action)
56         verify(action).run()
57     }
58 
59     @Test
runOnUserUnlocked_waits_to_run_action_until_user_is_unlockednull60     fun runOnUserUnlocked_waits_to_run_action_until_user_is_unlocked() {
61         `when`(userManager.isUserUnlocked(Process.myUserHandle())).thenReturn(false)
62         LockedUserState.INSTANCE.initializeForTesting(LockedUserState(context))
63         val action: Runnable = mock()
64 
65         LockedUserState.get(context).runOnUserUnlocked(action)
66         verifyZeroInteractions(action)
67 
68         LockedUserState.get(context)
69             .mUserUnlockedReceiver
70             .onReceive(context, Intent(Intent.ACTION_USER_UNLOCKED))
71 
72         verify(action).run()
73     }
74 
75     @Test
isUserUnlocked_returns_true_when_user_is_unlockednull76     fun isUserUnlocked_returns_true_when_user_is_unlocked() {
77         `when`(userManager.isUserUnlocked(Process.myUserHandle())).thenReturn(true)
78         LockedUserState.INSTANCE.initializeForTesting(LockedUserState(context))
79         assertThat(LockedUserState.get(context).isUserUnlocked).isTrue()
80     }
81 
82     @Test
isUserUnlocked_returns_false_when_user_is_lockednull83     fun isUserUnlocked_returns_false_when_user_is_locked() {
84         `when`(userManager.isUserUnlocked(Process.myUserHandle())).thenReturn(false)
85         LockedUserState.INSTANCE.initializeForTesting(LockedUserState(context))
86         assertThat(LockedUserState.get(context).isUserUnlocked).isFalse()
87     }
88 }
89