/* * 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.supervision.SupervisionManager import android.app.supervision.SupervisionRecoveryInfo import android.app.supervision.SupervisionRecoveryInfo.STATE_PENDING import android.app.supervision.SupervisionRecoveryInfo.STATE_VERIFIED import android.app.supervision.flags.Flags import android.content.Context import android.content.ContextWrapper import android.platform.test.annotations.DisableFlags import android.platform.test.annotations.EnableFlags import android.platform.test.flag.junit.SetFlagsRule import androidx.test.core.app.ApplicationProvider import androidx.test.ext.junit.runners.AndroidJUnit4 import com.android.settings.R import com.google.common.truth.Truth.assertThat import org.junit.Assert.assertEquals import org.junit.Rule import org.junit.Test import org.junit.runner.RunWith import org.mockito.kotlin.mock import org.mockito.kotlin.whenever @RunWith(AndroidJUnit4::class) class SupervisionPinRecoveryPreferenceTest { private val appContext: Context = ApplicationProvider.getApplicationContext() private val mockSupervisionManager = mock() private val mContext = object : ContextWrapper(appContext) { override fun getSystemService(name: String): Any = when (name) { SUPERVISION_SERVICE -> mockSupervisionManager else -> super.getSystemService(name) } } private val preference = SupervisionPinRecoveryPreference() @get:Rule val setFlagsRule = SetFlagsRule() @Test fun getTitle() { assertThat(preference.title).isEqualTo(R.string.supervision_add_forgot_pin_preference_title) } @Test fun getIntent() { assertEquals( SupervisionPinRecoveryActivity.ACTION_RECOVERY, preference.intent(mContext)?.action, ) } @Test @EnableFlags(Flags.FLAG_ENABLE_SUPERVISION_PIN_RECOVERY_SCREEN) fun flagEnabled_recoveryNotExist_notAvailable() { whenever(mockSupervisionManager.supervisionRecoveryInfo).thenReturn(null) assertThat(preference.isAvailable(mContext)).isFalse() } @Test @EnableFlags(Flags.FLAG_ENABLE_SUPERVISION_PIN_RECOVERY_SCREEN) fun flagEnabled_recoveryInfoExist_isAvailable() { val recoveryInfo = SupervisionRecoveryInfo( /* accountName */ "email", /* accountType */ "default", /* state */ STATE_PENDING, /* accountData */ null, ) whenever(mockSupervisionManager.supervisionRecoveryInfo).thenReturn(recoveryInfo) assertThat(preference.isAvailable(mContext)).isTrue() } @Test @EnableFlags(Flags.FLAG_ENABLE_SUPERVISION_PIN_RECOVERY_SCREEN) fun flagEnabled_recoveryVerified_isAvailable() { val recoveryInfo = SupervisionRecoveryInfo( /* accountName */ "email", /* accountType */ "default", /* state */ STATE_VERIFIED, /* accountData */ null, ) whenever(mockSupervisionManager.supervisionRecoveryInfo).thenReturn(recoveryInfo) assertThat(preference.isAvailable(mContext)).isTrue() } @Test @DisableFlags(Flags.FLAG_ENABLE_SUPERVISION_PIN_RECOVERY_SCREEN) fun flagDisabled_recoveryInfoExist_notAvailable() { val recoveryInfo = SupervisionRecoveryInfo( /* accountName */ "email", /* accountType */ "default", /* state */ STATE_VERIFIED, /* accountData */ null, ) whenever(mockSupervisionManager.supervisionRecoveryInfo).thenReturn(recoveryInfo) assertThat(preference.isAvailable(mContext)).isFalse() } }