/*
 * Copyright (C) 2023 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.biometrics.fingerprint;

import static android.hardware.fingerprint.FingerprintSensorProperties.TYPE_POWER_BUTTON;
import static android.hardware.fingerprint.FingerprintSensorProperties.TYPE_REAR;
import static android.hardware.fingerprint.FingerprintSensorProperties.TYPE_UDFPS_OPTICAL;
import static android.hardware.fingerprint.FingerprintSensorProperties.TYPE_UDFPS_ULTRASONIC;

import static com.android.settings.biometrics.BiometricEnrollBase.BIOMETRIC_AUTH_REQUEST;
import static com.android.settings.biometrics.BiometricEnrollBase.CONFIRM_REQUEST;
import static com.android.settings.biometrics.BiometricEnrollBase.RESULT_FINISHED;
import static com.android.settings.biometrics.fingerprint.FingerprintSettings.FingerprintSettingsFragment;
import static com.android.settings.biometrics.fingerprint.FingerprintSettings.FingerprintSettingsFragment.CHOOSE_LOCK_GENERIC_REQUEST;
import static com.android.settings.biometrics.fingerprint.FingerprintSettings.FingerprintSettingsFragment.KEY_FINGERPRINT_UNLOCK_CATEGORY;
import static com.android.settings.biometrics.fingerprint.FingerprintSettings.FingerprintSettingsFragment.KEY_REQUIRE_SCREEN_ON_TO_AUTH;
import static com.android.settings.biometrics.fingerprint.FingerprintSettings.FingerprintSettingsFragment.KEY_SCREEN_OFF_FINGERPRINT_UNLOCK;

import static com.google.common.truth.Truth.assertThat;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.robolectric.Shadows.shadowOf;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.UserInfo;
import android.content.res.Resources;
import android.hardware.biometrics.BiometricManager;
import android.hardware.biometrics.ComponentInfoInternal;
import android.hardware.biometrics.SensorProperties;
import android.hardware.fingerprint.Fingerprint;
import android.hardware.fingerprint.FingerprintManager;
import android.hardware.fingerprint.FingerprintSensorProperties;
import android.hardware.fingerprint.FingerprintSensorPropertiesInternal;
import android.os.Bundle;
import android.os.CancellationSignal;
import android.os.Looper;
import android.os.UserHandle;
import android.os.UserManager;
import android.os.Vibrator;
import android.platform.test.annotations.DisableFlags;
import android.platform.test.annotations.EnableFlags;
import android.platform.test.flag.junit.SetFlagsRule;
import android.provider.Settings;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import androidx.preference.Preference;
import androidx.test.core.app.ApplicationProvider;

import com.android.settings.biometrics.fingerprint.feature.FingerprintExtPreferencesProvider;
import com.android.internal.widget.LockPatternUtils;
import com.android.settings.biometrics.fingerprint.feature.PrimarySwitchIntentPreference;
import com.android.settings.biometrics.fingerprint.feature.SfpsRestToUnlockFeature;
import com.android.settings.password.ChooseLockSettingsHelper;
import com.android.settings.password.ConfirmDeviceCredentialActivity;
import com.android.settings.search.BaseSearchIndexProvider;
import com.android.settings.testutils.FakeFeatureFactory;
import com.android.settings.testutils.shadow.ShadowFragment;
import com.android.settings.testutils.shadow.ShadowLockPatternUtils;
import com.android.settings.testutils.shadow.ShadowSettingsPreferenceFragment;
import com.android.settings.testutils.shadow.ShadowUserManager;
import com.android.settings.testutils.shadow.ShadowUtils;
import com.android.settingslib.RestrictedPreference;
import com.android.settingslib.RestrictedSwitchPreference;

import org.junit.After;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnit;
import org.mockito.junit.MockitoRule;
import org.robolectric.Robolectric;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;

import java.util.ArrayList;
import java.util.List;

@RunWith(RobolectricTestRunner.class)
@Config(shadows = {ShadowSettingsPreferenceFragment.class, ShadowUtils.class, ShadowFragment.class,
        ShadowUserManager.class, ShadowLockPatternUtils.class})
public class FingerprintSettingsFragmentTest {
    @Rule
    public final SetFlagsRule mSetFlagsRule = new SetFlagsRule();
    @Rule
    public final MockitoRule mMockitoRule = MockitoJUnit.rule();

    private static final int PRIMARY_USER_ID = 0;
    private static final int GUEST_USER_ID = 10;

    private FingerprintSettingsFragment mFragment;
    private Context mContext;
    private FragmentActivity mActivity;

    @Mock
    private FingerprintManager mFingerprintManager;
    @Mock
    private FragmentTransaction mFragmentTransaction;
    @Mock
    private PackageManager mPackageManager;
    @Mock
    private BiometricManager mBiometricManager;
    @Mock
    private UserManager mUserManager;
    @Mock
    private FingerprintExtPreferencesProvider mExtPreferencesProvider;
    @Mock
    private RestrictedPreference mRestrictedPreference0;
    @Mock
    private RestrictedPreference mRestrictedPreference1;

    @Captor
    private ArgumentCaptor<CancellationSignal> mCancellationSignalArgumentCaptor =
            ArgumentCaptor.forClass(CancellationSignal.class);
    @Captor
    private ArgumentCaptor<FingerprintManager.AuthenticationCallback>
            mAuthenticationCallbackArgumentCaptor = ArgumentCaptor.forClass(
            FingerprintManager.AuthenticationCallback.class);

    @Mock
    private Vibrator mVibrator;

    private FingerprintSettingsFeatureProvider mFingerprintSettingsFeatureProvider;

    private FakeFeatureFactory mFakeFeatureFactory;
    private FingerprintAuthenticateSidecar mFingerprintAuthenticateSidecar;
    private FingerprintRemoveSidecar mFingerprintRemoveSidecar;

    private FakeFeatureFactory mFeatureFactory;
    @Mock
    private LockPatternUtils mLockPatternUtils;
    @Mock
    private SfpsRestToUnlockFeature mSfpsRestToUnlockFeature;

    @Before
    public void setUp() {
        ShadowUtils.setFingerprintManager(mFingerprintManager);
        mFakeFeatureFactory = FakeFeatureFactory.setupForTest();

        mContext = spy(ApplicationProvider.getApplicationContext());
        mFragment = spy(new FingerprintSettingsFragment());
        mFingerprintSettingsFeatureProvider = new FingerprintSettingsFeatureProvider();
        doReturn(mContext).when(mFragment).getContext();
        doReturn(mBiometricManager).when(mContext).getSystemService(BiometricManager.class);
        doReturn(true).when(mFingerprintManager).isHardwareDetected();
        doReturn(mVibrator).when(mContext).getSystemService(Vibrator.class);
        doReturn(true).when(mUserManager).isProfile(GUEST_USER_ID);
        doReturn(mUserManager).when(mContext).getSystemService(UserManager.class);
        when(mBiometricManager.canAuthenticate(PRIMARY_USER_ID,
                BiometricManager.Authenticators.IDENTITY_CHECK))
                .thenReturn(BiometricManager.BIOMETRIC_ERROR_HW_UNAVAILABLE);
        when(mFakeFeatureFactory.getFingerprintFeatureProvider()
                .getFingerprintSettingsFeatureProvider())
                .thenReturn(mFingerprintSettingsFeatureProvider);

        when(mFakeFeatureFactory.getFingerprintFeatureProvider()
                .getExtPreferenceProvider(mContext))
                .thenReturn(mExtPreferencesProvider);
        when(mExtPreferencesProvider.getSize()).thenReturn(0);

        when(mFakeFeatureFactory.securityFeatureProvider.getLockPatternUtils(mContext))
                .thenReturn(mLockPatternUtils);
        when(mFakeFeatureFactory.mFingerprintFeatureProvider.getSfpsRestToUnlockFeature(mContext))
                .thenReturn(mSfpsRestToUnlockFeature);

        when(mLockPatternUtils.checkUserSupportsBiometricSecondFactor(anyInt())).thenReturn(true);
    }

    @After
    public void tearDown() {
        ShadowUtils.reset();
    }

    @Test
    public void testChooseLockKeyForFingerprint() {
        setUpFragment(true);
        ArgumentCaptor<Intent> intentArgumentCaptor = ArgumentCaptor.forClass(
                Intent.class);
        verify(mFragment).startActivityForResult(intentArgumentCaptor.capture(),
                eq(CHOOSE_LOCK_GENERIC_REQUEST));

        Intent intent = intentArgumentCaptor.getValue();
        assertThat(intent.getBooleanExtra(ChooseLockSettingsHelper.EXTRA_KEY_FOR_FINGERPRINT,
                false)).isTrue();
    }

    @Test
    @Ignore("b/353706169")
    public void testLaunchBiometricPromptForFingerprint() {
        when(mBiometricManager.canAuthenticate(PRIMARY_USER_ID,
                BiometricManager.Authenticators.IDENTITY_CHECK))
                .thenReturn(BiometricManager.BIOMETRIC_SUCCESS);
        doNothing().when(mFingerprintManager).generateChallenge(anyInt(), any());
        when(mFingerprintManager.hasEnrolledFingerprints(anyInt())).thenReturn(true);
        setUpFragment(false);
        ArgumentCaptor<Intent> intentArgumentCaptor = ArgumentCaptor.forClass(Intent.class);
        mFragment.onActivityResult(CONFIRM_REQUEST, RESULT_FINISHED,
                new Intent().putExtra(ChooseLockSettingsHelper.EXTRA_KEY_GK_PW_HANDLE, 1L));

        verify(mFragment).startActivityForResult(intentArgumentCaptor.capture(),
                eq(BIOMETRIC_AUTH_REQUEST));

        final Intent intent = intentArgumentCaptor.getValue();
        assertThat(intent.getComponent().getClassName()).isEqualTo(
                ConfirmDeviceCredentialActivity.InternalActivity.class.getName());
    }

    // Test the case when FingerprintAuthenticateSidecar receives an error callback from the
    // framework or from another authentication client. The cancellation signal should not be set
    // to null because there may exist a running authentication client.
    // The signal can only be cancelled from the caller in FingerprintSettings.
    @Test
    public void testCancellationSignalLifeCycle() {
        setUpFragment(false);

        mFingerprintAuthenticateSidecar.setFingerprintManager(mFingerprintManager);

        doNothing().when(mFingerprintManager).authenticate(any(),
                mCancellationSignalArgumentCaptor.capture(),
                mAuthenticationCallbackArgumentCaptor.capture(), any(), anyInt());

        mFingerprintAuthenticateSidecar.startAuthentication(1);

        assertThat(mAuthenticationCallbackArgumentCaptor.getValue()).isNotNull();
        assertThat(mCancellationSignalArgumentCaptor.getValue()).isNotNull();

        // Authentication error callback should not cancel the signal.
        mAuthenticationCallbackArgumentCaptor.getValue().onAuthenticationError(0, "");
        assertThat(mFingerprintAuthenticateSidecar.isCancelled()).isFalse();

        // The signal should be cancelled when caller stops the authentication.
        mFingerprintAuthenticateSidecar.stopAuthentication();
        assertThat(mFingerprintAuthenticateSidecar.isCancelled()).isTrue();
    }

    @Ignore("b/313342682")
    @Test
    public void testGuestUserRequireScreenOnToAuth() {
        Settings.Secure.putIntForUser(
                mContext.getContentResolver(),
                Settings.Secure.SFPS_PERFORMANT_AUTH_ENABLED,
                0,
                UserHandle.of(PRIMARY_USER_ID).getIdentifier());

        Settings.Secure.putIntForUser(
                mContext.getContentResolver(),
                Settings.Secure.SFPS_PERFORMANT_AUTH_ENABLED,
                1,
                UserHandle.of(GUEST_USER_ID).getIdentifier());

        setUpFragment(false, GUEST_USER_ID, TYPE_POWER_BUTTON, 1);

        final RestrictedSwitchPreference requireScreenOnToAuthPreference = mFragment.findPreference(
                KEY_REQUIRE_SCREEN_ON_TO_AUTH);
        assertThat(requireScreenOnToAuthPreference.isChecked()).isTrue();
    }

    private void setUpFragment(boolean showChooseLock) {
        setUpFragment(showChooseLock, PRIMARY_USER_ID, TYPE_UDFPS_OPTICAL, 1);
    }

    private void setUpFragment(boolean showChooseLock, int maxFingerprints) {
        setUpFragment(showChooseLock, PRIMARY_USER_ID, TYPE_UDFPS_OPTICAL, maxFingerprints);
    }

    private void setUpFragment(boolean showChooseLock, int userId,
            @FingerprintSensorProperties.SensorType int sensorType, int maxFingerprints) {
        ShadowUserManager.getShadow().addProfile(new UserInfo(userId, "", 0));

        Intent intent = new Intent();
        if (!showChooseLock) {
            intent.putExtra(ChooseLockSettingsHelper.EXTRA_KEY_CHALLENGE_TOKEN, new byte[0]);
            intent.putExtra(ChooseLockSettingsHelper.EXTRA_KEY_GK_PW_HANDLE, 1L);
        }
        intent.putExtra(Intent.EXTRA_USER_ID, userId);
        mActivity = spy(Robolectric.buildActivity(FragmentActivity.class, intent).get());
        doReturn(mActivity).when(mFragment).getActivity();

        FragmentManager fragmentManager = mock(FragmentManager.class);
        doReturn(mFragmentTransaction).when(fragmentManager).beginTransaction();
        doReturn(mFragmentTransaction).when(mFragmentTransaction).add(any(), anyString());
        doReturn(fragmentManager).when(mFragment).getFragmentManager();
        doReturn(fragmentManager).when(mActivity).getSupportFragmentManager();

        mFingerprintAuthenticateSidecar = new FingerprintAuthenticateSidecar();
        doReturn(mFingerprintAuthenticateSidecar).when(fragmentManager).findFragmentByTag(
                "authenticate_sidecar");

        mFingerprintRemoveSidecar = new FingerprintRemoveSidecar();
        doReturn(mFingerprintRemoveSidecar).when(fragmentManager).findFragmentByTag(
                "removal_sidecar");

        doNothing().when(mFragment).startActivityForResult(any(Intent.class), anyInt());

        setSensor(sensorType, maxFingerprints);

        // Start fragment
        mFragment.onAttach(mContext);
        mFragment.onCreate(null);
        mFragment.onCreateView(LayoutInflater.from(mContext), mock(ViewGroup.class), Bundle.EMPTY);
        mFragment.onResume();
    }

    @Ignore("b/315519360")
    @Test
    public void testFragmentVisibleWhenNoHardwareDetected() {
        doReturn(false).when(mFingerprintManager).isHardwareDetected();
        setUpFragment(false);
        assertThat(mFragment.isVisible()).isTrue();
    }

    @Test
    @Ignore("b/353726774")
    public void fingerprintVibratesOnAuthSuccess() {
        setUpFragment(false);

        doNothing().when(mFingerprintManager).authenticate(any(),
                mCancellationSignalArgumentCaptor.capture(),
                mAuthenticationCallbackArgumentCaptor.capture(), any(), anyInt());

        mFingerprintAuthenticateSidecar.startAuthentication(1);

        assertThat(mAuthenticationCallbackArgumentCaptor.getValue()).isNotNull();
        assertThat(mCancellationSignalArgumentCaptor.getValue()).isNotNull();

        mAuthenticationCallbackArgumentCaptor.getValue()
                .onAuthenticationSucceeded(new FingerprintManager.AuthenticationResult(null,
                        new Fingerprint("finger 1", 1, 1), 0 /* userId */, false));

        shadowOf(Looper.getMainLooper()).idle();
        verify(mVibrator).vibrate(FingerprintSettings.SUCCESS_VIBRATION_EFFECT);
    }

    @Test
    public void testNotIndexable_whenDisabled() {
        doReturn(mPackageManager).when(mContext).getPackageManager();
        doReturn(false)
                .when(mPackageManager).hasSystemFeature(PackageManager.FEATURE_FINGERPRINT);

        final BaseSearchIndexProvider provider = FingerprintSettingsFragment.SEARCH_INDEX_DATA_PROVIDER;
        assertThat(provider.getDynamicRawDataToIndex(mContext, true)).isEmpty();
    }

    @Ignore("b/353726774")
    @Test
    public void testAddButtonWorksAfterRemovalError() {
        final Fingerprint fingerprint = new Fingerprint("Test", 0, 0);
        doReturn(List.of(fingerprint)).when(mFingerprintManager).getEnrolledFingerprints(anyInt());
        setUpFragment(false, 5);
        shadowOf(Looper.getMainLooper()).idle();
        final Preference addPref = mFragment.findPreference("key_fingerprint_add");
        final FingerprintSettings.FingerprintPreference fpPref =
                mFragment.findPreference("key_fingerprint_item_0");
        assertThat(fpPref).isNotNull();
        assertThat(addPref).isNotNull();
        assertThat(addPref.isEnabled()).isTrue();

        mFingerprintRemoveSidecar.setListener(mFragment.mRemovalListener);
        mFragment.deleteFingerPrint(fingerprint);
        verify(mFingerprintManager).remove(any(), anyInt(), any());
        assertThat(addPref.isEnabled()).isFalse();

        mFingerprintRemoveSidecar.mRemoveCallback.onRemovalError(fingerprint, 0, "failure");

        shadowOf(Looper.getMainLooper()).idle();
        assertThat(addPref.isEnabled()).isTrue();
    }

    @Test
    @EnableFlags(com.android.settings.flags.Flags.FLAG_BIOMETRICS_ONBOARDING_EDUCATION)
    public void testCheckEnrolledShown_whenAtLeastOneFingerprintEnrolled_Udfps() {
        final Fingerprint fingerprint = new Fingerprint("Test", 0, 0);
        doReturn(List.of(fingerprint)).when(mFingerprintManager).getEnrolledFingerprints(anyInt());
        setUpFragment(false, PRIMARY_USER_ID, TYPE_UDFPS_OPTICAL, 5);

        shadowOf(Looper.getMainLooper()).idle();

        final Preference checkEnrolledPerf =
                mFragment.findPreference("key_fingerprint_check_enrolled");
        assertThat(checkEnrolledPerf).isNotNull();
        assertThat(checkEnrolledPerf.isVisible()).isTrue();
    }

    @Test
    @EnableFlags(com.android.settings.flags.Flags.FLAG_BIOMETRICS_ONBOARDING_EDUCATION)
    public void testCheckEnrolledHide_whenNoFingerprintEnrolled_Udfps() {
        doReturn(List.of()).when(mFingerprintManager).getEnrolledFingerprints(anyInt());
        setUpFragment(false, PRIMARY_USER_ID, TYPE_UDFPS_OPTICAL, 5);

        shadowOf(Looper.getMainLooper()).idle();

        final Preference checkEnrolledPerf =
                mFragment.findPreference("key_fingerprint_check_enrolled");
        assertThat(checkEnrolledPerf).isNull();
    }

    @Test
    @EnableFlags(com.android.settings.flags.Flags.FLAG_BIOMETRICS_ONBOARDING_EDUCATION)
    public void testUseFingerprintToPreference_isShown() {
        doReturn(List.of()).when(mFingerprintManager).getEnrolledFingerprints(anyInt());
        setUpFragment(false, PRIMARY_USER_ID, TYPE_UDFPS_OPTICAL, 5);

        shadowOf(Looper.getMainLooper()).idle();

        final Preference preference =
                mFragment.findPreference("biometric_settings_use_fingerprint_to");
        assertThat(preference.isVisible()).isTrue();
    }

    @Test
    @EnableFlags(com.android.settings.flags.Flags.FLAG_BIOMETRICS_ONBOARDING_EDUCATION)
    public void testCheckEnrolledHide_nonUdfps() {
        final Fingerprint fingerprint = new Fingerprint("Test", 0, 0);
        doReturn(List.of(fingerprint)).when(mFingerprintManager).getEnrolledFingerprints(anyInt());
        setUpFragment(false, PRIMARY_USER_ID, TYPE_REAR, 5);

        shadowOf(Looper.getMainLooper()).idle();

        final Preference checkEnrolledPerf =
                mFragment.findPreference("key_fingerprint_check_enrolled");
        assertThat(checkEnrolledPerf).isNull();
    }

    @Test
    public void testScreenOffUnlockHide_nonUdfps() {
        mFragment.mRequireScreenOnToAuthPreferenceController =
                mock(FingerprintSettingsRequireScreenOnToAuthPreferenceController.class);

        final Fingerprint fingerprint = new Fingerprint("Test", 0, 0);
        doReturn(List.of(fingerprint)).when(mFingerprintManager).getEnrolledFingerprints(anyInt());
        setUpFragment(false, PRIMARY_USER_ID, TYPE_POWER_BUTTON, 5);

        shadowOf(Looper.getMainLooper()).idle();

        final Preference screenOffUnlockPerf =
                mFragment.findPreference(KEY_SCREEN_OFF_FINGERPRINT_UNLOCK);
        assertThat(screenOffUnlockPerf).isNotNull();
        assertThat(screenOffUnlockPerf.isVisible()).isFalse();
    }

    @Test
    public void testScreenOffUnlockHide_managedProfile() {
        ensureScreenOffUnlockConfigEnabled();
        final Fingerprint fingerprint = new Fingerprint("Test", 0, 0);
        doReturn(List.of(fingerprint)).when(mFingerprintManager).getEnrolledFingerprints(anyInt());
        setUpFragment(false, GUEST_USER_ID, TYPE_UDFPS_ULTRASONIC, 5);

        shadowOf(Looper.getMainLooper()).idle();

        final Preference screenOffUnlockPerf =
                mFragment.findPreference(KEY_SCREEN_OFF_FINGERPRINT_UNLOCK);
        assertThat(screenOffUnlockPerf).isNotNull();
        assertThat(screenOffUnlockPerf.isVisible()).isFalse();
    }

    @Test
    @EnableFlags(android.hardware.biometrics.Flags.FLAG_SCREEN_OFF_UNLOCK_UDFPS)
    public void testScreenOffUnlockVisible() {
        ensureScreenOffUnlockConfigEnabled();
        final Fingerprint fingerprint = new Fingerprint("Test", 0, 0);
        doReturn(List.of(fingerprint)).when(mFingerprintManager).getEnrolledFingerprints(anyInt());
        setUpFragment(false, PRIMARY_USER_ID, TYPE_UDFPS_ULTRASONIC, 5);

        shadowOf(Looper.getMainLooper()).idle();

        final Preference screenOffUnlockPerf =
                mFragment.findPreference(KEY_SCREEN_OFF_FINGERPRINT_UNLOCK);
        assertThat(screenOffUnlockPerf).isNotNull();
        assertThat(screenOffUnlockPerf.isVisible()).isTrue();
    }

    @Test
    @EnableFlags(android.hardware.biometrics.Flags.FLAG_SCREEN_OFF_UNLOCK_UDFPS)
    public void testFingerprintUnlockCategoryVisibility_hasVisibleChild() {
        ensureScreenOffUnlockConfigEnabled();
        final Fingerprint fingerprint = new Fingerprint("Test", 0, 0);
        doReturn(List.of(fingerprint)).when(mFingerprintManager).getEnrolledFingerprints(anyInt());
        setUpFragment(false, PRIMARY_USER_ID, TYPE_UDFPS_OPTICAL, 5);

        shadowOf(Looper.getMainLooper()).idle();

        final Preference screenOffUnlockPerf =
                mFragment.findPreference(KEY_SCREEN_OFF_FINGERPRINT_UNLOCK);
        assertThat(screenOffUnlockPerf).isNotNull();
        assertThat(screenOffUnlockPerf.isVisible()).isTrue();
        final Preference fingerprintUnlockCategoryPerf =
                mFragment.findPreference(KEY_FINGERPRINT_UNLOCK_CATEGORY);
        assertThat(fingerprintUnlockCategoryPerf).isNotNull();
        assertThat(fingerprintUnlockCategoryPerf.isVisible()).isTrue();
    }

    @Test
    public void testFingerprintUnlockCategoryVisibility_noVisibleChild() {
        final Fingerprint fingerprint = new Fingerprint("Test", 0, 0);
        doReturn(List.of(fingerprint)).when(mFingerprintManager).getEnrolledFingerprints(anyInt());
        setUpFragment(false, PRIMARY_USER_ID, TYPE_UDFPS_OPTICAL, 5);

        shadowOf(Looper.getMainLooper()).idle();

        final Preference screenOffUnlockPerf =
                mFragment.findPreference(KEY_SCREEN_OFF_FINGERPRINT_UNLOCK);
        assertThat(screenOffUnlockPerf).isNotNull();
        assertThat(screenOffUnlockPerf.isVisible()).isFalse();
        final Preference fingerprintUnlockCategoryPerf =
                mFragment.findPreference(KEY_FINGERPRINT_UNLOCK_CATEGORY);
        assertThat(fingerprintUnlockCategoryPerf).isNotNull();
        assertThat(fingerprintUnlockCategoryPerf.isVisible()).isFalse();
    }

    private void ensureScreenOffUnlockConfigEnabled() {
        Resources res = spy(mContext.getResources());
        doReturn(true).when(res).getBoolean(
                eq(com.android.internal.R.bool.config_screen_off_udfps_enabled));
        doReturn(res).when(mContext).getResources();
    }

    @Test
    @EnableFlags(android.hardware.biometrics.Flags.FLAG_SCREEN_OFF_UNLOCK_UDFPS)
    public void testHasExtPreferencesEnableScreenOffUnlockUdfpsFlag() {
        testHasExtPreferences();
    }

    @Test
    @DisableFlags(android.hardware.biometrics.Flags.FLAG_SCREEN_OFF_UNLOCK_UDFPS)
    public void testHasExtPreferencesDisableScreenOffUnlockUdfpsFlag() {
        testHasExtPreferences();
    }

    private void testHasExtPreferences() {
        String key0 = "ExtKey0";
        String key1 = "ExtKey1";
        when(mRestrictedPreference0.getKey()).thenReturn(key0);
        when(mRestrictedPreference1.getKey()).thenReturn(key1);
        when(mExtPreferencesProvider.getSize()).thenReturn(2);
        when(mExtPreferencesProvider.newPreference(eq(0),
                any(FingerprintExtPreferencesProvider.PreferenceInflater.class)))
                .thenReturn(mRestrictedPreference0);
        when(mExtPreferencesProvider.newPreference(eq(1),
                any(FingerprintExtPreferencesProvider.PreferenceInflater.class)))
                .thenReturn(mRestrictedPreference1);

        Fingerprint fingerprint = new Fingerprint("Test", 0, 0);
        doReturn(List.of(fingerprint)).when(mFingerprintManager).getEnrolledFingerprints(anyInt());
        setUpFragment(false, PRIMARY_USER_ID, TYPE_UDFPS_OPTICAL, 5);

        shadowOf(Looper.getMainLooper()).idle();

        Preference preference0 = mFragment.findPreference(key0);
        assertThat(preference0).isEqualTo(mRestrictedPreference0);

        Preference preference1 = mFragment.findPreference(key1);
        assertThat(preference1).isEqualTo(mRestrictedPreference1);
    }

    @Test
    @EnableFlags(android.hardware.biometrics.Flags.FLAG_SCREEN_OFF_UNLOCK_UDFPS)
    public void testPrimarySwitchIntentPreferenceNullResultKey_enableScreenOffUnlockUdfpsFlag() {
        testPrimarySwitchIntentPreferenceNullResultKey();
    }

    @Test
    @DisableFlags(android.hardware.biometrics.Flags.FLAG_SCREEN_OFF_UNLOCK_UDFPS)
    public void testPrimarySwitchIntentPreferenceNullResultKey_disableScreenOffUnlockUdfpsFlag() {
        testPrimarySwitchIntentPreferenceNullResultKey();
    }

    private void testPrimarySwitchIntentPreferenceNullResultKey() {
        PrimarySwitchIntentPreference spiedPrimarySwitchIntentPref = spy(
                new PrimarySwitchIntentPreference(mContext) {
                    @Override
                    public String getKey() {
                        return "TEST_KEY";
                    }
                    @Override
                    public String getConfirmDialogFragmentResultKey() {
                        return null;
                    }
                    @NonNull
                    @Override
                    public Intent getLaunchedIntent(@NonNull byte[] token) {
                        return new Intent();
                    }
                });
        when(mExtPreferencesProvider.getSize()).thenReturn(1);
        when(mExtPreferencesProvider.newPreference(eq(0),
                any(FingerprintExtPreferencesProvider.PreferenceInflater.class)))
                .thenReturn(spiedPrimarySwitchIntentPref);

        Fingerprint fingerprint = new Fingerprint("Test", 0, 0);
        doReturn(List.of(fingerprint)).when(mFingerprintManager).getEnrolledFingerprints(anyInt());
        setUpFragment(false, PRIMARY_USER_ID, TYPE_UDFPS_OPTICAL, 5);

        shadowOf(Looper.getMainLooper()).idle();

        // Verify click, and no changeListener for later switching toggle case
        verify(spiedPrimarySwitchIntentPref).setOnPreferenceClickListener(any());
        verify(spiedPrimarySwitchIntentPref, never()).setOnPreferenceChangeListener(any());
    }

    @Test
    @EnableFlags(android.hardware.biometrics.Flags.FLAG_SCREEN_OFF_UNLOCK_UDFPS)
    public void testPrimarySwitchIntentPreferenceEmptyResultKey_enableScreenOffUnlockUdfpsFlag() {
        testPrimarySwitchIntentPreferenceEmptyResultKey();
    }

    @Test
    @DisableFlags(android.hardware.biometrics.Flags.FLAG_SCREEN_OFF_UNLOCK_UDFPS)
    public void testPrimarySwitchIntentPreferenceEmptyResultKey_disableScreenOffUnlockUdfpsFlag() {
        testPrimarySwitchIntentPreferenceEmptyResultKey();
    }

    private void testPrimarySwitchIntentPreferenceEmptyResultKey() {
        PrimarySwitchIntentPreference spiedPrimarySwitchIntentPref = spy(
                new PrimarySwitchIntentPreference(mContext) {
                    @Override
                    public String getKey() {
                        return "TEST_KEY";
                    }
                    @Override
                    public String getConfirmDialogFragmentResultKey() {
                        return "";
                    }
                    @NonNull
                    @Override
                    public Intent getLaunchedIntent(@NonNull byte[] token) {
                        return new Intent();
                    }
                });
        when(mExtPreferencesProvider.getSize()).thenReturn(1);
        when(mExtPreferencesProvider.newPreference(eq(0),
                any(FingerprintExtPreferencesProvider.PreferenceInflater.class)))
                .thenReturn(spiedPrimarySwitchIntentPref);

        Fingerprint fingerprint = new Fingerprint("Test", 0, 0);
        doReturn(List.of(fingerprint)).when(mFingerprintManager).getEnrolledFingerprints(anyInt());
        setUpFragment(false, PRIMARY_USER_ID, TYPE_UDFPS_OPTICAL, 5);

        shadowOf(Looper.getMainLooper()).idle();

        // Verify click, and no changeListener for later switching toggle case
        verify(spiedPrimarySwitchIntentPref).setOnPreferenceClickListener(any());
        verify(spiedPrimarySwitchIntentPref, never()).setOnPreferenceChangeListener(any());
    }

    private void setSensor(@FingerprintSensorProperties.SensorType int sensorType,
            int maxFingerprints) {
        final ArrayList<FingerprintSensorPropertiesInternal> props = new ArrayList<>();
        props.add(new FingerprintSensorPropertiesInternal(
                0 /* sensorId */,
                SensorProperties.STRENGTH_STRONG,
                maxFingerprints /* maxEnrollmentsPerUser */,
                new ArrayList<ComponentInfoInternal>(),
                sensorType,
                true /* resetLockoutRequiresHardwareAuthToken */));
        doReturn(props).when(mFingerprintManager).getSensorPropertiesInternal();
    }
}
