blob: a3cb6c05ca7a1ead04d72f1d8fa3e5bd7900202e [file] [log] [blame]
Lucas Dupin5e0f0d22018-02-26 13:32:16 -08001/*
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
17package com.android.keyguard;
18
Malcolm Chen5c63b512019-08-13 13:24:07 -070019import static android.telephony.SubscriptionManager.DATA_ROAMING_DISABLE;
20import static android.telephony.SubscriptionManager.NAME_SOURCE_DEFAULT_SOURCE;
21
Bill Linef81cbd2018-07-05 17:48:49 +080022import static com.google.common.truth.Truth.assertThat;
23
Lucas Dupin3d053532019-01-29 12:35:22 -080024import static org.mockito.ArgumentMatchers.any;
Malcolm Chen5c63b512019-08-13 13:24:07 -070025import static org.mockito.ArgumentMatchers.anyBoolean;
Lucas Dupin3d053532019-01-29 12:35:22 -080026import static org.mockito.ArgumentMatchers.anyInt;
27import static org.mockito.ArgumentMatchers.anyString;
Lucas Dupin8eec2682019-07-01 16:41:17 -070028import static org.mockito.Mockito.clearInvocations;
Lucas Dupin3d053532019-01-29 12:35:22 -080029import static org.mockito.Mockito.doAnswer;
30import static org.mockito.Mockito.never;
Lucas Dupin3d053532019-01-29 12:35:22 -080031import static org.mockito.Mockito.spy;
32import static org.mockito.Mockito.verify;
33import static org.mockito.Mockito.when;
Bill Linef81cbd2018-07-05 17:48:49 +080034
Lucas Dupin3d053532019-01-29 12:35:22 -080035import android.app.admin.DevicePolicyManager;
36import android.app.trust.TrustManager;
Lucas Dupin7ff82b02018-05-16 12:14:10 -070037import android.content.Context;
Lucas Dupin5e0f0d22018-02-26 13:32:16 -080038import android.content.Intent;
Lucas Dupin3d053532019-01-29 12:35:22 -080039import android.content.pm.PackageManager;
40import android.hardware.biometrics.BiometricManager;
41import android.hardware.biometrics.BiometricSourceType;
42import android.hardware.biometrics.IBiometricEnabledOnKeyguardCallback;
43import android.hardware.face.FaceManager;
44import android.hardware.fingerprint.FingerprintManager;
Bill Linef81cbd2018-07-05 17:48:49 +080045import android.os.Bundle;
Lucas Dupin3d053532019-01-29 12:35:22 -080046import android.os.UserManager;
Bill Linef81cbd2018-07-05 17:48:49 +080047import android.telephony.ServiceState;
Malcolm Chen5c63b512019-08-13 13:24:07 -070048import android.telephony.SubscriptionInfo;
Bill Linef81cbd2018-07-05 17:48:49 +080049import android.telephony.SubscriptionManager;
Lucas Dupin5e0f0d22018-02-26 13:32:16 -080050import android.test.suitebuilder.annotation.SmallTest;
51import android.testing.AndroidTestingRunner;
Lucas Dupin3d053532019-01-29 12:35:22 -080052import android.testing.TestableContext;
Lucas Dupin5e0f0d22018-02-26 13:32:16 -080053import android.testing.TestableLooper;
54import android.testing.TestableLooper.RunWithLooper;
55
56import com.android.internal.telephony.IccCardConstants;
Bill Linef81cbd2018-07-05 17:48:49 +080057import com.android.internal.telephony.PhoneConstants;
Lucas Dupin5e0f0d22018-02-26 13:32:16 -080058import com.android.internal.telephony.TelephonyIntents;
59import com.android.systemui.SysuiTestCase;
Lucas Dupin4befb742019-07-01 11:31:31 -070060import com.android.systemui.statusbar.phone.KeyguardBypassController;
Lucas Dupin5e0f0d22018-02-26 13:32:16 -080061
62import org.junit.Assert;
63import org.junit.Before;
64import org.junit.Test;
65import org.junit.runner.RunWith;
Lucas Dupin3d053532019-01-29 12:35:22 -080066import org.mockito.Mock;
67import org.mockito.MockitoAnnotations;
Lucas Dupin5e0f0d22018-02-26 13:32:16 -080068
Malcolm Chen5c63b512019-08-13 13:24:07 -070069import java.util.ArrayList;
70import java.util.List;
Lucas Dupin5e0f0d22018-02-26 13:32:16 -080071import java.util.concurrent.atomic.AtomicBoolean;
72
73@SmallTest
74@RunWith(AndroidTestingRunner.class)
Lucas Dupin7ff82b02018-05-16 12:14:10 -070075// We must run on the main looper because KeyguardUpdateMonitor#mHandler is initialized with
76// new Handler(Looper.getMainLooper()).
77//
78// Using the main looper should be avoided whenever possible, please don't copy this over to
79// new tests.
80@RunWithLooper(setAsMainLooper = true)
Lucas Dupin5e0f0d22018-02-26 13:32:16 -080081public class KeyguardUpdateMonitorTest extends SysuiTestCase {
Malcolm Chen5c63b512019-08-13 13:24:07 -070082 private static final String TEST_CARRIER = "TEST_CARRIER";
83 private static final String TEST_CARRIER_2 = "TEST_CARRIER_2";
84 private static final int TEST_CARRIER_ID = 1;
85 private static final String TEST_GROUP_UUID = "59b5c870-fc4c-47a4-a99e-9db826b48b24";
86 private static final SubscriptionInfo TEST_SUBSCRIPTION = new SubscriptionInfo(1, "", 0,
87 TEST_CARRIER, TEST_CARRIER, NAME_SOURCE_DEFAULT_SOURCE, 0xFFFFFF, "",
88 DATA_ROAMING_DISABLE, null, null, null, null, false, null, "", false, TEST_GROUP_UUID,
89 TEST_CARRIER_ID, 0);
90 private static final SubscriptionInfo TEST_SUBSCRIPTION_2 = new SubscriptionInfo(2, "", 0,
91 TEST_CARRIER, TEST_CARRIER_2, NAME_SOURCE_DEFAULT_SOURCE, 0xFFFFFF, "",
92 DATA_ROAMING_DISABLE, null, null, null, null, false, null, "", true, TEST_GROUP_UUID,
93 TEST_CARRIER_ID, 0);
Lucas Dupin3d053532019-01-29 12:35:22 -080094 @Mock
95 private KeyguardUpdateMonitor.StrongAuthTracker mStrongAuthTracker;
96 @Mock
97 private TrustManager mTrustManager;
98 @Mock
99 private FingerprintManager mFingerprintManager;
100 @Mock
101 private FaceManager mFaceManager;
102 @Mock
103 private BiometricManager mBiometricManager;
104 @Mock
105 private PackageManager mPackageManager;
106 @Mock
107 private UserManager mUserManager;
108 @Mock
109 private DevicePolicyManager mDevicePolicyManager;
Lucas Dupin4befb742019-07-01 11:31:31 -0700110 @Mock
111 private KeyguardBypassController mKeyguardBypassController;
Malcolm Chen5c63b512019-08-13 13:24:07 -0700112 @Mock
113 private SubscriptionManager mSubscriptionManager;
Lucas Dupin5e0f0d22018-02-26 13:32:16 -0800114 private TestableLooper mTestableLooper;
Lucas Dupin3d053532019-01-29 12:35:22 -0800115 private TestableKeyguardUpdateMonitor mKeyguardUpdateMonitor;
Lucas Dupin5e0f0d22018-02-26 13:32:16 -0800116
117 @Before
118 public void setup() {
Lucas Dupin3d053532019-01-29 12:35:22 -0800119 MockitoAnnotations.initMocks(this);
120 TestableContext context = spy(mContext);
121 when(mPackageManager.hasSystemFeature(anyString())).thenReturn(true);
122 when(context.getPackageManager()).thenReturn(mPackageManager);
123 doAnswer(invocation -> {
124 IBiometricEnabledOnKeyguardCallback callback = invocation.getArgument(0);
Lucas Dupin7d95f152019-07-17 16:25:54 -0700125 callback.onChanged(BiometricSourceType.FACE, true /* enabled */,
126 KeyguardUpdateMonitor.getCurrentUser());
Lucas Dupin3d053532019-01-29 12:35:22 -0800127 return null;
128 }).when(mBiometricManager).registerEnabledOnKeyguardCallback(any());
129 when(mFaceManager.isHardwareDetected()).thenReturn(true);
130 when(mFaceManager.hasEnrolledTemplates()).thenReturn(true);
131 when(mFaceManager.hasEnrolledTemplates(anyInt())).thenReturn(true);
132 when(mUserManager.isUserUnlocked(anyInt())).thenReturn(true);
Kevin Chynfdfd2d32019-03-01 14:52:15 -0800133 when(mUserManager.isPrimaryUser()).thenReturn(true);
Lucas Dupin3d053532019-01-29 12:35:22 -0800134 when(mStrongAuthTracker.isUnlockingWithBiometricAllowed()).thenReturn(true);
135 context.addMockSystemService(TrustManager.class, mTrustManager);
136 context.addMockSystemService(FingerprintManager.class, mFingerprintManager);
137 context.addMockSystemService(BiometricManager.class, mBiometricManager);
138 context.addMockSystemService(FaceManager.class, mFaceManager);
139 context.addMockSystemService(UserManager.class, mUserManager);
140 context.addMockSystemService(DevicePolicyManager.class, mDevicePolicyManager);
Malcolm Chen5c63b512019-08-13 13:24:07 -0700141 context.addMockSystemService(SubscriptionManager.class, mSubscriptionManager);
Lucas Dupin3d053532019-01-29 12:35:22 -0800142
Lucas Dupin5e0f0d22018-02-26 13:32:16 -0800143 mTestableLooper = TestableLooper.get(this);
Lucas Dupin3d053532019-01-29 12:35:22 -0800144 mKeyguardUpdateMonitor = new TestableKeyguardUpdateMonitor(context);
Lucas Dupin5e0f0d22018-02-26 13:32:16 -0800145 }
146
147 @Test
148 public void testIgnoresSimStateCallback_rebroadcast() {
149 Intent intent = new Intent(TelephonyIntents.ACTION_SIM_STATE_CHANGED);
150
Lucas Dupin3d053532019-01-29 12:35:22 -0800151 mKeyguardUpdateMonitor.mBroadcastReceiver.onReceive(getContext(), intent);
Lucas Dupin5e0f0d22018-02-26 13:32:16 -0800152 mTestableLooper.processAllMessages();
Lucas Dupin7ff82b02018-05-16 12:14:10 -0700153 Assert.assertTrue("onSimStateChanged not called",
Lucas Dupin3d053532019-01-29 12:35:22 -0800154 mKeyguardUpdateMonitor.hasSimStateJustChanged());
Lucas Dupin5e0f0d22018-02-26 13:32:16 -0800155
156 intent.putExtra(TelephonyIntents.EXTRA_REBROADCAST_ON_UNLOCK, true);
Lucas Dupin3d053532019-01-29 12:35:22 -0800157 mKeyguardUpdateMonitor.mBroadcastReceiver.onReceive(getContext(), intent);
Lucas Dupin5e0f0d22018-02-26 13:32:16 -0800158 mTestableLooper.processAllMessages();
Lucas Dupin7ff82b02018-05-16 12:14:10 -0700159 Assert.assertFalse("onSimStateChanged should have been skipped",
Lucas Dupin3d053532019-01-29 12:35:22 -0800160 mKeyguardUpdateMonitor.hasSimStateJustChanged());
Lucas Dupin7ff82b02018-05-16 12:14:10 -0700161 }
162
Bill Linef81cbd2018-07-05 17:48:49 +0800163 @Test
164 public void testTelephonyCapable_BootInitState() {
Lucas Dupin3d053532019-01-29 12:35:22 -0800165 assertThat(mKeyguardUpdateMonitor.mTelephonyCapable).isFalse();
Bill Linef81cbd2018-07-05 17:48:49 +0800166 }
167
168 @Test
169 public void testTelephonyCapable_SimState_Absent() {
170 Intent intent = new Intent(TelephonyIntents.ACTION_SIM_STATE_CHANGED);
andychou695a7602019-05-16 23:14:00 +0800171 intent.putExtra(IccCardConstants.INTENT_KEY_ICC_STATE,
172 IccCardConstants.INTENT_VALUE_ICC_ABSENT);
173 mKeyguardUpdateMonitor.mBroadcastReceiver.onReceive(getContext(),
174 putPhoneInfo(intent, null, false));
175 mTestableLooper.processAllMessages();
176 assertThat(mKeyguardUpdateMonitor.mTelephonyCapable).isTrue();
177 }
178
179 @Test
180 public void testTelephonyCapable_SimState_CardIOError() {
181 Intent intent = new Intent(TelephonyIntents.ACTION_SIM_STATE_CHANGED);
182 intent.putExtra(IccCardConstants.INTENT_KEY_ICC_STATE,
183 IccCardConstants.INTENT_VALUE_ICC_CARD_IO_ERROR);
184 mKeyguardUpdateMonitor.mBroadcastReceiver.onReceive(getContext(),
185 putPhoneInfo(intent, null, false));
Bill Linef81cbd2018-07-05 17:48:49 +0800186 mTestableLooper.processAllMessages();
Lucas Dupin3d053532019-01-29 12:35:22 -0800187 assertThat(mKeyguardUpdateMonitor.mTelephonyCapable).isTrue();
Bill Linef81cbd2018-07-05 17:48:49 +0800188 }
189
190 @Test
191 public void testTelephonyCapable_SimInvalid_ServiceState_InService() {
192 // SERVICE_STATE - IN_SERVICE, but SIM_STATE is invalid TelephonyCapable should be False
Bill Linef81cbd2018-07-05 17:48:49 +0800193 Intent intent = new Intent(TelephonyIntents.ACTION_SERVICE_STATE_CHANGED);
194 Bundle data = new Bundle();
195 ServiceState state = new ServiceState();
196 state.setState(ServiceState.STATE_IN_SERVICE);
197 state.fillInNotifierBundle(data);
Lucas Dupin3d053532019-01-29 12:35:22 -0800198 mKeyguardUpdateMonitor.mBroadcastReceiver.onReceive(getContext()
Bill Linef81cbd2018-07-05 17:48:49 +0800199 , putPhoneInfo(intent, data, false));
200 mTestableLooper.processAllMessages();
Lucas Dupin3d053532019-01-29 12:35:22 -0800201 assertThat(mKeyguardUpdateMonitor.mTelephonyCapable).isFalse();
Bill Linef81cbd2018-07-05 17:48:49 +0800202 }
203
204 @Test
205 public void testTelephonyCapable_SimValid_ServiceState_PowerOff() {
206 // Simulate AirplaneMode case, SERVICE_STATE - POWER_OFF, check TelephonyCapable False
207 // Only receive ServiceState callback IN_SERVICE -> OUT_OF_SERVICE -> POWER_OFF
Bill Linef81cbd2018-07-05 17:48:49 +0800208 Intent intent = new Intent(TelephonyIntents.ACTION_SERVICE_STATE_CHANGED);
209 intent.putExtra(IccCardConstants.INTENT_KEY_ICC_STATE
210 , IccCardConstants.INTENT_VALUE_ICC_LOADED);
211 Bundle data = new Bundle();
212 ServiceState state = new ServiceState();
213 state.setState(ServiceState.STATE_POWER_OFF);
214 state.fillInNotifierBundle(data);
Lucas Dupin3d053532019-01-29 12:35:22 -0800215 mKeyguardUpdateMonitor.mBroadcastReceiver.onReceive(getContext()
Bill Linef81cbd2018-07-05 17:48:49 +0800216 , putPhoneInfo(intent, data, true));
217 mTestableLooper.processAllMessages();
Lucas Dupin3d053532019-01-29 12:35:22 -0800218 assertThat(mKeyguardUpdateMonitor.mTelephonyCapable).isTrue();
Bill Linef81cbd2018-07-05 17:48:49 +0800219 }
220
221 /* Normal SIM inserted flow
222 * ServiceState: ---OutOfServie----->PowerOff->OutOfServie--->InService
223 * SimState: ----NOT_READY---->READY----------------------LOADED>>>
224 * Subscription: --------null---->null--->"Chunghwa Telecom"-------->>>
225 * System: -------------------------------BOOT_COMPLETED------>>>
226 * TelephonyCapable:(F)-(F)-(F)-(F)-(F)-(F)-(F)-(F)-(F)-(F)------(T)-(T)>>
227 */
228 @Test
229 public void testTelephonyCapable_BootInitState_ServiceState_OutOfService() {
Bill Linef81cbd2018-07-05 17:48:49 +0800230 Intent intent = new Intent(TelephonyIntents.ACTION_SERVICE_STATE_CHANGED);
231 Bundle data = new Bundle();
232 ServiceState state = new ServiceState();
233 state.setState(ServiceState.STATE_OUT_OF_SERVICE);
234 state.fillInNotifierBundle(data);
235 intent.putExtras(data);
Lucas Dupin3d053532019-01-29 12:35:22 -0800236 mKeyguardUpdateMonitor.mBroadcastReceiver.onReceive(getContext()
Bill Linef81cbd2018-07-05 17:48:49 +0800237 , putPhoneInfo(intent, data, false));
238 mTestableLooper.processAllMessages();
Lucas Dupin3d053532019-01-29 12:35:22 -0800239 assertThat(mKeyguardUpdateMonitor.mTelephonyCapable).isFalse();
Bill Linef81cbd2018-07-05 17:48:49 +0800240 }
241
242 @Test
243 public void testTelephonyCapable_BootInitState_SimState_NotReady() {
Bill Linef81cbd2018-07-05 17:48:49 +0800244 Bundle data = new Bundle();
245 ServiceState state = new ServiceState();
246 state.setState(ServiceState.STATE_OUT_OF_SERVICE);
247 state.fillInNotifierBundle(data);
248 Intent intent = new Intent(TelephonyIntents.ACTION_SIM_STATE_CHANGED);
249 intent.putExtra(IccCardConstants.INTENT_KEY_ICC_STATE
250 , IccCardConstants.INTENT_VALUE_ICC_NOT_READY);
Lucas Dupin3d053532019-01-29 12:35:22 -0800251 mKeyguardUpdateMonitor.mBroadcastReceiver.onReceive(getContext()
Bill Linef81cbd2018-07-05 17:48:49 +0800252 , putPhoneInfo(intent, data, false));
253 mTestableLooper.processAllMessages();
Lucas Dupin3d053532019-01-29 12:35:22 -0800254 assertThat(mKeyguardUpdateMonitor.mTelephonyCapable).isFalse();
Bill Linef81cbd2018-07-05 17:48:49 +0800255 }
256
257 @Test
258 public void testTelephonyCapable_BootInitState_SimState_Ready() {
Bill Linef81cbd2018-07-05 17:48:49 +0800259 Bundle data = new Bundle();
260 ServiceState state = new ServiceState();
261 state.setState(ServiceState.STATE_OUT_OF_SERVICE);
262 state.fillInNotifierBundle(data);
263 Intent intent = new Intent(TelephonyIntents.ACTION_SIM_STATE_CHANGED);
264 intent.putExtra(IccCardConstants.INTENT_KEY_ICC_STATE
265 , IccCardConstants.INTENT_VALUE_ICC_READY);
Lucas Dupin3d053532019-01-29 12:35:22 -0800266 mKeyguardUpdateMonitor.mBroadcastReceiver.onReceive(getContext()
Bill Linef81cbd2018-07-05 17:48:49 +0800267 , putPhoneInfo(intent, data, false));
268 mTestableLooper.processAllMessages();
Lucas Dupin3d053532019-01-29 12:35:22 -0800269 assertThat(mKeyguardUpdateMonitor.mTelephonyCapable).isFalse();
Bill Linef81cbd2018-07-05 17:48:49 +0800270 }
271
272 @Test
273 public void testTelephonyCapable_BootInitState_ServiceState_PowerOff() {
Bill Linef81cbd2018-07-05 17:48:49 +0800274 Intent intent = new Intent(TelephonyIntents.ACTION_SERVICE_STATE_CHANGED);
275 Bundle data = new Bundle();
276 ServiceState state = new ServiceState();
277 state.setState(ServiceState.STATE_POWER_OFF);
278 state.fillInNotifierBundle(data);
Lucas Dupin3d053532019-01-29 12:35:22 -0800279 mKeyguardUpdateMonitor.mBroadcastReceiver.onReceive(getContext()
Bill Linef81cbd2018-07-05 17:48:49 +0800280 , putPhoneInfo(intent, data, false));
281 mTestableLooper.processAllMessages();
Lucas Dupin3d053532019-01-29 12:35:22 -0800282 assertThat(mKeyguardUpdateMonitor.mTelephonyCapable).isFalse();
Bill Linef81cbd2018-07-05 17:48:49 +0800283 }
284
285 @Test
286 public void testTelephonyCapable_SimValid_ServiceState_InService() {
Bill Linef81cbd2018-07-05 17:48:49 +0800287 Intent intent = new Intent(TelephonyIntents.ACTION_SERVICE_STATE_CHANGED);
288 Bundle data = new Bundle();
289 ServiceState state = new ServiceState();
290 state.setState(ServiceState.STATE_IN_SERVICE);
291 state.fillInNotifierBundle(data);
Lucas Dupin3d053532019-01-29 12:35:22 -0800292 mKeyguardUpdateMonitor.mBroadcastReceiver.onReceive(getContext()
Bill Linef81cbd2018-07-05 17:48:49 +0800293 , putPhoneInfo(intent, data, true));
294 mTestableLooper.processAllMessages();
Lucas Dupin3d053532019-01-29 12:35:22 -0800295 assertThat(mKeyguardUpdateMonitor.mTelephonyCapable).isTrue();
Bill Linef81cbd2018-07-05 17:48:49 +0800296 }
297
298 @Test
299 public void testTelephonyCapable_SimValid_SimState_Loaded() {
Bill Linef81cbd2018-07-05 17:48:49 +0800300 Bundle data = new Bundle();
301 ServiceState state = new ServiceState();
302 state.setState(ServiceState.STATE_IN_SERVICE);
303 state.fillInNotifierBundle(data);
304 Intent intentSimState = new Intent(TelephonyIntents.ACTION_SIM_STATE_CHANGED);
305 intentSimState.putExtra(IccCardConstants.INTENT_KEY_ICC_STATE
306 , IccCardConstants.INTENT_VALUE_ICC_LOADED);
Lucas Dupin3d053532019-01-29 12:35:22 -0800307 mKeyguardUpdateMonitor.mBroadcastReceiver.onReceive(getContext()
Bill Linef81cbd2018-07-05 17:48:49 +0800308 , putPhoneInfo(intentSimState, data, true));
309 mTestableLooper.processAllMessages();
310 // Even SimState Loaded, still need ACTION_SERVICE_STATE_CHANGED turn on mTelephonyCapable
Lucas Dupin3d053532019-01-29 12:35:22 -0800311 assertThat(mKeyguardUpdateMonitor.mTelephonyCapable).isFalse();
Bill Linef81cbd2018-07-05 17:48:49 +0800312
313 Intent intentServiceState = new Intent(TelephonyIntents.ACTION_SERVICE_STATE_CHANGED);
314 intentSimState.putExtra(IccCardConstants.INTENT_KEY_ICC_STATE
315 , IccCardConstants.INTENT_VALUE_ICC_LOADED);
Lucas Dupin3d053532019-01-29 12:35:22 -0800316 mKeyguardUpdateMonitor.mBroadcastReceiver.onReceive(getContext()
Bill Linef81cbd2018-07-05 17:48:49 +0800317 , putPhoneInfo(intentServiceState, data, true));
318 mTestableLooper.processAllMessages();
Lucas Dupin3d053532019-01-29 12:35:22 -0800319 assertThat(mKeyguardUpdateMonitor.mTelephonyCapable).isTrue();
320 }
321
322 @Test
323 public void testTriesToAuthenticate_whenBouncer() {
324 mKeyguardUpdateMonitor.sendKeyguardBouncerChanged(true);
325 mTestableLooper.processAllMessages();
326
Kevin Chyn8d2694a2019-04-11 18:30:40 -0700327 verify(mFaceManager).authenticate(any(), any(), anyInt(), any(), any(), anyInt());
Lucas Dupin3d053532019-01-29 12:35:22 -0800328 verify(mFaceManager).isHardwareDetected();
329 verify(mFaceManager).hasEnrolledTemplates(anyInt());
330 }
331
332 @Test
333 public void testTriesToAuthenticate_whenKeyguard() {
334 mKeyguardUpdateMonitor.dispatchStartedWakingUp();
335 mTestableLooper.processAllMessages();
336 mKeyguardUpdateMonitor.onKeyguardVisibilityChanged(true);
Kevin Chyn8d2694a2019-04-11 18:30:40 -0700337 verify(mFaceManager).authenticate(any(), any(), anyInt(), any(), any(), anyInt());
Lucas Dupin3d053532019-01-29 12:35:22 -0800338 }
339
340 @Test
341 public void skipsAuthentication_whenEncryptedKeyguard() {
Lucas Dupin8eec2682019-07-01 16:41:17 -0700342 when(mStrongAuthTracker.getStrongAuthForUser(anyInt())).thenReturn(
343 KeyguardUpdateMonitor.StrongAuthTracker.STRONG_AUTH_REQUIRED_AFTER_BOOT);
344 mKeyguardUpdateMonitor.setKeyguardBypassController(mKeyguardBypassController);
Lucas Dupin3d053532019-01-29 12:35:22 -0800345
346 mKeyguardUpdateMonitor.dispatchStartedWakingUp();
347 mTestableLooper.processAllMessages();
348 mKeyguardUpdateMonitor.onKeyguardVisibilityChanged(true);
Lucas Dupin8eec2682019-07-01 16:41:17 -0700349 verify(mFaceManager, never()).authenticate(any(), any(), anyInt(), any(), any(), anyInt());
350 }
351
352 @Test
353 public void requiresAuthentication_whenEncryptedKeyguard_andBypass() {
354 testStrongAuthExceptOnBouncer(
355 KeyguardUpdateMonitor.StrongAuthTracker.STRONG_AUTH_REQUIRED_AFTER_BOOT);
356 }
357
358 @Test
359 public void requiresAuthentication_whenTimeoutKeyguard_andBypass() {
360 testStrongAuthExceptOnBouncer(
361 KeyguardUpdateMonitor.StrongAuthTracker.STRONG_AUTH_REQUIRED_AFTER_TIMEOUT);
362 }
363
364 private void testStrongAuthExceptOnBouncer(int strongAuth) {
365 when(mKeyguardBypassController.canBypass()).thenReturn(true);
366 mKeyguardUpdateMonitor.setKeyguardBypassController(mKeyguardBypassController);
367 when(mStrongAuthTracker.getStrongAuthForUser(anyInt())).thenReturn(strongAuth);
368
369 mKeyguardUpdateMonitor.dispatchStartedWakingUp();
370 mTestableLooper.processAllMessages();
371 mKeyguardUpdateMonitor.onKeyguardVisibilityChanged(true);
372 verify(mFaceManager).authenticate(any(), any(), anyInt(), any(), any(), anyInt());
373
374 // Stop scanning when bouncer becomes visible
375 mKeyguardUpdateMonitor.sendKeyguardBouncerChanged(true /* showingBouncer */);
376 mTestableLooper.processAllMessages();
377 clearInvocations(mFaceManager);
378 mKeyguardUpdateMonitor.requestFaceAuth();
379 verify(mFaceManager, never()).authenticate(any(), any(), anyInt(), any(), any(), anyInt());
Lucas Dupin3d053532019-01-29 12:35:22 -0800380 }
381
382 @Test
383 public void testTriesToAuthenticate_whenAssistant() {
384 mKeyguardUpdateMonitor.setKeyguardOccluded(true);
385 mKeyguardUpdateMonitor.setAssistantVisible(true);
386
Kevin Chyn8d2694a2019-04-11 18:30:40 -0700387 verify(mFaceManager).authenticate(any(), any(), anyInt(), any(), any(), anyInt());
Lucas Dupin3d053532019-01-29 12:35:22 -0800388 }
389
390 @Test
Lucas Dupin4befb742019-07-01 11:31:31 -0700391 public void testTriesToAuthenticate_whenTrustOnAgentKeyguard_ifBypass() {
392 mKeyguardUpdateMonitor.setKeyguardBypassController(mKeyguardBypassController);
393 mKeyguardUpdateMonitor.dispatchStartedWakingUp();
394 mTestableLooper.processAllMessages();
395 when(mKeyguardBypassController.canBypass()).thenReturn(true);
396 mKeyguardUpdateMonitor.onTrustChanged(true /* enabled */,
397 KeyguardUpdateMonitor.getCurrentUser(), 0 /* flags */);
398 mKeyguardUpdateMonitor.onKeyguardVisibilityChanged(true);
399 verify(mFaceManager).authenticate(any(), any(), anyInt(), any(), any(), anyInt());
400 }
401
402 @Test
403 public void testIgnoresAuth_whenTrustAgentOnKeyguard_withoutBypass() {
404 mKeyguardUpdateMonitor.dispatchStartedWakingUp();
405 mTestableLooper.processAllMessages();
406 mKeyguardUpdateMonitor.onTrustChanged(true /* enabled */,
407 KeyguardUpdateMonitor.getCurrentUser(), 0 /* flags */);
408 mKeyguardUpdateMonitor.onKeyguardVisibilityChanged(true);
409 verify(mFaceManager, never()).authenticate(any(), any(), anyInt(), any(), any(), anyInt());
410 }
411
412 @Test
Lucas Dupin8eec2682019-07-01 16:41:17 -0700413 public void testIgnoresAuth_whenLockdown() {
414 mKeyguardUpdateMonitor.dispatchStartedWakingUp();
415 mTestableLooper.processAllMessages();
416 when(mStrongAuthTracker.getStrongAuthForUser(anyInt())).thenReturn(
417 KeyguardUpdateMonitor.StrongAuthTracker.STRONG_AUTH_REQUIRED_AFTER_USER_LOCKDOWN);
418
419 mKeyguardUpdateMonitor.onKeyguardVisibilityChanged(true);
420 verify(mFaceManager, never()).authenticate(any(), any(), anyInt(), any(), any(), anyInt());
421 }
422
423 @Test
424 public void testIgnoresAuth_whenLockout() {
425 mKeyguardUpdateMonitor.dispatchStartedWakingUp();
426 mTestableLooper.processAllMessages();
427 when(mStrongAuthTracker.getStrongAuthForUser(anyInt())).thenReturn(
428 KeyguardUpdateMonitor.StrongAuthTracker.STRONG_AUTH_REQUIRED_AFTER_LOCKOUT);
429
430 mKeyguardUpdateMonitor.onKeyguardVisibilityChanged(true);
431 verify(mFaceManager, never()).authenticate(any(), any(), anyInt(), any(), any(), anyInt());
432 }
433
434 @Test
Lucas Dupin3d053532019-01-29 12:35:22 -0800435 public void testOnFaceAuthenticated_skipsFaceWhenAuthenticated() {
436 mKeyguardUpdateMonitor.onFaceAuthenticated(KeyguardUpdateMonitor.getCurrentUser());
437 mKeyguardUpdateMonitor.sendKeyguardBouncerChanged(true);
438 mTestableLooper.processAllMessages();
439
440 verify(mFaceManager, never()).authenticate(any(), any(), anyInt(), any(), any());
441 }
442
443 @Test
444 public void testGetUserCanSkipBouncer_whenFace() {
445 int user = KeyguardUpdateMonitor.getCurrentUser();
446 mKeyguardUpdateMonitor.onFaceAuthenticated(user);
447 assertThat(mKeyguardUpdateMonitor.getUserCanSkipBouncer(user)).isTrue();
448 }
449
450 @Test
451 public void testGetUserCanSkipBouncer_whenFingerprint() {
452 int user = KeyguardUpdateMonitor.getCurrentUser();
453 mKeyguardUpdateMonitor.onFingerprintAuthenticated(user);
454 assertThat(mKeyguardUpdateMonitor.getUserCanSkipBouncer(user)).isTrue();
455 }
456
457 @Test
458 public void testGetUserCanSkipBouncer_whenTrust() {
459 int user = KeyguardUpdateMonitor.getCurrentUser();
460 mKeyguardUpdateMonitor.onTrustChanged(true /* enabled */, user, 0 /* flags */);
461 assertThat(mKeyguardUpdateMonitor.getUserCanSkipBouncer(user)).isTrue();
Bill Linef81cbd2018-07-05 17:48:49 +0800462 }
463
Malcolm Chen5c63b512019-08-13 13:24:07 -0700464 @Test
465 public void testGetSubscriptionInfo_whenInGroupedSubWithOpportunistic() {
466 List<SubscriptionInfo> list = new ArrayList<>();
467 list.add(TEST_SUBSCRIPTION);
468 list.add(TEST_SUBSCRIPTION_2);
469 when(mSubscriptionManager.getActiveSubscriptionInfoList(anyBoolean())).thenReturn(list);
470 mKeyguardUpdateMonitor.mPhoneStateListener.onActiveDataSubscriptionIdChanged(
471 TEST_SUBSCRIPTION_2.getSubscriptionId());
472 mTestableLooper.processAllMessages();
473
474 List<SubscriptionInfo> listToVerify = mKeyguardUpdateMonitor
475 .getFilteredSubscriptionInfo(false);
476 assertThat(listToVerify.size()).isEqualTo(1);
477 assertThat(listToVerify.get(0)).isEqualTo(TEST_SUBSCRIPTION_2);
478 }
479
Bill Linef81cbd2018-07-05 17:48:49 +0800480 private Intent putPhoneInfo(Intent intent, Bundle data, Boolean simInited) {
481 int subscription = simInited
482 ? 1/* mock subid=1 */ : SubscriptionManager.DUMMY_SUBSCRIPTION_ID_BASE;
483 if (data != null) intent.putExtras(data);
484 intent.putExtra(PhoneConstants.PHONE_NAME_KEY, "Phone");
485 intent.putExtra("subscription", subscription);
486 intent.putExtra("slot", 0/* SLOT 1 */);
487 return intent;
488 }
489
Lucas Dupin7ff82b02018-05-16 12:14:10 -0700490 private class TestableKeyguardUpdateMonitor extends KeyguardUpdateMonitor {
491 AtomicBoolean mSimStateChanged = new AtomicBoolean(false);
492
493 protected TestableKeyguardUpdateMonitor(Context context) {
494 super(context);
Lucas Dupin7ff82b02018-05-16 12:14:10 -0700495 context.unregisterReceiver(mBroadcastReceiver);
Lucas Dupin3d053532019-01-29 12:35:22 -0800496 context.unregisterReceiver(mBroadcastAllReceiver);
497 mStrongAuthTracker = KeyguardUpdateMonitorTest.this.mStrongAuthTracker;
Lucas Dupin7ff82b02018-05-16 12:14:10 -0700498 }
499
500 public boolean hasSimStateJustChanged() {
501 return mSimStateChanged.getAndSet(false);
502 }
503
504 @Override
505 protected void handleSimStateChange(int subId, int slotId,
506 IccCardConstants.State state) {
507 mSimStateChanged.set(true);
508 super.handleSimStateChange(subId, slotId, state);
509 }
Lucas Dupin5e0f0d22018-02-26 13:32:16 -0800510 }
511}