1 package com.android.systemui.biometrics.udfps
2 
3 import android.graphics.Rect
4 import androidx.test.filters.SmallTest
5 import com.android.systemui.SysuiTestCase
6 import com.google.common.truth.Truth.assertThat
7 import org.junit.Test
8 import org.junit.runner.RunWith
9 import org.junit.runners.Parameterized
10 import org.junit.runners.Parameterized.Parameters
11 
12 @SmallTest
13 @RunWith(Parameterized::class)
14 class NormalizedTouchDataTest(val testCase: TestCase) : SysuiTestCase() {
15 
16     @Test
17     fun isWithinSensor() {
18         val touchData = TOUCH_DATA.copy(x = testCase.x.toFloat(), y = testCase.y.toFloat())
19         val actual = touchData.isWithinBounds(SENSOR)
20 
21         assertThat(actual).isEqualTo(testCase.expected)
22     }
23 
24     data class TestCase(val x: Int, val y: Int, val expected: Boolean)
25 
26     companion object {
27         @Parameters(name = "{0}")
28         @JvmStatic
29         fun data(): List<TestCase> =
30             listOf(
31                     genPositiveTestCases(
32                         validXs = listOf(SENSOR.left, SENSOR.right, SENSOR.centerX()),
33                         validYs = listOf(SENSOR.top, SENSOR.bottom, SENSOR.centerY())
34                     ),
35                     genNegativeTestCases(
36                         invalidXs = listOf(SENSOR.left - 1, SENSOR.right + 1),
37                         invalidYs = listOf(SENSOR.top - 1, SENSOR.bottom + 1),
38                         validXs = listOf(SENSOR.left, SENSOR.right, SENSOR.centerX()),
39                         validYs = listOf(SENSOR.top, SENSOR.bottom, SENSOR.centerY())
40                     )
41                 )
42                 .flatten()
43     }
44 }
45 
46 /* Placeholder touch parameters. */
47 private const val POINTER_ID = 42
48 private const val NATIVE_MINOR = 2.71828f
49 private const val NATIVE_MAJOR = 3.14f
50 private const val ORIENTATION = 1.23f
51 private const val TIME = 12345699L
52 private const val GESTURE_START = 12345600L
53 
54 /* Template [NormalizedTouchData]. */
55 private val TOUCH_DATA =
56     NormalizedTouchData(
57         POINTER_ID,
58         x = 0f,
59         y = 0f,
60         NATIVE_MINOR,
61         NATIVE_MAJOR,
62         ORIENTATION,
63         TIME,
64         GESTURE_START
65     )
66 
67 private val SENSOR = Rect(100 /* left */, 200 /* top */, 300 /* right */, 500 /* bottom */)
68 
69 private fun genTestCases(
70     xs: List<Int>,
71     ys: List<Int>,
72     expected: Boolean
73 ): List<NormalizedTouchDataTest.TestCase> {
74     return xs.flatMap { x -> ys.map { y -> NormalizedTouchDataTest.TestCase(x, y, expected) } }
75 }
76 
77 private fun genPositiveTestCases(
78     validXs: List<Int>,
79     validYs: List<Int>,
80 ) = genTestCases(validXs, validYs, expected = true)
81 
82 private fun genNegativeTestCases(
83     invalidXs: List<Int>,
84     invalidYs: List<Int>,
85     validXs: List<Int>,
86     validYs: List<Int>,
87 ): List<NormalizedTouchDataTest.TestCase> {
88     return genTestCases(invalidXs, validYs, expected = false) +
89         genTestCases(validXs, invalidYs, expected = false)
90 }
91