/* * Copyright (C) 2025 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.android.settings.supervision import android.app.KeyguardManager import android.content.ComponentName import android.content.ContextWrapper import android.content.pm.UserInfo import android.os.UserManager import android.os.UserManager.USER_TYPE_PROFILE_SUPERVISING import androidx.test.core.app.ApplicationProvider import androidx.test.ext.junit.runners.AndroidJUnit4 import com.android.settings.R import com.android.settings.password.ChooseLockGeneric import com.google.common.truth.Truth.assertThat import org.junit.Test import org.junit.runner.RunWith import org.mockito.kotlin.mock import org.mockito.kotlin.whenever @RunWith(AndroidJUnit4::class) class SupervisionChangePinPreferenceTest { private val mockUserManager = mock() private val mockKeyguardManager = mock() private val context = object : ContextWrapper(ApplicationProvider.getApplicationContext()) { override fun getSystemService(name: String): Any = when (name) { getSystemServiceName(UserManager::class.java) -> mockUserManager getSystemServiceName(KeyguardManager::class.java) -> mockKeyguardManager else -> super.getSystemService(name) } } private val preference = SupervisionChangePinPreference() @Test fun getTitle() { assertThat(preference.title).isEqualTo(R.string.supervision_change_pin_preference_title) } @Test fun getIntent_supervisingCredentialSet() { whenever(mockUserManager.users).thenReturn(listOf(SUPERVISING_USER_INFO)) whenever(mockKeyguardManager.isDeviceSecure(SUPERVISING_USER_ID)).thenReturn(true) assertThat(preference.intent(context)?.component) .isEqualTo(ComponentName(context, ChooseLockGeneric::class.java)) } @Test fun getIntent_supervisingCredentialNotSet() { assertThat(preference.intent(context)?.component).isNull() } private companion object { const val SUPERVISING_USER_ID = 5 val SUPERVISING_USER_INFO = UserInfo( SUPERVISING_USER_ID, /* name */ "supervising", /* iconPath */ "", /* flags */ 0, USER_TYPE_PROFILE_SUPERVISING, ) } }