1/* 2 * Copyright (C) 2014 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 17package com.google.android.apps.common.testing.ui.espresso.action; 18 19import static com.google.common.base.Preconditions.checkState; 20 21import android.os.Build; 22import android.view.KeyEvent; 23 24/** 25 * Class that wraps the key code and meta state of the desired key press. 26 */ 27public final class EspressoKey { 28 private final int keyCode; 29 private final int metaState; 30 31 private EspressoKey(Builder builder) { 32 this.keyCode = builder.builderKeyCode; 33 this.metaState = builder.getMetaState(); 34 } 35 36 public int getKeyCode() { 37 return keyCode; 38 } 39 40 public int getMetaState() { 41 return metaState; 42 } 43 44 @Override 45 public String toString() { 46 return String.format("keyCode: %s, metaState: %s", keyCode, metaState); 47 } 48 49 /** 50 * Builder for the EspressoKey class. 51 */ 52 public static class Builder { 53 private int builderKeyCode = -1; 54 private boolean isShiftPressed; 55 private boolean isAltPressed; 56 private boolean isCtrlPressed; 57 58 public Builder withKeyCode(int keyCode) { 59 builderKeyCode = keyCode; 60 return this; 61 } 62 63 /** 64 * Sets the SHIFT_ON meta state of the resulting key. 65 */ 66 public Builder withShiftPressed(boolean shiftPressed) { 67 isShiftPressed = shiftPressed; 68 return this; 69 } 70 71 /** 72 * On Honeycomb and above, sets the CTRL_ON meta state of the resulting key. On Gingerbread and 73 * below, this is a noop. 74 */ 75 public Builder withCtrlPressed(boolean ctrlPressed) { 76 isCtrlPressed = ctrlPressed; 77 return this; 78 } 79 80 /** 81 * Sets the ALT_ON meta state of the resulting key. 82 */ 83 public Builder withAltPressed(boolean altPressed) { 84 isAltPressed = altPressed; 85 return this; 86 } 87 88 private int getMetaState() { 89 int metaState = 0; 90 if (isShiftPressed) { 91 metaState |= KeyEvent.META_SHIFT_ON; 92 } 93 94 if (isAltPressed) { 95 metaState |= KeyEvent.META_ALT_ON; 96 } 97 98 if (isCtrlPressed && Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { 99 metaState |= KeyEvent.META_CTRL_ON; 100 } 101 102 return metaState; 103 } 104 105 public EspressoKey build() { 106 checkState(builderKeyCode > 0 && builderKeyCode < KeyEvent.getMaxKeyCode(), 107 "Invalid key code: %s", builderKeyCode); 108 return new EspressoKey(this); 109 } 110 } 111} 112