KeyboardStateTestsBase.java revision a5c96f376ad57e78a88942bb618e067054ed818a
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(NO_AUTO_CAPS);
33
34        loadKeyboard(ALPHABET_UNSHIFTED);
35    }
36
37    public void setAutoCapsMode(boolean 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, int afterPress) {
68        mSwitcher.onPressKey(code);
69        assertLayout(afterPress, mSwitcher.getLayoutId());
70    }
71
72    public void pressKey(int code, int afterPress) {
73        mSwitcher.expireDoubleTapTimeout();
74        pressKeyWithoutTimerExpire(code, 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        pressKey(code, afterPress);
90    }
91
92    public void chordingReleaseKey(int code, int afterRelease) {
93        mSwitcher.onCodeInput(code, MULTI);
94        mSwitcher.onReleaseKey(code, NOT_SLIDING);
95        assertLayout(afterRelease, mSwitcher.getLayoutId());
96    }
97
98    public void chordingPressAndReleaseKey(int code, int afterPress, int afterRelease) {
99        chordingPressKey(code, afterPress);
100        chordingReleaseKey(code, afterRelease);
101    }
102
103    public void pressAndSlideFromKey(int code, int afterPress, int afterSlide) {
104        pressKey(code, afterPress);
105        mSwitcher.onReleaseKey(code, SLIDING);
106        assertLayout(afterSlide, mSwitcher.getLayoutId());
107    }
108
109    public void longPressAndReleaseKey(int code, int afterPress, int afterLongPress) {
110        pressKey(code, afterPress);
111        mSwitcher.onLongPressTimeout(code);
112        assertLayout(afterLongPress, mSwitcher.getLayoutId());
113        releaseKey(code, afterLongPress);
114    }
115
116    public void secondPressAndReleaseKey(int code, int afterPress, int afterRelease) {
117        pressKeyWithoutTimerExpire(code, afterPress);
118        releaseKey(code, afterRelease);
119    }
120}
121