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(String message, int expected, int actual) {
46        assertTrue(message + ": 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", afterUpdate, mSwitcher.getLayoutId());
54    }
55
56    public void loadKeyboard(int afterLoad) {
57        mSwitcher.loadKeyboard(mLayoutSwitchBackSymbols);
58        mSwitcher.updateShiftState();
59        assertLayout("afterLoad", afterLoad, mSwitcher.getLayoutId());
60    }
61
62    public void rotateDevice(int afterRotate) {
63        mSwitcher.saveKeyboardState();
64        mSwitcher.loadKeyboard(mLayoutSwitchBackSymbols);
65        assertLayout("afterRotate", afterRotate, mSwitcher.getLayoutId());
66    }
67
68    private void pressKeyWithoutTimerExpire(int code, boolean isSinglePointer, int afterPress) {
69        mSwitcher.onPressKey(code, isSinglePointer);
70        assertLayout("afterPress", afterPress, mSwitcher.getLayoutId());
71    }
72
73    public void pressKey(int code, int afterPress) {
74        mSwitcher.expireDoubleTapTimeout();
75        pressKeyWithoutTimerExpire(code, true, afterPress);
76    }
77
78    public void releaseKey(int code, int afterRelease) {
79        mSwitcher.onCodeInput(code, SINGLE);
80        mSwitcher.onReleaseKey(code, NOT_SLIDING);
81        assertLayout("afterRelease", afterRelease, mSwitcher.getLayoutId());
82    }
83
84    public void pressAndReleaseKey(int code, int afterPress, int afterRelease) {
85        pressKey(code, afterPress);
86        releaseKey(code, afterRelease);
87    }
88
89    public void chordingPressKey(int code, int afterPress) {
90        mSwitcher.expireDoubleTapTimeout();
91        pressKeyWithoutTimerExpire(code, false, afterPress);
92    }
93
94    public void chordingReleaseKey(int code, int afterRelease) {
95        mSwitcher.onCodeInput(code, MULTI);
96        mSwitcher.onReleaseKey(code, NOT_SLIDING);
97        assertLayout("afterRelease", afterRelease, mSwitcher.getLayoutId());
98    }
99
100    public void chordingPressAndReleaseKey(int code, int afterPress, int afterRelease) {
101        chordingPressKey(code, afterPress);
102        chordingReleaseKey(code, afterRelease);
103    }
104
105    public void pressAndSlideFromKey(int code, int afterPress, int afterSlide) {
106        pressKey(code, afterPress);
107        mSwitcher.onReleaseKey(code, SLIDING);
108        assertLayout("afterSlide", afterSlide, mSwitcher.getLayoutId());
109    }
110
111    public void longPressKey(int code, int afterPress, int afterLongPress) {
112        pressKey(code, afterPress);
113        mSwitcher.onLongPressTimeout(code);
114        assertLayout("afterLongPress", afterLongPress, mSwitcher.getLayoutId());
115    }
116
117    public void longPressAndReleaseKey(int code, int afterPress, int afterLongPress,
118            int afterRelease) {
119        longPressKey(code, afterPress, afterLongPress);
120        releaseKey(code, afterRelease);
121    }
122
123    public void secondPressKey(int code, int afterPress) {
124        pressKeyWithoutTimerExpire(code, true, afterPress);
125    }
126
127    public void secondPressAndReleaseKey(int code, int afterPress, int afterRelease) {
128        secondPressKey(code, afterPress);
129        releaseKey(code, afterRelease);
130    }
131}
132