blob: c1da53b09cfc016524f2c264f4bd615c9cfc0f6d [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 Dupinf2c53502019-10-03 13:56:18 -070028import static org.mockito.ArgumentMatchers.eq;
Fabian Kozynski5ca7a512019-10-16 19:56:11 +000029import static org.mockito.Mockito.atLeastOnce;
Lucas Dupin8eec2682019-07-01 16:41:17 -070030import static org.mockito.Mockito.clearInvocations;
Lucas Dupin3d053532019-01-29 12:35:22 -080031import static org.mockito.Mockito.doAnswer;
32import static org.mockito.Mockito.never;
Lucas Dupin3d053532019-01-29 12:35:22 -080033import static org.mockito.Mockito.spy;
34import static org.mockito.Mockito.verify;
35import static org.mockito.Mockito.when;
Bill Linef81cbd2018-07-05 17:48:49 +080036
Lucas Dupin3d053532019-01-29 12:35:22 -080037import android.app.admin.DevicePolicyManager;
38import android.app.trust.TrustManager;
Lucas Dupin7ff82b02018-05-16 12:14:10 -070039import android.content.Context;
Lucas Dupin5e0f0d22018-02-26 13:32:16 -080040import android.content.Intent;
Fabian Kozynski5ca7a512019-10-16 19:56:11 +000041import android.content.IntentFilter;
Lucas Dupin3d053532019-01-29 12:35:22 -080042import android.content.pm.PackageManager;
43import android.hardware.biometrics.BiometricManager;
44import android.hardware.biometrics.BiometricSourceType;
45import android.hardware.biometrics.IBiometricEnabledOnKeyguardCallback;
46import android.hardware.face.FaceManager;
47import android.hardware.fingerprint.FingerprintManager;
Bill Linef81cbd2018-07-05 17:48:49 +080048import android.os.Bundle;
Fabian Kozynski5ca7a512019-10-16 19:56:11 +000049import android.os.Handler;
50import android.os.UserHandle;
Lucas Dupin3d053532019-01-29 12:35:22 -080051import android.os.UserManager;
Bill Linef81cbd2018-07-05 17:48:49 +080052import android.telephony.ServiceState;
Malcolm Chen5c63b512019-08-13 13:24:07 -070053import android.telephony.SubscriptionInfo;
Bill Linef81cbd2018-07-05 17:48:49 +080054import android.telephony.SubscriptionManager;
Lucas Dupin5e0f0d22018-02-26 13:32:16 -080055import android.test.suitebuilder.annotation.SmallTest;
56import android.testing.AndroidTestingRunner;
Lucas Dupin3d053532019-01-29 12:35:22 -080057import android.testing.TestableContext;
Lucas Dupin5e0f0d22018-02-26 13:32:16 -080058import android.testing.TestableLooper;
Lucas Dupin5e0f0d22018-02-26 13:32:16 -080059
60import com.android.internal.telephony.IccCardConstants;
Bill Linef81cbd2018-07-05 17:48:49 +080061import com.android.internal.telephony.PhoneConstants;
Lucas Dupin5e0f0d22018-02-26 13:32:16 -080062import com.android.internal.telephony.TelephonyIntents;
Lucas Dupin64171fe2019-10-30 14:28:29 -070063import com.android.systemui.DumpController;
Lucas Dupin5e0f0d22018-02-26 13:32:16 -080064import com.android.systemui.SysuiTestCase;
Fabian Kozynski5ca7a512019-10-16 19:56:11 +000065import com.android.systemui.broadcast.BroadcastDispatcher;
Lucas Dupin4befb742019-07-01 11:31:31 -070066import com.android.systemui.statusbar.phone.KeyguardBypassController;
Lucas Dupin5e0f0d22018-02-26 13:32:16 -080067
68import org.junit.Assert;
69import org.junit.Before;
70import org.junit.Test;
71import org.junit.runner.RunWith;
Lucas Dupin3d053532019-01-29 12:35:22 -080072import org.mockito.Mock;
73import org.mockito.MockitoAnnotations;
Lucas Dupin5e0f0d22018-02-26 13:32:16 -080074
Malcolm Chen5c63b512019-08-13 13:24:07 -070075import java.util.ArrayList;
76import java.util.List;
Lucas Dupin5e0f0d22018-02-26 13:32:16 -080077import java.util.concurrent.atomic.AtomicBoolean;
78
79@SmallTest
80@RunWith(AndroidTestingRunner.class)
Dave Mankoffe2294692019-08-14 11:53:13 -040081@TestableLooper.RunWithLooper
Lucas Dupin5e0f0d22018-02-26 13:32:16 -080082public class KeyguardUpdateMonitorTest extends SysuiTestCase {
Malcolm Chen5c63b512019-08-13 13:24:07 -070083 private static final String TEST_CARRIER = "TEST_CARRIER";
84 private static final String TEST_CARRIER_2 = "TEST_CARRIER_2";
85 private static final int TEST_CARRIER_ID = 1;
86 private static final String TEST_GROUP_UUID = "59b5c870-fc4c-47a4-a99e-9db826b48b24";
87 private static final SubscriptionInfo TEST_SUBSCRIPTION = new SubscriptionInfo(1, "", 0,
88 TEST_CARRIER, TEST_CARRIER, NAME_SOURCE_DEFAULT_SOURCE, 0xFFFFFF, "",
89 DATA_ROAMING_DISABLE, null, null, null, null, false, null, "", false, TEST_GROUP_UUID,
90 TEST_CARRIER_ID, 0);
91 private static final SubscriptionInfo TEST_SUBSCRIPTION_2 = new SubscriptionInfo(2, "", 0,
92 TEST_CARRIER, TEST_CARRIER_2, NAME_SOURCE_DEFAULT_SOURCE, 0xFFFFFF, "",
93 DATA_ROAMING_DISABLE, null, null, null, null, false, null, "", true, TEST_GROUP_UUID,
94 TEST_CARRIER_ID, 0);
Lucas Dupin3d053532019-01-29 12:35:22 -080095 @Mock
Lucas Dupin64171fe2019-10-30 14:28:29 -070096 private DumpController mDumpController;
97 @Mock
Lucas Dupin3d053532019-01-29 12:35:22 -080098 private KeyguardUpdateMonitor.StrongAuthTracker mStrongAuthTracker;
99 @Mock
100 private TrustManager mTrustManager;
101 @Mock
102 private FingerprintManager mFingerprintManager;
103 @Mock
104 private FaceManager mFaceManager;
105 @Mock
106 private BiometricManager mBiometricManager;
107 @Mock
108 private PackageManager mPackageManager;
109 @Mock
110 private UserManager mUserManager;
111 @Mock
112 private DevicePolicyManager mDevicePolicyManager;
Lucas Dupin4befb742019-07-01 11:31:31 -0700113 @Mock
114 private KeyguardBypassController mKeyguardBypassController;
Malcolm Chen5c63b512019-08-13 13:24:07 -0700115 @Mock
116 private SubscriptionManager mSubscriptionManager;
Fabian Kozynski5ca7a512019-10-16 19:56:11 +0000117 @Mock
118 private BroadcastDispatcher mBroadcastDispatcher;
Lucas Dupin5e0f0d22018-02-26 13:32:16 -0800119 private TestableLooper mTestableLooper;
Lucas Dupin3d053532019-01-29 12:35:22 -0800120 private TestableKeyguardUpdateMonitor mKeyguardUpdateMonitor;
Lucas Dupin5e0f0d22018-02-26 13:32:16 -0800121
122 @Before
123 public void setup() {
Lucas Dupin3d053532019-01-29 12:35:22 -0800124 MockitoAnnotations.initMocks(this);
125 TestableContext context = spy(mContext);
126 when(mPackageManager.hasSystemFeature(anyString())).thenReturn(true);
127 when(context.getPackageManager()).thenReturn(mPackageManager);
128 doAnswer(invocation -> {
129 IBiometricEnabledOnKeyguardCallback callback = invocation.getArgument(0);
Lucas Dupin7d95f152019-07-17 16:25:54 -0700130 callback.onChanged(BiometricSourceType.FACE, true /* enabled */,
131 KeyguardUpdateMonitor.getCurrentUser());
Lucas Dupin3d053532019-01-29 12:35:22 -0800132 return null;
133 }).when(mBiometricManager).registerEnabledOnKeyguardCallback(any());
134 when(mFaceManager.isHardwareDetected()).thenReturn(true);
135 when(mFaceManager.hasEnrolledTemplates()).thenReturn(true);
136 when(mFaceManager.hasEnrolledTemplates(anyInt())).thenReturn(true);
137 when(mUserManager.isUserUnlocked(anyInt())).thenReturn(true);
Kevin Chynfdfd2d32019-03-01 14:52:15 -0800138 when(mUserManager.isPrimaryUser()).thenReturn(true);
Lucas Dupin3d053532019-01-29 12:35:22 -0800139 when(mStrongAuthTracker.isUnlockingWithBiometricAllowed()).thenReturn(true);
140 context.addMockSystemService(TrustManager.class, mTrustManager);
141 context.addMockSystemService(FingerprintManager.class, mFingerprintManager);
142 context.addMockSystemService(BiometricManager.class, mBiometricManager);
143 context.addMockSystemService(FaceManager.class, mFaceManager);
144 context.addMockSystemService(UserManager.class, mUserManager);
145 context.addMockSystemService(DevicePolicyManager.class, mDevicePolicyManager);
Malcolm Chen5c63b512019-08-13 13:24:07 -0700146 context.addMockSystemService(SubscriptionManager.class, mSubscriptionManager);
Lucas Dupin3d053532019-01-29 12:35:22 -0800147
Lucas Dupin5e0f0d22018-02-26 13:32:16 -0800148 mTestableLooper = TestableLooper.get(this);
Lucas Dupin3d053532019-01-29 12:35:22 -0800149 mKeyguardUpdateMonitor = new TestableKeyguardUpdateMonitor(context);
Lucas Dupin5e0f0d22018-02-26 13:32:16 -0800150 }
151
152 @Test
Fabian Kozynski5ca7a512019-10-16 19:56:11 +0000153 public void testReceiversRegistered() {
154 verify(mBroadcastDispatcher, atLeastOnce()).registerReceiver(
155 eq(mKeyguardUpdateMonitor.mBroadcastReceiver),
156 any(IntentFilter.class), any(Handler.class));
157 verify(mBroadcastDispatcher, atLeastOnce()).registerReceiver(
158 eq(mKeyguardUpdateMonitor.mBroadcastAllReceiver),
159 any(IntentFilter.class), any(Handler.class), eq(UserHandle.ALL));
160 }
161
162 @Test
Lucas Dupin5e0f0d22018-02-26 13:32:16 -0800163 public void testIgnoresSimStateCallback_rebroadcast() {
164 Intent intent = new Intent(TelephonyIntents.ACTION_SIM_STATE_CHANGED);
165
Lucas Dupin3d053532019-01-29 12:35:22 -0800166 mKeyguardUpdateMonitor.mBroadcastReceiver.onReceive(getContext(), intent);
Lucas Dupin5e0f0d22018-02-26 13:32:16 -0800167 mTestableLooper.processAllMessages();
Lucas Dupin7ff82b02018-05-16 12:14:10 -0700168 Assert.assertTrue("onSimStateChanged not called",
Lucas Dupin3d053532019-01-29 12:35:22 -0800169 mKeyguardUpdateMonitor.hasSimStateJustChanged());
Lucas Dupin5e0f0d22018-02-26 13:32:16 -0800170
171 intent.putExtra(TelephonyIntents.EXTRA_REBROADCAST_ON_UNLOCK, true);
Lucas Dupin3d053532019-01-29 12:35:22 -0800172 mKeyguardUpdateMonitor.mBroadcastReceiver.onReceive(getContext(), intent);
Lucas Dupin5e0f0d22018-02-26 13:32:16 -0800173 mTestableLooper.processAllMessages();
Lucas Dupin7ff82b02018-05-16 12:14:10 -0700174 Assert.assertFalse("onSimStateChanged should have been skipped",
Lucas Dupin3d053532019-01-29 12:35:22 -0800175 mKeyguardUpdateMonitor.hasSimStateJustChanged());
Lucas Dupin7ff82b02018-05-16 12:14:10 -0700176 }
177
Bill Linef81cbd2018-07-05 17:48:49 +0800178 @Test
179 public void testTelephonyCapable_BootInitState() {
Lucas Dupin3d053532019-01-29 12:35:22 -0800180 assertThat(mKeyguardUpdateMonitor.mTelephonyCapable).isFalse();
Bill Linef81cbd2018-07-05 17:48:49 +0800181 }
182
183 @Test
184 public void testTelephonyCapable_SimState_Absent() {
185 Intent intent = new Intent(TelephonyIntents.ACTION_SIM_STATE_CHANGED);
andychou695a7602019-05-16 23:14:00 +0800186 intent.putExtra(IccCardConstants.INTENT_KEY_ICC_STATE,
187 IccCardConstants.INTENT_VALUE_ICC_ABSENT);
188 mKeyguardUpdateMonitor.mBroadcastReceiver.onReceive(getContext(),
189 putPhoneInfo(intent, null, false));
190 mTestableLooper.processAllMessages();
191 assertThat(mKeyguardUpdateMonitor.mTelephonyCapable).isTrue();
192 }
193
194 @Test
195 public void testTelephonyCapable_SimState_CardIOError() {
196 Intent intent = new Intent(TelephonyIntents.ACTION_SIM_STATE_CHANGED);
197 intent.putExtra(IccCardConstants.INTENT_KEY_ICC_STATE,
198 IccCardConstants.INTENT_VALUE_ICC_CARD_IO_ERROR);
199 mKeyguardUpdateMonitor.mBroadcastReceiver.onReceive(getContext(),
200 putPhoneInfo(intent, null, false));
Bill Linef81cbd2018-07-05 17:48:49 +0800201 mTestableLooper.processAllMessages();
Lucas Dupin3d053532019-01-29 12:35:22 -0800202 assertThat(mKeyguardUpdateMonitor.mTelephonyCapable).isTrue();
Bill Linef81cbd2018-07-05 17:48:49 +0800203 }
204
205 @Test
206 public void testTelephonyCapable_SimInvalid_ServiceState_InService() {
207 // SERVICE_STATE - IN_SERVICE, but SIM_STATE is invalid TelephonyCapable should be False
Bill Linef81cbd2018-07-05 17:48:49 +0800208 Intent intent = new Intent(TelephonyIntents.ACTION_SERVICE_STATE_CHANGED);
209 Bundle data = new Bundle();
210 ServiceState state = new ServiceState();
211 state.setState(ServiceState.STATE_IN_SERVICE);
212 state.fillInNotifierBundle(data);
Lucas Dupin3d053532019-01-29 12:35:22 -0800213 mKeyguardUpdateMonitor.mBroadcastReceiver.onReceive(getContext()
Bill Linef81cbd2018-07-05 17:48:49 +0800214 , putPhoneInfo(intent, data, false));
215 mTestableLooper.processAllMessages();
Lucas Dupin3d053532019-01-29 12:35:22 -0800216 assertThat(mKeyguardUpdateMonitor.mTelephonyCapable).isFalse();
Bill Linef81cbd2018-07-05 17:48:49 +0800217 }
218
219 @Test
220 public void testTelephonyCapable_SimValid_ServiceState_PowerOff() {
221 // Simulate AirplaneMode case, SERVICE_STATE - POWER_OFF, check TelephonyCapable False
222 // Only receive ServiceState callback IN_SERVICE -> OUT_OF_SERVICE -> POWER_OFF
Bill Linef81cbd2018-07-05 17:48:49 +0800223 Intent intent = new Intent(TelephonyIntents.ACTION_SERVICE_STATE_CHANGED);
224 intent.putExtra(IccCardConstants.INTENT_KEY_ICC_STATE
225 , IccCardConstants.INTENT_VALUE_ICC_LOADED);
226 Bundle data = new Bundle();
227 ServiceState state = new ServiceState();
228 state.setState(ServiceState.STATE_POWER_OFF);
229 state.fillInNotifierBundle(data);
Lucas Dupin3d053532019-01-29 12:35:22 -0800230 mKeyguardUpdateMonitor.mBroadcastReceiver.onReceive(getContext()
Bill Linef81cbd2018-07-05 17:48:49 +0800231 , putPhoneInfo(intent, data, true));
232 mTestableLooper.processAllMessages();
Lucas Dupin3d053532019-01-29 12:35:22 -0800233 assertThat(mKeyguardUpdateMonitor.mTelephonyCapable).isTrue();
Bill Linef81cbd2018-07-05 17:48:49 +0800234 }
235
236 /* Normal SIM inserted flow
237 * ServiceState: ---OutOfServie----->PowerOff->OutOfServie--->InService
238 * SimState: ----NOT_READY---->READY----------------------LOADED>>>
239 * Subscription: --------null---->null--->"Chunghwa Telecom"-------->>>
240 * System: -------------------------------BOOT_COMPLETED------>>>
241 * TelephonyCapable:(F)-(F)-(F)-(F)-(F)-(F)-(F)-(F)-(F)-(F)------(T)-(T)>>
242 */
243 @Test
244 public void testTelephonyCapable_BootInitState_ServiceState_OutOfService() {
Bill Linef81cbd2018-07-05 17:48:49 +0800245 Intent intent = new Intent(TelephonyIntents.ACTION_SERVICE_STATE_CHANGED);
246 Bundle data = new Bundle();
247 ServiceState state = new ServiceState();
248 state.setState(ServiceState.STATE_OUT_OF_SERVICE);
249 state.fillInNotifierBundle(data);
250 intent.putExtras(data);
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_NotReady() {
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_NOT_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_SimState_Ready() {
Bill Linef81cbd2018-07-05 17:48:49 +0800274 Bundle data = new Bundle();
275 ServiceState state = new ServiceState();
276 state.setState(ServiceState.STATE_OUT_OF_SERVICE);
277 state.fillInNotifierBundle(data);
278 Intent intent = new Intent(TelephonyIntents.ACTION_SIM_STATE_CHANGED);
279 intent.putExtra(IccCardConstants.INTENT_KEY_ICC_STATE
280 , IccCardConstants.INTENT_VALUE_ICC_READY);
Lucas Dupin3d053532019-01-29 12:35:22 -0800281 mKeyguardUpdateMonitor.mBroadcastReceiver.onReceive(getContext()
Bill Linef81cbd2018-07-05 17:48:49 +0800282 , putPhoneInfo(intent, data, false));
283 mTestableLooper.processAllMessages();
Lucas Dupin3d053532019-01-29 12:35:22 -0800284 assertThat(mKeyguardUpdateMonitor.mTelephonyCapable).isFalse();
Bill Linef81cbd2018-07-05 17:48:49 +0800285 }
286
287 @Test
288 public void testTelephonyCapable_BootInitState_ServiceState_PowerOff() {
Bill Linef81cbd2018-07-05 17:48:49 +0800289 Intent intent = new Intent(TelephonyIntents.ACTION_SERVICE_STATE_CHANGED);
290 Bundle data = new Bundle();
291 ServiceState state = new ServiceState();
292 state.setState(ServiceState.STATE_POWER_OFF);
293 state.fillInNotifierBundle(data);
Lucas Dupin3d053532019-01-29 12:35:22 -0800294 mKeyguardUpdateMonitor.mBroadcastReceiver.onReceive(getContext()
Bill Linef81cbd2018-07-05 17:48:49 +0800295 , putPhoneInfo(intent, data, false));
296 mTestableLooper.processAllMessages();
Lucas Dupin3d053532019-01-29 12:35:22 -0800297 assertThat(mKeyguardUpdateMonitor.mTelephonyCapable).isFalse();
Bill Linef81cbd2018-07-05 17:48:49 +0800298 }
299
300 @Test
301 public void testTelephonyCapable_SimValid_ServiceState_InService() {
Bill Linef81cbd2018-07-05 17:48:49 +0800302 Intent intent = new Intent(TelephonyIntents.ACTION_SERVICE_STATE_CHANGED);
303 Bundle data = new Bundle();
304 ServiceState state = new ServiceState();
305 state.setState(ServiceState.STATE_IN_SERVICE);
306 state.fillInNotifierBundle(data);
Lucas Dupin3d053532019-01-29 12:35:22 -0800307 mKeyguardUpdateMonitor.mBroadcastReceiver.onReceive(getContext()
Bill Linef81cbd2018-07-05 17:48:49 +0800308 , putPhoneInfo(intent, data, true));
309 mTestableLooper.processAllMessages();
Lucas Dupin3d053532019-01-29 12:35:22 -0800310 assertThat(mKeyguardUpdateMonitor.mTelephonyCapable).isTrue();
Bill Linef81cbd2018-07-05 17:48:49 +0800311 }
312
313 @Test
314 public void testTelephonyCapable_SimValid_SimState_Loaded() {
Bill Linef81cbd2018-07-05 17:48:49 +0800315 Bundle data = new Bundle();
316 ServiceState state = new ServiceState();
317 state.setState(ServiceState.STATE_IN_SERVICE);
318 state.fillInNotifierBundle(data);
319 Intent intentSimState = new Intent(TelephonyIntents.ACTION_SIM_STATE_CHANGED);
320 intentSimState.putExtra(IccCardConstants.INTENT_KEY_ICC_STATE
321 , IccCardConstants.INTENT_VALUE_ICC_LOADED);
Lucas Dupin3d053532019-01-29 12:35:22 -0800322 mKeyguardUpdateMonitor.mBroadcastReceiver.onReceive(getContext()
Bill Linef81cbd2018-07-05 17:48:49 +0800323 , putPhoneInfo(intentSimState, data, true));
324 mTestableLooper.processAllMessages();
325 // Even SimState Loaded, still need ACTION_SERVICE_STATE_CHANGED turn on mTelephonyCapable
Lucas Dupin3d053532019-01-29 12:35:22 -0800326 assertThat(mKeyguardUpdateMonitor.mTelephonyCapable).isFalse();
Bill Linef81cbd2018-07-05 17:48:49 +0800327
328 Intent intentServiceState = new Intent(TelephonyIntents.ACTION_SERVICE_STATE_CHANGED);
329 intentSimState.putExtra(IccCardConstants.INTENT_KEY_ICC_STATE
330 , IccCardConstants.INTENT_VALUE_ICC_LOADED);
Lucas Dupin3d053532019-01-29 12:35:22 -0800331 mKeyguardUpdateMonitor.mBroadcastReceiver.onReceive(getContext()
Bill Linef81cbd2018-07-05 17:48:49 +0800332 , putPhoneInfo(intentServiceState, data, true));
333 mTestableLooper.processAllMessages();
Lucas Dupin3d053532019-01-29 12:35:22 -0800334 assertThat(mKeyguardUpdateMonitor.mTelephonyCapable).isTrue();
335 }
336
337 @Test
338 public void testTriesToAuthenticate_whenBouncer() {
339 mKeyguardUpdateMonitor.sendKeyguardBouncerChanged(true);
340 mTestableLooper.processAllMessages();
341
Kevin Chyn8d2694a2019-04-11 18:30:40 -0700342 verify(mFaceManager).authenticate(any(), any(), anyInt(), any(), any(), anyInt());
Lucas Dupin3d053532019-01-29 12:35:22 -0800343 verify(mFaceManager).isHardwareDetected();
344 verify(mFaceManager).hasEnrolledTemplates(anyInt());
345 }
346
347 @Test
348 public void testTriesToAuthenticate_whenKeyguard() {
349 mKeyguardUpdateMonitor.dispatchStartedWakingUp();
350 mTestableLooper.processAllMessages();
351 mKeyguardUpdateMonitor.onKeyguardVisibilityChanged(true);
Kevin Chyn8d2694a2019-04-11 18:30:40 -0700352 verify(mFaceManager).authenticate(any(), any(), anyInt(), any(), any(), anyInt());
Lucas Dupin3d053532019-01-29 12:35:22 -0800353 }
354
355 @Test
356 public void skipsAuthentication_whenEncryptedKeyguard() {
Lucas Dupin8eec2682019-07-01 16:41:17 -0700357 when(mStrongAuthTracker.getStrongAuthForUser(anyInt())).thenReturn(
358 KeyguardUpdateMonitor.StrongAuthTracker.STRONG_AUTH_REQUIRED_AFTER_BOOT);
359 mKeyguardUpdateMonitor.setKeyguardBypassController(mKeyguardBypassController);
Lucas Dupin3d053532019-01-29 12:35:22 -0800360
361 mKeyguardUpdateMonitor.dispatchStartedWakingUp();
362 mTestableLooper.processAllMessages();
363 mKeyguardUpdateMonitor.onKeyguardVisibilityChanged(true);
Lucas Dupin8eec2682019-07-01 16:41:17 -0700364 verify(mFaceManager, never()).authenticate(any(), any(), anyInt(), any(), any(), anyInt());
365 }
366
367 @Test
368 public void requiresAuthentication_whenEncryptedKeyguard_andBypass() {
369 testStrongAuthExceptOnBouncer(
370 KeyguardUpdateMonitor.StrongAuthTracker.STRONG_AUTH_REQUIRED_AFTER_BOOT);
371 }
372
373 @Test
374 public void requiresAuthentication_whenTimeoutKeyguard_andBypass() {
375 testStrongAuthExceptOnBouncer(
376 KeyguardUpdateMonitor.StrongAuthTracker.STRONG_AUTH_REQUIRED_AFTER_TIMEOUT);
377 }
378
379 private void testStrongAuthExceptOnBouncer(int strongAuth) {
380 when(mKeyguardBypassController.canBypass()).thenReturn(true);
381 mKeyguardUpdateMonitor.setKeyguardBypassController(mKeyguardBypassController);
382 when(mStrongAuthTracker.getStrongAuthForUser(anyInt())).thenReturn(strongAuth);
383
384 mKeyguardUpdateMonitor.dispatchStartedWakingUp();
385 mTestableLooper.processAllMessages();
386 mKeyguardUpdateMonitor.onKeyguardVisibilityChanged(true);
387 verify(mFaceManager).authenticate(any(), any(), anyInt(), any(), any(), anyInt());
388
389 // Stop scanning when bouncer becomes visible
390 mKeyguardUpdateMonitor.sendKeyguardBouncerChanged(true /* showingBouncer */);
391 mTestableLooper.processAllMessages();
392 clearInvocations(mFaceManager);
393 mKeyguardUpdateMonitor.requestFaceAuth();
394 verify(mFaceManager, never()).authenticate(any(), any(), anyInt(), any(), any(), anyInt());
Lucas Dupin3d053532019-01-29 12:35:22 -0800395 }
396
397 @Test
398 public void testTriesToAuthenticate_whenAssistant() {
399 mKeyguardUpdateMonitor.setKeyguardOccluded(true);
400 mKeyguardUpdateMonitor.setAssistantVisible(true);
401
Kevin Chyn8d2694a2019-04-11 18:30:40 -0700402 verify(mFaceManager).authenticate(any(), any(), anyInt(), any(), any(), anyInt());
Lucas Dupin3d053532019-01-29 12:35:22 -0800403 }
404
405 @Test
Lucas Dupin4befb742019-07-01 11:31:31 -0700406 public void testTriesToAuthenticate_whenTrustOnAgentKeyguard_ifBypass() {
407 mKeyguardUpdateMonitor.setKeyguardBypassController(mKeyguardBypassController);
408 mKeyguardUpdateMonitor.dispatchStartedWakingUp();
409 mTestableLooper.processAllMessages();
410 when(mKeyguardBypassController.canBypass()).thenReturn(true);
411 mKeyguardUpdateMonitor.onTrustChanged(true /* enabled */,
412 KeyguardUpdateMonitor.getCurrentUser(), 0 /* flags */);
413 mKeyguardUpdateMonitor.onKeyguardVisibilityChanged(true);
414 verify(mFaceManager).authenticate(any(), any(), anyInt(), any(), any(), anyInt());
415 }
416
417 @Test
418 public void testIgnoresAuth_whenTrustAgentOnKeyguard_withoutBypass() {
419 mKeyguardUpdateMonitor.dispatchStartedWakingUp();
420 mTestableLooper.processAllMessages();
421 mKeyguardUpdateMonitor.onTrustChanged(true /* enabled */,
422 KeyguardUpdateMonitor.getCurrentUser(), 0 /* flags */);
423 mKeyguardUpdateMonitor.onKeyguardVisibilityChanged(true);
424 verify(mFaceManager, never()).authenticate(any(), any(), anyInt(), any(), any(), anyInt());
425 }
426
427 @Test
Lucas Dupin8eec2682019-07-01 16:41:17 -0700428 public void testIgnoresAuth_whenLockdown() {
429 mKeyguardUpdateMonitor.dispatchStartedWakingUp();
430 mTestableLooper.processAllMessages();
431 when(mStrongAuthTracker.getStrongAuthForUser(anyInt())).thenReturn(
432 KeyguardUpdateMonitor.StrongAuthTracker.STRONG_AUTH_REQUIRED_AFTER_USER_LOCKDOWN);
433
434 mKeyguardUpdateMonitor.onKeyguardVisibilityChanged(true);
435 verify(mFaceManager, never()).authenticate(any(), any(), anyInt(), any(), any(), anyInt());
436 }
437
438 @Test
439 public void testIgnoresAuth_whenLockout() {
440 mKeyguardUpdateMonitor.dispatchStartedWakingUp();
441 mTestableLooper.processAllMessages();
442 when(mStrongAuthTracker.getStrongAuthForUser(anyInt())).thenReturn(
443 KeyguardUpdateMonitor.StrongAuthTracker.STRONG_AUTH_REQUIRED_AFTER_LOCKOUT);
444
445 mKeyguardUpdateMonitor.onKeyguardVisibilityChanged(true);
446 verify(mFaceManager, never()).authenticate(any(), any(), anyInt(), any(), any(), anyInt());
447 }
448
449 @Test
Lucas Dupin3d053532019-01-29 12:35:22 -0800450 public void testOnFaceAuthenticated_skipsFaceWhenAuthenticated() {
451 mKeyguardUpdateMonitor.onFaceAuthenticated(KeyguardUpdateMonitor.getCurrentUser());
452 mKeyguardUpdateMonitor.sendKeyguardBouncerChanged(true);
453 mTestableLooper.processAllMessages();
454
455 verify(mFaceManager, never()).authenticate(any(), any(), anyInt(), any(), any());
456 }
457
458 @Test
459 public void testGetUserCanSkipBouncer_whenFace() {
460 int user = KeyguardUpdateMonitor.getCurrentUser();
461 mKeyguardUpdateMonitor.onFaceAuthenticated(user);
462 assertThat(mKeyguardUpdateMonitor.getUserCanSkipBouncer(user)).isTrue();
463 }
464
465 @Test
466 public void testGetUserCanSkipBouncer_whenFingerprint() {
467 int user = KeyguardUpdateMonitor.getCurrentUser();
468 mKeyguardUpdateMonitor.onFingerprintAuthenticated(user);
469 assertThat(mKeyguardUpdateMonitor.getUserCanSkipBouncer(user)).isTrue();
470 }
471
472 @Test
473 public void testGetUserCanSkipBouncer_whenTrust() {
474 int user = KeyguardUpdateMonitor.getCurrentUser();
475 mKeyguardUpdateMonitor.onTrustChanged(true /* enabled */, user, 0 /* flags */);
476 assertThat(mKeyguardUpdateMonitor.getUserCanSkipBouncer(user)).isTrue();
Bill Linef81cbd2018-07-05 17:48:49 +0800477 }
478
Malcolm Chen5c63b512019-08-13 13:24:07 -0700479 @Test
480 public void testGetSubscriptionInfo_whenInGroupedSubWithOpportunistic() {
481 List<SubscriptionInfo> list = new ArrayList<>();
482 list.add(TEST_SUBSCRIPTION);
483 list.add(TEST_SUBSCRIPTION_2);
484 when(mSubscriptionManager.getActiveSubscriptionInfoList(anyBoolean())).thenReturn(list);
485 mKeyguardUpdateMonitor.mPhoneStateListener.onActiveDataSubscriptionIdChanged(
486 TEST_SUBSCRIPTION_2.getSubscriptionId());
487 mTestableLooper.processAllMessages();
488
489 List<SubscriptionInfo> listToVerify = mKeyguardUpdateMonitor
490 .getFilteredSubscriptionInfo(false);
491 assertThat(listToVerify.size()).isEqualTo(1);
492 assertThat(listToVerify.get(0)).isEqualTo(TEST_SUBSCRIPTION_2);
493 }
494
Lucas Dupin6c6d5732019-08-27 17:36:05 -0700495 @Test
496 public void testIsUserUnlocked() {
497 // mUserManager will report the user as unlocked on @Before
498 assertThat(mKeyguardUpdateMonitor.isUserUnlocked(KeyguardUpdateMonitor.getCurrentUser()))
499 .isTrue();
500 // Invalid user should not be unlocked.
501 int randomUser = 99;
502 assertThat(mKeyguardUpdateMonitor.isUserUnlocked(randomUser)).isFalse();
503 }
504
Lucas Dupinf2c53502019-10-03 13:56:18 -0700505 @Test
506 public void testTrustUsuallyManaged_whenTrustChanges() {
507 int user = KeyguardUpdateMonitor.getCurrentUser();
508 when(mTrustManager.isTrustUsuallyManaged(eq(user))).thenReturn(true);
509 mKeyguardUpdateMonitor.onTrustManagedChanged(false /* managed */, user);
510 assertThat(mKeyguardUpdateMonitor.isTrustUsuallyManaged(user)).isTrue();
511 }
512
513 @Test
514 public void testTrustUsuallyManaged_resetWhenUserIsRemoved() {
515 int user = KeyguardUpdateMonitor.getCurrentUser();
516 when(mTrustManager.isTrustUsuallyManaged(eq(user))).thenReturn(true);
517 mKeyguardUpdateMonitor.onTrustManagedChanged(false /* managed */, user);
518 assertThat(mKeyguardUpdateMonitor.isTrustUsuallyManaged(user)).isTrue();
519
520 mKeyguardUpdateMonitor.handleUserRemoved(user);
521 assertThat(mKeyguardUpdateMonitor.isTrustUsuallyManaged(user)).isFalse();
522 }
523
Bill Linef81cbd2018-07-05 17:48:49 +0800524 private Intent putPhoneInfo(Intent intent, Bundle data, Boolean simInited) {
525 int subscription = simInited
526 ? 1/* mock subid=1 */ : SubscriptionManager.DUMMY_SUBSCRIPTION_ID_BASE;
527 if (data != null) intent.putExtras(data);
528 intent.putExtra(PhoneConstants.PHONE_NAME_KEY, "Phone");
529 intent.putExtra("subscription", subscription);
530 intent.putExtra("slot", 0/* SLOT 1 */);
531 return intent;
532 }
533
Lucas Dupin7ff82b02018-05-16 12:14:10 -0700534 private class TestableKeyguardUpdateMonitor extends KeyguardUpdateMonitor {
535 AtomicBoolean mSimStateChanged = new AtomicBoolean(false);
536
537 protected TestableKeyguardUpdateMonitor(Context context) {
Fabian Kozynski5ca7a512019-10-16 19:56:11 +0000538 super(context,
539 TestableLooper.get(KeyguardUpdateMonitorTest.this).getLooper(),
540 mBroadcastDispatcher, mDumpController);
Lucas Dupin3d053532019-01-29 12:35:22 -0800541 mStrongAuthTracker = KeyguardUpdateMonitorTest.this.mStrongAuthTracker;
Lucas Dupin7ff82b02018-05-16 12:14:10 -0700542 }
543
544 public boolean hasSimStateJustChanged() {
545 return mSimStateChanged.getAndSet(false);
546 }
547
548 @Override
549 protected void handleSimStateChange(int subId, int slotId,
550 IccCardConstants.State state) {
551 mSimStateChanged.set(true);
552 super.handleSimStateChange(subId, slotId, state);
553 }
Lucas Dupin5e0f0d22018-02-26 13:32:16 -0800554 }
555}