kwaky | e31cf5d | 2020-04-14 08:00:37 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2020 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.car.keyguard; |
| 18 | |
Abhijoy Saha | 3645184 | 2020-04-23 22:41:39 +0000 | [diff] [blame] | 19 | import static org.mockito.ArgumentMatchers.any; |
kwaky | e31cf5d | 2020-04-14 08:00:37 -0700 | [diff] [blame] | 20 | import static org.mockito.ArgumentMatchers.anyBoolean; |
Abhijoy Saha | 3645184 | 2020-04-23 22:41:39 +0000 | [diff] [blame] | 21 | import static org.mockito.ArgumentMatchers.eq; |
| 22 | import static org.mockito.Mockito.inOrder; |
kwaky | e31cf5d | 2020-04-14 08:00:37 -0700 | [diff] [blame] | 23 | import static org.mockito.Mockito.mock; |
| 24 | import static org.mockito.Mockito.never; |
| 25 | import static org.mockito.Mockito.verify; |
| 26 | import static org.mockito.Mockito.when; |
| 27 | |
| 28 | import android.content.Context; |
| 29 | import android.os.Handler; |
| 30 | import android.testing.AndroidTestingRunner; |
| 31 | import android.testing.TestableLooper; |
| 32 | import android.view.LayoutInflater; |
kwaky | e31cf5d | 2020-04-14 08:00:37 -0700 | [diff] [blame] | 33 | import android.view.ViewGroup; |
| 34 | |
JianYang Liu | 55eb9c0 | 2020-05-08 13:52:08 -0700 | [diff] [blame] | 35 | import androidx.test.filters.SmallTest; |
| 36 | |
kwaky | e31cf5d | 2020-04-14 08:00:37 -0700 | [diff] [blame] | 37 | import com.android.internal.widget.LockPatternUtils; |
| 38 | import com.android.keyguard.KeyguardUpdateMonitor; |
| 39 | import com.android.keyguard.ViewMediatorCallback; |
| 40 | import com.android.systemui.R; |
| 41 | import com.android.systemui.SysuiTestCase; |
| 42 | import com.android.systemui.car.CarServiceProvider; |
Heemin Seog | 9b078d3 | 2020-04-21 15:07:32 -0700 | [diff] [blame] | 43 | import com.android.systemui.car.navigationbar.CarNavigationBarController; |
| 44 | import com.android.systemui.car.window.OverlayViewGlobalStateController; |
kwaky | e31cf5d | 2020-04-14 08:00:37 -0700 | [diff] [blame] | 45 | import com.android.systemui.keyguard.DismissCallbackRegistry; |
kwaky | e31cf5d | 2020-04-14 08:00:37 -0700 | [diff] [blame] | 46 | import com.android.systemui.plugins.FalsingManager; |
| 47 | import com.android.systemui.statusbar.phone.BiometricUnlockController; |
| 48 | import com.android.systemui.statusbar.phone.KeyguardBouncer; |
| 49 | import com.android.systemui.statusbar.phone.KeyguardBypassController; |
| 50 | import com.android.systemui.statusbar.policy.KeyguardStateController; |
kwaky | e31cf5d | 2020-04-14 08:00:37 -0700 | [diff] [blame] | 51 | |
| 52 | import org.junit.Before; |
| 53 | import org.junit.Test; |
| 54 | import org.junit.runner.RunWith; |
Abhijoy Saha | 3645184 | 2020-04-23 22:41:39 +0000 | [diff] [blame] | 55 | import org.mockito.InOrder; |
kwaky | e31cf5d | 2020-04-14 08:00:37 -0700 | [diff] [blame] | 56 | import org.mockito.Mock; |
| 57 | import org.mockito.MockitoAnnotations; |
| 58 | |
Heemin Seog | 09970f4 | 2020-04-27 22:01:05 -0700 | [diff] [blame] | 59 | import dagger.Lazy; |
| 60 | |
kwaky | e31cf5d | 2020-04-14 08:00:37 -0700 | [diff] [blame] | 61 | @RunWith(AndroidTestingRunner.class) |
| 62 | @TestableLooper.RunWithLooper |
JianYang Liu | 55eb9c0 | 2020-05-08 13:52:08 -0700 | [diff] [blame] | 63 | @SmallTest |
kwaky | e31cf5d | 2020-04-14 08:00:37 -0700 | [diff] [blame] | 64 | public class CarKeyguardViewControllerTest extends SysuiTestCase { |
| 65 | |
| 66 | private TestableCarKeyguardViewController mCarKeyguardViewController; |
kwaky | e31cf5d | 2020-04-14 08:00:37 -0700 | [diff] [blame] | 67 | |
| 68 | @Mock |
Abhijoy Saha | 3645184 | 2020-04-23 22:41:39 +0000 | [diff] [blame] | 69 | private OverlayViewGlobalStateController mOverlayViewGlobalStateController; |
| 70 | @Mock |
kwaky | e31cf5d | 2020-04-14 08:00:37 -0700 | [diff] [blame] | 71 | private KeyguardBouncer mBouncer; |
| 72 | @Mock |
| 73 | private CarNavigationBarController mCarNavigationBarController; |
| 74 | @Mock |
kwaky | e31cf5d | 2020-04-14 08:00:37 -0700 | [diff] [blame] | 75 | private CarKeyguardViewController.OnKeyguardCancelClickedListener mCancelClickedListener; |
| 76 | |
| 77 | @Before |
| 78 | public void setUp() { |
| 79 | MockitoAnnotations.initMocks(this); |
| 80 | |
kwaky | e31cf5d | 2020-04-14 08:00:37 -0700 | [diff] [blame] | 81 | mCarKeyguardViewController = new TestableCarKeyguardViewController( |
| 82 | mContext, |
| 83 | Handler.getMain(), |
| 84 | mock(CarServiceProvider.class), |
| 85 | mOverlayViewGlobalStateController, |
| 86 | mock(KeyguardStateController.class), |
| 87 | mock(KeyguardUpdateMonitor.class), |
Heemin Seog | 09970f4 | 2020-04-27 22:01:05 -0700 | [diff] [blame] | 88 | () -> mock(BiometricUnlockController.class), |
kwaky | e31cf5d | 2020-04-14 08:00:37 -0700 | [diff] [blame] | 89 | mock(ViewMediatorCallback.class), |
| 90 | mock(CarNavigationBarController.class), |
| 91 | mock(LockPatternUtils.class), |
| 92 | mock(DismissCallbackRegistry.class), |
| 93 | mock(FalsingManager.class), |
Heemin Seog | 09970f4 | 2020-04-27 22:01:05 -0700 | [diff] [blame] | 94 | () -> mock(KeyguardBypassController.class) |
kwaky | e31cf5d | 2020-04-14 08:00:37 -0700 | [diff] [blame] | 95 | ); |
Abhijoy Saha | 3645184 | 2020-04-23 22:41:39 +0000 | [diff] [blame] | 96 | mCarKeyguardViewController.inflate((ViewGroup) LayoutInflater.from(mContext).inflate( |
| 97 | R.layout.sysui_overlay_window, /* root= */ null)); |
kwaky | e31cf5d | 2020-04-14 08:00:37 -0700 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | @Test |
| 101 | public void onShow_bouncerIsSecure_showsBouncerWithSecuritySelectionReset() { |
| 102 | when(mBouncer.isSecure()).thenReturn(true); |
| 103 | mCarKeyguardViewController.show(/* options= */ null); |
| 104 | |
| 105 | verify(mBouncer).show(/* resetSecuritySelection= */ true); |
| 106 | } |
| 107 | |
| 108 | @Test |
| 109 | public void onShow_bouncerIsSecure_keyguardIsVisible() { |
| 110 | when(mBouncer.isSecure()).thenReturn(true); |
| 111 | mCarKeyguardViewController.show(/* options= */ null); |
| 112 | |
Abhijoy Saha | 3645184 | 2020-04-23 22:41:39 +0000 | [diff] [blame] | 113 | verify(mOverlayViewGlobalStateController).showView(eq(mCarKeyguardViewController), any()); |
kwaky | e31cf5d | 2020-04-14 08:00:37 -0700 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | @Test |
| 117 | public void onShow_bouncerNotSecure_hidesBouncerAndDestroysTheView() { |
| 118 | when(mBouncer.isSecure()).thenReturn(false); |
| 119 | mCarKeyguardViewController.show(/* options= */ null); |
| 120 | |
| 121 | verify(mBouncer).hide(/* destroyView= */ true); |
| 122 | } |
| 123 | |
| 124 | @Test |
| 125 | public void onShow_bouncerNotSecure_keyguardIsNotVisible() { |
| 126 | when(mBouncer.isSecure()).thenReturn(false); |
| 127 | mCarKeyguardViewController.show(/* options= */ null); |
| 128 | |
Abhijoy Saha | 3645184 | 2020-04-23 22:41:39 +0000 | [diff] [blame] | 129 | // Here we check for both showView and hideView since the current implementation of show |
| 130 | // with bouncer being not secure has the following method execution orders: |
| 131 | // 1) show -> start -> showView |
| 132 | // 2) show -> reset -> dismissAndCollapse -> hide -> stop -> hideView |
| 133 | // Hence, we want to make sure that showView is called before hideView and not in any |
| 134 | // other combination. |
| 135 | InOrder inOrder = inOrder(mOverlayViewGlobalStateController); |
| 136 | inOrder.verify(mOverlayViewGlobalStateController).showView(eq(mCarKeyguardViewController), |
| 137 | any()); |
| 138 | inOrder.verify(mOverlayViewGlobalStateController).hideView(eq(mCarKeyguardViewController), |
| 139 | any()); |
kwaky | e31cf5d | 2020-04-14 08:00:37 -0700 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | @Test |
| 143 | public void onHide_keyguardShowing_hidesBouncerAndDestroysTheView() { |
| 144 | when(mBouncer.isSecure()).thenReturn(true); |
| 145 | mCarKeyguardViewController.show(/* options= */ null); |
| 146 | mCarKeyguardViewController.hide(/* startTime= */ 0, /* fadeoutDelay= */ 0); |
| 147 | |
| 148 | verify(mBouncer).hide(/* destroyView= */ true); |
| 149 | } |
| 150 | |
| 151 | @Test |
| 152 | public void onHide_keyguardNotShown_doesNotHideOrDestroyBouncer() { |
| 153 | mCarKeyguardViewController.hide(/* startTime= */ 0, /* fadeoutDelay= */ 0); |
| 154 | |
| 155 | verify(mBouncer, never()).hide(anyBoolean()); |
| 156 | } |
| 157 | |
| 158 | @Test |
| 159 | public void onHide_KeyguardNotVisible() { |
| 160 | when(mBouncer.isSecure()).thenReturn(true); |
| 161 | mCarKeyguardViewController.show(/* options= */ null); |
| 162 | mCarKeyguardViewController.hide(/* startTime= */ 0, /* fadeoutDelay= */ 0); |
| 163 | |
Abhijoy Saha | 3645184 | 2020-04-23 22:41:39 +0000 | [diff] [blame] | 164 | InOrder inOrder = inOrder(mOverlayViewGlobalStateController); |
| 165 | inOrder.verify(mOverlayViewGlobalStateController).showView(eq(mCarKeyguardViewController), |
| 166 | any()); |
| 167 | inOrder.verify(mOverlayViewGlobalStateController).hideView(eq(mCarKeyguardViewController), |
| 168 | any()); |
kwaky | e31cf5d | 2020-04-14 08:00:37 -0700 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | @Test |
| 172 | public void onCancelClicked_callsCancelClickedListener() { |
| 173 | when(mBouncer.isSecure()).thenReturn(true); |
| 174 | mCarKeyguardViewController.show(/* options= */ null); |
| 175 | mCarKeyguardViewController.registerOnKeyguardCancelClickedListener(mCancelClickedListener); |
| 176 | mCarKeyguardViewController.onCancelClicked(); |
| 177 | |
| 178 | verify(mCancelClickedListener).onCancelClicked(); |
| 179 | } |
| 180 | |
| 181 | @Test |
| 182 | public void onCancelClicked_hidesBouncerAndDestroysTheView() { |
| 183 | when(mBouncer.isSecure()).thenReturn(true); |
| 184 | mCarKeyguardViewController.show(/* options= */ null); |
| 185 | mCarKeyguardViewController.registerOnKeyguardCancelClickedListener(mCancelClickedListener); |
| 186 | mCarKeyguardViewController.onCancelClicked(); |
| 187 | |
| 188 | verify(mBouncer).hide(/* destroyView= */ true); |
| 189 | } |
| 190 | |
| 191 | private class TestableCarKeyguardViewController extends CarKeyguardViewController { |
| 192 | |
| 193 | TestableCarKeyguardViewController(Context context, |
| 194 | Handler mainHandler, |
| 195 | CarServiceProvider carServiceProvider, |
| 196 | OverlayViewGlobalStateController overlayViewGlobalStateController, |
| 197 | KeyguardStateController keyguardStateController, |
| 198 | KeyguardUpdateMonitor keyguardUpdateMonitor, |
Heemin Seog | 09970f4 | 2020-04-27 22:01:05 -0700 | [diff] [blame] | 199 | Lazy<BiometricUnlockController> biometricUnlockControllerLazy, |
kwaky | e31cf5d | 2020-04-14 08:00:37 -0700 | [diff] [blame] | 200 | ViewMediatorCallback viewMediatorCallback, |
| 201 | CarNavigationBarController carNavigationBarController, |
| 202 | LockPatternUtils lockPatternUtils, |
| 203 | DismissCallbackRegistry dismissCallbackRegistry, |
| 204 | FalsingManager falsingManager, |
Heemin Seog | 09970f4 | 2020-04-27 22:01:05 -0700 | [diff] [blame] | 205 | Lazy<KeyguardBypassController> keyguardBypassControllerLazy) { |
kwaky | e31cf5d | 2020-04-14 08:00:37 -0700 | [diff] [blame] | 206 | super(context, mainHandler, carServiceProvider, overlayViewGlobalStateController, |
Heemin Seog | 09970f4 | 2020-04-27 22:01:05 -0700 | [diff] [blame] | 207 | keyguardStateController, keyguardUpdateMonitor, biometricUnlockControllerLazy, |
kwaky | e31cf5d | 2020-04-14 08:00:37 -0700 | [diff] [blame] | 208 | viewMediatorCallback, carNavigationBarController, lockPatternUtils, |
Heemin Seog | 09970f4 | 2020-04-27 22:01:05 -0700 | [diff] [blame] | 209 | dismissCallbackRegistry, falsingManager, keyguardBypassControllerLazy); |
kwaky | e31cf5d | 2020-04-14 08:00:37 -0700 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | @Override |
| 213 | public void onFinishInflate() { |
| 214 | super.onFinishInflate(); |
| 215 | setKeyguardBouncer(CarKeyguardViewControllerTest.this.mBouncer); |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | } |