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