blob: 9e056cf16ec7265e9094b76c941f5ded57cc8eb5 [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;
Beverlyf882ee22020-05-11 16:12:09 -040041import android.app.trust.IStrongAuthTracker;
Lucas Dupin3d053532019-01-29 12:35:22 -080042import android.app.trust.TrustManager;
Yvonne Jiangb7024a22019-12-05 16:57:08 -080043import android.content.BroadcastReceiver;
Yvonne Jiang8345da32020-03-19 15:01:16 -070044import android.content.ComponentName;
Lucas Dupin7ff82b02018-05-16 12:14:10 -070045import android.content.Context;
Lucas Dupin5e0f0d22018-02-26 13:32:16 -080046import android.content.Intent;
Fabian Kozynski5ca7a512019-10-16 19:56:11 +000047import android.content.IntentFilter;
Lucas Dupin3d053532019-01-29 12:35:22 -080048import android.content.pm.PackageManager;
Yvonne Jiangb7024a22019-12-05 16:57:08 -080049import android.content.pm.ResolveInfo;
50import android.content.pm.ServiceInfo;
Lucas Dupin3d053532019-01-29 12:35:22 -080051import android.hardware.biometrics.BiometricManager;
52import android.hardware.biometrics.BiometricSourceType;
53import android.hardware.biometrics.IBiometricEnabledOnKeyguardCallback;
54import android.hardware.face.FaceManager;
55import android.hardware.fingerprint.FingerprintManager;
Fabian Kozynskib6a20372020-04-01 09:36:43 -040056import android.media.AudioManager;
Bill Linef81cbd2018-07-05 17:48:49 +080057import android.os.Bundle;
Fabian Kozynski5ca7a512019-10-16 19:56:11 +000058import android.os.Handler;
Kevin Chyn505eb892020-03-26 13:07:03 -070059import android.os.IRemoteCallback;
Fabian Kozynski5ca7a512019-10-16 19:56:11 +000060import android.os.UserHandle;
Lucas Dupin3d053532019-01-29 12:35:22 -080061import android.os.UserManager;
Bill Linef81cbd2018-07-05 17:48:49 +080062import android.telephony.ServiceState;
Malcolm Chen5c63b512019-08-13 13:24:07 -070063import android.telephony.SubscriptionInfo;
Bill Linef81cbd2018-07-05 17:48:49 +080064import android.telephony.SubscriptionManager;
Fabian Kozynskib00c70b2020-04-03 12:41:31 -040065import android.telephony.TelephonyManager;
Lucas Dupin5e0f0d22018-02-26 13:32:16 -080066import android.test.suitebuilder.annotation.SmallTest;
67import android.testing.AndroidTestingRunner;
Lucas Dupin3d053532019-01-29 12:35:22 -080068import android.testing.TestableContext;
Lucas Dupin5e0f0d22018-02-26 13:32:16 -080069import android.testing.TestableLooper;
Lucas Dupin5e0f0d22018-02-26 13:32:16 -080070
Fabian Kozynskib6a20372020-04-01 09:36:43 -040071import androidx.lifecycle.LiveData;
72import androidx.lifecycle.Observer;
73
Hall Liu45066122020-04-07 15:37:44 -070074import com.android.dx.mockito.inline.extended.ExtendedMockito;
Lucas Dupin5e0f0d22018-02-26 13:32:16 -080075import com.android.internal.telephony.TelephonyIntents;
Beverlyf882ee22020-05-11 16:12:09 -040076import com.android.internal.widget.ILockSettings;
77import com.android.internal.widget.LockPatternUtils;
Kevin Chyn505eb892020-03-26 13:07:03 -070078import com.android.keyguard.KeyguardUpdateMonitor.BiometricAuthenticated;
Lucas Dupin5e0f0d22018-02-26 13:32:16 -080079import com.android.systemui.SysuiTestCase;
Fabian Kozynski5ca7a512019-10-16 19:56:11 +000080import com.android.systemui.broadcast.BroadcastDispatcher;
Ned Burnsaaeb44b2020-02-12 23:48:26 -050081import com.android.systemui.dump.DumpManager;
Curtis Belmonte9dc68152020-05-08 17:12:13 -070082import com.android.systemui.plugins.statusbar.StatusBarStateController;
83import com.android.systemui.statusbar.StatusBarState;
Lucas Dupin4befb742019-07-01 11:31:31 -070084import com.android.systemui.statusbar.phone.KeyguardBypassController;
Fabian Kozynskib6a20372020-04-01 09:36:43 -040085import com.android.systemui.util.RingerModeTracker;
Lucas Dupin5e0f0d22018-02-26 13:32:16 -080086
Beverly4e7dd9a2020-02-28 17:04:21 -050087import org.junit.After;
Lucas Dupin5e0f0d22018-02-26 13:32:16 -080088import org.junit.Assert;
89import org.junit.Before;
90import org.junit.Test;
91import org.junit.runner.RunWith;
Fabian Kozynskib6a20372020-04-01 09:36:43 -040092import org.mockito.ArgumentCaptor;
93import org.mockito.InOrder;
Lucas Dupin3d053532019-01-29 12:35:22 -080094import org.mockito.Mock;
95import org.mockito.MockitoAnnotations;
Hall Liu45066122020-04-07 15:37:44 -070096import org.mockito.MockitoSession;
Lucas Dupin5e0f0d22018-02-26 13:32:16 -080097
Malcolm Chen5c63b512019-08-13 13:24:07 -070098import java.util.ArrayList;
99import java.util.List;
Haining Chenc06c4812020-01-13 20:38:53 -0800100import java.util.concurrent.Executor;
Lucas Dupin5e0f0d22018-02-26 13:32:16 -0800101import java.util.concurrent.atomic.AtomicBoolean;
102
103@SmallTest
104@RunWith(AndroidTestingRunner.class)
Dave Mankoffe2294692019-08-14 11:53:13 -0400105@TestableLooper.RunWithLooper
Lucas Dupin5e0f0d22018-02-26 13:32:16 -0800106public class KeyguardUpdateMonitorTest extends SysuiTestCase {
Malcolm Chen5c63b512019-08-13 13:24:07 -0700107 private static final String TEST_CARRIER = "TEST_CARRIER";
108 private static final String TEST_CARRIER_2 = "TEST_CARRIER_2";
109 private static final int TEST_CARRIER_ID = 1;
110 private static final String TEST_GROUP_UUID = "59b5c870-fc4c-47a4-a99e-9db826b48b24";
111 private static final SubscriptionInfo TEST_SUBSCRIPTION = new SubscriptionInfo(1, "", 0,
Peter Wangff7f25f2020-01-31 18:46:26 -0800112 TEST_CARRIER, TEST_CARRIER, NAME_SOURCE_CARRIER_ID, 0xFFFFFF, "",
Malcolm Chen5c63b512019-08-13 13:24:07 -0700113 DATA_ROAMING_DISABLE, null, null, null, null, false, null, "", false, TEST_GROUP_UUID,
114 TEST_CARRIER_ID, 0);
115 private static final SubscriptionInfo TEST_SUBSCRIPTION_2 = new SubscriptionInfo(2, "", 0,
Peter Wangff7f25f2020-01-31 18:46:26 -0800116 TEST_CARRIER, TEST_CARRIER_2, NAME_SOURCE_CARRIER_ID, 0xFFFFFF, "",
Malcolm Chen5c63b512019-08-13 13:24:07 -0700117 DATA_ROAMING_DISABLE, null, null, null, null, false, null, "", true, TEST_GROUP_UUID,
118 TEST_CARRIER_ID, 0);
Lucas Dupin3d053532019-01-29 12:35:22 -0800119 @Mock
Ned Burnsaaeb44b2020-02-12 23:48:26 -0500120 private DumpManager mDumpManager;
Lucas Dupin64171fe2019-10-30 14:28:29 -0700121 @Mock
Lucas Dupin3d053532019-01-29 12:35:22 -0800122 private KeyguardUpdateMonitor.StrongAuthTracker mStrongAuthTracker;
123 @Mock
124 private TrustManager mTrustManager;
125 @Mock
Beverlyf882ee22020-05-11 16:12:09 -0400126 private LockPatternUtils mLockPatternUtils;
127 @Mock
128 private ILockSettings mLockSettings;
129 @Mock
Lucas Dupin3d053532019-01-29 12:35:22 -0800130 private FingerprintManager mFingerprintManager;
131 @Mock
132 private FaceManager mFaceManager;
133 @Mock
134 private BiometricManager mBiometricManager;
135 @Mock
136 private PackageManager mPackageManager;
137 @Mock
138 private UserManager mUserManager;
139 @Mock
140 private DevicePolicyManager mDevicePolicyManager;
Lucas Dupin4befb742019-07-01 11:31:31 -0700141 @Mock
142 private KeyguardBypassController mKeyguardBypassController;
Malcolm Chen5c63b512019-08-13 13:24:07 -0700143 @Mock
144 private SubscriptionManager mSubscriptionManager;
Fabian Kozynski5ca7a512019-10-16 19:56:11 +0000145 @Mock
146 private BroadcastDispatcher mBroadcastDispatcher;
Haining Chenc06c4812020-01-13 20:38:53 -0800147 @Mock
Hall Liu45066122020-04-07 15:37:44 -0700148 private TelephonyManager mTelephonyManager;
149 @Mock
Fabian Kozynskib6a20372020-04-01 09:36:43 -0400150 private RingerModeTracker mRingerModeTracker;
151 @Mock
152 private LiveData<Integer> mRingerModeLiveData;
Curtis Belmonte9dc68152020-05-08 17:12:13 -0700153 @Mock
154 private StatusBarStateController mStatusBarStateController;
Fabian Kozynskib00c70b2020-04-03 12:41:31 -0400155 // Direct executor
156 private Executor mBackgroundExecutor = Runnable::run;
Lucas Dupin5e0f0d22018-02-26 13:32:16 -0800157 private TestableLooper mTestableLooper;
Lucas Dupin3d053532019-01-29 12:35:22 -0800158 private TestableKeyguardUpdateMonitor mKeyguardUpdateMonitor;
Fabian Kozynskib00c70b2020-04-03 12:41:31 -0400159 private TestableContext mSpiedContext;
Hall Liu45066122020-04-07 15:37:44 -0700160 private MockitoSession mMockitoSession;
Lucas Dupin5e0f0d22018-02-26 13:32:16 -0800161
162 @Before
163 public void setup() {
Lucas Dupin3d053532019-01-29 12:35:22 -0800164 MockitoAnnotations.initMocks(this);
Fabian Kozynskib00c70b2020-04-03 12:41:31 -0400165 mSpiedContext = spy(mContext);
Lucas Dupin3d053532019-01-29 12:35:22 -0800166 when(mPackageManager.hasSystemFeature(anyString())).thenReturn(true);
Fabian Kozynskib00c70b2020-04-03 12:41:31 -0400167 when(mSpiedContext.getPackageManager()).thenReturn(mPackageManager);
Lucas Dupin3d053532019-01-29 12:35:22 -0800168 doAnswer(invocation -> {
169 IBiometricEnabledOnKeyguardCallback callback = invocation.getArgument(0);
Lucas Dupin7d95f152019-07-17 16:25:54 -0700170 callback.onChanged(BiometricSourceType.FACE, true /* enabled */,
171 KeyguardUpdateMonitor.getCurrentUser());
Lucas Dupin3d053532019-01-29 12:35:22 -0800172 return null;
173 }).when(mBiometricManager).registerEnabledOnKeyguardCallback(any());
174 when(mFaceManager.isHardwareDetected()).thenReturn(true);
175 when(mFaceManager.hasEnrolledTemplates()).thenReturn(true);
176 when(mFaceManager.hasEnrolledTemplates(anyInt())).thenReturn(true);
177 when(mUserManager.isUserUnlocked(anyInt())).thenReturn(true);
Kevin Chynfdfd2d32019-03-01 14:52:15 -0800178 when(mUserManager.isPrimaryUser()).thenReturn(true);
Beverlyf882ee22020-05-11 16:12:09 -0400179 when(mStrongAuthTracker.getStub()).thenReturn(mock(IStrongAuthTracker.Stub.class));
Haining Chenc06c4812020-01-13 20:38:53 -0800180 when(mStrongAuthTracker
181 .isUnlockingWithBiometricAllowed(anyBoolean() /* isStrongBiometric */))
182 .thenReturn(true);
Hall Liu45066122020-04-07 15:37:44 -0700183 when(mTelephonyManager.getServiceStateForSubscriber(anyInt()))
184 .thenReturn(new ServiceState());
Beverlyf882ee22020-05-11 16:12:09 -0400185 when(mLockPatternUtils.getLockSettings()).thenReturn(mLockSettings);
Fabian Kozynskib00c70b2020-04-03 12:41:31 -0400186 mSpiedContext.addMockSystemService(TrustManager.class, mTrustManager);
187 mSpiedContext.addMockSystemService(FingerprintManager.class, mFingerprintManager);
188 mSpiedContext.addMockSystemService(BiometricManager.class, mBiometricManager);
189 mSpiedContext.addMockSystemService(FaceManager.class, mFaceManager);
190 mSpiedContext.addMockSystemService(UserManager.class, mUserManager);
191 mSpiedContext.addMockSystemService(DevicePolicyManager.class, mDevicePolicyManager);
192 mSpiedContext.addMockSystemService(SubscriptionManager.class, mSubscriptionManager);
193 mSpiedContext.addMockSystemService(TelephonyManager.class, mTelephonyManager);
Lucas Dupin3d053532019-01-29 12:35:22 -0800194
Fabian Kozynskib6a20372020-04-01 09:36:43 -0400195 when(mRingerModeTracker.getRingerMode()).thenReturn(mRingerModeLiveData);
196
Hall Liu45066122020-04-07 15:37:44 -0700197 mMockitoSession = ExtendedMockito.mockitoSession()
198 .spyStatic(SubscriptionManager.class).startMocking();
199 ExtendedMockito.doReturn(SubscriptionManager.INVALID_SUBSCRIPTION_ID)
200 .when(SubscriptionManager::getDefaultSubscriptionId);
201
Lucas Dupin5e0f0d22018-02-26 13:32:16 -0800202 mTestableLooper = TestableLooper.get(this);
Beverly1467c9e2020-02-18 13:31:29 -0500203 allowTestableLooperAsMainThread();
Fabian Kozynskib00c70b2020-04-03 12:41:31 -0400204 mKeyguardUpdateMonitor = new TestableKeyguardUpdateMonitor(mSpiedContext);
Lucas Dupin5e0f0d22018-02-26 13:32:16 -0800205 }
206
Beverly4e7dd9a2020-02-28 17:04:21 -0500207 @After
208 public void tearDown() {
Hall Liu45066122020-04-07 15:37:44 -0700209 mMockitoSession.finishMocking();
Beverly4e7dd9a2020-02-28 17:04:21 -0500210 mKeyguardUpdateMonitor.destroy();
211 }
212
Lucas Dupin5e0f0d22018-02-26 13:32:16 -0800213 @Test
Fabian Kozynski99abb712020-06-25 16:12:31 -0400214 public void testInitialBatteryLevelRequested() {
215 mTestableLooper.processAllMessages();
216
217 assertThat(mKeyguardUpdateMonitor.mBatteryStatus).isNotNull();
218 }
219
220 @Test
Fabian Kozynski5ca7a512019-10-16 19:56:11 +0000221 public void testReceiversRegistered() {
Fabian Kozynski5e92c6f2020-01-03 13:56:17 -0500222 verify(mBroadcastDispatcher, atLeastOnce()).registerReceiverWithHandler(
Fabian Kozynski5ca7a512019-10-16 19:56:11 +0000223 eq(mKeyguardUpdateMonitor.mBroadcastReceiver),
224 any(IntentFilter.class), any(Handler.class));
Fabian Kozynski5e92c6f2020-01-03 13:56:17 -0500225 verify(mBroadcastDispatcher, atLeastOnce()).registerReceiverWithHandler(
Fabian Kozynski5ca7a512019-10-16 19:56:11 +0000226 eq(mKeyguardUpdateMonitor.mBroadcastAllReceiver),
227 any(IntentFilter.class), any(Handler.class), eq(UserHandle.ALL));
228 }
229
230 @Test
Fabian Kozynskib00c70b2020-04-03 12:41:31 -0400231 public void testSimStateInitialized() {
232 final int subId = 3;
233 final int state = TelephonyManager.SIM_STATE_ABSENT;
234
235 when(mTelephonyManager.getActiveModemCount()).thenReturn(1);
236 when(mTelephonyManager.getSimState(anyInt())).thenReturn(state);
237 when(mSubscriptionManager.getSubscriptionIds(anyInt())).thenReturn(new int[] { subId });
238
239 KeyguardUpdateMonitor testKUM = new TestableKeyguardUpdateMonitor(mSpiedContext);
240
241 mTestableLooper.processAllMessages();
242
243 assertThat(testKUM.getSimState(subId)).isEqualTo(state);
244 }
245
246 @Test
Lucas Dupin5e0f0d22018-02-26 13:32:16 -0800247 public void testIgnoresSimStateCallback_rebroadcast() {
248 Intent intent = new Intent(TelephonyIntents.ACTION_SIM_STATE_CHANGED);
249
Lucas Dupin3d053532019-01-29 12:35:22 -0800250 mKeyguardUpdateMonitor.mBroadcastReceiver.onReceive(getContext(), intent);
Lucas Dupin5e0f0d22018-02-26 13:32:16 -0800251 mTestableLooper.processAllMessages();
Lucas Dupin7ff82b02018-05-16 12:14:10 -0700252 Assert.assertTrue("onSimStateChanged not called",
Lucas Dupin3d053532019-01-29 12:35:22 -0800253 mKeyguardUpdateMonitor.hasSimStateJustChanged());
Lucas Dupin5e0f0d22018-02-26 13:32:16 -0800254
Meng Wang0e8d91a2020-01-17 14:58:01 -0800255 intent.putExtra(Intent.EXTRA_REBROADCAST_ON_UNLOCK, true);
Lucas Dupin3d053532019-01-29 12:35:22 -0800256 mKeyguardUpdateMonitor.mBroadcastReceiver.onReceive(getContext(), intent);
Lucas Dupin5e0f0d22018-02-26 13:32:16 -0800257 mTestableLooper.processAllMessages();
Lucas Dupin7ff82b02018-05-16 12:14:10 -0700258 Assert.assertFalse("onSimStateChanged should have been skipped",
Lucas Dupin3d053532019-01-29 12:35:22 -0800259 mKeyguardUpdateMonitor.hasSimStateJustChanged());
Lucas Dupin7ff82b02018-05-16 12:14:10 -0700260 }
261
Bill Linef81cbd2018-07-05 17:48:49 +0800262 @Test
263 public void testTelephonyCapable_BootInitState() {
Lucas Dupin3d053532019-01-29 12:35:22 -0800264 assertThat(mKeyguardUpdateMonitor.mTelephonyCapable).isFalse();
Bill Linef81cbd2018-07-05 17:48:49 +0800265 }
266
267 @Test
268 public void testTelephonyCapable_SimState_Absent() {
269 Intent intent = new Intent(TelephonyIntents.ACTION_SIM_STATE_CHANGED);
Jayachandran C0173df82019-12-19 12:11:03 -0800270 intent.putExtra(Intent.EXTRA_SIM_STATE,
271 Intent.SIM_STATE_ABSENT);
andychou695a7602019-05-16 23:14:00 +0800272 mKeyguardUpdateMonitor.mBroadcastReceiver.onReceive(getContext(),
273 putPhoneInfo(intent, null, false));
274 mTestableLooper.processAllMessages();
275 assertThat(mKeyguardUpdateMonitor.mTelephonyCapable).isTrue();
276 }
277
278 @Test
279 public void testTelephonyCapable_SimState_CardIOError() {
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_CARD_IO_ERROR);
andychou695a7602019-05-16 23:14:00 +0800283 mKeyguardUpdateMonitor.mBroadcastReceiver.onReceive(getContext(),
284 putPhoneInfo(intent, null, false));
Bill Linef81cbd2018-07-05 17:48:49 +0800285 mTestableLooper.processAllMessages();
Lucas Dupin3d053532019-01-29 12:35:22 -0800286 assertThat(mKeyguardUpdateMonitor.mTelephonyCapable).isTrue();
Bill Linef81cbd2018-07-05 17:48:49 +0800287 }
288
289 @Test
290 public void testTelephonyCapable_SimInvalid_ServiceState_InService() {
291 // SERVICE_STATE - IN_SERVICE, but SIM_STATE is invalid TelephonyCapable should be False
Jayachandran C041e7692019-12-20 16:20:02 -0800292 Intent intent = new Intent(Intent.ACTION_SERVICE_STATE);
Bill Linef81cbd2018-07-05 17:48:49 +0800293 Bundle data = new Bundle();
294 ServiceState state = new ServiceState();
295 state.setState(ServiceState.STATE_IN_SERVICE);
296 state.fillInNotifierBundle(data);
Lucas Dupin3d053532019-01-29 12:35:22 -0800297 mKeyguardUpdateMonitor.mBroadcastReceiver.onReceive(getContext()
Bill Linef81cbd2018-07-05 17:48:49 +0800298 , putPhoneInfo(intent, data, false));
299 mTestableLooper.processAllMessages();
Lucas Dupin3d053532019-01-29 12:35:22 -0800300 assertThat(mKeyguardUpdateMonitor.mTelephonyCapable).isFalse();
Bill Linef81cbd2018-07-05 17:48:49 +0800301 }
302
303 @Test
304 public void testTelephonyCapable_SimValid_ServiceState_PowerOff() {
305 // Simulate AirplaneMode case, SERVICE_STATE - POWER_OFF, check TelephonyCapable False
306 // Only receive ServiceState callback IN_SERVICE -> OUT_OF_SERVICE -> POWER_OFF
Jayachandran C041e7692019-12-20 16:20:02 -0800307 Intent intent = new Intent(Intent.ACTION_SERVICE_STATE);
Jayachandran C0173df82019-12-19 12:11:03 -0800308 intent.putExtra(Intent.EXTRA_SIM_STATE
309 , Intent.SIM_STATE_LOADED);
Bill Linef81cbd2018-07-05 17:48:49 +0800310 Bundle data = new Bundle();
311 ServiceState state = new ServiceState();
312 state.setState(ServiceState.STATE_POWER_OFF);
313 state.fillInNotifierBundle(data);
Lucas Dupin3d053532019-01-29 12:35:22 -0800314 mKeyguardUpdateMonitor.mBroadcastReceiver.onReceive(getContext()
Bill Linef81cbd2018-07-05 17:48:49 +0800315 , putPhoneInfo(intent, data, true));
316 mTestableLooper.processAllMessages();
Lucas Dupin3d053532019-01-29 12:35:22 -0800317 assertThat(mKeyguardUpdateMonitor.mTelephonyCapable).isTrue();
Bill Linef81cbd2018-07-05 17:48:49 +0800318 }
319
320 /* Normal SIM inserted flow
321 * ServiceState: ---OutOfServie----->PowerOff->OutOfServie--->InService
322 * SimState: ----NOT_READY---->READY----------------------LOADED>>>
323 * Subscription: --------null---->null--->"Chunghwa Telecom"-------->>>
324 * System: -------------------------------BOOT_COMPLETED------>>>
325 * TelephonyCapable:(F)-(F)-(F)-(F)-(F)-(F)-(F)-(F)-(F)-(F)------(T)-(T)>>
326 */
327 @Test
328 public void testTelephonyCapable_BootInitState_ServiceState_OutOfService() {
Jayachandran C041e7692019-12-20 16:20:02 -0800329 Intent intent = new Intent(Intent.ACTION_SERVICE_STATE);
Bill Linef81cbd2018-07-05 17:48:49 +0800330 Bundle data = new Bundle();
331 ServiceState state = new ServiceState();
332 state.setState(ServiceState.STATE_OUT_OF_SERVICE);
333 state.fillInNotifierBundle(data);
334 intent.putExtras(data);
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_NotReady() {
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_NOT_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_SimState_Ready() {
Bill Linef81cbd2018-07-05 17:48:49 +0800358 Bundle data = new Bundle();
359 ServiceState state = new ServiceState();
360 state.setState(ServiceState.STATE_OUT_OF_SERVICE);
361 state.fillInNotifierBundle(data);
362 Intent intent = new Intent(TelephonyIntents.ACTION_SIM_STATE_CHANGED);
Jayachandran C0173df82019-12-19 12:11:03 -0800363 intent.putExtra(Intent.EXTRA_SIM_STATE
364 , Intent.SIM_STATE_READY);
Lucas Dupin3d053532019-01-29 12:35:22 -0800365 mKeyguardUpdateMonitor.mBroadcastReceiver.onReceive(getContext()
Bill Linef81cbd2018-07-05 17:48:49 +0800366 , putPhoneInfo(intent, data, false));
367 mTestableLooper.processAllMessages();
Lucas Dupin3d053532019-01-29 12:35:22 -0800368 assertThat(mKeyguardUpdateMonitor.mTelephonyCapable).isFalse();
Bill Linef81cbd2018-07-05 17:48:49 +0800369 }
370
371 @Test
372 public void testTelephonyCapable_BootInitState_ServiceState_PowerOff() {
Jayachandran C041e7692019-12-20 16:20:02 -0800373 Intent intent = new Intent(Intent.ACTION_SERVICE_STATE);
Bill Linef81cbd2018-07-05 17:48:49 +0800374 Bundle data = new Bundle();
375 ServiceState state = new ServiceState();
376 state.setState(ServiceState.STATE_POWER_OFF);
377 state.fillInNotifierBundle(data);
Lucas Dupin3d053532019-01-29 12:35:22 -0800378 mKeyguardUpdateMonitor.mBroadcastReceiver.onReceive(getContext()
Bill Linef81cbd2018-07-05 17:48:49 +0800379 , putPhoneInfo(intent, data, false));
380 mTestableLooper.processAllMessages();
Lucas Dupin3d053532019-01-29 12:35:22 -0800381 assertThat(mKeyguardUpdateMonitor.mTelephonyCapable).isFalse();
Bill Linef81cbd2018-07-05 17:48:49 +0800382 }
383
384 @Test
385 public void testTelephonyCapable_SimValid_ServiceState_InService() {
Jayachandran C041e7692019-12-20 16:20:02 -0800386 Intent intent = new Intent(Intent.ACTION_SERVICE_STATE);
Bill Linef81cbd2018-07-05 17:48:49 +0800387 Bundle data = new Bundle();
388 ServiceState state = new ServiceState();
389 state.setState(ServiceState.STATE_IN_SERVICE);
390 state.fillInNotifierBundle(data);
Lucas Dupin3d053532019-01-29 12:35:22 -0800391 mKeyguardUpdateMonitor.mBroadcastReceiver.onReceive(getContext()
Bill Linef81cbd2018-07-05 17:48:49 +0800392 , putPhoneInfo(intent, data, true));
393 mTestableLooper.processAllMessages();
Lucas Dupin3d053532019-01-29 12:35:22 -0800394 assertThat(mKeyguardUpdateMonitor.mTelephonyCapable).isTrue();
Bill Linef81cbd2018-07-05 17:48:49 +0800395 }
396
397 @Test
398 public void testTelephonyCapable_SimValid_SimState_Loaded() {
Bill Linef81cbd2018-07-05 17:48:49 +0800399 Bundle data = new Bundle();
400 ServiceState state = new ServiceState();
401 state.setState(ServiceState.STATE_IN_SERVICE);
402 state.fillInNotifierBundle(data);
403 Intent intentSimState = new Intent(TelephonyIntents.ACTION_SIM_STATE_CHANGED);
Jayachandran C0173df82019-12-19 12:11:03 -0800404 intentSimState.putExtra(Intent.EXTRA_SIM_STATE
405 , Intent.SIM_STATE_LOADED);
Lucas Dupin3d053532019-01-29 12:35:22 -0800406 mKeyguardUpdateMonitor.mBroadcastReceiver.onReceive(getContext()
Bill Linef81cbd2018-07-05 17:48:49 +0800407 , putPhoneInfo(intentSimState, data, true));
408 mTestableLooper.processAllMessages();
Jayachandran C041e7692019-12-20 16:20:02 -0800409 // Even SimState Loaded, still need ACTION_SERVICE_STATE turn on mTelephonyCapable
Lucas Dupin3d053532019-01-29 12:35:22 -0800410 assertThat(mKeyguardUpdateMonitor.mTelephonyCapable).isFalse();
Bill Linef81cbd2018-07-05 17:48:49 +0800411
Jayachandran C041e7692019-12-20 16:20:02 -0800412 Intent intentServiceState = new Intent(Intent.ACTION_SERVICE_STATE);
Jayachandran C0173df82019-12-19 12:11:03 -0800413 intentSimState.putExtra(Intent.EXTRA_SIM_STATE
414 , Intent.SIM_STATE_LOADED);
Lucas Dupin3d053532019-01-29 12:35:22 -0800415 mKeyguardUpdateMonitor.mBroadcastReceiver.onReceive(getContext()
Bill Linef81cbd2018-07-05 17:48:49 +0800416 , putPhoneInfo(intentServiceState, data, true));
417 mTestableLooper.processAllMessages();
Lucas Dupin3d053532019-01-29 12:35:22 -0800418 assertThat(mKeyguardUpdateMonitor.mTelephonyCapable).isTrue();
419 }
420
421 @Test
422 public void testTriesToAuthenticate_whenBouncer() {
423 mKeyguardUpdateMonitor.sendKeyguardBouncerChanged(true);
424 mTestableLooper.processAllMessages();
425
Kevin Chyn8d2694a2019-04-11 18:30:40 -0700426 verify(mFaceManager).authenticate(any(), any(), anyInt(), any(), any(), anyInt());
Lucas Dupin3d053532019-01-29 12:35:22 -0800427 verify(mFaceManager).isHardwareDetected();
428 verify(mFaceManager).hasEnrolledTemplates(anyInt());
429 }
430
431 @Test
432 public void testTriesToAuthenticate_whenKeyguard() {
433 mKeyguardUpdateMonitor.dispatchStartedWakingUp();
434 mTestableLooper.processAllMessages();
435 mKeyguardUpdateMonitor.onKeyguardVisibilityChanged(true);
Kevin Chyn8d2694a2019-04-11 18:30:40 -0700436 verify(mFaceManager).authenticate(any(), any(), anyInt(), any(), any(), anyInt());
Lucas Dupin3d053532019-01-29 12:35:22 -0800437 }
438
439 @Test
Curtis Belmonte9dc68152020-05-08 17:12:13 -0700440 public void skipsAuthentication_whenStatusBarShadeLocked() {
441 when(mStatusBarStateController.getState()).thenReturn(StatusBarState.SHADE_LOCKED);
442
443 mKeyguardUpdateMonitor.dispatchStartedWakingUp();
444 mTestableLooper.processAllMessages();
445 mKeyguardUpdateMonitor.onKeyguardVisibilityChanged(true);
446 verify(mFaceManager, never()).authenticate(any(), any(), anyInt(), any(), any(), anyInt());
447 }
448
449 @Test
Lucas Dupin3d053532019-01-29 12:35:22 -0800450 public void skipsAuthentication_whenEncryptedKeyguard() {
Lucas Dupin8eec2682019-07-01 16:41:17 -0700451 when(mStrongAuthTracker.getStrongAuthForUser(anyInt())).thenReturn(
452 KeyguardUpdateMonitor.StrongAuthTracker.STRONG_AUTH_REQUIRED_AFTER_BOOT);
453 mKeyguardUpdateMonitor.setKeyguardBypassController(mKeyguardBypassController);
Lucas Dupin3d053532019-01-29 12:35:22 -0800454
455 mKeyguardUpdateMonitor.dispatchStartedWakingUp();
456 mTestableLooper.processAllMessages();
457 mKeyguardUpdateMonitor.onKeyguardVisibilityChanged(true);
Lucas Dupin8eec2682019-07-01 16:41:17 -0700458 verify(mFaceManager, never()).authenticate(any(), any(), anyInt(), any(), any(), anyInt());
459 }
460
461 @Test
Kevin Chyn35eb8a12020-06-18 04:48:57 +0000462 public void requiresAuthentication_whenEncryptedKeyguard_andBypass() {
463 testStrongAuthExceptOnBouncer(
464 KeyguardUpdateMonitor.StrongAuthTracker.STRONG_AUTH_REQUIRED_AFTER_BOOT);
465 }
466
467 @Test
Lucas Dupin8eec2682019-07-01 16:41:17 -0700468 public void requiresAuthentication_whenTimeoutKeyguard_andBypass() {
469 testStrongAuthExceptOnBouncer(
470 KeyguardUpdateMonitor.StrongAuthTracker.STRONG_AUTH_REQUIRED_AFTER_TIMEOUT);
471 }
472
473 private void testStrongAuthExceptOnBouncer(int strongAuth) {
474 when(mKeyguardBypassController.canBypass()).thenReturn(true);
475 mKeyguardUpdateMonitor.setKeyguardBypassController(mKeyguardBypassController);
476 when(mStrongAuthTracker.getStrongAuthForUser(anyInt())).thenReturn(strongAuth);
477
478 mKeyguardUpdateMonitor.dispatchStartedWakingUp();
479 mTestableLooper.processAllMessages();
480 mKeyguardUpdateMonitor.onKeyguardVisibilityChanged(true);
481 verify(mFaceManager).authenticate(any(), any(), anyInt(), any(), any(), anyInt());
482
483 // Stop scanning when bouncer becomes visible
484 mKeyguardUpdateMonitor.sendKeyguardBouncerChanged(true /* showingBouncer */);
485 mTestableLooper.processAllMessages();
486 clearInvocations(mFaceManager);
487 mKeyguardUpdateMonitor.requestFaceAuth();
488 verify(mFaceManager, never()).authenticate(any(), any(), anyInt(), any(), any(), anyInt());
Lucas Dupin3d053532019-01-29 12:35:22 -0800489 }
490
491 @Test
492 public void testTriesToAuthenticate_whenAssistant() {
493 mKeyguardUpdateMonitor.setKeyguardOccluded(true);
494 mKeyguardUpdateMonitor.setAssistantVisible(true);
495
Kevin Chyn8d2694a2019-04-11 18:30:40 -0700496 verify(mFaceManager).authenticate(any(), any(), anyInt(), any(), any(), anyInt());
Lucas Dupin3d053532019-01-29 12:35:22 -0800497 }
498
499 @Test
Lucas Dupin4befb742019-07-01 11:31:31 -0700500 public void testTriesToAuthenticate_whenTrustOnAgentKeyguard_ifBypass() {
501 mKeyguardUpdateMonitor.setKeyguardBypassController(mKeyguardBypassController);
502 mKeyguardUpdateMonitor.dispatchStartedWakingUp();
503 mTestableLooper.processAllMessages();
504 when(mKeyguardBypassController.canBypass()).thenReturn(true);
505 mKeyguardUpdateMonitor.onTrustChanged(true /* enabled */,
506 KeyguardUpdateMonitor.getCurrentUser(), 0 /* flags */);
507 mKeyguardUpdateMonitor.onKeyguardVisibilityChanged(true);
508 verify(mFaceManager).authenticate(any(), any(), anyInt(), any(), any(), anyInt());
509 }
510
511 @Test
512 public void testIgnoresAuth_whenTrustAgentOnKeyguard_withoutBypass() {
513 mKeyguardUpdateMonitor.dispatchStartedWakingUp();
514 mTestableLooper.processAllMessages();
515 mKeyguardUpdateMonitor.onTrustChanged(true /* enabled */,
516 KeyguardUpdateMonitor.getCurrentUser(), 0 /* flags */);
517 mKeyguardUpdateMonitor.onKeyguardVisibilityChanged(true);
518 verify(mFaceManager, never()).authenticate(any(), any(), anyInt(), any(), any(), anyInt());
519 }
520
521 @Test
Lucas Dupin8eec2682019-07-01 16:41:17 -0700522 public void testIgnoresAuth_whenLockdown() {
523 mKeyguardUpdateMonitor.dispatchStartedWakingUp();
524 mTestableLooper.processAllMessages();
Kevin Chyn35eb8a12020-06-18 04:48:57 +0000525 when(mStrongAuthTracker.getStrongAuthForUser(anyInt())).thenReturn(
526 KeyguardUpdateMonitor.StrongAuthTracker.STRONG_AUTH_REQUIRED_AFTER_USER_LOCKDOWN);
Lucas Dupin8eec2682019-07-01 16:41:17 -0700527
528 mKeyguardUpdateMonitor.onKeyguardVisibilityChanged(true);
529 verify(mFaceManager, never()).authenticate(any(), any(), anyInt(), any(), any(), anyInt());
530 }
531
532 @Test
Curtis Belmontec47df862020-01-28 16:58:34 -0800533 public void testTriesToAuthenticate_whenLockout() {
Lucas Dupin8eec2682019-07-01 16:41:17 -0700534 mKeyguardUpdateMonitor.dispatchStartedWakingUp();
535 mTestableLooper.processAllMessages();
536 when(mStrongAuthTracker.getStrongAuthForUser(anyInt())).thenReturn(
537 KeyguardUpdateMonitor.StrongAuthTracker.STRONG_AUTH_REQUIRED_AFTER_LOCKOUT);
538
539 mKeyguardUpdateMonitor.onKeyguardVisibilityChanged(true);
Curtis Belmontec47df862020-01-28 16:58:34 -0800540 verify(mFaceManager).authenticate(any(), any(), anyInt(), any(), any(), anyInt());
Lucas Dupin8eec2682019-07-01 16:41:17 -0700541 }
542
543 @Test
Lucas Dupin3d053532019-01-29 12:35:22 -0800544 public void testOnFaceAuthenticated_skipsFaceWhenAuthenticated() {
Haining Chenc06c4812020-01-13 20:38:53 -0800545 // test whether face will be skipped if authenticated, so the value of isStrongBiometric
546 // doesn't matter here
547 mKeyguardUpdateMonitor.onFaceAuthenticated(KeyguardUpdateMonitor.getCurrentUser(),
548 true /* isStrongBiometric */);
Lucas Dupin3d053532019-01-29 12:35:22 -0800549 mKeyguardUpdateMonitor.sendKeyguardBouncerChanged(true);
550 mTestableLooper.processAllMessages();
551
552 verify(mFaceManager, never()).authenticate(any(), any(), anyInt(), any(), any());
553 }
554
555 @Test
556 public void testGetUserCanSkipBouncer_whenFace() {
557 int user = KeyguardUpdateMonitor.getCurrentUser();
Haining Chenc06c4812020-01-13 20:38:53 -0800558 mKeyguardUpdateMonitor.onFaceAuthenticated(user, true /* isStrongBiometric */);
Lucas Dupin3d053532019-01-29 12:35:22 -0800559 assertThat(mKeyguardUpdateMonitor.getUserCanSkipBouncer(user)).isTrue();
560 }
561
562 @Test
Haining Chenc06c4812020-01-13 20:38:53 -0800563 public void testGetUserCanSkipBouncer_whenFace_nonStrongAndDisallowed() {
564 when(mStrongAuthTracker.isUnlockingWithBiometricAllowed(false /* isStrongBiometric */))
565 .thenReturn(false);
566 int user = KeyguardUpdateMonitor.getCurrentUser();
567 mKeyguardUpdateMonitor.onFaceAuthenticated(user, false /* isStrongBiometric */);
568 assertThat(mKeyguardUpdateMonitor.getUserCanSkipBouncer(user)).isFalse();
569 }
570
571 @Test
Lucas Dupin3d053532019-01-29 12:35:22 -0800572 public void testGetUserCanSkipBouncer_whenFingerprint() {
573 int user = KeyguardUpdateMonitor.getCurrentUser();
Haining Chenc06c4812020-01-13 20:38:53 -0800574 mKeyguardUpdateMonitor.onFingerprintAuthenticated(user, true /* isStrongBiometric */);
Lucas Dupin3d053532019-01-29 12:35:22 -0800575 assertThat(mKeyguardUpdateMonitor.getUserCanSkipBouncer(user)).isTrue();
576 }
577
578 @Test
Haining Chenc06c4812020-01-13 20:38:53 -0800579 public void testGetUserCanSkipBouncer_whenFingerprint_nonStrongAndDisallowed() {
580 when(mStrongAuthTracker.isUnlockingWithBiometricAllowed(false /* isStrongBiometric */))
581 .thenReturn(false);
582 int user = KeyguardUpdateMonitor.getCurrentUser();
583 mKeyguardUpdateMonitor.onFingerprintAuthenticated(user, false /* isStrongBiometric */);
584 assertThat(mKeyguardUpdateMonitor.getUserCanSkipBouncer(user)).isFalse();
585 }
586
587 @Test
Kevin Chyn505eb892020-03-26 13:07:03 -0700588 public void testBiometricsCleared_whenUserSwitches() throws Exception {
589 final IRemoteCallback reply = new IRemoteCallback.Stub() {
590 @Override
591 public void sendResult(Bundle data) {} // do nothing
592 };
593 final BiometricAuthenticated dummyAuthentication =
594 new BiometricAuthenticated(true /* authenticated */, true /* strong */);
595 mKeyguardUpdateMonitor.mUserFaceAuthenticated.put(0 /* user */, dummyAuthentication);
596 mKeyguardUpdateMonitor.mUserFingerprintAuthenticated.put(0 /* user */, dummyAuthentication);
597 assertThat(mKeyguardUpdateMonitor.mUserFingerprintAuthenticated.size()).isEqualTo(1);
598 assertThat(mKeyguardUpdateMonitor.mUserFaceAuthenticated.size()).isEqualTo(1);
599
600 mKeyguardUpdateMonitor.handleUserSwitching(10 /* user */, reply);
601 assertThat(mKeyguardUpdateMonitor.mUserFingerprintAuthenticated.size()).isEqualTo(0);
602 assertThat(mKeyguardUpdateMonitor.mUserFaceAuthenticated.size()).isEqualTo(0);
603 }
604
605 @Test
Lucas Dupin3d053532019-01-29 12:35:22 -0800606 public void testGetUserCanSkipBouncer_whenTrust() {
607 int user = KeyguardUpdateMonitor.getCurrentUser();
608 mKeyguardUpdateMonitor.onTrustChanged(true /* enabled */, user, 0 /* flags */);
609 assertThat(mKeyguardUpdateMonitor.getUserCanSkipBouncer(user)).isTrue();
Bill Linef81cbd2018-07-05 17:48:49 +0800610 }
611
Malcolm Chen5c63b512019-08-13 13:24:07 -0700612 @Test
613 public void testGetSubscriptionInfo_whenInGroupedSubWithOpportunistic() {
614 List<SubscriptionInfo> list = new ArrayList<>();
615 list.add(TEST_SUBSCRIPTION);
616 list.add(TEST_SUBSCRIPTION_2);
Sooraj Sasindran40468062020-01-06 19:10:50 -0800617 when(mSubscriptionManager.getCompleteActiveSubscriptionInfoList()).thenReturn(list);
Malcolm Chen5c63b512019-08-13 13:24:07 -0700618 mKeyguardUpdateMonitor.mPhoneStateListener.onActiveDataSubscriptionIdChanged(
619 TEST_SUBSCRIPTION_2.getSubscriptionId());
620 mTestableLooper.processAllMessages();
621
622 List<SubscriptionInfo> listToVerify = mKeyguardUpdateMonitor
623 .getFilteredSubscriptionInfo(false);
624 assertThat(listToVerify.size()).isEqualTo(1);
625 assertThat(listToVerify.get(0)).isEqualTo(TEST_SUBSCRIPTION_2);
626 }
627
Lucas Dupin6c6d5732019-08-27 17:36:05 -0700628 @Test
629 public void testIsUserUnlocked() {
630 // mUserManager will report the user as unlocked on @Before
631 assertThat(mKeyguardUpdateMonitor.isUserUnlocked(KeyguardUpdateMonitor.getCurrentUser()))
632 .isTrue();
633 // Invalid user should not be unlocked.
634 int randomUser = 99;
635 assertThat(mKeyguardUpdateMonitor.isUserUnlocked(randomUser)).isFalse();
636 }
637
Lucas Dupinf2c53502019-10-03 13:56:18 -0700638 @Test
639 public void testTrustUsuallyManaged_whenTrustChanges() {
640 int user = KeyguardUpdateMonitor.getCurrentUser();
641 when(mTrustManager.isTrustUsuallyManaged(eq(user))).thenReturn(true);
642 mKeyguardUpdateMonitor.onTrustManagedChanged(false /* managed */, user);
643 assertThat(mKeyguardUpdateMonitor.isTrustUsuallyManaged(user)).isTrue();
644 }
645
646 @Test
647 public void testTrustUsuallyManaged_resetWhenUserIsRemoved() {
648 int user = KeyguardUpdateMonitor.getCurrentUser();
649 when(mTrustManager.isTrustUsuallyManaged(eq(user))).thenReturn(true);
650 mKeyguardUpdateMonitor.onTrustManagedChanged(false /* managed */, user);
651 assertThat(mKeyguardUpdateMonitor.isTrustUsuallyManaged(user)).isTrue();
652
653 mKeyguardUpdateMonitor.handleUserRemoved(user);
654 assertThat(mKeyguardUpdateMonitor.isTrustUsuallyManaged(user)).isFalse();
655 }
656
Yvonne Jiangb7024a22019-12-05 16:57:08 -0800657 @Test
658 public void testSecondaryLockscreenRequirement() {
659 int user = KeyguardUpdateMonitor.getCurrentUser();
660 String packageName = "fake.test.package";
661 String cls = "FakeService";
662 ServiceInfo serviceInfo = new ServiceInfo();
663 serviceInfo.packageName = packageName;
664 serviceInfo.name = cls;
665 ResolveInfo resolveInfo = new ResolveInfo();
666 resolveInfo.serviceInfo = serviceInfo;
667 when(mPackageManager.resolveService(any(Intent.class), eq(0))).thenReturn(resolveInfo);
Yvonne Jiang8345da32020-03-19 15:01:16 -0700668 when(mDevicePolicyManager.isSecondaryLockscreenEnabled(eq(UserHandle.of(user))))
669 .thenReturn(true, false);
Yvonne Jiang70a62372020-03-31 16:51:17 -0700670 when(mDevicePolicyManager.getProfileOwnerOrDeviceOwnerSupervisionComponent(
671 UserHandle.of(user)))
Yvonne Jiang8345da32020-03-19 15:01:16 -0700672 .thenReturn(new ComponentName(packageName, cls));
Yvonne Jiangb7024a22019-12-05 16:57:08 -0800673
674 // Initially null.
675 assertThat(mKeyguardUpdateMonitor.getSecondaryLockscreenRequirement(user)).isNull();
676
677 // Set non-null after DPM change.
678 setBroadcastReceiverPendingResult(mKeyguardUpdateMonitor.mBroadcastAllReceiver);
679 Intent intent = new Intent(DevicePolicyManager.ACTION_DEVICE_POLICY_MANAGER_STATE_CHANGED);
680 mKeyguardUpdateMonitor.mBroadcastAllReceiver.onReceive(getContext(), intent);
681 mTestableLooper.processAllMessages();
682
683 Intent storedIntent = mKeyguardUpdateMonitor.getSecondaryLockscreenRequirement(user);
684 assertThat(storedIntent.getComponent().getClassName()).isEqualTo(cls);
685 assertThat(storedIntent.getComponent().getPackageName()).isEqualTo(packageName);
686
687 // Back to null after another DPM change.
688 mKeyguardUpdateMonitor.mBroadcastAllReceiver.onReceive(getContext(), intent);
689 mTestableLooper.processAllMessages();
690 assertThat(mKeyguardUpdateMonitor.getSecondaryLockscreenRequirement(user)).isNull();
691 }
692
Fabian Kozynskib6a20372020-04-01 09:36:43 -0400693 @Test
694 public void testRingerModeChange() {
695 ArgumentCaptor<Observer<Integer>> captor = ArgumentCaptor.forClass(Observer.class);
696 verify(mRingerModeLiveData).observeForever(captor.capture());
697 Observer<Integer> observer = captor.getValue();
698
699 KeyguardUpdateMonitorCallback callback = mock(KeyguardUpdateMonitorCallback.class);
700
701 mKeyguardUpdateMonitor.registerCallback(callback);
702
703 observer.onChanged(AudioManager.RINGER_MODE_NORMAL);
704 observer.onChanged(AudioManager.RINGER_MODE_SILENT);
705 observer.onChanged(AudioManager.RINGER_MODE_VIBRATE);
706
707 mTestableLooper.processAllMessages();
708
709 InOrder orderVerify = inOrder(callback);
710 orderVerify.verify(callback).onRingerModeChanged(anyInt()); // Initial update on register
711 orderVerify.verify(callback).onRingerModeChanged(AudioManager.RINGER_MODE_NORMAL);
712 orderVerify.verify(callback).onRingerModeChanged(AudioManager.RINGER_MODE_SILENT);
713 orderVerify.verify(callback).onRingerModeChanged(AudioManager.RINGER_MODE_VIBRATE);
714 }
715
Yvonne Jiangb7024a22019-12-05 16:57:08 -0800716 private void setBroadcastReceiverPendingResult(BroadcastReceiver receiver) {
717 BroadcastReceiver.PendingResult pendingResult =
718 new BroadcastReceiver.PendingResult(Activity.RESULT_OK,
719 "resultData",
720 /* resultExtras= */ null,
721 BroadcastReceiver.PendingResult.TYPE_UNREGISTERED,
722 /* ordered= */ true,
723 /* sticky= */ false,
724 /* token= */ null,
725 UserHandle.myUserId(),
726 /* flags= */ 0);
727 receiver.setPendingResult(pendingResult);
728 }
729
Bill Linef81cbd2018-07-05 17:48:49 +0800730 private Intent putPhoneInfo(Intent intent, Bundle data, Boolean simInited) {
731 int subscription = simInited
732 ? 1/* mock subid=1 */ : SubscriptionManager.DUMMY_SUBSCRIPTION_ID_BASE;
733 if (data != null) intent.putExtras(data);
Daniel Bright63514be2020-01-15 12:10:53 -0800734
735 intent.putExtra(SubscriptionManager.EXTRA_SUBSCRIPTION_INDEX, subscription);
736 intent.putExtra(SubscriptionManager.EXTRA_SLOT_INDEX, 0);
Bill Linef81cbd2018-07-05 17:48:49 +0800737 return intent;
738 }
739
Lucas Dupin7ff82b02018-05-16 12:14:10 -0700740 private class TestableKeyguardUpdateMonitor extends KeyguardUpdateMonitor {
741 AtomicBoolean mSimStateChanged = new AtomicBoolean(false);
742
743 protected TestableKeyguardUpdateMonitor(Context context) {
Fabian Kozynski5ca7a512019-10-16 19:56:11 +0000744 super(context,
745 TestableLooper.get(KeyguardUpdateMonitorTest.this).getLooper(),
Fabian Kozynskib6a20372020-04-01 09:36:43 -0400746 mBroadcastDispatcher, mDumpManager,
Beverlyf882ee22020-05-11 16:12:09 -0400747 mRingerModeTracker, mBackgroundExecutor,
748 mStatusBarStateController, mLockPatternUtils);
749 setStrongAuthTracker(KeyguardUpdateMonitorTest.this.mStrongAuthTracker);
Lucas Dupin7ff82b02018-05-16 12:14:10 -0700750 }
751
752 public boolean hasSimStateJustChanged() {
753 return mSimStateChanged.getAndSet(false);
754 }
755
756 @Override
Jayachandran Cf5436a62019-11-08 18:22:45 -0800757 protected void handleSimStateChange(int subId, int slotId, int state) {
Lucas Dupin7ff82b02018-05-16 12:14:10 -0700758 mSimStateChanged.set(true);
759 super.handleSimStateChange(subId, slotId, state);
760 }
Lucas Dupin5e0f0d22018-02-26 13:32:16 -0800761 }
762}