1 /* 2 * Copyright (C) 2016 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.platform.test.utils; 18 19 import android.app.Instrumentation; 20 import android.os.SystemClock; 21 import android.support.test.uiautomator.Direction; 22 import android.support.test.uiautomator.EventCondition; 23 import android.support.test.uiautomator.UiDevice; 24 import android.util.Log; 25 import android.view.KeyEvent; 26 27 import java.io.IOException; 28 29 30 public class DPadUtil { 31 32 private static final String TAG = DPadUtil.class.getSimpleName(); 33 private static final long DPAD_DEFAULT_WAIT_TIME_MS = 1000; // 1 sec 34 private UiDevice mDevice; 35 36 DPadUtil(Instrumentation instrumentation)37 public DPadUtil(Instrumentation instrumentation) { 38 mDevice = UiDevice.getInstance(instrumentation); 39 } 40 DPadUtil(UiDevice uiDevice)41 public DPadUtil(UiDevice uiDevice) { 42 mDevice = uiDevice; 43 } 44 setUiDevice(UiDevice uiDevice)45 public void setUiDevice(UiDevice uiDevice) { 46 mDevice = uiDevice; 47 } 48 pressDPad(Direction direction)49 public boolean pressDPad(Direction direction) { 50 return pressDPad(direction, 1, DPAD_DEFAULT_WAIT_TIME_MS); 51 } 52 pressDPad(Direction direction, long repeat)53 public void pressDPad(Direction direction, long repeat) { 54 pressDPad(direction, repeat, DPAD_DEFAULT_WAIT_TIME_MS); 55 } 56 57 /** 58 * Presses DPad button of the same direction for the count times. 59 * It sleeps between each press for DPAD_DEFAULT_WAIT_TIME_MS. 60 * 61 * @param direction the direction of the button to press. 62 * @param repeat the number of times to press the button. 63 * @param timeout the timeout for the wait. 64 * @return true if the last key simulation is successful, else return false 65 */ pressDPad(Direction direction, long repeat, long timeout)66 public boolean pressDPad(Direction direction, long repeat, long timeout) { 67 int iteration = 0; 68 boolean result = false; 69 while (iteration++ < repeat) { 70 switch (direction) { 71 case LEFT: 72 result = mDevice.pressDPadLeft(); 73 break; 74 case RIGHT: 75 result = mDevice.pressDPadRight(); 76 break; 77 case UP: 78 result = mDevice.pressDPadUp(); 79 break; 80 case DOWN: 81 result = mDevice.pressDPadDown(); 82 break; 83 } 84 SystemClock.sleep(timeout); 85 } 86 return result; 87 } 88 pressDPadLeft()89 public boolean pressDPadLeft() { 90 return mDevice.pressDPadLeft(); 91 } 92 pressDPadRight()93 public boolean pressDPadRight() { 94 return mDevice.pressDPadRight(); 95 } 96 pressDPadUp()97 public boolean pressDPadUp() { 98 return mDevice.pressDPadUp(); 99 } 100 pressDPadDown()101 public boolean pressDPadDown() { 102 return mDevice.pressDPadDown(); 103 } 104 pressHome()105 public boolean pressHome() { 106 return mDevice.pressHome(); 107 } 108 pressBack()109 public boolean pressBack() { 110 return mDevice.pressBack(); 111 } 112 pressDPadCenter()113 public boolean pressDPadCenter() { 114 return mDevice.pressDPadCenter(); 115 } 116 pressEnter()117 public boolean pressEnter() { 118 return mDevice.pressEnter(); 119 } 120 pressPipKey()121 public boolean pressPipKey() { 122 return mDevice.pressKeyCode(KeyEvent.KEYCODE_WINDOW); 123 } 124 pressKeyCode(int keyCode)125 public boolean pressKeyCode(int keyCode) { 126 return mDevice.pressKeyCode(keyCode); 127 } 128 longPressKeyCode(int keyCode)129 public boolean longPressKeyCode(int keyCode) { 130 try { 131 mDevice.executeShellCommand(String.format("input keyevent --longpress %d", keyCode)); 132 return true; 133 } catch (IOException e) { 134 // Ignore 135 Log.w(TAG, String.format("Failed to long press the key code: %d", keyCode)); 136 return false; 137 } 138 } 139 140 /** 141 * Press the key code, and waits for the given condition to become true. 142 * @param condition 143 * @param keyCode 144 * @param timeout 145 * @param <R> 146 * @return 147 */ pressKeyCodeAndWait(int keyCode, EventCondition<R> condition, long timeout)148 public <R> R pressKeyCodeAndWait(int keyCode, EventCondition<R> condition, long timeout) { 149 return mDevice.performActionAndWait(new KeyEventRunnable(keyCode), condition, timeout); 150 } 151 pressDPadCenterAndWait(EventCondition<R> condition, long timeout)152 public <R> R pressDPadCenterAndWait(EventCondition<R> condition, long timeout) { 153 return mDevice.performActionAndWait(new KeyEventRunnable(KeyEvent.KEYCODE_DPAD_CENTER), 154 condition, timeout); 155 } 156 pressEnterAndWait(EventCondition<R> condition, long timeout)157 public <R> R pressEnterAndWait(EventCondition<R> condition, long timeout) { 158 return mDevice.performActionAndWait(new KeyEventRunnable(KeyEvent.KEYCODE_ENTER), 159 condition, timeout); 160 } 161 162 private class KeyEventRunnable implements Runnable { 163 private int mKeyCode; KeyEventRunnable(int keyCode)164 public KeyEventRunnable(int keyCode) { 165 mKeyCode = keyCode; 166 } 167 @Override run()168 public void run() { 169 mDevice.pressKeyCode(mKeyCode); 170 } 171 } 172 } 173