summaryrefslogtreecommitdiff
path: root/packages/SystemUI/tests/src/com/android/systemui/keyguard/domain/interactor/PrimaryBouncerInteractorTest.kt
blob: 9153d6ef138f2217509c9bd3212a81856eadb7bf (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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
/*
 * Copyright (C) 2022 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.keyguard.domain.interactor

import android.hardware.biometrics.BiometricSourceType
import android.testing.AndroidTestingRunner
import android.testing.TestableLooper.RunWithLooper
import android.testing.TestableResources
import android.view.View
import androidx.test.filters.SmallTest
import com.android.keyguard.KeyguardSecurityModel
import com.android.keyguard.KeyguardUpdateMonitor
import com.android.systemui.DejankUtils
import com.android.systemui.R
import com.android.systemui.SysuiTestCase
import com.android.systemui.classifier.FalsingCollector
import com.android.systemui.flags.FakeFeatureFlags
import com.android.systemui.flags.Flags
import com.android.systemui.keyguard.DismissCallbackRegistry
import com.android.systemui.keyguard.data.BouncerView
import com.android.systemui.keyguard.data.BouncerViewDelegate
import com.android.systemui.keyguard.data.repository.FakeTrustRepository
import com.android.systemui.keyguard.data.repository.KeyguardBouncerRepository
import com.android.systemui.keyguard.shared.constants.KeyguardBouncerConstants.EXPANSION_HIDDEN
import com.android.systemui.keyguard.shared.constants.KeyguardBouncerConstants.EXPANSION_VISIBLE
import com.android.systemui.keyguard.shared.model.BouncerShowMessageModel
import com.android.systemui.plugins.ActivityStarter
import com.android.systemui.statusbar.policy.KeyguardStateController
import com.android.systemui.util.mockito.any
import com.android.systemui.util.mockito.whenever
import com.android.systemui.utils.os.FakeHandler
import com.google.common.truth.Truth.assertThat
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.Answers
import org.mockito.ArgumentCaptor
import org.mockito.Mock
import org.mockito.Mockito.anyInt
import org.mockito.Mockito.mock
import org.mockito.Mockito.never
import org.mockito.Mockito.verify
import org.mockito.MockitoAnnotations

@SmallTest
@RunWithLooper(setAsMainLooper = true)
@RunWith(AndroidTestingRunner::class)
class PrimaryBouncerInteractorTest : SysuiTestCase() {
    @Mock(answer = Answers.RETURNS_DEEP_STUBS)
    private lateinit var repository: KeyguardBouncerRepository
    @Mock(answer = Answers.RETURNS_DEEP_STUBS) private lateinit var bouncerView: BouncerView
    @Mock private lateinit var bouncerViewDelegate: BouncerViewDelegate
    @Mock private lateinit var keyguardStateController: KeyguardStateController
    @Mock private lateinit var keyguardSecurityModel: KeyguardSecurityModel
    @Mock private lateinit var mPrimaryBouncerCallbackInteractor: PrimaryBouncerCallbackInteractor
    @Mock private lateinit var falsingCollector: FalsingCollector
    @Mock private lateinit var dismissCallbackRegistry: DismissCallbackRegistry
    @Mock private lateinit var keyguardUpdateMonitor: KeyguardUpdateMonitor
    private lateinit var mainHandler: FakeHandler
    private lateinit var underTest: PrimaryBouncerInteractor
    private lateinit var resources: TestableResources
    private lateinit var trustRepository: FakeTrustRepository
    private lateinit var featureFlags: FakeFeatureFlags

    @Before
    fun setUp() {
        MockitoAnnotations.initMocks(this)
        whenever(keyguardSecurityModel.getSecurityMode(anyInt()))
            .thenReturn(KeyguardSecurityModel.SecurityMode.PIN)

        DejankUtils.setImmediate(true)
        mainHandler = FakeHandler(android.os.Looper.getMainLooper())
        trustRepository = FakeTrustRepository()
        featureFlags = FakeFeatureFlags().apply { set(Flags.DELAY_BOUNCER, false) }
        underTest =
            PrimaryBouncerInteractor(
                repository,
                bouncerView,
                mainHandler,
                keyguardStateController,
                keyguardSecurityModel,
                mPrimaryBouncerCallbackInteractor,
                falsingCollector,
                dismissCallbackRegistry,
                context,
                keyguardUpdateMonitor,
                trustRepository,
                featureFlags,
            )
        whenever(repository.primaryBouncerStartingDisappearAnimation.value).thenReturn(null)
        whenever(repository.primaryBouncerShow.value).thenReturn(false)
        whenever(bouncerView.delegate).thenReturn(bouncerViewDelegate)
        resources = context.orCreateTestableResources
    }

    @Test
    fun testShow_isScrimmed() {
        underTest.show(true)
        verify(repository).setKeyguardAuthenticated(null)
        verify(repository).setPrimaryStartingToHide(false)
        verify(repository).setPrimaryScrimmed(true)
        verify(repository).setPanelExpansion(EXPANSION_VISIBLE)
        verify(repository).setPrimaryShowingSoon(true)
        verify(keyguardStateController).notifyPrimaryBouncerShowing(true)
        verify(mPrimaryBouncerCallbackInteractor).dispatchStartingToShow()
        verify(repository).setPrimaryShow(true)
        verify(repository).setPrimaryShowingSoon(false)
        verify(mPrimaryBouncerCallbackInteractor).dispatchVisibilityChanged(View.VISIBLE)
    }

    @Test
    fun testShow_isNotScrimmed() {
        verify(repository, never()).setPanelExpansion(EXPANSION_VISIBLE)
    }

    @Test
    fun testShow_keyguardIsDone() {
        whenever(bouncerView.delegate?.showNextSecurityScreenOrFinish()).thenReturn(true)
        verify(keyguardStateController, never()).notifyPrimaryBouncerShowing(true)
        verify(mPrimaryBouncerCallbackInteractor, never()).dispatchStartingToShow()
    }

    @Test
    fun testShow_isResumed() {
        whenever(repository.primaryBouncerShow.value).thenReturn(true)
        whenever(keyguardSecurityModel.getSecurityMode(anyInt()))
            .thenReturn(KeyguardSecurityModel.SecurityMode.SimPuk)

        underTest.show(true)
        verify(repository).setPrimaryShow(false)
        verify(repository).setPrimaryShow(true)
    }

    @Test
    fun testHide() {
        underTest.hide()
        verify(falsingCollector).onBouncerHidden()
        verify(keyguardStateController).notifyPrimaryBouncerShowing(false)
        verify(repository).setPrimaryShowingSoon(false)
        verify(repository).setPrimaryShow(false)
        verify(mPrimaryBouncerCallbackInteractor).dispatchVisibilityChanged(View.INVISIBLE)
        verify(repository).setPrimaryStartDisappearAnimation(null)
        verify(repository).setPanelExpansion(EXPANSION_HIDDEN)
    }

    @Test
    fun testExpansion() {
        whenever(repository.panelExpansionAmount.value).thenReturn(0.5f)
        underTest.setPanelExpansion(0.6f)
        verify(repository).setPanelExpansion(0.6f)
        verify(mPrimaryBouncerCallbackInteractor).dispatchExpansionChanged(0.6f)
    }

    @Test
    fun testExpansion_fullyShown() {
        whenever(repository.panelExpansionAmount.value).thenReturn(0.5f)
        whenever(repository.primaryBouncerStartingDisappearAnimation.value).thenReturn(null)
        underTest.setPanelExpansion(EXPANSION_VISIBLE)
        verify(falsingCollector).onBouncerShown()
        verify(mPrimaryBouncerCallbackInteractor).dispatchFullyShown()
    }

    @Test
    fun testExpansion_fullyHidden() {
        whenever(repository.panelExpansionAmount.value).thenReturn(0.5f)
        whenever(repository.primaryBouncerStartingDisappearAnimation.value).thenReturn(null)
        underTest.setPanelExpansion(EXPANSION_HIDDEN)
        verify(repository).setPrimaryShow(false)
        verify(falsingCollector).onBouncerHidden()
        verify(mPrimaryBouncerCallbackInteractor).dispatchReset()
        verify(mPrimaryBouncerCallbackInteractor).dispatchFullyHidden()
    }

    @Test
    fun testExpansion_startingToHide() {
        whenever(repository.panelExpansionAmount.value).thenReturn(EXPANSION_VISIBLE)
        underTest.setPanelExpansion(0.1f)
        verify(repository).setPrimaryStartingToHide(true)
        verify(mPrimaryBouncerCallbackInteractor).dispatchStartingToHide()
    }

    @Test
    fun testShowMessage() {
        val argCaptor = ArgumentCaptor.forClass(BouncerShowMessageModel::class.java)
        underTest.showMessage("abc", null)
        verify(repository).setShowMessage(argCaptor.capture())
        assertThat(argCaptor.value.message).isEqualTo("abc")
    }

    @Test
    fun testDismissAction() {
        val onDismissAction = mock(ActivityStarter.OnDismissAction::class.java)
        val cancelAction = mock(Runnable::class.java)
        underTest.setDismissAction(onDismissAction, cancelAction)
        verify(bouncerViewDelegate).setDismissAction(onDismissAction, cancelAction)
    }

    @Test
    fun testUpdateResources() {
        underTest.updateResources()
        verify(repository).setResourceUpdateRequests(true)
    }

    @Test
    fun testNotifyKeyguardAuthenticated() {
        underTest.notifyKeyguardAuthenticated(true)
        verify(repository).setKeyguardAuthenticated(true)
    }

    @Test
    fun testNotifyShowedMessage() {
        underTest.onMessageShown()
        verify(repository).setShowMessage(null)
    }

    @Test
    fun testSetKeyguardPosition() {
        underTest.setKeyguardPosition(0f)
        verify(repository).setKeyguardPosition(0f)
    }

    @Test
    fun testNotifyKeyguardAuthenticatedHandled() {
        underTest.notifyKeyguardAuthenticatedHandled()
        verify(repository).setKeyguardAuthenticated(null)
    }

    @Test
    fun testNotifyUpdatedResources() {
        underTest.notifyUpdatedResources()
        verify(repository).setResourceUpdateRequests(false)
    }

    @Test
    fun testSetBackButtonEnabled() {
        underTest.setBackButtonEnabled(true)
        verify(repository).setIsBackButtonEnabled(true)
    }

    @Test
    fun testStartDisappearAnimation_willRunDismissFromKeyguard() {
        whenever(bouncerViewDelegate.willRunDismissFromKeyguard()).thenReturn(true)

        val runnable = mock(Runnable::class.java)
        underTest.startDisappearAnimation(runnable)
        // End runnable should run immediately
        verify(runnable).run()
        // ... while the disappear animation should never be run
        verify(repository, never()).setPrimaryStartDisappearAnimation(any(Runnable::class.java))
    }

    @Test
    fun testStartDisappearAnimation_willNotRunDismissFromKeyguard_() {
        whenever(bouncerViewDelegate.willRunDismissFromKeyguard()).thenReturn(false)

        val runnable = mock(Runnable::class.java)
        underTest.startDisappearAnimation(runnable)
        verify(repository).setPrimaryStartDisappearAnimation(any(Runnable::class.java))
    }

    @Test
    fun testIsFullShowing() {
        whenever(repository.primaryBouncerShow.value).thenReturn(true)
        whenever(repository.panelExpansionAmount.value).thenReturn(EXPANSION_VISIBLE)
        whenever(repository.primaryBouncerStartingDisappearAnimation.value).thenReturn(null)
        assertThat(underTest.isFullyShowing()).isTrue()
        whenever(repository.primaryBouncerShow.value).thenReturn(false)
        assertThat(underTest.isFullyShowing()).isFalse()
    }

    @Test
    fun testIsScrimmed() {
        whenever(repository.primaryBouncerScrimmed.value).thenReturn(true)
        assertThat(underTest.isScrimmed()).isTrue()
        whenever(repository.primaryBouncerScrimmed.value).thenReturn(false)
        assertThat(underTest.isScrimmed()).isFalse()
    }

    @Test
    fun testIsInTransit() {
        whenever(repository.primaryBouncerShowingSoon.value).thenReturn(true)
        assertThat(underTest.isInTransit()).isTrue()
        whenever(repository.primaryBouncerShowingSoon.value).thenReturn(false)
        assertThat(underTest.isInTransit()).isFalse()
        whenever(repository.panelExpansionAmount.value).thenReturn(0.5f)
        assertThat(underTest.isInTransit()).isTrue()
    }

    @Test
    fun testIsAnimatingAway() {
        whenever(repository.primaryBouncerStartingDisappearAnimation.value).thenReturn(Runnable {})
        assertThat(underTest.isAnimatingAway()).isTrue()
        whenever(repository.primaryBouncerStartingDisappearAnimation.value).thenReturn(null)
        assertThat(underTest.isAnimatingAway()).isFalse()
    }

    @Test
    fun testWillDismissWithAction() {
        whenever(bouncerViewDelegate.willDismissWithActions()).thenReturn(true)
        assertThat(underTest.willDismissWithAction()).isTrue()
        whenever(bouncerViewDelegate.willDismissWithActions()).thenReturn(false)
        assertThat(underTest.willDismissWithAction()).isFalse()
    }

    @Test
    fun testSideFpsVisibility() {
        updateSideFpsVisibilityParameters(
            isVisible = true,
            sfpsEnabled = true,
            fpsDetectionRunning = true,
            isUnlockingWithFpAllowed = true,
            isAnimatingAway = false
        )
        underTest.updateSideFpsVisibility()
        verify(repository).setSideFpsShowing(true)
    }

    @Test
    fun testSideFpsVisibility_notVisible() {
        updateSideFpsVisibilityParameters(
            isVisible = false,
            sfpsEnabled = true,
            fpsDetectionRunning = true,
            isUnlockingWithFpAllowed = true,
            isAnimatingAway = false
        )
        underTest.updateSideFpsVisibility()
        verify(repository).setSideFpsShowing(false)
    }

    @Test
    fun testSideFpsVisibility_sfpsNotEnabled() {
        updateSideFpsVisibilityParameters(
            isVisible = true,
            sfpsEnabled = false,
            fpsDetectionRunning = true,
            isUnlockingWithFpAllowed = true,
            isAnimatingAway = false
        )
        underTest.updateSideFpsVisibility()
        verify(repository).setSideFpsShowing(false)
    }

    @Test
    fun testSideFpsVisibility_fpsDetectionNotRunning() {
        updateSideFpsVisibilityParameters(
            isVisible = true,
            sfpsEnabled = true,
            fpsDetectionRunning = false,
            isUnlockingWithFpAllowed = true,
            isAnimatingAway = false
        )
        underTest.updateSideFpsVisibility()
        verify(repository).setSideFpsShowing(false)
    }

    @Test
    fun testSideFpsVisibility_UnlockingWithFpNotAllowed() {
        updateSideFpsVisibilityParameters(
            isVisible = true,
            sfpsEnabled = true,
            fpsDetectionRunning = true,
            isUnlockingWithFpAllowed = false,
            isAnimatingAway = false
        )
        underTest.updateSideFpsVisibility()
        verify(repository).setSideFpsShowing(false)
    }

    @Test
    fun testSideFpsVisibility_AnimatingAway() {
        updateSideFpsVisibilityParameters(
            isVisible = true,
            sfpsEnabled = true,
            fpsDetectionRunning = true,
            isUnlockingWithFpAllowed = true,
            isAnimatingAway = true
        )
        underTest.updateSideFpsVisibility()
        verify(repository).setSideFpsShowing(false)
    }

    @Test
    fun delayBouncerWhenFaceAuthPossible() {
        mainHandler.setMode(FakeHandler.Mode.QUEUEING)

        // GIVEN bouncer should be delayed due to face auth
        featureFlags.apply { set(Flags.DELAY_BOUNCER, true) }
        whenever(keyguardStateController.isFaceAuthEnabled).thenReturn(true)
        whenever(keyguardUpdateMonitor.isUnlockingWithBiometricAllowed(BiometricSourceType.FACE))
            .thenReturn(true)

        // WHEN bouncer show is requested
        underTest.show(true)

        // THEN primary show & primary showing soon aren't updated immediately
        verify(repository, never()).setPrimaryShow(true)
        verify(repository, never()).setPrimaryShowingSoon(false)

        // WHEN all queued messages are dispatched
        mainHandler.dispatchQueuedMessages()

        // THEN primary show & primary showing soon are updated
        verify(repository).setPrimaryShow(true)
        verify(repository).setPrimaryShowingSoon(false)
    }

    @Test
    fun delayBouncerWhenActiveUnlockPossible() {
        mainHandler.setMode(FakeHandler.Mode.QUEUEING)

        // GIVEN bouncer should be delayed due to active unlock
        featureFlags.apply { set(Flags.DELAY_BOUNCER, true) }
        trustRepository.setCurrentUserActiveUnlockAvailable(true)
        whenever(keyguardUpdateMonitor.canTriggerActiveUnlockBasedOnDeviceState()).thenReturn(true)

        // WHEN bouncer show is requested
        underTest.show(true)

        // THEN primary show & primary showing soon were scheduled to update
        verify(repository, never()).setPrimaryShow(true)
        verify(repository, never()).setPrimaryShowingSoon(false)

        // WHEN all queued messages are dispatched
        mainHandler.dispatchQueuedMessages()

        // THEN primary show & primary showing soon are updated
        verify(repository).setPrimaryShow(true)
        verify(repository).setPrimaryShowingSoon(false)
    }

    private fun updateSideFpsVisibilityParameters(
        isVisible: Boolean,
        sfpsEnabled: Boolean,
        fpsDetectionRunning: Boolean,
        isUnlockingWithFpAllowed: Boolean,
        isAnimatingAway: Boolean
    ) {
        whenever(repository.primaryBouncerShow.value).thenReturn(isVisible)
        resources.addOverride(R.bool.config_show_sidefps_hint_on_bouncer, sfpsEnabled)
        whenever(keyguardUpdateMonitor.isFingerprintDetectionRunning)
            .thenReturn(fpsDetectionRunning)
        whenever(keyguardUpdateMonitor.isUnlockingWithFingerprintAllowed)
            .thenReturn(isUnlockingWithFpAllowed)
        whenever(repository.primaryBouncerStartingDisappearAnimation.value)
            .thenReturn(if (isAnimatingAway) Runnable {} else null)
    }
}