1/*
2 * Copyright (C) 2012 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.android.inputmethod.keyboard.internal;
18
19import android.test.AndroidTestCase;
20
21public class KeyboardStateTestsBase extends AndroidTestCase
22        implements MockKeyboardSwitcher.MockConstants {
23    protected MockKeyboardSwitcher mSwitcher;
24
25    @Override
26    protected void setUp() throws Exception {
27        super.setUp();
28
29        mSwitcher = new MockKeyboardSwitcher();
30        mSwitcher.setAutoCapsMode(CAP_MODE_OFF);
31
32        loadKeyboard(ALPHABET_UNSHIFTED);
33    }
34
35    public void setAutoCapsMode(final int autoCaps) {
36        mSwitcher.setAutoCapsMode(autoCaps);
37    }
38
39    private static void assertLayout(final String message, final int expected, final int actual) {
40        assertTrue(message + ": expected=" + MockKeyboardSwitcher.getLayoutName(expected)
41                + " actual=" + MockKeyboardSwitcher.getLayoutName(actual),
42                expected == actual);
43    }
44
45    public void updateShiftState(final int afterUpdate) {
46        mSwitcher.updateShiftState();
47        assertLayout("afterUpdate", afterUpdate, mSwitcher.getLayoutId());
48    }
49
50    public void loadKeyboard(final int afterLoad) {
51        mSwitcher.loadKeyboard();
52        mSwitcher.updateShiftState();
53        assertLayout("afterLoad", afterLoad, mSwitcher.getLayoutId());
54    }
55
56    public void rotateDevice(final int afterRotate) {
57        mSwitcher.saveKeyboardState();
58        mSwitcher.loadKeyboard();
59        assertLayout("afterRotate", afterRotate, mSwitcher.getLayoutId());
60    }
61
62    private void pressKeyWithoutTimerExpire(final int code, final boolean isSinglePointer,
63            final int afterPress) {
64        mSwitcher.onPressKey(code, isSinglePointer);
65        assertLayout("afterPress", afterPress, mSwitcher.getLayoutId());
66    }
67
68    public void pressKey(final int code, final int afterPress) {
69        mSwitcher.expireDoubleTapTimeout();
70        pressKeyWithoutTimerExpire(code, true, afterPress);
71    }
72
73    public void releaseKey(final int code, final int afterRelease) {
74        mSwitcher.onCodeInput(code);
75        mSwitcher.onReleaseKey(code, NOT_SLIDING);
76        assertLayout("afterRelease", afterRelease, mSwitcher.getLayoutId());
77    }
78
79    public void pressAndReleaseKey(final int code, final int afterPress, final int afterRelease) {
80        pressKey(code, afterPress);
81        releaseKey(code, afterRelease);
82    }
83
84    public void chordingPressKey(final int code, final int afterPress) {
85        mSwitcher.expireDoubleTapTimeout();
86        pressKeyWithoutTimerExpire(code, false, afterPress);
87    }
88
89    public void chordingReleaseKey(final int code, final int afterRelease) {
90        mSwitcher.onCodeInput(code);
91        mSwitcher.onReleaseKey(code, NOT_SLIDING);
92        assertLayout("afterRelease", afterRelease, mSwitcher.getLayoutId());
93    }
94
95    public void chordingPressAndReleaseKey(final int code, final int afterPress,
96            final int afterRelease) {
97        chordingPressKey(code, afterPress);
98        chordingReleaseKey(code, afterRelease);
99    }
100
101    public void pressAndSlideFromKey(final int code, final int afterPress, final int afterSlide) {
102        pressKey(code, afterPress);
103        mSwitcher.onReleaseKey(code, SLIDING);
104        assertLayout("afterSlide", afterSlide, mSwitcher.getLayoutId());
105    }
106
107    public void stopSlidingOnKey(final int code, final int afterPress, final int afterSlide) {
108        pressKey(code, afterPress);
109        mSwitcher.onCodeInput(code);
110        mSwitcher.onReleaseKey(code, NOT_SLIDING);
111        mSwitcher.onFinishSlidingInput();
112        assertLayout("afterSlide", afterSlide, mSwitcher.getLayoutId());
113    }
114
115    public void stopSlidingAndCancel(final int afterCancelSliding) {
116        mSwitcher.onFinishSlidingInput();
117        assertLayout("afterCancelSliding", afterCancelSliding, mSwitcher.getLayoutId());
118    }
119
120    public void longPressKey(final int code, final int afterPress, final int afterLongPress) {
121        pressKey(code, afterPress);
122        mSwitcher.onLongPressTimeout(code);
123        assertLayout("afterLongPress", afterLongPress, mSwitcher.getLayoutId());
124    }
125
126    public void longPressAndReleaseKey(final int code, final int afterPress,
127            final int afterLongPress, final int afterRelease) {
128        longPressKey(code, afterPress, afterLongPress);
129        releaseKey(code, afterRelease);
130    }
131
132    public void secondPressKey(int code, int afterPress) {
133        pressKeyWithoutTimerExpire(code, true, afterPress);
134    }
135
136    public void secondPressAndReleaseKey(int code, int afterPress, int afterRelease) {
137        secondPressKey(code, afterPress);
138        releaseKey(code, afterRelease);
139    }
140}
141