summaryrefslogtreecommitdiff
path: root/tests/src/com/android/systemui/car/keyguard/CarKeyguardViewControllerTest.java
blob: b32be3872442c6bb84ef7232b55cb189fc71b95c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/*
 * Copyright (C) 2020 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.systemui.car.keyguard;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.inOrder;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.reset;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import android.testing.AndroidTestingRunner;
import android.testing.TestableLooper;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.FrameLayout;

import androidx.test.filters.SmallTest;

import com.android.keyguard.KeyguardMessageAreaController;
import com.android.keyguard.KeyguardSecurityContainerController;
import com.android.keyguard.KeyguardSecurityModel;
import com.android.keyguard.KeyguardUpdateMonitor;
import com.android.keyguard.ViewMediatorCallback;
import com.android.keyguard.dagger.KeyguardBouncerComponent;
import com.android.systemui.R;
import com.android.systemui.SysuiTestCase;

import com.android.systemui.bouncer.domain.interactor.BouncerMessageInteractor;
import com.android.systemui.bouncer.domain.interactor.PrimaryBouncerCallbackInteractor;
import com.android.systemui.bouncer.domain.interactor.PrimaryBouncerInteractor;
import com.android.systemui.bouncer.ui.BouncerView;
import com.android.systemui.bouncer.ui.viewmodel.KeyguardBouncerViewModel;
import com.android.systemui.car.CarSystemUiTest;
import com.android.systemui.car.systembar.CarSystemBarController;
import com.android.systemui.car.window.OverlayViewGlobalStateController;
import com.android.systemui.car.window.SystemUIOverlayWindowController;
import com.android.systemui.flags.FakeFeatureFlags;
import com.android.systemui.flags.Flags;
import com.android.systemui.keyguard.ui.viewmodel.PrimaryBouncerToGoneTransitionViewModel;
import com.android.systemui.log.BouncerLogger;
import com.android.systemui.settings.UserTracker;
import com.android.systemui.statusbar.phone.BiometricUnlockController;
import com.android.systemui.statusbar.policy.KeyguardStateController;
import com.android.systemui.toast.ToastFactory;
import com.android.systemui.util.concurrency.FakeExecutor;
import com.android.systemui.util.time.FakeSystemClock;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InOrder;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;

@CarSystemUiTest
@RunWith(AndroidTestingRunner.class)
@TestableLooper.RunWithLooper(setAsMainLooper = true)
@SmallTest
public class CarKeyguardViewControllerTest extends SysuiTestCase {

    private CarKeyguardViewController mCarKeyguardViewController;
    private FakeExecutor mExecutor;

    @Mock
    private UserTracker mUserTracker;
    @Mock
    private OverlayViewGlobalStateController mOverlayViewGlobalStateController;
    @Mock
    private SystemUIOverlayWindowController mSystemUIOverlayWindowController;
    @Mock
    private CarKeyguardViewController.OnKeyguardCancelClickedListener mCancelClickedListener;
    @Mock
    private PrimaryBouncerCallbackInteractor mPrimaryBouncerCallbackInteractor;
    @Mock
    private PrimaryBouncerInteractor mPrimaryBouncerInteractor;
    @Mock
    private KeyguardSecurityModel mKeyguardSecurityModel;
    @Mock
    private KeyguardBouncerViewModel mKeyguardBouncerViewModel;
    @Mock
    private KeyguardBouncerComponent.Factory mKeyguardBouncerComponentFactory;
    @Mock
    private PrimaryBouncerToGoneTransitionViewModel mPrimaryBouncerToGoneTransitionViewModel;
    @Mock
    private BouncerView mBouncerView;

    @Before
    public void setUp() {
        MockitoAnnotations.initMocks(this);

        ViewGroup mockBaseLayout = new FrameLayout(mContext);

        when(mSystemUIOverlayWindowController.getBaseLayout()).thenReturn(mockBaseLayout);
        mExecutor = new FakeExecutor(new FakeSystemClock());

        KeyguardBouncerComponent keyguardBouncerComponent = mock(KeyguardBouncerComponent.class);
        KeyguardSecurityContainerController securityContainerController = mock(
                KeyguardSecurityContainerController.class);
        when(mKeyguardBouncerComponentFactory.create(any(ViewGroup.class))).thenReturn(
                keyguardBouncerComponent);
        when(keyguardBouncerComponent.getSecurityContainerController()).thenReturn(
                securityContainerController);

        FakeFeatureFlags fakeFeatureFlags = new FakeFeatureFlags();
        fakeFeatureFlags.set(Flags.REVAMPED_BOUNCER_MESSAGES, true);
        mCarKeyguardViewController = new CarKeyguardViewController(
                mContext,
                mUserTracker,
                mExecutor,
                mock(WindowManager.class),
                mock(ToastFactory.class),
                mSystemUIOverlayWindowController,
                mOverlayViewGlobalStateController,
                mock(KeyguardStateController.class),
                mock(KeyguardUpdateMonitor.class),
                () -> mock(BiometricUnlockController.class),
                mock(ViewMediatorCallback.class),
                mock(CarSystemBarController.class),
                mPrimaryBouncerCallbackInteractor,
                mPrimaryBouncerInteractor,
                mKeyguardSecurityModel,
                mKeyguardBouncerViewModel,
                mPrimaryBouncerToGoneTransitionViewModel,
                mKeyguardBouncerComponentFactory,
                mBouncerView,
                mock(KeyguardMessageAreaController.Factory.class),
                mock(BouncerLogger.class),
                fakeFeatureFlags,
                mock(BouncerMessageInteractor.class)
        );
        mCarKeyguardViewController.inflate((ViewGroup) LayoutInflater.from(mContext).inflate(
                R.layout.sysui_overlay_window, /* root= */ null));
    }

    @Test
    public void onShow_bouncerIsSecure_showsBouncerWithSecuritySelectionReset() {
        setIsSecure(true);
        mCarKeyguardViewController.show(/* options= */ null);
        waitForDelayableExecutor();

        verify(mPrimaryBouncerInteractor).show(/* isScrimmed= */ true);
    }

    @Test
    public void onShow_bouncerIsSecure_keyguardIsVisible() {
        setIsSecure(true);
        mCarKeyguardViewController.show(/* options= */ null);

        verify(mOverlayViewGlobalStateController).showView(eq(mCarKeyguardViewController), any());
    }

    @Test
    public void onShow_bouncerNotSecure_hidesBouncerAndDestroysTheView() {
        setIsSecure(false);
        mCarKeyguardViewController.show(/* options= */ null);
        waitForDelayableExecutor();

        verify(mPrimaryBouncerInteractor, Mockito.times(2)).hide();
    }

    @Test
    public void onShow_bouncerNotSecure_keyguardIsNotVisible() {
        setIsSecure(false);
        mCarKeyguardViewController.show(/* options= */ null);
        waitForDelayableExecutor();

        // Here we check for both showView and hideView since the current implementation of show
        // with bouncer being not secure has the following method execution orders:
        // 1) show -> start -> showView
        // 2) show -> reset -> dismissAndCollapse -> hide -> stop -> hideView
        // Hence, we want to make sure that showView is called before hideView and not in any
        // other combination.
        InOrder inOrder = inOrder(mOverlayViewGlobalStateController);
        inOrder.verify(mOverlayViewGlobalStateController).showView(eq(mCarKeyguardViewController),
                any());
        inOrder.verify(mOverlayViewGlobalStateController).hideView(eq(mCarKeyguardViewController),
                any());
    }

    @Test
    public void onHide_keyguardShowing_hidesBouncerAndDestroysTheView() {
        setIsSecure(true);
        mCarKeyguardViewController.show(/* options= */ null);
        mCarKeyguardViewController.hide(/* startTime= */ 0, /* fadeoutDelay= */ 0);

        verify(mPrimaryBouncerInteractor).hide();
    }

    @Test
    public void onHide_keyguardNotShown_doesNotHideOrDestroyBouncer() {
        mCarKeyguardViewController.hide(/* startTime= */ 0, /* fadeoutDelay= */ 0);

        verify(mPrimaryBouncerInteractor, never()).hide();
    }

    @Test
    public void onHide_KeyguardNotVisible() {
        setIsSecure(true);
        mCarKeyguardViewController.show(/* options= */ null);
        mCarKeyguardViewController.hide(/* startTime= */ 0, /* fadeoutDelay= */ 0);

        InOrder inOrder = inOrder(mOverlayViewGlobalStateController);
        inOrder.verify(mOverlayViewGlobalStateController).showView(eq(mCarKeyguardViewController),
                any());
        inOrder.verify(mOverlayViewGlobalStateController).hideView(eq(mCarKeyguardViewController),
                any());
    }

    @Test
    public void setOccludedFalse_currentlyOccluded_showsKeyguard() {
        setIsSecure(true);
        mCarKeyguardViewController.show(/* options= */ null);
        mCarKeyguardViewController.setOccluded(/* occluded= */ true, /* animate= */ false);
        reset(mPrimaryBouncerInteractor);

        mCarKeyguardViewController.setOccluded(/* occluded= */ false, /* animate= */ false);
        waitForDelayableExecutor();

        verify(mPrimaryBouncerInteractor).show(/* isScrimmed= */ true);
    }

    @Test
    public void onCancelClicked_callsCancelClickedListener() {
        setIsSecure(true);
        mCarKeyguardViewController.show(/* options= */ null);
        mCarKeyguardViewController.registerOnKeyguardCancelClickedListener(mCancelClickedListener);
        mCarKeyguardViewController.onCancelClicked();

        verify(mCancelClickedListener).onCancelClicked();
    }

    @Test
    public void onEnterSleepModeAndThenShowKeyguard_bouncerNotSecure_keyguardIsVisible() {
        setIsSecure(false);
        mCarKeyguardViewController.onStartedGoingToSleep();
        mCarKeyguardViewController.show(/* options= */ null);
        waitForDelayableExecutor();

        // We want to make sure that showView is called beforehand and hideView is never called
        // so that the Keyguard is visible as a result.
        InOrder inOrder = inOrder(mOverlayViewGlobalStateController);
        inOrder.verify(mOverlayViewGlobalStateController).showView(eq(mCarKeyguardViewController),
                any());
        inOrder.verify(mOverlayViewGlobalStateController, never()).hideView(
                eq(mCarKeyguardViewController), any());
    }

    @Test
    public void onFinishedGoingToSleep() {
        mCarKeyguardViewController.onFinishedGoingToSleep();
        verify(mPrimaryBouncerInteractor).hide();
    }

    @Test
    public void onDeviceWakeUpWhileKeyguardShown_bouncerNotSecure_keyguardIsNotVisible() {
        setIsSecure(false);
        mCarKeyguardViewController.onStartedGoingToSleep();
        mCarKeyguardViewController.show(/* options= */ null);
        mCarKeyguardViewController.onStartedWakingUp();
        waitForDelayableExecutor();

        // We want to make sure that showView is called beforehand and then hideView is called so
        // that the Keyguard is invisible as a result.
        InOrder inOrder = inOrder(mOverlayViewGlobalStateController);
        inOrder.verify(mOverlayViewGlobalStateController).showView(eq(mCarKeyguardViewController),
                any());
        inOrder.verify(mOverlayViewGlobalStateController).hideView(eq(mCarKeyguardViewController),
                any());
    }

    @Test
    public void onCancelClicked_hidesBouncerAndDestroysTheView() {
        setIsSecure(true);
        mCarKeyguardViewController.show(/* options= */ null);
        mCarKeyguardViewController.registerOnKeyguardCancelClickedListener(mCancelClickedListener);
        mCarKeyguardViewController.onCancelClicked();

        verify(mPrimaryBouncerInteractor).hide();
    }

    private void waitForDelayableExecutor() {
        mExecutor.advanceClockToLast();
        mExecutor.runAllReady();
    }

    private void setIsSecure(boolean isSecure) {
        when(mKeyguardSecurityModel.getSecurityMode(anyInt())).thenReturn(
                isSecure ? KeyguardSecurityModel.SecurityMode.PIN
                        : KeyguardSecurityModel.SecurityMode.None);
    }
}