blob: 795cbb92fefe159700f2347e0cfbaccbf8b5dc73 [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;
25import static org.mockito.ArgumentMatchers.anyInt;
26import static org.mockito.ArgumentMatchers.anyString;
Lucas Dupinf2c53502019-10-03 13:56:18 -070027import static org.mockito.ArgumentMatchers.eq;
Fabian Kozynski5ca7a512019-10-16 19:56:11 +000028import static org.mockito.Mockito.atLeastOnce;
Lucas Dupin8eec2682019-07-01 16:41:17 -070029import static org.mockito.Mockito.clearInvocations;
Lucas Dupin3d053532019-01-29 12:35:22 -080030import static org.mockito.Mockito.doAnswer;
31import static org.mockito.Mockito.never;
Lucas Dupin3d053532019-01-29 12:35:22 -080032import static org.mockito.Mockito.spy;
33import static org.mockito.Mockito.verify;
34import static org.mockito.Mockito.when;
Bill Linef81cbd2018-07-05 17:48:49 +080035
Yvonne Jiangb7024a22019-12-05 16:57:08 -080036import android.app.Activity;
Lucas Dupin3d053532019-01-29 12:35:22 -080037import android.app.admin.DevicePolicyManager;
38import android.app.trust.TrustManager;
Yvonne Jiangb7024a22019-12-05 16:57:08 -080039import android.content.BroadcastReceiver;
Lucas Dupin7ff82b02018-05-16 12:14:10 -070040import android.content.Context;
Lucas Dupin5e0f0d22018-02-26 13:32:16 -080041import android.content.Intent;
Fabian Kozynski5ca7a512019-10-16 19:56:11 +000042import android.content.IntentFilter;
Lucas Dupin3d053532019-01-29 12:35:22 -080043import android.content.pm.PackageManager;
Yvonne Jiangb7024a22019-12-05 16:57:08 -080044import android.content.pm.ResolveInfo;
45import android.content.pm.ServiceInfo;
Lucas Dupin3d053532019-01-29 12:35:22 -080046import android.hardware.biometrics.BiometricManager;
47import android.hardware.biometrics.BiometricSourceType;
48import android.hardware.biometrics.IBiometricEnabledOnKeyguardCallback;
49import android.hardware.face.FaceManager;
50import android.hardware.fingerprint.FingerprintManager;
Bill Linef81cbd2018-07-05 17:48:49 +080051import android.os.Bundle;
Fabian Kozynski5ca7a512019-10-16 19:56:11 +000052import android.os.Handler;
53import android.os.UserHandle;
Lucas Dupin3d053532019-01-29 12:35:22 -080054import android.os.UserManager;
Bill Linef81cbd2018-07-05 17:48:49 +080055import android.telephony.ServiceState;
Malcolm Chen5c63b512019-08-13 13:24:07 -070056import android.telephony.SubscriptionInfo;
Bill Linef81cbd2018-07-05 17:48:49 +080057import android.telephony.SubscriptionManager;
Lucas Dupin5e0f0d22018-02-26 13:32:16 -080058import android.test.suitebuilder.annotation.SmallTest;
59import android.testing.AndroidTestingRunner;
Lucas Dupin3d053532019-01-29 12:35:22 -080060import android.testing.TestableContext;
Lucas Dupin5e0f0d22018-02-26 13:32:16 -080061import android.testing.TestableLooper;
Lucas Dupin5e0f0d22018-02-26 13:32:16 -080062
Lucas Dupin5e0f0d22018-02-26 13:32:16 -080063import com.android.internal.telephony.TelephonyIntents;
Lucas Dupin64171fe2019-10-30 14:28:29 -070064import com.android.systemui.DumpController;
Lucas Dupin5e0f0d22018-02-26 13:32:16 -080065import com.android.systemui.SysuiTestCase;
Fabian Kozynski5ca7a512019-10-16 19:56:11 +000066import com.android.systemui.broadcast.BroadcastDispatcher;
Lucas Dupin4befb742019-07-01 11:31:31 -070067import com.android.systemui.statusbar.phone.KeyguardBypassController;
Lucas Dupin5e0f0d22018-02-26 13:32:16 -080068
69import org.junit.Assert;
70import org.junit.Before;
71import org.junit.Test;
72import org.junit.runner.RunWith;
Lucas Dupin3d053532019-01-29 12:35:22 -080073import org.mockito.Mock;
74import org.mockito.MockitoAnnotations;
Lucas Dupin5e0f0d22018-02-26 13:32:16 -080075
Malcolm Chen5c63b512019-08-13 13:24:07 -070076import java.util.ArrayList;
77import java.util.List;
Lucas Dupin5e0f0d22018-02-26 13:32:16 -080078import java.util.concurrent.atomic.AtomicBoolean;
79
80@SmallTest
81@RunWith(AndroidTestingRunner.class)
Dave Mankoffe2294692019-08-14 11:53:13 -040082@TestableLooper.RunWithLooper
Lucas Dupin5e0f0d22018-02-26 13:32:16 -080083public class KeyguardUpdateMonitorTest extends SysuiTestCase {
Malcolm Chen5c63b512019-08-13 13:24:07 -070084 private static final String TEST_CARRIER = "TEST_CARRIER";
85 private static final String TEST_CARRIER_2 = "TEST_CARRIER_2";
86 private static final int TEST_CARRIER_ID = 1;
87 private static final String TEST_GROUP_UUID = "59b5c870-fc4c-47a4-a99e-9db826b48b24";
88 private static final SubscriptionInfo TEST_SUBSCRIPTION = new SubscriptionInfo(1, "", 0,
Peter Wang37916d62019-11-25 16:50:50 -080089 TEST_CARRIER, TEST_CARRIER, NAME_SOURCE_DEFAULT, 0xFFFFFF, "",
Malcolm Chen5c63b512019-08-13 13:24:07 -070090 DATA_ROAMING_DISABLE, null, null, null, null, false, null, "", false, TEST_GROUP_UUID,
91 TEST_CARRIER_ID, 0);
92 private static final SubscriptionInfo TEST_SUBSCRIPTION_2 = new SubscriptionInfo(2, "", 0,
Peter Wang37916d62019-11-25 16:50:50 -080093 TEST_CARRIER, TEST_CARRIER_2, NAME_SOURCE_DEFAULT, 0xFFFFFF, "",
Malcolm Chen5c63b512019-08-13 13:24:07 -070094 DATA_ROAMING_DISABLE, null, null, null, null, false, null, "", true, TEST_GROUP_UUID,
95 TEST_CARRIER_ID, 0);
Lucas Dupin3d053532019-01-29 12:35:22 -080096 @Mock
Lucas Dupin64171fe2019-10-30 14:28:29 -070097 private DumpController mDumpController;
98 @Mock
Lucas Dupin3d053532019-01-29 12:35:22 -080099 private KeyguardUpdateMonitor.StrongAuthTracker mStrongAuthTracker;
100 @Mock
101 private TrustManager mTrustManager;
102 @Mock
103 private FingerprintManager mFingerprintManager;
104 @Mock
105 private FaceManager mFaceManager;
106 @Mock
107 private BiometricManager mBiometricManager;
108 @Mock
109 private PackageManager mPackageManager;
110 @Mock
111 private UserManager mUserManager;
112 @Mock
113 private DevicePolicyManager mDevicePolicyManager;
Lucas Dupin4befb742019-07-01 11:31:31 -0700114 @Mock
115 private KeyguardBypassController mKeyguardBypassController;
Malcolm Chen5c63b512019-08-13 13:24:07 -0700116 @Mock
117 private SubscriptionManager mSubscriptionManager;
Fabian Kozynski5ca7a512019-10-16 19:56:11 +0000118 @Mock
119 private BroadcastDispatcher mBroadcastDispatcher;
Lucas Dupin5e0f0d22018-02-26 13:32:16 -0800120 private TestableLooper mTestableLooper;
Lucas Dupin3d053532019-01-29 12:35:22 -0800121 private TestableKeyguardUpdateMonitor mKeyguardUpdateMonitor;
Lucas Dupin5e0f0d22018-02-26 13:32:16 -0800122
123 @Before
124 public void setup() {
Lucas Dupin3d053532019-01-29 12:35:22 -0800125 MockitoAnnotations.initMocks(this);
126 TestableContext context = spy(mContext);
127 when(mPackageManager.hasSystemFeature(anyString())).thenReturn(true);
128 when(context.getPackageManager()).thenReturn(mPackageManager);
129 doAnswer(invocation -> {
130 IBiometricEnabledOnKeyguardCallback callback = invocation.getArgument(0);
Lucas Dupin7d95f152019-07-17 16:25:54 -0700131 callback.onChanged(BiometricSourceType.FACE, true /* enabled */,
132 KeyguardUpdateMonitor.getCurrentUser());
Lucas Dupin3d053532019-01-29 12:35:22 -0800133 return null;
134 }).when(mBiometricManager).registerEnabledOnKeyguardCallback(any());
135 when(mFaceManager.isHardwareDetected()).thenReturn(true);
136 when(mFaceManager.hasEnrolledTemplates()).thenReturn(true);
137 when(mFaceManager.hasEnrolledTemplates(anyInt())).thenReturn(true);
138 when(mUserManager.isUserUnlocked(anyInt())).thenReturn(true);
Kevin Chynfdfd2d32019-03-01 14:52:15 -0800139 when(mUserManager.isPrimaryUser()).thenReturn(true);
Lucas Dupin3d053532019-01-29 12:35:22 -0800140 when(mStrongAuthTracker.isUnlockingWithBiometricAllowed()).thenReturn(true);
141 context.addMockSystemService(TrustManager.class, mTrustManager);
142 context.addMockSystemService(FingerprintManager.class, mFingerprintManager);
143 context.addMockSystemService(BiometricManager.class, mBiometricManager);
144 context.addMockSystemService(FaceManager.class, mFaceManager);
145 context.addMockSystemService(UserManager.class, mUserManager);
146 context.addMockSystemService(DevicePolicyManager.class, mDevicePolicyManager);
Malcolm Chen5c63b512019-08-13 13:24:07 -0700147 context.addMockSystemService(SubscriptionManager.class, mSubscriptionManager);
Lucas Dupin3d053532019-01-29 12:35:22 -0800148
Lucas Dupin5e0f0d22018-02-26 13:32:16 -0800149 mTestableLooper = TestableLooper.get(this);
Lucas Dupin3d053532019-01-29 12:35:22 -0800150 mKeyguardUpdateMonitor = new TestableKeyguardUpdateMonitor(context);
Lucas Dupin5e0f0d22018-02-26 13:32:16 -0800151 }
152
153 @Test
Fabian Kozynski5ca7a512019-10-16 19:56:11 +0000154 public void testReceiversRegistered() {
Fabian Kozynski5e92c6f2020-01-03 13:56:17 -0500155 verify(mBroadcastDispatcher, atLeastOnce()).registerReceiverWithHandler(
Fabian Kozynski5ca7a512019-10-16 19:56:11 +0000156 eq(mKeyguardUpdateMonitor.mBroadcastReceiver),
157 any(IntentFilter.class), any(Handler.class));
Fabian Kozynski5e92c6f2020-01-03 13:56:17 -0500158 verify(mBroadcastDispatcher, atLeastOnce()).registerReceiverWithHandler(
Fabian Kozynski5ca7a512019-10-16 19:56:11 +0000159 eq(mKeyguardUpdateMonitor.mBroadcastAllReceiver),
160 any(IntentFilter.class), any(Handler.class), eq(UserHandle.ALL));
161 }
162
163 @Test
Lucas Dupin5e0f0d22018-02-26 13:32:16 -0800164 public void testIgnoresSimStateCallback_rebroadcast() {
165 Intent intent = new Intent(TelephonyIntents.ACTION_SIM_STATE_CHANGED);
166
Lucas Dupin3d053532019-01-29 12:35:22 -0800167 mKeyguardUpdateMonitor.mBroadcastReceiver.onReceive(getContext(), intent);
Lucas Dupin5e0f0d22018-02-26 13:32:16 -0800168 mTestableLooper.processAllMessages();
Lucas Dupin7ff82b02018-05-16 12:14:10 -0700169 Assert.assertTrue("onSimStateChanged not called",
Lucas Dupin3d053532019-01-29 12:35:22 -0800170 mKeyguardUpdateMonitor.hasSimStateJustChanged());
Lucas Dupin5e0f0d22018-02-26 13:32:16 -0800171
Meng Wang0e8d91a2020-01-17 14:58:01 -0800172 intent.putExtra(Intent.EXTRA_REBROADCAST_ON_UNLOCK, true);
Lucas Dupin3d053532019-01-29 12:35:22 -0800173 mKeyguardUpdateMonitor.mBroadcastReceiver.onReceive(getContext(), intent);
Lucas Dupin5e0f0d22018-02-26 13:32:16 -0800174 mTestableLooper.processAllMessages();
Lucas Dupin7ff82b02018-05-16 12:14:10 -0700175 Assert.assertFalse("onSimStateChanged should have been skipped",
Lucas Dupin3d053532019-01-29 12:35:22 -0800176 mKeyguardUpdateMonitor.hasSimStateJustChanged());
Lucas Dupin7ff82b02018-05-16 12:14:10 -0700177 }
178
Bill Linef81cbd2018-07-05 17:48:49 +0800179 @Test
180 public void testTelephonyCapable_BootInitState() {
Lucas Dupin3d053532019-01-29 12:35:22 -0800181 assertThat(mKeyguardUpdateMonitor.mTelephonyCapable).isFalse();
Bill Linef81cbd2018-07-05 17:48:49 +0800182 }
183
184 @Test
185 public void testTelephonyCapable_SimState_Absent() {
186 Intent intent = new Intent(TelephonyIntents.ACTION_SIM_STATE_CHANGED);
Jayachandran C0173df82019-12-19 12:11:03 -0800187 intent.putExtra(Intent.EXTRA_SIM_STATE,
188 Intent.SIM_STATE_ABSENT);
andychou695a7602019-05-16 23:14:00 +0800189 mKeyguardUpdateMonitor.mBroadcastReceiver.onReceive(getContext(),
190 putPhoneInfo(intent, null, false));
191 mTestableLooper.processAllMessages();
192 assertThat(mKeyguardUpdateMonitor.mTelephonyCapable).isTrue();
193 }
194
195 @Test
196 public void testTelephonyCapable_SimState_CardIOError() {
197 Intent intent = new Intent(TelephonyIntents.ACTION_SIM_STATE_CHANGED);
Jayachandran C0173df82019-12-19 12:11:03 -0800198 intent.putExtra(Intent.EXTRA_SIM_STATE,
199 Intent.SIM_STATE_CARD_IO_ERROR);
andychou695a7602019-05-16 23:14:00 +0800200 mKeyguardUpdateMonitor.mBroadcastReceiver.onReceive(getContext(),
201 putPhoneInfo(intent, null, false));
Bill Linef81cbd2018-07-05 17:48:49 +0800202 mTestableLooper.processAllMessages();
Lucas Dupin3d053532019-01-29 12:35:22 -0800203 assertThat(mKeyguardUpdateMonitor.mTelephonyCapable).isTrue();
Bill Linef81cbd2018-07-05 17:48:49 +0800204 }
205
206 @Test
207 public void testTelephonyCapable_SimInvalid_ServiceState_InService() {
208 // SERVICE_STATE - IN_SERVICE, but SIM_STATE is invalid TelephonyCapable should be False
Jayachandran C041e7692019-12-20 16:20:02 -0800209 Intent intent = new Intent(Intent.ACTION_SERVICE_STATE);
Bill Linef81cbd2018-07-05 17:48:49 +0800210 Bundle data = new Bundle();
211 ServiceState state = new ServiceState();
212 state.setState(ServiceState.STATE_IN_SERVICE);
213 state.fillInNotifierBundle(data);
Lucas Dupin3d053532019-01-29 12:35:22 -0800214 mKeyguardUpdateMonitor.mBroadcastReceiver.onReceive(getContext()
Bill Linef81cbd2018-07-05 17:48:49 +0800215 , putPhoneInfo(intent, data, false));
216 mTestableLooper.processAllMessages();
Lucas Dupin3d053532019-01-29 12:35:22 -0800217 assertThat(mKeyguardUpdateMonitor.mTelephonyCapable).isFalse();
Bill Linef81cbd2018-07-05 17:48:49 +0800218 }
219
220 @Test
221 public void testTelephonyCapable_SimValid_ServiceState_PowerOff() {
222 // Simulate AirplaneMode case, SERVICE_STATE - POWER_OFF, check TelephonyCapable False
223 // Only receive ServiceState callback IN_SERVICE -> OUT_OF_SERVICE -> POWER_OFF
Jayachandran C041e7692019-12-20 16:20:02 -0800224 Intent intent = new Intent(Intent.ACTION_SERVICE_STATE);
Jayachandran C0173df82019-12-19 12:11:03 -0800225 intent.putExtra(Intent.EXTRA_SIM_STATE
226 , Intent.SIM_STATE_LOADED);
Bill Linef81cbd2018-07-05 17:48:49 +0800227 Bundle data = new Bundle();
228 ServiceState state = new ServiceState();
229 state.setState(ServiceState.STATE_POWER_OFF);
230 state.fillInNotifierBundle(data);
Lucas Dupin3d053532019-01-29 12:35:22 -0800231 mKeyguardUpdateMonitor.mBroadcastReceiver.onReceive(getContext()
Bill Linef81cbd2018-07-05 17:48:49 +0800232 , putPhoneInfo(intent, data, true));
233 mTestableLooper.processAllMessages();
Lucas Dupin3d053532019-01-29 12:35:22 -0800234 assertThat(mKeyguardUpdateMonitor.mTelephonyCapable).isTrue();
Bill Linef81cbd2018-07-05 17:48:49 +0800235 }
236
237 /* Normal SIM inserted flow
238 * ServiceState: ---OutOfServie----->PowerOff->OutOfServie--->InService
239 * SimState: ----NOT_READY---->READY----------------------LOADED>>>
240 * Subscription: --------null---->null--->"Chunghwa Telecom"-------->>>
241 * System: -------------------------------BOOT_COMPLETED------>>>
242 * TelephonyCapable:(F)-(F)-(F)-(F)-(F)-(F)-(F)-(F)-(F)-(F)------(T)-(T)>>
243 */
244 @Test
245 public void testTelephonyCapable_BootInitState_ServiceState_OutOfService() {
Jayachandran C041e7692019-12-20 16:20:02 -0800246 Intent intent = new Intent(Intent.ACTION_SERVICE_STATE);
Bill Linef81cbd2018-07-05 17:48:49 +0800247 Bundle data = new Bundle();
248 ServiceState state = new ServiceState();
249 state.setState(ServiceState.STATE_OUT_OF_SERVICE);
250 state.fillInNotifierBundle(data);
251 intent.putExtras(data);
Lucas Dupin3d053532019-01-29 12:35:22 -0800252 mKeyguardUpdateMonitor.mBroadcastReceiver.onReceive(getContext()
Bill Linef81cbd2018-07-05 17:48:49 +0800253 , putPhoneInfo(intent, data, false));
254 mTestableLooper.processAllMessages();
Lucas Dupin3d053532019-01-29 12:35:22 -0800255 assertThat(mKeyguardUpdateMonitor.mTelephonyCapable).isFalse();
Bill Linef81cbd2018-07-05 17:48:49 +0800256 }
257
258 @Test
259 public void testTelephonyCapable_BootInitState_SimState_NotReady() {
Bill Linef81cbd2018-07-05 17:48:49 +0800260 Bundle data = new Bundle();
261 ServiceState state = new ServiceState();
262 state.setState(ServiceState.STATE_OUT_OF_SERVICE);
263 state.fillInNotifierBundle(data);
264 Intent intent = new Intent(TelephonyIntents.ACTION_SIM_STATE_CHANGED);
Jayachandran C0173df82019-12-19 12:11:03 -0800265 intent.putExtra(Intent.EXTRA_SIM_STATE
266 , Intent.SIM_STATE_NOT_READY);
Lucas Dupin3d053532019-01-29 12:35:22 -0800267 mKeyguardUpdateMonitor.mBroadcastReceiver.onReceive(getContext()
Bill Linef81cbd2018-07-05 17:48:49 +0800268 , putPhoneInfo(intent, data, false));
269 mTestableLooper.processAllMessages();
Lucas Dupin3d053532019-01-29 12:35:22 -0800270 assertThat(mKeyguardUpdateMonitor.mTelephonyCapable).isFalse();
Bill Linef81cbd2018-07-05 17:48:49 +0800271 }
272
273 @Test
274 public void testTelephonyCapable_BootInitState_SimState_Ready() {
Bill Linef81cbd2018-07-05 17:48:49 +0800275 Bundle data = new Bundle();
276 ServiceState state = new ServiceState();
277 state.setState(ServiceState.STATE_OUT_OF_SERVICE);
278 state.fillInNotifierBundle(data);
279 Intent intent = new Intent(TelephonyIntents.ACTION_SIM_STATE_CHANGED);
Jayachandran C0173df82019-12-19 12:11:03 -0800280 intent.putExtra(Intent.EXTRA_SIM_STATE
281 , Intent.SIM_STATE_READY);
Lucas Dupin3d053532019-01-29 12:35:22 -0800282 mKeyguardUpdateMonitor.mBroadcastReceiver.onReceive(getContext()
Bill Linef81cbd2018-07-05 17:48:49 +0800283 , putPhoneInfo(intent, data, false));
284 mTestableLooper.processAllMessages();
Lucas Dupin3d053532019-01-29 12:35:22 -0800285 assertThat(mKeyguardUpdateMonitor.mTelephonyCapable).isFalse();
Bill Linef81cbd2018-07-05 17:48:49 +0800286 }
287
288 @Test
289 public void testTelephonyCapable_BootInitState_ServiceState_PowerOff() {
Jayachandran C041e7692019-12-20 16:20:02 -0800290 Intent intent = new Intent(Intent.ACTION_SERVICE_STATE);
Bill Linef81cbd2018-07-05 17:48:49 +0800291 Bundle data = new Bundle();
292 ServiceState state = new ServiceState();
293 state.setState(ServiceState.STATE_POWER_OFF);
294 state.fillInNotifierBundle(data);
Lucas Dupin3d053532019-01-29 12:35:22 -0800295 mKeyguardUpdateMonitor.mBroadcastReceiver.onReceive(getContext()
Bill Linef81cbd2018-07-05 17:48:49 +0800296 , putPhoneInfo(intent, data, false));
297 mTestableLooper.processAllMessages();
Lucas Dupin3d053532019-01-29 12:35:22 -0800298 assertThat(mKeyguardUpdateMonitor.mTelephonyCapable).isFalse();
Bill Linef81cbd2018-07-05 17:48:49 +0800299 }
300
301 @Test
302 public void testTelephonyCapable_SimValid_ServiceState_InService() {
Jayachandran C041e7692019-12-20 16:20:02 -0800303 Intent intent = new Intent(Intent.ACTION_SERVICE_STATE);
Bill Linef81cbd2018-07-05 17:48:49 +0800304 Bundle data = new Bundle();
305 ServiceState state = new ServiceState();
306 state.setState(ServiceState.STATE_IN_SERVICE);
307 state.fillInNotifierBundle(data);
Lucas Dupin3d053532019-01-29 12:35:22 -0800308 mKeyguardUpdateMonitor.mBroadcastReceiver.onReceive(getContext()
Bill Linef81cbd2018-07-05 17:48:49 +0800309 , putPhoneInfo(intent, data, true));
310 mTestableLooper.processAllMessages();
Lucas Dupin3d053532019-01-29 12:35:22 -0800311 assertThat(mKeyguardUpdateMonitor.mTelephonyCapable).isTrue();
Bill Linef81cbd2018-07-05 17:48:49 +0800312 }
313
314 @Test
315 public void testTelephonyCapable_SimValid_SimState_Loaded() {
Bill Linef81cbd2018-07-05 17:48:49 +0800316 Bundle data = new Bundle();
317 ServiceState state = new ServiceState();
318 state.setState(ServiceState.STATE_IN_SERVICE);
319 state.fillInNotifierBundle(data);
320 Intent intentSimState = new Intent(TelephonyIntents.ACTION_SIM_STATE_CHANGED);
Jayachandran C0173df82019-12-19 12:11:03 -0800321 intentSimState.putExtra(Intent.EXTRA_SIM_STATE
322 , Intent.SIM_STATE_LOADED);
Lucas Dupin3d053532019-01-29 12:35:22 -0800323 mKeyguardUpdateMonitor.mBroadcastReceiver.onReceive(getContext()
Bill Linef81cbd2018-07-05 17:48:49 +0800324 , putPhoneInfo(intentSimState, data, true));
325 mTestableLooper.processAllMessages();
Jayachandran C041e7692019-12-20 16:20:02 -0800326 // Even SimState Loaded, still need ACTION_SERVICE_STATE turn on mTelephonyCapable
Lucas Dupin3d053532019-01-29 12:35:22 -0800327 assertThat(mKeyguardUpdateMonitor.mTelephonyCapable).isFalse();
Bill Linef81cbd2018-07-05 17:48:49 +0800328
Jayachandran C041e7692019-12-20 16:20:02 -0800329 Intent intentServiceState = new Intent(Intent.ACTION_SERVICE_STATE);
Jayachandran C0173df82019-12-19 12:11:03 -0800330 intentSimState.putExtra(Intent.EXTRA_SIM_STATE
331 , Intent.SIM_STATE_LOADED);
Lucas Dupin3d053532019-01-29 12:35:22 -0800332 mKeyguardUpdateMonitor.mBroadcastReceiver.onReceive(getContext()
Bill Linef81cbd2018-07-05 17:48:49 +0800333 , putPhoneInfo(intentServiceState, data, true));
334 mTestableLooper.processAllMessages();
Lucas Dupin3d053532019-01-29 12:35:22 -0800335 assertThat(mKeyguardUpdateMonitor.mTelephonyCapable).isTrue();
336 }
337
338 @Test
339 public void testTriesToAuthenticate_whenBouncer() {
340 mKeyguardUpdateMonitor.sendKeyguardBouncerChanged(true);
341 mTestableLooper.processAllMessages();
342
Kevin Chyn8d2694a2019-04-11 18:30:40 -0700343 verify(mFaceManager).authenticate(any(), any(), anyInt(), any(), any(), anyInt());
Lucas Dupin3d053532019-01-29 12:35:22 -0800344 verify(mFaceManager).isHardwareDetected();
345 verify(mFaceManager).hasEnrolledTemplates(anyInt());
346 }
347
348 @Test
349 public void testTriesToAuthenticate_whenKeyguard() {
350 mKeyguardUpdateMonitor.dispatchStartedWakingUp();
351 mTestableLooper.processAllMessages();
352 mKeyguardUpdateMonitor.onKeyguardVisibilityChanged(true);
Kevin Chyn8d2694a2019-04-11 18:30:40 -0700353 verify(mFaceManager).authenticate(any(), any(), anyInt(), any(), any(), anyInt());
Lucas Dupin3d053532019-01-29 12:35:22 -0800354 }
355
356 @Test
357 public void skipsAuthentication_whenEncryptedKeyguard() {
Lucas Dupin8eec2682019-07-01 16:41:17 -0700358 when(mStrongAuthTracker.getStrongAuthForUser(anyInt())).thenReturn(
359 KeyguardUpdateMonitor.StrongAuthTracker.STRONG_AUTH_REQUIRED_AFTER_BOOT);
360 mKeyguardUpdateMonitor.setKeyguardBypassController(mKeyguardBypassController);
Lucas Dupin3d053532019-01-29 12:35:22 -0800361
362 mKeyguardUpdateMonitor.dispatchStartedWakingUp();
363 mTestableLooper.processAllMessages();
364 mKeyguardUpdateMonitor.onKeyguardVisibilityChanged(true);
Lucas Dupin8eec2682019-07-01 16:41:17 -0700365 verify(mFaceManager, never()).authenticate(any(), any(), anyInt(), any(), any(), anyInt());
366 }
367
368 @Test
369 public void requiresAuthentication_whenEncryptedKeyguard_andBypass() {
370 testStrongAuthExceptOnBouncer(
371 KeyguardUpdateMonitor.StrongAuthTracker.STRONG_AUTH_REQUIRED_AFTER_BOOT);
372 }
373
374 @Test
375 public void requiresAuthentication_whenTimeoutKeyguard_andBypass() {
376 testStrongAuthExceptOnBouncer(
377 KeyguardUpdateMonitor.StrongAuthTracker.STRONG_AUTH_REQUIRED_AFTER_TIMEOUT);
378 }
379
380 private void testStrongAuthExceptOnBouncer(int strongAuth) {
381 when(mKeyguardBypassController.canBypass()).thenReturn(true);
382 mKeyguardUpdateMonitor.setKeyguardBypassController(mKeyguardBypassController);
383 when(mStrongAuthTracker.getStrongAuthForUser(anyInt())).thenReturn(strongAuth);
384
385 mKeyguardUpdateMonitor.dispatchStartedWakingUp();
386 mTestableLooper.processAllMessages();
387 mKeyguardUpdateMonitor.onKeyguardVisibilityChanged(true);
388 verify(mFaceManager).authenticate(any(), any(), anyInt(), any(), any(), anyInt());
389
390 // Stop scanning when bouncer becomes visible
391 mKeyguardUpdateMonitor.sendKeyguardBouncerChanged(true /* showingBouncer */);
392 mTestableLooper.processAllMessages();
393 clearInvocations(mFaceManager);
394 mKeyguardUpdateMonitor.requestFaceAuth();
395 verify(mFaceManager, never()).authenticate(any(), any(), anyInt(), any(), any(), anyInt());
Lucas Dupin3d053532019-01-29 12:35:22 -0800396 }
397
398 @Test
399 public void testTriesToAuthenticate_whenAssistant() {
400 mKeyguardUpdateMonitor.setKeyguardOccluded(true);
401 mKeyguardUpdateMonitor.setAssistantVisible(true);
402
Kevin Chyn8d2694a2019-04-11 18:30:40 -0700403 verify(mFaceManager).authenticate(any(), any(), anyInt(), any(), any(), anyInt());
Lucas Dupin3d053532019-01-29 12:35:22 -0800404 }
405
406 @Test
Lucas Dupin4befb742019-07-01 11:31:31 -0700407 public void testTriesToAuthenticate_whenTrustOnAgentKeyguard_ifBypass() {
408 mKeyguardUpdateMonitor.setKeyguardBypassController(mKeyguardBypassController);
409 mKeyguardUpdateMonitor.dispatchStartedWakingUp();
410 mTestableLooper.processAllMessages();
411 when(mKeyguardBypassController.canBypass()).thenReturn(true);
412 mKeyguardUpdateMonitor.onTrustChanged(true /* enabled */,
413 KeyguardUpdateMonitor.getCurrentUser(), 0 /* flags */);
414 mKeyguardUpdateMonitor.onKeyguardVisibilityChanged(true);
415 verify(mFaceManager).authenticate(any(), any(), anyInt(), any(), any(), anyInt());
416 }
417
418 @Test
419 public void testIgnoresAuth_whenTrustAgentOnKeyguard_withoutBypass() {
420 mKeyguardUpdateMonitor.dispatchStartedWakingUp();
421 mTestableLooper.processAllMessages();
422 mKeyguardUpdateMonitor.onTrustChanged(true /* enabled */,
423 KeyguardUpdateMonitor.getCurrentUser(), 0 /* flags */);
424 mKeyguardUpdateMonitor.onKeyguardVisibilityChanged(true);
425 verify(mFaceManager, never()).authenticate(any(), any(), anyInt(), any(), any(), anyInt());
426 }
427
428 @Test
Lucas Dupin8eec2682019-07-01 16:41:17 -0700429 public void testIgnoresAuth_whenLockdown() {
430 mKeyguardUpdateMonitor.dispatchStartedWakingUp();
431 mTestableLooper.processAllMessages();
432 when(mStrongAuthTracker.getStrongAuthForUser(anyInt())).thenReturn(
433 KeyguardUpdateMonitor.StrongAuthTracker.STRONG_AUTH_REQUIRED_AFTER_USER_LOCKDOWN);
434
435 mKeyguardUpdateMonitor.onKeyguardVisibilityChanged(true);
436 verify(mFaceManager, never()).authenticate(any(), any(), anyInt(), any(), any(), anyInt());
437 }
438
439 @Test
440 public void testIgnoresAuth_whenLockout() {
441 mKeyguardUpdateMonitor.dispatchStartedWakingUp();
442 mTestableLooper.processAllMessages();
443 when(mStrongAuthTracker.getStrongAuthForUser(anyInt())).thenReturn(
444 KeyguardUpdateMonitor.StrongAuthTracker.STRONG_AUTH_REQUIRED_AFTER_LOCKOUT);
445
446 mKeyguardUpdateMonitor.onKeyguardVisibilityChanged(true);
447 verify(mFaceManager, never()).authenticate(any(), any(), anyInt(), any(), any(), anyInt());
448 }
449
450 @Test
Lucas Dupin3d053532019-01-29 12:35:22 -0800451 public void testOnFaceAuthenticated_skipsFaceWhenAuthenticated() {
452 mKeyguardUpdateMonitor.onFaceAuthenticated(KeyguardUpdateMonitor.getCurrentUser());
453 mKeyguardUpdateMonitor.sendKeyguardBouncerChanged(true);
454 mTestableLooper.processAllMessages();
455
456 verify(mFaceManager, never()).authenticate(any(), any(), anyInt(), any(), any());
457 }
458
459 @Test
460 public void testGetUserCanSkipBouncer_whenFace() {
461 int user = KeyguardUpdateMonitor.getCurrentUser();
462 mKeyguardUpdateMonitor.onFaceAuthenticated(user);
463 assertThat(mKeyguardUpdateMonitor.getUserCanSkipBouncer(user)).isTrue();
464 }
465
466 @Test
467 public void testGetUserCanSkipBouncer_whenFingerprint() {
468 int user = KeyguardUpdateMonitor.getCurrentUser();
469 mKeyguardUpdateMonitor.onFingerprintAuthenticated(user);
470 assertThat(mKeyguardUpdateMonitor.getUserCanSkipBouncer(user)).isTrue();
471 }
472
473 @Test
474 public void testGetUserCanSkipBouncer_whenTrust() {
475 int user = KeyguardUpdateMonitor.getCurrentUser();
476 mKeyguardUpdateMonitor.onTrustChanged(true /* enabled */, user, 0 /* flags */);
477 assertThat(mKeyguardUpdateMonitor.getUserCanSkipBouncer(user)).isTrue();
Bill Linef81cbd2018-07-05 17:48:49 +0800478 }
479
Malcolm Chen5c63b512019-08-13 13:24:07 -0700480 @Test
481 public void testGetSubscriptionInfo_whenInGroupedSubWithOpportunistic() {
482 List<SubscriptionInfo> list = new ArrayList<>();
483 list.add(TEST_SUBSCRIPTION);
484 list.add(TEST_SUBSCRIPTION_2);
Amit Mahajan75eab472020-01-29 10:47:48 -0800485 when(mSubscriptionManager.getActiveAndHiddenSubscriptionInfoList()).thenReturn(list);
Malcolm Chen5c63b512019-08-13 13:24:07 -0700486 mKeyguardUpdateMonitor.mPhoneStateListener.onActiveDataSubscriptionIdChanged(
487 TEST_SUBSCRIPTION_2.getSubscriptionId());
488 mTestableLooper.processAllMessages();
489
490 List<SubscriptionInfo> listToVerify = mKeyguardUpdateMonitor
491 .getFilteredSubscriptionInfo(false);
492 assertThat(listToVerify.size()).isEqualTo(1);
493 assertThat(listToVerify.get(0)).isEqualTo(TEST_SUBSCRIPTION_2);
494 }
495
Lucas Dupin6c6d5732019-08-27 17:36:05 -0700496 @Test
497 public void testIsUserUnlocked() {
498 // mUserManager will report the user as unlocked on @Before
499 assertThat(mKeyguardUpdateMonitor.isUserUnlocked(KeyguardUpdateMonitor.getCurrentUser()))
500 .isTrue();
501 // Invalid user should not be unlocked.
502 int randomUser = 99;
503 assertThat(mKeyguardUpdateMonitor.isUserUnlocked(randomUser)).isFalse();
504 }
505
Lucas Dupinf2c53502019-10-03 13:56:18 -0700506 @Test
507 public void testTrustUsuallyManaged_whenTrustChanges() {
508 int user = KeyguardUpdateMonitor.getCurrentUser();
509 when(mTrustManager.isTrustUsuallyManaged(eq(user))).thenReturn(true);
510 mKeyguardUpdateMonitor.onTrustManagedChanged(false /* managed */, user);
511 assertThat(mKeyguardUpdateMonitor.isTrustUsuallyManaged(user)).isTrue();
512 }
513
514 @Test
515 public void testTrustUsuallyManaged_resetWhenUserIsRemoved() {
516 int user = KeyguardUpdateMonitor.getCurrentUser();
517 when(mTrustManager.isTrustUsuallyManaged(eq(user))).thenReturn(true);
518 mKeyguardUpdateMonitor.onTrustManagedChanged(false /* managed */, user);
519 assertThat(mKeyguardUpdateMonitor.isTrustUsuallyManaged(user)).isTrue();
520
521 mKeyguardUpdateMonitor.handleUserRemoved(user);
522 assertThat(mKeyguardUpdateMonitor.isTrustUsuallyManaged(user)).isFalse();
523 }
524
Yvonne Jiangb7024a22019-12-05 16:57:08 -0800525 @Test
526 public void testSecondaryLockscreenRequirement() {
527 int user = KeyguardUpdateMonitor.getCurrentUser();
528 String packageName = "fake.test.package";
529 String cls = "FakeService";
530 ServiceInfo serviceInfo = new ServiceInfo();
531 serviceInfo.packageName = packageName;
532 serviceInfo.name = cls;
533 ResolveInfo resolveInfo = new ResolveInfo();
534 resolveInfo.serviceInfo = serviceInfo;
535 when(mPackageManager.resolveService(any(Intent.class), eq(0))).thenReturn(resolveInfo);
536 when(mDevicePolicyManager.isSecondaryLockscreenEnabled(eq(user))).thenReturn(true, false);
537
538 // Initially null.
539 assertThat(mKeyguardUpdateMonitor.getSecondaryLockscreenRequirement(user)).isNull();
540
541 // Set non-null after DPM change.
542 setBroadcastReceiverPendingResult(mKeyguardUpdateMonitor.mBroadcastAllReceiver);
543 Intent intent = new Intent(DevicePolicyManager.ACTION_DEVICE_POLICY_MANAGER_STATE_CHANGED);
544 mKeyguardUpdateMonitor.mBroadcastAllReceiver.onReceive(getContext(), intent);
545 mTestableLooper.processAllMessages();
546
547 Intent storedIntent = mKeyguardUpdateMonitor.getSecondaryLockscreenRequirement(user);
548 assertThat(storedIntent.getComponent().getClassName()).isEqualTo(cls);
549 assertThat(storedIntent.getComponent().getPackageName()).isEqualTo(packageName);
550
551 // Back to null after another DPM change.
552 mKeyguardUpdateMonitor.mBroadcastAllReceiver.onReceive(getContext(), intent);
553 mTestableLooper.processAllMessages();
554 assertThat(mKeyguardUpdateMonitor.getSecondaryLockscreenRequirement(user)).isNull();
555 }
556
557 private void setBroadcastReceiverPendingResult(BroadcastReceiver receiver) {
558 BroadcastReceiver.PendingResult pendingResult =
559 new BroadcastReceiver.PendingResult(Activity.RESULT_OK,
560 "resultData",
561 /* resultExtras= */ null,
562 BroadcastReceiver.PendingResult.TYPE_UNREGISTERED,
563 /* ordered= */ true,
564 /* sticky= */ false,
565 /* token= */ null,
566 UserHandle.myUserId(),
567 /* flags= */ 0);
568 receiver.setPendingResult(pendingResult);
569 }
570
Bill Linef81cbd2018-07-05 17:48:49 +0800571 private Intent putPhoneInfo(Intent intent, Bundle data, Boolean simInited) {
572 int subscription = simInited
573 ? 1/* mock subid=1 */ : SubscriptionManager.DUMMY_SUBSCRIPTION_ID_BASE;
574 if (data != null) intent.putExtras(data);
Daniel Bright63514be2020-01-15 12:10:53 -0800575
576 intent.putExtra(SubscriptionManager.EXTRA_SUBSCRIPTION_INDEX, subscription);
577 intent.putExtra(SubscriptionManager.EXTRA_SLOT_INDEX, 0);
Bill Linef81cbd2018-07-05 17:48:49 +0800578 return intent;
579 }
580
Lucas Dupin7ff82b02018-05-16 12:14:10 -0700581 private class TestableKeyguardUpdateMonitor extends KeyguardUpdateMonitor {
582 AtomicBoolean mSimStateChanged = new AtomicBoolean(false);
583
584 protected TestableKeyguardUpdateMonitor(Context context) {
Fabian Kozynski5ca7a512019-10-16 19:56:11 +0000585 super(context,
586 TestableLooper.get(KeyguardUpdateMonitorTest.this).getLooper(),
587 mBroadcastDispatcher, mDumpController);
Lucas Dupin3d053532019-01-29 12:35:22 -0800588 mStrongAuthTracker = KeyguardUpdateMonitorTest.this.mStrongAuthTracker;
Lucas Dupin7ff82b02018-05-16 12:14:10 -0700589 }
590
591 public boolean hasSimStateJustChanged() {
592 return mSimStateChanged.getAndSet(false);
593 }
594
595 @Override
Jayachandran Cf5436a62019-11-08 18:22:45 -0800596 protected void handleSimStateChange(int subId, int slotId, int state) {
Lucas Dupin7ff82b02018-05-16 12:14:10 -0700597 mSimStateChanged.set(true);
598 super.handleSimStateChange(subId, slotId, state);
599 }
Lucas Dupin5e0f0d22018-02-26 13:32:16 -0800600 }
601}