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 | ff7f25f | 2020-01-31 18:46:26 -0800 | [diff] [blame] | 20 | import static android.telephony.SubscriptionManager.NAME_SOURCE_CARRIER_ID; |
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; |
Haining Chen | c06c481 | 2020-01-13 20:38:53 -0800 | [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; |
Fabian Kozynski | b6a2037 | 2020-04-01 09:36:43 -0400 | [diff] [blame] | 32 | import static org.mockito.Mockito.inOrder; |
| 33 | import static org.mockito.Mockito.mock; |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 34 | import static org.mockito.Mockito.never; |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 35 | import static org.mockito.Mockito.spy; |
| 36 | import static org.mockito.Mockito.verify; |
| 37 | import static org.mockito.Mockito.when; |
Bill Lin | ef81cbd | 2018-07-05 17:48:49 +0800 | [diff] [blame] | 38 | |
Yvonne Jiang | b7024a2 | 2019-12-05 16:57:08 -0800 | [diff] [blame] | 39 | import android.app.Activity; |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 40 | import android.app.admin.DevicePolicyManager; |
| 41 | import android.app.trust.TrustManager; |
Yvonne Jiang | b7024a2 | 2019-12-05 16:57:08 -0800 | [diff] [blame] | 42 | import android.content.BroadcastReceiver; |
Yvonne Jiang | 8345da3 | 2020-03-19 15:01:16 -0700 | [diff] [blame] | 43 | import android.content.ComponentName; |
Lucas Dupin | 7ff82b0 | 2018-05-16 12:14:10 -0700 | [diff] [blame] | 44 | import android.content.Context; |
Lucas Dupin | 5e0f0d2 | 2018-02-26 13:32:16 -0800 | [diff] [blame] | 45 | import android.content.Intent; |
Fabian Kozynski | 5ca7a51 | 2019-10-16 19:56:11 +0000 | [diff] [blame] | 46 | import android.content.IntentFilter; |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 47 | import android.content.pm.PackageManager; |
Yvonne Jiang | b7024a2 | 2019-12-05 16:57:08 -0800 | [diff] [blame] | 48 | import android.content.pm.ResolveInfo; |
| 49 | import android.content.pm.ServiceInfo; |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 50 | import android.hardware.biometrics.BiometricManager; |
| 51 | import android.hardware.biometrics.BiometricSourceType; |
| 52 | import android.hardware.biometrics.IBiometricEnabledOnKeyguardCallback; |
| 53 | import android.hardware.face.FaceManager; |
| 54 | import android.hardware.fingerprint.FingerprintManager; |
Fabian Kozynski | b6a2037 | 2020-04-01 09:36:43 -0400 | [diff] [blame] | 55 | import android.media.AudioManager; |
Bill Lin | ef81cbd | 2018-07-05 17:48:49 +0800 | [diff] [blame] | 56 | import android.os.Bundle; |
Fabian Kozynski | 5ca7a51 | 2019-10-16 19:56:11 +0000 | [diff] [blame] | 57 | import android.os.Handler; |
Kevin Chyn | 505eb89 | 2020-03-26 13:07:03 -0700 | [diff] [blame] | 58 | import android.os.IRemoteCallback; |
Fabian Kozynski | 5ca7a51 | 2019-10-16 19:56:11 +0000 | [diff] [blame] | 59 | import android.os.UserHandle; |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 60 | import android.os.UserManager; |
Bill Lin | ef81cbd | 2018-07-05 17:48:49 +0800 | [diff] [blame] | 61 | import android.telephony.ServiceState; |
Malcolm Chen | 5c63b51 | 2019-08-13 13:24:07 -0700 | [diff] [blame] | 62 | import android.telephony.SubscriptionInfo; |
Bill Lin | ef81cbd | 2018-07-05 17:48:49 +0800 | [diff] [blame] | 63 | import android.telephony.SubscriptionManager; |
Fabian Kozynski | b00c70b | 2020-04-03 12:41:31 -0400 | [diff] [blame] | 64 | import android.telephony.TelephonyManager; |
Lucas Dupin | 5e0f0d2 | 2018-02-26 13:32:16 -0800 | [diff] [blame] | 65 | import android.test.suitebuilder.annotation.SmallTest; |
| 66 | import android.testing.AndroidTestingRunner; |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 67 | import android.testing.TestableContext; |
Lucas Dupin | 5e0f0d2 | 2018-02-26 13:32:16 -0800 | [diff] [blame] | 68 | import android.testing.TestableLooper; |
Lucas Dupin | 5e0f0d2 | 2018-02-26 13:32:16 -0800 | [diff] [blame] | 69 | |
Fabian Kozynski | b6a2037 | 2020-04-01 09:36:43 -0400 | [diff] [blame] | 70 | import androidx.lifecycle.LiveData; |
| 71 | import androidx.lifecycle.Observer; |
| 72 | |
Hall Liu | 4506612 | 2020-04-07 15:37:44 -0700 | [diff] [blame] | 73 | import com.android.dx.mockito.inline.extended.ExtendedMockito; |
Lucas Dupin | 5e0f0d2 | 2018-02-26 13:32:16 -0800 | [diff] [blame] | 74 | import com.android.internal.telephony.TelephonyIntents; |
Kevin Chyn | 505eb89 | 2020-03-26 13:07:03 -0700 | [diff] [blame] | 75 | import com.android.keyguard.KeyguardUpdateMonitor.BiometricAuthenticated; |
Lucas Dupin | 5e0f0d2 | 2018-02-26 13:32:16 -0800 | [diff] [blame] | 76 | import com.android.systemui.SysuiTestCase; |
Fabian Kozynski | 5ca7a51 | 2019-10-16 19:56:11 +0000 | [diff] [blame] | 77 | import com.android.systemui.broadcast.BroadcastDispatcher; |
Ned Burns | aaeb44b | 2020-02-12 23:48:26 -0500 | [diff] [blame] | 78 | import com.android.systemui.dump.DumpManager; |
Curtis Belmonte | 9dc6815 | 2020-05-08 17:12:13 -0700 | [diff] [blame] | 79 | import com.android.systemui.plugins.statusbar.StatusBarStateController; |
| 80 | import com.android.systemui.statusbar.StatusBarState; |
Lucas Dupin | 4befb74 | 2019-07-01 11:31:31 -0700 | [diff] [blame] | 81 | import com.android.systemui.statusbar.phone.KeyguardBypassController; |
Fabian Kozynski | b6a2037 | 2020-04-01 09:36:43 -0400 | [diff] [blame] | 82 | import com.android.systemui.util.RingerModeTracker; |
Lucas Dupin | 5e0f0d2 | 2018-02-26 13:32:16 -0800 | [diff] [blame] | 83 | |
Beverly | 4e7dd9a | 2020-02-28 17:04:21 -0500 | [diff] [blame] | 84 | import org.junit.After; |
Lucas Dupin | 5e0f0d2 | 2018-02-26 13:32:16 -0800 | [diff] [blame] | 85 | import org.junit.Assert; |
| 86 | import org.junit.Before; |
| 87 | import org.junit.Test; |
| 88 | import org.junit.runner.RunWith; |
Fabian Kozynski | b6a2037 | 2020-04-01 09:36:43 -0400 | [diff] [blame] | 89 | import org.mockito.ArgumentCaptor; |
| 90 | import org.mockito.InOrder; |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 91 | import org.mockito.Mock; |
| 92 | import org.mockito.MockitoAnnotations; |
Hall Liu | 4506612 | 2020-04-07 15:37:44 -0700 | [diff] [blame] | 93 | import org.mockito.MockitoSession; |
Lucas Dupin | 5e0f0d2 | 2018-02-26 13:32:16 -0800 | [diff] [blame] | 94 | |
Malcolm Chen | 5c63b51 | 2019-08-13 13:24:07 -0700 | [diff] [blame] | 95 | import java.util.ArrayList; |
| 96 | import java.util.List; |
Haining Chen | c06c481 | 2020-01-13 20:38:53 -0800 | [diff] [blame] | 97 | import java.util.concurrent.Executor; |
Lucas Dupin | 5e0f0d2 | 2018-02-26 13:32:16 -0800 | [diff] [blame] | 98 | import java.util.concurrent.atomic.AtomicBoolean; |
| 99 | |
| 100 | @SmallTest |
| 101 | @RunWith(AndroidTestingRunner.class) |
Dave Mankoff | e229469 | 2019-08-14 11:53:13 -0400 | [diff] [blame] | 102 | @TestableLooper.RunWithLooper |
Lucas Dupin | 5e0f0d2 | 2018-02-26 13:32:16 -0800 | [diff] [blame] | 103 | public class KeyguardUpdateMonitorTest extends SysuiTestCase { |
Malcolm Chen | 5c63b51 | 2019-08-13 13:24:07 -0700 | [diff] [blame] | 104 | private static final String TEST_CARRIER = "TEST_CARRIER"; |
| 105 | private static final String TEST_CARRIER_2 = "TEST_CARRIER_2"; |
| 106 | private static final int TEST_CARRIER_ID = 1; |
| 107 | private static final String TEST_GROUP_UUID = "59b5c870-fc4c-47a4-a99e-9db826b48b24"; |
| 108 | private static final SubscriptionInfo TEST_SUBSCRIPTION = new SubscriptionInfo(1, "", 0, |
Peter Wang | ff7f25f | 2020-01-31 18:46:26 -0800 | [diff] [blame] | 109 | TEST_CARRIER, TEST_CARRIER, NAME_SOURCE_CARRIER_ID, 0xFFFFFF, "", |
Malcolm Chen | 5c63b51 | 2019-08-13 13:24:07 -0700 | [diff] [blame] | 110 | DATA_ROAMING_DISABLE, null, null, null, null, false, null, "", false, TEST_GROUP_UUID, |
| 111 | TEST_CARRIER_ID, 0); |
| 112 | private static final SubscriptionInfo TEST_SUBSCRIPTION_2 = new SubscriptionInfo(2, "", 0, |
Peter Wang | ff7f25f | 2020-01-31 18:46:26 -0800 | [diff] [blame] | 113 | TEST_CARRIER, TEST_CARRIER_2, NAME_SOURCE_CARRIER_ID, 0xFFFFFF, "", |
Malcolm Chen | 5c63b51 | 2019-08-13 13:24:07 -0700 | [diff] [blame] | 114 | DATA_ROAMING_DISABLE, null, null, null, null, false, null, "", true, TEST_GROUP_UUID, |
| 115 | TEST_CARRIER_ID, 0); |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 116 | @Mock |
Ned Burns | aaeb44b | 2020-02-12 23:48:26 -0500 | [diff] [blame] | 117 | private DumpManager mDumpManager; |
Lucas Dupin | 64171fe | 2019-10-30 14:28:29 -0700 | [diff] [blame] | 118 | @Mock |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 119 | private KeyguardUpdateMonitor.StrongAuthTracker mStrongAuthTracker; |
| 120 | @Mock |
| 121 | private TrustManager mTrustManager; |
| 122 | @Mock |
| 123 | private FingerprintManager mFingerprintManager; |
| 124 | @Mock |
| 125 | private FaceManager mFaceManager; |
| 126 | @Mock |
| 127 | private BiometricManager mBiometricManager; |
| 128 | @Mock |
| 129 | private PackageManager mPackageManager; |
| 130 | @Mock |
| 131 | private UserManager mUserManager; |
| 132 | @Mock |
| 133 | private DevicePolicyManager mDevicePolicyManager; |
Lucas Dupin | 4befb74 | 2019-07-01 11:31:31 -0700 | [diff] [blame] | 134 | @Mock |
| 135 | private KeyguardBypassController mKeyguardBypassController; |
Malcolm Chen | 5c63b51 | 2019-08-13 13:24:07 -0700 | [diff] [blame] | 136 | @Mock |
| 137 | private SubscriptionManager mSubscriptionManager; |
Fabian Kozynski | 5ca7a51 | 2019-10-16 19:56:11 +0000 | [diff] [blame] | 138 | @Mock |
| 139 | private BroadcastDispatcher mBroadcastDispatcher; |
Haining Chen | c06c481 | 2020-01-13 20:38:53 -0800 | [diff] [blame] | 140 | @Mock |
Hall Liu | 4506612 | 2020-04-07 15:37:44 -0700 | [diff] [blame] | 141 | private TelephonyManager mTelephonyManager; |
| 142 | @Mock |
Fabian Kozynski | b6a2037 | 2020-04-01 09:36:43 -0400 | [diff] [blame] | 143 | private RingerModeTracker mRingerModeTracker; |
| 144 | @Mock |
| 145 | private LiveData<Integer> mRingerModeLiveData; |
Curtis Belmonte | 9dc6815 | 2020-05-08 17:12:13 -0700 | [diff] [blame] | 146 | @Mock |
| 147 | private StatusBarStateController mStatusBarStateController; |
Fabian Kozynski | b00c70b | 2020-04-03 12:41:31 -0400 | [diff] [blame] | 148 | // Direct executor |
| 149 | private Executor mBackgroundExecutor = Runnable::run; |
Lucas Dupin | 5e0f0d2 | 2018-02-26 13:32:16 -0800 | [diff] [blame] | 150 | private TestableLooper mTestableLooper; |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 151 | private TestableKeyguardUpdateMonitor mKeyguardUpdateMonitor; |
Fabian Kozynski | b00c70b | 2020-04-03 12:41:31 -0400 | [diff] [blame] | 152 | private TestableContext mSpiedContext; |
Hall Liu | 4506612 | 2020-04-07 15:37:44 -0700 | [diff] [blame] | 153 | private MockitoSession mMockitoSession; |
Lucas Dupin | 5e0f0d2 | 2018-02-26 13:32:16 -0800 | [diff] [blame] | 154 | |
| 155 | @Before |
| 156 | public void setup() { |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 157 | MockitoAnnotations.initMocks(this); |
Fabian Kozynski | b00c70b | 2020-04-03 12:41:31 -0400 | [diff] [blame] | 158 | mSpiedContext = spy(mContext); |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 159 | when(mPackageManager.hasSystemFeature(anyString())).thenReturn(true); |
Fabian Kozynski | b00c70b | 2020-04-03 12:41:31 -0400 | [diff] [blame] | 160 | when(mSpiedContext.getPackageManager()).thenReturn(mPackageManager); |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 161 | doAnswer(invocation -> { |
| 162 | IBiometricEnabledOnKeyguardCallback callback = invocation.getArgument(0); |
Lucas Dupin | 7d95f15 | 2019-07-17 16:25:54 -0700 | [diff] [blame] | 163 | callback.onChanged(BiometricSourceType.FACE, true /* enabled */, |
| 164 | KeyguardUpdateMonitor.getCurrentUser()); |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 165 | return null; |
| 166 | }).when(mBiometricManager).registerEnabledOnKeyguardCallback(any()); |
| 167 | when(mFaceManager.isHardwareDetected()).thenReturn(true); |
| 168 | when(mFaceManager.hasEnrolledTemplates()).thenReturn(true); |
| 169 | when(mFaceManager.hasEnrolledTemplates(anyInt())).thenReturn(true); |
| 170 | when(mUserManager.isUserUnlocked(anyInt())).thenReturn(true); |
Kevin Chyn | fdfd2d3 | 2019-03-01 14:52:15 -0800 | [diff] [blame] | 171 | when(mUserManager.isPrimaryUser()).thenReturn(true); |
Haining Chen | c06c481 | 2020-01-13 20:38:53 -0800 | [diff] [blame] | 172 | when(mStrongAuthTracker |
| 173 | .isUnlockingWithBiometricAllowed(anyBoolean() /* isStrongBiometric */)) |
| 174 | .thenReturn(true); |
Hall Liu | 4506612 | 2020-04-07 15:37:44 -0700 | [diff] [blame] | 175 | |
| 176 | when(mTelephonyManager.getServiceStateForSubscriber(anyInt())) |
| 177 | .thenReturn(new ServiceState()); |
Fabian Kozynski | b00c70b | 2020-04-03 12:41:31 -0400 | [diff] [blame] | 178 | mSpiedContext.addMockSystemService(TrustManager.class, mTrustManager); |
| 179 | mSpiedContext.addMockSystemService(FingerprintManager.class, mFingerprintManager); |
| 180 | mSpiedContext.addMockSystemService(BiometricManager.class, mBiometricManager); |
| 181 | mSpiedContext.addMockSystemService(FaceManager.class, mFaceManager); |
| 182 | mSpiedContext.addMockSystemService(UserManager.class, mUserManager); |
| 183 | mSpiedContext.addMockSystemService(DevicePolicyManager.class, mDevicePolicyManager); |
| 184 | mSpiedContext.addMockSystemService(SubscriptionManager.class, mSubscriptionManager); |
| 185 | mSpiedContext.addMockSystemService(TelephonyManager.class, mTelephonyManager); |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 186 | |
Fabian Kozynski | b6a2037 | 2020-04-01 09:36:43 -0400 | [diff] [blame] | 187 | when(mRingerModeTracker.getRingerMode()).thenReturn(mRingerModeLiveData); |
| 188 | |
Hall Liu | 4506612 | 2020-04-07 15:37:44 -0700 | [diff] [blame] | 189 | mMockitoSession = ExtendedMockito.mockitoSession() |
| 190 | .spyStatic(SubscriptionManager.class).startMocking(); |
| 191 | ExtendedMockito.doReturn(SubscriptionManager.INVALID_SUBSCRIPTION_ID) |
| 192 | .when(SubscriptionManager::getDefaultSubscriptionId); |
| 193 | |
Lucas Dupin | 5e0f0d2 | 2018-02-26 13:32:16 -0800 | [diff] [blame] | 194 | mTestableLooper = TestableLooper.get(this); |
Beverly | 1467c9e | 2020-02-18 13:31:29 -0500 | [diff] [blame] | 195 | allowTestableLooperAsMainThread(); |
Fabian Kozynski | b00c70b | 2020-04-03 12:41:31 -0400 | [diff] [blame] | 196 | mKeyguardUpdateMonitor = new TestableKeyguardUpdateMonitor(mSpiedContext); |
Lucas Dupin | 5e0f0d2 | 2018-02-26 13:32:16 -0800 | [diff] [blame] | 197 | } |
| 198 | |
Beverly | 4e7dd9a | 2020-02-28 17:04:21 -0500 | [diff] [blame] | 199 | @After |
| 200 | public void tearDown() { |
Hall Liu | 4506612 | 2020-04-07 15:37:44 -0700 | [diff] [blame] | 201 | mMockitoSession.finishMocking(); |
Beverly | 4e7dd9a | 2020-02-28 17:04:21 -0500 | [diff] [blame] | 202 | mKeyguardUpdateMonitor.destroy(); |
| 203 | } |
| 204 | |
Lucas Dupin | 5e0f0d2 | 2018-02-26 13:32:16 -0800 | [diff] [blame] | 205 | @Test |
Fabian Kozynski | 5ca7a51 | 2019-10-16 19:56:11 +0000 | [diff] [blame] | 206 | public void testReceiversRegistered() { |
Fabian Kozynski | 5e92c6f | 2020-01-03 13:56:17 -0500 | [diff] [blame] | 207 | verify(mBroadcastDispatcher, atLeastOnce()).registerReceiverWithHandler( |
Fabian Kozynski | 5ca7a51 | 2019-10-16 19:56:11 +0000 | [diff] [blame] | 208 | eq(mKeyguardUpdateMonitor.mBroadcastReceiver), |
| 209 | any(IntentFilter.class), any(Handler.class)); |
Fabian Kozynski | 5e92c6f | 2020-01-03 13:56:17 -0500 | [diff] [blame] | 210 | verify(mBroadcastDispatcher, atLeastOnce()).registerReceiverWithHandler( |
Fabian Kozynski | 5ca7a51 | 2019-10-16 19:56:11 +0000 | [diff] [blame] | 211 | eq(mKeyguardUpdateMonitor.mBroadcastAllReceiver), |
| 212 | any(IntentFilter.class), any(Handler.class), eq(UserHandle.ALL)); |
| 213 | } |
| 214 | |
| 215 | @Test |
Fabian Kozynski | b00c70b | 2020-04-03 12:41:31 -0400 | [diff] [blame] | 216 | public void testSimStateInitialized() { |
| 217 | final int subId = 3; |
| 218 | final int state = TelephonyManager.SIM_STATE_ABSENT; |
| 219 | |
| 220 | when(mTelephonyManager.getActiveModemCount()).thenReturn(1); |
| 221 | when(mTelephonyManager.getSimState(anyInt())).thenReturn(state); |
| 222 | when(mSubscriptionManager.getSubscriptionIds(anyInt())).thenReturn(new int[] { subId }); |
| 223 | |
| 224 | KeyguardUpdateMonitor testKUM = new TestableKeyguardUpdateMonitor(mSpiedContext); |
| 225 | |
| 226 | mTestableLooper.processAllMessages(); |
| 227 | |
| 228 | assertThat(testKUM.getSimState(subId)).isEqualTo(state); |
| 229 | } |
| 230 | |
| 231 | @Test |
Lucas Dupin | 5e0f0d2 | 2018-02-26 13:32:16 -0800 | [diff] [blame] | 232 | public void testIgnoresSimStateCallback_rebroadcast() { |
| 233 | Intent intent = new Intent(TelephonyIntents.ACTION_SIM_STATE_CHANGED); |
| 234 | |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 235 | mKeyguardUpdateMonitor.mBroadcastReceiver.onReceive(getContext(), intent); |
Lucas Dupin | 5e0f0d2 | 2018-02-26 13:32:16 -0800 | [diff] [blame] | 236 | mTestableLooper.processAllMessages(); |
Lucas Dupin | 7ff82b0 | 2018-05-16 12:14:10 -0700 | [diff] [blame] | 237 | Assert.assertTrue("onSimStateChanged not called", |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 238 | mKeyguardUpdateMonitor.hasSimStateJustChanged()); |
Lucas Dupin | 5e0f0d2 | 2018-02-26 13:32:16 -0800 | [diff] [blame] | 239 | |
Meng Wang | 0e8d91a | 2020-01-17 14:58:01 -0800 | [diff] [blame] | 240 | intent.putExtra(Intent.EXTRA_REBROADCAST_ON_UNLOCK, true); |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 241 | mKeyguardUpdateMonitor.mBroadcastReceiver.onReceive(getContext(), intent); |
Lucas Dupin | 5e0f0d2 | 2018-02-26 13:32:16 -0800 | [diff] [blame] | 242 | mTestableLooper.processAllMessages(); |
Lucas Dupin | 7ff82b0 | 2018-05-16 12:14:10 -0700 | [diff] [blame] | 243 | Assert.assertFalse("onSimStateChanged should have been skipped", |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 244 | mKeyguardUpdateMonitor.hasSimStateJustChanged()); |
Lucas Dupin | 7ff82b0 | 2018-05-16 12:14:10 -0700 | [diff] [blame] | 245 | } |
| 246 | |
Bill Lin | ef81cbd | 2018-07-05 17:48:49 +0800 | [diff] [blame] | 247 | @Test |
| 248 | public void testTelephonyCapable_BootInitState() { |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 249 | assertThat(mKeyguardUpdateMonitor.mTelephonyCapable).isFalse(); |
Bill Lin | ef81cbd | 2018-07-05 17:48:49 +0800 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | @Test |
| 253 | public void testTelephonyCapable_SimState_Absent() { |
| 254 | Intent intent = new Intent(TelephonyIntents.ACTION_SIM_STATE_CHANGED); |
Jayachandran C | 0173df8 | 2019-12-19 12:11:03 -0800 | [diff] [blame] | 255 | intent.putExtra(Intent.EXTRA_SIM_STATE, |
| 256 | Intent.SIM_STATE_ABSENT); |
andychou | 695a760 | 2019-05-16 23:14:00 +0800 | [diff] [blame] | 257 | mKeyguardUpdateMonitor.mBroadcastReceiver.onReceive(getContext(), |
| 258 | putPhoneInfo(intent, null, false)); |
| 259 | mTestableLooper.processAllMessages(); |
| 260 | assertThat(mKeyguardUpdateMonitor.mTelephonyCapable).isTrue(); |
| 261 | } |
| 262 | |
| 263 | @Test |
| 264 | public void testTelephonyCapable_SimState_CardIOError() { |
| 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_CARD_IO_ERROR); |
andychou | 695a760 | 2019-05-16 23:14:00 +0800 | [diff] [blame] | 268 | mKeyguardUpdateMonitor.mBroadcastReceiver.onReceive(getContext(), |
| 269 | putPhoneInfo(intent, null, false)); |
Bill Lin | ef81cbd | 2018-07-05 17:48:49 +0800 | [diff] [blame] | 270 | mTestableLooper.processAllMessages(); |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 271 | assertThat(mKeyguardUpdateMonitor.mTelephonyCapable).isTrue(); |
Bill Lin | ef81cbd | 2018-07-05 17:48:49 +0800 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | @Test |
| 275 | public void testTelephonyCapable_SimInvalid_ServiceState_InService() { |
| 276 | // 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] | 277 | Intent intent = new Intent(Intent.ACTION_SERVICE_STATE); |
Bill Lin | ef81cbd | 2018-07-05 17:48:49 +0800 | [diff] [blame] | 278 | Bundle data = new Bundle(); |
| 279 | ServiceState state = new ServiceState(); |
| 280 | state.setState(ServiceState.STATE_IN_SERVICE); |
| 281 | state.fillInNotifierBundle(data); |
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_SimValid_ServiceState_PowerOff() { |
| 290 | // Simulate AirplaneMode case, SERVICE_STATE - POWER_OFF, check TelephonyCapable False |
| 291 | // Only receive ServiceState callback IN_SERVICE -> OUT_OF_SERVICE -> POWER_OFF |
Jayachandran C | 041e769 | 2019-12-20 16:20:02 -0800 | [diff] [blame] | 292 | Intent intent = new Intent(Intent.ACTION_SERVICE_STATE); |
Jayachandran C | 0173df8 | 2019-12-19 12:11:03 -0800 | [diff] [blame] | 293 | intent.putExtra(Intent.EXTRA_SIM_STATE |
| 294 | , Intent.SIM_STATE_LOADED); |
Bill Lin | ef81cbd | 2018-07-05 17:48:49 +0800 | [diff] [blame] | 295 | Bundle data = new Bundle(); |
| 296 | ServiceState state = new ServiceState(); |
| 297 | state.setState(ServiceState.STATE_POWER_OFF); |
| 298 | state.fillInNotifierBundle(data); |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 299 | mKeyguardUpdateMonitor.mBroadcastReceiver.onReceive(getContext() |
Bill Lin | ef81cbd | 2018-07-05 17:48:49 +0800 | [diff] [blame] | 300 | , putPhoneInfo(intent, data, true)); |
| 301 | mTestableLooper.processAllMessages(); |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 302 | assertThat(mKeyguardUpdateMonitor.mTelephonyCapable).isTrue(); |
Bill Lin | ef81cbd | 2018-07-05 17:48:49 +0800 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | /* Normal SIM inserted flow |
| 306 | * ServiceState: ---OutOfServie----->PowerOff->OutOfServie--->InService |
| 307 | * SimState: ----NOT_READY---->READY----------------------LOADED>>> |
| 308 | * Subscription: --------null---->null--->"Chunghwa Telecom"-------->>> |
| 309 | * System: -------------------------------BOOT_COMPLETED------>>> |
| 310 | * TelephonyCapable:(F)-(F)-(F)-(F)-(F)-(F)-(F)-(F)-(F)-(F)------(T)-(T)>> |
| 311 | */ |
| 312 | @Test |
| 313 | public void testTelephonyCapable_BootInitState_ServiceState_OutOfService() { |
Jayachandran C | 041e769 | 2019-12-20 16:20:02 -0800 | [diff] [blame] | 314 | Intent intent = new Intent(Intent.ACTION_SERVICE_STATE); |
Bill Lin | ef81cbd | 2018-07-05 17:48:49 +0800 | [diff] [blame] | 315 | Bundle data = new Bundle(); |
| 316 | ServiceState state = new ServiceState(); |
| 317 | state.setState(ServiceState.STATE_OUT_OF_SERVICE); |
| 318 | state.fillInNotifierBundle(data); |
| 319 | intent.putExtras(data); |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 320 | mKeyguardUpdateMonitor.mBroadcastReceiver.onReceive(getContext() |
Bill Lin | ef81cbd | 2018-07-05 17:48:49 +0800 | [diff] [blame] | 321 | , putPhoneInfo(intent, data, false)); |
| 322 | mTestableLooper.processAllMessages(); |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 323 | assertThat(mKeyguardUpdateMonitor.mTelephonyCapable).isFalse(); |
Bill Lin | ef81cbd | 2018-07-05 17:48:49 +0800 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | @Test |
| 327 | public void testTelephonyCapable_BootInitState_SimState_NotReady() { |
Bill Lin | ef81cbd | 2018-07-05 17:48:49 +0800 | [diff] [blame] | 328 | Bundle data = new Bundle(); |
| 329 | ServiceState state = new ServiceState(); |
| 330 | state.setState(ServiceState.STATE_OUT_OF_SERVICE); |
| 331 | state.fillInNotifierBundle(data); |
| 332 | Intent intent = new Intent(TelephonyIntents.ACTION_SIM_STATE_CHANGED); |
Jayachandran C | 0173df8 | 2019-12-19 12:11:03 -0800 | [diff] [blame] | 333 | intent.putExtra(Intent.EXTRA_SIM_STATE |
| 334 | , Intent.SIM_STATE_NOT_READY); |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 335 | mKeyguardUpdateMonitor.mBroadcastReceiver.onReceive(getContext() |
Bill Lin | ef81cbd | 2018-07-05 17:48:49 +0800 | [diff] [blame] | 336 | , putPhoneInfo(intent, data, false)); |
| 337 | mTestableLooper.processAllMessages(); |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 338 | assertThat(mKeyguardUpdateMonitor.mTelephonyCapable).isFalse(); |
Bill Lin | ef81cbd | 2018-07-05 17:48:49 +0800 | [diff] [blame] | 339 | } |
| 340 | |
| 341 | @Test |
| 342 | public void testTelephonyCapable_BootInitState_SimState_Ready() { |
Bill Lin | ef81cbd | 2018-07-05 17:48:49 +0800 | [diff] [blame] | 343 | Bundle data = new Bundle(); |
| 344 | ServiceState state = new ServiceState(); |
| 345 | state.setState(ServiceState.STATE_OUT_OF_SERVICE); |
| 346 | state.fillInNotifierBundle(data); |
| 347 | Intent intent = new Intent(TelephonyIntents.ACTION_SIM_STATE_CHANGED); |
Jayachandran C | 0173df8 | 2019-12-19 12:11:03 -0800 | [diff] [blame] | 348 | intent.putExtra(Intent.EXTRA_SIM_STATE |
| 349 | , Intent.SIM_STATE_READY); |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 350 | mKeyguardUpdateMonitor.mBroadcastReceiver.onReceive(getContext() |
Bill Lin | ef81cbd | 2018-07-05 17:48:49 +0800 | [diff] [blame] | 351 | , putPhoneInfo(intent, data, false)); |
| 352 | mTestableLooper.processAllMessages(); |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 353 | assertThat(mKeyguardUpdateMonitor.mTelephonyCapable).isFalse(); |
Bill Lin | ef81cbd | 2018-07-05 17:48:49 +0800 | [diff] [blame] | 354 | } |
| 355 | |
| 356 | @Test |
| 357 | public void testTelephonyCapable_BootInitState_ServiceState_PowerOff() { |
Jayachandran C | 041e769 | 2019-12-20 16:20:02 -0800 | [diff] [blame] | 358 | Intent intent = new Intent(Intent.ACTION_SERVICE_STATE); |
Bill Lin | ef81cbd | 2018-07-05 17:48:49 +0800 | [diff] [blame] | 359 | Bundle data = new Bundle(); |
| 360 | ServiceState state = new ServiceState(); |
| 361 | state.setState(ServiceState.STATE_POWER_OFF); |
| 362 | state.fillInNotifierBundle(data); |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 363 | mKeyguardUpdateMonitor.mBroadcastReceiver.onReceive(getContext() |
Bill Lin | ef81cbd | 2018-07-05 17:48:49 +0800 | [diff] [blame] | 364 | , putPhoneInfo(intent, data, false)); |
| 365 | mTestableLooper.processAllMessages(); |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 366 | assertThat(mKeyguardUpdateMonitor.mTelephonyCapable).isFalse(); |
Bill Lin | ef81cbd | 2018-07-05 17:48:49 +0800 | [diff] [blame] | 367 | } |
| 368 | |
| 369 | @Test |
| 370 | public void testTelephonyCapable_SimValid_ServiceState_InService() { |
Jayachandran C | 041e769 | 2019-12-20 16:20:02 -0800 | [diff] [blame] | 371 | Intent intent = new Intent(Intent.ACTION_SERVICE_STATE); |
Bill Lin | ef81cbd | 2018-07-05 17:48:49 +0800 | [diff] [blame] | 372 | Bundle data = new Bundle(); |
| 373 | ServiceState state = new ServiceState(); |
| 374 | state.setState(ServiceState.STATE_IN_SERVICE); |
| 375 | state.fillInNotifierBundle(data); |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 376 | mKeyguardUpdateMonitor.mBroadcastReceiver.onReceive(getContext() |
Bill Lin | ef81cbd | 2018-07-05 17:48:49 +0800 | [diff] [blame] | 377 | , putPhoneInfo(intent, data, true)); |
| 378 | mTestableLooper.processAllMessages(); |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 379 | assertThat(mKeyguardUpdateMonitor.mTelephonyCapable).isTrue(); |
Bill Lin | ef81cbd | 2018-07-05 17:48:49 +0800 | [diff] [blame] | 380 | } |
| 381 | |
| 382 | @Test |
| 383 | public void testTelephonyCapable_SimValid_SimState_Loaded() { |
Bill Lin | ef81cbd | 2018-07-05 17:48:49 +0800 | [diff] [blame] | 384 | Bundle data = new Bundle(); |
| 385 | ServiceState state = new ServiceState(); |
| 386 | state.setState(ServiceState.STATE_IN_SERVICE); |
| 387 | state.fillInNotifierBundle(data); |
| 388 | Intent intentSimState = new Intent(TelephonyIntents.ACTION_SIM_STATE_CHANGED); |
Jayachandran C | 0173df8 | 2019-12-19 12:11:03 -0800 | [diff] [blame] | 389 | intentSimState.putExtra(Intent.EXTRA_SIM_STATE |
| 390 | , Intent.SIM_STATE_LOADED); |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 391 | mKeyguardUpdateMonitor.mBroadcastReceiver.onReceive(getContext() |
Bill Lin | ef81cbd | 2018-07-05 17:48:49 +0800 | [diff] [blame] | 392 | , putPhoneInfo(intentSimState, data, true)); |
| 393 | mTestableLooper.processAllMessages(); |
Jayachandran C | 041e769 | 2019-12-20 16:20:02 -0800 | [diff] [blame] | 394 | // Even SimState Loaded, still need ACTION_SERVICE_STATE turn on mTelephonyCapable |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 395 | assertThat(mKeyguardUpdateMonitor.mTelephonyCapable).isFalse(); |
Bill Lin | ef81cbd | 2018-07-05 17:48:49 +0800 | [diff] [blame] | 396 | |
Jayachandran C | 041e769 | 2019-12-20 16:20:02 -0800 | [diff] [blame] | 397 | Intent intentServiceState = new Intent(Intent.ACTION_SERVICE_STATE); |
Jayachandran C | 0173df8 | 2019-12-19 12:11:03 -0800 | [diff] [blame] | 398 | intentSimState.putExtra(Intent.EXTRA_SIM_STATE |
| 399 | , Intent.SIM_STATE_LOADED); |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 400 | mKeyguardUpdateMonitor.mBroadcastReceiver.onReceive(getContext() |
Bill Lin | ef81cbd | 2018-07-05 17:48:49 +0800 | [diff] [blame] | 401 | , putPhoneInfo(intentServiceState, data, true)); |
| 402 | mTestableLooper.processAllMessages(); |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 403 | assertThat(mKeyguardUpdateMonitor.mTelephonyCapable).isTrue(); |
| 404 | } |
| 405 | |
| 406 | @Test |
| 407 | public void testTriesToAuthenticate_whenBouncer() { |
| 408 | mKeyguardUpdateMonitor.sendKeyguardBouncerChanged(true); |
| 409 | mTestableLooper.processAllMessages(); |
| 410 | |
Kevin Chyn | 8d2694a | 2019-04-11 18:30:40 -0700 | [diff] [blame] | 411 | verify(mFaceManager).authenticate(any(), any(), anyInt(), any(), any(), anyInt()); |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 412 | verify(mFaceManager).isHardwareDetected(); |
| 413 | verify(mFaceManager).hasEnrolledTemplates(anyInt()); |
| 414 | } |
| 415 | |
| 416 | @Test |
| 417 | public void testTriesToAuthenticate_whenKeyguard() { |
| 418 | mKeyguardUpdateMonitor.dispatchStartedWakingUp(); |
| 419 | mTestableLooper.processAllMessages(); |
| 420 | mKeyguardUpdateMonitor.onKeyguardVisibilityChanged(true); |
Kevin Chyn | 8d2694a | 2019-04-11 18:30:40 -0700 | [diff] [blame] | 421 | verify(mFaceManager).authenticate(any(), any(), anyInt(), any(), any(), anyInt()); |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 422 | } |
| 423 | |
| 424 | @Test |
Curtis Belmonte | 9dc6815 | 2020-05-08 17:12:13 -0700 | [diff] [blame] | 425 | public void skipsAuthentication_whenStatusBarShadeLocked() { |
| 426 | when(mStatusBarStateController.getState()).thenReturn(StatusBarState.SHADE_LOCKED); |
| 427 | |
| 428 | mKeyguardUpdateMonitor.dispatchStartedWakingUp(); |
| 429 | mTestableLooper.processAllMessages(); |
| 430 | mKeyguardUpdateMonitor.onKeyguardVisibilityChanged(true); |
| 431 | verify(mFaceManager, never()).authenticate(any(), any(), anyInt(), any(), any(), anyInt()); |
| 432 | } |
| 433 | |
| 434 | @Test |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 435 | public void skipsAuthentication_whenEncryptedKeyguard() { |
Lucas Dupin | 8eec268 | 2019-07-01 16:41:17 -0700 | [diff] [blame] | 436 | when(mStrongAuthTracker.getStrongAuthForUser(anyInt())).thenReturn( |
| 437 | KeyguardUpdateMonitor.StrongAuthTracker.STRONG_AUTH_REQUIRED_AFTER_BOOT); |
| 438 | mKeyguardUpdateMonitor.setKeyguardBypassController(mKeyguardBypassController); |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 439 | |
| 440 | mKeyguardUpdateMonitor.dispatchStartedWakingUp(); |
| 441 | mTestableLooper.processAllMessages(); |
| 442 | mKeyguardUpdateMonitor.onKeyguardVisibilityChanged(true); |
Lucas Dupin | 8eec268 | 2019-07-01 16:41:17 -0700 | [diff] [blame] | 443 | verify(mFaceManager, never()).authenticate(any(), any(), anyInt(), any(), any(), anyInt()); |
| 444 | } |
| 445 | |
| 446 | @Test |
| 447 | public void requiresAuthentication_whenEncryptedKeyguard_andBypass() { |
| 448 | testStrongAuthExceptOnBouncer( |
| 449 | KeyguardUpdateMonitor.StrongAuthTracker.STRONG_AUTH_REQUIRED_AFTER_BOOT); |
| 450 | } |
| 451 | |
| 452 | @Test |
| 453 | public void requiresAuthentication_whenTimeoutKeyguard_andBypass() { |
| 454 | testStrongAuthExceptOnBouncer( |
| 455 | KeyguardUpdateMonitor.StrongAuthTracker.STRONG_AUTH_REQUIRED_AFTER_TIMEOUT); |
| 456 | } |
| 457 | |
| 458 | private void testStrongAuthExceptOnBouncer(int strongAuth) { |
| 459 | when(mKeyguardBypassController.canBypass()).thenReturn(true); |
| 460 | mKeyguardUpdateMonitor.setKeyguardBypassController(mKeyguardBypassController); |
| 461 | when(mStrongAuthTracker.getStrongAuthForUser(anyInt())).thenReturn(strongAuth); |
| 462 | |
| 463 | mKeyguardUpdateMonitor.dispatchStartedWakingUp(); |
| 464 | mTestableLooper.processAllMessages(); |
| 465 | mKeyguardUpdateMonitor.onKeyguardVisibilityChanged(true); |
| 466 | verify(mFaceManager).authenticate(any(), any(), anyInt(), any(), any(), anyInt()); |
| 467 | |
| 468 | // Stop scanning when bouncer becomes visible |
| 469 | mKeyguardUpdateMonitor.sendKeyguardBouncerChanged(true /* showingBouncer */); |
| 470 | mTestableLooper.processAllMessages(); |
| 471 | clearInvocations(mFaceManager); |
| 472 | mKeyguardUpdateMonitor.requestFaceAuth(); |
| 473 | verify(mFaceManager, never()).authenticate(any(), any(), anyInt(), any(), any(), anyInt()); |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 474 | } |
| 475 | |
| 476 | @Test |
| 477 | public void testTriesToAuthenticate_whenAssistant() { |
| 478 | mKeyguardUpdateMonitor.setKeyguardOccluded(true); |
| 479 | mKeyguardUpdateMonitor.setAssistantVisible(true); |
| 480 | |
Kevin Chyn | 8d2694a | 2019-04-11 18:30:40 -0700 | [diff] [blame] | 481 | verify(mFaceManager).authenticate(any(), any(), anyInt(), any(), any(), anyInt()); |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 482 | } |
| 483 | |
| 484 | @Test |
Lucas Dupin | 4befb74 | 2019-07-01 11:31:31 -0700 | [diff] [blame] | 485 | public void testTriesToAuthenticate_whenTrustOnAgentKeyguard_ifBypass() { |
| 486 | mKeyguardUpdateMonitor.setKeyguardBypassController(mKeyguardBypassController); |
| 487 | mKeyguardUpdateMonitor.dispatchStartedWakingUp(); |
| 488 | mTestableLooper.processAllMessages(); |
| 489 | when(mKeyguardBypassController.canBypass()).thenReturn(true); |
| 490 | mKeyguardUpdateMonitor.onTrustChanged(true /* enabled */, |
| 491 | KeyguardUpdateMonitor.getCurrentUser(), 0 /* flags */); |
| 492 | mKeyguardUpdateMonitor.onKeyguardVisibilityChanged(true); |
| 493 | verify(mFaceManager).authenticate(any(), any(), anyInt(), any(), any(), anyInt()); |
| 494 | } |
| 495 | |
| 496 | @Test |
| 497 | public void testIgnoresAuth_whenTrustAgentOnKeyguard_withoutBypass() { |
| 498 | mKeyguardUpdateMonitor.dispatchStartedWakingUp(); |
| 499 | mTestableLooper.processAllMessages(); |
| 500 | mKeyguardUpdateMonitor.onTrustChanged(true /* enabled */, |
| 501 | KeyguardUpdateMonitor.getCurrentUser(), 0 /* flags */); |
| 502 | mKeyguardUpdateMonitor.onKeyguardVisibilityChanged(true); |
| 503 | verify(mFaceManager, never()).authenticate(any(), any(), anyInt(), any(), any(), anyInt()); |
| 504 | } |
| 505 | |
| 506 | @Test |
Lucas Dupin | 8eec268 | 2019-07-01 16:41:17 -0700 | [diff] [blame] | 507 | public void testIgnoresAuth_whenLockdown() { |
| 508 | mKeyguardUpdateMonitor.dispatchStartedWakingUp(); |
| 509 | mTestableLooper.processAllMessages(); |
| 510 | when(mStrongAuthTracker.getStrongAuthForUser(anyInt())).thenReturn( |
| 511 | KeyguardUpdateMonitor.StrongAuthTracker.STRONG_AUTH_REQUIRED_AFTER_USER_LOCKDOWN); |
| 512 | |
| 513 | mKeyguardUpdateMonitor.onKeyguardVisibilityChanged(true); |
| 514 | verify(mFaceManager, never()).authenticate(any(), any(), anyInt(), any(), any(), anyInt()); |
| 515 | } |
| 516 | |
| 517 | @Test |
Curtis Belmonte | c47df86 | 2020-01-28 16:58:34 -0800 | [diff] [blame] | 518 | public void testTriesToAuthenticate_whenLockout() { |
Lucas Dupin | 8eec268 | 2019-07-01 16:41:17 -0700 | [diff] [blame] | 519 | mKeyguardUpdateMonitor.dispatchStartedWakingUp(); |
| 520 | mTestableLooper.processAllMessages(); |
| 521 | when(mStrongAuthTracker.getStrongAuthForUser(anyInt())).thenReturn( |
| 522 | KeyguardUpdateMonitor.StrongAuthTracker.STRONG_AUTH_REQUIRED_AFTER_LOCKOUT); |
| 523 | |
| 524 | mKeyguardUpdateMonitor.onKeyguardVisibilityChanged(true); |
Curtis Belmonte | c47df86 | 2020-01-28 16:58:34 -0800 | [diff] [blame] | 525 | verify(mFaceManager).authenticate(any(), any(), anyInt(), any(), any(), anyInt()); |
Lucas Dupin | 8eec268 | 2019-07-01 16:41:17 -0700 | [diff] [blame] | 526 | } |
| 527 | |
| 528 | @Test |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 529 | public void testOnFaceAuthenticated_skipsFaceWhenAuthenticated() { |
Haining Chen | c06c481 | 2020-01-13 20:38:53 -0800 | [diff] [blame] | 530 | // test whether face will be skipped if authenticated, so the value of isStrongBiometric |
| 531 | // doesn't matter here |
| 532 | mKeyguardUpdateMonitor.onFaceAuthenticated(KeyguardUpdateMonitor.getCurrentUser(), |
| 533 | true /* isStrongBiometric */); |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 534 | mKeyguardUpdateMonitor.sendKeyguardBouncerChanged(true); |
| 535 | mTestableLooper.processAllMessages(); |
| 536 | |
| 537 | verify(mFaceManager, never()).authenticate(any(), any(), anyInt(), any(), any()); |
| 538 | } |
| 539 | |
| 540 | @Test |
| 541 | public void testGetUserCanSkipBouncer_whenFace() { |
| 542 | int user = KeyguardUpdateMonitor.getCurrentUser(); |
Haining Chen | c06c481 | 2020-01-13 20:38:53 -0800 | [diff] [blame] | 543 | mKeyguardUpdateMonitor.onFaceAuthenticated(user, true /* isStrongBiometric */); |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 544 | assertThat(mKeyguardUpdateMonitor.getUserCanSkipBouncer(user)).isTrue(); |
| 545 | } |
| 546 | |
| 547 | @Test |
Haining Chen | c06c481 | 2020-01-13 20:38:53 -0800 | [diff] [blame] | 548 | public void testGetUserCanSkipBouncer_whenFace_nonStrongAndDisallowed() { |
| 549 | when(mStrongAuthTracker.isUnlockingWithBiometricAllowed(false /* isStrongBiometric */)) |
| 550 | .thenReturn(false); |
| 551 | int user = KeyguardUpdateMonitor.getCurrentUser(); |
| 552 | mKeyguardUpdateMonitor.onFaceAuthenticated(user, false /* isStrongBiometric */); |
| 553 | assertThat(mKeyguardUpdateMonitor.getUserCanSkipBouncer(user)).isFalse(); |
| 554 | } |
| 555 | |
| 556 | @Test |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 557 | public void testGetUserCanSkipBouncer_whenFingerprint() { |
| 558 | int user = KeyguardUpdateMonitor.getCurrentUser(); |
Haining Chen | c06c481 | 2020-01-13 20:38:53 -0800 | [diff] [blame] | 559 | mKeyguardUpdateMonitor.onFingerprintAuthenticated(user, true /* isStrongBiometric */); |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 560 | assertThat(mKeyguardUpdateMonitor.getUserCanSkipBouncer(user)).isTrue(); |
| 561 | } |
| 562 | |
| 563 | @Test |
Haining Chen | c06c481 | 2020-01-13 20:38:53 -0800 | [diff] [blame] | 564 | public void testGetUserCanSkipBouncer_whenFingerprint_nonStrongAndDisallowed() { |
| 565 | when(mStrongAuthTracker.isUnlockingWithBiometricAllowed(false /* isStrongBiometric */)) |
| 566 | .thenReturn(false); |
| 567 | int user = KeyguardUpdateMonitor.getCurrentUser(); |
| 568 | mKeyguardUpdateMonitor.onFingerprintAuthenticated(user, false /* isStrongBiometric */); |
| 569 | assertThat(mKeyguardUpdateMonitor.getUserCanSkipBouncer(user)).isFalse(); |
| 570 | } |
| 571 | |
| 572 | @Test |
Kevin Chyn | 505eb89 | 2020-03-26 13:07:03 -0700 | [diff] [blame] | 573 | public void testBiometricsCleared_whenUserSwitches() throws Exception { |
| 574 | final IRemoteCallback reply = new IRemoteCallback.Stub() { |
| 575 | @Override |
| 576 | public void sendResult(Bundle data) {} // do nothing |
| 577 | }; |
| 578 | final BiometricAuthenticated dummyAuthentication = |
| 579 | new BiometricAuthenticated(true /* authenticated */, true /* strong */); |
| 580 | mKeyguardUpdateMonitor.mUserFaceAuthenticated.put(0 /* user */, dummyAuthentication); |
| 581 | mKeyguardUpdateMonitor.mUserFingerprintAuthenticated.put(0 /* user */, dummyAuthentication); |
| 582 | assertThat(mKeyguardUpdateMonitor.mUserFingerprintAuthenticated.size()).isEqualTo(1); |
| 583 | assertThat(mKeyguardUpdateMonitor.mUserFaceAuthenticated.size()).isEqualTo(1); |
| 584 | |
| 585 | mKeyguardUpdateMonitor.handleUserSwitching(10 /* user */, reply); |
| 586 | assertThat(mKeyguardUpdateMonitor.mUserFingerprintAuthenticated.size()).isEqualTo(0); |
| 587 | assertThat(mKeyguardUpdateMonitor.mUserFaceAuthenticated.size()).isEqualTo(0); |
| 588 | } |
| 589 | |
| 590 | @Test |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 591 | public void testGetUserCanSkipBouncer_whenTrust() { |
| 592 | int user = KeyguardUpdateMonitor.getCurrentUser(); |
| 593 | mKeyguardUpdateMonitor.onTrustChanged(true /* enabled */, user, 0 /* flags */); |
| 594 | assertThat(mKeyguardUpdateMonitor.getUserCanSkipBouncer(user)).isTrue(); |
Bill Lin | ef81cbd | 2018-07-05 17:48:49 +0800 | [diff] [blame] | 595 | } |
| 596 | |
Malcolm Chen | 5c63b51 | 2019-08-13 13:24:07 -0700 | [diff] [blame] | 597 | @Test |
| 598 | public void testGetSubscriptionInfo_whenInGroupedSubWithOpportunistic() { |
| 599 | List<SubscriptionInfo> list = new ArrayList<>(); |
| 600 | list.add(TEST_SUBSCRIPTION); |
| 601 | list.add(TEST_SUBSCRIPTION_2); |
Sooraj Sasindran | 4046806 | 2020-01-06 19:10:50 -0800 | [diff] [blame] | 602 | when(mSubscriptionManager.getCompleteActiveSubscriptionInfoList()).thenReturn(list); |
Malcolm Chen | 5c63b51 | 2019-08-13 13:24:07 -0700 | [diff] [blame] | 603 | mKeyguardUpdateMonitor.mPhoneStateListener.onActiveDataSubscriptionIdChanged( |
| 604 | TEST_SUBSCRIPTION_2.getSubscriptionId()); |
| 605 | mTestableLooper.processAllMessages(); |
| 606 | |
| 607 | List<SubscriptionInfo> listToVerify = mKeyguardUpdateMonitor |
| 608 | .getFilteredSubscriptionInfo(false); |
| 609 | assertThat(listToVerify.size()).isEqualTo(1); |
| 610 | assertThat(listToVerify.get(0)).isEqualTo(TEST_SUBSCRIPTION_2); |
| 611 | } |
| 612 | |
Lucas Dupin | 6c6d573 | 2019-08-27 17:36:05 -0700 | [diff] [blame] | 613 | @Test |
| 614 | public void testIsUserUnlocked() { |
| 615 | // mUserManager will report the user as unlocked on @Before |
| 616 | assertThat(mKeyguardUpdateMonitor.isUserUnlocked(KeyguardUpdateMonitor.getCurrentUser())) |
| 617 | .isTrue(); |
| 618 | // Invalid user should not be unlocked. |
| 619 | int randomUser = 99; |
| 620 | assertThat(mKeyguardUpdateMonitor.isUserUnlocked(randomUser)).isFalse(); |
| 621 | } |
| 622 | |
Lucas Dupin | f2c5350 | 2019-10-03 13:56:18 -0700 | [diff] [blame] | 623 | @Test |
| 624 | public void testTrustUsuallyManaged_whenTrustChanges() { |
| 625 | int user = KeyguardUpdateMonitor.getCurrentUser(); |
| 626 | when(mTrustManager.isTrustUsuallyManaged(eq(user))).thenReturn(true); |
| 627 | mKeyguardUpdateMonitor.onTrustManagedChanged(false /* managed */, user); |
| 628 | assertThat(mKeyguardUpdateMonitor.isTrustUsuallyManaged(user)).isTrue(); |
| 629 | } |
| 630 | |
| 631 | @Test |
| 632 | public void testTrustUsuallyManaged_resetWhenUserIsRemoved() { |
| 633 | int user = KeyguardUpdateMonitor.getCurrentUser(); |
| 634 | when(mTrustManager.isTrustUsuallyManaged(eq(user))).thenReturn(true); |
| 635 | mKeyguardUpdateMonitor.onTrustManagedChanged(false /* managed */, user); |
| 636 | assertThat(mKeyguardUpdateMonitor.isTrustUsuallyManaged(user)).isTrue(); |
| 637 | |
| 638 | mKeyguardUpdateMonitor.handleUserRemoved(user); |
| 639 | assertThat(mKeyguardUpdateMonitor.isTrustUsuallyManaged(user)).isFalse(); |
| 640 | } |
| 641 | |
Yvonne Jiang | b7024a2 | 2019-12-05 16:57:08 -0800 | [diff] [blame] | 642 | @Test |
| 643 | public void testSecondaryLockscreenRequirement() { |
| 644 | int user = KeyguardUpdateMonitor.getCurrentUser(); |
| 645 | String packageName = "fake.test.package"; |
| 646 | String cls = "FakeService"; |
| 647 | ServiceInfo serviceInfo = new ServiceInfo(); |
| 648 | serviceInfo.packageName = packageName; |
| 649 | serviceInfo.name = cls; |
| 650 | ResolveInfo resolveInfo = new ResolveInfo(); |
| 651 | resolveInfo.serviceInfo = serviceInfo; |
| 652 | when(mPackageManager.resolveService(any(Intent.class), eq(0))).thenReturn(resolveInfo); |
Yvonne Jiang | 8345da3 | 2020-03-19 15:01:16 -0700 | [diff] [blame] | 653 | when(mDevicePolicyManager.isSecondaryLockscreenEnabled(eq(UserHandle.of(user)))) |
| 654 | .thenReturn(true, false); |
Yvonne Jiang | 70a6237 | 2020-03-31 16:51:17 -0700 | [diff] [blame] | 655 | when(mDevicePolicyManager.getProfileOwnerOrDeviceOwnerSupervisionComponent( |
| 656 | UserHandle.of(user))) |
Yvonne Jiang | 8345da3 | 2020-03-19 15:01:16 -0700 | [diff] [blame] | 657 | .thenReturn(new ComponentName(packageName, cls)); |
Yvonne Jiang | b7024a2 | 2019-12-05 16:57:08 -0800 | [diff] [blame] | 658 | |
| 659 | // Initially null. |
| 660 | assertThat(mKeyguardUpdateMonitor.getSecondaryLockscreenRequirement(user)).isNull(); |
| 661 | |
| 662 | // Set non-null after DPM change. |
| 663 | setBroadcastReceiverPendingResult(mKeyguardUpdateMonitor.mBroadcastAllReceiver); |
| 664 | Intent intent = new Intent(DevicePolicyManager.ACTION_DEVICE_POLICY_MANAGER_STATE_CHANGED); |
| 665 | mKeyguardUpdateMonitor.mBroadcastAllReceiver.onReceive(getContext(), intent); |
| 666 | mTestableLooper.processAllMessages(); |
| 667 | |
| 668 | Intent storedIntent = mKeyguardUpdateMonitor.getSecondaryLockscreenRequirement(user); |
| 669 | assertThat(storedIntent.getComponent().getClassName()).isEqualTo(cls); |
| 670 | assertThat(storedIntent.getComponent().getPackageName()).isEqualTo(packageName); |
| 671 | |
| 672 | // Back to null after another DPM change. |
| 673 | mKeyguardUpdateMonitor.mBroadcastAllReceiver.onReceive(getContext(), intent); |
| 674 | mTestableLooper.processAllMessages(); |
| 675 | assertThat(mKeyguardUpdateMonitor.getSecondaryLockscreenRequirement(user)).isNull(); |
| 676 | } |
| 677 | |
Fabian Kozynski | b6a2037 | 2020-04-01 09:36:43 -0400 | [diff] [blame] | 678 | @Test |
| 679 | public void testRingerModeChange() { |
| 680 | ArgumentCaptor<Observer<Integer>> captor = ArgumentCaptor.forClass(Observer.class); |
| 681 | verify(mRingerModeLiveData).observeForever(captor.capture()); |
| 682 | Observer<Integer> observer = captor.getValue(); |
| 683 | |
| 684 | KeyguardUpdateMonitorCallback callback = mock(KeyguardUpdateMonitorCallback.class); |
| 685 | |
| 686 | mKeyguardUpdateMonitor.registerCallback(callback); |
| 687 | |
| 688 | observer.onChanged(AudioManager.RINGER_MODE_NORMAL); |
| 689 | observer.onChanged(AudioManager.RINGER_MODE_SILENT); |
| 690 | observer.onChanged(AudioManager.RINGER_MODE_VIBRATE); |
| 691 | |
| 692 | mTestableLooper.processAllMessages(); |
| 693 | |
| 694 | InOrder orderVerify = inOrder(callback); |
| 695 | orderVerify.verify(callback).onRingerModeChanged(anyInt()); // Initial update on register |
| 696 | orderVerify.verify(callback).onRingerModeChanged(AudioManager.RINGER_MODE_NORMAL); |
| 697 | orderVerify.verify(callback).onRingerModeChanged(AudioManager.RINGER_MODE_SILENT); |
| 698 | orderVerify.verify(callback).onRingerModeChanged(AudioManager.RINGER_MODE_VIBRATE); |
| 699 | } |
| 700 | |
Yvonne Jiang | b7024a2 | 2019-12-05 16:57:08 -0800 | [diff] [blame] | 701 | private void setBroadcastReceiverPendingResult(BroadcastReceiver receiver) { |
| 702 | BroadcastReceiver.PendingResult pendingResult = |
| 703 | new BroadcastReceiver.PendingResult(Activity.RESULT_OK, |
| 704 | "resultData", |
| 705 | /* resultExtras= */ null, |
| 706 | BroadcastReceiver.PendingResult.TYPE_UNREGISTERED, |
| 707 | /* ordered= */ true, |
| 708 | /* sticky= */ false, |
| 709 | /* token= */ null, |
| 710 | UserHandle.myUserId(), |
| 711 | /* flags= */ 0); |
| 712 | receiver.setPendingResult(pendingResult); |
| 713 | } |
| 714 | |
Bill Lin | ef81cbd | 2018-07-05 17:48:49 +0800 | [diff] [blame] | 715 | private Intent putPhoneInfo(Intent intent, Bundle data, Boolean simInited) { |
| 716 | int subscription = simInited |
| 717 | ? 1/* mock subid=1 */ : SubscriptionManager.DUMMY_SUBSCRIPTION_ID_BASE; |
| 718 | if (data != null) intent.putExtras(data); |
Daniel Bright | 63514be | 2020-01-15 12:10:53 -0800 | [diff] [blame] | 719 | |
| 720 | intent.putExtra(SubscriptionManager.EXTRA_SUBSCRIPTION_INDEX, subscription); |
| 721 | intent.putExtra(SubscriptionManager.EXTRA_SLOT_INDEX, 0); |
Bill Lin | ef81cbd | 2018-07-05 17:48:49 +0800 | [diff] [blame] | 722 | return intent; |
| 723 | } |
| 724 | |
Lucas Dupin | 7ff82b0 | 2018-05-16 12:14:10 -0700 | [diff] [blame] | 725 | private class TestableKeyguardUpdateMonitor extends KeyguardUpdateMonitor { |
| 726 | AtomicBoolean mSimStateChanged = new AtomicBoolean(false); |
| 727 | |
| 728 | protected TestableKeyguardUpdateMonitor(Context context) { |
Fabian Kozynski | 5ca7a51 | 2019-10-16 19:56:11 +0000 | [diff] [blame] | 729 | super(context, |
| 730 | TestableLooper.get(KeyguardUpdateMonitorTest.this).getLooper(), |
Fabian Kozynski | b6a2037 | 2020-04-01 09:36:43 -0400 | [diff] [blame] | 731 | mBroadcastDispatcher, mDumpManager, |
Curtis Belmonte | 9dc6815 | 2020-05-08 17:12:13 -0700 | [diff] [blame] | 732 | mRingerModeTracker, mBackgroundExecutor, mStatusBarStateController); |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 733 | mStrongAuthTracker = KeyguardUpdateMonitorTest.this.mStrongAuthTracker; |
Lucas Dupin | 7ff82b0 | 2018-05-16 12:14:10 -0700 | [diff] [blame] | 734 | } |
| 735 | |
| 736 | public boolean hasSimStateJustChanged() { |
| 737 | return mSimStateChanged.getAndSet(false); |
| 738 | } |
| 739 | |
| 740 | @Override |
Jayachandran C | f5436a6 | 2019-11-08 18:22:45 -0800 | [diff] [blame] | 741 | protected void handleSimStateChange(int subId, int slotId, int state) { |
Lucas Dupin | 7ff82b0 | 2018-05-16 12:14:10 -0700 | [diff] [blame] | 742 | mSimStateChanged.set(true); |
| 743 | super.handleSimStateChange(subId, slotId, state); |
| 744 | } |
Lucas Dupin | 5e0f0d2 | 2018-02-26 13:32:16 -0800 | [diff] [blame] | 745 | } |
| 746 | } |