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