1 /* 2 * Copyright 2023 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 com.android.server.policy; 18 19 import android.platform.test.annotations.Presubmit; 20 import android.view.KeyEvent; 21 22 import androidx.test.filters.SmallTest; 23 24 import com.android.internal.annotations.Keep; 25 import com.android.server.input.KeyboardMetricsCollector.KeyboardLogEvent; 26 27 import org.junit.Before; 28 import org.junit.Test; 29 import org.junit.runner.RunWith; 30 31 import junitparams.JUnitParamsRunner; 32 import junitparams.Parameters; 33 34 @Presubmit 35 @SmallTest 36 @RunWith(JUnitParamsRunner.class) 37 public class ShortcutLoggingTests extends ShortcutKeyTestBase { 38 39 private static final int VENDOR_ID = 0x123; 40 private static final int PRODUCT_ID = 0x456; 41 private static final int META_KEY = KeyEvent.KEYCODE_META_LEFT; 42 private static final int META_ON = MODIFIER.get(KeyEvent.KEYCODE_META_LEFT); 43 private static final int ALT_KEY = KeyEvent.KEYCODE_ALT_LEFT; 44 private static final int ALT_ON = MODIFIER.get(KeyEvent.KEYCODE_ALT_LEFT); 45 private static final int CTRL_KEY = KeyEvent.KEYCODE_CTRL_LEFT; 46 private static final int CTRL_ON = MODIFIER.get(KeyEvent.KEYCODE_CTRL_LEFT); 47 private static final int SHIFT_KEY = KeyEvent.KEYCODE_SHIFT_LEFT; 48 private static final int SHIFT_ON = MODIFIER.get(KeyEvent.KEYCODE_SHIFT_LEFT); 49 50 @Keep shortcutTestArguments()51 private static Object[][] shortcutTestArguments() { 52 // testName, testKeys, expectedLogEvent, expectedKey, expectedModifierState 53 return new Object[][]{ 54 {"Meta + H -> Open Home", new int[]{META_KEY, KeyEvent.KEYCODE_H}, 55 KeyboardLogEvent.HOME, KeyEvent.KEYCODE_H, META_ON}, 56 {"Meta + Enter -> Open Home", new int[]{META_KEY, KeyEvent.KEYCODE_ENTER}, 57 KeyboardLogEvent.HOME, KeyEvent.KEYCODE_ENTER, META_ON}, 58 {"HOME key -> Open Home", new int[]{KeyEvent.KEYCODE_HOME}, KeyboardLogEvent.HOME, 59 KeyEvent.KEYCODE_HOME, 0}, 60 {"RECENT_APPS key -> Open Overview", new int[]{KeyEvent.KEYCODE_RECENT_APPS}, 61 KeyboardLogEvent.RECENT_APPS, KeyEvent.KEYCODE_RECENT_APPS, 0}, 62 {"Meta + Tab -> Open OVerview", new int[]{META_KEY, KeyEvent.KEYCODE_TAB}, 63 KeyboardLogEvent.RECENT_APPS, KeyEvent.KEYCODE_TAB, META_ON}, 64 {"Alt + Tab -> Open Overview", new int[]{ALT_KEY, KeyEvent.KEYCODE_TAB}, 65 KeyboardLogEvent.RECENT_APPS, KeyEvent.KEYCODE_TAB, ALT_ON}, 66 {"BACK key -> Go back", new int[]{KeyEvent.KEYCODE_BACK}, KeyboardLogEvent.BACK, 67 KeyEvent.KEYCODE_BACK, 0}, 68 {"APP_SWITCH key -> Open App switcher", new int[]{KeyEvent.KEYCODE_APP_SWITCH}, 69 KeyboardLogEvent.APP_SWITCH, KeyEvent.KEYCODE_APP_SWITCH, 0}, 70 {"ASSIST key -> Launch assistant", new int[]{KeyEvent.KEYCODE_ASSIST}, 71 KeyboardLogEvent.LAUNCH_ASSISTANT, KeyEvent.KEYCODE_ASSIST, 0}, 72 {"Meta + A -> Launch assistant", new int[]{META_KEY, KeyEvent.KEYCODE_A}, 73 KeyboardLogEvent.LAUNCH_ASSISTANT, KeyEvent.KEYCODE_A, META_ON}, 74 {"VOICE_ASSIST key -> Launch Voice Assistant", 75 new int[]{KeyEvent.KEYCODE_VOICE_ASSIST}, 76 KeyboardLogEvent.LAUNCH_VOICE_ASSISTANT, KeyEvent.KEYCODE_VOICE_ASSIST, 0}, 77 {"Meta + I -> Launch System Settings", new int[]{META_KEY, KeyEvent.KEYCODE_I}, 78 KeyboardLogEvent.LAUNCH_SYSTEM_SETTINGS, KeyEvent.KEYCODE_I, META_ON}, 79 {"Meta + N -> Toggle Notification panel", new int[]{META_KEY, KeyEvent.KEYCODE_N}, 80 KeyboardLogEvent.TOGGLE_NOTIFICATION_PANEL, KeyEvent.KEYCODE_N, META_ON}, 81 {"NOTIFICATION key -> Toggle Notification Panel", 82 new int[]{KeyEvent.KEYCODE_NOTIFICATION}, 83 KeyboardLogEvent.TOGGLE_NOTIFICATION_PANEL, KeyEvent.KEYCODE_NOTIFICATION, 84 0}, 85 {"Meta + T -> Toggle Taskbar", new int[]{META_KEY, KeyEvent.KEYCODE_T}, 86 KeyboardLogEvent.TOGGLE_TASKBAR, KeyEvent.KEYCODE_T, META_ON}, 87 {"Meta + Ctrl + S -> Take Screenshot", 88 new int[]{META_KEY, CTRL_KEY, KeyEvent.KEYCODE_S}, 89 KeyboardLogEvent.TAKE_SCREENSHOT, KeyEvent.KEYCODE_S, META_ON | CTRL_ON}, 90 {"Meta + / -> Open Shortcut Helper", new int[]{META_KEY, KeyEvent.KEYCODE_SLASH}, 91 KeyboardLogEvent.OPEN_SHORTCUT_HELPER, KeyEvent.KEYCODE_SLASH, META_ON}, 92 {"BRIGHTNESS_UP key -> Increase Brightness", 93 new int[]{KeyEvent.KEYCODE_BRIGHTNESS_UP}, KeyboardLogEvent.BRIGHTNESS_UP, 94 KeyEvent.KEYCODE_BRIGHTNESS_UP, 0}, 95 {"BRIGHTNESS_DOWN key -> Decrease Brightness", 96 new int[]{KeyEvent.KEYCODE_BRIGHTNESS_DOWN}, 97 KeyboardLogEvent.BRIGHTNESS_DOWN, KeyEvent.KEYCODE_BRIGHTNESS_DOWN, 0}, 98 {"KEYBOARD_BACKLIGHT_UP key -> Increase Keyboard Backlight", 99 new int[]{KeyEvent.KEYCODE_KEYBOARD_BACKLIGHT_UP}, 100 KeyboardLogEvent.KEYBOARD_BACKLIGHT_UP, 101 KeyEvent.KEYCODE_KEYBOARD_BACKLIGHT_UP, 0}, 102 {"KEYBOARD_BACKLIGHT_DOWN key -> Decrease Keyboard Backlight", 103 new int[]{KeyEvent.KEYCODE_KEYBOARD_BACKLIGHT_DOWN}, 104 KeyboardLogEvent.KEYBOARD_BACKLIGHT_DOWN, 105 KeyEvent.KEYCODE_KEYBOARD_BACKLIGHT_DOWN, 0}, 106 {"KEYBOARD_BACKLIGHT_TOGGLE key -> Toggle Keyboard Backlight", 107 new int[]{KeyEvent.KEYCODE_KEYBOARD_BACKLIGHT_TOGGLE}, 108 KeyboardLogEvent.KEYBOARD_BACKLIGHT_TOGGLE, 109 KeyEvent.KEYCODE_KEYBOARD_BACKLIGHT_TOGGLE, 0}, 110 {"VOLUME_UP key -> Increase Volume", new int[]{KeyEvent.KEYCODE_VOLUME_UP}, 111 KeyboardLogEvent.VOLUME_UP, KeyEvent.KEYCODE_VOLUME_UP, 0}, 112 {"VOLUME_DOWN key -> Decrease Volume", new int[]{KeyEvent.KEYCODE_VOLUME_DOWN}, 113 KeyboardLogEvent.VOLUME_DOWN, KeyEvent.KEYCODE_VOLUME_DOWN, 0}, 114 {"VOLUME_MUTE key -> Mute Volume", new int[]{KeyEvent.KEYCODE_VOLUME_MUTE}, 115 KeyboardLogEvent.VOLUME_MUTE, KeyEvent.KEYCODE_VOLUME_MUTE, 0}, 116 {"ALL_APPS key -> Open App Drawer", new int[]{KeyEvent.KEYCODE_ALL_APPS}, 117 KeyboardLogEvent.ALL_APPS, KeyEvent.KEYCODE_ALL_APPS, 0}, 118 {"SEARCH key -> Launch Search Activity", new int[]{KeyEvent.KEYCODE_SEARCH}, 119 KeyboardLogEvent.LAUNCH_SEARCH, KeyEvent.KEYCODE_SEARCH, 0}, 120 {"LANGUAGE_SWITCH key -> Switch Keyboard Language", 121 new int[]{KeyEvent.KEYCODE_LANGUAGE_SWITCH}, 122 KeyboardLogEvent.LANGUAGE_SWITCH, KeyEvent.KEYCODE_LANGUAGE_SWITCH, 0}, 123 {"Meta + Space -> Switch Keyboard Language", 124 new int[]{META_KEY, KeyEvent.KEYCODE_SPACE}, 125 KeyboardLogEvent.LANGUAGE_SWITCH, KeyEvent.KEYCODE_SPACE, META_ON}, 126 {"Meta + Shift + Space -> Switch Keyboard Language", 127 new int[]{META_KEY, SHIFT_KEY, KeyEvent.KEYCODE_SPACE}, 128 KeyboardLogEvent.LANGUAGE_SWITCH, KeyEvent.KEYCODE_SPACE, 129 META_ON | SHIFT_ON}, 130 {"META key -> Open App Drawer in Accessibility mode", new int[]{META_KEY}, 131 KeyboardLogEvent.ACCESSIBILITY_ALL_APPS, META_KEY, META_ON}, 132 {"Meta + Alt -> Toggle CapsLock", new int[]{META_KEY, ALT_KEY}, 133 KeyboardLogEvent.TOGGLE_CAPS_LOCK, ALT_KEY, META_ON | ALT_ON}, 134 {"Alt + Meta -> Toggle CapsLock", new int[]{ALT_KEY, META_KEY}, 135 KeyboardLogEvent.TOGGLE_CAPS_LOCK, META_KEY, META_ON | ALT_ON}, 136 {"CAPS_LOCK key -> Toggle CapsLock", new int[]{KeyEvent.KEYCODE_CAPS_LOCK}, 137 KeyboardLogEvent.TOGGLE_CAPS_LOCK, KeyEvent.KEYCODE_CAPS_LOCK, 0}, 138 {"MUTE key -> Mute System Microphone", new int[]{KeyEvent.KEYCODE_MUTE}, 139 KeyboardLogEvent.SYSTEM_MUTE, KeyEvent.KEYCODE_MUTE, 0}, 140 {"Meta + Ctrl + DPAD_UP -> Split screen navigation", 141 new int[]{META_KEY, CTRL_KEY, KeyEvent.KEYCODE_DPAD_UP}, 142 KeyboardLogEvent.SPLIT_SCREEN_NAVIGATION, KeyEvent.KEYCODE_DPAD_UP, 143 META_ON | CTRL_ON}, 144 {"Meta + Ctrl + DPAD_LEFT -> Split screen navigation", 145 new int[]{META_KEY, CTRL_KEY, KeyEvent.KEYCODE_DPAD_LEFT}, 146 KeyboardLogEvent.SPLIT_SCREEN_NAVIGATION, KeyEvent.KEYCODE_DPAD_LEFT, 147 META_ON | CTRL_ON}, 148 {"Meta + Ctrl + DPAD_RIGHT -> Split screen navigation", 149 new int[]{META_KEY, CTRL_KEY, KeyEvent.KEYCODE_DPAD_RIGHT}, 150 KeyboardLogEvent.SPLIT_SCREEN_NAVIGATION, KeyEvent.KEYCODE_DPAD_RIGHT, 151 META_ON | CTRL_ON}, 152 {"Shift + Menu -> Trigger Bug Report", new int[]{SHIFT_KEY, KeyEvent.KEYCODE_MENU}, 153 KeyboardLogEvent.TRIGGER_BUG_REPORT, KeyEvent.KEYCODE_MENU, SHIFT_ON}, 154 {"Meta + L -> Lock Homescreen", new int[]{META_KEY, KeyEvent.KEYCODE_L}, 155 KeyboardLogEvent.LOCK_SCREEN, KeyEvent.KEYCODE_L, META_ON}, 156 {"Meta + Ctrl + N -> Open Notes", new int[]{META_KEY, CTRL_KEY, KeyEvent.KEYCODE_N}, 157 KeyboardLogEvent.OPEN_NOTES, KeyEvent.KEYCODE_N, META_ON | CTRL_ON}, 158 {"POWER key -> Toggle Power", new int[]{KeyEvent.KEYCODE_POWER}, 159 KeyboardLogEvent.TOGGLE_POWER, KeyEvent.KEYCODE_POWER, 0}, 160 {"TV_POWER key -> Toggle Power", new int[]{KeyEvent.KEYCODE_TV_POWER}, 161 KeyboardLogEvent.TOGGLE_POWER, KeyEvent.KEYCODE_TV_POWER, 0}, 162 {"SYSTEM_NAVIGATION_DOWN key -> System Navigation", 163 new int[]{KeyEvent.KEYCODE_SYSTEM_NAVIGATION_DOWN}, 164 KeyboardLogEvent.SYSTEM_NAVIGATION, KeyEvent.KEYCODE_SYSTEM_NAVIGATION_DOWN, 165 0}, 166 {"SYSTEM_NAVIGATION_UP key -> System Navigation", 167 new int[]{KeyEvent.KEYCODE_SYSTEM_NAVIGATION_UP}, 168 KeyboardLogEvent.SYSTEM_NAVIGATION, KeyEvent.KEYCODE_SYSTEM_NAVIGATION_UP, 169 0}, 170 {"SYSTEM_NAVIGATION_LEFT key -> System Navigation", 171 new int[]{KeyEvent.KEYCODE_SYSTEM_NAVIGATION_LEFT}, 172 KeyboardLogEvent.SYSTEM_NAVIGATION, KeyEvent.KEYCODE_SYSTEM_NAVIGATION_LEFT, 173 0}, 174 {"SYSTEM_NAVIGATION_RIGHT key -> System Navigation", 175 new int[]{KeyEvent.KEYCODE_SYSTEM_NAVIGATION_RIGHT}, 176 KeyboardLogEvent.SYSTEM_NAVIGATION, 177 KeyEvent.KEYCODE_SYSTEM_NAVIGATION_RIGHT, 0}, 178 {"SLEEP key -> System Sleep", new int[]{KeyEvent.KEYCODE_SLEEP}, 179 KeyboardLogEvent.SLEEP, KeyEvent.KEYCODE_SLEEP, 0}, 180 {"SOFT_SLEEP key -> System Sleep", new int[]{KeyEvent.KEYCODE_SOFT_SLEEP}, 181 KeyboardLogEvent.SLEEP, KeyEvent.KEYCODE_SOFT_SLEEP, 0}, 182 {"WAKEUP key -> System Wakeup", new int[]{KeyEvent.KEYCODE_WAKEUP}, 183 KeyboardLogEvent.WAKEUP, KeyEvent.KEYCODE_WAKEUP, 0}, 184 {"MEDIA_PLAY key -> Media Control", new int[]{KeyEvent.KEYCODE_MEDIA_PLAY}, 185 KeyboardLogEvent.MEDIA_KEY, KeyEvent.KEYCODE_MEDIA_PLAY, 0}, 186 {"MEDIA_PAUSE key -> Media Control", new int[]{KeyEvent.KEYCODE_MEDIA_PAUSE}, 187 KeyboardLogEvent.MEDIA_KEY, KeyEvent.KEYCODE_MEDIA_PAUSE, 0}, 188 {"MEDIA_PLAY_PAUSE key -> Media Control", 189 new int[]{KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE}, KeyboardLogEvent.MEDIA_KEY, 190 KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE, 0}, 191 {"Meta + B -> Launch Default Browser", new int[]{META_KEY, KeyEvent.KEYCODE_B}, 192 KeyboardLogEvent.LAUNCH_DEFAULT_BROWSER, KeyEvent.KEYCODE_B, META_ON}, 193 {"EXPLORER key -> Launch Default Browser", new int[]{KeyEvent.KEYCODE_EXPLORER}, 194 KeyboardLogEvent.LAUNCH_DEFAULT_BROWSER, KeyEvent.KEYCODE_EXPLORER, 0}, 195 {"Meta + C -> Launch Default Contacts", new int[]{META_KEY, KeyEvent.KEYCODE_C}, 196 KeyboardLogEvent.LAUNCH_DEFAULT_CONTACTS, KeyEvent.KEYCODE_C, META_ON}, 197 {"CONTACTS key -> Launch Default Contacts", new int[]{KeyEvent.KEYCODE_CONTACTS}, 198 KeyboardLogEvent.LAUNCH_DEFAULT_CONTACTS, KeyEvent.KEYCODE_CONTACTS, 0}, 199 {"Meta + E -> Launch Default Email", new int[]{META_KEY, KeyEvent.KEYCODE_E}, 200 KeyboardLogEvent.LAUNCH_DEFAULT_EMAIL, KeyEvent.KEYCODE_E, META_ON}, 201 {"ENVELOPE key -> Launch Default Email", new int[]{KeyEvent.KEYCODE_ENVELOPE}, 202 KeyboardLogEvent.LAUNCH_DEFAULT_EMAIL, KeyEvent.KEYCODE_ENVELOPE, 0}, 203 {"Meta + K -> Launch Default Calendar", new int[]{META_KEY, KeyEvent.KEYCODE_K}, 204 KeyboardLogEvent.LAUNCH_DEFAULT_CALENDAR, KeyEvent.KEYCODE_K, META_ON}, 205 {"CALENDAR key -> Launch Default Calendar", new int[]{KeyEvent.KEYCODE_CALENDAR}, 206 KeyboardLogEvent.LAUNCH_DEFAULT_CALENDAR, KeyEvent.KEYCODE_CALENDAR, 0}, 207 {"Meta + P -> Launch Default Music", new int[]{META_KEY, KeyEvent.KEYCODE_P}, 208 KeyboardLogEvent.LAUNCH_DEFAULT_MUSIC, KeyEvent.KEYCODE_P, META_ON}, 209 {"MUSIC key -> Launch Default Music", new int[]{KeyEvent.KEYCODE_MUSIC}, 210 KeyboardLogEvent.LAUNCH_DEFAULT_MUSIC, KeyEvent.KEYCODE_MUSIC, 0}, 211 {"Meta + U -> Launch Default Calculator", new int[]{META_KEY, KeyEvent.KEYCODE_U}, 212 KeyboardLogEvent.LAUNCH_DEFAULT_CALCULATOR, KeyEvent.KEYCODE_U, META_ON}, 213 {"CALCULATOR key -> Launch Default Calculator", 214 new int[]{KeyEvent.KEYCODE_CALCULATOR}, 215 KeyboardLogEvent.LAUNCH_DEFAULT_CALCULATOR, KeyEvent.KEYCODE_CALCULATOR, 0}, 216 {"Meta + M -> Launch Default Maps", new int[]{META_KEY, KeyEvent.KEYCODE_M}, 217 KeyboardLogEvent.LAUNCH_DEFAULT_MAPS, KeyEvent.KEYCODE_M, META_ON}, 218 {"Meta + S -> Launch Default Messaging App", 219 new int[]{META_KEY, KeyEvent.KEYCODE_S}, 220 KeyboardLogEvent.LAUNCH_DEFAULT_MESSAGING, KeyEvent.KEYCODE_S, META_ON}}; 221 } 222 223 @Before 224 @Override setUp()225 public void setUp() { 226 super.setUp(); 227 mPhoneWindowManager.overrideKeyEventSource(VENDOR_ID, PRODUCT_ID); 228 mPhoneWindowManager.overrideLaunchHome(); 229 mPhoneWindowManager.overrideSearchKeyBehavior( 230 PhoneWindowManager.SEARCH_BEHAVIOR_TARGET_ACTIVITY); 231 mPhoneWindowManager.overrideEnableBugReportTrigger(true); 232 mPhoneWindowManager.overrideStatusBarManagerInternal(); 233 mPhoneWindowManager.overrideStartActivity(); 234 mPhoneWindowManager.overrideUserSetupComplete(); 235 } 236 237 @Test 238 @Parameters(method = "shortcutTestArguments") testShortcuts(String testName, int[] testKeys, KeyboardLogEvent expectedLogEvent, int expectedKey, int expectedModifierState)239 public void testShortcuts(String testName, int[] testKeys, KeyboardLogEvent expectedLogEvent, 240 int expectedKey, int expectedModifierState) { 241 sendKeyCombination(testKeys, 0); 242 mPhoneWindowManager.assertShortcutLogged(VENDOR_ID, PRODUCT_ID, expectedLogEvent, 243 expectedKey, expectedModifierState, "Failed while executing " + testName); 244 } 245 } 246