Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 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.systemui.statusbar.phone; |
| 18 | |
Lucas Dupin | a055a67 | 2019-07-16 13:46:25 -0700 | [diff] [blame] | 19 | import static com.google.common.truth.Truth.assertThat; |
| 20 | |
Lucas Dupin | 45142ba | 2019-04-10 13:13:20 -0700 | [diff] [blame] | 21 | import static org.mockito.ArgumentMatchers.any; |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 22 | import static org.mockito.ArgumentMatchers.anyBoolean; |
| 23 | import static org.mockito.ArgumentMatchers.anyFloat; |
| 24 | import static org.mockito.ArgumentMatchers.eq; |
| 25 | import static org.mockito.Mockito.never; |
| 26 | import static org.mockito.Mockito.reset; |
| 27 | import static org.mockito.Mockito.verify; |
| 28 | import static org.mockito.Mockito.when; |
| 29 | |
| 30 | import android.hardware.biometrics.BiometricSourceType; |
| 31 | import android.os.Handler; |
| 32 | import android.os.PowerManager; |
| 33 | import android.os.UserHandle; |
| 34 | import android.test.suitebuilder.annotation.SmallTest; |
| 35 | import android.testing.AndroidTestingRunner; |
| 36 | import android.testing.TestableLooper.RunWithLooper; |
| 37 | |
| 38 | import com.android.keyguard.KeyguardUpdateMonitor; |
| 39 | import com.android.systemui.SysuiTestCase; |
| 40 | import com.android.systemui.keyguard.KeyguardViewMediator; |
| 41 | import com.android.systemui.statusbar.NotificationMediaManager; |
| 42 | |
| 43 | import org.junit.Before; |
| 44 | import org.junit.Test; |
| 45 | import org.junit.runner.RunWith; |
| 46 | import org.mockito.Mock; |
| 47 | import org.mockito.MockitoAnnotations; |
| 48 | |
| 49 | @SmallTest |
| 50 | @RunWith(AndroidTestingRunner.class) |
| 51 | @RunWithLooper |
| 52 | public class BiometricsUnlockControllerTest extends SysuiTestCase { |
| 53 | |
| 54 | @Mock |
| 55 | private NotificationMediaManager mMediaManager; |
| 56 | @Mock |
| 57 | private PowerManager mPowerManager; |
| 58 | @Mock |
| 59 | private KeyguardUpdateMonitor mUpdateMonitor; |
| 60 | @Mock |
| 61 | private StatusBarKeyguardViewManager mStatusBarKeyguardViewManager; |
| 62 | @Mock |
| 63 | private StatusBarWindowController mStatusBarWindowController; |
| 64 | @Mock |
| 65 | private DozeScrimController mDozeScrimController; |
| 66 | @Mock |
| 67 | private KeyguardViewMediator mKeyguardViewMediator; |
| 68 | @Mock |
| 69 | private ScrimController mScrimController; |
| 70 | @Mock |
| 71 | private StatusBar mStatusBar; |
| 72 | @Mock |
| 73 | private UnlockMethodCache mUnlockMethodCache; |
Lucas Dupin | c94963f | 2019-03-07 14:54:05 -0800 | [diff] [blame] | 74 | @Mock |
Lucas Dupin | 45142ba | 2019-04-10 13:13:20 -0700 | [diff] [blame] | 75 | private Handler mHandler; |
Lucas Dupin | 9bae9c2 | 2019-06-04 16:37:40 -0700 | [diff] [blame] | 76 | @Mock |
| 77 | private KeyguardBypassController mKeyguardBypassController; |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 78 | private BiometricUnlockController mBiometricUnlockController; |
| 79 | |
| 80 | @Before |
| 81 | public void setUp() { |
| 82 | MockitoAnnotations.initMocks(this); |
| 83 | when(mStatusBarKeyguardViewManager.isShowing()).thenReturn(true); |
| 84 | when(mUpdateMonitor.isDeviceInteractive()).thenReturn(true); |
Lucas Dupin | 4c50704 | 2019-07-22 16:47:34 -0700 | [diff] [blame^] | 85 | when(mUnlockMethodCache.isFaceAuthEnabled()).thenReturn(true); |
Selim Cinek | bd84c16 | 2019-06-11 16:23:23 -0700 | [diff] [blame] | 86 | when(mKeyguardBypassController.onBiometricAuthenticated(any())).thenReturn(true); |
Lucas Dupin | fa817a0 | 2019-07-02 15:22:54 -0700 | [diff] [blame] | 87 | when(mKeyguardBypassController.canPlaySubtleWindowAnimations()).thenReturn(true); |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 88 | mContext.addMockSystemService(PowerManager.class, mPowerManager); |
| 89 | mDependency.injectTestDependency(NotificationMediaManager.class, mMediaManager); |
| 90 | mDependency.injectTestDependency(StatusBarWindowController.class, |
| 91 | mStatusBarWindowController); |
Lucas Dupin | 9bae9c2 | 2019-06-04 16:37:40 -0700 | [diff] [blame] | 92 | mBiometricUnlockController = new BiometricUnlockController(mContext, mDozeScrimController, |
| 93 | mKeyguardViewMediator, mScrimController, mStatusBar, mUnlockMethodCache, |
| 94 | mHandler, mUpdateMonitor, 0 /* wakeUpDelay */, mKeyguardBypassController); |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 95 | mBiometricUnlockController.setStatusBarKeyguardViewManager(mStatusBarKeyguardViewManager); |
| 96 | } |
| 97 | |
| 98 | @Test |
| 99 | public void onBiometricAuthenticated_whenFingerprintAndBiometricsDisallowed_showBouncer() { |
| 100 | mBiometricUnlockController.onBiometricAuthenticated(UserHandle.USER_CURRENT, |
| 101 | BiometricSourceType.FINGERPRINT); |
| 102 | verify(mStatusBarKeyguardViewManager).showBouncer(eq(false)); |
| 103 | verify(mStatusBarKeyguardViewManager).animateCollapsePanels(anyFloat()); |
| 104 | } |
| 105 | |
| 106 | @Test |
| 107 | public void onBiometricAuthenticated_whenFingerprintAndNotInteractive_wakeAndUnlock() { |
| 108 | reset(mUpdateMonitor); |
| 109 | reset(mStatusBarKeyguardViewManager); |
| 110 | when(mStatusBarKeyguardViewManager.isShowing()).thenReturn(true); |
| 111 | when(mUpdateMonitor.isUnlockingWithBiometricAllowed()).thenReturn(true); |
| 112 | when(mDozeScrimController.isPulsing()).thenReturn(true); |
| 113 | mBiometricUnlockController.onBiometricAuthenticated(UserHandle.USER_CURRENT, |
| 114 | BiometricSourceType.FINGERPRINT); |
| 115 | |
| 116 | verify(mKeyguardViewMediator).onWakeAndUnlocking(); |
| 117 | } |
| 118 | |
| 119 | @Test |
| 120 | public void onBiometricAuthenticated_whenFingerprint_dismissKeyguard() { |
| 121 | when(mUpdateMonitor.isUnlockingWithBiometricAllowed()).thenReturn(true); |
| 122 | mBiometricUnlockController.onBiometricAuthenticated(UserHandle.USER_CURRENT, |
| 123 | BiometricSourceType.FINGERPRINT); |
| 124 | |
| 125 | verify(mStatusBarKeyguardViewManager, never()).showBouncer(anyBoolean()); |
Lucas Dupin | fa817a0 | 2019-07-02 15:22:54 -0700 | [diff] [blame] | 126 | verify(mStatusBarKeyguardViewManager).animateCollapsePanels(anyFloat()); |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | @Test |
| 130 | public void onBiometricAuthenticated_whenFingerprintOnBouncer_dismissBouncer() { |
| 131 | when(mUpdateMonitor.isUnlockingWithBiometricAllowed()).thenReturn(true); |
Lucas Dupin | fa817a0 | 2019-07-02 15:22:54 -0700 | [diff] [blame] | 132 | when(mStatusBarKeyguardViewManager.bouncerIsOrWillBeShowing()).thenReturn(true); |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 133 | mBiometricUnlockController.onBiometricAuthenticated(UserHandle.USER_CURRENT, |
| 134 | BiometricSourceType.FINGERPRINT); |
| 135 | |
| 136 | verify(mStatusBarKeyguardViewManager).notifyKeyguardAuthenticated(eq(false)); |
| 137 | } |
| 138 | |
| 139 | @Test |
| 140 | public void onBiometricAuthenticated_whenFace_dontDismissKeyguard() { |
| 141 | when(mUpdateMonitor.isUnlockingWithBiometricAllowed()).thenReturn(true); |
| 142 | mBiometricUnlockController.onBiometricAuthenticated(UserHandle.USER_CURRENT, |
| 143 | BiometricSourceType.FACE); |
| 144 | |
| 145 | verify(mStatusBarKeyguardViewManager, never()).animateCollapsePanels(anyFloat()); |
Lucas Dupin | fa817a0 | 2019-07-02 15:22:54 -0700 | [diff] [blame] | 146 | verify(mStatusBarKeyguardViewManager, never()).notifyKeyguardAuthenticated(anyBoolean()); |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | @Test |
Lucas Dupin | 9bae9c2 | 2019-06-04 16:37:40 -0700 | [diff] [blame] | 150 | public void onBiometricAuthenticated_whenFace_andBypass_dismissKeyguard() { |
| 151 | when(mKeyguardBypassController.getBypassEnabled()).thenReturn(true); |
Lucas Dupin | c94963f | 2019-03-07 14:54:05 -0800 | [diff] [blame] | 152 | mBiometricUnlockController.setStatusBarKeyguardViewManager(mStatusBarKeyguardViewManager); |
| 153 | |
| 154 | when(mUpdateMonitor.isUnlockingWithBiometricAllowed()).thenReturn(true); |
| 155 | mBiometricUnlockController.onBiometricAuthenticated(UserHandle.USER_CURRENT, |
| 156 | BiometricSourceType.FACE); |
| 157 | |
Lucas Dupin | fa817a0 | 2019-07-02 15:22:54 -0700 | [diff] [blame] | 158 | verify(mStatusBarKeyguardViewManager, never()).animateCollapsePanels(anyFloat()); |
Lucas Dupin | e78794c | 2019-06-18 11:55:37 -0700 | [diff] [blame] | 159 | verify(mStatusBarKeyguardViewManager).notifyKeyguardAuthenticated(eq(false)); |
Lucas Dupin | c94963f | 2019-03-07 14:54:05 -0800 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | @Test |
Lucas Dupin | 8eec268 | 2019-07-01 16:41:17 -0700 | [diff] [blame] | 163 | public void onBiometricAuthenticated_whenFace_andBypass_encrypted_showBouncer() { |
| 164 | when(mKeyguardBypassController.getBypassEnabled()).thenReturn(true); |
| 165 | mBiometricUnlockController.setStatusBarKeyguardViewManager(mStatusBarKeyguardViewManager); |
| 166 | |
| 167 | when(mUpdateMonitor.isUnlockingWithBiometricAllowed()).thenReturn(false); |
| 168 | mBiometricUnlockController.onBiometricAuthenticated(UserHandle.USER_CURRENT, |
| 169 | BiometricSourceType.FACE); |
| 170 | |
| 171 | verify(mStatusBarKeyguardViewManager).showBouncer(eq(false)); |
| 172 | } |
| 173 | |
| 174 | @Test |
| 175 | public void onBiometricAuthenticated_whenFace_noBypass_encrypted_doNothing() { |
| 176 | mBiometricUnlockController.setStatusBarKeyguardViewManager(mStatusBarKeyguardViewManager); |
| 177 | |
| 178 | when(mUpdateMonitor.isUnlockingWithBiometricAllowed()).thenReturn(false); |
| 179 | mBiometricUnlockController.onBiometricAuthenticated(UserHandle.USER_CURRENT, |
| 180 | BiometricSourceType.FACE); |
| 181 | |
| 182 | verify(mStatusBarKeyguardViewManager, never()).showBouncer(anyBoolean()); |
| 183 | verify(mStatusBarKeyguardViewManager, never()).animateCollapsePanels(anyFloat()); |
| 184 | } |
| 185 | |
| 186 | @Test |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 187 | public void onBiometricAuthenticated_whenFaceOnBouncer_dismissBouncer() { |
| 188 | when(mUpdateMonitor.isUnlockingWithBiometricAllowed()).thenReturn(true); |
Lucas Dupin | fa817a0 | 2019-07-02 15:22:54 -0700 | [diff] [blame] | 189 | when(mStatusBarKeyguardViewManager.bouncerIsOrWillBeShowing()).thenReturn(true); |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 190 | mBiometricUnlockController.onBiometricAuthenticated(UserHandle.USER_CURRENT, |
| 191 | BiometricSourceType.FACE); |
| 192 | |
| 193 | verify(mStatusBarKeyguardViewManager).notifyKeyguardAuthenticated(eq(false)); |
| 194 | } |
| 195 | |
| 196 | @Test |
Lucas Dupin | a055a67 | 2019-07-16 13:46:25 -0700 | [diff] [blame] | 197 | public void onBiometricAuthenticated_whenBypassOnBouncer_dismissBouncer() { |
| 198 | reset(mKeyguardBypassController); |
| 199 | when(mUpdateMonitor.isUnlockingWithBiometricAllowed()).thenReturn(true); |
| 200 | when(mKeyguardBypassController.getBypassEnabled()).thenReturn(true); |
| 201 | when(mKeyguardBypassController.onBiometricAuthenticated(any())).thenReturn(true); |
| 202 | when(mStatusBarKeyguardViewManager.bouncerIsOrWillBeShowing()).thenReturn(true); |
| 203 | mBiometricUnlockController.onBiometricAuthenticated(UserHandle.USER_CURRENT, |
| 204 | BiometricSourceType.FACE); |
| 205 | |
| 206 | verify(mStatusBarKeyguardViewManager).notifyKeyguardAuthenticated(eq(false)); |
| 207 | assertThat(mBiometricUnlockController.getMode()) |
| 208 | .isEqualTo(BiometricUnlockController.MODE_DISMISS_BOUNCER); |
| 209 | } |
| 210 | |
| 211 | @Test |
| 212 | public void onBiometricAuthenticated_whenBypassOnBouncer_respectsCanPlaySubtleAnim() { |
| 213 | when(mUpdateMonitor.isUnlockingWithBiometricAllowed()).thenReturn(true); |
| 214 | when(mKeyguardBypassController.getBypassEnabled()).thenReturn(true); |
| 215 | when(mStatusBarKeyguardViewManager.bouncerIsOrWillBeShowing()).thenReturn(true); |
| 216 | mBiometricUnlockController.onBiometricAuthenticated(UserHandle.USER_CURRENT, |
| 217 | BiometricSourceType.FACE); |
| 218 | |
| 219 | verify(mStatusBarKeyguardViewManager).notifyKeyguardAuthenticated(eq(false)); |
| 220 | assertThat(mBiometricUnlockController.getMode()) |
| 221 | .isEqualTo(BiometricUnlockController.MODE_UNLOCK_FADING); |
| 222 | } |
| 223 | |
| 224 | @Test |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 225 | public void onBiometricAuthenticated_whenFaceAndPulsing_dontDismissKeyguard() { |
| 226 | reset(mUpdateMonitor); |
| 227 | reset(mStatusBarKeyguardViewManager); |
| 228 | when(mUpdateMonitor.isUnlockingWithBiometricAllowed()).thenReturn(true); |
| 229 | when(mDozeScrimController.isPulsing()).thenReturn(true); |
| 230 | mBiometricUnlockController.onBiometricAuthenticated(UserHandle.USER_CURRENT, |
| 231 | BiometricSourceType.FACE); |
| 232 | |
| 233 | verify(mStatusBarKeyguardViewManager, never()).animateCollapsePanels(anyFloat()); |
| 234 | } |
Lucas Dupin | c94963f | 2019-03-07 14:54:05 -0800 | [diff] [blame] | 235 | |
Lucas Dupin | 45142ba | 2019-04-10 13:13:20 -0700 | [diff] [blame] | 236 | @Test |
| 237 | public void onFinishedGoingToSleep_authenticatesWhenPending() { |
| 238 | when(mUpdateMonitor.isGoingToSleep()).thenReturn(true); |
| 239 | mBiometricUnlockController.onFinishedGoingToSleep(-1); |
| 240 | verify(mHandler, never()).post(any()); |
| 241 | |
| 242 | mBiometricUnlockController.onBiometricAuthenticated(1 /* userId */, |
| 243 | BiometricSourceType.FACE); |
| 244 | mBiometricUnlockController.onFinishedGoingToSleep(-1); |
| 245 | verify(mHandler).post(any()); |
| 246 | } |
Lucas Dupin | 3d05353 | 2019-01-29 12:35:22 -0800 | [diff] [blame] | 247 | } |