blob: 38836d85e8d47969d82e3a915a26bb0ce9c9abae [file] [log] [blame]
kwakye31cf5d2020-04-14 08:00:37 -07001/*
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
17package com.android.systemui.car.keyguard;
18
Abhijoy Saha36451842020-04-23 22:41:39 +000019import static org.mockito.ArgumentMatchers.any;
kwakye31cf5d2020-04-14 08:00:37 -070020import static org.mockito.ArgumentMatchers.anyBoolean;
Abhijoy Saha36451842020-04-23 22:41:39 +000021import static org.mockito.ArgumentMatchers.eq;
22import static org.mockito.Mockito.inOrder;
kwakye31cf5d2020-04-14 08:00:37 -070023import static org.mockito.Mockito.mock;
24import static org.mockito.Mockito.never;
25import static org.mockito.Mockito.verify;
26import static org.mockito.Mockito.when;
27
28import android.content.Context;
29import android.os.Handler;
30import android.testing.AndroidTestingRunner;
31import android.testing.TestableLooper;
32import android.view.LayoutInflater;
kwakye31cf5d2020-04-14 08:00:37 -070033import android.view.ViewGroup;
34
JianYang Liu55eb9c02020-05-08 13:52:08 -070035import androidx.test.filters.SmallTest;
36
kwakye31cf5d2020-04-14 08:00:37 -070037import com.android.internal.widget.LockPatternUtils;
38import com.android.keyguard.KeyguardUpdateMonitor;
39import com.android.keyguard.ViewMediatorCallback;
40import com.android.systemui.R;
41import com.android.systemui.SysuiTestCase;
42import com.android.systemui.car.CarServiceProvider;
Heemin Seog9b078d32020-04-21 15:07:32 -070043import com.android.systemui.car.navigationbar.CarNavigationBarController;
44import com.android.systemui.car.window.OverlayViewGlobalStateController;
kwakye31cf5d2020-04-14 08:00:37 -070045import com.android.systemui.keyguard.DismissCallbackRegistry;
kwakye31cf5d2020-04-14 08:00:37 -070046import com.android.systemui.plugins.FalsingManager;
47import com.android.systemui.statusbar.phone.BiometricUnlockController;
48import com.android.systemui.statusbar.phone.KeyguardBouncer;
49import com.android.systemui.statusbar.phone.KeyguardBypassController;
50import com.android.systemui.statusbar.policy.KeyguardStateController;
kwakye31cf5d2020-04-14 08:00:37 -070051
52import org.junit.Before;
53import org.junit.Test;
54import org.junit.runner.RunWith;
Abhijoy Saha36451842020-04-23 22:41:39 +000055import org.mockito.InOrder;
kwakye31cf5d2020-04-14 08:00:37 -070056import org.mockito.Mock;
57import org.mockito.MockitoAnnotations;
58
Heemin Seog09970f42020-04-27 22:01:05 -070059import dagger.Lazy;
60
kwakye31cf5d2020-04-14 08:00:37 -070061@RunWith(AndroidTestingRunner.class)
62@TestableLooper.RunWithLooper
JianYang Liu55eb9c02020-05-08 13:52:08 -070063@SmallTest
kwakye31cf5d2020-04-14 08:00:37 -070064public class CarKeyguardViewControllerTest extends SysuiTestCase {
65
66 private TestableCarKeyguardViewController mCarKeyguardViewController;
kwakye31cf5d2020-04-14 08:00:37 -070067
68 @Mock
Abhijoy Saha36451842020-04-23 22:41:39 +000069 private OverlayViewGlobalStateController mOverlayViewGlobalStateController;
70 @Mock
kwakye31cf5d2020-04-14 08:00:37 -070071 private KeyguardBouncer mBouncer;
72 @Mock
73 private CarNavigationBarController mCarNavigationBarController;
74 @Mock
kwakye31cf5d2020-04-14 08:00:37 -070075 private CarKeyguardViewController.OnKeyguardCancelClickedListener mCancelClickedListener;
76
77 @Before
78 public void setUp() {
79 MockitoAnnotations.initMocks(this);
80
kwakye31cf5d2020-04-14 08:00:37 -070081 mCarKeyguardViewController = new TestableCarKeyguardViewController(
82 mContext,
83 Handler.getMain(),
84 mock(CarServiceProvider.class),
85 mOverlayViewGlobalStateController,
86 mock(KeyguardStateController.class),
87 mock(KeyguardUpdateMonitor.class),
Heemin Seog09970f42020-04-27 22:01:05 -070088 () -> mock(BiometricUnlockController.class),
kwakye31cf5d2020-04-14 08:00:37 -070089 mock(ViewMediatorCallback.class),
90 mock(CarNavigationBarController.class),
91 mock(LockPatternUtils.class),
92 mock(DismissCallbackRegistry.class),
93 mock(FalsingManager.class),
Heemin Seog09970f42020-04-27 22:01:05 -070094 () -> mock(KeyguardBypassController.class)
kwakye31cf5d2020-04-14 08:00:37 -070095 );
Abhijoy Saha36451842020-04-23 22:41:39 +000096 mCarKeyguardViewController.inflate((ViewGroup) LayoutInflater.from(mContext).inflate(
97 R.layout.sysui_overlay_window, /* root= */ null));
kwakye31cf5d2020-04-14 08:00:37 -070098 }
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 Saha36451842020-04-23 22:41:39 +0000113 verify(mOverlayViewGlobalStateController).showView(eq(mCarKeyguardViewController), any());
kwakye31cf5d2020-04-14 08:00:37 -0700114 }
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 Saha36451842020-04-23 22:41:39 +0000129 // 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());
kwakye31cf5d2020-04-14 08:00:37 -0700140 }
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 Saha36451842020-04-23 22:41:39 +0000164 InOrder inOrder = inOrder(mOverlayViewGlobalStateController);
165 inOrder.verify(mOverlayViewGlobalStateController).showView(eq(mCarKeyguardViewController),
166 any());
167 inOrder.verify(mOverlayViewGlobalStateController).hideView(eq(mCarKeyguardViewController),
168 any());
kwakye31cf5d2020-04-14 08:00:37 -0700169 }
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 Seog09970f42020-04-27 22:01:05 -0700199 Lazy<BiometricUnlockController> biometricUnlockControllerLazy,
kwakye31cf5d2020-04-14 08:00:37 -0700200 ViewMediatorCallback viewMediatorCallback,
201 CarNavigationBarController carNavigationBarController,
202 LockPatternUtils lockPatternUtils,
203 DismissCallbackRegistry dismissCallbackRegistry,
204 FalsingManager falsingManager,
Heemin Seog09970f42020-04-27 22:01:05 -0700205 Lazy<KeyguardBypassController> keyguardBypassControllerLazy) {
kwakye31cf5d2020-04-14 08:00:37 -0700206 super(context, mainHandler, carServiceProvider, overlayViewGlobalStateController,
Heemin Seog09970f42020-04-27 22:01:05 -0700207 keyguardStateController, keyguardUpdateMonitor, biometricUnlockControllerLazy,
kwakye31cf5d2020-04-14 08:00:37 -0700208 viewMediatorCallback, carNavigationBarController, lockPatternUtils,
Heemin Seog09970f42020-04-27 22:01:05 -0700209 dismissCallbackRegistry, falsingManager, keyguardBypassControllerLazy);
kwakye31cf5d2020-04-14 08:00:37 -0700210 }
211
212 @Override
213 public void onFinishInflate() {
214 super.onFinishInflate();
215 setKeyguardBouncer(CarKeyguardViewControllerTest.this.mBouncer);
216 }
217 }
218
219}