KeyboardStateTestsBase.java revision 16950d65c323f99507d97cb7f0403dc653e2506c
1/* 2 * Copyright (C) 2012 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 * use this file except in compliance with the License. You may obtain a copy of 6 * 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, WITHOUT 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 * License for the specific language governing permissions and limitations under 14 * the License. 15 */ 16 17package com.android.inputmethod.keyboard.internal; 18 19import android.test.AndroidTestCase; 20 21public class KeyboardStateTestsBase extends AndroidTestCase 22 implements MockKeyboardSwitcher.Constants { 23 protected MockKeyboardSwitcher mSwitcher; 24 25 private String mLayoutSwitchBackSymbols = ""; 26 27 @Override 28 protected void setUp() throws Exception { 29 super.setUp(); 30 31 mSwitcher = new MockKeyboardSwitcher(); 32 mSwitcher.setAutoCapsMode(CAP_MODE_OFF); 33 34 loadKeyboard(ALPHABET_UNSHIFTED); 35 } 36 37 public void setAutoCapsMode(int autoCaps) { 38 mSwitcher.setAutoCapsMode(autoCaps); 39 } 40 41 public void setLayoutSwitchBackSymbols(String switchBackSymbols) { 42 mLayoutSwitchBackSymbols = switchBackSymbols; 43 } 44 45 private static void assertLayout(int expected, int actual) { 46 assertTrue("expected=" + MockKeyboardSwitcher.getLayoutName(expected) 47 + " actual=" + MockKeyboardSwitcher.getLayoutName(actual), 48 expected == actual); 49 } 50 51 public void updateShiftState(int afterUpdate) { 52 mSwitcher.updateShiftState(); 53 assertLayout(afterUpdate, mSwitcher.getLayoutId()); 54 } 55 56 public void loadKeyboard(int afterLoad) { 57 mSwitcher.loadKeyboard(mLayoutSwitchBackSymbols); 58 updateShiftState(afterLoad); 59 } 60 61 public void rotateDevice(int afterRotate) { 62 mSwitcher.saveKeyboardState(); 63 mSwitcher.loadKeyboard(mLayoutSwitchBackSymbols); 64 assertLayout(afterRotate, mSwitcher.getLayoutId()); 65 } 66 67 private void pressKeyWithoutTimerExpire(int code, boolean isSinglePointer, int afterPress) { 68 mSwitcher.onPressKey(code, isSinglePointer); 69 assertLayout(afterPress, mSwitcher.getLayoutId()); 70 } 71 72 public void pressKey(int code, int afterPress) { 73 mSwitcher.expireDoubleTapTimeout(); 74 pressKeyWithoutTimerExpire(code, true, afterPress); 75 } 76 77 public void releaseKey(int code, int afterRelease) { 78 mSwitcher.onCodeInput(code, SINGLE); 79 mSwitcher.onReleaseKey(code, NOT_SLIDING); 80 assertLayout(afterRelease, mSwitcher.getLayoutId()); 81 } 82 83 public void pressAndReleaseKey(int code, int afterPress, int afterRelease) { 84 pressKey(code, afterPress); 85 releaseKey(code, afterRelease); 86 } 87 88 public void chordingPressKey(int code, int afterPress) { 89 mSwitcher.expireDoubleTapTimeout(); 90 pressKeyWithoutTimerExpire(code, false, afterPress); 91 } 92 93 public void chordingReleaseKey(int code, int afterRelease) { 94 mSwitcher.onCodeInput(code, MULTI); 95 mSwitcher.onReleaseKey(code, NOT_SLIDING); 96 assertLayout(afterRelease, mSwitcher.getLayoutId()); 97 } 98 99 public void chordingPressAndReleaseKey(int code, int afterPress, int afterRelease) { 100 chordingPressKey(code, afterPress); 101 chordingReleaseKey(code, afterRelease); 102 } 103 104 public void pressAndSlideFromKey(int code, int afterPress, int afterSlide) { 105 pressKey(code, afterPress); 106 mSwitcher.onReleaseKey(code, SLIDING); 107 assertLayout(afterSlide, mSwitcher.getLayoutId()); 108 } 109 110 public void longPressAndReleaseKey(int code, int afterPress, int afterLongPress) { 111 pressKey(code, afterPress); 112 mSwitcher.onLongPressTimeout(code); 113 assertLayout(afterLongPress, mSwitcher.getLayoutId()); 114 releaseKey(code, afterLongPress); 115 } 116 117 public void secondPressAndReleaseKey(int code, int afterPress, int afterRelease) { 118 pressKeyWithoutTimerExpire(code, true, afterPress); 119 releaseKey(code, afterRelease); 120 } 121} 122