2 * Copyright (C) 2016 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.settings.password;
19 import static android.content.pm.PackageManager.FEATURE_FINGERPRINT;
20 import static com.android.settings.password.ChooseLockGeneric.ChooseLockGenericFragment
22 import static com.android.settings.password.ChooseLockGeneric.ChooseLockGenericFragment
24 import static com.android.settings.password.ChooseLockSettingsHelper.EXTRA_KEY_CHALLENGE;
25 import static com.android.settings.password.ChooseLockSettingsHelper.EXTRA_KEY_FOR_FINGERPRINT;
26 import static com.android.settings.password.ChooseLockSettingsHelper.EXTRA_KEY_HAS_CHALLENGE;
27 import static com.google.common.truth.Truth.assertThat;
28 import static org.junit.Assert.assertEquals;
29 import static org.junit.Assert.assertTrue;
30 import static org.mockito.Matchers.any;
31 import static org.mockito.Matchers.eq;
32 import static org.mockito.Mockito.verify;
33 import static org.mockito.Mockito.when;
35 import android.app.admin.DevicePolicyManager;
36 import android.content.ComponentName;
37 import android.content.Intent;
38 import android.content.pm.PackageManager;
39 import android.hardware.fingerprint.FingerprintManager;
40 import android.os.Bundle;
42 import com.android.settings.testutils.SettingsRobolectricTestRunner;
44 import org.junit.Before;
45 import org.junit.Test;
46 import org.junit.runner.RunWith;
47 import org.mockito.ArgumentCaptor;
48 import org.mockito.Mock;
49 import org.mockito.MockitoAnnotations;
51 @RunWith(SettingsRobolectricTestRunner.class)
52 public final class SetNewPasswordControllerTest {
54 private static final int CURRENT_USER_ID = 101;
55 private static final long FINGERPRINT_CHALLENGE = -9876512313131L;
58 private PackageManager mPackageManager;
60 private FingerprintManager mFingerprintManager;
62 private DevicePolicyManager mDevicePolicyManager;
64 private SetNewPasswordController.Ui mUi;
66 private SetNewPasswordController mSetNewPasswordController;
70 MockitoAnnotations.initMocks(this);
71 mSetNewPasswordController = new SetNewPasswordController(
72 CURRENT_USER_ID, mPackageManager, mFingerprintManager, mDevicePolicyManager, mUi);
74 when(mFingerprintManager.preEnroll()).thenReturn(FINGERPRINT_CHALLENGE);
75 when(mPackageManager.hasSystemFeature(eq(FEATURE_FINGERPRINT))).thenReturn(true);
79 public void launchChooseLockWithFingerprint() {
80 // GIVEN the device supports fingerprint.
81 when(mFingerprintManager.isHardwareDetected()).thenReturn(true);
82 // GIVEN there are no enrolled fingerprints.
83 when(mFingerprintManager.hasEnrolledFingerprints(CURRENT_USER_ID)).thenReturn(false);
84 // GIVEN DPC does not disallow fingerprint for keyguard usage.
85 when(mDevicePolicyManager.getKeyguardDisabledFeatures(any(ComponentName.class)))
88 // WHEN the controller dispatches a set new password intent.
89 mSetNewPasswordController.dispatchSetNewPasswordIntent();
91 // THEN the choose lock activity is launched with fingerprint extras.
92 ArgumentCaptor<Bundle> bundleArgumentCaptor = ArgumentCaptor.forClass(Bundle.class);
93 verify(mUi).launchChooseLock(bundleArgumentCaptor.capture());
94 // THEN the extras have all values for fingerprint setup.
95 compareFingerprintExtras(bundleArgumentCaptor.getValue());
99 public void launchChooseLockWithoutFingerprint_noFingerprintFeature() {
100 // GIVEN the device does NOT support fingerprint feature.
101 when(mPackageManager.hasSystemFeature(eq(FEATURE_FINGERPRINT))).thenReturn(false);
103 // WHEN the controller dispatches a set new password intent.
104 mSetNewPasswordController.dispatchSetNewPasswordIntent();
106 // THEN the choose lock activity is launched without fingerprint extras.
107 ArgumentCaptor<Bundle> bundleArgumentCaptor = ArgumentCaptor.forClass(Bundle.class);
108 verify(mUi).launchChooseLock(bundleArgumentCaptor.capture());
109 assertBundleContainsUserIdOnly(bundleArgumentCaptor.getValue());
113 public void launchChooseLockWithoutFingerprint_noFingerprintSensor() {
114 // GIVEN the device does NOT support fingerprint.
115 when(mFingerprintManager.isHardwareDetected()).thenReturn(false);
116 // GIVEN there are no enrolled fingerprints.
117 when(mFingerprintManager.hasEnrolledFingerprints(CURRENT_USER_ID)).thenReturn(false);
118 // GIVEN DPC does not disallow fingerprint for keyguard usage.
119 when(mDevicePolicyManager.getKeyguardDisabledFeatures(any(ComponentName.class)))
122 // WHEN the controller dispatches a set new password intent.
123 mSetNewPasswordController.dispatchSetNewPasswordIntent();
125 // THEN the choose lock activity is launched without a bundle contains user id only.
126 ArgumentCaptor<Bundle> bundleArgumentCaptor = ArgumentCaptor.forClass(Bundle.class);
127 verify(mUi).launchChooseLock(bundleArgumentCaptor.capture());
128 assertBundleContainsUserIdOnly(bundleArgumentCaptor.getValue());
132 public void launchChooseLockWithoutFingerprint_hasFingerprintEnrolled() {
133 // GIVEN the device supports fingerprint.
134 when(mFingerprintManager.isHardwareDetected()).thenReturn(true);
135 // GIVEN there are no enrolled fingerprints.
136 when(mFingerprintManager.hasEnrolledFingerprints(CURRENT_USER_ID)).thenReturn(true);
137 // GIVEN DPC does not disallow fingerprint for keyguard usage.
138 when(mDevicePolicyManager.getKeyguardDisabledFeatures(any(ComponentName.class)))
141 // WHEN the controller dispatches a set new password intent.
142 mSetNewPasswordController.dispatchSetNewPasswordIntent();
144 // THEN the choose lock activity is launched without a bundle contains user id only.
145 ArgumentCaptor<Bundle> bundleArgumentCaptor = ArgumentCaptor.forClass(Bundle.class);
146 verify(mUi).launchChooseLock(bundleArgumentCaptor.capture());
147 assertBundleContainsUserIdOnly(bundleArgumentCaptor.getValue());
151 public void launchChooseLockWithoutFingerprint_deviceAdminDisallowFingerprintForKeyguard() {
152 // GIVEN the device supports fingerprint.
153 when(mFingerprintManager.isHardwareDetected()).thenReturn(true);
154 // GIVEN there is an enrolled fingerprint.
155 when(mFingerprintManager.hasEnrolledFingerprints(CURRENT_USER_ID)).thenReturn(true);
156 // GIVEN DPC disallows fingerprint for keyguard usage.
157 when(mDevicePolicyManager.getKeyguardDisabledFeatures(any(ComponentName.class)))
158 .thenReturn(DevicePolicyManager.KEYGUARD_DISABLE_FINGERPRINT);
160 // WHEN the controller dispatches a set new password intent.
161 mSetNewPasswordController.dispatchSetNewPasswordIntent();
163 // THEN the choose lock activity is launched without a bundle contains user id only.
164 ArgumentCaptor<Bundle> bundleArgumentCaptor = ArgumentCaptor.forClass(Bundle.class);
165 verify(mUi).launchChooseLock(bundleArgumentCaptor.capture());
166 assertBundleContainsUserIdOnly(bundleArgumentCaptor.getValue());
169 private void compareFingerprintExtras(Bundle actualBundle) {
171 "Password quality must be something in order to config fingerprint.",
172 DevicePolicyManager.PASSWORD_QUALITY_SOMETHING,
173 actualBundle.getInt(MINIMUM_QUALITY_KEY));
175 "All disabled preference should be removed.",
176 actualBundle.getBoolean(HIDE_DISABLED_PREFS));
178 "There must be a fingerprint challenge.",
179 actualBundle.getBoolean(EXTRA_KEY_HAS_CHALLENGE));
181 "The fingerprint challenge must come from the FingerprintManager",
182 FINGERPRINT_CHALLENGE,
183 actualBundle.getLong(EXTRA_KEY_CHALLENGE));
185 "The request must be a fingerprint set up request.",
186 actualBundle.getBoolean(EXTRA_KEY_FOR_FINGERPRINT));
188 "User id must be equaled to the input one.",
190 actualBundle.getInt(Intent.EXTRA_USER_ID));
193 private void assertBundleContainsUserIdOnly(Bundle actualBundle) {
194 assertThat(actualBundle.size()).isEqualTo(1);
195 assertThat(actualBundle.getInt(Intent.EXTRA_USER_ID)).isEqualTo(CURRENT_USER_ID);