1 /* 2 * Copyright (C) 2007 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.util; 18 19 import android.app.Instrumentation; 20 import android.os.SystemClock; 21 import android.test.ActivityInstrumentationTestCase; 22 import android.test.InstrumentationTestCase; 23 import android.view.Gravity; 24 import android.view.KeyCharacterMap; 25 import android.view.KeyEvent; 26 import android.view.MotionEvent; 27 import android.view.View; 28 import android.view.ViewConfiguration; 29 import android.view.ViewGroup; 30 31 /** 32 * Reusable methods for generating key events. 33 * <p> 34 * Definitions: 35 * <li> Tap refers to pushing and releasing a button (down and up event). 36 * <li> Chord refers to pushing a modifier key, tapping a regular key, and 37 * releasing the modifier key. 38 */ 39 public class KeyUtils { 40 /** 41 * Simulates tapping the menu key. 42 * 43 * @param test The test case that is being run. 44 */ 45 public static void tapMenuKey(ActivityInstrumentationTestCase test) { 46 final Instrumentation inst = test.getInstrumentation(); 47 48 inst.sendKeySync(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MENU)); 49 inst.sendKeySync(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_MENU)); 50 } 51 52 /** 53 * Simulates chording the menu key. 54 * 55 * @param test The test case that is being run. 56 * @param shortcutKey The shortcut key to tap while chording the menu key. 57 */ 58 public static void chordMenuKey(ActivityInstrumentationTestCase test, char shortcutKey) { 59 final Instrumentation inst = test.getInstrumentation(); 60 61 final KeyEvent pushMenuKey = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MENU); 62 final KeyCharacterMap keyCharMap = KeyCharacterMap.load(pushMenuKey.getDeviceId()); 63 final KeyEvent shortcutKeyEvent = keyCharMap.getEvents(new char[] { shortcutKey })[0]; 64 final int shortcutKeyCode = shortcutKeyEvent.getKeyCode(); 65 66 inst.sendKeySync(pushMenuKey); 67 inst.sendKeySync(new KeyEvent(KeyEvent.ACTION_DOWN, shortcutKeyCode)); 68 inst.sendKeySync(new KeyEvent(KeyEvent.ACTION_UP, shortcutKeyCode)); 69 inst.sendKeySync(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_MENU)); 70 } 71 72 /** 73 * Simulates a long click via the keyboard. 74 * 75 * @param test The test case that is being run. 76 */ 77 public static void longClick(ActivityInstrumentationTestCase test) { 78 final Instrumentation inst = test.getInstrumentation(); 79 80 inst.sendKeySync(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DPAD_CENTER)); 81 try { 82 Thread.sleep((long)(ViewConfiguration.getLongPressTimeout() * 1.5f)); 83 } catch (InterruptedException e) { 84 e.printStackTrace(); 85 } 86 inst.sendKeySync(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_DPAD_CENTER)); 87 } 88 } 89