• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2015 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 com.android.tv.testing.uihelper;
17 
18 import android.support.test.uiautomator.Direction;
19 import android.support.test.uiautomator.UiDevice;
20 import android.view.KeyEvent;
21 
22 /** Static utility methods for {@link UiDevice}. */
23 public final class UiDeviceUtils {
24 
pressDpad(UiDevice uiDevice, Direction direction)25     public static void pressDpad(UiDevice uiDevice, Direction direction) {
26         switch (direction) {
27             case UP:
28                 uiDevice.pressDPadUp();
29                 break;
30             case DOWN:
31                 uiDevice.pressDPadDown();
32                 break;
33             case LEFT:
34                 uiDevice.pressDPadLeft();
35                 break;
36             case RIGHT:
37                 uiDevice.pressDPadRight();
38                 break;
39             default:
40                 throw new IllegalArgumentException(direction.toString());
41         }
42     }
43 
pressKeys(UiDevice uiDevice, int... keyCodes)44     public static void pressKeys(UiDevice uiDevice, int... keyCodes) {
45         for (int k : keyCodes) {
46             uiDevice.pressKeyCode(k);
47         }
48     }
49 
50     /**
51      * Parses the string and sends the corresponding individual key presses.
52      *
53      * <p><b>Note:</b> only handles 0-9, '.', and '-'.
54      */
pressKeys(UiDevice uiDevice, String keys)55     public static void pressKeys(UiDevice uiDevice, String keys) {
56         for (char c : keys.toCharArray()) {
57             if (c >= '0' && c <= '9') {
58                 uiDevice.pressKeyCode(KeyEvent.KEYCODE_0 + c - '0');
59             } else if (c == '-') {
60                 uiDevice.pressKeyCode(KeyEvent.KEYCODE_MINUS);
61             } else if (c == '.') {
62                 uiDevice.pressKeyCode(KeyEvent.KEYCODE_PERIOD);
63             } else {
64                 throw new IllegalArgumentException(c + " is not supported");
65             }
66         }
67     }
68 
UiDeviceUtils()69     private UiDeviceUtils() {}
70 }
71