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 static com.android.server.policy.PhoneWindowManager.DOUBLE_TAP_HOME_RECENT_SYSTEM_UI;
20 import static com.android.server.policy.PhoneWindowManager.LONG_PRESS_HOME_ALL_APPS;
21 import static com.android.server.policy.PhoneWindowManager.LONG_PRESS_HOME_ASSIST;
22 import static com.android.server.policy.PhoneWindowManager.LONG_PRESS_HOME_NOTIFICATION_PANEL;
23 import static com.android.server.policy.PhoneWindowManager.SETTINGS_KEY_BEHAVIOR_NOTIFICATION_PANEL;
24 
25 import android.platform.test.annotations.Presubmit;
26 import android.platform.test.annotations.RequiresFlagsEnabled;
27 import android.platform.test.flag.junit.CheckFlagsRule;
28 import android.platform.test.flag.junit.DeviceFlagsValueProvider;
29 import android.view.KeyEvent;
30 
31 import androidx.test.filters.MediumTest;
32 
33 import com.android.internal.annotations.Keep;
34 import com.android.server.input.KeyboardMetricsCollector.KeyboardLogEvent;
35 
36 import junitparams.JUnitParamsRunner;
37 import junitparams.Parameters;
38 
39 import org.junit.Before;
40 import org.junit.Rule;
41 import org.junit.Test;
42 import org.junit.runner.RunWith;
43 
44 @Presubmit
45 @MediumTest
46 @RunWith(JUnitParamsRunner.class)
47 public class ShortcutLoggingTests extends ShortcutKeyTestBase {
48 
49     @Rule
50     public final CheckFlagsRule mCheckFlagsRule =
51             DeviceFlagsValueProvider.createCheckFlagsRule();
52 
53     private static final int VENDOR_ID = 0x123;
54     private static final int PRODUCT_ID = 0x456;
55     private static final int DEVICE_BUS = 0x789;
56     private static final int META_KEY = KeyEvent.KEYCODE_META_LEFT;
57     private static final int META_ON = MODIFIER.get(KeyEvent.KEYCODE_META_LEFT);
58     private static final int ALT_KEY = KeyEvent.KEYCODE_ALT_LEFT;
59     private static final int ALT_ON = MODIFIER.get(KeyEvent.KEYCODE_ALT_LEFT);
60     private static final int CTRL_KEY = KeyEvent.KEYCODE_CTRL_LEFT;
61     private static final int CTRL_ON = MODIFIER.get(KeyEvent.KEYCODE_CTRL_LEFT);
62     private static final int SHIFT_KEY = KeyEvent.KEYCODE_SHIFT_LEFT;
63     private static final int SHIFT_ON = MODIFIER.get(KeyEvent.KEYCODE_SHIFT_LEFT);
64 
65     @Keep
shortcutTestArguments()66     private static Object[][] shortcutTestArguments() {
67         // testName, testKeys, expectedLogEvent, expectedKey, expectedModifierState
68         return new Object[][]{
69                 {"Meta + H -> Open Home", new int[]{META_KEY, KeyEvent.KEYCODE_H},
70                         KeyboardLogEvent.HOME, KeyEvent.KEYCODE_H, META_ON},
71                 {"Meta + Enter -> Open Home", new int[]{META_KEY, KeyEvent.KEYCODE_ENTER},
72                         KeyboardLogEvent.HOME, KeyEvent.KEYCODE_ENTER, META_ON},
73                 {"HOME key -> Open Home", new int[]{KeyEvent.KEYCODE_HOME}, KeyboardLogEvent.HOME,
74                         KeyEvent.KEYCODE_HOME, 0},
75                 {"RECENT_APPS key -> Open Overview", new int[]{KeyEvent.KEYCODE_RECENT_APPS},
76                         KeyboardLogEvent.RECENT_APPS, KeyEvent.KEYCODE_RECENT_APPS, 0},
77                 {"Meta + Tab -> Open OVerview", new int[]{META_KEY, KeyEvent.KEYCODE_TAB},
78                         KeyboardLogEvent.RECENT_APPS, KeyEvent.KEYCODE_TAB, META_ON},
79                 {"Alt + Tab -> Open Overview", new int[]{ALT_KEY, KeyEvent.KEYCODE_TAB},
80                         KeyboardLogEvent.RECENT_APPS, KeyEvent.KEYCODE_TAB, ALT_ON},
81                 {"BACK key -> Go back", new int[]{KeyEvent.KEYCODE_BACK}, KeyboardLogEvent.BACK,
82                         KeyEvent.KEYCODE_BACK, 0},
83                 {"Meta + Escape -> Go back", new int[]{META_KEY, KeyEvent.KEYCODE_ESCAPE},
84                         KeyboardLogEvent.BACK, KeyEvent.KEYCODE_ESCAPE, META_ON},
85                 {"Meta + Left arrow -> Go back", new int[]{META_KEY, KeyEvent.KEYCODE_DPAD_LEFT},
86                         KeyboardLogEvent.BACK, KeyEvent.KEYCODE_DPAD_LEFT, META_ON},
87                 {"Meta + Del -> Go back", new int[]{META_KEY, KeyEvent.KEYCODE_DEL},
88                         KeyboardLogEvent.BACK, KeyEvent.KEYCODE_DEL, META_ON},
89                 {"APP_SWITCH key -> Open App switcher", new int[]{KeyEvent.KEYCODE_APP_SWITCH},
90                         KeyboardLogEvent.APP_SWITCH, KeyEvent.KEYCODE_APP_SWITCH, 0},
91                 {"ASSIST key -> Launch assistant", new int[]{KeyEvent.KEYCODE_ASSIST},
92                         KeyboardLogEvent.LAUNCH_ASSISTANT, KeyEvent.KEYCODE_ASSIST, 0},
93                 {"Meta + A -> Launch assistant", new int[]{META_KEY, KeyEvent.KEYCODE_A},
94                         KeyboardLogEvent.LAUNCH_ASSISTANT, KeyEvent.KEYCODE_A, META_ON},
95                 {"VOICE_ASSIST key -> Launch Voice Assistant",
96                         new int[]{KeyEvent.KEYCODE_VOICE_ASSIST},
97                         KeyboardLogEvent.LAUNCH_VOICE_ASSISTANT, KeyEvent.KEYCODE_VOICE_ASSIST, 0},
98                 {"Meta + I -> Launch System Settings", new int[]{META_KEY, KeyEvent.KEYCODE_I},
99                         KeyboardLogEvent.LAUNCH_SYSTEM_SETTINGS, KeyEvent.KEYCODE_I, META_ON},
100                 {"Meta + N -> Toggle Notification panel", new int[]{META_KEY, KeyEvent.KEYCODE_N},
101                         KeyboardLogEvent.TOGGLE_NOTIFICATION_PANEL, KeyEvent.KEYCODE_N, META_ON},
102                 {"NOTIFICATION key -> Toggle Notification Panel",
103                         new int[]{KeyEvent.KEYCODE_NOTIFICATION},
104                         KeyboardLogEvent.TOGGLE_NOTIFICATION_PANEL, KeyEvent.KEYCODE_NOTIFICATION,
105                         0},
106                 {"Meta + Ctrl + S -> Take Screenshot",
107                         new int[]{META_KEY, CTRL_KEY, KeyEvent.KEYCODE_S},
108                         KeyboardLogEvent.TAKE_SCREENSHOT, KeyEvent.KEYCODE_S, META_ON | CTRL_ON},
109                 {"Meta + / -> Open Shortcut Helper", new int[]{META_KEY, KeyEvent.KEYCODE_SLASH},
110                         KeyboardLogEvent.OPEN_SHORTCUT_HELPER, KeyEvent.KEYCODE_SLASH, META_ON},
111                 {"BRIGHTNESS_UP key -> Increase Brightness",
112                         new int[]{KeyEvent.KEYCODE_BRIGHTNESS_UP}, KeyboardLogEvent.BRIGHTNESS_UP,
113                         KeyEvent.KEYCODE_BRIGHTNESS_UP, 0},
114                 {"BRIGHTNESS_DOWN key -> Decrease Brightness",
115                         new int[]{KeyEvent.KEYCODE_BRIGHTNESS_DOWN},
116                         KeyboardLogEvent.BRIGHTNESS_DOWN, KeyEvent.KEYCODE_BRIGHTNESS_DOWN, 0},
117                 {"KEYBOARD_BACKLIGHT_UP key -> Increase Keyboard Backlight",
118                         new int[]{KeyEvent.KEYCODE_KEYBOARD_BACKLIGHT_UP},
119                         KeyboardLogEvent.KEYBOARD_BACKLIGHT_UP,
120                         KeyEvent.KEYCODE_KEYBOARD_BACKLIGHT_UP, 0},
121                 {"KEYBOARD_BACKLIGHT_DOWN key -> Decrease Keyboard Backlight",
122                         new int[]{KeyEvent.KEYCODE_KEYBOARD_BACKLIGHT_DOWN},
123                         KeyboardLogEvent.KEYBOARD_BACKLIGHT_DOWN,
124                         KeyEvent.KEYCODE_KEYBOARD_BACKLIGHT_DOWN, 0},
125                 {"KEYBOARD_BACKLIGHT_TOGGLE key -> Toggle Keyboard Backlight",
126                         new int[]{KeyEvent.KEYCODE_KEYBOARD_BACKLIGHT_TOGGLE},
127                         KeyboardLogEvent.KEYBOARD_BACKLIGHT_TOGGLE,
128                         KeyEvent.KEYCODE_KEYBOARD_BACKLIGHT_TOGGLE, 0},
129                 {"VOLUME_UP key -> Increase Volume", new int[]{KeyEvent.KEYCODE_VOLUME_UP},
130                         KeyboardLogEvent.VOLUME_UP, KeyEvent.KEYCODE_VOLUME_UP, 0},
131                 {"VOLUME_DOWN key -> Decrease Volume", new int[]{KeyEvent.KEYCODE_VOLUME_DOWN},
132                         KeyboardLogEvent.VOLUME_DOWN, KeyEvent.KEYCODE_VOLUME_DOWN, 0},
133                 {"VOLUME_MUTE key -> Mute Volume", new int[]{KeyEvent.KEYCODE_VOLUME_MUTE},
134                         KeyboardLogEvent.VOLUME_MUTE, KeyEvent.KEYCODE_VOLUME_MUTE, 0},
135                 {"ALL_APPS key -> Open App Drawer", new int[]{KeyEvent.KEYCODE_ALL_APPS},
136                         KeyboardLogEvent.ALL_APPS, KeyEvent.KEYCODE_ALL_APPS, 0},
137                 {"SEARCH key -> Launch Search Activity", new int[]{KeyEvent.KEYCODE_SEARCH},
138                         KeyboardLogEvent.LAUNCH_SEARCH, KeyEvent.KEYCODE_SEARCH, 0},
139                 {"LANGUAGE_SWITCH key -> Switch Keyboard Language",
140                         new int[]{KeyEvent.KEYCODE_LANGUAGE_SWITCH},
141                         KeyboardLogEvent.LANGUAGE_SWITCH, KeyEvent.KEYCODE_LANGUAGE_SWITCH, 0},
142                 {"META key -> Open App Drawer in Accessibility mode", new int[]{META_KEY},
143                         KeyboardLogEvent.ACCESSIBILITY_ALL_APPS, META_KEY, META_ON},
144                 {"Meta + Alt -> Toggle CapsLock", new int[]{META_KEY, ALT_KEY},
145                         KeyboardLogEvent.TOGGLE_CAPS_LOCK, ALT_KEY, META_ON | ALT_ON},
146                 {"Alt + Meta -> Toggle CapsLock", new int[]{ALT_KEY, META_KEY},
147                         KeyboardLogEvent.TOGGLE_CAPS_LOCK, META_KEY, META_ON | ALT_ON},
148                 {"CAPS_LOCK key -> Toggle CapsLock", new int[]{KeyEvent.KEYCODE_CAPS_LOCK},
149                         KeyboardLogEvent.TOGGLE_CAPS_LOCK, KeyEvent.KEYCODE_CAPS_LOCK, 0},
150                 {"MUTE key -> Mute System Microphone", new int[]{KeyEvent.KEYCODE_MUTE},
151                         KeyboardLogEvent.SYSTEM_MUTE, KeyEvent.KEYCODE_MUTE, 0},
152                 {"Meta + Ctrl + DPAD_UP -> Split screen navigation",
153                         new int[]{META_KEY, CTRL_KEY, KeyEvent.KEYCODE_DPAD_UP},
154                         KeyboardLogEvent.MULTI_WINDOW_NAVIGATION, KeyEvent.KEYCODE_DPAD_UP,
155                         META_ON | CTRL_ON},
156                 {"Meta + Ctrl + DPAD_LEFT -> Split screen navigation",
157                         new int[]{META_KEY, CTRL_KEY, KeyEvent.KEYCODE_DPAD_LEFT},
158                         KeyboardLogEvent.SPLIT_SCREEN_NAVIGATION, KeyEvent.KEYCODE_DPAD_LEFT,
159                         META_ON | CTRL_ON},
160                 {"Meta + Ctrl + DPAD_RIGHT -> Split screen navigation",
161                         new int[]{META_KEY, CTRL_KEY, KeyEvent.KEYCODE_DPAD_RIGHT},
162                         KeyboardLogEvent.SPLIT_SCREEN_NAVIGATION, KeyEvent.KEYCODE_DPAD_RIGHT,
163                         META_ON | CTRL_ON},
164                 {"Meta + L -> Lock Homescreen", new int[]{META_KEY, KeyEvent.KEYCODE_L},
165                         KeyboardLogEvent.LOCK_SCREEN, KeyEvent.KEYCODE_L, META_ON},
166                 {"Meta + Ctrl + N -> Open Notes", new int[]{META_KEY, CTRL_KEY, KeyEvent.KEYCODE_N},
167                         KeyboardLogEvent.OPEN_NOTES, KeyEvent.KEYCODE_N, META_ON | CTRL_ON},
168                 {"POWER key -> Toggle Power", new int[]{KeyEvent.KEYCODE_POWER},
169                         KeyboardLogEvent.TOGGLE_POWER, KeyEvent.KEYCODE_POWER, 0},
170                 {"TV_POWER key -> Toggle Power", new int[]{KeyEvent.KEYCODE_TV_POWER},
171                         KeyboardLogEvent.TOGGLE_POWER, KeyEvent.KEYCODE_TV_POWER, 0},
172                 {"SYSTEM_NAVIGATION_DOWN key -> System Navigation",
173                         new int[]{KeyEvent.KEYCODE_SYSTEM_NAVIGATION_DOWN},
174                         KeyboardLogEvent.SYSTEM_NAVIGATION, KeyEvent.KEYCODE_SYSTEM_NAVIGATION_DOWN,
175                         0},
176                 {"SYSTEM_NAVIGATION_UP key -> System Navigation",
177                         new int[]{KeyEvent.KEYCODE_SYSTEM_NAVIGATION_UP},
178                         KeyboardLogEvent.SYSTEM_NAVIGATION, KeyEvent.KEYCODE_SYSTEM_NAVIGATION_UP,
179                         0},
180                 {"SYSTEM_NAVIGATION_LEFT key -> System Navigation",
181                         new int[]{KeyEvent.KEYCODE_SYSTEM_NAVIGATION_LEFT},
182                         KeyboardLogEvent.SYSTEM_NAVIGATION, KeyEvent.KEYCODE_SYSTEM_NAVIGATION_LEFT,
183                         0},
184                 {"SYSTEM_NAVIGATION_RIGHT key -> System Navigation",
185                         new int[]{KeyEvent.KEYCODE_SYSTEM_NAVIGATION_RIGHT},
186                         KeyboardLogEvent.SYSTEM_NAVIGATION,
187                         KeyEvent.KEYCODE_SYSTEM_NAVIGATION_RIGHT, 0},
188                 {"SLEEP key -> System Sleep", new int[]{KeyEvent.KEYCODE_SLEEP},
189                         KeyboardLogEvent.SLEEP, KeyEvent.KEYCODE_SLEEP, 0},
190                 {"SOFT_SLEEP key -> System Sleep", new int[]{KeyEvent.KEYCODE_SOFT_SLEEP},
191                         KeyboardLogEvent.SLEEP, KeyEvent.KEYCODE_SOFT_SLEEP, 0},
192                 {"WAKEUP key -> System Wakeup", new int[]{KeyEvent.KEYCODE_WAKEUP},
193                         KeyboardLogEvent.WAKEUP, KeyEvent.KEYCODE_WAKEUP, 0},
194                 {"MEDIA_PLAY key -> Media Control", new int[]{KeyEvent.KEYCODE_MEDIA_PLAY},
195                         KeyboardLogEvent.MEDIA_KEY, KeyEvent.KEYCODE_MEDIA_PLAY, 0},
196                 {"MEDIA_PAUSE key -> Media Control", new int[]{KeyEvent.KEYCODE_MEDIA_PAUSE},
197                         KeyboardLogEvent.MEDIA_KEY, KeyEvent.KEYCODE_MEDIA_PAUSE, 0},
198                 {"MEDIA_PLAY_PAUSE key -> Media Control",
199                         new int[]{KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE}, KeyboardLogEvent.MEDIA_KEY,
200                         KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE, 0},
201                 {"Meta + B -> Launch Default Browser", new int[]{META_KEY, KeyEvent.KEYCODE_B},
202                         KeyboardLogEvent.LAUNCH_DEFAULT_BROWSER, KeyEvent.KEYCODE_B, META_ON},
203                 {"EXPLORER key -> Launch Default Browser", new int[]{KeyEvent.KEYCODE_EXPLORER},
204                         KeyboardLogEvent.LAUNCH_DEFAULT_BROWSER, KeyEvent.KEYCODE_EXPLORER, 0},
205                 {"Meta + C -> Launch Default Contacts", new int[]{META_KEY, KeyEvent.KEYCODE_C},
206                         KeyboardLogEvent.LAUNCH_DEFAULT_CONTACTS, KeyEvent.KEYCODE_C, META_ON},
207                 {"CONTACTS key -> Launch Default Contacts", new int[]{KeyEvent.KEYCODE_CONTACTS},
208                         KeyboardLogEvent.LAUNCH_DEFAULT_CONTACTS, KeyEvent.KEYCODE_CONTACTS, 0},
209                 {"Meta + E -> Launch Default Email", new int[]{META_KEY, KeyEvent.KEYCODE_E},
210                         KeyboardLogEvent.LAUNCH_DEFAULT_EMAIL, KeyEvent.KEYCODE_E, META_ON},
211                 {"ENVELOPE key -> Launch Default Email", new int[]{KeyEvent.KEYCODE_ENVELOPE},
212                         KeyboardLogEvent.LAUNCH_DEFAULT_EMAIL, KeyEvent.KEYCODE_ENVELOPE, 0},
213                 {"Meta + K -> Launch Default Calendar", new int[]{META_KEY, KeyEvent.KEYCODE_K},
214                         KeyboardLogEvent.LAUNCH_DEFAULT_CALENDAR, KeyEvent.KEYCODE_K, META_ON},
215                 {"CALENDAR key -> Launch Default Calendar", new int[]{KeyEvent.KEYCODE_CALENDAR},
216                         KeyboardLogEvent.LAUNCH_DEFAULT_CALENDAR, KeyEvent.KEYCODE_CALENDAR, 0},
217                 {"Meta + P -> Launch Default Music", new int[]{META_KEY, KeyEvent.KEYCODE_P},
218                         KeyboardLogEvent.LAUNCH_DEFAULT_MUSIC, KeyEvent.KEYCODE_P, META_ON},
219                 {"MUSIC key -> Launch Default Music", new int[]{KeyEvent.KEYCODE_MUSIC},
220                         KeyboardLogEvent.LAUNCH_DEFAULT_MUSIC, KeyEvent.KEYCODE_MUSIC, 0},
221                 {"Meta + U -> Launch Default Calculator", new int[]{META_KEY, KeyEvent.KEYCODE_U},
222                         KeyboardLogEvent.LAUNCH_DEFAULT_CALCULATOR, KeyEvent.KEYCODE_U, META_ON},
223                 {"CALCULATOR key -> Launch Default Calculator",
224                         new int[]{KeyEvent.KEYCODE_CALCULATOR},
225                         KeyboardLogEvent.LAUNCH_DEFAULT_CALCULATOR, KeyEvent.KEYCODE_CALCULATOR, 0},
226                 {"Meta + M -> Launch Default Maps", new int[]{META_KEY, KeyEvent.KEYCODE_M},
227                         KeyboardLogEvent.LAUNCH_DEFAULT_MAPS, KeyEvent.KEYCODE_M, META_ON},
228                 {"Meta + S -> Launch Default Messaging App",
229                         new int[]{META_KEY, KeyEvent.KEYCODE_S},
230                         KeyboardLogEvent.LAUNCH_DEFAULT_MESSAGING, KeyEvent.KEYCODE_S, META_ON},
231                 {"Meta + Ctrl + DPAD_DOWN -> Enter desktop mode",
232                         new int[]{META_KEY, CTRL_KEY, KeyEvent.KEYCODE_DPAD_DOWN},
233                         KeyboardLogEvent.DESKTOP_MODE, KeyEvent.KEYCODE_DPAD_DOWN,
234                         META_ON | CTRL_ON}};
235     }
236 
237     @Keep
longPressOnHomeTestArguments()238     private static Object[][] longPressOnHomeTestArguments() {
239         // testName, testKeys, longPressOnHomeBehavior, expectedLogEvent, expectedKey,
240         // expectedModifierState
241         return new Object[][]{
242                 {"Long press HOME key -> Toggle Notification panel",
243                         new int[]{KeyEvent.KEYCODE_HOME}, LONG_PRESS_HOME_NOTIFICATION_PANEL,
244                         KeyboardLogEvent.TOGGLE_NOTIFICATION_PANEL, KeyEvent.KEYCODE_HOME, 0},
245                 {"Long press META + ENTER -> Toggle Notification panel",
246                         new int[]{META_KEY, KeyEvent.KEYCODE_ENTER},
247                         LONG_PRESS_HOME_NOTIFICATION_PANEL,
248                         KeyboardLogEvent.TOGGLE_NOTIFICATION_PANEL, KeyEvent.KEYCODE_ENTER,
249                         META_ON},
250                 {"Long press META + H -> Toggle Notification panel",
251                         new int[]{META_KEY, KeyEvent.KEYCODE_H}, LONG_PRESS_HOME_NOTIFICATION_PANEL,
252                         KeyboardLogEvent.TOGGLE_NOTIFICATION_PANEL, KeyEvent.KEYCODE_H, META_ON},
253                 {"Long press HOME key -> Launch assistant",
254                         new int[]{KeyEvent.KEYCODE_HOME}, LONG_PRESS_HOME_ASSIST,
255                         KeyboardLogEvent.LAUNCH_ASSISTANT, KeyEvent.KEYCODE_HOME, 0},
256                 {"Long press META + ENTER -> Launch assistant",
257                         new int[]{META_KEY, KeyEvent.KEYCODE_ENTER}, LONG_PRESS_HOME_ASSIST,
258                         KeyboardLogEvent.LAUNCH_ASSISTANT, KeyEvent.KEYCODE_ENTER, META_ON},
259                 {"Long press META + H -> Launch assistant",
260                         new int[]{META_KEY, KeyEvent.KEYCODE_H}, LONG_PRESS_HOME_ASSIST,
261                         KeyboardLogEvent.LAUNCH_ASSISTANT, KeyEvent.KEYCODE_H, META_ON},
262                 {"Long press HOME key -> Open App Drawer",
263                         new int[]{KeyEvent.KEYCODE_HOME}, LONG_PRESS_HOME_ALL_APPS,
264                         KeyboardLogEvent.ALL_APPS, KeyEvent.KEYCODE_HOME, 0},
265                 {"Long press META + ENTER -> Open App Drawer",
266                         new int[]{META_KEY, KeyEvent.KEYCODE_ENTER}, LONG_PRESS_HOME_ALL_APPS,
267                         KeyboardLogEvent.ALL_APPS, KeyEvent.KEYCODE_ENTER, META_ON},
268                 {"Long press META + H -> Open App Drawer", new int[]{META_KEY, KeyEvent.KEYCODE_H},
269                         LONG_PRESS_HOME_ALL_APPS, KeyboardLogEvent.ALL_APPS,
270                         KeyEvent.KEYCODE_H, META_ON}};
271     }
272 
273     @Keep
doubleTapOnHomeTestArguments()274     private static Object[][] doubleTapOnHomeTestArguments() {
275         // testName, testKeys, doubleTapOnHomeBehavior, expectedLogEvent, expectedKey,
276         // expectedModifierState
277         return new Object[][]{
278                 {"Double tap HOME -> Open App switcher",
279                         new int[]{KeyEvent.KEYCODE_HOME}, DOUBLE_TAP_HOME_RECENT_SYSTEM_UI,
280                         KeyboardLogEvent.APP_SWITCH, KeyEvent.KEYCODE_HOME, 0},
281                 {"Double tap META + ENTER -> Open App switcher",
282                         new int[]{META_KEY, KeyEvent.KEYCODE_ENTER},
283                         DOUBLE_TAP_HOME_RECENT_SYSTEM_UI, KeyboardLogEvent.APP_SWITCH,
284                         KeyEvent.KEYCODE_ENTER, META_ON},
285                 {"Double tap META + H -> Open App switcher",
286                         new int[]{META_KEY, KeyEvent.KEYCODE_H}, DOUBLE_TAP_HOME_RECENT_SYSTEM_UI,
287                         KeyboardLogEvent.APP_SWITCH, KeyEvent.KEYCODE_H, META_ON}};
288     }
289 
290     @Keep
settingsKeyTestArguments()291     private static Object[][] settingsKeyTestArguments() {
292         // testName, testKeys, settingsKeyBehavior, expectedLogEvent, expectedKey,
293         // expectedModifierState
294         return new Object[][]{
295                 {"SETTINGS key -> Toggle Notification panel", new int[]{KeyEvent.KEYCODE_SETTINGS},
296                         SETTINGS_KEY_BEHAVIOR_NOTIFICATION_PANEL,
297                         KeyboardLogEvent.TOGGLE_NOTIFICATION_PANEL, KeyEvent.KEYCODE_SETTINGS, 0}};
298     }
299 
300     @Before
setUp()301     public void setUp() {
302         setUpPhoneWindowManager(/*supportSettingsUpdate*/ true);
303         mPhoneWindowManager.overrideKeyEventSource(VENDOR_ID, PRODUCT_ID, DEVICE_BUS);
304         mPhoneWindowManager.overrideLaunchHome();
305         mPhoneWindowManager.overrideSearchKeyBehavior(
306                 PhoneWindowManager.SEARCH_KEY_BEHAVIOR_TARGET_ACTIVITY);
307         mPhoneWindowManager.overrideEnableBugReportTrigger(true);
308         mPhoneWindowManager.overrideStatusBarManagerInternal();
309         mPhoneWindowManager.overrideStartActivity();
310         mPhoneWindowManager.overrideSendBroadcast();
311         mPhoneWindowManager.overrideUserSetupComplete();
312         mPhoneWindowManager.setupAssistForLaunch();
313         mPhoneWindowManager.overrideTogglePanel();
314         mPhoneWindowManager.overrideInjectKeyEvent();
315     }
316 
317     @Test
318     @Parameters(method = "shortcutTestArguments")
testShortcuts(String testName, int[] testKeys, KeyboardLogEvent expectedLogEvent, int expectedKey, int expectedModifierState)319     public void testShortcuts(String testName, int[] testKeys, KeyboardLogEvent expectedLogEvent,
320             int expectedKey, int expectedModifierState) {
321         sendKeyCombination(testKeys, 0 /* duration */);
322         mPhoneWindowManager.assertShortcutLogged(VENDOR_ID, PRODUCT_ID, expectedLogEvent,
323                 expectedKey, expectedModifierState, DEVICE_BUS,
324                 "Failed while executing " + testName);
325     }
326 
327     @Test
328     @Parameters(method = "longPressOnHomeTestArguments")
testLongPressOnHome(String testName, int[] testKeys, int longPressOnHomeBehavior, KeyboardLogEvent expectedLogEvent, int expectedKey, int expectedModifierState)329     public void testLongPressOnHome(String testName, int[] testKeys, int longPressOnHomeBehavior,
330             KeyboardLogEvent expectedLogEvent, int expectedKey, int expectedModifierState) {
331         mPhoneWindowManager.overrideLongPressOnHomeBehavior(longPressOnHomeBehavior);
332         sendLongPressKeyCombination(testKeys);
333         mPhoneWindowManager.assertShortcutLogged(VENDOR_ID, PRODUCT_ID, expectedLogEvent,
334                 expectedKey, expectedModifierState, DEVICE_BUS,
335                 "Failed while executing " + testName);
336     }
337 
338     @Test
339     @Parameters(method = "doubleTapOnHomeTestArguments")
testDoubleTapOnHomeBehavior(String testName, int[] testKeys, int doubleTapOnHomeBehavior, KeyboardLogEvent expectedLogEvent, int expectedKey, int expectedModifierState)340     public void testDoubleTapOnHomeBehavior(String testName, int[] testKeys,
341             int doubleTapOnHomeBehavior, KeyboardLogEvent expectedLogEvent, int expectedKey,
342             int expectedModifierState) {
343         mPhoneWindowManager.overriderDoubleTapOnHomeBehavior(doubleTapOnHomeBehavior);
344         sendKeyCombination(testKeys, 0 /* duration */);
345         sendKeyCombination(testKeys, 0 /* duration */);
346         mPhoneWindowManager.assertShortcutLogged(VENDOR_ID, PRODUCT_ID, expectedLogEvent,
347                 expectedKey, expectedModifierState, DEVICE_BUS,
348                 "Failed while executing " + testName);
349     }
350 
351     @Test
352     @Parameters(method = "settingsKeyTestArguments")
testSettingsKey(String testName, int[] testKeys, int settingsKeyBehavior, KeyboardLogEvent expectedLogEvent, int expectedKey, int expectedModifierState)353     public void testSettingsKey(String testName, int[] testKeys,
354             int settingsKeyBehavior, KeyboardLogEvent expectedLogEvent, int expectedKey,
355             int expectedModifierState) {
356         mPhoneWindowManager.overrideSettingsKeyBehavior(settingsKeyBehavior);
357         sendKeyCombination(testKeys, 0 /* duration */);
358         mPhoneWindowManager.assertShortcutLogged(VENDOR_ID, PRODUCT_ID, expectedLogEvent,
359                 expectedKey, expectedModifierState, DEVICE_BUS,
360                 "Failed while executing " + testName);
361     }
362 
363     @Test
364     @RequiresFlagsEnabled(com.android.server.flags.Flags.FLAG_NEW_BUGREPORT_KEYBOARD_SHORTCUT)
testBugreportShortcutPress()365     public void testBugreportShortcutPress() {
366         sendKeyCombination(new int[]{META_KEY, CTRL_KEY, KeyEvent.KEYCODE_DEL}, 0);
367         mPhoneWindowManager.assertShortcutLogged(VENDOR_ID, PRODUCT_ID,
368                 KeyboardLogEvent.TRIGGER_BUG_REPORT, KeyEvent.KEYCODE_DEL, META_ON | CTRL_ON,
369                 DEVICE_BUS, "Failed to log bugreport shortcut.");
370     }
371 }
372