Lucas Dupin | 5e0f0d2 | 2018-02-26 13:32:16 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.keyguard; |
| 18 | |
Malcolm Chen | 5c63b51 | 2019-08-13 13:24:07 -0700 | [diff] [blame^] | 19 | import static android.telephony.SubscriptionManager.DATA_ROAMING_DISABLE; |
| 20 | import static android.telephony.SubscriptionManager.NAME_SOURCE_DEFAULT_SOURCE; |
| 21 | |
Bill Lin | ef81cbd | 2018-07-05 17:48:49 +0800 | [diff] [blame] | 22 | import static com.google.common.truth.Truth.assertThat; |
| 23 | |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 24 | import static org.mockito.ArgumentMatchers.any; |
Malcolm Chen | 5c63b51 | 2019-08-13 13:24:07 -0700 | [diff] [blame^] | 25 | import static org.mockito.ArgumentMatchers.anyBoolean; |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 26 | import static org.mockito.ArgumentMatchers.anyInt; |
| 27 | import static org.mockito.ArgumentMatchers.anyString; |
Lucas Dupin | 8eec268 | 2019-07-01 16:41:17 -0700 | [diff] [blame] | 28 | import static org.mockito.Mockito.clearInvocations; |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 29 | import static org.mockito.Mockito.doAnswer; |
| 30 | import static org.mockito.Mockito.never; |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 31 | import static org.mockito.Mockito.spy; |
| 32 | import static org.mockito.Mockito.verify; |
| 33 | import static org.mockito.Mockito.when; |
Bill Lin | ef81cbd | 2018-07-05 17:48:49 +0800 | [diff] [blame] | 34 | |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 35 | import android.app.admin.DevicePolicyManager; |
| 36 | import android.app.trust.TrustManager; |
Lucas Dupin | 7ff82b0 | 2018-05-16 12:14:10 -0700 | [diff] [blame] | 37 | import android.content.Context; |
Lucas Dupin | 5e0f0d2 | 2018-02-26 13:32:16 -0800 | [diff] [blame] | 38 | import android.content.Intent; |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 39 | import android.content.pm.PackageManager; |
| 40 | import android.hardware.biometrics.BiometricManager; |
| 41 | import android.hardware.biometrics.BiometricSourceType; |
| 42 | import android.hardware.biometrics.IBiometricEnabledOnKeyguardCallback; |
| 43 | import android.hardware.face.FaceManager; |
| 44 | import android.hardware.fingerprint.FingerprintManager; |
Bill Lin | ef81cbd | 2018-07-05 17:48:49 +0800 | [diff] [blame] | 45 | import android.os.Bundle; |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 46 | import android.os.UserManager; |
Bill Lin | ef81cbd | 2018-07-05 17:48:49 +0800 | [diff] [blame] | 47 | import android.telephony.ServiceState; |
Malcolm Chen | 5c63b51 | 2019-08-13 13:24:07 -0700 | [diff] [blame^] | 48 | import android.telephony.SubscriptionInfo; |
Bill Lin | ef81cbd | 2018-07-05 17:48:49 +0800 | [diff] [blame] | 49 | import android.telephony.SubscriptionManager; |
Lucas Dupin | 5e0f0d2 | 2018-02-26 13:32:16 -0800 | [diff] [blame] | 50 | import android.test.suitebuilder.annotation.SmallTest; |
| 51 | import android.testing.AndroidTestingRunner; |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 52 | import android.testing.TestableContext; |
Lucas Dupin | 5e0f0d2 | 2018-02-26 13:32:16 -0800 | [diff] [blame] | 53 | import android.testing.TestableLooper; |
| 54 | import android.testing.TestableLooper.RunWithLooper; |
| 55 | |
| 56 | import com.android.internal.telephony.IccCardConstants; |
Bill Lin | ef81cbd | 2018-07-05 17:48:49 +0800 | [diff] [blame] | 57 | import com.android.internal.telephony.PhoneConstants; |
Lucas Dupin | 5e0f0d2 | 2018-02-26 13:32:16 -0800 | [diff] [blame] | 58 | import com.android.internal.telephony.TelephonyIntents; |
| 59 | import com.android.systemui.SysuiTestCase; |
Lucas Dupin | 4befb74 | 2019-07-01 11:31:31 -0700 | [diff] [blame] | 60 | import com.android.systemui.statusbar.phone.KeyguardBypassController; |
Lucas Dupin | 5e0f0d2 | 2018-02-26 13:32:16 -0800 | [diff] [blame] | 61 | |
| 62 | import org.junit.Assert; |
| 63 | import org.junit.Before; |
| 64 | import org.junit.Test; |
| 65 | import org.junit.runner.RunWith; |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 66 | import org.mockito.Mock; |
| 67 | import org.mockito.MockitoAnnotations; |
Lucas Dupin | 5e0f0d2 | 2018-02-26 13:32:16 -0800 | [diff] [blame] | 68 | |
Malcolm Chen | 5c63b51 | 2019-08-13 13:24:07 -0700 | [diff] [blame^] | 69 | import java.util.ArrayList; |
| 70 | import java.util.List; |
Lucas Dupin | 5e0f0d2 | 2018-02-26 13:32:16 -0800 | [diff] [blame] | 71 | import java.util.concurrent.atomic.AtomicBoolean; |
| 72 | |
| 73 | @SmallTest |
| 74 | @RunWith(AndroidTestingRunner.class) |
Lucas Dupin | 7ff82b0 | 2018-05-16 12:14:10 -0700 | [diff] [blame] | 75 | // We must run on the main looper because KeyguardUpdateMonitor#mHandler is initialized with |
| 76 | // new Handler(Looper.getMainLooper()). |
| 77 | // |
| 78 | // Using the main looper should be avoided whenever possible, please don't copy this over to |
| 79 | // new tests. |
| 80 | @RunWithLooper(setAsMainLooper = true) |
Lucas Dupin | 5e0f0d2 | 2018-02-26 13:32:16 -0800 | [diff] [blame] | 81 | public class KeyguardUpdateMonitorTest extends SysuiTestCase { |
Malcolm Chen | 5c63b51 | 2019-08-13 13:24:07 -0700 | [diff] [blame^] | 82 | private static final String TEST_CARRIER = "TEST_CARRIER"; |
| 83 | private static final String TEST_CARRIER_2 = "TEST_CARRIER_2"; |
| 84 | private static final int TEST_CARRIER_ID = 1; |
| 85 | private static final String TEST_GROUP_UUID = "59b5c870-fc4c-47a4-a99e-9db826b48b24"; |
| 86 | private static final SubscriptionInfo TEST_SUBSCRIPTION = new SubscriptionInfo(1, "", 0, |
| 87 | TEST_CARRIER, TEST_CARRIER, NAME_SOURCE_DEFAULT_SOURCE, 0xFFFFFF, "", |
| 88 | DATA_ROAMING_DISABLE, null, null, null, null, false, null, "", false, TEST_GROUP_UUID, |
| 89 | TEST_CARRIER_ID, 0); |
| 90 | private static final SubscriptionInfo TEST_SUBSCRIPTION_2 = new SubscriptionInfo(2, "", 0, |
| 91 | TEST_CARRIER, TEST_CARRIER_2, NAME_SOURCE_DEFAULT_SOURCE, 0xFFFFFF, "", |
| 92 | DATA_ROAMING_DISABLE, null, null, null, null, false, null, "", true, TEST_GROUP_UUID, |
| 93 | TEST_CARRIER_ID, 0); |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 94 | @Mock |
| 95 | private KeyguardUpdateMonitor.StrongAuthTracker mStrongAuthTracker; |
| 96 | @Mock |
| 97 | private TrustManager mTrustManager; |
| 98 | @Mock |
| 99 | private FingerprintManager mFingerprintManager; |
| 100 | @Mock |
| 101 | private FaceManager mFaceManager; |
| 102 | @Mock |
| 103 | private BiometricManager mBiometricManager; |
| 104 | @Mock |
| 105 | private PackageManager mPackageManager; |
| 106 | @Mock |
| 107 | private UserManager mUserManager; |
| 108 | @Mock |
| 109 | private DevicePolicyManager mDevicePolicyManager; |
Lucas Dupin | 4befb74 | 2019-07-01 11:31:31 -0700 | [diff] [blame] | 110 | @Mock |
| 111 | private KeyguardBypassController mKeyguardBypassController; |
Malcolm Chen | 5c63b51 | 2019-08-13 13:24:07 -0700 | [diff] [blame^] | 112 | @Mock |
| 113 | private SubscriptionManager mSubscriptionManager; |
Lucas Dupin | 5e0f0d2 | 2018-02-26 13:32:16 -0800 | [diff] [blame] | 114 | private TestableLooper mTestableLooper; |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 115 | private TestableKeyguardUpdateMonitor mKeyguardUpdateMonitor; |
Lucas Dupin | 5e0f0d2 | 2018-02-26 13:32:16 -0800 | [diff] [blame] | 116 | |
| 117 | @Before |
| 118 | public void setup() { |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 119 | MockitoAnnotations.initMocks(this); |
| 120 | TestableContext context = spy(mContext); |
| 121 | when(mPackageManager.hasSystemFeature(anyString())).thenReturn(true); |
| 122 | when(context.getPackageManager()).thenReturn(mPackageManager); |
| 123 | doAnswer(invocation -> { |
| 124 | IBiometricEnabledOnKeyguardCallback callback = invocation.getArgument(0); |
Lucas Dupin | 7d95f15 | 2019-07-17 16:25:54 -0700 | [diff] [blame] | 125 | callback.onChanged(BiometricSourceType.FACE, true /* enabled */, |
| 126 | KeyguardUpdateMonitor.getCurrentUser()); |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 127 | return null; |
| 128 | }).when(mBiometricManager).registerEnabledOnKeyguardCallback(any()); |
| 129 | when(mFaceManager.isHardwareDetected()).thenReturn(true); |
| 130 | when(mFaceManager.hasEnrolledTemplates()).thenReturn(true); |
| 131 | when(mFaceManager.hasEnrolledTemplates(anyInt())).thenReturn(true); |
| 132 | when(mUserManager.isUserUnlocked(anyInt())).thenReturn(true); |
Kevin Chyn | fdfd2d3 | 2019-03-01 14:52:15 -0800 | [diff] [blame] | 133 | when(mUserManager.isPrimaryUser()).thenReturn(true); |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 134 | when(mStrongAuthTracker.isUnlockingWithBiometricAllowed()).thenReturn(true); |
| 135 | context.addMockSystemService(TrustManager.class, mTrustManager); |
| 136 | context.addMockSystemService(FingerprintManager.class, mFingerprintManager); |
| 137 | context.addMockSystemService(BiometricManager.class, mBiometricManager); |
| 138 | context.addMockSystemService(FaceManager.class, mFaceManager); |
| 139 | context.addMockSystemService(UserManager.class, mUserManager); |
| 140 | context.addMockSystemService(DevicePolicyManager.class, mDevicePolicyManager); |
Malcolm Chen | 5c63b51 | 2019-08-13 13:24:07 -0700 | [diff] [blame^] | 141 | context.addMockSystemService(SubscriptionManager.class, mSubscriptionManager); |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 142 | |
Lucas Dupin | 5e0f0d2 | 2018-02-26 13:32:16 -0800 | [diff] [blame] | 143 | mTestableLooper = TestableLooper.get(this); |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 144 | mKeyguardUpdateMonitor = new TestableKeyguardUpdateMonitor(context); |
Lucas Dupin | 5e0f0d2 | 2018-02-26 13:32:16 -0800 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | @Test |
| 148 | public void testIgnoresSimStateCallback_rebroadcast() { |
| 149 | Intent intent = new Intent(TelephonyIntents.ACTION_SIM_STATE_CHANGED); |
| 150 | |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 151 | mKeyguardUpdateMonitor.mBroadcastReceiver.onReceive(getContext(), intent); |
Lucas Dupin | 5e0f0d2 | 2018-02-26 13:32:16 -0800 | [diff] [blame] | 152 | mTestableLooper.processAllMessages(); |
Lucas Dupin | 7ff82b0 | 2018-05-16 12:14:10 -0700 | [diff] [blame] | 153 | Assert.assertTrue("onSimStateChanged not called", |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 154 | mKeyguardUpdateMonitor.hasSimStateJustChanged()); |
Lucas Dupin | 5e0f0d2 | 2018-02-26 13:32:16 -0800 | [diff] [blame] | 155 | |
| 156 | intent.putExtra(TelephonyIntents.EXTRA_REBROADCAST_ON_UNLOCK, true); |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 157 | mKeyguardUpdateMonitor.mBroadcastReceiver.onReceive(getContext(), intent); |
Lucas Dupin | 5e0f0d2 | 2018-02-26 13:32:16 -0800 | [diff] [blame] | 158 | mTestableLooper.processAllMessages(); |
Lucas Dupin | 7ff82b0 | 2018-05-16 12:14:10 -0700 | [diff] [blame] | 159 | Assert.assertFalse("onSimStateChanged should have been skipped", |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 160 | mKeyguardUpdateMonitor.hasSimStateJustChanged()); |
Lucas Dupin | 7ff82b0 | 2018-05-16 12:14:10 -0700 | [diff] [blame] | 161 | } |
| 162 | |
Bill Lin | ef81cbd | 2018-07-05 17:48:49 +0800 | [diff] [blame] | 163 | @Test |
| 164 | public void testTelephonyCapable_BootInitState() { |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 165 | assertThat(mKeyguardUpdateMonitor.mTelephonyCapable).isFalse(); |
Bill Lin | ef81cbd | 2018-07-05 17:48:49 +0800 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | @Test |
| 169 | public void testTelephonyCapable_SimState_Absent() { |
| 170 | Intent intent = new Intent(TelephonyIntents.ACTION_SIM_STATE_CHANGED); |
andychou | 695a760 | 2019-05-16 23:14:00 +0800 | [diff] [blame] | 171 | intent.putExtra(IccCardConstants.INTENT_KEY_ICC_STATE, |
| 172 | IccCardConstants.INTENT_VALUE_ICC_ABSENT); |
| 173 | mKeyguardUpdateMonitor.mBroadcastReceiver.onReceive(getContext(), |
| 174 | putPhoneInfo(intent, null, false)); |
| 175 | mTestableLooper.processAllMessages(); |
| 176 | assertThat(mKeyguardUpdateMonitor.mTelephonyCapable).isTrue(); |
| 177 | } |
| 178 | |
| 179 | @Test |
| 180 | public void testTelephonyCapable_SimState_CardIOError() { |
| 181 | Intent intent = new Intent(TelephonyIntents.ACTION_SIM_STATE_CHANGED); |
| 182 | intent.putExtra(IccCardConstants.INTENT_KEY_ICC_STATE, |
| 183 | IccCardConstants.INTENT_VALUE_ICC_CARD_IO_ERROR); |
| 184 | mKeyguardUpdateMonitor.mBroadcastReceiver.onReceive(getContext(), |
| 185 | putPhoneInfo(intent, null, false)); |
Bill Lin | ef81cbd | 2018-07-05 17:48:49 +0800 | [diff] [blame] | 186 | mTestableLooper.processAllMessages(); |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 187 | assertThat(mKeyguardUpdateMonitor.mTelephonyCapable).isTrue(); |
Bill Lin | ef81cbd | 2018-07-05 17:48:49 +0800 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | @Test |
| 191 | public void testTelephonyCapable_SimInvalid_ServiceState_InService() { |
| 192 | // SERVICE_STATE - IN_SERVICE, but SIM_STATE is invalid TelephonyCapable should be False |
Bill Lin | ef81cbd | 2018-07-05 17:48:49 +0800 | [diff] [blame] | 193 | Intent intent = new Intent(TelephonyIntents.ACTION_SERVICE_STATE_CHANGED); |
| 194 | Bundle data = new Bundle(); |
| 195 | ServiceState state = new ServiceState(); |
| 196 | state.setState(ServiceState.STATE_IN_SERVICE); |
| 197 | state.fillInNotifierBundle(data); |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 198 | mKeyguardUpdateMonitor.mBroadcastReceiver.onReceive(getContext() |
Bill Lin | ef81cbd | 2018-07-05 17:48:49 +0800 | [diff] [blame] | 199 | , putPhoneInfo(intent, data, false)); |
| 200 | mTestableLooper.processAllMessages(); |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 201 | assertThat(mKeyguardUpdateMonitor.mTelephonyCapable).isFalse(); |
Bill Lin | ef81cbd | 2018-07-05 17:48:49 +0800 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | @Test |
| 205 | public void testTelephonyCapable_SimValid_ServiceState_PowerOff() { |
| 206 | // Simulate AirplaneMode case, SERVICE_STATE - POWER_OFF, check TelephonyCapable False |
| 207 | // Only receive ServiceState callback IN_SERVICE -> OUT_OF_SERVICE -> POWER_OFF |
Bill Lin | ef81cbd | 2018-07-05 17:48:49 +0800 | [diff] [blame] | 208 | Intent intent = new Intent(TelephonyIntents.ACTION_SERVICE_STATE_CHANGED); |
| 209 | intent.putExtra(IccCardConstants.INTENT_KEY_ICC_STATE |
| 210 | , IccCardConstants.INTENT_VALUE_ICC_LOADED); |
| 211 | Bundle data = new Bundle(); |
| 212 | ServiceState state = new ServiceState(); |
| 213 | state.setState(ServiceState.STATE_POWER_OFF); |
| 214 | state.fillInNotifierBundle(data); |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 215 | mKeyguardUpdateMonitor.mBroadcastReceiver.onReceive(getContext() |
Bill Lin | ef81cbd | 2018-07-05 17:48:49 +0800 | [diff] [blame] | 216 | , putPhoneInfo(intent, data, true)); |
| 217 | mTestableLooper.processAllMessages(); |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 218 | assertThat(mKeyguardUpdateMonitor.mTelephonyCapable).isTrue(); |
Bill Lin | ef81cbd | 2018-07-05 17:48:49 +0800 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | /* Normal SIM inserted flow |
| 222 | * ServiceState: ---OutOfServie----->PowerOff->OutOfServie--->InService |
| 223 | * SimState: ----NOT_READY---->READY----------------------LOADED>>> |
| 224 | * Subscription: --------null---->null--->"Chunghwa Telecom"-------->>> |
| 225 | * System: -------------------------------BOOT_COMPLETED------>>> |
| 226 | * TelephonyCapable:(F)-(F)-(F)-(F)-(F)-(F)-(F)-(F)-(F)-(F)------(T)-(T)>> |
| 227 | */ |
| 228 | @Test |
| 229 | public void testTelephonyCapable_BootInitState_ServiceState_OutOfService() { |
Bill Lin | ef81cbd | 2018-07-05 17:48:49 +0800 | [diff] [blame] | 230 | Intent intent = new Intent(TelephonyIntents.ACTION_SERVICE_STATE_CHANGED); |
| 231 | Bundle data = new Bundle(); |
| 232 | ServiceState state = new ServiceState(); |
| 233 | state.setState(ServiceState.STATE_OUT_OF_SERVICE); |
| 234 | state.fillInNotifierBundle(data); |
| 235 | intent.putExtras(data); |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 236 | mKeyguardUpdateMonitor.mBroadcastReceiver.onReceive(getContext() |
Bill Lin | ef81cbd | 2018-07-05 17:48:49 +0800 | [diff] [blame] | 237 | , putPhoneInfo(intent, data, false)); |
| 238 | mTestableLooper.processAllMessages(); |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 239 | assertThat(mKeyguardUpdateMonitor.mTelephonyCapable).isFalse(); |
Bill Lin | ef81cbd | 2018-07-05 17:48:49 +0800 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | @Test |
| 243 | public void testTelephonyCapable_BootInitState_SimState_NotReady() { |
Bill Lin | ef81cbd | 2018-07-05 17:48:49 +0800 | [diff] [blame] | 244 | Bundle data = new Bundle(); |
| 245 | ServiceState state = new ServiceState(); |
| 246 | state.setState(ServiceState.STATE_OUT_OF_SERVICE); |
| 247 | state.fillInNotifierBundle(data); |
| 248 | Intent intent = new Intent(TelephonyIntents.ACTION_SIM_STATE_CHANGED); |
| 249 | intent.putExtra(IccCardConstants.INTENT_KEY_ICC_STATE |
| 250 | , IccCardConstants.INTENT_VALUE_ICC_NOT_READY); |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 251 | mKeyguardUpdateMonitor.mBroadcastReceiver.onReceive(getContext() |
Bill Lin | ef81cbd | 2018-07-05 17:48:49 +0800 | [diff] [blame] | 252 | , putPhoneInfo(intent, data, false)); |
| 253 | mTestableLooper.processAllMessages(); |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 254 | assertThat(mKeyguardUpdateMonitor.mTelephonyCapable).isFalse(); |
Bill Lin | ef81cbd | 2018-07-05 17:48:49 +0800 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | @Test |
| 258 | public void testTelephonyCapable_BootInitState_SimState_Ready() { |
Bill Lin | ef81cbd | 2018-07-05 17:48:49 +0800 | [diff] [blame] | 259 | Bundle data = new Bundle(); |
| 260 | ServiceState state = new ServiceState(); |
| 261 | state.setState(ServiceState.STATE_OUT_OF_SERVICE); |
| 262 | state.fillInNotifierBundle(data); |
| 263 | Intent intent = new Intent(TelephonyIntents.ACTION_SIM_STATE_CHANGED); |
| 264 | intent.putExtra(IccCardConstants.INTENT_KEY_ICC_STATE |
| 265 | , IccCardConstants.INTENT_VALUE_ICC_READY); |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 266 | mKeyguardUpdateMonitor.mBroadcastReceiver.onReceive(getContext() |
Bill Lin | ef81cbd | 2018-07-05 17:48:49 +0800 | [diff] [blame] | 267 | , putPhoneInfo(intent, data, false)); |
| 268 | mTestableLooper.processAllMessages(); |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 269 | assertThat(mKeyguardUpdateMonitor.mTelephonyCapable).isFalse(); |
Bill Lin | ef81cbd | 2018-07-05 17:48:49 +0800 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | @Test |
| 273 | public void testTelephonyCapable_BootInitState_ServiceState_PowerOff() { |
Bill Lin | ef81cbd | 2018-07-05 17:48:49 +0800 | [diff] [blame] | 274 | Intent intent = new Intent(TelephonyIntents.ACTION_SERVICE_STATE_CHANGED); |
| 275 | Bundle data = new Bundle(); |
| 276 | ServiceState state = new ServiceState(); |
| 277 | state.setState(ServiceState.STATE_POWER_OFF); |
| 278 | state.fillInNotifierBundle(data); |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 279 | mKeyguardUpdateMonitor.mBroadcastReceiver.onReceive(getContext() |
Bill Lin | ef81cbd | 2018-07-05 17:48:49 +0800 | [diff] [blame] | 280 | , putPhoneInfo(intent, data, false)); |
| 281 | mTestableLooper.processAllMessages(); |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 282 | assertThat(mKeyguardUpdateMonitor.mTelephonyCapable).isFalse(); |
Bill Lin | ef81cbd | 2018-07-05 17:48:49 +0800 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | @Test |
| 286 | public void testTelephonyCapable_SimValid_ServiceState_InService() { |
Bill Lin | ef81cbd | 2018-07-05 17:48:49 +0800 | [diff] [blame] | 287 | Intent intent = new Intent(TelephonyIntents.ACTION_SERVICE_STATE_CHANGED); |
| 288 | Bundle data = new Bundle(); |
| 289 | ServiceState state = new ServiceState(); |
| 290 | state.setState(ServiceState.STATE_IN_SERVICE); |
| 291 | state.fillInNotifierBundle(data); |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 292 | mKeyguardUpdateMonitor.mBroadcastReceiver.onReceive(getContext() |
Bill Lin | ef81cbd | 2018-07-05 17:48:49 +0800 | [diff] [blame] | 293 | , putPhoneInfo(intent, data, true)); |
| 294 | mTestableLooper.processAllMessages(); |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 295 | assertThat(mKeyguardUpdateMonitor.mTelephonyCapable).isTrue(); |
Bill Lin | ef81cbd | 2018-07-05 17:48:49 +0800 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | @Test |
| 299 | public void testTelephonyCapable_SimValid_SimState_Loaded() { |
Bill Lin | ef81cbd | 2018-07-05 17:48:49 +0800 | [diff] [blame] | 300 | Bundle data = new Bundle(); |
| 301 | ServiceState state = new ServiceState(); |
| 302 | state.setState(ServiceState.STATE_IN_SERVICE); |
| 303 | state.fillInNotifierBundle(data); |
| 304 | Intent intentSimState = new Intent(TelephonyIntents.ACTION_SIM_STATE_CHANGED); |
| 305 | intentSimState.putExtra(IccCardConstants.INTENT_KEY_ICC_STATE |
| 306 | , IccCardConstants.INTENT_VALUE_ICC_LOADED); |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 307 | mKeyguardUpdateMonitor.mBroadcastReceiver.onReceive(getContext() |
Bill Lin | ef81cbd | 2018-07-05 17:48:49 +0800 | [diff] [blame] | 308 | , putPhoneInfo(intentSimState, data, true)); |
| 309 | mTestableLooper.processAllMessages(); |
| 310 | // Even SimState Loaded, still need ACTION_SERVICE_STATE_CHANGED turn on mTelephonyCapable |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 311 | assertThat(mKeyguardUpdateMonitor.mTelephonyCapable).isFalse(); |
Bill Lin | ef81cbd | 2018-07-05 17:48:49 +0800 | [diff] [blame] | 312 | |
| 313 | Intent intentServiceState = new Intent(TelephonyIntents.ACTION_SERVICE_STATE_CHANGED); |
| 314 | intentSimState.putExtra(IccCardConstants.INTENT_KEY_ICC_STATE |
| 315 | , IccCardConstants.INTENT_VALUE_ICC_LOADED); |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 316 | mKeyguardUpdateMonitor.mBroadcastReceiver.onReceive(getContext() |
Bill Lin | ef81cbd | 2018-07-05 17:48:49 +0800 | [diff] [blame] | 317 | , putPhoneInfo(intentServiceState, data, true)); |
| 318 | mTestableLooper.processAllMessages(); |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 319 | assertThat(mKeyguardUpdateMonitor.mTelephonyCapable).isTrue(); |
| 320 | } |
| 321 | |
| 322 | @Test |
| 323 | public void testTriesToAuthenticate_whenBouncer() { |
| 324 | mKeyguardUpdateMonitor.sendKeyguardBouncerChanged(true); |
| 325 | mTestableLooper.processAllMessages(); |
| 326 | |
Kevin Chyn | 8d2694a | 2019-04-11 18:30:40 -0700 | [diff] [blame] | 327 | verify(mFaceManager).authenticate(any(), any(), anyInt(), any(), any(), anyInt()); |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 328 | verify(mFaceManager).isHardwareDetected(); |
| 329 | verify(mFaceManager).hasEnrolledTemplates(anyInt()); |
| 330 | } |
| 331 | |
| 332 | @Test |
| 333 | public void testTriesToAuthenticate_whenKeyguard() { |
| 334 | mKeyguardUpdateMonitor.dispatchStartedWakingUp(); |
| 335 | mTestableLooper.processAllMessages(); |
| 336 | mKeyguardUpdateMonitor.onKeyguardVisibilityChanged(true); |
Kevin Chyn | 8d2694a | 2019-04-11 18:30:40 -0700 | [diff] [blame] | 337 | verify(mFaceManager).authenticate(any(), any(), anyInt(), any(), any(), anyInt()); |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | @Test |
| 341 | public void skipsAuthentication_whenEncryptedKeyguard() { |
Lucas Dupin | 8eec268 | 2019-07-01 16:41:17 -0700 | [diff] [blame] | 342 | when(mStrongAuthTracker.getStrongAuthForUser(anyInt())).thenReturn( |
| 343 | KeyguardUpdateMonitor.StrongAuthTracker.STRONG_AUTH_REQUIRED_AFTER_BOOT); |
| 344 | mKeyguardUpdateMonitor.setKeyguardBypassController(mKeyguardBypassController); |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 345 | |
| 346 | mKeyguardUpdateMonitor.dispatchStartedWakingUp(); |
| 347 | mTestableLooper.processAllMessages(); |
| 348 | mKeyguardUpdateMonitor.onKeyguardVisibilityChanged(true); |
Lucas Dupin | 8eec268 | 2019-07-01 16:41:17 -0700 | [diff] [blame] | 349 | verify(mFaceManager, never()).authenticate(any(), any(), anyInt(), any(), any(), anyInt()); |
| 350 | } |
| 351 | |
| 352 | @Test |
| 353 | public void requiresAuthentication_whenEncryptedKeyguard_andBypass() { |
| 354 | testStrongAuthExceptOnBouncer( |
| 355 | KeyguardUpdateMonitor.StrongAuthTracker.STRONG_AUTH_REQUIRED_AFTER_BOOT); |
| 356 | } |
| 357 | |
| 358 | @Test |
| 359 | public void requiresAuthentication_whenTimeoutKeyguard_andBypass() { |
| 360 | testStrongAuthExceptOnBouncer( |
| 361 | KeyguardUpdateMonitor.StrongAuthTracker.STRONG_AUTH_REQUIRED_AFTER_TIMEOUT); |
| 362 | } |
| 363 | |
| 364 | private void testStrongAuthExceptOnBouncer(int strongAuth) { |
| 365 | when(mKeyguardBypassController.canBypass()).thenReturn(true); |
| 366 | mKeyguardUpdateMonitor.setKeyguardBypassController(mKeyguardBypassController); |
| 367 | when(mStrongAuthTracker.getStrongAuthForUser(anyInt())).thenReturn(strongAuth); |
| 368 | |
| 369 | mKeyguardUpdateMonitor.dispatchStartedWakingUp(); |
| 370 | mTestableLooper.processAllMessages(); |
| 371 | mKeyguardUpdateMonitor.onKeyguardVisibilityChanged(true); |
| 372 | verify(mFaceManager).authenticate(any(), any(), anyInt(), any(), any(), anyInt()); |
| 373 | |
| 374 | // Stop scanning when bouncer becomes visible |
| 375 | mKeyguardUpdateMonitor.sendKeyguardBouncerChanged(true /* showingBouncer */); |
| 376 | mTestableLooper.processAllMessages(); |
| 377 | clearInvocations(mFaceManager); |
| 378 | mKeyguardUpdateMonitor.requestFaceAuth(); |
| 379 | verify(mFaceManager, never()).authenticate(any(), any(), anyInt(), any(), any(), anyInt()); |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 380 | } |
| 381 | |
| 382 | @Test |
| 383 | public void testTriesToAuthenticate_whenAssistant() { |
| 384 | mKeyguardUpdateMonitor.setKeyguardOccluded(true); |
| 385 | mKeyguardUpdateMonitor.setAssistantVisible(true); |
| 386 | |
Kevin Chyn | 8d2694a | 2019-04-11 18:30:40 -0700 | [diff] [blame] | 387 | verify(mFaceManager).authenticate(any(), any(), anyInt(), any(), any(), anyInt()); |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 388 | } |
| 389 | |
| 390 | @Test |
Lucas Dupin | 4befb74 | 2019-07-01 11:31:31 -0700 | [diff] [blame] | 391 | public void testTriesToAuthenticate_whenTrustOnAgentKeyguard_ifBypass() { |
| 392 | mKeyguardUpdateMonitor.setKeyguardBypassController(mKeyguardBypassController); |
| 393 | mKeyguardUpdateMonitor.dispatchStartedWakingUp(); |
| 394 | mTestableLooper.processAllMessages(); |
| 395 | when(mKeyguardBypassController.canBypass()).thenReturn(true); |
| 396 | mKeyguardUpdateMonitor.onTrustChanged(true /* enabled */, |
| 397 | KeyguardUpdateMonitor.getCurrentUser(), 0 /* flags */); |
| 398 | mKeyguardUpdateMonitor.onKeyguardVisibilityChanged(true); |
| 399 | verify(mFaceManager).authenticate(any(), any(), anyInt(), any(), any(), anyInt()); |
| 400 | } |
| 401 | |
| 402 | @Test |
| 403 | public void testIgnoresAuth_whenTrustAgentOnKeyguard_withoutBypass() { |
| 404 | mKeyguardUpdateMonitor.dispatchStartedWakingUp(); |
| 405 | mTestableLooper.processAllMessages(); |
| 406 | mKeyguardUpdateMonitor.onTrustChanged(true /* enabled */, |
| 407 | KeyguardUpdateMonitor.getCurrentUser(), 0 /* flags */); |
| 408 | mKeyguardUpdateMonitor.onKeyguardVisibilityChanged(true); |
| 409 | verify(mFaceManager, never()).authenticate(any(), any(), anyInt(), any(), any(), anyInt()); |
| 410 | } |
| 411 | |
| 412 | @Test |
Lucas Dupin | 8eec268 | 2019-07-01 16:41:17 -0700 | [diff] [blame] | 413 | public void testIgnoresAuth_whenLockdown() { |
| 414 | mKeyguardUpdateMonitor.dispatchStartedWakingUp(); |
| 415 | mTestableLooper.processAllMessages(); |
| 416 | when(mStrongAuthTracker.getStrongAuthForUser(anyInt())).thenReturn( |
| 417 | KeyguardUpdateMonitor.StrongAuthTracker.STRONG_AUTH_REQUIRED_AFTER_USER_LOCKDOWN); |
| 418 | |
| 419 | mKeyguardUpdateMonitor.onKeyguardVisibilityChanged(true); |
| 420 | verify(mFaceManager, never()).authenticate(any(), any(), anyInt(), any(), any(), anyInt()); |
| 421 | } |
| 422 | |
| 423 | @Test |
| 424 | public void testIgnoresAuth_whenLockout() { |
| 425 | mKeyguardUpdateMonitor.dispatchStartedWakingUp(); |
| 426 | mTestableLooper.processAllMessages(); |
| 427 | when(mStrongAuthTracker.getStrongAuthForUser(anyInt())).thenReturn( |
| 428 | KeyguardUpdateMonitor.StrongAuthTracker.STRONG_AUTH_REQUIRED_AFTER_LOCKOUT); |
| 429 | |
| 430 | mKeyguardUpdateMonitor.onKeyguardVisibilityChanged(true); |
| 431 | verify(mFaceManager, never()).authenticate(any(), any(), anyInt(), any(), any(), anyInt()); |
| 432 | } |
| 433 | |
| 434 | @Test |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 435 | public void testOnFaceAuthenticated_skipsFaceWhenAuthenticated() { |
| 436 | mKeyguardUpdateMonitor.onFaceAuthenticated(KeyguardUpdateMonitor.getCurrentUser()); |
| 437 | mKeyguardUpdateMonitor.sendKeyguardBouncerChanged(true); |
| 438 | mTestableLooper.processAllMessages(); |
| 439 | |
| 440 | verify(mFaceManager, never()).authenticate(any(), any(), anyInt(), any(), any()); |
| 441 | } |
| 442 | |
| 443 | @Test |
| 444 | public void testGetUserCanSkipBouncer_whenFace() { |
| 445 | int user = KeyguardUpdateMonitor.getCurrentUser(); |
| 446 | mKeyguardUpdateMonitor.onFaceAuthenticated(user); |
| 447 | assertThat(mKeyguardUpdateMonitor.getUserCanSkipBouncer(user)).isTrue(); |
| 448 | } |
| 449 | |
| 450 | @Test |
| 451 | public void testGetUserCanSkipBouncer_whenFingerprint() { |
| 452 | int user = KeyguardUpdateMonitor.getCurrentUser(); |
| 453 | mKeyguardUpdateMonitor.onFingerprintAuthenticated(user); |
| 454 | assertThat(mKeyguardUpdateMonitor.getUserCanSkipBouncer(user)).isTrue(); |
| 455 | } |
| 456 | |
| 457 | @Test |
| 458 | public void testGetUserCanSkipBouncer_whenTrust() { |
| 459 | int user = KeyguardUpdateMonitor.getCurrentUser(); |
| 460 | mKeyguardUpdateMonitor.onTrustChanged(true /* enabled */, user, 0 /* flags */); |
| 461 | assertThat(mKeyguardUpdateMonitor.getUserCanSkipBouncer(user)).isTrue(); |
Bill Lin | ef81cbd | 2018-07-05 17:48:49 +0800 | [diff] [blame] | 462 | } |
| 463 | |
Malcolm Chen | 5c63b51 | 2019-08-13 13:24:07 -0700 | [diff] [blame^] | 464 | @Test |
| 465 | public void testGetSubscriptionInfo_whenInGroupedSubWithOpportunistic() { |
| 466 | List<SubscriptionInfo> list = new ArrayList<>(); |
| 467 | list.add(TEST_SUBSCRIPTION); |
| 468 | list.add(TEST_SUBSCRIPTION_2); |
| 469 | when(mSubscriptionManager.getActiveSubscriptionInfoList(anyBoolean())).thenReturn(list); |
| 470 | mKeyguardUpdateMonitor.mPhoneStateListener.onActiveDataSubscriptionIdChanged( |
| 471 | TEST_SUBSCRIPTION_2.getSubscriptionId()); |
| 472 | mTestableLooper.processAllMessages(); |
| 473 | |
| 474 | List<SubscriptionInfo> listToVerify = mKeyguardUpdateMonitor |
| 475 | .getFilteredSubscriptionInfo(false); |
| 476 | assertThat(listToVerify.size()).isEqualTo(1); |
| 477 | assertThat(listToVerify.get(0)).isEqualTo(TEST_SUBSCRIPTION_2); |
| 478 | } |
| 479 | |
Bill Lin | ef81cbd | 2018-07-05 17:48:49 +0800 | [diff] [blame] | 480 | private Intent putPhoneInfo(Intent intent, Bundle data, Boolean simInited) { |
| 481 | int subscription = simInited |
| 482 | ? 1/* mock subid=1 */ : SubscriptionManager.DUMMY_SUBSCRIPTION_ID_BASE; |
| 483 | if (data != null) intent.putExtras(data); |
| 484 | intent.putExtra(PhoneConstants.PHONE_NAME_KEY, "Phone"); |
| 485 | intent.putExtra("subscription", subscription); |
| 486 | intent.putExtra("slot", 0/* SLOT 1 */); |
| 487 | return intent; |
| 488 | } |
| 489 | |
Lucas Dupin | 7ff82b0 | 2018-05-16 12:14:10 -0700 | [diff] [blame] | 490 | private class TestableKeyguardUpdateMonitor extends KeyguardUpdateMonitor { |
| 491 | AtomicBoolean mSimStateChanged = new AtomicBoolean(false); |
| 492 | |
| 493 | protected TestableKeyguardUpdateMonitor(Context context) { |
| 494 | super(context); |
Lucas Dupin | 7ff82b0 | 2018-05-16 12:14:10 -0700 | [diff] [blame] | 495 | context.unregisterReceiver(mBroadcastReceiver); |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 496 | context.unregisterReceiver(mBroadcastAllReceiver); |
| 497 | mStrongAuthTracker = KeyguardUpdateMonitorTest.this.mStrongAuthTracker; |
Lucas Dupin | 7ff82b0 | 2018-05-16 12:14:10 -0700 | [diff] [blame] | 498 | } |
| 499 | |
| 500 | public boolean hasSimStateJustChanged() { |
| 501 | return mSimStateChanged.getAndSet(false); |
| 502 | } |
| 503 | |
| 504 | @Override |
| 505 | protected void handleSimStateChange(int subId, int slotId, |
| 506 | IccCardConstants.State state) { |
| 507 | mSimStateChanged.set(true); |
| 508 | super.handleSimStateChange(subId, slotId, state); |
| 509 | } |
Lucas Dupin | 5e0f0d2 | 2018-02-26 13:32:16 -0800 | [diff] [blame] | 510 | } |
| 511 | } |