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.android.launcher3.util.Executors.MAIN_EXECUTOR 26 import com.android.launcher3.util.Executors.UI_HELPER_EXECUTOR 27 import com.google.common.truth.Truth.assertThat 28 import org.junit.After 29 import org.junit.Before 30 import org.junit.Test 31 import org.junit.runner.RunWith 32 import org.mockito.kotlin.mock 33 import org.mockito.kotlin.verify 34 import org.mockito.kotlin.verifyNoMoreInteractions 35 import org.mockito.kotlin.whenever 36 37 /** Unit tests for {@link LockedUserState} */ 38 @SmallTest 39 @RunWith(AndroidJUnit4::class) 40 class LockedUserStateTest { 41 42 private val userManager: UserManager = mock() 43 private val context: Context = mock() 44 private val lifeCycle: DaggerSingletonTracker = mock() 45 46 @Before setupnull47 fun setup() { 48 whenever(context.getSystemService(UserManager::class.java)).thenReturn(userManager) 49 } 50 51 @After tearDownnull52 fun tearDown() { 53 UI_HELPER_EXECUTOR.submit {}.get() 54 MAIN_EXECUTOR.submit {}.get() 55 } 56 57 @Test runOnUserUnlocked_runs_action_immediately_if_already_unlockednull58 fun runOnUserUnlocked_runs_action_immediately_if_already_unlocked() { 59 whenever(userManager.isUserUnlocked(Process.myUserHandle())).thenReturn(true) 60 val action: Runnable = mock() 61 LockedUserState(context, lifeCycle).runOnUserUnlocked(action) 62 verify(action).run() 63 } 64 65 @Test runOnUserUnlocked_waits_to_run_action_until_user_is_unlockednull66 fun runOnUserUnlocked_waits_to_run_action_until_user_is_unlocked() { 67 whenever(userManager.isUserUnlocked(Process.myUserHandle())).thenReturn(false) 68 val action: Runnable = mock() 69 val state = LockedUserState(context, lifeCycle) 70 state.runOnUserUnlocked(action) 71 // b/343530737 72 verifyNoMoreInteractions(action) 73 state.userUnlockedReceiver.onReceive(context, Intent(Intent.ACTION_USER_UNLOCKED)) 74 verify(action).run() 75 } 76 77 @Test isUserUnlocked_returns_true_when_user_is_unlockednull78 fun isUserUnlocked_returns_true_when_user_is_unlocked() { 79 whenever(userManager.isUserUnlocked(Process.myUserHandle())).thenReturn(true) 80 assertThat(LockedUserState(context, lifeCycle).isUserUnlocked).isTrue() 81 } 82 83 @Test isUserUnlocked_returns_false_when_user_is_lockednull84 fun isUserUnlocked_returns_false_when_user_is_locked() { 85 whenever(userManager.isUserUnlocked(Process.myUserHandle())).thenReturn(false) 86 assertThat(LockedUserState(context, lifeCycle).isUserUnlocked).isFalse() 87 } 88 } 89