1 /* 2 * Copyright (C) 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 package android.input.cts 17 18 import android.view.KeyEvent 19 import androidx.test.ext.junit.rules.ActivityScenarioRule 20 import androidx.test.ext.junit.runners.AndroidJUnit4 21 import androidx.test.filters.SmallTest 22 import androidx.test.platform.app.InstrumentationRegistry 23 import com.android.compatibility.common.util.PollingCheck 24 import com.android.compatibility.common.util.ShellUtils 25 import org.junit.Before 26 import org.junit.Rule 27 import org.junit.Test 28 import org.junit.runner.RunWith 29 import org.junit.Assert.assertEquals 30 31 /** 32 * Tests for the common key combinations should be consumed by app first. 33 */ 34 @SmallTest 35 @RunWith(AndroidJUnit4::class) 36 class AppKeyCombinationsTest { 37 @get:Rule 38 var activityRule = ActivityScenarioRule(CaptureEventActivity::class.java) 39 private lateinit var activity: CaptureEventActivity 40 private val instrumentation = InstrumentationRegistry.getInstrumentation() 41 42 @Before setUpnull43 fun setUp() { 44 activityRule.getScenario().onActivity { 45 activity = it 46 } 47 PollingCheck.waitFor { activity.hasWindowFocus() } 48 instrumentation.uiAutomation.syncInputTransactions() 49 } 50 51 @Test testCtrlSpacenull52 fun testCtrlSpace() { 53 ShellUtils.runShellCommand("input keycombination CTRL_LEFT SPACE") 54 assertKeyEvent(KeyEvent.KEYCODE_CTRL_LEFT, KeyEvent.ACTION_DOWN, 0) 55 assertKeyEvent(KeyEvent.KEYCODE_SPACE, KeyEvent.ACTION_DOWN, META_CTRL) 56 assertKeyEvent(KeyEvent.KEYCODE_CTRL_LEFT, KeyEvent.ACTION_UP, META_CTRL) 57 assertKeyEvent(KeyEvent.KEYCODE_SPACE, KeyEvent.ACTION_UP, 0) 58 } 59 60 @Test testCtrlAltZnull61 fun testCtrlAltZ() { 62 ShellUtils.runShellCommand("input keycombination CTRL_LEFT ALT_LEFT Z") 63 assertKeyEvent(KeyEvent.KEYCODE_CTRL_LEFT, KeyEvent.ACTION_DOWN, 0) 64 assertKeyEvent(KeyEvent.KEYCODE_ALT_LEFT, KeyEvent.ACTION_DOWN, META_CTRL) 65 assertKeyEvent(KeyEvent.KEYCODE_Z, KeyEvent.ACTION_DOWN, META_CTRL or META_ALT) 66 assertKeyEvent(KeyEvent.KEYCODE_CTRL_LEFT, KeyEvent.ACTION_UP, META_CTRL or META_ALT) 67 assertKeyEvent(KeyEvent.KEYCODE_ALT_LEFT, KeyEvent.ACTION_UP, META_ALT) 68 assertKeyEvent(KeyEvent.KEYCODE_Z, KeyEvent.ACTION_UP, 0) 69 } 70 71 @Test testSYSRQnull72 fun testSYSRQ() { 73 ShellUtils.runShellCommand("input keyevent KEYCODE_SYSRQ") 74 assertKeyEvent(KeyEvent.KEYCODE_SYSRQ, KeyEvent.ACTION_DOWN, 0) 75 assertKeyEvent(KeyEvent.KEYCODE_SYSRQ, KeyEvent.ACTION_UP, 0) 76 } 77 assertKeyEventnull78 private fun assertKeyEvent(keyCode: Int, action: Int, metaState: Int) { 79 val event = activity.getInputEvent() as KeyEvent 80 assertEquals(keyCode, event.keyCode) 81 assertEquals(action, event.action) 82 assertEquals(0, event.flags) 83 assertEquals(metaState, event.metaState) 84 } 85 86 companion object { 87 const val META_CTRL = KeyEvent.META_CTRL_ON or KeyEvent.META_CTRL_LEFT_ON 88 const val META_ALT = KeyEvent.META_ALT_ON or KeyEvent.META_ALT_LEFT_ON 89 } 90 } 91