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.security; 18 19 import static android.app.admin.DevicePolicyManager.PASSWORD_QUALITY_ALPHABETIC; 20 import static android.app.admin.DevicePolicyManager.PASSWORD_QUALITY_SOMETHING; 21 import static android.app.admin.DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED; 22 23 import static com.android.settings.core.BasePreferenceController.AVAILABLE; 24 import static com.android.settings.core.BasePreferenceController.DISABLED_FOR_USER; 25 26 import static com.google.common.truth.Truth.assertThat; 27 28 import static org.mockito.ArgumentMatchers.anyInt; 29 import static org.mockito.Mockito.spy; 30 import static org.mockito.Mockito.when; 31 32 import android.content.Context; 33 import android.content.pm.PackageManager; 34 import android.content.pm.UserInfo; 35 import android.hardware.fingerprint.FingerprintManager; 36 import android.os.UserManager; 37 38 import androidx.lifecycle.LifecycleOwner; 39 40 import com.android.internal.widget.LockPatternUtils; 41 import com.android.settings.testutils.FakeFeatureFactory; 42 import com.android.settingslib.core.lifecycle.Lifecycle; 43 44 import org.junit.Before; 45 import org.junit.Test; 46 import org.junit.runner.RunWith; 47 import org.mockito.Mock; 48 import org.mockito.MockitoAnnotations; 49 import org.robolectric.RobolectricTestRunner; 50 import org.robolectric.RuntimeEnvironment; 51 import org.robolectric.shadows.ShadowApplication; 52 53 import java.util.Arrays; 54 55 @RunWith(RobolectricTestRunner.class) 56 public class VisiblePatternProfilePreferenceControllerTest { 57 58 private static final int FAKE_PROFILE_USER_ID = 1234; 59 60 @Mock 61 private PackageManager mPackageManager; 62 @Mock 63 private LockPatternUtils mLockPatternUtils; 64 @Mock 65 private FingerprintManager mFingerprintManager; 66 @Mock 67 private UserManager mUm; 68 69 private Lifecycle mLifecycle; 70 private LifecycleOwner mLifecycleOwner; 71 private FakeFeatureFactory mFeatureFactory; 72 private Context mContext; 73 private VisiblePatternProfilePreferenceController mController; 74 75 @Before setUp()76 public void setUp() { 77 MockitoAnnotations.initMocks(this); 78 mContext = spy(RuntimeEnvironment.application); 79 when(mContext.getPackageManager()).thenReturn(mPackageManager); 80 when(mPackageManager.hasSystemFeature(PackageManager.FEATURE_FINGERPRINT)).thenReturn(true); 81 final ShadowApplication application = ShadowApplication.getInstance(); 82 application.setSystemService(Context.FINGERPRINT_SERVICE, mFingerprintManager); 83 application.setSystemService(Context.USER_SERVICE, mUm); 84 85 mFeatureFactory = FakeFeatureFactory.setupForTest(); 86 when(mFeatureFactory.securityFeatureProvider.getLockPatternUtils(mContext)) 87 .thenReturn(mLockPatternUtils); 88 when(mUm.getProfiles(anyInt())).thenReturn(Arrays.asList(new UserInfo( 89 FAKE_PROFILE_USER_ID, "", UserInfo.FLAG_MANAGED_PROFILE | UserInfo.FLAG_PROFILE))); 90 91 92 mLifecycleOwner = () -> mLifecycle; 93 mLifecycle = new Lifecycle(mLifecycleOwner); 94 mController = new VisiblePatternProfilePreferenceController(mContext, mLifecycle); 95 } 96 97 @Test getAvailabilityStatus_notSecure_DISABLED()98 public void getAvailabilityStatus_notSecure_DISABLED() { 99 when(mLockPatternUtils.isSecure(FAKE_PROFILE_USER_ID)).thenReturn(false); 100 when(mLockPatternUtils.getKeyguardStoredPasswordQuality(FAKE_PROFILE_USER_ID)) 101 .thenReturn(PASSWORD_QUALITY_UNSPECIFIED); 102 103 assertThat(mController.getAvailabilityStatus()).isEqualTo(DISABLED_FOR_USER); 104 } 105 106 @Test getAvailabilityStatus_secureWithPassword_DISABLED()107 public void getAvailabilityStatus_secureWithPassword_DISABLED() { 108 when(mLockPatternUtils.isSecure(FAKE_PROFILE_USER_ID)).thenReturn(true); 109 when(mLockPatternUtils.getKeyguardStoredPasswordQuality(FAKE_PROFILE_USER_ID)) 110 .thenReturn(PASSWORD_QUALITY_ALPHABETIC); 111 112 assertThat(mController.getAvailabilityStatus()).isEqualTo(DISABLED_FOR_USER); 113 } 114 115 @Test getAvailabilityStatus_secureWithPattern_AVAILABLE()116 public void getAvailabilityStatus_secureWithPattern_AVAILABLE() { 117 when(mLockPatternUtils.isSecure(FAKE_PROFILE_USER_ID)).thenReturn(true); 118 when(mLockPatternUtils.getKeyguardStoredPasswordQuality(FAKE_PROFILE_USER_ID)) 119 .thenReturn(PASSWORD_QUALITY_SOMETHING); 120 121 assertThat(mController.getAvailabilityStatus()).isEqualTo(AVAILABLE); 122 } 123 124 @Test getPreferenceKey_byDefault_returnsDefaultValue()125 public void getPreferenceKey_byDefault_returnsDefaultValue() { 126 assertThat(mController.getPreferenceKey()).isEqualTo("visiblepattern_profile"); 127 } 128 129 @Test getPreferenceKey_whenGivenValue_returnsGivenValue()130 public void getPreferenceKey_whenGivenValue_returnsGivenValue() { 131 mController = new VisiblePatternProfilePreferenceController(mContext, mLifecycle, "key"); 132 133 assertThat(mController.getPreferenceKey()).isEqualTo("key"); 134 } 135 } 136