blob: b5609dde37c73aacc8c0f3fa85bf1e71324731d5 [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 Wangff7f25f2020-01-31 18:46:26 -080020import static android.telephony.SubscriptionManager.NAME_SOURCE_CARRIER_ID;
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;
Haining Chenc06c4812020-01-13 20:38:53 -080025import 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;
Fabian Kozynskib6a20372020-04-01 09:36:43 -040032import static org.mockito.Mockito.inOrder;
33import static org.mockito.Mockito.mock;
Lucas Dupin3d053532019-01-29 12:35:22 -080034import static org.mockito.Mockito.never;
Lucas Dupin3d053532019-01-29 12:35:22 -080035import static org.mockito.Mockito.spy;
36import static org.mockito.Mockito.verify;
37import static org.mockito.Mockito.when;
Bill Linef81cbd2018-07-05 17:48:49 +080038
Yvonne Jiangb7024a22019-12-05 16:57:08 -080039import android.app.Activity;
Lucas Dupin3d053532019-01-29 12:35:22 -080040import android.app.admin.DevicePolicyManager;
41import android.app.trust.TrustManager;
Yvonne Jiangb7024a22019-12-05 16:57:08 -080042import android.content.BroadcastReceiver;
Yvonne Jiang8345da32020-03-19 15:01:16 -070043import android.content.ComponentName;
Lucas Dupin7ff82b02018-05-16 12:14:10 -070044import android.content.Context;
Lucas Dupin5e0f0d22018-02-26 13:32:16 -080045import android.content.Intent;
Fabian Kozynski5ca7a512019-10-16 19:56:11 +000046import android.content.IntentFilter;
Lucas Dupin3d053532019-01-29 12:35:22 -080047import android.content.pm.PackageManager;
Yvonne Jiangb7024a22019-12-05 16:57:08 -080048import android.content.pm.ResolveInfo;
49import android.content.pm.ServiceInfo;
Lucas Dupin3d053532019-01-29 12:35:22 -080050import android.hardware.biometrics.BiometricManager;
51import android.hardware.biometrics.BiometricSourceType;
52import android.hardware.biometrics.IBiometricEnabledOnKeyguardCallback;
53import android.hardware.face.FaceManager;
54import android.hardware.fingerprint.FingerprintManager;
Fabian Kozynskib6a20372020-04-01 09:36:43 -040055import android.media.AudioManager;
Bill Linef81cbd2018-07-05 17:48:49 +080056import android.os.Bundle;
Fabian Kozynski5ca7a512019-10-16 19:56:11 +000057import android.os.Handler;
Kevin Chyn505eb892020-03-26 13:07:03 -070058import android.os.IRemoteCallback;
Fabian Kozynski5ca7a512019-10-16 19:56:11 +000059import android.os.UserHandle;
Lucas Dupin3d053532019-01-29 12:35:22 -080060import android.os.UserManager;
Bill Linef81cbd2018-07-05 17:48:49 +080061import android.telephony.ServiceState;
Malcolm Chen5c63b512019-08-13 13:24:07 -070062import android.telephony.SubscriptionInfo;
Bill Linef81cbd2018-07-05 17:48:49 +080063import android.telephony.SubscriptionManager;
Fabian Kozynskib00c70b2020-04-03 12:41:31 -040064import android.telephony.TelephonyManager;
Lucas Dupin5e0f0d22018-02-26 13:32:16 -080065import android.test.suitebuilder.annotation.SmallTest;
66import android.testing.AndroidTestingRunner;
Lucas Dupin3d053532019-01-29 12:35:22 -080067import android.testing.TestableContext;
Lucas Dupin5e0f0d22018-02-26 13:32:16 -080068import android.testing.TestableLooper;
Lucas Dupin5e0f0d22018-02-26 13:32:16 -080069
Fabian Kozynskib6a20372020-04-01 09:36:43 -040070import androidx.lifecycle.LiveData;
71import androidx.lifecycle.Observer;
72
Hall Liu45066122020-04-07 15:37:44 -070073import com.android.dx.mockito.inline.extended.ExtendedMockito;
Lucas Dupin5e0f0d22018-02-26 13:32:16 -080074import com.android.internal.telephony.TelephonyIntents;
Kevin Chyn505eb892020-03-26 13:07:03 -070075import com.android.keyguard.KeyguardUpdateMonitor.BiometricAuthenticated;
Lucas Dupin5e0f0d22018-02-26 13:32:16 -080076import com.android.systemui.SysuiTestCase;
Fabian Kozynski5ca7a512019-10-16 19:56:11 +000077import com.android.systemui.broadcast.BroadcastDispatcher;
Ned Burnsaaeb44b2020-02-12 23:48:26 -050078import com.android.systemui.dump.DumpManager;
Curtis Belmonte9dc68152020-05-08 17:12:13 -070079import com.android.systemui.plugins.statusbar.StatusBarStateController;
80import com.android.systemui.statusbar.StatusBarState;
Lucas Dupin4befb742019-07-01 11:31:31 -070081import com.android.systemui.statusbar.phone.KeyguardBypassController;
Fabian Kozynskib6a20372020-04-01 09:36:43 -040082import com.android.systemui.util.RingerModeTracker;
Lucas Dupin5e0f0d22018-02-26 13:32:16 -080083
Beverly4e7dd9a2020-02-28 17:04:21 -050084import org.junit.After;
Lucas Dupin5e0f0d22018-02-26 13:32:16 -080085import org.junit.Assert;
86import org.junit.Before;
87import org.junit.Test;
88import org.junit.runner.RunWith;
Fabian Kozynskib6a20372020-04-01 09:36:43 -040089import org.mockito.ArgumentCaptor;
90import org.mockito.InOrder;
Lucas Dupin3d053532019-01-29 12:35:22 -080091import org.mockito.Mock;
92import org.mockito.MockitoAnnotations;
Hall Liu45066122020-04-07 15:37:44 -070093import org.mockito.MockitoSession;
Lucas Dupin5e0f0d22018-02-26 13:32:16 -080094
Malcolm Chen5c63b512019-08-13 13:24:07 -070095import java.util.ArrayList;
96import java.util.List;
Haining Chenc06c4812020-01-13 20:38:53 -080097import java.util.concurrent.Executor;
Lucas Dupin5e0f0d22018-02-26 13:32:16 -080098import java.util.concurrent.atomic.AtomicBoolean;
99
100@SmallTest
101@RunWith(AndroidTestingRunner.class)
Dave Mankoffe2294692019-08-14 11:53:13 -0400102@TestableLooper.RunWithLooper
Lucas Dupin5e0f0d22018-02-26 13:32:16 -0800103public class KeyguardUpdateMonitorTest extends SysuiTestCase {
Malcolm Chen5c63b512019-08-13 13:24:07 -0700104 private static final String TEST_CARRIER = "TEST_CARRIER";
105 private static final String TEST_CARRIER_2 = "TEST_CARRIER_2";
106 private static final int TEST_CARRIER_ID = 1;
107 private static final String TEST_GROUP_UUID = "59b5c870-fc4c-47a4-a99e-9db826b48b24";
108 private static final SubscriptionInfo TEST_SUBSCRIPTION = new SubscriptionInfo(1, "", 0,
Peter Wangff7f25f2020-01-31 18:46:26 -0800109 TEST_CARRIER, TEST_CARRIER, NAME_SOURCE_CARRIER_ID, 0xFFFFFF, "",
Malcolm Chen5c63b512019-08-13 13:24:07 -0700110 DATA_ROAMING_DISABLE, null, null, null, null, false, null, "", false, TEST_GROUP_UUID,
111 TEST_CARRIER_ID, 0);
112 private static final SubscriptionInfo TEST_SUBSCRIPTION_2 = new SubscriptionInfo(2, "", 0,
Peter Wangff7f25f2020-01-31 18:46:26 -0800113 TEST_CARRIER, TEST_CARRIER_2, NAME_SOURCE_CARRIER_ID, 0xFFFFFF, "",
Malcolm Chen5c63b512019-08-13 13:24:07 -0700114 DATA_ROAMING_DISABLE, null, null, null, null, false, null, "", true, TEST_GROUP_UUID,
115 TEST_CARRIER_ID, 0);
Lucas Dupin3d053532019-01-29 12:35:22 -0800116 @Mock
Ned Burnsaaeb44b2020-02-12 23:48:26 -0500117 private DumpManager mDumpManager;
Lucas Dupin64171fe2019-10-30 14:28:29 -0700118 @Mock
Lucas Dupin3d053532019-01-29 12:35:22 -0800119 private KeyguardUpdateMonitor.StrongAuthTracker mStrongAuthTracker;
120 @Mock
121 private TrustManager mTrustManager;
122 @Mock
123 private FingerprintManager mFingerprintManager;
124 @Mock
125 private FaceManager mFaceManager;
126 @Mock
127 private BiometricManager mBiometricManager;
128 @Mock
129 private PackageManager mPackageManager;
130 @Mock
131 private UserManager mUserManager;
132 @Mock
133 private DevicePolicyManager mDevicePolicyManager;
Lucas Dupin4befb742019-07-01 11:31:31 -0700134 @Mock
135 private KeyguardBypassController mKeyguardBypassController;
Malcolm Chen5c63b512019-08-13 13:24:07 -0700136 @Mock
137 private SubscriptionManager mSubscriptionManager;
Fabian Kozynski5ca7a512019-10-16 19:56:11 +0000138 @Mock
139 private BroadcastDispatcher mBroadcastDispatcher;
Haining Chenc06c4812020-01-13 20:38:53 -0800140 @Mock
Hall Liu45066122020-04-07 15:37:44 -0700141 private TelephonyManager mTelephonyManager;
142 @Mock
Fabian Kozynskib6a20372020-04-01 09:36:43 -0400143 private RingerModeTracker mRingerModeTracker;
144 @Mock
145 private LiveData<Integer> mRingerModeLiveData;
Curtis Belmonte9dc68152020-05-08 17:12:13 -0700146 @Mock
147 private StatusBarStateController mStatusBarStateController;
Fabian Kozynskib00c70b2020-04-03 12:41:31 -0400148 // Direct executor
149 private Executor mBackgroundExecutor = Runnable::run;
Lucas Dupin5e0f0d22018-02-26 13:32:16 -0800150 private TestableLooper mTestableLooper;
Lucas Dupin3d053532019-01-29 12:35:22 -0800151 private TestableKeyguardUpdateMonitor mKeyguardUpdateMonitor;
Fabian Kozynskib00c70b2020-04-03 12:41:31 -0400152 private TestableContext mSpiedContext;
Hall Liu45066122020-04-07 15:37:44 -0700153 private MockitoSession mMockitoSession;
Lucas Dupin5e0f0d22018-02-26 13:32:16 -0800154
155 @Before
156 public void setup() {
Lucas Dupin3d053532019-01-29 12:35:22 -0800157 MockitoAnnotations.initMocks(this);
Fabian Kozynskib00c70b2020-04-03 12:41:31 -0400158 mSpiedContext = spy(mContext);
Lucas Dupin3d053532019-01-29 12:35:22 -0800159 when(mPackageManager.hasSystemFeature(anyString())).thenReturn(true);
Fabian Kozynskib00c70b2020-04-03 12:41:31 -0400160 when(mSpiedContext.getPackageManager()).thenReturn(mPackageManager);
Lucas Dupin3d053532019-01-29 12:35:22 -0800161 doAnswer(invocation -> {
162 IBiometricEnabledOnKeyguardCallback callback = invocation.getArgument(0);
Lucas Dupin7d95f152019-07-17 16:25:54 -0700163 callback.onChanged(BiometricSourceType.FACE, true /* enabled */,
164 KeyguardUpdateMonitor.getCurrentUser());
Lucas Dupin3d053532019-01-29 12:35:22 -0800165 return null;
166 }).when(mBiometricManager).registerEnabledOnKeyguardCallback(any());
167 when(mFaceManager.isHardwareDetected()).thenReturn(true);
168 when(mFaceManager.hasEnrolledTemplates()).thenReturn(true);
169 when(mFaceManager.hasEnrolledTemplates(anyInt())).thenReturn(true);
170 when(mUserManager.isUserUnlocked(anyInt())).thenReturn(true);
Kevin Chynfdfd2d32019-03-01 14:52:15 -0800171 when(mUserManager.isPrimaryUser()).thenReturn(true);
Haining Chenc06c4812020-01-13 20:38:53 -0800172 when(mStrongAuthTracker
173 .isUnlockingWithBiometricAllowed(anyBoolean() /* isStrongBiometric */))
174 .thenReturn(true);
Hall Liu45066122020-04-07 15:37:44 -0700175
176 when(mTelephonyManager.getServiceStateForSubscriber(anyInt()))
177 .thenReturn(new ServiceState());
Fabian Kozynskib00c70b2020-04-03 12:41:31 -0400178 mSpiedContext.addMockSystemService(TrustManager.class, mTrustManager);
179 mSpiedContext.addMockSystemService(FingerprintManager.class, mFingerprintManager);
180 mSpiedContext.addMockSystemService(BiometricManager.class, mBiometricManager);
181 mSpiedContext.addMockSystemService(FaceManager.class, mFaceManager);
182 mSpiedContext.addMockSystemService(UserManager.class, mUserManager);
183 mSpiedContext.addMockSystemService(DevicePolicyManager.class, mDevicePolicyManager);
184 mSpiedContext.addMockSystemService(SubscriptionManager.class, mSubscriptionManager);
185 mSpiedContext.addMockSystemService(TelephonyManager.class, mTelephonyManager);
Lucas Dupin3d053532019-01-29 12:35:22 -0800186
Fabian Kozynskib6a20372020-04-01 09:36:43 -0400187 when(mRingerModeTracker.getRingerMode()).thenReturn(mRingerModeLiveData);
188
Hall Liu45066122020-04-07 15:37:44 -0700189 mMockitoSession = ExtendedMockito.mockitoSession()
190 .spyStatic(SubscriptionManager.class).startMocking();
191 ExtendedMockito.doReturn(SubscriptionManager.INVALID_SUBSCRIPTION_ID)
192 .when(SubscriptionManager::getDefaultSubscriptionId);
193
Lucas Dupin5e0f0d22018-02-26 13:32:16 -0800194 mTestableLooper = TestableLooper.get(this);
Beverly1467c9e2020-02-18 13:31:29 -0500195 allowTestableLooperAsMainThread();
Fabian Kozynskib00c70b2020-04-03 12:41:31 -0400196 mKeyguardUpdateMonitor = new TestableKeyguardUpdateMonitor(mSpiedContext);
Lucas Dupin5e0f0d22018-02-26 13:32:16 -0800197 }
198
Beverly4e7dd9a2020-02-28 17:04:21 -0500199 @After
200 public void tearDown() {
Hall Liu45066122020-04-07 15:37:44 -0700201 mMockitoSession.finishMocking();
Beverly4e7dd9a2020-02-28 17:04:21 -0500202 mKeyguardUpdateMonitor.destroy();
203 }
204
Lucas Dupin5e0f0d22018-02-26 13:32:16 -0800205 @Test
Fabian Kozynski5ca7a512019-10-16 19:56:11 +0000206 public void testReceiversRegistered() {
Fabian Kozynski5e92c6f2020-01-03 13:56:17 -0500207 verify(mBroadcastDispatcher, atLeastOnce()).registerReceiverWithHandler(
Fabian Kozynski5ca7a512019-10-16 19:56:11 +0000208 eq(mKeyguardUpdateMonitor.mBroadcastReceiver),
209 any(IntentFilter.class), any(Handler.class));
Fabian Kozynski5e92c6f2020-01-03 13:56:17 -0500210 verify(mBroadcastDispatcher, atLeastOnce()).registerReceiverWithHandler(
Fabian Kozynski5ca7a512019-10-16 19:56:11 +0000211 eq(mKeyguardUpdateMonitor.mBroadcastAllReceiver),
212 any(IntentFilter.class), any(Handler.class), eq(UserHandle.ALL));
213 }
214
215 @Test
Fabian Kozynskib00c70b2020-04-03 12:41:31 -0400216 public void testSimStateInitialized() {
217 final int subId = 3;
218 final int state = TelephonyManager.SIM_STATE_ABSENT;
219
220 when(mTelephonyManager.getActiveModemCount()).thenReturn(1);
221 when(mTelephonyManager.getSimState(anyInt())).thenReturn(state);
222 when(mSubscriptionManager.getSubscriptionIds(anyInt())).thenReturn(new int[] { subId });
223
224 KeyguardUpdateMonitor testKUM = new TestableKeyguardUpdateMonitor(mSpiedContext);
225
226 mTestableLooper.processAllMessages();
227
228 assertThat(testKUM.getSimState(subId)).isEqualTo(state);
229 }
230
231 @Test
Lucas Dupin5e0f0d22018-02-26 13:32:16 -0800232 public void testIgnoresSimStateCallback_rebroadcast() {
233 Intent intent = new Intent(TelephonyIntents.ACTION_SIM_STATE_CHANGED);
234
Lucas Dupin3d053532019-01-29 12:35:22 -0800235 mKeyguardUpdateMonitor.mBroadcastReceiver.onReceive(getContext(), intent);
Lucas Dupin5e0f0d22018-02-26 13:32:16 -0800236 mTestableLooper.processAllMessages();
Lucas Dupin7ff82b02018-05-16 12:14:10 -0700237 Assert.assertTrue("onSimStateChanged not called",
Lucas Dupin3d053532019-01-29 12:35:22 -0800238 mKeyguardUpdateMonitor.hasSimStateJustChanged());
Lucas Dupin5e0f0d22018-02-26 13:32:16 -0800239
Meng Wang0e8d91a2020-01-17 14:58:01 -0800240 intent.putExtra(Intent.EXTRA_REBROADCAST_ON_UNLOCK, true);
Lucas Dupin3d053532019-01-29 12:35:22 -0800241 mKeyguardUpdateMonitor.mBroadcastReceiver.onReceive(getContext(), intent);
Lucas Dupin5e0f0d22018-02-26 13:32:16 -0800242 mTestableLooper.processAllMessages();
Lucas Dupin7ff82b02018-05-16 12:14:10 -0700243 Assert.assertFalse("onSimStateChanged should have been skipped",
Lucas Dupin3d053532019-01-29 12:35:22 -0800244 mKeyguardUpdateMonitor.hasSimStateJustChanged());
Lucas Dupin7ff82b02018-05-16 12:14:10 -0700245 }
246
Bill Linef81cbd2018-07-05 17:48:49 +0800247 @Test
248 public void testTelephonyCapable_BootInitState() {
Lucas Dupin3d053532019-01-29 12:35:22 -0800249 assertThat(mKeyguardUpdateMonitor.mTelephonyCapable).isFalse();
Bill Linef81cbd2018-07-05 17:48:49 +0800250 }
251
252 @Test
253 public void testTelephonyCapable_SimState_Absent() {
254 Intent intent = new Intent(TelephonyIntents.ACTION_SIM_STATE_CHANGED);
Jayachandran C0173df82019-12-19 12:11:03 -0800255 intent.putExtra(Intent.EXTRA_SIM_STATE,
256 Intent.SIM_STATE_ABSENT);
andychou695a7602019-05-16 23:14:00 +0800257 mKeyguardUpdateMonitor.mBroadcastReceiver.onReceive(getContext(),
258 putPhoneInfo(intent, null, false));
259 mTestableLooper.processAllMessages();
260 assertThat(mKeyguardUpdateMonitor.mTelephonyCapable).isTrue();
261 }
262
263 @Test
264 public void testTelephonyCapable_SimState_CardIOError() {
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_CARD_IO_ERROR);
andychou695a7602019-05-16 23:14:00 +0800268 mKeyguardUpdateMonitor.mBroadcastReceiver.onReceive(getContext(),
269 putPhoneInfo(intent, null, false));
Bill Linef81cbd2018-07-05 17:48:49 +0800270 mTestableLooper.processAllMessages();
Lucas Dupin3d053532019-01-29 12:35:22 -0800271 assertThat(mKeyguardUpdateMonitor.mTelephonyCapable).isTrue();
Bill Linef81cbd2018-07-05 17:48:49 +0800272 }
273
274 @Test
275 public void testTelephonyCapable_SimInvalid_ServiceState_InService() {
276 // SERVICE_STATE - IN_SERVICE, but SIM_STATE is invalid TelephonyCapable should be False
Jayachandran C041e7692019-12-20 16:20:02 -0800277 Intent intent = new Intent(Intent.ACTION_SERVICE_STATE);
Bill Linef81cbd2018-07-05 17:48:49 +0800278 Bundle data = new Bundle();
279 ServiceState state = new ServiceState();
280 state.setState(ServiceState.STATE_IN_SERVICE);
281 state.fillInNotifierBundle(data);
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_SimValid_ServiceState_PowerOff() {
290 // Simulate AirplaneMode case, SERVICE_STATE - POWER_OFF, check TelephonyCapable False
291 // Only receive ServiceState callback IN_SERVICE -> OUT_OF_SERVICE -> POWER_OFF
Jayachandran C041e7692019-12-20 16:20:02 -0800292 Intent intent = new Intent(Intent.ACTION_SERVICE_STATE);
Jayachandran C0173df82019-12-19 12:11:03 -0800293 intent.putExtra(Intent.EXTRA_SIM_STATE
294 , Intent.SIM_STATE_LOADED);
Bill Linef81cbd2018-07-05 17:48:49 +0800295 Bundle data = new Bundle();
296 ServiceState state = new ServiceState();
297 state.setState(ServiceState.STATE_POWER_OFF);
298 state.fillInNotifierBundle(data);
Lucas Dupin3d053532019-01-29 12:35:22 -0800299 mKeyguardUpdateMonitor.mBroadcastReceiver.onReceive(getContext()
Bill Linef81cbd2018-07-05 17:48:49 +0800300 , putPhoneInfo(intent, data, true));
301 mTestableLooper.processAllMessages();
Lucas Dupin3d053532019-01-29 12:35:22 -0800302 assertThat(mKeyguardUpdateMonitor.mTelephonyCapable).isTrue();
Bill Linef81cbd2018-07-05 17:48:49 +0800303 }
304
305 /* Normal SIM inserted flow
306 * ServiceState: ---OutOfServie----->PowerOff->OutOfServie--->InService
307 * SimState: ----NOT_READY---->READY----------------------LOADED>>>
308 * Subscription: --------null---->null--->"Chunghwa Telecom"-------->>>
309 * System: -------------------------------BOOT_COMPLETED------>>>
310 * TelephonyCapable:(F)-(F)-(F)-(F)-(F)-(F)-(F)-(F)-(F)-(F)------(T)-(T)>>
311 */
312 @Test
313 public void testTelephonyCapable_BootInitState_ServiceState_OutOfService() {
Jayachandran C041e7692019-12-20 16:20:02 -0800314 Intent intent = new Intent(Intent.ACTION_SERVICE_STATE);
Bill Linef81cbd2018-07-05 17:48:49 +0800315 Bundle data = new Bundle();
316 ServiceState state = new ServiceState();
317 state.setState(ServiceState.STATE_OUT_OF_SERVICE);
318 state.fillInNotifierBundle(data);
319 intent.putExtras(data);
Lucas Dupin3d053532019-01-29 12:35:22 -0800320 mKeyguardUpdateMonitor.mBroadcastReceiver.onReceive(getContext()
Bill Linef81cbd2018-07-05 17:48:49 +0800321 , putPhoneInfo(intent, data, false));
322 mTestableLooper.processAllMessages();
Lucas Dupin3d053532019-01-29 12:35:22 -0800323 assertThat(mKeyguardUpdateMonitor.mTelephonyCapable).isFalse();
Bill Linef81cbd2018-07-05 17:48:49 +0800324 }
325
326 @Test
327 public void testTelephonyCapable_BootInitState_SimState_NotReady() {
Bill Linef81cbd2018-07-05 17:48:49 +0800328 Bundle data = new Bundle();
329 ServiceState state = new ServiceState();
330 state.setState(ServiceState.STATE_OUT_OF_SERVICE);
331 state.fillInNotifierBundle(data);
332 Intent intent = new Intent(TelephonyIntents.ACTION_SIM_STATE_CHANGED);
Jayachandran C0173df82019-12-19 12:11:03 -0800333 intent.putExtra(Intent.EXTRA_SIM_STATE
334 , Intent.SIM_STATE_NOT_READY);
Lucas Dupin3d053532019-01-29 12:35:22 -0800335 mKeyguardUpdateMonitor.mBroadcastReceiver.onReceive(getContext()
Bill Linef81cbd2018-07-05 17:48:49 +0800336 , putPhoneInfo(intent, data, false));
337 mTestableLooper.processAllMessages();
Lucas Dupin3d053532019-01-29 12:35:22 -0800338 assertThat(mKeyguardUpdateMonitor.mTelephonyCapable).isFalse();
Bill Linef81cbd2018-07-05 17:48:49 +0800339 }
340
341 @Test
342 public void testTelephonyCapable_BootInitState_SimState_Ready() {
Bill Linef81cbd2018-07-05 17:48:49 +0800343 Bundle data = new Bundle();
344 ServiceState state = new ServiceState();
345 state.setState(ServiceState.STATE_OUT_OF_SERVICE);
346 state.fillInNotifierBundle(data);
347 Intent intent = new Intent(TelephonyIntents.ACTION_SIM_STATE_CHANGED);
Jayachandran C0173df82019-12-19 12:11:03 -0800348 intent.putExtra(Intent.EXTRA_SIM_STATE
349 , Intent.SIM_STATE_READY);
Lucas Dupin3d053532019-01-29 12:35:22 -0800350 mKeyguardUpdateMonitor.mBroadcastReceiver.onReceive(getContext()
Bill Linef81cbd2018-07-05 17:48:49 +0800351 , putPhoneInfo(intent, data, false));
352 mTestableLooper.processAllMessages();
Lucas Dupin3d053532019-01-29 12:35:22 -0800353 assertThat(mKeyguardUpdateMonitor.mTelephonyCapable).isFalse();
Bill Linef81cbd2018-07-05 17:48:49 +0800354 }
355
356 @Test
357 public void testTelephonyCapable_BootInitState_ServiceState_PowerOff() {
Jayachandran C041e7692019-12-20 16:20:02 -0800358 Intent intent = new Intent(Intent.ACTION_SERVICE_STATE);
Bill Linef81cbd2018-07-05 17:48:49 +0800359 Bundle data = new Bundle();
360 ServiceState state = new ServiceState();
361 state.setState(ServiceState.STATE_POWER_OFF);
362 state.fillInNotifierBundle(data);
Lucas Dupin3d053532019-01-29 12:35:22 -0800363 mKeyguardUpdateMonitor.mBroadcastReceiver.onReceive(getContext()
Bill Linef81cbd2018-07-05 17:48:49 +0800364 , putPhoneInfo(intent, data, false));
365 mTestableLooper.processAllMessages();
Lucas Dupin3d053532019-01-29 12:35:22 -0800366 assertThat(mKeyguardUpdateMonitor.mTelephonyCapable).isFalse();
Bill Linef81cbd2018-07-05 17:48:49 +0800367 }
368
369 @Test
370 public void testTelephonyCapable_SimValid_ServiceState_InService() {
Jayachandran C041e7692019-12-20 16:20:02 -0800371 Intent intent = new Intent(Intent.ACTION_SERVICE_STATE);
Bill Linef81cbd2018-07-05 17:48:49 +0800372 Bundle data = new Bundle();
373 ServiceState state = new ServiceState();
374 state.setState(ServiceState.STATE_IN_SERVICE);
375 state.fillInNotifierBundle(data);
Lucas Dupin3d053532019-01-29 12:35:22 -0800376 mKeyguardUpdateMonitor.mBroadcastReceiver.onReceive(getContext()
Bill Linef81cbd2018-07-05 17:48:49 +0800377 , putPhoneInfo(intent, data, true));
378 mTestableLooper.processAllMessages();
Lucas Dupin3d053532019-01-29 12:35:22 -0800379 assertThat(mKeyguardUpdateMonitor.mTelephonyCapable).isTrue();
Bill Linef81cbd2018-07-05 17:48:49 +0800380 }
381
382 @Test
383 public void testTelephonyCapable_SimValid_SimState_Loaded() {
Bill Linef81cbd2018-07-05 17:48:49 +0800384 Bundle data = new Bundle();
385 ServiceState state = new ServiceState();
386 state.setState(ServiceState.STATE_IN_SERVICE);
387 state.fillInNotifierBundle(data);
388 Intent intentSimState = new Intent(TelephonyIntents.ACTION_SIM_STATE_CHANGED);
Jayachandran C0173df82019-12-19 12:11:03 -0800389 intentSimState.putExtra(Intent.EXTRA_SIM_STATE
390 , Intent.SIM_STATE_LOADED);
Lucas Dupin3d053532019-01-29 12:35:22 -0800391 mKeyguardUpdateMonitor.mBroadcastReceiver.onReceive(getContext()
Bill Linef81cbd2018-07-05 17:48:49 +0800392 , putPhoneInfo(intentSimState, data, true));
393 mTestableLooper.processAllMessages();
Jayachandran C041e7692019-12-20 16:20:02 -0800394 // Even SimState Loaded, still need ACTION_SERVICE_STATE turn on mTelephonyCapable
Lucas Dupin3d053532019-01-29 12:35:22 -0800395 assertThat(mKeyguardUpdateMonitor.mTelephonyCapable).isFalse();
Bill Linef81cbd2018-07-05 17:48:49 +0800396
Jayachandran C041e7692019-12-20 16:20:02 -0800397 Intent intentServiceState = new Intent(Intent.ACTION_SERVICE_STATE);
Jayachandran C0173df82019-12-19 12:11:03 -0800398 intentSimState.putExtra(Intent.EXTRA_SIM_STATE
399 , Intent.SIM_STATE_LOADED);
Lucas Dupin3d053532019-01-29 12:35:22 -0800400 mKeyguardUpdateMonitor.mBroadcastReceiver.onReceive(getContext()
Bill Linef81cbd2018-07-05 17:48:49 +0800401 , putPhoneInfo(intentServiceState, data, true));
402 mTestableLooper.processAllMessages();
Lucas Dupin3d053532019-01-29 12:35:22 -0800403 assertThat(mKeyguardUpdateMonitor.mTelephonyCapable).isTrue();
404 }
405
406 @Test
407 public void testTriesToAuthenticate_whenBouncer() {
408 mKeyguardUpdateMonitor.sendKeyguardBouncerChanged(true);
409 mTestableLooper.processAllMessages();
410
Kevin Chyn8d2694a2019-04-11 18:30:40 -0700411 verify(mFaceManager).authenticate(any(), any(), anyInt(), any(), any(), anyInt());
Lucas Dupin3d053532019-01-29 12:35:22 -0800412 verify(mFaceManager).isHardwareDetected();
413 verify(mFaceManager).hasEnrolledTemplates(anyInt());
414 }
415
416 @Test
417 public void testTriesToAuthenticate_whenKeyguard() {
418 mKeyguardUpdateMonitor.dispatchStartedWakingUp();
419 mTestableLooper.processAllMessages();
420 mKeyguardUpdateMonitor.onKeyguardVisibilityChanged(true);
Kevin Chyn8d2694a2019-04-11 18:30:40 -0700421 verify(mFaceManager).authenticate(any(), any(), anyInt(), any(), any(), anyInt());
Lucas Dupin3d053532019-01-29 12:35:22 -0800422 }
423
424 @Test
Curtis Belmonte9dc68152020-05-08 17:12:13 -0700425 public void skipsAuthentication_whenStatusBarShadeLocked() {
426 when(mStatusBarStateController.getState()).thenReturn(StatusBarState.SHADE_LOCKED);
427
428 mKeyguardUpdateMonitor.dispatchStartedWakingUp();
429 mTestableLooper.processAllMessages();
430 mKeyguardUpdateMonitor.onKeyguardVisibilityChanged(true);
431 verify(mFaceManager, never()).authenticate(any(), any(), anyInt(), any(), any(), anyInt());
432 }
433
434 @Test
Lucas Dupin3d053532019-01-29 12:35:22 -0800435 public void skipsAuthentication_whenEncryptedKeyguard() {
Lucas Dupin8eec2682019-07-01 16:41:17 -0700436 when(mStrongAuthTracker.getStrongAuthForUser(anyInt())).thenReturn(
437 KeyguardUpdateMonitor.StrongAuthTracker.STRONG_AUTH_REQUIRED_AFTER_BOOT);
438 mKeyguardUpdateMonitor.setKeyguardBypassController(mKeyguardBypassController);
Lucas Dupin3d053532019-01-29 12:35:22 -0800439
440 mKeyguardUpdateMonitor.dispatchStartedWakingUp();
441 mTestableLooper.processAllMessages();
442 mKeyguardUpdateMonitor.onKeyguardVisibilityChanged(true);
Lucas Dupin8eec2682019-07-01 16:41:17 -0700443 verify(mFaceManager, never()).authenticate(any(), any(), anyInt(), any(), any(), anyInt());
444 }
445
446 @Test
447 public void requiresAuthentication_whenEncryptedKeyguard_andBypass() {
448 testStrongAuthExceptOnBouncer(
449 KeyguardUpdateMonitor.StrongAuthTracker.STRONG_AUTH_REQUIRED_AFTER_BOOT);
450 }
451
452 @Test
453 public void requiresAuthentication_whenTimeoutKeyguard_andBypass() {
454 testStrongAuthExceptOnBouncer(
455 KeyguardUpdateMonitor.StrongAuthTracker.STRONG_AUTH_REQUIRED_AFTER_TIMEOUT);
456 }
457
458 private void testStrongAuthExceptOnBouncer(int strongAuth) {
459 when(mKeyguardBypassController.canBypass()).thenReturn(true);
460 mKeyguardUpdateMonitor.setKeyguardBypassController(mKeyguardBypassController);
461 when(mStrongAuthTracker.getStrongAuthForUser(anyInt())).thenReturn(strongAuth);
462
463 mKeyguardUpdateMonitor.dispatchStartedWakingUp();
464 mTestableLooper.processAllMessages();
465 mKeyguardUpdateMonitor.onKeyguardVisibilityChanged(true);
466 verify(mFaceManager).authenticate(any(), any(), anyInt(), any(), any(), anyInt());
467
468 // Stop scanning when bouncer becomes visible
469 mKeyguardUpdateMonitor.sendKeyguardBouncerChanged(true /* showingBouncer */);
470 mTestableLooper.processAllMessages();
471 clearInvocations(mFaceManager);
472 mKeyguardUpdateMonitor.requestFaceAuth();
473 verify(mFaceManager, never()).authenticate(any(), any(), anyInt(), any(), any(), anyInt());
Lucas Dupin3d053532019-01-29 12:35:22 -0800474 }
475
476 @Test
477 public void testTriesToAuthenticate_whenAssistant() {
478 mKeyguardUpdateMonitor.setKeyguardOccluded(true);
479 mKeyguardUpdateMonitor.setAssistantVisible(true);
480
Kevin Chyn8d2694a2019-04-11 18:30:40 -0700481 verify(mFaceManager).authenticate(any(), any(), anyInt(), any(), any(), anyInt());
Lucas Dupin3d053532019-01-29 12:35:22 -0800482 }
483
484 @Test
Lucas Dupin4befb742019-07-01 11:31:31 -0700485 public void testTriesToAuthenticate_whenTrustOnAgentKeyguard_ifBypass() {
486 mKeyguardUpdateMonitor.setKeyguardBypassController(mKeyguardBypassController);
487 mKeyguardUpdateMonitor.dispatchStartedWakingUp();
488 mTestableLooper.processAllMessages();
489 when(mKeyguardBypassController.canBypass()).thenReturn(true);
490 mKeyguardUpdateMonitor.onTrustChanged(true /* enabled */,
491 KeyguardUpdateMonitor.getCurrentUser(), 0 /* flags */);
492 mKeyguardUpdateMonitor.onKeyguardVisibilityChanged(true);
493 verify(mFaceManager).authenticate(any(), any(), anyInt(), any(), any(), anyInt());
494 }
495
496 @Test
497 public void testIgnoresAuth_whenTrustAgentOnKeyguard_withoutBypass() {
498 mKeyguardUpdateMonitor.dispatchStartedWakingUp();
499 mTestableLooper.processAllMessages();
500 mKeyguardUpdateMonitor.onTrustChanged(true /* enabled */,
501 KeyguardUpdateMonitor.getCurrentUser(), 0 /* flags */);
502 mKeyguardUpdateMonitor.onKeyguardVisibilityChanged(true);
503 verify(mFaceManager, never()).authenticate(any(), any(), anyInt(), any(), any(), anyInt());
504 }
505
506 @Test
Lucas Dupin8eec2682019-07-01 16:41:17 -0700507 public void testIgnoresAuth_whenLockdown() {
508 mKeyguardUpdateMonitor.dispatchStartedWakingUp();
509 mTestableLooper.processAllMessages();
510 when(mStrongAuthTracker.getStrongAuthForUser(anyInt())).thenReturn(
511 KeyguardUpdateMonitor.StrongAuthTracker.STRONG_AUTH_REQUIRED_AFTER_USER_LOCKDOWN);
512
513 mKeyguardUpdateMonitor.onKeyguardVisibilityChanged(true);
514 verify(mFaceManager, never()).authenticate(any(), any(), anyInt(), any(), any(), anyInt());
515 }
516
517 @Test
Curtis Belmontec47df862020-01-28 16:58:34 -0800518 public void testTriesToAuthenticate_whenLockout() {
Lucas Dupin8eec2682019-07-01 16:41:17 -0700519 mKeyguardUpdateMonitor.dispatchStartedWakingUp();
520 mTestableLooper.processAllMessages();
521 when(mStrongAuthTracker.getStrongAuthForUser(anyInt())).thenReturn(
522 KeyguardUpdateMonitor.StrongAuthTracker.STRONG_AUTH_REQUIRED_AFTER_LOCKOUT);
523
524 mKeyguardUpdateMonitor.onKeyguardVisibilityChanged(true);
Curtis Belmontec47df862020-01-28 16:58:34 -0800525 verify(mFaceManager).authenticate(any(), any(), anyInt(), any(), any(), anyInt());
Lucas Dupin8eec2682019-07-01 16:41:17 -0700526 }
527
528 @Test
Lucas Dupin3d053532019-01-29 12:35:22 -0800529 public void testOnFaceAuthenticated_skipsFaceWhenAuthenticated() {
Haining Chenc06c4812020-01-13 20:38:53 -0800530 // test whether face will be skipped if authenticated, so the value of isStrongBiometric
531 // doesn't matter here
532 mKeyguardUpdateMonitor.onFaceAuthenticated(KeyguardUpdateMonitor.getCurrentUser(),
533 true /* isStrongBiometric */);
Lucas Dupin3d053532019-01-29 12:35:22 -0800534 mKeyguardUpdateMonitor.sendKeyguardBouncerChanged(true);
535 mTestableLooper.processAllMessages();
536
537 verify(mFaceManager, never()).authenticate(any(), any(), anyInt(), any(), any());
538 }
539
540 @Test
541 public void testGetUserCanSkipBouncer_whenFace() {
542 int user = KeyguardUpdateMonitor.getCurrentUser();
Haining Chenc06c4812020-01-13 20:38:53 -0800543 mKeyguardUpdateMonitor.onFaceAuthenticated(user, true /* isStrongBiometric */);
Lucas Dupin3d053532019-01-29 12:35:22 -0800544 assertThat(mKeyguardUpdateMonitor.getUserCanSkipBouncer(user)).isTrue();
545 }
546
547 @Test
Haining Chenc06c4812020-01-13 20:38:53 -0800548 public void testGetUserCanSkipBouncer_whenFace_nonStrongAndDisallowed() {
549 when(mStrongAuthTracker.isUnlockingWithBiometricAllowed(false /* isStrongBiometric */))
550 .thenReturn(false);
551 int user = KeyguardUpdateMonitor.getCurrentUser();
552 mKeyguardUpdateMonitor.onFaceAuthenticated(user, false /* isStrongBiometric */);
553 assertThat(mKeyguardUpdateMonitor.getUserCanSkipBouncer(user)).isFalse();
554 }
555
556 @Test
Lucas Dupin3d053532019-01-29 12:35:22 -0800557 public void testGetUserCanSkipBouncer_whenFingerprint() {
558 int user = KeyguardUpdateMonitor.getCurrentUser();
Haining Chenc06c4812020-01-13 20:38:53 -0800559 mKeyguardUpdateMonitor.onFingerprintAuthenticated(user, true /* isStrongBiometric */);
Lucas Dupin3d053532019-01-29 12:35:22 -0800560 assertThat(mKeyguardUpdateMonitor.getUserCanSkipBouncer(user)).isTrue();
561 }
562
563 @Test
Haining Chenc06c4812020-01-13 20:38:53 -0800564 public void testGetUserCanSkipBouncer_whenFingerprint_nonStrongAndDisallowed() {
565 when(mStrongAuthTracker.isUnlockingWithBiometricAllowed(false /* isStrongBiometric */))
566 .thenReturn(false);
567 int user = KeyguardUpdateMonitor.getCurrentUser();
568 mKeyguardUpdateMonitor.onFingerprintAuthenticated(user, false /* isStrongBiometric */);
569 assertThat(mKeyguardUpdateMonitor.getUserCanSkipBouncer(user)).isFalse();
570 }
571
572 @Test
Kevin Chyn505eb892020-03-26 13:07:03 -0700573 public void testBiometricsCleared_whenUserSwitches() throws Exception {
574 final IRemoteCallback reply = new IRemoteCallback.Stub() {
575 @Override
576 public void sendResult(Bundle data) {} // do nothing
577 };
578 final BiometricAuthenticated dummyAuthentication =
579 new BiometricAuthenticated(true /* authenticated */, true /* strong */);
580 mKeyguardUpdateMonitor.mUserFaceAuthenticated.put(0 /* user */, dummyAuthentication);
581 mKeyguardUpdateMonitor.mUserFingerprintAuthenticated.put(0 /* user */, dummyAuthentication);
582 assertThat(mKeyguardUpdateMonitor.mUserFingerprintAuthenticated.size()).isEqualTo(1);
583 assertThat(mKeyguardUpdateMonitor.mUserFaceAuthenticated.size()).isEqualTo(1);
584
585 mKeyguardUpdateMonitor.handleUserSwitching(10 /* user */, reply);
586 assertThat(mKeyguardUpdateMonitor.mUserFingerprintAuthenticated.size()).isEqualTo(0);
587 assertThat(mKeyguardUpdateMonitor.mUserFaceAuthenticated.size()).isEqualTo(0);
588 }
589
590 @Test
Lucas Dupin3d053532019-01-29 12:35:22 -0800591 public void testGetUserCanSkipBouncer_whenTrust() {
592 int user = KeyguardUpdateMonitor.getCurrentUser();
593 mKeyguardUpdateMonitor.onTrustChanged(true /* enabled */, user, 0 /* flags */);
594 assertThat(mKeyguardUpdateMonitor.getUserCanSkipBouncer(user)).isTrue();
Bill Linef81cbd2018-07-05 17:48:49 +0800595 }
596
Malcolm Chen5c63b512019-08-13 13:24:07 -0700597 @Test
598 public void testGetSubscriptionInfo_whenInGroupedSubWithOpportunistic() {
599 List<SubscriptionInfo> list = new ArrayList<>();
600 list.add(TEST_SUBSCRIPTION);
601 list.add(TEST_SUBSCRIPTION_2);
Sooraj Sasindran40468062020-01-06 19:10:50 -0800602 when(mSubscriptionManager.getCompleteActiveSubscriptionInfoList()).thenReturn(list);
Malcolm Chen5c63b512019-08-13 13:24:07 -0700603 mKeyguardUpdateMonitor.mPhoneStateListener.onActiveDataSubscriptionIdChanged(
604 TEST_SUBSCRIPTION_2.getSubscriptionId());
605 mTestableLooper.processAllMessages();
606
607 List<SubscriptionInfo> listToVerify = mKeyguardUpdateMonitor
608 .getFilteredSubscriptionInfo(false);
609 assertThat(listToVerify.size()).isEqualTo(1);
610 assertThat(listToVerify.get(0)).isEqualTo(TEST_SUBSCRIPTION_2);
611 }
612
Lucas Dupin6c6d5732019-08-27 17:36:05 -0700613 @Test
614 public void testIsUserUnlocked() {
615 // mUserManager will report the user as unlocked on @Before
616 assertThat(mKeyguardUpdateMonitor.isUserUnlocked(KeyguardUpdateMonitor.getCurrentUser()))
617 .isTrue();
618 // Invalid user should not be unlocked.
619 int randomUser = 99;
620 assertThat(mKeyguardUpdateMonitor.isUserUnlocked(randomUser)).isFalse();
621 }
622
Lucas Dupinf2c53502019-10-03 13:56:18 -0700623 @Test
624 public void testTrustUsuallyManaged_whenTrustChanges() {
625 int user = KeyguardUpdateMonitor.getCurrentUser();
626 when(mTrustManager.isTrustUsuallyManaged(eq(user))).thenReturn(true);
627 mKeyguardUpdateMonitor.onTrustManagedChanged(false /* managed */, user);
628 assertThat(mKeyguardUpdateMonitor.isTrustUsuallyManaged(user)).isTrue();
629 }
630
631 @Test
632 public void testTrustUsuallyManaged_resetWhenUserIsRemoved() {
633 int user = KeyguardUpdateMonitor.getCurrentUser();
634 when(mTrustManager.isTrustUsuallyManaged(eq(user))).thenReturn(true);
635 mKeyguardUpdateMonitor.onTrustManagedChanged(false /* managed */, user);
636 assertThat(mKeyguardUpdateMonitor.isTrustUsuallyManaged(user)).isTrue();
637
638 mKeyguardUpdateMonitor.handleUserRemoved(user);
639 assertThat(mKeyguardUpdateMonitor.isTrustUsuallyManaged(user)).isFalse();
640 }
641
Yvonne Jiangb7024a22019-12-05 16:57:08 -0800642 @Test
643 public void testSecondaryLockscreenRequirement() {
644 int user = KeyguardUpdateMonitor.getCurrentUser();
645 String packageName = "fake.test.package";
646 String cls = "FakeService";
647 ServiceInfo serviceInfo = new ServiceInfo();
648 serviceInfo.packageName = packageName;
649 serviceInfo.name = cls;
650 ResolveInfo resolveInfo = new ResolveInfo();
651 resolveInfo.serviceInfo = serviceInfo;
652 when(mPackageManager.resolveService(any(Intent.class), eq(0))).thenReturn(resolveInfo);
Yvonne Jiang8345da32020-03-19 15:01:16 -0700653 when(mDevicePolicyManager.isSecondaryLockscreenEnabled(eq(UserHandle.of(user))))
654 .thenReturn(true, false);
Yvonne Jiang70a62372020-03-31 16:51:17 -0700655 when(mDevicePolicyManager.getProfileOwnerOrDeviceOwnerSupervisionComponent(
656 UserHandle.of(user)))
Yvonne Jiang8345da32020-03-19 15:01:16 -0700657 .thenReturn(new ComponentName(packageName, cls));
Yvonne Jiangb7024a22019-12-05 16:57:08 -0800658
659 // Initially null.
660 assertThat(mKeyguardUpdateMonitor.getSecondaryLockscreenRequirement(user)).isNull();
661
662 // Set non-null after DPM change.
663 setBroadcastReceiverPendingResult(mKeyguardUpdateMonitor.mBroadcastAllReceiver);
664 Intent intent = new Intent(DevicePolicyManager.ACTION_DEVICE_POLICY_MANAGER_STATE_CHANGED);
665 mKeyguardUpdateMonitor.mBroadcastAllReceiver.onReceive(getContext(), intent);
666 mTestableLooper.processAllMessages();
667
668 Intent storedIntent = mKeyguardUpdateMonitor.getSecondaryLockscreenRequirement(user);
669 assertThat(storedIntent.getComponent().getClassName()).isEqualTo(cls);
670 assertThat(storedIntent.getComponent().getPackageName()).isEqualTo(packageName);
671
672 // Back to null after another DPM change.
673 mKeyguardUpdateMonitor.mBroadcastAllReceiver.onReceive(getContext(), intent);
674 mTestableLooper.processAllMessages();
675 assertThat(mKeyguardUpdateMonitor.getSecondaryLockscreenRequirement(user)).isNull();
676 }
677
Fabian Kozynskib6a20372020-04-01 09:36:43 -0400678 @Test
679 public void testRingerModeChange() {
680 ArgumentCaptor<Observer<Integer>> captor = ArgumentCaptor.forClass(Observer.class);
681 verify(mRingerModeLiveData).observeForever(captor.capture());
682 Observer<Integer> observer = captor.getValue();
683
684 KeyguardUpdateMonitorCallback callback = mock(KeyguardUpdateMonitorCallback.class);
685
686 mKeyguardUpdateMonitor.registerCallback(callback);
687
688 observer.onChanged(AudioManager.RINGER_MODE_NORMAL);
689 observer.onChanged(AudioManager.RINGER_MODE_SILENT);
690 observer.onChanged(AudioManager.RINGER_MODE_VIBRATE);
691
692 mTestableLooper.processAllMessages();
693
694 InOrder orderVerify = inOrder(callback);
695 orderVerify.verify(callback).onRingerModeChanged(anyInt()); // Initial update on register
696 orderVerify.verify(callback).onRingerModeChanged(AudioManager.RINGER_MODE_NORMAL);
697 orderVerify.verify(callback).onRingerModeChanged(AudioManager.RINGER_MODE_SILENT);
698 orderVerify.verify(callback).onRingerModeChanged(AudioManager.RINGER_MODE_VIBRATE);
699 }
700
Yvonne Jiangb7024a22019-12-05 16:57:08 -0800701 private void setBroadcastReceiverPendingResult(BroadcastReceiver receiver) {
702 BroadcastReceiver.PendingResult pendingResult =
703 new BroadcastReceiver.PendingResult(Activity.RESULT_OK,
704 "resultData",
705 /* resultExtras= */ null,
706 BroadcastReceiver.PendingResult.TYPE_UNREGISTERED,
707 /* ordered= */ true,
708 /* sticky= */ false,
709 /* token= */ null,
710 UserHandle.myUserId(),
711 /* flags= */ 0);
712 receiver.setPendingResult(pendingResult);
713 }
714
Bill Linef81cbd2018-07-05 17:48:49 +0800715 private Intent putPhoneInfo(Intent intent, Bundle data, Boolean simInited) {
716 int subscription = simInited
717 ? 1/* mock subid=1 */ : SubscriptionManager.DUMMY_SUBSCRIPTION_ID_BASE;
718 if (data != null) intent.putExtras(data);
Daniel Bright63514be2020-01-15 12:10:53 -0800719
720 intent.putExtra(SubscriptionManager.EXTRA_SUBSCRIPTION_INDEX, subscription);
721 intent.putExtra(SubscriptionManager.EXTRA_SLOT_INDEX, 0);
Bill Linef81cbd2018-07-05 17:48:49 +0800722 return intent;
723 }
724
Lucas Dupin7ff82b02018-05-16 12:14:10 -0700725 private class TestableKeyguardUpdateMonitor extends KeyguardUpdateMonitor {
726 AtomicBoolean mSimStateChanged = new AtomicBoolean(false);
727
728 protected TestableKeyguardUpdateMonitor(Context context) {
Fabian Kozynski5ca7a512019-10-16 19:56:11 +0000729 super(context,
730 TestableLooper.get(KeyguardUpdateMonitorTest.this).getLooper(),
Fabian Kozynskib6a20372020-04-01 09:36:43 -0400731 mBroadcastDispatcher, mDumpManager,
Curtis Belmonte9dc68152020-05-08 17:12:13 -0700732 mRingerModeTracker, mBackgroundExecutor, mStatusBarStateController);
Lucas Dupin3d053532019-01-29 12:35:22 -0800733 mStrongAuthTracker = KeyguardUpdateMonitorTest.this.mStrongAuthTracker;
Lucas Dupin7ff82b02018-05-16 12:14:10 -0700734 }
735
736 public boolean hasSimStateJustChanged() {
737 return mSimStateChanged.getAndSet(false);
738 }
739
740 @Override
Jayachandran Cf5436a62019-11-08 18:22:45 -0800741 protected void handleSimStateChange(int subId, int slotId, int state) {
Lucas Dupin7ff82b02018-05-16 12:14:10 -0700742 mSimStateChanged.set(true);
743 super.handleSimStateChange(subId, slotId, state);
744 }
Lucas Dupin5e0f0d22018-02-26 13:32:16 -0800745 }
746}