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