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; |
Peter Wang | 37916d6 | 2019-11-25 16:50:50 -0800 | [diff] [blame] | 20 | import static android.telephony.SubscriptionManager.NAME_SOURCE_DEFAULT; |
Malcolm Chen | 5c63b51 | 2019-08-13 13:24:07 -0700 | [diff] [blame] | 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; |
| 25 | import static org.mockito.ArgumentMatchers.anyInt; |
| 26 | import static org.mockito.ArgumentMatchers.anyString; |
Lucas Dupin | f2c5350 | 2019-10-03 13:56:18 -0700 | [diff] [blame] | 27 | import static org.mockito.ArgumentMatchers.eq; |
Fabian Kozynski | 5ca7a51 | 2019-10-16 19:56:11 +0000 | [diff] [blame] | 28 | import static org.mockito.Mockito.atLeastOnce; |
Lucas Dupin | 8eec268 | 2019-07-01 16:41:17 -0700 | [diff] [blame] | 29 | import static org.mockito.Mockito.clearInvocations; |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 30 | import static org.mockito.Mockito.doAnswer; |
| 31 | import static org.mockito.Mockito.never; |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 32 | import static org.mockito.Mockito.spy; |
| 33 | import static org.mockito.Mockito.verify; |
| 34 | import static org.mockito.Mockito.when; |
Bill Lin | ef81cbd | 2018-07-05 17:48:49 +0800 | [diff] [blame] | 35 | |
Yvonne Jiang | b7024a2 | 2019-12-05 16:57:08 -0800 | [diff] [blame] | 36 | import android.app.Activity; |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 37 | import android.app.admin.DevicePolicyManager; |
| 38 | import android.app.trust.TrustManager; |
Yvonne Jiang | b7024a2 | 2019-12-05 16:57:08 -0800 | [diff] [blame] | 39 | import android.content.BroadcastReceiver; |
Lucas Dupin | 7ff82b0 | 2018-05-16 12:14:10 -0700 | [diff] [blame] | 40 | import android.content.Context; |
Lucas Dupin | 5e0f0d2 | 2018-02-26 13:32:16 -0800 | [diff] [blame] | 41 | import android.content.Intent; |
Fabian Kozynski | 5ca7a51 | 2019-10-16 19:56:11 +0000 | [diff] [blame] | 42 | import android.content.IntentFilter; |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 43 | import android.content.pm.PackageManager; |
Yvonne Jiang | b7024a2 | 2019-12-05 16:57:08 -0800 | [diff] [blame] | 44 | import android.content.pm.ResolveInfo; |
| 45 | import android.content.pm.ServiceInfo; |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 46 | import android.hardware.biometrics.BiometricManager; |
| 47 | import android.hardware.biometrics.BiometricSourceType; |
| 48 | import android.hardware.biometrics.IBiometricEnabledOnKeyguardCallback; |
| 49 | import android.hardware.face.FaceManager; |
| 50 | import android.hardware.fingerprint.FingerprintManager; |
Bill Lin | ef81cbd | 2018-07-05 17:48:49 +0800 | [diff] [blame] | 51 | import android.os.Bundle; |
Fabian Kozynski | 5ca7a51 | 2019-10-16 19:56:11 +0000 | [diff] [blame] | 52 | import android.os.Handler; |
| 53 | import android.os.UserHandle; |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 54 | import android.os.UserManager; |
Bill Lin | ef81cbd | 2018-07-05 17:48:49 +0800 | [diff] [blame] | 55 | import android.telephony.ServiceState; |
Malcolm Chen | 5c63b51 | 2019-08-13 13:24:07 -0700 | [diff] [blame] | 56 | import android.telephony.SubscriptionInfo; |
Bill Lin | ef81cbd | 2018-07-05 17:48:49 +0800 | [diff] [blame] | 57 | import android.telephony.SubscriptionManager; |
Lucas Dupin | 5e0f0d2 | 2018-02-26 13:32:16 -0800 | [diff] [blame] | 58 | import android.test.suitebuilder.annotation.SmallTest; |
| 59 | import android.testing.AndroidTestingRunner; |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 60 | import android.testing.TestableContext; |
Lucas Dupin | 5e0f0d2 | 2018-02-26 13:32:16 -0800 | [diff] [blame] | 61 | import android.testing.TestableLooper; |
Lucas Dupin | 5e0f0d2 | 2018-02-26 13:32:16 -0800 | [diff] [blame] | 62 | |
Lucas Dupin | 5e0f0d2 | 2018-02-26 13:32:16 -0800 | [diff] [blame] | 63 | import com.android.internal.telephony.TelephonyIntents; |
Lucas Dupin | 64171fe | 2019-10-30 14:28:29 -0700 | [diff] [blame] | 64 | import com.android.systemui.DumpController; |
Lucas Dupin | 5e0f0d2 | 2018-02-26 13:32:16 -0800 | [diff] [blame] | 65 | import com.android.systemui.SysuiTestCase; |
Fabian Kozynski | 5ca7a51 | 2019-10-16 19:56:11 +0000 | [diff] [blame] | 66 | import com.android.systemui.broadcast.BroadcastDispatcher; |
Lucas Dupin | 4befb74 | 2019-07-01 11:31:31 -0700 | [diff] [blame] | 67 | import com.android.systemui.statusbar.phone.KeyguardBypassController; |
Lucas Dupin | 5e0f0d2 | 2018-02-26 13:32:16 -0800 | [diff] [blame] | 68 | |
| 69 | import org.junit.Assert; |
| 70 | import org.junit.Before; |
| 71 | import org.junit.Test; |
| 72 | import org.junit.runner.RunWith; |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 73 | import org.mockito.Mock; |
| 74 | import org.mockito.MockitoAnnotations; |
Lucas Dupin | 5e0f0d2 | 2018-02-26 13:32:16 -0800 | [diff] [blame] | 75 | |
Malcolm Chen | 5c63b51 | 2019-08-13 13:24:07 -0700 | [diff] [blame] | 76 | import java.util.ArrayList; |
| 77 | import java.util.List; |
Lucas Dupin | 5e0f0d2 | 2018-02-26 13:32:16 -0800 | [diff] [blame] | 78 | import java.util.concurrent.atomic.AtomicBoolean; |
| 79 | |
| 80 | @SmallTest |
| 81 | @RunWith(AndroidTestingRunner.class) |
Dave Mankoff | e229469 | 2019-08-14 11:53:13 -0400 | [diff] [blame] | 82 | @TestableLooper.RunWithLooper |
Lucas Dupin | 5e0f0d2 | 2018-02-26 13:32:16 -0800 | [diff] [blame] | 83 | public class KeyguardUpdateMonitorTest extends SysuiTestCase { |
Malcolm Chen | 5c63b51 | 2019-08-13 13:24:07 -0700 | [diff] [blame] | 84 | private static final String TEST_CARRIER = "TEST_CARRIER"; |
| 85 | private static final String TEST_CARRIER_2 = "TEST_CARRIER_2"; |
| 86 | private static final int TEST_CARRIER_ID = 1; |
| 87 | private static final String TEST_GROUP_UUID = "59b5c870-fc4c-47a4-a99e-9db826b48b24"; |
| 88 | private static final SubscriptionInfo TEST_SUBSCRIPTION = new SubscriptionInfo(1, "", 0, |
Peter Wang | 37916d6 | 2019-11-25 16:50:50 -0800 | [diff] [blame] | 89 | TEST_CARRIER, TEST_CARRIER, NAME_SOURCE_DEFAULT, 0xFFFFFF, "", |
Malcolm Chen | 5c63b51 | 2019-08-13 13:24:07 -0700 | [diff] [blame] | 90 | DATA_ROAMING_DISABLE, null, null, null, null, false, null, "", false, TEST_GROUP_UUID, |
| 91 | TEST_CARRIER_ID, 0); |
| 92 | private static final SubscriptionInfo TEST_SUBSCRIPTION_2 = new SubscriptionInfo(2, "", 0, |
Peter Wang | 37916d6 | 2019-11-25 16:50:50 -0800 | [diff] [blame] | 93 | TEST_CARRIER, TEST_CARRIER_2, NAME_SOURCE_DEFAULT, 0xFFFFFF, "", |
Malcolm Chen | 5c63b51 | 2019-08-13 13:24:07 -0700 | [diff] [blame] | 94 | DATA_ROAMING_DISABLE, null, null, null, null, false, null, "", true, TEST_GROUP_UUID, |
| 95 | TEST_CARRIER_ID, 0); |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 96 | @Mock |
Lucas Dupin | 64171fe | 2019-10-30 14:28:29 -0700 | [diff] [blame] | 97 | private DumpController mDumpController; |
| 98 | @Mock |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 99 | private KeyguardUpdateMonitor.StrongAuthTracker mStrongAuthTracker; |
| 100 | @Mock |
| 101 | private TrustManager mTrustManager; |
| 102 | @Mock |
| 103 | private FingerprintManager mFingerprintManager; |
| 104 | @Mock |
| 105 | private FaceManager mFaceManager; |
| 106 | @Mock |
| 107 | private BiometricManager mBiometricManager; |
| 108 | @Mock |
| 109 | private PackageManager mPackageManager; |
| 110 | @Mock |
| 111 | private UserManager mUserManager; |
| 112 | @Mock |
| 113 | private DevicePolicyManager mDevicePolicyManager; |
Lucas Dupin | 4befb74 | 2019-07-01 11:31:31 -0700 | [diff] [blame] | 114 | @Mock |
| 115 | private KeyguardBypassController mKeyguardBypassController; |
Malcolm Chen | 5c63b51 | 2019-08-13 13:24:07 -0700 | [diff] [blame] | 116 | @Mock |
| 117 | private SubscriptionManager mSubscriptionManager; |
Fabian Kozynski | 5ca7a51 | 2019-10-16 19:56:11 +0000 | [diff] [blame] | 118 | @Mock |
| 119 | private BroadcastDispatcher mBroadcastDispatcher; |
Lucas Dupin | 5e0f0d2 | 2018-02-26 13:32:16 -0800 | [diff] [blame] | 120 | private TestableLooper mTestableLooper; |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 121 | private TestableKeyguardUpdateMonitor mKeyguardUpdateMonitor; |
Lucas Dupin | 5e0f0d2 | 2018-02-26 13:32:16 -0800 | [diff] [blame] | 122 | |
| 123 | @Before |
| 124 | public void setup() { |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 125 | MockitoAnnotations.initMocks(this); |
| 126 | TestableContext context = spy(mContext); |
| 127 | when(mPackageManager.hasSystemFeature(anyString())).thenReturn(true); |
| 128 | when(context.getPackageManager()).thenReturn(mPackageManager); |
| 129 | doAnswer(invocation -> { |
| 130 | IBiometricEnabledOnKeyguardCallback callback = invocation.getArgument(0); |
Lucas Dupin | 7d95f15 | 2019-07-17 16:25:54 -0700 | [diff] [blame] | 131 | callback.onChanged(BiometricSourceType.FACE, true /* enabled */, |
| 132 | KeyguardUpdateMonitor.getCurrentUser()); |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 133 | return null; |
| 134 | }).when(mBiometricManager).registerEnabledOnKeyguardCallback(any()); |
| 135 | when(mFaceManager.isHardwareDetected()).thenReturn(true); |
| 136 | when(mFaceManager.hasEnrolledTemplates()).thenReturn(true); |
| 137 | when(mFaceManager.hasEnrolledTemplates(anyInt())).thenReturn(true); |
| 138 | when(mUserManager.isUserUnlocked(anyInt())).thenReturn(true); |
Kevin Chyn | fdfd2d3 | 2019-03-01 14:52:15 -0800 | [diff] [blame] | 139 | when(mUserManager.isPrimaryUser()).thenReturn(true); |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 140 | when(mStrongAuthTracker.isUnlockingWithBiometricAllowed()).thenReturn(true); |
| 141 | context.addMockSystemService(TrustManager.class, mTrustManager); |
| 142 | context.addMockSystemService(FingerprintManager.class, mFingerprintManager); |
| 143 | context.addMockSystemService(BiometricManager.class, mBiometricManager); |
| 144 | context.addMockSystemService(FaceManager.class, mFaceManager); |
| 145 | context.addMockSystemService(UserManager.class, mUserManager); |
| 146 | context.addMockSystemService(DevicePolicyManager.class, mDevicePolicyManager); |
Malcolm Chen | 5c63b51 | 2019-08-13 13:24:07 -0700 | [diff] [blame] | 147 | context.addMockSystemService(SubscriptionManager.class, mSubscriptionManager); |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 148 | |
Lucas Dupin | 5e0f0d2 | 2018-02-26 13:32:16 -0800 | [diff] [blame] | 149 | mTestableLooper = TestableLooper.get(this); |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 150 | mKeyguardUpdateMonitor = new TestableKeyguardUpdateMonitor(context); |
Lucas Dupin | 5e0f0d2 | 2018-02-26 13:32:16 -0800 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | @Test |
Fabian Kozynski | 5ca7a51 | 2019-10-16 19:56:11 +0000 | [diff] [blame] | 154 | public void testReceiversRegistered() { |
Fabian Kozynski | 5e92c6f | 2020-01-03 13:56:17 -0500 | [diff] [blame] | 155 | verify(mBroadcastDispatcher, atLeastOnce()).registerReceiverWithHandler( |
Fabian Kozynski | 5ca7a51 | 2019-10-16 19:56:11 +0000 | [diff] [blame] | 156 | eq(mKeyguardUpdateMonitor.mBroadcastReceiver), |
| 157 | any(IntentFilter.class), any(Handler.class)); |
Fabian Kozynski | 5e92c6f | 2020-01-03 13:56:17 -0500 | [diff] [blame] | 158 | verify(mBroadcastDispatcher, atLeastOnce()).registerReceiverWithHandler( |
Fabian Kozynski | 5ca7a51 | 2019-10-16 19:56:11 +0000 | [diff] [blame] | 159 | eq(mKeyguardUpdateMonitor.mBroadcastAllReceiver), |
| 160 | any(IntentFilter.class), any(Handler.class), eq(UserHandle.ALL)); |
| 161 | } |
| 162 | |
| 163 | @Test |
Lucas Dupin | 5e0f0d2 | 2018-02-26 13:32:16 -0800 | [diff] [blame] | 164 | public void testIgnoresSimStateCallback_rebroadcast() { |
| 165 | Intent intent = new Intent(TelephonyIntents.ACTION_SIM_STATE_CHANGED); |
| 166 | |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 167 | mKeyguardUpdateMonitor.mBroadcastReceiver.onReceive(getContext(), intent); |
Lucas Dupin | 5e0f0d2 | 2018-02-26 13:32:16 -0800 | [diff] [blame] | 168 | mTestableLooper.processAllMessages(); |
Lucas Dupin | 7ff82b0 | 2018-05-16 12:14:10 -0700 | [diff] [blame] | 169 | Assert.assertTrue("onSimStateChanged not called", |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 170 | mKeyguardUpdateMonitor.hasSimStateJustChanged()); |
Lucas Dupin | 5e0f0d2 | 2018-02-26 13:32:16 -0800 | [diff] [blame] | 171 | |
Meng Wang | 0e8d91a | 2020-01-17 14:58:01 -0800 | [diff] [blame] | 172 | intent.putExtra(Intent.EXTRA_REBROADCAST_ON_UNLOCK, true); |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 173 | mKeyguardUpdateMonitor.mBroadcastReceiver.onReceive(getContext(), intent); |
Lucas Dupin | 5e0f0d2 | 2018-02-26 13:32:16 -0800 | [diff] [blame] | 174 | mTestableLooper.processAllMessages(); |
Lucas Dupin | 7ff82b0 | 2018-05-16 12:14:10 -0700 | [diff] [blame] | 175 | Assert.assertFalse("onSimStateChanged should have been skipped", |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 176 | mKeyguardUpdateMonitor.hasSimStateJustChanged()); |
Lucas Dupin | 7ff82b0 | 2018-05-16 12:14:10 -0700 | [diff] [blame] | 177 | } |
| 178 | |
Bill Lin | ef81cbd | 2018-07-05 17:48:49 +0800 | [diff] [blame] | 179 | @Test |
| 180 | public void testTelephonyCapable_BootInitState() { |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 181 | assertThat(mKeyguardUpdateMonitor.mTelephonyCapable).isFalse(); |
Bill Lin | ef81cbd | 2018-07-05 17:48:49 +0800 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | @Test |
| 185 | public void testTelephonyCapable_SimState_Absent() { |
| 186 | Intent intent = new Intent(TelephonyIntents.ACTION_SIM_STATE_CHANGED); |
Jayachandran C | 0173df8 | 2019-12-19 12:11:03 -0800 | [diff] [blame] | 187 | intent.putExtra(Intent.EXTRA_SIM_STATE, |
| 188 | Intent.SIM_STATE_ABSENT); |
andychou | 695a760 | 2019-05-16 23:14:00 +0800 | [diff] [blame] | 189 | mKeyguardUpdateMonitor.mBroadcastReceiver.onReceive(getContext(), |
| 190 | putPhoneInfo(intent, null, false)); |
| 191 | mTestableLooper.processAllMessages(); |
| 192 | assertThat(mKeyguardUpdateMonitor.mTelephonyCapable).isTrue(); |
| 193 | } |
| 194 | |
| 195 | @Test |
| 196 | public void testTelephonyCapable_SimState_CardIOError() { |
| 197 | Intent intent = new Intent(TelephonyIntents.ACTION_SIM_STATE_CHANGED); |
Jayachandran C | 0173df8 | 2019-12-19 12:11:03 -0800 | [diff] [blame] | 198 | intent.putExtra(Intent.EXTRA_SIM_STATE, |
| 199 | Intent.SIM_STATE_CARD_IO_ERROR); |
andychou | 695a760 | 2019-05-16 23:14:00 +0800 | [diff] [blame] | 200 | mKeyguardUpdateMonitor.mBroadcastReceiver.onReceive(getContext(), |
| 201 | putPhoneInfo(intent, null, false)); |
Bill Lin | ef81cbd | 2018-07-05 17:48:49 +0800 | [diff] [blame] | 202 | mTestableLooper.processAllMessages(); |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 203 | assertThat(mKeyguardUpdateMonitor.mTelephonyCapable).isTrue(); |
Bill Lin | ef81cbd | 2018-07-05 17:48:49 +0800 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | @Test |
| 207 | public void testTelephonyCapable_SimInvalid_ServiceState_InService() { |
| 208 | // SERVICE_STATE - IN_SERVICE, but SIM_STATE is invalid TelephonyCapable should be False |
Jayachandran C | 041e769 | 2019-12-20 16:20:02 -0800 | [diff] [blame] | 209 | Intent intent = new Intent(Intent.ACTION_SERVICE_STATE); |
Bill Lin | ef81cbd | 2018-07-05 17:48:49 +0800 | [diff] [blame] | 210 | Bundle data = new Bundle(); |
| 211 | ServiceState state = new ServiceState(); |
| 212 | state.setState(ServiceState.STATE_IN_SERVICE); |
| 213 | state.fillInNotifierBundle(data); |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 214 | mKeyguardUpdateMonitor.mBroadcastReceiver.onReceive(getContext() |
Bill Lin | ef81cbd | 2018-07-05 17:48:49 +0800 | [diff] [blame] | 215 | , putPhoneInfo(intent, data, false)); |
| 216 | mTestableLooper.processAllMessages(); |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 217 | assertThat(mKeyguardUpdateMonitor.mTelephonyCapable).isFalse(); |
Bill Lin | ef81cbd | 2018-07-05 17:48:49 +0800 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | @Test |
| 221 | public void testTelephonyCapable_SimValid_ServiceState_PowerOff() { |
| 222 | // Simulate AirplaneMode case, SERVICE_STATE - POWER_OFF, check TelephonyCapable False |
| 223 | // Only receive ServiceState callback IN_SERVICE -> OUT_OF_SERVICE -> POWER_OFF |
Jayachandran C | 041e769 | 2019-12-20 16:20:02 -0800 | [diff] [blame] | 224 | Intent intent = new Intent(Intent.ACTION_SERVICE_STATE); |
Jayachandran C | 0173df8 | 2019-12-19 12:11:03 -0800 | [diff] [blame] | 225 | intent.putExtra(Intent.EXTRA_SIM_STATE |
| 226 | , Intent.SIM_STATE_LOADED); |
Bill Lin | ef81cbd | 2018-07-05 17:48:49 +0800 | [diff] [blame] | 227 | Bundle data = new Bundle(); |
| 228 | ServiceState state = new ServiceState(); |
| 229 | state.setState(ServiceState.STATE_POWER_OFF); |
| 230 | state.fillInNotifierBundle(data); |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 231 | mKeyguardUpdateMonitor.mBroadcastReceiver.onReceive(getContext() |
Bill Lin | ef81cbd | 2018-07-05 17:48:49 +0800 | [diff] [blame] | 232 | , putPhoneInfo(intent, data, true)); |
| 233 | mTestableLooper.processAllMessages(); |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 234 | assertThat(mKeyguardUpdateMonitor.mTelephonyCapable).isTrue(); |
Bill Lin | ef81cbd | 2018-07-05 17:48:49 +0800 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | /* Normal SIM inserted flow |
| 238 | * ServiceState: ---OutOfServie----->PowerOff->OutOfServie--->InService |
| 239 | * SimState: ----NOT_READY---->READY----------------------LOADED>>> |
| 240 | * Subscription: --------null---->null--->"Chunghwa Telecom"-------->>> |
| 241 | * System: -------------------------------BOOT_COMPLETED------>>> |
| 242 | * TelephonyCapable:(F)-(F)-(F)-(F)-(F)-(F)-(F)-(F)-(F)-(F)------(T)-(T)>> |
| 243 | */ |
| 244 | @Test |
| 245 | public void testTelephonyCapable_BootInitState_ServiceState_OutOfService() { |
Jayachandran C | 041e769 | 2019-12-20 16:20:02 -0800 | [diff] [blame] | 246 | Intent intent = new Intent(Intent.ACTION_SERVICE_STATE); |
Bill Lin | ef81cbd | 2018-07-05 17:48:49 +0800 | [diff] [blame] | 247 | Bundle data = new Bundle(); |
| 248 | ServiceState state = new ServiceState(); |
| 249 | state.setState(ServiceState.STATE_OUT_OF_SERVICE); |
| 250 | state.fillInNotifierBundle(data); |
| 251 | intent.putExtras(data); |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 252 | mKeyguardUpdateMonitor.mBroadcastReceiver.onReceive(getContext() |
Bill Lin | ef81cbd | 2018-07-05 17:48:49 +0800 | [diff] [blame] | 253 | , putPhoneInfo(intent, data, false)); |
| 254 | mTestableLooper.processAllMessages(); |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 255 | assertThat(mKeyguardUpdateMonitor.mTelephonyCapable).isFalse(); |
Bill Lin | ef81cbd | 2018-07-05 17:48:49 +0800 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | @Test |
| 259 | public void testTelephonyCapable_BootInitState_SimState_NotReady() { |
Bill Lin | ef81cbd | 2018-07-05 17:48:49 +0800 | [diff] [blame] | 260 | Bundle data = new Bundle(); |
| 261 | ServiceState state = new ServiceState(); |
| 262 | state.setState(ServiceState.STATE_OUT_OF_SERVICE); |
| 263 | state.fillInNotifierBundle(data); |
| 264 | Intent intent = new Intent(TelephonyIntents.ACTION_SIM_STATE_CHANGED); |
Jayachandran C | 0173df8 | 2019-12-19 12:11:03 -0800 | [diff] [blame] | 265 | intent.putExtra(Intent.EXTRA_SIM_STATE |
| 266 | , Intent.SIM_STATE_NOT_READY); |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 267 | mKeyguardUpdateMonitor.mBroadcastReceiver.onReceive(getContext() |
Bill Lin | ef81cbd | 2018-07-05 17:48:49 +0800 | [diff] [blame] | 268 | , putPhoneInfo(intent, data, false)); |
| 269 | mTestableLooper.processAllMessages(); |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 270 | assertThat(mKeyguardUpdateMonitor.mTelephonyCapable).isFalse(); |
Bill Lin | ef81cbd | 2018-07-05 17:48:49 +0800 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | @Test |
| 274 | public void testTelephonyCapable_BootInitState_SimState_Ready() { |
Bill Lin | ef81cbd | 2018-07-05 17:48:49 +0800 | [diff] [blame] | 275 | Bundle data = new Bundle(); |
| 276 | ServiceState state = new ServiceState(); |
| 277 | state.setState(ServiceState.STATE_OUT_OF_SERVICE); |
| 278 | state.fillInNotifierBundle(data); |
| 279 | Intent intent = new Intent(TelephonyIntents.ACTION_SIM_STATE_CHANGED); |
Jayachandran C | 0173df8 | 2019-12-19 12:11:03 -0800 | [diff] [blame] | 280 | intent.putExtra(Intent.EXTRA_SIM_STATE |
| 281 | , Intent.SIM_STATE_READY); |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 282 | mKeyguardUpdateMonitor.mBroadcastReceiver.onReceive(getContext() |
Bill Lin | ef81cbd | 2018-07-05 17:48:49 +0800 | [diff] [blame] | 283 | , putPhoneInfo(intent, data, false)); |
| 284 | mTestableLooper.processAllMessages(); |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 285 | assertThat(mKeyguardUpdateMonitor.mTelephonyCapable).isFalse(); |
Bill Lin | ef81cbd | 2018-07-05 17:48:49 +0800 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | @Test |
| 289 | public void testTelephonyCapable_BootInitState_ServiceState_PowerOff() { |
Jayachandran C | 041e769 | 2019-12-20 16:20:02 -0800 | [diff] [blame] | 290 | Intent intent = new Intent(Intent.ACTION_SERVICE_STATE); |
Bill Lin | ef81cbd | 2018-07-05 17:48:49 +0800 | [diff] [blame] | 291 | Bundle data = new Bundle(); |
| 292 | ServiceState state = new ServiceState(); |
| 293 | state.setState(ServiceState.STATE_POWER_OFF); |
| 294 | state.fillInNotifierBundle(data); |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 295 | mKeyguardUpdateMonitor.mBroadcastReceiver.onReceive(getContext() |
Bill Lin | ef81cbd | 2018-07-05 17:48:49 +0800 | [diff] [blame] | 296 | , putPhoneInfo(intent, data, false)); |
| 297 | mTestableLooper.processAllMessages(); |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 298 | assertThat(mKeyguardUpdateMonitor.mTelephonyCapable).isFalse(); |
Bill Lin | ef81cbd | 2018-07-05 17:48:49 +0800 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | @Test |
| 302 | public void testTelephonyCapable_SimValid_ServiceState_InService() { |
Jayachandran C | 041e769 | 2019-12-20 16:20:02 -0800 | [diff] [blame] | 303 | Intent intent = new Intent(Intent.ACTION_SERVICE_STATE); |
Bill Lin | ef81cbd | 2018-07-05 17:48:49 +0800 | [diff] [blame] | 304 | Bundle data = new Bundle(); |
| 305 | ServiceState state = new ServiceState(); |
| 306 | state.setState(ServiceState.STATE_IN_SERVICE); |
| 307 | state.fillInNotifierBundle(data); |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 308 | mKeyguardUpdateMonitor.mBroadcastReceiver.onReceive(getContext() |
Bill Lin | ef81cbd | 2018-07-05 17:48:49 +0800 | [diff] [blame] | 309 | , putPhoneInfo(intent, data, true)); |
| 310 | mTestableLooper.processAllMessages(); |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 311 | assertThat(mKeyguardUpdateMonitor.mTelephonyCapable).isTrue(); |
Bill Lin | ef81cbd | 2018-07-05 17:48:49 +0800 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | @Test |
| 315 | public void testTelephonyCapable_SimValid_SimState_Loaded() { |
Bill Lin | ef81cbd | 2018-07-05 17:48:49 +0800 | [diff] [blame] | 316 | Bundle data = new Bundle(); |
| 317 | ServiceState state = new ServiceState(); |
| 318 | state.setState(ServiceState.STATE_IN_SERVICE); |
| 319 | state.fillInNotifierBundle(data); |
| 320 | Intent intentSimState = new Intent(TelephonyIntents.ACTION_SIM_STATE_CHANGED); |
Jayachandran C | 0173df8 | 2019-12-19 12:11:03 -0800 | [diff] [blame] | 321 | intentSimState.putExtra(Intent.EXTRA_SIM_STATE |
| 322 | , Intent.SIM_STATE_LOADED); |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 323 | mKeyguardUpdateMonitor.mBroadcastReceiver.onReceive(getContext() |
Bill Lin | ef81cbd | 2018-07-05 17:48:49 +0800 | [diff] [blame] | 324 | , putPhoneInfo(intentSimState, data, true)); |
| 325 | mTestableLooper.processAllMessages(); |
Jayachandran C | 041e769 | 2019-12-20 16:20:02 -0800 | [diff] [blame] | 326 | // Even SimState Loaded, still need ACTION_SERVICE_STATE turn on mTelephonyCapable |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 327 | assertThat(mKeyguardUpdateMonitor.mTelephonyCapable).isFalse(); |
Bill Lin | ef81cbd | 2018-07-05 17:48:49 +0800 | [diff] [blame] | 328 | |
Jayachandran C | 041e769 | 2019-12-20 16:20:02 -0800 | [diff] [blame] | 329 | Intent intentServiceState = new Intent(Intent.ACTION_SERVICE_STATE); |
Jayachandran C | 0173df8 | 2019-12-19 12:11:03 -0800 | [diff] [blame] | 330 | intentSimState.putExtra(Intent.EXTRA_SIM_STATE |
| 331 | , Intent.SIM_STATE_LOADED); |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 332 | mKeyguardUpdateMonitor.mBroadcastReceiver.onReceive(getContext() |
Bill Lin | ef81cbd | 2018-07-05 17:48:49 +0800 | [diff] [blame] | 333 | , putPhoneInfo(intentServiceState, data, true)); |
| 334 | mTestableLooper.processAllMessages(); |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 335 | assertThat(mKeyguardUpdateMonitor.mTelephonyCapable).isTrue(); |
| 336 | } |
| 337 | |
| 338 | @Test |
| 339 | public void testTriesToAuthenticate_whenBouncer() { |
| 340 | mKeyguardUpdateMonitor.sendKeyguardBouncerChanged(true); |
| 341 | mTestableLooper.processAllMessages(); |
| 342 | |
Kevin Chyn | 8d2694a | 2019-04-11 18:30:40 -0700 | [diff] [blame] | 343 | verify(mFaceManager).authenticate(any(), any(), anyInt(), any(), any(), anyInt()); |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 344 | verify(mFaceManager).isHardwareDetected(); |
| 345 | verify(mFaceManager).hasEnrolledTemplates(anyInt()); |
| 346 | } |
| 347 | |
| 348 | @Test |
| 349 | public void testTriesToAuthenticate_whenKeyguard() { |
| 350 | mKeyguardUpdateMonitor.dispatchStartedWakingUp(); |
| 351 | mTestableLooper.processAllMessages(); |
| 352 | mKeyguardUpdateMonitor.onKeyguardVisibilityChanged(true); |
Kevin Chyn | 8d2694a | 2019-04-11 18:30:40 -0700 | [diff] [blame] | 353 | verify(mFaceManager).authenticate(any(), any(), anyInt(), any(), any(), anyInt()); |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 354 | } |
| 355 | |
| 356 | @Test |
| 357 | public void skipsAuthentication_whenEncryptedKeyguard() { |
Lucas Dupin | 8eec268 | 2019-07-01 16:41:17 -0700 | [diff] [blame] | 358 | when(mStrongAuthTracker.getStrongAuthForUser(anyInt())).thenReturn( |
| 359 | KeyguardUpdateMonitor.StrongAuthTracker.STRONG_AUTH_REQUIRED_AFTER_BOOT); |
| 360 | mKeyguardUpdateMonitor.setKeyguardBypassController(mKeyguardBypassController); |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 361 | |
| 362 | mKeyguardUpdateMonitor.dispatchStartedWakingUp(); |
| 363 | mTestableLooper.processAllMessages(); |
| 364 | mKeyguardUpdateMonitor.onKeyguardVisibilityChanged(true); |
Lucas Dupin | 8eec268 | 2019-07-01 16:41:17 -0700 | [diff] [blame] | 365 | verify(mFaceManager, never()).authenticate(any(), any(), anyInt(), any(), any(), anyInt()); |
| 366 | } |
| 367 | |
| 368 | @Test |
| 369 | public void requiresAuthentication_whenEncryptedKeyguard_andBypass() { |
| 370 | testStrongAuthExceptOnBouncer( |
| 371 | KeyguardUpdateMonitor.StrongAuthTracker.STRONG_AUTH_REQUIRED_AFTER_BOOT); |
| 372 | } |
| 373 | |
| 374 | @Test |
| 375 | public void requiresAuthentication_whenTimeoutKeyguard_andBypass() { |
| 376 | testStrongAuthExceptOnBouncer( |
| 377 | KeyguardUpdateMonitor.StrongAuthTracker.STRONG_AUTH_REQUIRED_AFTER_TIMEOUT); |
| 378 | } |
| 379 | |
| 380 | private void testStrongAuthExceptOnBouncer(int strongAuth) { |
| 381 | when(mKeyguardBypassController.canBypass()).thenReturn(true); |
| 382 | mKeyguardUpdateMonitor.setKeyguardBypassController(mKeyguardBypassController); |
| 383 | when(mStrongAuthTracker.getStrongAuthForUser(anyInt())).thenReturn(strongAuth); |
| 384 | |
| 385 | mKeyguardUpdateMonitor.dispatchStartedWakingUp(); |
| 386 | mTestableLooper.processAllMessages(); |
| 387 | mKeyguardUpdateMonitor.onKeyguardVisibilityChanged(true); |
| 388 | verify(mFaceManager).authenticate(any(), any(), anyInt(), any(), any(), anyInt()); |
| 389 | |
| 390 | // Stop scanning when bouncer becomes visible |
| 391 | mKeyguardUpdateMonitor.sendKeyguardBouncerChanged(true /* showingBouncer */); |
| 392 | mTestableLooper.processAllMessages(); |
| 393 | clearInvocations(mFaceManager); |
| 394 | mKeyguardUpdateMonitor.requestFaceAuth(); |
| 395 | verify(mFaceManager, never()).authenticate(any(), any(), anyInt(), any(), any(), anyInt()); |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 396 | } |
| 397 | |
| 398 | @Test |
| 399 | public void testTriesToAuthenticate_whenAssistant() { |
| 400 | mKeyguardUpdateMonitor.setKeyguardOccluded(true); |
| 401 | mKeyguardUpdateMonitor.setAssistantVisible(true); |
| 402 | |
Kevin Chyn | 8d2694a | 2019-04-11 18:30:40 -0700 | [diff] [blame] | 403 | verify(mFaceManager).authenticate(any(), any(), anyInt(), any(), any(), anyInt()); |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 404 | } |
| 405 | |
| 406 | @Test |
Lucas Dupin | 4befb74 | 2019-07-01 11:31:31 -0700 | [diff] [blame] | 407 | public void testTriesToAuthenticate_whenTrustOnAgentKeyguard_ifBypass() { |
| 408 | mKeyguardUpdateMonitor.setKeyguardBypassController(mKeyguardBypassController); |
| 409 | mKeyguardUpdateMonitor.dispatchStartedWakingUp(); |
| 410 | mTestableLooper.processAllMessages(); |
| 411 | when(mKeyguardBypassController.canBypass()).thenReturn(true); |
| 412 | mKeyguardUpdateMonitor.onTrustChanged(true /* enabled */, |
| 413 | KeyguardUpdateMonitor.getCurrentUser(), 0 /* flags */); |
| 414 | mKeyguardUpdateMonitor.onKeyguardVisibilityChanged(true); |
| 415 | verify(mFaceManager).authenticate(any(), any(), anyInt(), any(), any(), anyInt()); |
| 416 | } |
| 417 | |
| 418 | @Test |
| 419 | public void testIgnoresAuth_whenTrustAgentOnKeyguard_withoutBypass() { |
| 420 | mKeyguardUpdateMonitor.dispatchStartedWakingUp(); |
| 421 | mTestableLooper.processAllMessages(); |
| 422 | mKeyguardUpdateMonitor.onTrustChanged(true /* enabled */, |
| 423 | KeyguardUpdateMonitor.getCurrentUser(), 0 /* flags */); |
| 424 | mKeyguardUpdateMonitor.onKeyguardVisibilityChanged(true); |
| 425 | verify(mFaceManager, never()).authenticate(any(), any(), anyInt(), any(), any(), anyInt()); |
| 426 | } |
| 427 | |
| 428 | @Test |
Lucas Dupin | 8eec268 | 2019-07-01 16:41:17 -0700 | [diff] [blame] | 429 | public void testIgnoresAuth_whenLockdown() { |
| 430 | mKeyguardUpdateMonitor.dispatchStartedWakingUp(); |
| 431 | mTestableLooper.processAllMessages(); |
| 432 | when(mStrongAuthTracker.getStrongAuthForUser(anyInt())).thenReturn( |
| 433 | KeyguardUpdateMonitor.StrongAuthTracker.STRONG_AUTH_REQUIRED_AFTER_USER_LOCKDOWN); |
| 434 | |
| 435 | mKeyguardUpdateMonitor.onKeyguardVisibilityChanged(true); |
| 436 | verify(mFaceManager, never()).authenticate(any(), any(), anyInt(), any(), any(), anyInt()); |
| 437 | } |
| 438 | |
| 439 | @Test |
| 440 | public void testIgnoresAuth_whenLockout() { |
| 441 | mKeyguardUpdateMonitor.dispatchStartedWakingUp(); |
| 442 | mTestableLooper.processAllMessages(); |
| 443 | when(mStrongAuthTracker.getStrongAuthForUser(anyInt())).thenReturn( |
| 444 | KeyguardUpdateMonitor.StrongAuthTracker.STRONG_AUTH_REQUIRED_AFTER_LOCKOUT); |
| 445 | |
| 446 | mKeyguardUpdateMonitor.onKeyguardVisibilityChanged(true); |
| 447 | verify(mFaceManager, never()).authenticate(any(), any(), anyInt(), any(), any(), anyInt()); |
| 448 | } |
| 449 | |
| 450 | @Test |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 451 | public void testOnFaceAuthenticated_skipsFaceWhenAuthenticated() { |
| 452 | mKeyguardUpdateMonitor.onFaceAuthenticated(KeyguardUpdateMonitor.getCurrentUser()); |
| 453 | mKeyguardUpdateMonitor.sendKeyguardBouncerChanged(true); |
| 454 | mTestableLooper.processAllMessages(); |
| 455 | |
| 456 | verify(mFaceManager, never()).authenticate(any(), any(), anyInt(), any(), any()); |
| 457 | } |
| 458 | |
| 459 | @Test |
| 460 | public void testGetUserCanSkipBouncer_whenFace() { |
| 461 | int user = KeyguardUpdateMonitor.getCurrentUser(); |
| 462 | mKeyguardUpdateMonitor.onFaceAuthenticated(user); |
| 463 | assertThat(mKeyguardUpdateMonitor.getUserCanSkipBouncer(user)).isTrue(); |
| 464 | } |
| 465 | |
| 466 | @Test |
| 467 | public void testGetUserCanSkipBouncer_whenFingerprint() { |
| 468 | int user = KeyguardUpdateMonitor.getCurrentUser(); |
| 469 | mKeyguardUpdateMonitor.onFingerprintAuthenticated(user); |
| 470 | assertThat(mKeyguardUpdateMonitor.getUserCanSkipBouncer(user)).isTrue(); |
| 471 | } |
| 472 | |
| 473 | @Test |
| 474 | public void testGetUserCanSkipBouncer_whenTrust() { |
| 475 | int user = KeyguardUpdateMonitor.getCurrentUser(); |
| 476 | mKeyguardUpdateMonitor.onTrustChanged(true /* enabled */, user, 0 /* flags */); |
| 477 | assertThat(mKeyguardUpdateMonitor.getUserCanSkipBouncer(user)).isTrue(); |
Bill Lin | ef81cbd | 2018-07-05 17:48:49 +0800 | [diff] [blame] | 478 | } |
| 479 | |
Malcolm Chen | 5c63b51 | 2019-08-13 13:24:07 -0700 | [diff] [blame] | 480 | @Test |
| 481 | public void testGetSubscriptionInfo_whenInGroupedSubWithOpportunistic() { |
| 482 | List<SubscriptionInfo> list = new ArrayList<>(); |
| 483 | list.add(TEST_SUBSCRIPTION); |
| 484 | list.add(TEST_SUBSCRIPTION_2); |
Amit Mahajan | 75eab47 | 2020-01-29 10:47:48 -0800 | [diff] [blame] | 485 | when(mSubscriptionManager.getActiveAndHiddenSubscriptionInfoList()).thenReturn(list); |
Malcolm Chen | 5c63b51 | 2019-08-13 13:24:07 -0700 | [diff] [blame] | 486 | mKeyguardUpdateMonitor.mPhoneStateListener.onActiveDataSubscriptionIdChanged( |
| 487 | TEST_SUBSCRIPTION_2.getSubscriptionId()); |
| 488 | mTestableLooper.processAllMessages(); |
| 489 | |
| 490 | List<SubscriptionInfo> listToVerify = mKeyguardUpdateMonitor |
| 491 | .getFilteredSubscriptionInfo(false); |
| 492 | assertThat(listToVerify.size()).isEqualTo(1); |
| 493 | assertThat(listToVerify.get(0)).isEqualTo(TEST_SUBSCRIPTION_2); |
| 494 | } |
| 495 | |
Lucas Dupin | 6c6d573 | 2019-08-27 17:36:05 -0700 | [diff] [blame] | 496 | @Test |
| 497 | public void testIsUserUnlocked() { |
| 498 | // mUserManager will report the user as unlocked on @Before |
| 499 | assertThat(mKeyguardUpdateMonitor.isUserUnlocked(KeyguardUpdateMonitor.getCurrentUser())) |
| 500 | .isTrue(); |
| 501 | // Invalid user should not be unlocked. |
| 502 | int randomUser = 99; |
| 503 | assertThat(mKeyguardUpdateMonitor.isUserUnlocked(randomUser)).isFalse(); |
| 504 | } |
| 505 | |
Lucas Dupin | f2c5350 | 2019-10-03 13:56:18 -0700 | [diff] [blame] | 506 | @Test |
| 507 | public void testTrustUsuallyManaged_whenTrustChanges() { |
| 508 | int user = KeyguardUpdateMonitor.getCurrentUser(); |
| 509 | when(mTrustManager.isTrustUsuallyManaged(eq(user))).thenReturn(true); |
| 510 | mKeyguardUpdateMonitor.onTrustManagedChanged(false /* managed */, user); |
| 511 | assertThat(mKeyguardUpdateMonitor.isTrustUsuallyManaged(user)).isTrue(); |
| 512 | } |
| 513 | |
| 514 | @Test |
| 515 | public void testTrustUsuallyManaged_resetWhenUserIsRemoved() { |
| 516 | int user = KeyguardUpdateMonitor.getCurrentUser(); |
| 517 | when(mTrustManager.isTrustUsuallyManaged(eq(user))).thenReturn(true); |
| 518 | mKeyguardUpdateMonitor.onTrustManagedChanged(false /* managed */, user); |
| 519 | assertThat(mKeyguardUpdateMonitor.isTrustUsuallyManaged(user)).isTrue(); |
| 520 | |
| 521 | mKeyguardUpdateMonitor.handleUserRemoved(user); |
| 522 | assertThat(mKeyguardUpdateMonitor.isTrustUsuallyManaged(user)).isFalse(); |
| 523 | } |
| 524 | |
Yvonne Jiang | b7024a2 | 2019-12-05 16:57:08 -0800 | [diff] [blame] | 525 | @Test |
| 526 | public void testSecondaryLockscreenRequirement() { |
| 527 | int user = KeyguardUpdateMonitor.getCurrentUser(); |
| 528 | String packageName = "fake.test.package"; |
| 529 | String cls = "FakeService"; |
| 530 | ServiceInfo serviceInfo = new ServiceInfo(); |
| 531 | serviceInfo.packageName = packageName; |
| 532 | serviceInfo.name = cls; |
| 533 | ResolveInfo resolveInfo = new ResolveInfo(); |
| 534 | resolveInfo.serviceInfo = serviceInfo; |
| 535 | when(mPackageManager.resolveService(any(Intent.class), eq(0))).thenReturn(resolveInfo); |
| 536 | when(mDevicePolicyManager.isSecondaryLockscreenEnabled(eq(user))).thenReturn(true, false); |
| 537 | |
| 538 | // Initially null. |
| 539 | assertThat(mKeyguardUpdateMonitor.getSecondaryLockscreenRequirement(user)).isNull(); |
| 540 | |
| 541 | // Set non-null after DPM change. |
| 542 | setBroadcastReceiverPendingResult(mKeyguardUpdateMonitor.mBroadcastAllReceiver); |
| 543 | Intent intent = new Intent(DevicePolicyManager.ACTION_DEVICE_POLICY_MANAGER_STATE_CHANGED); |
| 544 | mKeyguardUpdateMonitor.mBroadcastAllReceiver.onReceive(getContext(), intent); |
| 545 | mTestableLooper.processAllMessages(); |
| 546 | |
| 547 | Intent storedIntent = mKeyguardUpdateMonitor.getSecondaryLockscreenRequirement(user); |
| 548 | assertThat(storedIntent.getComponent().getClassName()).isEqualTo(cls); |
| 549 | assertThat(storedIntent.getComponent().getPackageName()).isEqualTo(packageName); |
| 550 | |
| 551 | // Back to null after another DPM change. |
| 552 | mKeyguardUpdateMonitor.mBroadcastAllReceiver.onReceive(getContext(), intent); |
| 553 | mTestableLooper.processAllMessages(); |
| 554 | assertThat(mKeyguardUpdateMonitor.getSecondaryLockscreenRequirement(user)).isNull(); |
| 555 | } |
| 556 | |
| 557 | private void setBroadcastReceiverPendingResult(BroadcastReceiver receiver) { |
| 558 | BroadcastReceiver.PendingResult pendingResult = |
| 559 | new BroadcastReceiver.PendingResult(Activity.RESULT_OK, |
| 560 | "resultData", |
| 561 | /* resultExtras= */ null, |
| 562 | BroadcastReceiver.PendingResult.TYPE_UNREGISTERED, |
| 563 | /* ordered= */ true, |
| 564 | /* sticky= */ false, |
| 565 | /* token= */ null, |
| 566 | UserHandle.myUserId(), |
| 567 | /* flags= */ 0); |
| 568 | receiver.setPendingResult(pendingResult); |
| 569 | } |
| 570 | |
Bill Lin | ef81cbd | 2018-07-05 17:48:49 +0800 | [diff] [blame] | 571 | private Intent putPhoneInfo(Intent intent, Bundle data, Boolean simInited) { |
| 572 | int subscription = simInited |
| 573 | ? 1/* mock subid=1 */ : SubscriptionManager.DUMMY_SUBSCRIPTION_ID_BASE; |
| 574 | if (data != null) intent.putExtras(data); |
Daniel Bright | 63514be | 2020-01-15 12:10:53 -0800 | [diff] [blame] | 575 | |
| 576 | intent.putExtra(SubscriptionManager.EXTRA_SUBSCRIPTION_INDEX, subscription); |
| 577 | intent.putExtra(SubscriptionManager.EXTRA_SLOT_INDEX, 0); |
Bill Lin | ef81cbd | 2018-07-05 17:48:49 +0800 | [diff] [blame] | 578 | return intent; |
| 579 | } |
| 580 | |
Lucas Dupin | 7ff82b0 | 2018-05-16 12:14:10 -0700 | [diff] [blame] | 581 | private class TestableKeyguardUpdateMonitor extends KeyguardUpdateMonitor { |
| 582 | AtomicBoolean mSimStateChanged = new AtomicBoolean(false); |
| 583 | |
| 584 | protected TestableKeyguardUpdateMonitor(Context context) { |
Fabian Kozynski | 5ca7a51 | 2019-10-16 19:56:11 +0000 | [diff] [blame] | 585 | super(context, |
| 586 | TestableLooper.get(KeyguardUpdateMonitorTest.this).getLooper(), |
| 587 | mBroadcastDispatcher, mDumpController); |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 588 | mStrongAuthTracker = KeyguardUpdateMonitorTest.this.mStrongAuthTracker; |
Lucas Dupin | 7ff82b0 | 2018-05-16 12:14:10 -0700 | [diff] [blame] | 589 | } |
| 590 | |
| 591 | public boolean hasSimStateJustChanged() { |
| 592 | return mSimStateChanged.getAndSet(false); |
| 593 | } |
| 594 | |
| 595 | @Override |
Jayachandran C | f5436a6 | 2019-11-08 18:22:45 -0800 | [diff] [blame] | 596 | protected void handleSimStateChange(int subId, int slotId, int state) { |
Lucas Dupin | 7ff82b0 | 2018-05-16 12:14:10 -0700 | [diff] [blame] | 597 | mSimStateChanged.set(true); |
| 598 | super.handleSimStateChange(subId, slotId, state); |
| 599 | } |
Lucas Dupin | 5e0f0d2 | 2018-02-26 13:32:16 -0800 | [diff] [blame] | 600 | } |
| 601 | } |