xref: /aosp_15_r20/cts/tests/input/src/android/input/cts/VerifyHardwareKeyEventTest.kt (revision b7c941bb3fa97aba169d73cee0bed2de8ac964bf)
1 /*
2  * Copyright 2021 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package android.input.cts
18 
19 import android.hardware.input.InputManager
20 import androidx.test.ext.junit.rules.ActivityScenarioRule
21 import androidx.test.ext.junit.runners.AndroidJUnit4
22 import androidx.test.filters.MediumTest
23 import androidx.test.platform.app.InstrumentationRegistry
24 import com.android.compatibility.common.util.PollingCheck
25 import com.android.cts.input.UinputKeyboard
26 import org.junit.Assert.assertNotNull
27 import org.junit.Before
28 import org.junit.Rule
29 import org.junit.Test
30 import org.junit.runner.RunWith
31 
32 /**
33  * Create a virtual keyboard and inject a 'hardware' key event. Ensure that the event can be
34  * verified using the InputManager::verifyInputEvent api.
35  */
36 @MediumTest
37 @RunWith(AndroidJUnit4::class)
38 class VerifyHardwareKeyEventTest {
39 
40     companion object {
41         const val KEY_A = 30
42     }
43 
44     private val instrumentation = InstrumentationRegistry.getInstrumentation()
45 
46     @get:Rule
47     val rule = ActivityScenarioRule<CaptureEventActivity>(CaptureEventActivity::class.java)
48 
49     private lateinit var activity: CaptureEventActivity
50     private lateinit var inputManager: InputManager
51 
52     @Before
setUpnull53     fun setUp() {
54         rule.getScenario().onActivity {
55             inputManager = it.getSystemService(InputManager::class.java)
56             activity = it
57         }
58         PollingCheck.waitFor { activity.hasWindowFocus() }
59     }
60 
assertReceivedEventsCanBeVerifiednull61     private fun assertReceivedEventsCanBeVerified(numEvents: Int) {
62         for (i in 1..numEvents) {
63             val lastInputEvent = activity.getInputEvent()
64             assertNotNull("Event number $i is null!", lastInputEvent)
65             assertNotNull(inputManager.verifyInputEvent(lastInputEvent!!))
66         }
67     }
68 
69     /**
70      * Send a hardware key event and check that InputManager::verifyInputEvent returns non-null
71      * result.
72      */
73     @Test
testVerifyHardwareKeyEventnull74     fun testVerifyHardwareKeyEvent() {
75         val keyboardDevice = UinputKeyboard(instrumentation)
76 
77         injectKeyDown(keyboardDevice, KEY_A)
78         // Send the UP event right away to avoid key repeat
79         injectKeyUp(keyboardDevice, KEY_A)
80 
81         assertReceivedEventsCanBeVerified(numEvents = 2)
82 
83         keyboardDevice.close()
84     }
85 }
86