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 | |
Bill Lin | ef81cbd | 2018-07-05 17:48:49 +0800 | [diff] [blame] | 19 | import static com.google.common.truth.Truth.assertThat; |
| 20 | |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 21 | import static org.mockito.ArgumentMatchers.any; |
| 22 | import static org.mockito.ArgumentMatchers.anyInt; |
| 23 | import static org.mockito.ArgumentMatchers.anyString; |
| 24 | import static org.mockito.Mockito.doAnswer; |
| 25 | import static org.mockito.Mockito.never; |
| 26 | import static org.mockito.Mockito.reset; |
| 27 | import static org.mockito.Mockito.spy; |
| 28 | import static org.mockito.Mockito.verify; |
| 29 | import static org.mockito.Mockito.when; |
Bill Lin | ef81cbd | 2018-07-05 17:48:49 +0800 | [diff] [blame] | 30 | |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 31 | import android.app.admin.DevicePolicyManager; |
| 32 | import android.app.trust.TrustManager; |
Lucas Dupin | 7ff82b0 | 2018-05-16 12:14:10 -0700 | [diff] [blame] | 33 | import android.content.Context; |
Lucas Dupin | 5e0f0d2 | 2018-02-26 13:32:16 -0800 | [diff] [blame] | 34 | import android.content.Intent; |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 35 | import android.content.pm.PackageManager; |
| 36 | import android.hardware.biometrics.BiometricManager; |
| 37 | import android.hardware.biometrics.BiometricSourceType; |
| 38 | import android.hardware.biometrics.IBiometricEnabledOnKeyguardCallback; |
| 39 | import android.hardware.face.FaceManager; |
| 40 | import android.hardware.fingerprint.FingerprintManager; |
Bill Lin | ef81cbd | 2018-07-05 17:48:49 +0800 | [diff] [blame] | 41 | import android.os.Bundle; |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 42 | import android.os.UserManager; |
Bill Lin | ef81cbd | 2018-07-05 17:48:49 +0800 | [diff] [blame] | 43 | import android.telephony.ServiceState; |
| 44 | import android.telephony.SubscriptionManager; |
Lucas Dupin | 5e0f0d2 | 2018-02-26 13:32:16 -0800 | [diff] [blame] | 45 | import android.test.suitebuilder.annotation.SmallTest; |
| 46 | import android.testing.AndroidTestingRunner; |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 47 | import android.testing.TestableContext; |
Lucas Dupin | 5e0f0d2 | 2018-02-26 13:32:16 -0800 | [diff] [blame] | 48 | import android.testing.TestableLooper; |
| 49 | import android.testing.TestableLooper.RunWithLooper; |
| 50 | |
| 51 | import com.android.internal.telephony.IccCardConstants; |
Bill Lin | ef81cbd | 2018-07-05 17:48:49 +0800 | [diff] [blame] | 52 | import com.android.internal.telephony.PhoneConstants; |
Lucas Dupin | 5e0f0d2 | 2018-02-26 13:32:16 -0800 | [diff] [blame] | 53 | import com.android.internal.telephony.TelephonyIntents; |
| 54 | import com.android.systemui.SysuiTestCase; |
| 55 | |
| 56 | import org.junit.Assert; |
| 57 | import org.junit.Before; |
| 58 | import org.junit.Test; |
| 59 | import org.junit.runner.RunWith; |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 60 | import org.mockito.Mock; |
| 61 | import org.mockito.MockitoAnnotations; |
Lucas Dupin | 5e0f0d2 | 2018-02-26 13:32:16 -0800 | [diff] [blame] | 62 | |
| 63 | import java.util.concurrent.atomic.AtomicBoolean; |
| 64 | |
| 65 | @SmallTest |
| 66 | @RunWith(AndroidTestingRunner.class) |
Lucas Dupin | 7ff82b0 | 2018-05-16 12:14:10 -0700 | [diff] [blame] | 67 | // We must run on the main looper because KeyguardUpdateMonitor#mHandler is initialized with |
| 68 | // new Handler(Looper.getMainLooper()). |
| 69 | // |
| 70 | // Using the main looper should be avoided whenever possible, please don't copy this over to |
| 71 | // new tests. |
| 72 | @RunWithLooper(setAsMainLooper = true) |
Lucas Dupin | 5e0f0d2 | 2018-02-26 13:32:16 -0800 | [diff] [blame] | 73 | public class KeyguardUpdateMonitorTest extends SysuiTestCase { |
| 74 | |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 75 | @Mock |
| 76 | private KeyguardUpdateMonitor.StrongAuthTracker mStrongAuthTracker; |
| 77 | @Mock |
| 78 | private TrustManager mTrustManager; |
| 79 | @Mock |
| 80 | private FingerprintManager mFingerprintManager; |
| 81 | @Mock |
| 82 | private FaceManager mFaceManager; |
| 83 | @Mock |
| 84 | private BiometricManager mBiometricManager; |
| 85 | @Mock |
| 86 | private PackageManager mPackageManager; |
| 87 | @Mock |
| 88 | private UserManager mUserManager; |
| 89 | @Mock |
| 90 | private DevicePolicyManager mDevicePolicyManager; |
Lucas Dupin | 5e0f0d2 | 2018-02-26 13:32:16 -0800 | [diff] [blame] | 91 | private TestableLooper mTestableLooper; |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 92 | private TestableKeyguardUpdateMonitor mKeyguardUpdateMonitor; |
Lucas Dupin | 5e0f0d2 | 2018-02-26 13:32:16 -0800 | [diff] [blame] | 93 | |
| 94 | @Before |
| 95 | public void setup() { |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 96 | MockitoAnnotations.initMocks(this); |
| 97 | TestableContext context = spy(mContext); |
| 98 | when(mPackageManager.hasSystemFeature(anyString())).thenReturn(true); |
| 99 | when(context.getPackageManager()).thenReturn(mPackageManager); |
| 100 | doAnswer(invocation -> { |
| 101 | IBiometricEnabledOnKeyguardCallback callback = invocation.getArgument(0); |
| 102 | callback.onChanged(BiometricSourceType.FACE, true /* enabled */); |
| 103 | return null; |
| 104 | }).when(mBiometricManager).registerEnabledOnKeyguardCallback(any()); |
| 105 | when(mFaceManager.isHardwareDetected()).thenReturn(true); |
| 106 | when(mFaceManager.hasEnrolledTemplates()).thenReturn(true); |
| 107 | when(mFaceManager.hasEnrolledTemplates(anyInt())).thenReturn(true); |
| 108 | when(mUserManager.isUserUnlocked(anyInt())).thenReturn(true); |
Kevin Chyn | fdfd2d3 | 2019-03-01 14:52:15 -0800 | [diff] [blame] | 109 | when(mUserManager.isPrimaryUser()).thenReturn(true); |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 110 | when(mStrongAuthTracker.isUnlockingWithBiometricAllowed()).thenReturn(true); |
| 111 | context.addMockSystemService(TrustManager.class, mTrustManager); |
| 112 | context.addMockSystemService(FingerprintManager.class, mFingerprintManager); |
| 113 | context.addMockSystemService(BiometricManager.class, mBiometricManager); |
| 114 | context.addMockSystemService(FaceManager.class, mFaceManager); |
| 115 | context.addMockSystemService(UserManager.class, mUserManager); |
| 116 | context.addMockSystemService(DevicePolicyManager.class, mDevicePolicyManager); |
| 117 | |
Lucas Dupin | 5e0f0d2 | 2018-02-26 13:32:16 -0800 | [diff] [blame] | 118 | mTestableLooper = TestableLooper.get(this); |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 119 | mKeyguardUpdateMonitor = new TestableKeyguardUpdateMonitor(context); |
Lucas Dupin | 5e0f0d2 | 2018-02-26 13:32:16 -0800 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | @Test |
| 123 | public void testIgnoresSimStateCallback_rebroadcast() { |
| 124 | Intent intent = new Intent(TelephonyIntents.ACTION_SIM_STATE_CHANGED); |
| 125 | |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 126 | mKeyguardUpdateMonitor.mBroadcastReceiver.onReceive(getContext(), intent); |
Lucas Dupin | 5e0f0d2 | 2018-02-26 13:32:16 -0800 | [diff] [blame] | 127 | mTestableLooper.processAllMessages(); |
Lucas Dupin | 7ff82b0 | 2018-05-16 12:14:10 -0700 | [diff] [blame] | 128 | Assert.assertTrue("onSimStateChanged not called", |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 129 | mKeyguardUpdateMonitor.hasSimStateJustChanged()); |
Lucas Dupin | 5e0f0d2 | 2018-02-26 13:32:16 -0800 | [diff] [blame] | 130 | |
| 131 | intent.putExtra(TelephonyIntents.EXTRA_REBROADCAST_ON_UNLOCK, true); |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 132 | mKeyguardUpdateMonitor.mBroadcastReceiver.onReceive(getContext(), intent); |
Lucas Dupin | 5e0f0d2 | 2018-02-26 13:32:16 -0800 | [diff] [blame] | 133 | mTestableLooper.processAllMessages(); |
Lucas Dupin | 7ff82b0 | 2018-05-16 12:14:10 -0700 | [diff] [blame] | 134 | Assert.assertFalse("onSimStateChanged should have been skipped", |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 135 | mKeyguardUpdateMonitor.hasSimStateJustChanged()); |
Lucas Dupin | 7ff82b0 | 2018-05-16 12:14:10 -0700 | [diff] [blame] | 136 | } |
| 137 | |
Bill Lin | ef81cbd | 2018-07-05 17:48:49 +0800 | [diff] [blame] | 138 | @Test |
| 139 | public void testTelephonyCapable_BootInitState() { |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 140 | assertThat(mKeyguardUpdateMonitor.mTelephonyCapable).isFalse(); |
Bill Lin | ef81cbd | 2018-07-05 17:48:49 +0800 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | @Test |
| 144 | public void testTelephonyCapable_SimState_Absent() { |
| 145 | Intent intent = new Intent(TelephonyIntents.ACTION_SIM_STATE_CHANGED); |
| 146 | intent.putExtra(IccCardConstants.INTENT_KEY_ICC_STATE |
| 147 | , IccCardConstants.INTENT_VALUE_ICC_ABSENT); |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 148 | mKeyguardUpdateMonitor.mBroadcastReceiver.onReceive(getContext() |
Bill Lin | ef81cbd | 2018-07-05 17:48:49 +0800 | [diff] [blame] | 149 | , putPhoneInfo(intent,null, false)); |
| 150 | mTestableLooper.processAllMessages(); |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 151 | assertThat(mKeyguardUpdateMonitor.mTelephonyCapable).isTrue(); |
Bill Lin | ef81cbd | 2018-07-05 17:48:49 +0800 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | @Test |
| 155 | public void testTelephonyCapable_SimInvalid_ServiceState_InService() { |
| 156 | // 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] | 157 | Intent intent = new Intent(TelephonyIntents.ACTION_SERVICE_STATE_CHANGED); |
| 158 | Bundle data = new Bundle(); |
| 159 | ServiceState state = new ServiceState(); |
| 160 | state.setState(ServiceState.STATE_IN_SERVICE); |
| 161 | state.fillInNotifierBundle(data); |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 162 | mKeyguardUpdateMonitor.mBroadcastReceiver.onReceive(getContext() |
Bill Lin | ef81cbd | 2018-07-05 17:48:49 +0800 | [diff] [blame] | 163 | , putPhoneInfo(intent, data, false)); |
| 164 | mTestableLooper.processAllMessages(); |
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_SimValid_ServiceState_PowerOff() { |
| 170 | // Simulate AirplaneMode case, SERVICE_STATE - POWER_OFF, check TelephonyCapable False |
| 171 | // Only receive ServiceState callback IN_SERVICE -> OUT_OF_SERVICE -> POWER_OFF |
Bill Lin | ef81cbd | 2018-07-05 17:48:49 +0800 | [diff] [blame] | 172 | Intent intent = new Intent(TelephonyIntents.ACTION_SERVICE_STATE_CHANGED); |
| 173 | intent.putExtra(IccCardConstants.INTENT_KEY_ICC_STATE |
| 174 | , IccCardConstants.INTENT_VALUE_ICC_LOADED); |
| 175 | Bundle data = new Bundle(); |
| 176 | ServiceState state = new ServiceState(); |
| 177 | state.setState(ServiceState.STATE_POWER_OFF); |
| 178 | state.fillInNotifierBundle(data); |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 179 | mKeyguardUpdateMonitor.mBroadcastReceiver.onReceive(getContext() |
Bill Lin | ef81cbd | 2018-07-05 17:48:49 +0800 | [diff] [blame] | 180 | , putPhoneInfo(intent, data, true)); |
| 181 | mTestableLooper.processAllMessages(); |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 182 | assertThat(mKeyguardUpdateMonitor.mTelephonyCapable).isTrue(); |
Bill Lin | ef81cbd | 2018-07-05 17:48:49 +0800 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | /* Normal SIM inserted flow |
| 186 | * ServiceState: ---OutOfServie----->PowerOff->OutOfServie--->InService |
| 187 | * SimState: ----NOT_READY---->READY----------------------LOADED>>> |
| 188 | * Subscription: --------null---->null--->"Chunghwa Telecom"-------->>> |
| 189 | * System: -------------------------------BOOT_COMPLETED------>>> |
| 190 | * TelephonyCapable:(F)-(F)-(F)-(F)-(F)-(F)-(F)-(F)-(F)-(F)------(T)-(T)>> |
| 191 | */ |
| 192 | @Test |
| 193 | public void testTelephonyCapable_BootInitState_ServiceState_OutOfService() { |
Bill Lin | ef81cbd | 2018-07-05 17:48:49 +0800 | [diff] [blame] | 194 | Intent intent = new Intent(TelephonyIntents.ACTION_SERVICE_STATE_CHANGED); |
| 195 | Bundle data = new Bundle(); |
| 196 | ServiceState state = new ServiceState(); |
| 197 | state.setState(ServiceState.STATE_OUT_OF_SERVICE); |
| 198 | state.fillInNotifierBundle(data); |
| 199 | intent.putExtras(data); |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 200 | mKeyguardUpdateMonitor.mBroadcastReceiver.onReceive(getContext() |
Bill Lin | ef81cbd | 2018-07-05 17:48:49 +0800 | [diff] [blame] | 201 | , putPhoneInfo(intent, data, false)); |
| 202 | mTestableLooper.processAllMessages(); |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 203 | assertThat(mKeyguardUpdateMonitor.mTelephonyCapable).isFalse(); |
Bill Lin | ef81cbd | 2018-07-05 17:48:49 +0800 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | @Test |
| 207 | public void testTelephonyCapable_BootInitState_SimState_NotReady() { |
Bill Lin | ef81cbd | 2018-07-05 17:48:49 +0800 | [diff] [blame] | 208 | Bundle data = new Bundle(); |
| 209 | ServiceState state = new ServiceState(); |
| 210 | state.setState(ServiceState.STATE_OUT_OF_SERVICE); |
| 211 | state.fillInNotifierBundle(data); |
| 212 | Intent intent = new Intent(TelephonyIntents.ACTION_SIM_STATE_CHANGED); |
| 213 | intent.putExtra(IccCardConstants.INTENT_KEY_ICC_STATE |
| 214 | , IccCardConstants.INTENT_VALUE_ICC_NOT_READY); |
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, false)); |
| 217 | mTestableLooper.processAllMessages(); |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 218 | assertThat(mKeyguardUpdateMonitor.mTelephonyCapable).isFalse(); |
Bill Lin | ef81cbd | 2018-07-05 17:48:49 +0800 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | @Test |
| 222 | public void testTelephonyCapable_BootInitState_SimState_Ready() { |
Bill Lin | ef81cbd | 2018-07-05 17:48:49 +0800 | [diff] [blame] | 223 | Bundle data = new Bundle(); |
| 224 | ServiceState state = new ServiceState(); |
| 225 | state.setState(ServiceState.STATE_OUT_OF_SERVICE); |
| 226 | state.fillInNotifierBundle(data); |
| 227 | Intent intent = new Intent(TelephonyIntents.ACTION_SIM_STATE_CHANGED); |
| 228 | intent.putExtra(IccCardConstants.INTENT_KEY_ICC_STATE |
| 229 | , IccCardConstants.INTENT_VALUE_ICC_READY); |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 230 | mKeyguardUpdateMonitor.mBroadcastReceiver.onReceive(getContext() |
Bill Lin | ef81cbd | 2018-07-05 17:48:49 +0800 | [diff] [blame] | 231 | , putPhoneInfo(intent, data, false)); |
| 232 | mTestableLooper.processAllMessages(); |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 233 | assertThat(mKeyguardUpdateMonitor.mTelephonyCapable).isFalse(); |
Bill Lin | ef81cbd | 2018-07-05 17:48:49 +0800 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | @Test |
| 237 | public void testTelephonyCapable_BootInitState_ServiceState_PowerOff() { |
Bill Lin | ef81cbd | 2018-07-05 17:48:49 +0800 | [diff] [blame] | 238 | Intent intent = new Intent(TelephonyIntents.ACTION_SERVICE_STATE_CHANGED); |
| 239 | Bundle data = new Bundle(); |
| 240 | ServiceState state = new ServiceState(); |
| 241 | state.setState(ServiceState.STATE_POWER_OFF); |
| 242 | state.fillInNotifierBundle(data); |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 243 | mKeyguardUpdateMonitor.mBroadcastReceiver.onReceive(getContext() |
Bill Lin | ef81cbd | 2018-07-05 17:48:49 +0800 | [diff] [blame] | 244 | , putPhoneInfo(intent, data, false)); |
| 245 | mTestableLooper.processAllMessages(); |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 246 | assertThat(mKeyguardUpdateMonitor.mTelephonyCapable).isFalse(); |
Bill Lin | ef81cbd | 2018-07-05 17:48:49 +0800 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | @Test |
| 250 | public void testTelephonyCapable_SimValid_ServiceState_InService() { |
Bill Lin | ef81cbd | 2018-07-05 17:48:49 +0800 | [diff] [blame] | 251 | Intent intent = new Intent(TelephonyIntents.ACTION_SERVICE_STATE_CHANGED); |
| 252 | Bundle data = new Bundle(); |
| 253 | ServiceState state = new ServiceState(); |
| 254 | state.setState(ServiceState.STATE_IN_SERVICE); |
| 255 | state.fillInNotifierBundle(data); |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 256 | mKeyguardUpdateMonitor.mBroadcastReceiver.onReceive(getContext() |
Bill Lin | ef81cbd | 2018-07-05 17:48:49 +0800 | [diff] [blame] | 257 | , putPhoneInfo(intent, data, true)); |
| 258 | mTestableLooper.processAllMessages(); |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 259 | assertThat(mKeyguardUpdateMonitor.mTelephonyCapable).isTrue(); |
Bill Lin | ef81cbd | 2018-07-05 17:48:49 +0800 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | @Test |
| 263 | public void testTelephonyCapable_SimValid_SimState_Loaded() { |
Bill Lin | ef81cbd | 2018-07-05 17:48:49 +0800 | [diff] [blame] | 264 | Bundle data = new Bundle(); |
| 265 | ServiceState state = new ServiceState(); |
| 266 | state.setState(ServiceState.STATE_IN_SERVICE); |
| 267 | state.fillInNotifierBundle(data); |
| 268 | Intent intentSimState = new Intent(TelephonyIntents.ACTION_SIM_STATE_CHANGED); |
| 269 | intentSimState.putExtra(IccCardConstants.INTENT_KEY_ICC_STATE |
| 270 | , IccCardConstants.INTENT_VALUE_ICC_LOADED); |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 271 | mKeyguardUpdateMonitor.mBroadcastReceiver.onReceive(getContext() |
Bill Lin | ef81cbd | 2018-07-05 17:48:49 +0800 | [diff] [blame] | 272 | , putPhoneInfo(intentSimState, data, true)); |
| 273 | mTestableLooper.processAllMessages(); |
| 274 | // Even SimState Loaded, still need ACTION_SERVICE_STATE_CHANGED turn on mTelephonyCapable |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 275 | assertThat(mKeyguardUpdateMonitor.mTelephonyCapable).isFalse(); |
Bill Lin | ef81cbd | 2018-07-05 17:48:49 +0800 | [diff] [blame] | 276 | |
| 277 | Intent intentServiceState = new Intent(TelephonyIntents.ACTION_SERVICE_STATE_CHANGED); |
| 278 | intentSimState.putExtra(IccCardConstants.INTENT_KEY_ICC_STATE |
| 279 | , IccCardConstants.INTENT_VALUE_ICC_LOADED); |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 280 | mKeyguardUpdateMonitor.mBroadcastReceiver.onReceive(getContext() |
Bill Lin | ef81cbd | 2018-07-05 17:48:49 +0800 | [diff] [blame] | 281 | , putPhoneInfo(intentServiceState, data, true)); |
| 282 | mTestableLooper.processAllMessages(); |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 283 | assertThat(mKeyguardUpdateMonitor.mTelephonyCapable).isTrue(); |
| 284 | } |
| 285 | |
| 286 | @Test |
| 287 | public void testTriesToAuthenticate_whenBouncer() { |
| 288 | mKeyguardUpdateMonitor.sendKeyguardBouncerChanged(true); |
| 289 | mTestableLooper.processAllMessages(); |
| 290 | |
| 291 | verify(mFaceManager).authenticate(any(), any(), anyInt(), any(), any()); |
| 292 | verify(mFaceManager).isHardwareDetected(); |
| 293 | verify(mFaceManager).hasEnrolledTemplates(anyInt()); |
| 294 | } |
| 295 | |
| 296 | @Test |
| 297 | public void testTriesToAuthenticate_whenKeyguard() { |
| 298 | mKeyguardUpdateMonitor.dispatchStartedWakingUp(); |
| 299 | mTestableLooper.processAllMessages(); |
| 300 | mKeyguardUpdateMonitor.onKeyguardVisibilityChanged(true); |
| 301 | verify(mFaceManager).authenticate(any(), any(), anyInt(), any(), any()); |
| 302 | } |
| 303 | |
| 304 | @Test |
| 305 | public void skipsAuthentication_whenEncryptedKeyguard() { |
| 306 | reset(mUserManager); |
| 307 | when(mUserManager.isUserUnlocked(anyInt())).thenReturn(false); |
| 308 | |
| 309 | mKeyguardUpdateMonitor.dispatchStartedWakingUp(); |
| 310 | mTestableLooper.processAllMessages(); |
| 311 | mKeyguardUpdateMonitor.onKeyguardVisibilityChanged(true); |
| 312 | verify(mFaceManager, never()).authenticate(any(), any(), anyInt(), any(), any()); |
| 313 | } |
| 314 | |
| 315 | @Test |
| 316 | public void testTriesToAuthenticate_whenAssistant() { |
| 317 | mKeyguardUpdateMonitor.setKeyguardOccluded(true); |
| 318 | mKeyguardUpdateMonitor.setAssistantVisible(true); |
| 319 | |
| 320 | verify(mFaceManager).authenticate(any(), any(), anyInt(), any(), any()); |
| 321 | } |
| 322 | |
| 323 | @Test |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 324 | public void testOnFaceAuthenticated_skipsFaceWhenAuthenticated() { |
| 325 | mKeyguardUpdateMonitor.onFaceAuthenticated(KeyguardUpdateMonitor.getCurrentUser()); |
| 326 | mKeyguardUpdateMonitor.sendKeyguardBouncerChanged(true); |
| 327 | mTestableLooper.processAllMessages(); |
| 328 | |
| 329 | verify(mFaceManager, never()).authenticate(any(), any(), anyInt(), any(), any()); |
| 330 | } |
| 331 | |
| 332 | @Test |
| 333 | public void testGetUserCanSkipBouncer_whenFace() { |
| 334 | int user = KeyguardUpdateMonitor.getCurrentUser(); |
| 335 | mKeyguardUpdateMonitor.onFaceAuthenticated(user); |
| 336 | assertThat(mKeyguardUpdateMonitor.getUserCanSkipBouncer(user)).isTrue(); |
| 337 | } |
| 338 | |
| 339 | @Test |
| 340 | public void testGetUserCanSkipBouncer_whenFingerprint() { |
| 341 | int user = KeyguardUpdateMonitor.getCurrentUser(); |
| 342 | mKeyguardUpdateMonitor.onFingerprintAuthenticated(user); |
| 343 | assertThat(mKeyguardUpdateMonitor.getUserCanSkipBouncer(user)).isTrue(); |
| 344 | } |
| 345 | |
| 346 | @Test |
| 347 | public void testGetUserCanSkipBouncer_whenTrust() { |
| 348 | int user = KeyguardUpdateMonitor.getCurrentUser(); |
| 349 | mKeyguardUpdateMonitor.onTrustChanged(true /* enabled */, user, 0 /* flags */); |
| 350 | assertThat(mKeyguardUpdateMonitor.getUserCanSkipBouncer(user)).isTrue(); |
Bill Lin | ef81cbd | 2018-07-05 17:48:49 +0800 | [diff] [blame] | 351 | } |
| 352 | |
| 353 | private Intent putPhoneInfo(Intent intent, Bundle data, Boolean simInited) { |
| 354 | int subscription = simInited |
| 355 | ? 1/* mock subid=1 */ : SubscriptionManager.DUMMY_SUBSCRIPTION_ID_BASE; |
| 356 | if (data != null) intent.putExtras(data); |
| 357 | intent.putExtra(PhoneConstants.PHONE_NAME_KEY, "Phone"); |
| 358 | intent.putExtra("subscription", subscription); |
| 359 | intent.putExtra("slot", 0/* SLOT 1 */); |
| 360 | return intent; |
| 361 | } |
| 362 | |
Lucas Dupin | 7ff82b0 | 2018-05-16 12:14:10 -0700 | [diff] [blame] | 363 | private class TestableKeyguardUpdateMonitor extends KeyguardUpdateMonitor { |
| 364 | AtomicBoolean mSimStateChanged = new AtomicBoolean(false); |
| 365 | |
| 366 | protected TestableKeyguardUpdateMonitor(Context context) { |
| 367 | super(context); |
Lucas Dupin | 7ff82b0 | 2018-05-16 12:14:10 -0700 | [diff] [blame] | 368 | context.unregisterReceiver(mBroadcastReceiver); |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 369 | context.unregisterReceiver(mBroadcastAllReceiver); |
| 370 | mStrongAuthTracker = KeyguardUpdateMonitorTest.this.mStrongAuthTracker; |
Lucas Dupin | 7ff82b0 | 2018-05-16 12:14:10 -0700 | [diff] [blame] | 371 | } |
| 372 | |
| 373 | public boolean hasSimStateJustChanged() { |
| 374 | return mSimStateChanged.getAndSet(false); |
| 375 | } |
| 376 | |
| 377 | @Override |
| 378 | protected void handleSimStateChange(int subId, int slotId, |
| 379 | IccCardConstants.State state) { |
| 380 | mSimStateChanged.set(true); |
| 381 | super.handleSimStateChange(subId, slotId, state); |
| 382 | } |
Lucas Dupin | 5e0f0d2 | 2018-02-26 13:32:16 -0800 | [diff] [blame] | 383 | } |
| 384 | } |