OSDN Git Service

b5572574d6831c178088fac64dc8fa53de0af22c
[android-x86/packages-apps-Settings.git] / tests / robotests / src / com / android / settings / password / SetNewPasswordControllerTest.java
1 /*
2  * Copyright (C) 2016 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.password;
18
19 import static android.content.pm.PackageManager.FEATURE_FINGERPRINT;
20 import static com.android.settings.password.ChooseLockGeneric.ChooseLockGenericFragment
21         .HIDE_DISABLED_PREFS;
22 import static com.android.settings.password.ChooseLockGeneric.ChooseLockGenericFragment
23         .MINIMUM_QUALITY_KEY;
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;
34
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;
41
42 import com.android.settings.testutils.SettingsRobolectricTestRunner;
43
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;
50
51 @RunWith(SettingsRobolectricTestRunner.class)
52 public final class SetNewPasswordControllerTest {
53
54     private static final int CURRENT_USER_ID = 101;
55     private static final long FINGERPRINT_CHALLENGE = -9876512313131L;
56
57     @Mock
58     private PackageManager mPackageManager;
59     @Mock
60     private FingerprintManager mFingerprintManager;
61     @Mock
62     private DevicePolicyManager mDevicePolicyManager;
63     @Mock
64     private SetNewPasswordController.Ui mUi;
65
66     private SetNewPasswordController mSetNewPasswordController;
67
68     @Before
69     public void setUp() {
70         MockitoAnnotations.initMocks(this);
71         mSetNewPasswordController = new SetNewPasswordController(
72                 CURRENT_USER_ID, mPackageManager, mFingerprintManager, mDevicePolicyManager, mUi);
73
74         when(mFingerprintManager.preEnroll()).thenReturn(FINGERPRINT_CHALLENGE);
75         when(mPackageManager.hasSystemFeature(eq(FEATURE_FINGERPRINT))).thenReturn(true);
76     }
77
78     @Test
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)))
86                 .thenReturn(0);
87
88         // WHEN the controller dispatches a set new password intent.
89         mSetNewPasswordController.dispatchSetNewPasswordIntent();
90
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());
96     }
97
98     @Test
99     public void launchChooseLockWithoutFingerprint_noFingerprintFeature() {
100         // GIVEN the device does NOT support fingerprint feature.
101         when(mPackageManager.hasSystemFeature(eq(FEATURE_FINGERPRINT))).thenReturn(false);
102
103         // WHEN the controller dispatches a set new password intent.
104         mSetNewPasswordController.dispatchSetNewPasswordIntent();
105
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());
110     }
111
112     @Test
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)))
120                 .thenReturn(0);
121
122         // WHEN the controller dispatches a set new password intent.
123         mSetNewPasswordController.dispatchSetNewPasswordIntent();
124
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());
129     }
130
131     @Test
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)))
139                 .thenReturn(0);
140
141         // WHEN the controller dispatches a set new password intent.
142         mSetNewPasswordController.dispatchSetNewPasswordIntent();
143
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());
148     }
149
150     @Test
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);
159
160         // WHEN the controller dispatches a set new password intent.
161         mSetNewPasswordController.dispatchSetNewPasswordIntent();
162
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());
167     }
168
169     private void compareFingerprintExtras(Bundle actualBundle) {
170         assertEquals(
171                 "Password quality must be something in order to config fingerprint.",
172                 DevicePolicyManager.PASSWORD_QUALITY_SOMETHING,
173                 actualBundle.getInt(MINIMUM_QUALITY_KEY));
174         assertTrue(
175                 "All disabled preference should be removed.",
176                 actualBundle.getBoolean(HIDE_DISABLED_PREFS));
177         assertTrue(
178                 "There must be a fingerprint challenge.",
179                 actualBundle.getBoolean(EXTRA_KEY_HAS_CHALLENGE));
180         assertEquals(
181                 "The fingerprint challenge must come from the FingerprintManager",
182                 FINGERPRINT_CHALLENGE,
183                 actualBundle.getLong(EXTRA_KEY_CHALLENGE));
184         assertTrue(
185                 "The request must be a fingerprint set up request.",
186                 actualBundle.getBoolean(EXTRA_KEY_FOR_FINGERPRINT));
187         assertEquals(
188                 "User id must be equaled to the input one.",
189                 CURRENT_USER_ID,
190                 actualBundle.getInt(Intent.EXTRA_USER_ID));
191     }
192
193     private void assertBundleContainsUserIdOnly(Bundle actualBundle) {
194         assertThat(actualBundle.size()).isEqualTo(1);
195         assertThat(actualBundle.getInt(Intent.EXTRA_USER_ID)).isEqualTo(CURRENT_USER_ID);
196     }
197 }