blob: d0158429f1a3b7c7570558cf5e3044a11e5a0147 [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;
Peter Wang37916d62019-11-25 16:50:50 -080020import static android.telephony.SubscriptionManager.NAME_SOURCE_DEFAULT;
Malcolm Chen5c63b512019-08-13 13:24:07 -070021
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
Yvonne Jiangb7024a22019-12-05 16:57:08 -080037import android.app.Activity;
Lucas Dupin3d053532019-01-29 12:35:22 -080038import android.app.admin.DevicePolicyManager;
39import android.app.trust.TrustManager;
Yvonne Jiangb7024a22019-12-05 16:57:08 -080040import android.content.BroadcastReceiver;
Lucas Dupin7ff82b02018-05-16 12:14:10 -070041import android.content.Context;
Lucas Dupin5e0f0d22018-02-26 13:32:16 -080042import android.content.Intent;
Fabian Kozynski5ca7a512019-10-16 19:56:11 +000043import android.content.IntentFilter;
Lucas Dupin3d053532019-01-29 12:35:22 -080044import android.content.pm.PackageManager;
Yvonne Jiangb7024a22019-12-05 16:57:08 -080045import android.content.pm.ResolveInfo;
46import android.content.pm.ServiceInfo;
Lucas Dupin3d053532019-01-29 12:35:22 -080047import android.hardware.biometrics.BiometricManager;
48import android.hardware.biometrics.BiometricSourceType;
49import android.hardware.biometrics.IBiometricEnabledOnKeyguardCallback;
50import android.hardware.face.FaceManager;
51import android.hardware.fingerprint.FingerprintManager;
Bill Linef81cbd2018-07-05 17:48:49 +080052import android.os.Bundle;
Fabian Kozynski5ca7a512019-10-16 19:56:11 +000053import android.os.Handler;
54import android.os.UserHandle;
Lucas Dupin3d053532019-01-29 12:35:22 -080055import android.os.UserManager;
Bill Linef81cbd2018-07-05 17:48:49 +080056import android.telephony.ServiceState;
Malcolm Chen5c63b512019-08-13 13:24:07 -070057import android.telephony.SubscriptionInfo;
Bill Linef81cbd2018-07-05 17:48:49 +080058import android.telephony.SubscriptionManager;
Lucas Dupin5e0f0d22018-02-26 13:32:16 -080059import android.test.suitebuilder.annotation.SmallTest;
60import android.testing.AndroidTestingRunner;
Lucas Dupin3d053532019-01-29 12:35:22 -080061import android.testing.TestableContext;
Lucas Dupin5e0f0d22018-02-26 13:32:16 -080062import android.testing.TestableLooper;
Lucas Dupin5e0f0d22018-02-26 13:32:16 -080063
Lucas Dupin5e0f0d22018-02-26 13:32:16 -080064import com.android.internal.telephony.TelephonyIntents;
Lucas Dupin64171fe2019-10-30 14:28:29 -070065import com.android.systemui.DumpController;
Lucas Dupin5e0f0d22018-02-26 13:32:16 -080066import com.android.systemui.SysuiTestCase;
Fabian Kozynski5ca7a512019-10-16 19:56:11 +000067import com.android.systemui.broadcast.BroadcastDispatcher;
Lucas Dupin4befb742019-07-01 11:31:31 -070068import com.android.systemui.statusbar.phone.KeyguardBypassController;
Lucas Dupin5e0f0d22018-02-26 13:32:16 -080069
70import org.junit.Assert;
71import org.junit.Before;
72import org.junit.Test;
73import org.junit.runner.RunWith;
Lucas Dupin3d053532019-01-29 12:35:22 -080074import org.mockito.Mock;
75import org.mockito.MockitoAnnotations;
Lucas Dupin5e0f0d22018-02-26 13:32:16 -080076
Malcolm Chen5c63b512019-08-13 13:24:07 -070077import java.util.ArrayList;
78import java.util.List;
Lucas Dupin5e0f0d22018-02-26 13:32:16 -080079import java.util.concurrent.atomic.AtomicBoolean;
80
81@SmallTest
82@RunWith(AndroidTestingRunner.class)
Dave Mankoffe2294692019-08-14 11:53:13 -040083@TestableLooper.RunWithLooper
Lucas Dupin5e0f0d22018-02-26 13:32:16 -080084public class KeyguardUpdateMonitorTest extends SysuiTestCase {
Malcolm Chen5c63b512019-08-13 13:24:07 -070085 private static final String TEST_CARRIER = "TEST_CARRIER";
86 private static final String TEST_CARRIER_2 = "TEST_CARRIER_2";
87 private static final int TEST_CARRIER_ID = 1;
88 private static final String TEST_GROUP_UUID = "59b5c870-fc4c-47a4-a99e-9db826b48b24";
89 private static final SubscriptionInfo TEST_SUBSCRIPTION = new SubscriptionInfo(1, "", 0,
Peter Wang37916d62019-11-25 16:50:50 -080090 TEST_CARRIER, TEST_CARRIER, NAME_SOURCE_DEFAULT, 0xFFFFFF, "",
Malcolm Chen5c63b512019-08-13 13:24:07 -070091 DATA_ROAMING_DISABLE, null, null, null, null, false, null, "", false, TEST_GROUP_UUID,
92 TEST_CARRIER_ID, 0);
93 private static final SubscriptionInfo TEST_SUBSCRIPTION_2 = new SubscriptionInfo(2, "", 0,
Peter Wang37916d62019-11-25 16:50:50 -080094 TEST_CARRIER, TEST_CARRIER_2, NAME_SOURCE_DEFAULT, 0xFFFFFF, "",
Malcolm Chen5c63b512019-08-13 13:24:07 -070095 DATA_ROAMING_DISABLE, null, null, null, null, false, null, "", true, TEST_GROUP_UUID,
96 TEST_CARRIER_ID, 0);
Lucas Dupin3d053532019-01-29 12:35:22 -080097 @Mock
Lucas Dupin64171fe2019-10-30 14:28:29 -070098 private DumpController mDumpController;
99 @Mock
Lucas Dupin3d053532019-01-29 12:35:22 -0800100 private KeyguardUpdateMonitor.StrongAuthTracker mStrongAuthTracker;
101 @Mock
102 private TrustManager mTrustManager;
103 @Mock
104 private FingerprintManager mFingerprintManager;
105 @Mock
106 private FaceManager mFaceManager;
107 @Mock
108 private BiometricManager mBiometricManager;
109 @Mock
110 private PackageManager mPackageManager;
111 @Mock
112 private UserManager mUserManager;
113 @Mock
114 private DevicePolicyManager mDevicePolicyManager;
Lucas Dupin4befb742019-07-01 11:31:31 -0700115 @Mock
116 private KeyguardBypassController mKeyguardBypassController;
Malcolm Chen5c63b512019-08-13 13:24:07 -0700117 @Mock
118 private SubscriptionManager mSubscriptionManager;
Fabian Kozynski5ca7a512019-10-16 19:56:11 +0000119 @Mock
120 private BroadcastDispatcher mBroadcastDispatcher;
Lucas Dupin5e0f0d22018-02-26 13:32:16 -0800121 private TestableLooper mTestableLooper;
Lucas Dupin3d053532019-01-29 12:35:22 -0800122 private TestableKeyguardUpdateMonitor mKeyguardUpdateMonitor;
Lucas Dupin5e0f0d22018-02-26 13:32:16 -0800123
124 @Before
125 public void setup() {
Lucas Dupin3d053532019-01-29 12:35:22 -0800126 MockitoAnnotations.initMocks(this);
127 TestableContext context = spy(mContext);
128 when(mPackageManager.hasSystemFeature(anyString())).thenReturn(true);
129 when(context.getPackageManager()).thenReturn(mPackageManager);
130 doAnswer(invocation -> {
131 IBiometricEnabledOnKeyguardCallback callback = invocation.getArgument(0);
Lucas Dupin7d95f152019-07-17 16:25:54 -0700132 callback.onChanged(BiometricSourceType.FACE, true /* enabled */,
133 KeyguardUpdateMonitor.getCurrentUser());
Lucas Dupin3d053532019-01-29 12:35:22 -0800134 return null;
135 }).when(mBiometricManager).registerEnabledOnKeyguardCallback(any());
136 when(mFaceManager.isHardwareDetected()).thenReturn(true);
137 when(mFaceManager.hasEnrolledTemplates()).thenReturn(true);
138 when(mFaceManager.hasEnrolledTemplates(anyInt())).thenReturn(true);
139 when(mUserManager.isUserUnlocked(anyInt())).thenReturn(true);
Kevin Chynfdfd2d32019-03-01 14:52:15 -0800140 when(mUserManager.isPrimaryUser()).thenReturn(true);
Lucas Dupin3d053532019-01-29 12:35:22 -0800141 when(mStrongAuthTracker.isUnlockingWithBiometricAllowed()).thenReturn(true);
142 context.addMockSystemService(TrustManager.class, mTrustManager);
143 context.addMockSystemService(FingerprintManager.class, mFingerprintManager);
144 context.addMockSystemService(BiometricManager.class, mBiometricManager);
145 context.addMockSystemService(FaceManager.class, mFaceManager);
146 context.addMockSystemService(UserManager.class, mUserManager);
147 context.addMockSystemService(DevicePolicyManager.class, mDevicePolicyManager);
Malcolm Chen5c63b512019-08-13 13:24:07 -0700148 context.addMockSystemService(SubscriptionManager.class, mSubscriptionManager);
Lucas Dupin3d053532019-01-29 12:35:22 -0800149
Lucas Dupin5e0f0d22018-02-26 13:32:16 -0800150 mTestableLooper = TestableLooper.get(this);
Lucas Dupin3d053532019-01-29 12:35:22 -0800151 mKeyguardUpdateMonitor = new TestableKeyguardUpdateMonitor(context);
Lucas Dupin5e0f0d22018-02-26 13:32:16 -0800152 }
153
154 @Test
Fabian Kozynski5ca7a512019-10-16 19:56:11 +0000155 public void testReceiversRegistered() {
Fabian Kozynski5e92c6f2020-01-03 13:56:17 -0500156 verify(mBroadcastDispatcher, atLeastOnce()).registerReceiverWithHandler(
Fabian Kozynski5ca7a512019-10-16 19:56:11 +0000157 eq(mKeyguardUpdateMonitor.mBroadcastReceiver),
158 any(IntentFilter.class), any(Handler.class));
Fabian Kozynski5e92c6f2020-01-03 13:56:17 -0500159 verify(mBroadcastDispatcher, atLeastOnce()).registerReceiverWithHandler(
Fabian Kozynski5ca7a512019-10-16 19:56:11 +0000160 eq(mKeyguardUpdateMonitor.mBroadcastAllReceiver),
161 any(IntentFilter.class), any(Handler.class), eq(UserHandle.ALL));
162 }
163
164 @Test
Lucas Dupin5e0f0d22018-02-26 13:32:16 -0800165 public void testIgnoresSimStateCallback_rebroadcast() {
166 Intent intent = new Intent(TelephonyIntents.ACTION_SIM_STATE_CHANGED);
167
Lucas Dupin3d053532019-01-29 12:35:22 -0800168 mKeyguardUpdateMonitor.mBroadcastReceiver.onReceive(getContext(), intent);
Lucas Dupin5e0f0d22018-02-26 13:32:16 -0800169 mTestableLooper.processAllMessages();
Lucas Dupin7ff82b02018-05-16 12:14:10 -0700170 Assert.assertTrue("onSimStateChanged not called",
Lucas Dupin3d053532019-01-29 12:35:22 -0800171 mKeyguardUpdateMonitor.hasSimStateJustChanged());
Lucas Dupin5e0f0d22018-02-26 13:32:16 -0800172
Meng Wang0e8d91a2020-01-17 14:58:01 -0800173 intent.putExtra(Intent.EXTRA_REBROADCAST_ON_UNLOCK, true);
Lucas Dupin3d053532019-01-29 12:35:22 -0800174 mKeyguardUpdateMonitor.mBroadcastReceiver.onReceive(getContext(), intent);
Lucas Dupin5e0f0d22018-02-26 13:32:16 -0800175 mTestableLooper.processAllMessages();
Lucas Dupin7ff82b02018-05-16 12:14:10 -0700176 Assert.assertFalse("onSimStateChanged should have been skipped",
Lucas Dupin3d053532019-01-29 12:35:22 -0800177 mKeyguardUpdateMonitor.hasSimStateJustChanged());
Lucas Dupin7ff82b02018-05-16 12:14:10 -0700178 }
179
Bill Linef81cbd2018-07-05 17:48:49 +0800180 @Test
181 public void testTelephonyCapable_BootInitState() {
Lucas Dupin3d053532019-01-29 12:35:22 -0800182 assertThat(mKeyguardUpdateMonitor.mTelephonyCapable).isFalse();
Bill Linef81cbd2018-07-05 17:48:49 +0800183 }
184
185 @Test
186 public void testTelephonyCapable_SimState_Absent() {
187 Intent intent = new Intent(TelephonyIntents.ACTION_SIM_STATE_CHANGED);
Jayachandran C0173df82019-12-19 12:11:03 -0800188 intent.putExtra(Intent.EXTRA_SIM_STATE,
189 Intent.SIM_STATE_ABSENT);
andychou695a7602019-05-16 23:14:00 +0800190 mKeyguardUpdateMonitor.mBroadcastReceiver.onReceive(getContext(),
191 putPhoneInfo(intent, null, false));
192 mTestableLooper.processAllMessages();
193 assertThat(mKeyguardUpdateMonitor.mTelephonyCapable).isTrue();
194 }
195
196 @Test
197 public void testTelephonyCapable_SimState_CardIOError() {
198 Intent intent = new Intent(TelephonyIntents.ACTION_SIM_STATE_CHANGED);
Jayachandran C0173df82019-12-19 12:11:03 -0800199 intent.putExtra(Intent.EXTRA_SIM_STATE,
200 Intent.SIM_STATE_CARD_IO_ERROR);
andychou695a7602019-05-16 23:14:00 +0800201 mKeyguardUpdateMonitor.mBroadcastReceiver.onReceive(getContext(),
202 putPhoneInfo(intent, null, false));
Bill Linef81cbd2018-07-05 17:48:49 +0800203 mTestableLooper.processAllMessages();
Lucas Dupin3d053532019-01-29 12:35:22 -0800204 assertThat(mKeyguardUpdateMonitor.mTelephonyCapable).isTrue();
Bill Linef81cbd2018-07-05 17:48:49 +0800205 }
206
207 @Test
208 public void testTelephonyCapable_SimInvalid_ServiceState_InService() {
209 // SERVICE_STATE - IN_SERVICE, but SIM_STATE is invalid TelephonyCapable should be False
Jayachandran C041e7692019-12-20 16:20:02 -0800210 Intent intent = new Intent(Intent.ACTION_SERVICE_STATE);
Bill Linef81cbd2018-07-05 17:48:49 +0800211 Bundle data = new Bundle();
212 ServiceState state = new ServiceState();
213 state.setState(ServiceState.STATE_IN_SERVICE);
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, false));
217 mTestableLooper.processAllMessages();
Lucas Dupin3d053532019-01-29 12:35:22 -0800218 assertThat(mKeyguardUpdateMonitor.mTelephonyCapable).isFalse();
Bill Linef81cbd2018-07-05 17:48:49 +0800219 }
220
221 @Test
222 public void testTelephonyCapable_SimValid_ServiceState_PowerOff() {
223 // Simulate AirplaneMode case, SERVICE_STATE - POWER_OFF, check TelephonyCapable False
224 // Only receive ServiceState callback IN_SERVICE -> OUT_OF_SERVICE -> POWER_OFF
Jayachandran C041e7692019-12-20 16:20:02 -0800225 Intent intent = new Intent(Intent.ACTION_SERVICE_STATE);
Jayachandran C0173df82019-12-19 12:11:03 -0800226 intent.putExtra(Intent.EXTRA_SIM_STATE
227 , Intent.SIM_STATE_LOADED);
Bill Linef81cbd2018-07-05 17:48:49 +0800228 Bundle data = new Bundle();
229 ServiceState state = new ServiceState();
230 state.setState(ServiceState.STATE_POWER_OFF);
231 state.fillInNotifierBundle(data);
Lucas Dupin3d053532019-01-29 12:35:22 -0800232 mKeyguardUpdateMonitor.mBroadcastReceiver.onReceive(getContext()
Bill Linef81cbd2018-07-05 17:48:49 +0800233 , putPhoneInfo(intent, data, true));
234 mTestableLooper.processAllMessages();
Lucas Dupin3d053532019-01-29 12:35:22 -0800235 assertThat(mKeyguardUpdateMonitor.mTelephonyCapable).isTrue();
Bill Linef81cbd2018-07-05 17:48:49 +0800236 }
237
238 /* Normal SIM inserted flow
239 * ServiceState: ---OutOfServie----->PowerOff->OutOfServie--->InService
240 * SimState: ----NOT_READY---->READY----------------------LOADED>>>
241 * Subscription: --------null---->null--->"Chunghwa Telecom"-------->>>
242 * System: -------------------------------BOOT_COMPLETED------>>>
243 * TelephonyCapable:(F)-(F)-(F)-(F)-(F)-(F)-(F)-(F)-(F)-(F)------(T)-(T)>>
244 */
245 @Test
246 public void testTelephonyCapable_BootInitState_ServiceState_OutOfService() {
Jayachandran C041e7692019-12-20 16:20:02 -0800247 Intent intent = new Intent(Intent.ACTION_SERVICE_STATE);
Bill Linef81cbd2018-07-05 17:48:49 +0800248 Bundle data = new Bundle();
249 ServiceState state = new ServiceState();
250 state.setState(ServiceState.STATE_OUT_OF_SERVICE);
251 state.fillInNotifierBundle(data);
252 intent.putExtras(data);
Lucas Dupin3d053532019-01-29 12:35:22 -0800253 mKeyguardUpdateMonitor.mBroadcastReceiver.onReceive(getContext()
Bill Linef81cbd2018-07-05 17:48:49 +0800254 , putPhoneInfo(intent, data, false));
255 mTestableLooper.processAllMessages();
Lucas Dupin3d053532019-01-29 12:35:22 -0800256 assertThat(mKeyguardUpdateMonitor.mTelephonyCapable).isFalse();
Bill Linef81cbd2018-07-05 17:48:49 +0800257 }
258
259 @Test
260 public void testTelephonyCapable_BootInitState_SimState_NotReady() {
Bill Linef81cbd2018-07-05 17:48:49 +0800261 Bundle data = new Bundle();
262 ServiceState state = new ServiceState();
263 state.setState(ServiceState.STATE_OUT_OF_SERVICE);
264 state.fillInNotifierBundle(data);
265 Intent intent = new Intent(TelephonyIntents.ACTION_SIM_STATE_CHANGED);
Jayachandran C0173df82019-12-19 12:11:03 -0800266 intent.putExtra(Intent.EXTRA_SIM_STATE
267 , Intent.SIM_STATE_NOT_READY);
Lucas Dupin3d053532019-01-29 12:35:22 -0800268 mKeyguardUpdateMonitor.mBroadcastReceiver.onReceive(getContext()
Bill Linef81cbd2018-07-05 17:48:49 +0800269 , putPhoneInfo(intent, data, false));
270 mTestableLooper.processAllMessages();
Lucas Dupin3d053532019-01-29 12:35:22 -0800271 assertThat(mKeyguardUpdateMonitor.mTelephonyCapable).isFalse();
Bill Linef81cbd2018-07-05 17:48:49 +0800272 }
273
274 @Test
275 public void testTelephonyCapable_BootInitState_SimState_Ready() {
Bill Linef81cbd2018-07-05 17:48:49 +0800276 Bundle data = new Bundle();
277 ServiceState state = new ServiceState();
278 state.setState(ServiceState.STATE_OUT_OF_SERVICE);
279 state.fillInNotifierBundle(data);
280 Intent intent = new Intent(TelephonyIntents.ACTION_SIM_STATE_CHANGED);
Jayachandran C0173df82019-12-19 12:11:03 -0800281 intent.putExtra(Intent.EXTRA_SIM_STATE
282 , Intent.SIM_STATE_READY);
Lucas Dupin3d053532019-01-29 12:35:22 -0800283 mKeyguardUpdateMonitor.mBroadcastReceiver.onReceive(getContext()
Bill Linef81cbd2018-07-05 17:48:49 +0800284 , putPhoneInfo(intent, data, false));
285 mTestableLooper.processAllMessages();
Lucas Dupin3d053532019-01-29 12:35:22 -0800286 assertThat(mKeyguardUpdateMonitor.mTelephonyCapable).isFalse();
Bill Linef81cbd2018-07-05 17:48:49 +0800287 }
288
289 @Test
290 public void testTelephonyCapable_BootInitState_ServiceState_PowerOff() {
Jayachandran C041e7692019-12-20 16:20:02 -0800291 Intent intent = new Intent(Intent.ACTION_SERVICE_STATE);
Bill Linef81cbd2018-07-05 17:48:49 +0800292 Bundle data = new Bundle();
293 ServiceState state = new ServiceState();
294 state.setState(ServiceState.STATE_POWER_OFF);
295 state.fillInNotifierBundle(data);
Lucas Dupin3d053532019-01-29 12:35:22 -0800296 mKeyguardUpdateMonitor.mBroadcastReceiver.onReceive(getContext()
Bill Linef81cbd2018-07-05 17:48:49 +0800297 , putPhoneInfo(intent, data, false));
298 mTestableLooper.processAllMessages();
Lucas Dupin3d053532019-01-29 12:35:22 -0800299 assertThat(mKeyguardUpdateMonitor.mTelephonyCapable).isFalse();
Bill Linef81cbd2018-07-05 17:48:49 +0800300 }
301
302 @Test
303 public void testTelephonyCapable_SimValid_ServiceState_InService() {
Jayachandran C041e7692019-12-20 16:20:02 -0800304 Intent intent = new Intent(Intent.ACTION_SERVICE_STATE);
Bill Linef81cbd2018-07-05 17:48:49 +0800305 Bundle data = new Bundle();
306 ServiceState state = new ServiceState();
307 state.setState(ServiceState.STATE_IN_SERVICE);
308 state.fillInNotifierBundle(data);
Lucas Dupin3d053532019-01-29 12:35:22 -0800309 mKeyguardUpdateMonitor.mBroadcastReceiver.onReceive(getContext()
Bill Linef81cbd2018-07-05 17:48:49 +0800310 , putPhoneInfo(intent, data, true));
311 mTestableLooper.processAllMessages();
Lucas Dupin3d053532019-01-29 12:35:22 -0800312 assertThat(mKeyguardUpdateMonitor.mTelephonyCapable).isTrue();
Bill Linef81cbd2018-07-05 17:48:49 +0800313 }
314
315 @Test
316 public void testTelephonyCapable_SimValid_SimState_Loaded() {
Bill Linef81cbd2018-07-05 17:48:49 +0800317 Bundle data = new Bundle();
318 ServiceState state = new ServiceState();
319 state.setState(ServiceState.STATE_IN_SERVICE);
320 state.fillInNotifierBundle(data);
321 Intent intentSimState = new Intent(TelephonyIntents.ACTION_SIM_STATE_CHANGED);
Jayachandran C0173df82019-12-19 12:11:03 -0800322 intentSimState.putExtra(Intent.EXTRA_SIM_STATE
323 , Intent.SIM_STATE_LOADED);
Lucas Dupin3d053532019-01-29 12:35:22 -0800324 mKeyguardUpdateMonitor.mBroadcastReceiver.onReceive(getContext()
Bill Linef81cbd2018-07-05 17:48:49 +0800325 , putPhoneInfo(intentSimState, data, true));
326 mTestableLooper.processAllMessages();
Jayachandran C041e7692019-12-20 16:20:02 -0800327 // Even SimState Loaded, still need ACTION_SERVICE_STATE turn on mTelephonyCapable
Lucas Dupin3d053532019-01-29 12:35:22 -0800328 assertThat(mKeyguardUpdateMonitor.mTelephonyCapable).isFalse();
Bill Linef81cbd2018-07-05 17:48:49 +0800329
Jayachandran C041e7692019-12-20 16:20:02 -0800330 Intent intentServiceState = new Intent(Intent.ACTION_SERVICE_STATE);
Jayachandran C0173df82019-12-19 12:11:03 -0800331 intentSimState.putExtra(Intent.EXTRA_SIM_STATE
332 , Intent.SIM_STATE_LOADED);
Lucas Dupin3d053532019-01-29 12:35:22 -0800333 mKeyguardUpdateMonitor.mBroadcastReceiver.onReceive(getContext()
Bill Linef81cbd2018-07-05 17:48:49 +0800334 , putPhoneInfo(intentServiceState, data, true));
335 mTestableLooper.processAllMessages();
Lucas Dupin3d053532019-01-29 12:35:22 -0800336 assertThat(mKeyguardUpdateMonitor.mTelephonyCapable).isTrue();
337 }
338
339 @Test
340 public void testTriesToAuthenticate_whenBouncer() {
341 mKeyguardUpdateMonitor.sendKeyguardBouncerChanged(true);
342 mTestableLooper.processAllMessages();
343
Kevin Chyn8d2694a2019-04-11 18:30:40 -0700344 verify(mFaceManager).authenticate(any(), any(), anyInt(), any(), any(), anyInt());
Lucas Dupin3d053532019-01-29 12:35:22 -0800345 verify(mFaceManager).isHardwareDetected();
346 verify(mFaceManager).hasEnrolledTemplates(anyInt());
347 }
348
349 @Test
350 public void testTriesToAuthenticate_whenKeyguard() {
351 mKeyguardUpdateMonitor.dispatchStartedWakingUp();
352 mTestableLooper.processAllMessages();
353 mKeyguardUpdateMonitor.onKeyguardVisibilityChanged(true);
Kevin Chyn8d2694a2019-04-11 18:30:40 -0700354 verify(mFaceManager).authenticate(any(), any(), anyInt(), any(), any(), anyInt());
Lucas Dupin3d053532019-01-29 12:35:22 -0800355 }
356
357 @Test
358 public void skipsAuthentication_whenEncryptedKeyguard() {
Lucas Dupin8eec2682019-07-01 16:41:17 -0700359 when(mStrongAuthTracker.getStrongAuthForUser(anyInt())).thenReturn(
360 KeyguardUpdateMonitor.StrongAuthTracker.STRONG_AUTH_REQUIRED_AFTER_BOOT);
361 mKeyguardUpdateMonitor.setKeyguardBypassController(mKeyguardBypassController);
Lucas Dupin3d053532019-01-29 12:35:22 -0800362
363 mKeyguardUpdateMonitor.dispatchStartedWakingUp();
364 mTestableLooper.processAllMessages();
365 mKeyguardUpdateMonitor.onKeyguardVisibilityChanged(true);
Lucas Dupin8eec2682019-07-01 16:41:17 -0700366 verify(mFaceManager, never()).authenticate(any(), any(), anyInt(), any(), any(), anyInt());
367 }
368
369 @Test
370 public void requiresAuthentication_whenEncryptedKeyguard_andBypass() {
371 testStrongAuthExceptOnBouncer(
372 KeyguardUpdateMonitor.StrongAuthTracker.STRONG_AUTH_REQUIRED_AFTER_BOOT);
373 }
374
375 @Test
376 public void requiresAuthentication_whenTimeoutKeyguard_andBypass() {
377 testStrongAuthExceptOnBouncer(
378 KeyguardUpdateMonitor.StrongAuthTracker.STRONG_AUTH_REQUIRED_AFTER_TIMEOUT);
379 }
380
381 private void testStrongAuthExceptOnBouncer(int strongAuth) {
382 when(mKeyguardBypassController.canBypass()).thenReturn(true);
383 mKeyguardUpdateMonitor.setKeyguardBypassController(mKeyguardBypassController);
384 when(mStrongAuthTracker.getStrongAuthForUser(anyInt())).thenReturn(strongAuth);
385
386 mKeyguardUpdateMonitor.dispatchStartedWakingUp();
387 mTestableLooper.processAllMessages();
388 mKeyguardUpdateMonitor.onKeyguardVisibilityChanged(true);
389 verify(mFaceManager).authenticate(any(), any(), anyInt(), any(), any(), anyInt());
390
391 // Stop scanning when bouncer becomes visible
392 mKeyguardUpdateMonitor.sendKeyguardBouncerChanged(true /* showingBouncer */);
393 mTestableLooper.processAllMessages();
394 clearInvocations(mFaceManager);
395 mKeyguardUpdateMonitor.requestFaceAuth();
396 verify(mFaceManager, never()).authenticate(any(), any(), anyInt(), any(), any(), anyInt());
Lucas Dupin3d053532019-01-29 12:35:22 -0800397 }
398
399 @Test
400 public void testTriesToAuthenticate_whenAssistant() {
401 mKeyguardUpdateMonitor.setKeyguardOccluded(true);
402 mKeyguardUpdateMonitor.setAssistantVisible(true);
403
Kevin Chyn8d2694a2019-04-11 18:30:40 -0700404 verify(mFaceManager).authenticate(any(), any(), anyInt(), any(), any(), anyInt());
Lucas Dupin3d053532019-01-29 12:35:22 -0800405 }
406
407 @Test
Lucas Dupin4befb742019-07-01 11:31:31 -0700408 public void testTriesToAuthenticate_whenTrustOnAgentKeyguard_ifBypass() {
409 mKeyguardUpdateMonitor.setKeyguardBypassController(mKeyguardBypassController);
410 mKeyguardUpdateMonitor.dispatchStartedWakingUp();
411 mTestableLooper.processAllMessages();
412 when(mKeyguardBypassController.canBypass()).thenReturn(true);
413 mKeyguardUpdateMonitor.onTrustChanged(true /* enabled */,
414 KeyguardUpdateMonitor.getCurrentUser(), 0 /* flags */);
415 mKeyguardUpdateMonitor.onKeyguardVisibilityChanged(true);
416 verify(mFaceManager).authenticate(any(), any(), anyInt(), any(), any(), anyInt());
417 }
418
419 @Test
420 public void testIgnoresAuth_whenTrustAgentOnKeyguard_withoutBypass() {
421 mKeyguardUpdateMonitor.dispatchStartedWakingUp();
422 mTestableLooper.processAllMessages();
423 mKeyguardUpdateMonitor.onTrustChanged(true /* enabled */,
424 KeyguardUpdateMonitor.getCurrentUser(), 0 /* flags */);
425 mKeyguardUpdateMonitor.onKeyguardVisibilityChanged(true);
426 verify(mFaceManager, never()).authenticate(any(), any(), anyInt(), any(), any(), anyInt());
427 }
428
429 @Test
Lucas Dupin8eec2682019-07-01 16:41:17 -0700430 public void testIgnoresAuth_whenLockdown() {
431 mKeyguardUpdateMonitor.dispatchStartedWakingUp();
432 mTestableLooper.processAllMessages();
433 when(mStrongAuthTracker.getStrongAuthForUser(anyInt())).thenReturn(
434 KeyguardUpdateMonitor.StrongAuthTracker.STRONG_AUTH_REQUIRED_AFTER_USER_LOCKDOWN);
435
436 mKeyguardUpdateMonitor.onKeyguardVisibilityChanged(true);
437 verify(mFaceManager, never()).authenticate(any(), any(), anyInt(), any(), any(), anyInt());
438 }
439
440 @Test
Curtis Belmontec47df862020-01-28 16:58:34 -0800441 public void testTriesToAuthenticate_whenLockout() {
Lucas Dupin8eec2682019-07-01 16:41:17 -0700442 mKeyguardUpdateMonitor.dispatchStartedWakingUp();
443 mTestableLooper.processAllMessages();
444 when(mStrongAuthTracker.getStrongAuthForUser(anyInt())).thenReturn(
445 KeyguardUpdateMonitor.StrongAuthTracker.STRONG_AUTH_REQUIRED_AFTER_LOCKOUT);
446
447 mKeyguardUpdateMonitor.onKeyguardVisibilityChanged(true);
Curtis Belmontec47df862020-01-28 16:58:34 -0800448 verify(mFaceManager).authenticate(any(), any(), anyInt(), any(), any(), anyInt());
Lucas Dupin8eec2682019-07-01 16:41:17 -0700449 }
450
451 @Test
Lucas Dupin3d053532019-01-29 12:35:22 -0800452 public void testOnFaceAuthenticated_skipsFaceWhenAuthenticated() {
453 mKeyguardUpdateMonitor.onFaceAuthenticated(KeyguardUpdateMonitor.getCurrentUser());
454 mKeyguardUpdateMonitor.sendKeyguardBouncerChanged(true);
455 mTestableLooper.processAllMessages();
456
457 verify(mFaceManager, never()).authenticate(any(), any(), anyInt(), any(), any());
458 }
459
460 @Test
461 public void testGetUserCanSkipBouncer_whenFace() {
462 int user = KeyguardUpdateMonitor.getCurrentUser();
463 mKeyguardUpdateMonitor.onFaceAuthenticated(user);
464 assertThat(mKeyguardUpdateMonitor.getUserCanSkipBouncer(user)).isTrue();
465 }
466
467 @Test
468 public void testGetUserCanSkipBouncer_whenFingerprint() {
469 int user = KeyguardUpdateMonitor.getCurrentUser();
470 mKeyguardUpdateMonitor.onFingerprintAuthenticated(user);
471 assertThat(mKeyguardUpdateMonitor.getUserCanSkipBouncer(user)).isTrue();
472 }
473
474 @Test
475 public void testGetUserCanSkipBouncer_whenTrust() {
476 int user = KeyguardUpdateMonitor.getCurrentUser();
477 mKeyguardUpdateMonitor.onTrustChanged(true /* enabled */, user, 0 /* flags */);
478 assertThat(mKeyguardUpdateMonitor.getUserCanSkipBouncer(user)).isTrue();
Bill Linef81cbd2018-07-05 17:48:49 +0800479 }
480
Malcolm Chen5c63b512019-08-13 13:24:07 -0700481 @Test
482 public void testGetSubscriptionInfo_whenInGroupedSubWithOpportunistic() {
483 List<SubscriptionInfo> list = new ArrayList<>();
484 list.add(TEST_SUBSCRIPTION);
485 list.add(TEST_SUBSCRIPTION_2);
486 when(mSubscriptionManager.getActiveSubscriptionInfoList(anyBoolean())).thenReturn(list);
487 mKeyguardUpdateMonitor.mPhoneStateListener.onActiveDataSubscriptionIdChanged(
488 TEST_SUBSCRIPTION_2.getSubscriptionId());
489 mTestableLooper.processAllMessages();
490
491 List<SubscriptionInfo> listToVerify = mKeyguardUpdateMonitor
492 .getFilteredSubscriptionInfo(false);
493 assertThat(listToVerify.size()).isEqualTo(1);
494 assertThat(listToVerify.get(0)).isEqualTo(TEST_SUBSCRIPTION_2);
495 }
496
Lucas Dupin6c6d5732019-08-27 17:36:05 -0700497 @Test
498 public void testIsUserUnlocked() {
499 // mUserManager will report the user as unlocked on @Before
500 assertThat(mKeyguardUpdateMonitor.isUserUnlocked(KeyguardUpdateMonitor.getCurrentUser()))
501 .isTrue();
502 // Invalid user should not be unlocked.
503 int randomUser = 99;
504 assertThat(mKeyguardUpdateMonitor.isUserUnlocked(randomUser)).isFalse();
505 }
506
Lucas Dupinf2c53502019-10-03 13:56:18 -0700507 @Test
508 public void testTrustUsuallyManaged_whenTrustChanges() {
509 int user = KeyguardUpdateMonitor.getCurrentUser();
510 when(mTrustManager.isTrustUsuallyManaged(eq(user))).thenReturn(true);
511 mKeyguardUpdateMonitor.onTrustManagedChanged(false /* managed */, user);
512 assertThat(mKeyguardUpdateMonitor.isTrustUsuallyManaged(user)).isTrue();
513 }
514
515 @Test
516 public void testTrustUsuallyManaged_resetWhenUserIsRemoved() {
517 int user = KeyguardUpdateMonitor.getCurrentUser();
518 when(mTrustManager.isTrustUsuallyManaged(eq(user))).thenReturn(true);
519 mKeyguardUpdateMonitor.onTrustManagedChanged(false /* managed */, user);
520 assertThat(mKeyguardUpdateMonitor.isTrustUsuallyManaged(user)).isTrue();
521
522 mKeyguardUpdateMonitor.handleUserRemoved(user);
523 assertThat(mKeyguardUpdateMonitor.isTrustUsuallyManaged(user)).isFalse();
524 }
525
Yvonne Jiangb7024a22019-12-05 16:57:08 -0800526 @Test
527 public void testSecondaryLockscreenRequirement() {
528 int user = KeyguardUpdateMonitor.getCurrentUser();
529 String packageName = "fake.test.package";
530 String cls = "FakeService";
531 ServiceInfo serviceInfo = new ServiceInfo();
532 serviceInfo.packageName = packageName;
533 serviceInfo.name = cls;
534 ResolveInfo resolveInfo = new ResolveInfo();
535 resolveInfo.serviceInfo = serviceInfo;
536 when(mPackageManager.resolveService(any(Intent.class), eq(0))).thenReturn(resolveInfo);
537 when(mDevicePolicyManager.isSecondaryLockscreenEnabled(eq(user))).thenReturn(true, false);
538
539 // Initially null.
540 assertThat(mKeyguardUpdateMonitor.getSecondaryLockscreenRequirement(user)).isNull();
541
542 // Set non-null after DPM change.
543 setBroadcastReceiverPendingResult(mKeyguardUpdateMonitor.mBroadcastAllReceiver);
544 Intent intent = new Intent(DevicePolicyManager.ACTION_DEVICE_POLICY_MANAGER_STATE_CHANGED);
545 mKeyguardUpdateMonitor.mBroadcastAllReceiver.onReceive(getContext(), intent);
546 mTestableLooper.processAllMessages();
547
548 Intent storedIntent = mKeyguardUpdateMonitor.getSecondaryLockscreenRequirement(user);
549 assertThat(storedIntent.getComponent().getClassName()).isEqualTo(cls);
550 assertThat(storedIntent.getComponent().getPackageName()).isEqualTo(packageName);
551
552 // Back to null after another DPM change.
553 mKeyguardUpdateMonitor.mBroadcastAllReceiver.onReceive(getContext(), intent);
554 mTestableLooper.processAllMessages();
555 assertThat(mKeyguardUpdateMonitor.getSecondaryLockscreenRequirement(user)).isNull();
556 }
557
558 private void setBroadcastReceiverPendingResult(BroadcastReceiver receiver) {
559 BroadcastReceiver.PendingResult pendingResult =
560 new BroadcastReceiver.PendingResult(Activity.RESULT_OK,
561 "resultData",
562 /* resultExtras= */ null,
563 BroadcastReceiver.PendingResult.TYPE_UNREGISTERED,
564 /* ordered= */ true,
565 /* sticky= */ false,
566 /* token= */ null,
567 UserHandle.myUserId(),
568 /* flags= */ 0);
569 receiver.setPendingResult(pendingResult);
570 }
571
Bill Linef81cbd2018-07-05 17:48:49 +0800572 private Intent putPhoneInfo(Intent intent, Bundle data, Boolean simInited) {
573 int subscription = simInited
574 ? 1/* mock subid=1 */ : SubscriptionManager.DUMMY_SUBSCRIPTION_ID_BASE;
575 if (data != null) intent.putExtras(data);
Daniel Bright63514be2020-01-15 12:10:53 -0800576
577 intent.putExtra(SubscriptionManager.EXTRA_SUBSCRIPTION_INDEX, subscription);
578 intent.putExtra(SubscriptionManager.EXTRA_SLOT_INDEX, 0);
Bill Linef81cbd2018-07-05 17:48:49 +0800579 return intent;
580 }
581
Lucas Dupin7ff82b02018-05-16 12:14:10 -0700582 private class TestableKeyguardUpdateMonitor extends KeyguardUpdateMonitor {
583 AtomicBoolean mSimStateChanged = new AtomicBoolean(false);
584
585 protected TestableKeyguardUpdateMonitor(Context context) {
Fabian Kozynski5ca7a512019-10-16 19:56:11 +0000586 super(context,
587 TestableLooper.get(KeyguardUpdateMonitorTest.this).getLooper(),
588 mBroadcastDispatcher, mDumpController);
Lucas Dupin3d053532019-01-29 12:35:22 -0800589 mStrongAuthTracker = KeyguardUpdateMonitorTest.this.mStrongAuthTracker;
Lucas Dupin7ff82b02018-05-16 12:14:10 -0700590 }
591
592 public boolean hasSimStateJustChanged() {
593 return mSimStateChanged.getAndSet(false);
594 }
595
596 @Override
Jayachandran Cf5436a62019-11-08 18:22:45 -0800597 protected void handleSimStateChange(int subId, int slotId, int state) {
Lucas Dupin7ff82b02018-05-16 12:14:10 -0700598 mSimStateChanged.set(true);
599 super.handleSimStateChange(subId, slotId, state);
600 }
Lucas Dupin5e0f0d22018-02-26 13:32:16 -0800601 }
602}