1 /* 2 * Copyright (C) 2025 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 com.android.settings.supervision 17 18 import android.app.KeyguardManager 19 import android.content.Context 20 import android.content.ContextWrapper 21 import android.content.pm.UserInfo 22 import android.os.UserManager 23 import android.os.UserManager.USER_TYPE_PROFILE_SUPERVISING 24 import androidx.test.core.app.ApplicationProvider 25 import androidx.test.ext.junit.runners.AndroidJUnit4 26 import com.android.settings.R 27 import com.google.common.truth.Truth.assertThat 28 import org.junit.Before 29 import org.junit.Test 30 import org.junit.runner.RunWith 31 import org.mockito.kotlin.mock 32 import org.mockito.kotlin.whenever 33 34 @RunWith(AndroidJUnit4::class) 35 class SupervisionPinManagementScreenTest { 36 private val mockKeyguardManager = mock<KeyguardManager>() 37 private val mockUserManager = mock<UserManager>() 38 39 private val context: Context = 40 object : ContextWrapper(ApplicationProvider.getApplicationContext()) { getSystemServicenull41 override fun getSystemService(name: String): Any? = 42 when (name) { 43 Context.KEYGUARD_SERVICE -> mockKeyguardManager 44 Context.USER_SERVICE -> mockUserManager 45 else -> super.getSystemService(name) 46 } 47 } 48 49 private val supervisionPinManagementScreen = SupervisionPinManagementScreen() 50 51 @Before setupnull52 fun setup() { 53 SupervisionHelper.sInstance = null 54 } 55 56 @Test keynull57 fun key() { 58 assertThat(supervisionPinManagementScreen.key).isEqualTo(SupervisionPinManagementScreen.KEY) 59 } 60 61 @Test isAvailablenull62 fun isAvailable() { 63 whenever(mockUserManager.users).thenReturn(listOf(SUPERVISING_USER_INFO)) 64 whenever(mockKeyguardManager.isDeviceSecure(SUPERVISING_USER_ID)).thenReturn(true) 65 66 assertThat(supervisionPinManagementScreen.isAvailable(context)).isTrue() 67 } 68 69 @Test isAvailable_noSupervisingUser_returnsFalsenull70 fun isAvailable_noSupervisingUser_returnsFalse() { 71 whenever(mockUserManager.users).thenReturn(emptyList()) 72 whenever(mockKeyguardManager.isDeviceSecure(SUPERVISING_USER_ID)).thenReturn(true) 73 74 assertThat(supervisionPinManagementScreen.isAvailable(context)).isFalse() 75 } 76 77 @Test isAvailable_noSupervisingCredential_returnsFalsenull78 fun isAvailable_noSupervisingCredential_returnsFalse() { 79 whenever(mockUserManager.users).thenReturn(listOf(SUPERVISING_USER_INFO)) 80 whenever(mockKeyguardManager.isDeviceSecure(SUPERVISING_USER_ID)).thenReturn(false) 81 82 assertThat(supervisionPinManagementScreen.isAvailable(context)).isFalse() 83 } 84 85 @Test getTitlenull86 fun getTitle() { 87 assertThat(supervisionPinManagementScreen.title) 88 .isEqualTo(R.string.supervision_pin_management_preference_title) 89 } 90 91 @Test getSummary_addPinnull92 fun getSummary_addPin() { 93 assertThat(supervisionPinManagementScreen.summary) 94 .isEqualTo(R.string.supervision_pin_management_preference_summary_add) 95 } 96 97 private companion object { 98 const val SUPERVISING_USER_ID = 5 99 val SUPERVISING_USER_INFO = 100 UserInfo( 101 SUPERVISING_USER_ID, 102 /* name */ "supervising", 103 /* iconPath */ "", 104 /* flags */ 0, 105 USER_TYPE_PROFILE_SUPERVISING, 106 ) 107 } 108 } 109