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.settings.privatespace; 18 19 import static com.google.common.truth.Truth.assertThat; 20 21 import static org.mockito.ArgumentMatchers.anyInt; 22 import static org.mockito.Mockito.doReturn; 23 import static org.mockito.Mockito.when; 24 25 import android.content.Context; 26 import android.platform.test.flag.junit.SetFlagsRule; 27 28 import androidx.preference.Preference; 29 import androidx.test.core.app.ApplicationProvider; 30 import androidx.test.ext.junit.runners.AndroidJUnit4; 31 32 import com.android.internal.widget.LockPatternUtils; 33 import com.android.settings.privatespace.onelock.FaceFingerprintUnlockController; 34 import com.android.settings.testutils.FakeFeatureFactory; 35 import com.android.settingslib.core.lifecycle.Lifecycle; 36 37 import org.junit.After; 38 import org.junit.Before; 39 import org.junit.Rule; 40 import org.junit.Test; 41 import org.junit.runner.RunWith; 42 import org.mockito.Mock; 43 import org.mockito.MockitoAnnotations; 44 45 @RunWith(AndroidJUnit4.class) 46 public class FaceFingerprintUnlockControllerTest { 47 @Mock private Context mContext; 48 @Rule public final SetFlagsRule mSetFlagsRule = new SetFlagsRule(); 49 50 @Mock Lifecycle mLifecycle; 51 @Mock LockPatternUtils mLockPatternUtils; 52 53 private Preference mPreference; 54 private PrivateSpaceMaintainer mPrivateSpaceMaintainer; 55 private FaceFingerprintUnlockController mFaceFingerprintUnlockController; 56 57 /** Required setup before a test. */ 58 @Before setUp()59 public void setUp() { 60 MockitoAnnotations.initMocks(this); 61 mContext = ApplicationProvider.getApplicationContext(); 62 final String preferenceKey = "private_space_biometrics"; 63 64 mPreference = new Preference(ApplicationProvider.getApplicationContext()); 65 mPreference.setKey(preferenceKey); 66 67 final FakeFeatureFactory featureFactory = FakeFeatureFactory.setupForTest(); 68 when(featureFactory.securityFeatureProvider.getLockPatternUtils(mContext)) 69 .thenReturn(mLockPatternUtils); 70 doReturn(true).when(mLockPatternUtils).isSecure(anyInt()); 71 72 73 mPrivateSpaceMaintainer = PrivateSpaceMaintainer.getInstance(mContext); 74 assertThat(mPrivateSpaceMaintainer.createPrivateSpace()).isTrue(); 75 assertThat(mPrivateSpaceMaintainer.doesPrivateSpaceExist()).isTrue(); 76 77 mFaceFingerprintUnlockController = 78 new FaceFingerprintUnlockController(mContext, mLifecycle); 79 } 80 81 @After tearDown()82 public void tearDown() { 83 // Ensure PSMaintainer is able to remove PS. 84 mSetFlagsRule.enableFlags( 85 android.os.Flags.FLAG_ALLOW_PRIVATE_PROFILE, 86 android.multiuser.Flags.FLAG_ENABLE_PRIVATE_SPACE_FEATURES); 87 assertThat(mPrivateSpaceMaintainer.deletePrivateSpace()) 88 .isEqualTo(PrivateSpaceMaintainer.ErrorDeletingPrivateSpace.DELETE_PS_ERROR_NONE); 89 } 90 91 /** Tests that the controller is always available. */ 92 @Test getAvailabilityStatus_whenFlagsEnabled_returnsAvailable()93 public void getAvailabilityStatus_whenFlagsEnabled_returnsAvailable() { 94 mSetFlagsRule.enableFlags( 95 android.os.Flags.FLAG_ALLOW_PRIVATE_PROFILE, 96 android.multiuser.Flags.FLAG_ENABLE_BIOMETRICS_TO_UNLOCK_PRIVATE_SPACE, 97 android.multiuser.Flags.FLAG_ENABLE_PRIVATE_SPACE_FEATURES); 98 99 assertThat(mFaceFingerprintUnlockController.isAvailable()).isEqualTo(true); 100 } 101 102 /** Tests that the controller is not available when Biometrics flag is not enabled. */ 103 @Test getAvailabilityStatus_whenBiometricFlagDisabled_returnsFalse()104 public void getAvailabilityStatus_whenBiometricFlagDisabled_returnsFalse() { 105 mSetFlagsRule.enableFlags(android.os.Flags.FLAG_ALLOW_PRIVATE_PROFILE, 106 android.multiuser.Flags.FLAG_ENABLE_PRIVATE_SPACE_FEATURES); 107 mSetFlagsRule.disableFlags( 108 android.multiuser.Flags.FLAG_ENABLE_BIOMETRICS_TO_UNLOCK_PRIVATE_SPACE); 109 110 assertThat(mFaceFingerprintUnlockController.isAvailable()).isEqualTo(false); 111 } 112 113 /** 114 * Tests that the controller is not available when the main private space flag is not 115 * enabled. 116 */ 117 @Test getAvailabilityStatus_whenPsMainFlagDisabled_returnsFalse()118 public void getAvailabilityStatus_whenPsMainFlagDisabled_returnsFalse() { 119 mSetFlagsRule.disableFlags(android.os.Flags.FLAG_ALLOW_PRIVATE_PROFILE); 120 mSetFlagsRule.enableFlags( 121 android.multiuser.Flags.FLAG_ENABLE_BIOMETRICS_TO_UNLOCK_PRIVATE_SPACE, 122 android.multiuser.Flags.FLAG_ENABLE_PRIVATE_SPACE_FEATURES); 123 124 assertThat(mFaceFingerprintUnlockController.isAvailable()).isEqualTo(false); 125 } 126 127 /** Tests that the controller is not available when private features flag is not enabled. */ 128 @Test getAvailabilityStatus_whenPsFeaturesFlagDisabled_returnsFalse()129 public void getAvailabilityStatus_whenPsFeaturesFlagDisabled_returnsFalse() { 130 mSetFlagsRule.disableFlags(android.multiuser.Flags.FLAG_ENABLE_PRIVATE_SPACE_FEATURES); 131 mSetFlagsRule.enableFlags(android.os.Flags.FLAG_ALLOW_PRIVATE_PROFILE, 132 android.multiuser.Flags.FLAG_ENABLE_BIOMETRICS_TO_UNLOCK_PRIVATE_SPACE); 133 134 assertThat(mFaceFingerprintUnlockController.isAvailable()).isEqualTo(false); 135 } 136 137 /** Tests that preference is disabled and summary says same as device lock. */ 138 @Test getSummary_whenScreenLock()139 public void getSummary_whenScreenLock() { 140 doReturn(false).when(mLockPatternUtils).isSeparateProfileChallengeEnabled(anyInt()); 141 mSetFlagsRule.enableFlags( 142 android.os.Flags.FLAG_ALLOW_PRIVATE_PROFILE, 143 android.multiuser.Flags.FLAG_ENABLE_BIOMETRICS_TO_UNLOCK_PRIVATE_SPACE, 144 android.multiuser.Flags.FLAG_ENABLE_PRIVATE_SPACE_FEATURES); 145 146 mFaceFingerprintUnlockController.updateState(mPreference); 147 assertThat(mPreference.isEnabled()).isFalse(); 148 assertThat(mPreference.getSummary().toString()).isEqualTo("Same as device screen lock"); 149 } 150 151 /** Tests that preference is enabled and summary is not same as device lock. */ 152 @Test getSummary_whenSeparateProfileLock()153 public void getSummary_whenSeparateProfileLock() { 154 doReturn(true).when(mLockPatternUtils).isSeparateProfileChallengeEnabled(anyInt()); 155 mSetFlagsRule.enableFlags( 156 android.os.Flags.FLAG_ALLOW_PRIVATE_PROFILE, 157 android.multiuser.Flags.FLAG_ENABLE_BIOMETRICS_TO_UNLOCK_PRIVATE_SPACE, 158 android.multiuser.Flags.FLAG_ENABLE_PRIVATE_SPACE_FEATURES); 159 160 mFaceFingerprintUnlockController.updateState(mPreference); 161 assertThat(mPreference.isEnabled()).isTrue(); 162 assertThat(mPreference.getSummary().toString()).isNotEqualTo("Same as device screen lock"); 163 } 164 } 165