summaryrefslogtreecommitdiff
path: root/packages/SystemUI/tests/src/com/android/systemui/biometrics/UdfpsControllerOverlayTest.kt
blob: cb8358dd22ccf5f4f6069066bef15f1676b94065 (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
/*
 * Copyright (C) 2021 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.biometrics

import android.graphics.Rect
import android.hardware.biometrics.BiometricOverlayConstants.REASON_AUTH_BP
import android.hardware.biometrics.BiometricOverlayConstants.REASON_AUTH_KEYGUARD
import android.hardware.biometrics.BiometricOverlayConstants.REASON_AUTH_OTHER
import android.hardware.biometrics.BiometricOverlayConstants.REASON_AUTH_SETTINGS
import android.hardware.biometrics.BiometricOverlayConstants.REASON_ENROLL_ENROLLING
import android.hardware.biometrics.BiometricOverlayConstants.REASON_ENROLL_FIND_SENSOR
import android.hardware.biometrics.BiometricOverlayConstants.ShowReason
import android.hardware.fingerprint.FingerprintManager
import android.hardware.fingerprint.IUdfpsOverlayControllerCallback
import android.testing.AndroidTestingRunner
import android.testing.TestableLooper.RunWithLooper
import android.view.LayoutInflater
import android.view.MotionEvent
import android.view.View
import android.view.Surface
import android.view.Surface.Rotation
import android.view.WindowManager
import android.view.accessibility.AccessibilityManager
import androidx.test.filters.SmallTest
import com.android.keyguard.KeyguardUpdateMonitor
import com.android.systemui.R
import com.android.systemui.SysuiTestCase
import com.android.systemui.animation.ActivityLaunchAnimator
import com.android.systemui.dump.DumpManager
import com.android.systemui.plugins.statusbar.StatusBarStateController
import com.android.systemui.statusbar.LockscreenShadeTransitionController
import com.android.systemui.statusbar.phone.StatusBarKeyguardViewManager
import com.android.systemui.statusbar.phone.SystemUIDialogManager
import com.android.systemui.statusbar.phone.UnlockedScreenOffAnimationController
import com.android.systemui.statusbar.phone.panelstate.PanelExpansionStateManager
import com.android.systemui.statusbar.policy.ConfigurationController
import com.android.systemui.statusbar.policy.KeyguardStateController
import com.android.systemui.util.time.SystemClock
import com.google.common.truth.Truth.assertThat
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.ArgumentCaptor
import org.mockito.ArgumentMatchers.any
import org.mockito.ArgumentMatchers.eq
import org.mockito.Captor
import org.mockito.Mock
import org.mockito.Mockito.mock
import org.mockito.Mockito.verify
import org.mockito.junit.MockitoJUnit
import org.mockito.Mockito.`when` as whenever

private const val HAL_CONTROLS_ILLUMINATION = true
private const val REQUEST_ID = 2L

// Dimensions for the current display resolution.
private const val DISPLAY_WIDTH = 1080
private const val DISPLAY_HEIGHT = 1920
private const val SENSOR_WIDTH = 30
private const val SENSOR_HEIGHT = 60

@SmallTest
@RunWith(AndroidTestingRunner::class)
@RunWithLooper(setAsMainLooper = true)
class UdfpsControllerOverlayTest : SysuiTestCase() {

    @JvmField @Rule var rule = MockitoJUnit.rule()

    @Mock private lateinit var fingerprintManager: FingerprintManager
    @Mock private lateinit var inflater: LayoutInflater
    @Mock private lateinit var windowManager: WindowManager
    @Mock private lateinit var accessibilityManager: AccessibilityManager
    @Mock private lateinit var statusBarStateController: StatusBarStateController
    @Mock private lateinit var panelExpansionStateManager: PanelExpansionStateManager
    @Mock private lateinit var statusBarKeyguardViewManager: StatusBarKeyguardViewManager
    @Mock private lateinit var keyguardUpdateMonitor: KeyguardUpdateMonitor
    @Mock private lateinit var dialogManager: SystemUIDialogManager
    @Mock private lateinit var dumpManager: DumpManager
    @Mock private lateinit var transitionController: LockscreenShadeTransitionController
    @Mock private lateinit var configurationController: ConfigurationController
    @Mock private lateinit var systemClock: SystemClock
    @Mock private lateinit var keyguardStateController: KeyguardStateController
    @Mock private lateinit var unlockedScreenOffAnimationController: UnlockedScreenOffAnimationController
    @Mock private lateinit var hbmProvider: UdfpsHbmProvider
    @Mock private lateinit var controllerCallback: IUdfpsOverlayControllerCallback
    @Mock private lateinit var udfpsController: UdfpsController
    @Mock private lateinit var udfpsView: UdfpsView
    @Mock private lateinit var udfpsEnrollView: UdfpsEnrollView
    @Mock private lateinit var activityLaunchAnimator: ActivityLaunchAnimator
    @Captor private lateinit var layoutParamsCaptor: ArgumentCaptor<WindowManager.LayoutParams>

    private val onTouch = { _: View, _: MotionEvent, _: Boolean -> true }
    private var overlayParams: UdfpsOverlayParams = UdfpsOverlayParams()
    private lateinit var controllerOverlay: UdfpsControllerOverlay

    @Before
    fun setup() {
        context.orCreateTestableResources.addOverride(R.integer.config_udfpsEnrollProgressBar, 20)
        whenever(inflater.inflate(R.layout.udfps_view, null, false))
            .thenReturn(udfpsView)
        whenever(inflater.inflate(R.layout.udfps_enroll_view, null))
            .thenReturn(udfpsEnrollView)
        whenever(inflater.inflate(R.layout.udfps_bp_view, null))
            .thenReturn(mock(UdfpsBpView::class.java))
        whenever(inflater.inflate(R.layout.udfps_keyguard_view, null))
            .thenReturn(mock(UdfpsKeyguardView::class.java))
        whenever(inflater.inflate(R.layout.udfps_fpm_other_view, null))
            .thenReturn(mock(UdfpsFpmOtherView::class.java))
        whenever(udfpsEnrollView.context).thenReturn(context)
    }

    private fun withReason(@ShowReason reason: Int, block: () -> Unit) {
        controllerOverlay = UdfpsControllerOverlay(
            context, fingerprintManager, inflater, windowManager, accessibilityManager,
            statusBarStateController, panelExpansionStateManager, statusBarKeyguardViewManager,
            keyguardUpdateMonitor, dialogManager, dumpManager, transitionController,
            configurationController, systemClock, keyguardStateController,
            unlockedScreenOffAnimationController, HAL_CONTROLS_ILLUMINATION, hbmProvider,
            REQUEST_ID, reason, controllerCallback, onTouch, activityLaunchAnimator)
        block()
    }

    @Test
    fun showUdfpsOverlay_bp() = withReason(REASON_AUTH_BP) { showUdfpsOverlay() }

    @Test
    fun showUdfpsOverlay_keyguard() = withReason(REASON_AUTH_KEYGUARD) { showUdfpsOverlay() }

    @Test
    fun showUdfpsOverlay_settings() = withReason(REASON_AUTH_SETTINGS) { showUdfpsOverlay() }

    @Test
    fun showUdfpsOverlay_locate() = withReason(REASON_ENROLL_FIND_SENSOR) {
        showUdfpsOverlay(isEnrollUseCase = true)
    }

    @Test
    fun showUdfpsOverlay_enroll() = withReason(REASON_ENROLL_ENROLLING) {
        showUdfpsOverlay(isEnrollUseCase = true)
    }

    @Test
    fun showUdfpsOverlay_other() = withReason(REASON_AUTH_OTHER) { showUdfpsOverlay() }

    private fun withRotation(@Rotation rotation: Int, block: () -> Unit) {
        // Sensor that's in the top left corner of the display in natural orientation.
        val sensorBounds = Rect(0, 0, SENSOR_WIDTH, SENSOR_HEIGHT)
        overlayParams = UdfpsOverlayParams(
            sensorBounds,
            DISPLAY_WIDTH,
            DISPLAY_HEIGHT,
            scaleFactor = 1f,
            rotation
        )
        block()
    }

    @Test
    fun showUdfpsOverlay_withRotation0() = withRotation(Surface.ROTATION_0) {
        withReason(REASON_AUTH_BP) {
            controllerOverlay.show(udfpsController, overlayParams)
            verify(windowManager).addView(
                eq(controllerOverlay.overlayView),
                layoutParamsCaptor.capture()
            )

            // ROTATION_0 is the native orientation. Sensor should stay in the top left corner.
            val lp = layoutParamsCaptor.value
            assertThat(lp.x).isEqualTo(0)
            assertThat(lp.y).isEqualTo(0)
            assertThat(lp.width).isEqualTo(SENSOR_WIDTH)
            assertThat(lp.height).isEqualTo(SENSOR_HEIGHT)
        }
    }

    @Test
    fun showUdfpsOverlay_withRotation180() = withRotation(Surface.ROTATION_180) {
        withReason(REASON_AUTH_BP) {
            controllerOverlay.show(udfpsController, overlayParams)
            verify(windowManager).addView(
                eq(controllerOverlay.overlayView),
                layoutParamsCaptor.capture()
            )

            // ROTATION_180 is not supported. Sensor should stay in the top left corner.
            val lp = layoutParamsCaptor.value
            assertThat(lp.x).isEqualTo(0)
            assertThat(lp.y).isEqualTo(0)
            assertThat(lp.width).isEqualTo(SENSOR_WIDTH)
            assertThat(lp.height).isEqualTo(SENSOR_HEIGHT)
        }
    }

    @Test
    fun showUdfpsOverlay_withRotation90() = withRotation(Surface.ROTATION_90) {
        withReason(REASON_AUTH_BP) {
            controllerOverlay.show(udfpsController, overlayParams)
            verify(windowManager).addView(
                eq(controllerOverlay.overlayView),
                layoutParamsCaptor.capture()
            )

            // Sensor should be in the bottom left corner in ROTATION_90.
            val lp = layoutParamsCaptor.value
            assertThat(lp.x).isEqualTo(0)
            assertThat(lp.y).isEqualTo(DISPLAY_WIDTH - SENSOR_WIDTH)
            assertThat(lp.width).isEqualTo(SENSOR_HEIGHT)
            assertThat(lp.height).isEqualTo(SENSOR_WIDTH)
        }
    }

    @Test
    fun showUdfpsOverlay_withRotation270() = withRotation(Surface.ROTATION_270) {
        withReason(REASON_AUTH_BP) {
            controllerOverlay.show(udfpsController, overlayParams)
            verify(windowManager).addView(
                eq(controllerOverlay.overlayView),
                layoutParamsCaptor.capture()
            )

            // Sensor should be in the top right corner in ROTATION_270.
            val lp = layoutParamsCaptor.value
            assertThat(lp.x).isEqualTo(DISPLAY_HEIGHT - SENSOR_HEIGHT)
            assertThat(lp.y).isEqualTo(0)
            assertThat(lp.width).isEqualTo(SENSOR_HEIGHT)
            assertThat(lp.height).isEqualTo(SENSOR_WIDTH)
        }
    }

    private fun showUdfpsOverlay(isEnrollUseCase: Boolean = false) {
        val didShow = controllerOverlay.show(udfpsController, overlayParams)

        verify(windowManager).addView(eq(controllerOverlay.overlayView), any())
        verify(udfpsView).setHbmProvider(eq(hbmProvider))
        verify(udfpsView).animationViewController = any()
        verify(udfpsView).addView(any())

        assertThat(didShow).isTrue()
        assertThat(controllerOverlay.isShowing).isTrue()
        assertThat(controllerOverlay.isHiding).isFalse()
        assertThat(controllerOverlay.overlayView).isNotNull()
        if (isEnrollUseCase) {
            verify(udfpsEnrollView).updateSensorLocation(eq(overlayParams.sensorBounds))
            assertThat(controllerOverlay.enrollHelper).isNotNull()
        } else {
            assertThat(controllerOverlay.enrollHelper).isNull()
        }
    }

    @Test
    fun hideUdfpsOverlay_bp() = withReason(REASON_AUTH_BP) { hideUdfpsOverlay() }

    @Test
    fun hideUdfpsOverlay_keyguard() = withReason(REASON_AUTH_KEYGUARD) { hideUdfpsOverlay() }

    @Test
    fun hideUdfpsOverlay_settings() = withReason(REASON_AUTH_SETTINGS) { hideUdfpsOverlay() }

    @Test
    fun hideUdfpsOverlay_locate() = withReason(REASON_ENROLL_FIND_SENSOR) { hideUdfpsOverlay() }

    @Test
    fun hideUdfpsOverlay_enroll() = withReason(REASON_ENROLL_ENROLLING) { hideUdfpsOverlay() }

    @Test
    fun hideUdfpsOverlay_other() = withReason(REASON_AUTH_OTHER) { hideUdfpsOverlay() }

    private fun hideUdfpsOverlay() {
        val didShow = controllerOverlay.show(udfpsController, overlayParams)
        val view = controllerOverlay.overlayView
        val didHide = controllerOverlay.hide()

        verify(windowManager).removeView(eq(view))

        assertThat(didShow).isTrue()
        assertThat(didHide).isTrue()
        assertThat(controllerOverlay.overlayView).isNull()
        assertThat(controllerOverlay.animationViewController).isNull()
        assertThat(controllerOverlay.isShowing).isFalse()
        assertThat(controllerOverlay.isHiding).isTrue()
    }

    @Test
    fun canNotHide() = withReason(REASON_AUTH_BP) {
        assertThat(controllerOverlay.hide()).isFalse()
    }

    @Test
    fun canNotReshow() = withReason(REASON_AUTH_BP) {
        assertThat(controllerOverlay.show(udfpsController, overlayParams)).isTrue()
        assertThat(controllerOverlay.show(udfpsController, overlayParams)).isFalse()
    }

    @Test
    fun forwardEnrollProgressEvents() = withReason(REASON_ENROLL_ENROLLING) {
        controllerOverlay.show(udfpsController, overlayParams)

        with(EnrollListener(controllerOverlay)) {
            controllerOverlay.onEnrollmentProgress(/* remaining */20)
            controllerOverlay.onAcquiredGood()
            assertThat(progress).isTrue()
            assertThat(help).isFalse()
            assertThat(acquired).isFalse()
        }
    }

    @Test
    fun forwardEnrollHelpEvents() = withReason(REASON_ENROLL_ENROLLING) {
        controllerOverlay.show(udfpsController, overlayParams)

        with(EnrollListener(controllerOverlay)) {
            controllerOverlay.onEnrollmentHelp()
            assertThat(progress).isFalse()
            assertThat(help).isTrue()
            assertThat(acquired).isFalse()
        }
    }

    @Test
    fun forwardEnrollAcquiredEvents() = withReason(REASON_ENROLL_ENROLLING) {
        controllerOverlay.show(udfpsController, overlayParams)

        with(EnrollListener(controllerOverlay)) {
            controllerOverlay.onEnrollmentProgress(/* remaining */ 1)
            controllerOverlay.onAcquiredGood()
            assertThat(progress).isTrue()
            assertThat(help).isFalse()
            assertThat(acquired).isTrue()
        }
    }

    @Test
    fun cancels() = withReason(REASON_AUTH_BP) {
        controllerOverlay.cancel()
        verify(controllerCallback).onUserCanceled()
    }

    @Test
    fun stopIlluminatingOnHide() = withReason(REASON_AUTH_BP) {
        whenever(udfpsView.isIlluminationRequested).thenReturn(true)

        controllerOverlay.show(udfpsController, overlayParams)
        controllerOverlay.hide()
        verify(udfpsView).stopIllumination()
    }

    @Test
    fun matchesRequestIds() = withReason(REASON_AUTH_BP) {
        assertThat(controllerOverlay.matchesRequestId(REQUEST_ID)).isTrue()
        assertThat(controllerOverlay.matchesRequestId(REQUEST_ID + 1)).isFalse()
    }

    @Test
    fun testTouchOutsideAreaNoRotation() = withReason(REASON_ENROLL_ENROLLING) {
        val touchHints =
            context.resources.getStringArray(R.array.udfps_accessibility_touch_hints)
        val rotation = Surface.ROTATION_0
        // touch at 0 degrees
        assertThat(controllerOverlay.onTouchOutsideOfSensorAreaImpl(0.0f /* x */, 0.0f /* y */,
                0.0f /* sensorX */, 0.0f /* sensorY */, rotation))
                .isEqualTo(touchHints[0])
        // touch at 90 degrees
        assertThat(controllerOverlay.onTouchOutsideOfSensorAreaImpl(0.0f /* x */, -1.0f /* y */,
                0.0f /* sensorX */, 0.0f /* sensorY */, rotation))
                .isEqualTo(touchHints[1])
        // touch at 180 degrees
        assertThat(controllerOverlay.onTouchOutsideOfSensorAreaImpl(-1.0f /* x */, 0.0f /* y */,
                0.0f /* sensorX */, 0.0f /* sensorY */, rotation))
                .isEqualTo(touchHints[2])
        // touch at 270 degrees
        assertThat(controllerOverlay.onTouchOutsideOfSensorAreaImpl(0.0f /* x */, 1.0f /* y */,
                0.0f /* sensorX */, 0.0f /* sensorY */, rotation))
                .isEqualTo(touchHints[3])
    }

    fun testTouchOutsideAreaNoRotation90Degrees() = withReason(REASON_ENROLL_ENROLLING) {
        val touchHints =
            context.resources.getStringArray(R.array.udfps_accessibility_touch_hints)
        val rotation = Surface.ROTATION_90
        // touch at 0 degrees -> 90 degrees
        assertThat(controllerOverlay.onTouchOutsideOfSensorAreaImpl(0.0f /* x */, 0.0f /* y */,
                0.0f /* sensorX */, 0.0f /* sensorY */, rotation))
                .isEqualTo(touchHints[1])
        // touch at 90 degrees -> 180 degrees
        assertThat(controllerOverlay.onTouchOutsideOfSensorAreaImpl(0.0f /* x */, -1.0f /* y */,
                0.0f /* sensorX */, 0.0f /* sensorY */, rotation))
                .isEqualTo(touchHints[2])
        // touch at 180 degrees -> 270 degrees
        assertThat(controllerOverlay.onTouchOutsideOfSensorAreaImpl(-1.0f /* x */, 0.0f /* y */,
                0.0f /* sensorX */, 0.0f /* sensorY */, rotation))
                .isEqualTo(touchHints[3])
        // touch at 270 degrees -> 0 degrees
        assertThat(controllerOverlay.onTouchOutsideOfSensorAreaImpl(0.0f /* x */, 1.0f /* y */,
                0.0f /* sensorX */, 0.0f /* sensorY */, rotation))
                .isEqualTo(touchHints[0])
    }

    fun testTouchOutsideAreaNoRotation270Degrees() = withReason(REASON_ENROLL_ENROLLING) {
        val touchHints =
            context.resources.getStringArray(R.array.udfps_accessibility_touch_hints)
        val rotation = Surface.ROTATION_270
        // touch at 0 degrees -> 270 degrees
        assertThat(controllerOverlay.onTouchOutsideOfSensorAreaImpl(0.0f /* x */, 0.0f /* y */,
                0.0f /* sensorX */, 0.0f /* sensorY */, rotation))
                .isEqualTo(touchHints[3])
        // touch at 90 degrees -> 0 degrees
        assertThat(controllerOverlay.onTouchOutsideOfSensorAreaImpl(0.0f /* x */, -1.0f /* y */,
                0.0f /* sensorX */, 0.0f /* sensorY */, rotation))
                .isEqualTo(touchHints[0])
        // touch at 180 degrees -> 90 degrees
        assertThat(controllerOverlay.onTouchOutsideOfSensorAreaImpl(-1.0f /* x */, 0.0f /* y */,
                0.0f /* sensorX */, 0.0f /* sensorY */, rotation))
                .isEqualTo(touchHints[1])
        // touch at 270 degrees -> 180 degrees
        assertThat(controllerOverlay.onTouchOutsideOfSensorAreaImpl(0.0f /* x */, 1.0f /* y */,
                0.0f /* sensorX */, 0.0f /* sensorY */, rotation))
                .isEqualTo(touchHints[2])
    }
}

private class EnrollListener(
    overlay: UdfpsControllerOverlay,
    var progress: Boolean = false,
    var help: Boolean = false,
    var acquired: Boolean = false
) : UdfpsEnrollHelper.Listener {

    init {
        overlay.enrollHelper!!.setListener(this)
    }

    override fun onEnrollmentProgress(remaining: Int, totalSteps: Int) {
        progress = true
    }

    override fun onEnrollmentHelp(remaining: Int, totalSteps: Int) {
        help = true
    }

    override fun onLastStepAcquired() {
        acquired = true
    }
}