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.kotlin.mock 30 import org.mockito.kotlin.verify 31 import org.mockito.kotlin.verifyNoMoreInteractions 32 import org.mockito.kotlin.whenever 33 34 /** Unit tests for {@link LockedUserState} */ 35 @SmallTest 36 @RunWith(AndroidJUnit4::class) 37 class LockedUserStateTest { 38 39 private val userManager: UserManager = mock() 40 private val context: Context = mock() 41 42 @Before setupnull43 fun setup() { 44 whenever(context.getSystemService(UserManager::class.java)).thenReturn(userManager) 45 } 46 47 @Test runOnUserUnlocked_runs_action_immediately_if_already_unlockednull48 fun runOnUserUnlocked_runs_action_immediately_if_already_unlocked() { 49 whenever(userManager.isUserUnlocked(Process.myUserHandle())).thenReturn(true) 50 val action: Runnable = mock() 51 LockedUserState(context).runOnUserUnlocked(action) 52 verify(action).run() 53 } 54 55 @Test runOnUserUnlocked_waits_to_run_action_until_user_is_unlockednull56 fun runOnUserUnlocked_waits_to_run_action_until_user_is_unlocked() { 57 whenever(userManager.isUserUnlocked(Process.myUserHandle())).thenReturn(false) 58 val action: Runnable = mock() 59 val state = LockedUserState(context) 60 state.runOnUserUnlocked(action) 61 // b/343530737 62 verifyNoMoreInteractions(action) 63 state.mUserUnlockedReceiver.onReceive(context, Intent(Intent.ACTION_USER_UNLOCKED)) 64 verify(action).run() 65 } 66 67 @Test isUserUnlocked_returns_true_when_user_is_unlockednull68 fun isUserUnlocked_returns_true_when_user_is_unlocked() { 69 whenever(userManager.isUserUnlocked(Process.myUserHandle())).thenReturn(true) 70 assertThat(LockedUserState(context).isUserUnlocked).isTrue() 71 } 72 73 @Test isUserUnlocked_returns_false_when_user_is_lockednull74 fun isUserUnlocked_returns_false_when_user_is_locked() { 75 whenever(userManager.isUserUnlocked(Process.myUserHandle())).thenReturn(false) 76 assertThat(LockedUserState(context).isUserUnlocked).isFalse() 77 } 78 } 79