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