1 /* 2 * Copyright (C) 2018 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.fingerprint; 18 19 import static com.android.settings.core.BasePreferenceController.DISABLED_FOR_USER; 20 import static com.google.common.truth.Truth.assertThat; 21 import static org.mockito.ArgumentMatchers.anyInt; 22 import static org.mockito.Mockito.spy; 23 import static org.mockito.Mockito.when; 24 25 import android.content.Context; 26 import android.content.pm.PackageManager; 27 import android.hardware.fingerprint.FingerprintManager; 28 import android.os.UserManager; 29 30 import com.android.internal.widget.LockPatternUtils; 31 import com.android.settings.testutils.FakeFeatureFactory; 32 import com.android.settings.testutils.SettingsRobolectricTestRunner; 33 34 import org.junit.Before; 35 import org.junit.Test; 36 import org.junit.runner.RunWith; 37 import org.mockito.Mock; 38 import org.mockito.MockitoAnnotations; 39 import org.robolectric.RuntimeEnvironment; 40 import org.robolectric.shadows.ShadowApplication; 41 42 @RunWith(SettingsRobolectricTestRunner.class) 43 public class FingerprintProfileStatusPreferenceControllerTest { 44 45 private static final int FAKE_PROFILE_USER_ID = 1234; 46 47 @Mock 48 private PackageManager mPackageManager; 49 @Mock 50 private LockPatternUtils mLockPatternUtils; 51 @Mock 52 private FingerprintManager mFingerprintManager; 53 @Mock 54 private UserManager mUm; 55 56 private FakeFeatureFactory mFeatureFactory; 57 private Context mContext; 58 private FingerprintProfileStatusPreferenceController mController; 59 60 @Before setUp()61 public void setUp() { 62 MockitoAnnotations.initMocks(this); 63 mContext = spy(RuntimeEnvironment.application); 64 when(mContext.getPackageManager()).thenReturn(mPackageManager); 65 when(mPackageManager.hasSystemFeature(PackageManager.FEATURE_FINGERPRINT)).thenReturn(true); 66 ShadowApplication.getInstance().setSystemService(Context.FINGERPRINT_SERVICE, 67 mFingerprintManager); 68 ShadowApplication.getInstance().setSystemService(Context.USER_SERVICE, mUm); 69 70 mFeatureFactory = FakeFeatureFactory.setupForTest(); 71 when(mFeatureFactory.securityFeatureProvider.getLockPatternUtils(mContext)) 72 .thenReturn(mLockPatternUtils); 73 when(mUm.getProfileIdsWithDisabled(anyInt())).thenReturn(new int[] {1234}); 74 mController = new FingerprintProfileStatusPreferenceController(mContext); 75 } 76 77 @Test getUserId_shouldReturnProfileId()78 public void getUserId_shouldReturnProfileId() { 79 assertThat(mController.getUserId()).isEqualTo(FAKE_PROFILE_USER_ID); 80 } 81 82 @Test isUserSupported_separateChallengeAllowed_true()83 public void isUserSupported_separateChallengeAllowed_true() { 84 when(mLockPatternUtils.isSeparateProfileChallengeAllowed(anyInt())).thenReturn(true); 85 assertThat(mController.isUserSupported()).isTrue(); 86 } 87 88 @Test isUserSupported_separateChallengeNotAllowed_false()89 public void isUserSupported_separateChallengeNotAllowed_false() { 90 when(mLockPatternUtils.isSeparateProfileChallengeAllowed(anyInt())).thenReturn(false); 91 92 assertThat(mController.isUserSupported()).isFalse(); 93 } 94 95 @Test getAvailabilityStatus_userNotSupported_DISABLED()96 public void getAvailabilityStatus_userNotSupported_DISABLED() { 97 when(mFingerprintManager.isHardwareDetected()).thenReturn(true); 98 when(mLockPatternUtils.isSeparateProfileChallengeAllowed(anyInt())).thenReturn(false); 99 100 assertThat(mController.getAvailabilityStatus()).isEqualTo(DISABLED_FOR_USER); 101 } 102 } 103