1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
|
/*
* Copyright (C) 2012 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.inputmethod.keyboard.internal;
import android.test.AndroidTestCase;
import com.android.inputmethod.latin.common.Constants;
public class KeyboardStateTestsBase extends AndroidTestCase
implements MockKeyboardSwitcher.MockConstants {
protected MockKeyboardSwitcher mSwitcher;
@Override
protected void setUp() throws Exception {
super.setUp();
mSwitcher = new MockKeyboardSwitcher();
mSwitcher.setAutoCapsMode(CAP_MODE_OFF);
loadKeyboard(ALPHABET_UNSHIFTED);
}
/**
* Set auto caps mode.
*
* @param autoCaps the auto cap mode.
*/
public void setAutoCapsMode(final int autoCaps) {
mSwitcher.setAutoCapsMode(autoCaps);
}
private static void assertLayout(final String message, final int expected, final int actual) {
assertTrue(message + ": expected=" + MockKeyboardSwitcher.getLayoutName(expected)
+ " actual=" + MockKeyboardSwitcher.getLayoutName(actual),
expected == actual);
}
/**
* Emulate update keyboard shift state.
*
* @param afterUpdate the keyboard state after updating the keyboard shift state.
*/
public void updateShiftState(final int afterUpdate) {
mSwitcher.updateShiftState();
assertLayout("afterUpdate", afterUpdate, mSwitcher.getLayoutId());
}
/**
* Emulate load default keyboard.
*
* @param afterLoad the keyboard state after loading default keyboard.
*/
public void loadKeyboard(final int afterLoad) {
mSwitcher.loadKeyboard();
mSwitcher.updateShiftState();
assertLayout("afterLoad", afterLoad, mSwitcher.getLayoutId());
}
/**
* Emulate rotate device.
*
* @param afterRotate the keyboard state after rotating device.
*/
public void rotateDevice(final int afterRotate) {
mSwitcher.saveKeyboardState();
mSwitcher.loadKeyboard();
assertLayout("afterRotate", afterRotate, mSwitcher.getLayoutId());
}
private void pressKeyWithoutTimerExpire(final int code, final boolean isSinglePointer,
final int afterPress) {
mSwitcher.onPressKey(code, isSinglePointer);
assertLayout("afterPress", afterPress, mSwitcher.getLayoutId());
}
/**
* Emulate key press.
*
* @param code the key code to press.
* @param afterPress the keyboard state after pressing the key.
*/
public void pressKey(final int code, final int afterPress) {
mSwitcher.expireDoubleTapTimeout();
pressKeyWithoutTimerExpire(code, true, afterPress);
}
/**
* Emulate key release and register.
*
* @param code the key code to release and register
* @param afterRelease the keyboard state after releasing the key.
*/
public void releaseKey(final int code, final int afterRelease) {
mSwitcher.onCodeInput(code);
mSwitcher.onReleaseKey(code, NOT_SLIDING);
assertLayout("afterRelease", afterRelease, mSwitcher.getLayoutId());
}
/**
* Emulate key press and release.
*
* @param code the key code to press and release.
* @param afterPress the keyboard state after pressing the key.
* @param afterRelease the keyboard state after releasing the key.
*/
public void pressAndReleaseKey(final int code, final int afterPress, final int afterRelease) {
pressKey(code, afterPress);
releaseKey(code, afterRelease);
}
/**
* Emulate chording key press.
*
* @param code the chording key code.
* @param afterPress the keyboard state after pressing chording key.
*/
public void chordingPressKey(final int code, final int afterPress) {
mSwitcher.expireDoubleTapTimeout();
pressKeyWithoutTimerExpire(code, false, afterPress);
}
/**
* Emulate chording key release.
*
* @param code the cording key code.
* @param afterRelease the keyboard state after releasing chording key.
*/
public void chordingReleaseKey(final int code, final int afterRelease) {
mSwitcher.onCodeInput(code);
mSwitcher.onReleaseKey(code, NOT_SLIDING);
assertLayout("afterRelease", afterRelease, mSwitcher.getLayoutId());
}
/**
* Emulate chording key press and release.
*
* @param code the chording key code.
* @param afterPress the keyboard state after pressing chording key.
* @param afterRelease the keyboard state after releasing chording key.
*/
public void chordingPressAndReleaseKey(final int code, final int afterPress,
final int afterRelease) {
chordingPressKey(code, afterPress);
chordingReleaseKey(code, afterRelease);
}
/**
* Emulate start of the sliding key input.
*
* @param code the key code to start sliding.
* @param afterPress the keyboard state after pressing the key.
* @param afterSlide the keyboard state after releasing the key with sliding input.
*/
public void pressAndSlideFromKey(final int code, final int afterPress, final int afterSlide) {
pressKey(code, afterPress);
mSwitcher.onReleaseKey(code, SLIDING);
assertLayout("afterSlide", afterSlide, mSwitcher.getLayoutId());
}
/**
* Emulate end of the sliding key input.
*
* @param code the key code to stop sliding.
* @param afterPress the keyboard state after pressing the key.
* @param afterSlide the keyboard state after releasing the key and stop sliding.
*/
public void stopSlidingOnKey(final int code, final int afterPress, final int afterSlide) {
pressKey(code, afterPress);
mSwitcher.onCodeInput(code);
mSwitcher.onReleaseKey(code, NOT_SLIDING);
mSwitcher.onFinishSlidingInput();
assertLayout("afterSlide", afterSlide, mSwitcher.getLayoutId());
}
/**
* Emulate cancel the sliding key input.
*
* @param afterCancelSliding the keyboard state after canceling sliding input.
*/
public void stopSlidingAndCancel(final int afterCancelSliding) {
mSwitcher.onFinishSlidingInput();
assertLayout("afterCancelSliding", afterCancelSliding, mSwitcher.getLayoutId());
}
/**
* Emulate long press shift key.
*
* @param afterPress the keyboard state after pressing shift key.
* @param afterLongPress the keyboard state after long press fired.
*/
public void longPressShiftKey(final int afterPress, final int afterLongPress) {
// Long press shift key will register {@link Constants#CODE_CAPS_LOCK}. See
// {@link R.xml#key_styles_common} and its baseForShiftKeyStyle. We thus emulate the
// behavior that is implemented in {@link MainKeyboardView#onLongPress(PointerTracker)}.
pressKey(Constants.CODE_SHIFT, afterPress);
mSwitcher.onPressKey(Constants.CODE_CAPSLOCK, true /* isSinglePointer */);
mSwitcher.onCodeInput(Constants.CODE_CAPSLOCK);
assertLayout("afterLongPress", afterLongPress, mSwitcher.getLayoutId());
}
/**
* Emulate long press shift key and release.
*
* @param afterPress the keyboard state after pressing shift key.
* @param afterLongPress the keyboard state after long press fired.
* @param afterRelease the keyboard state after shift key is released.
*/
public void longPressAndReleaseShiftKey(final int afterPress, final int afterLongPress,
final int afterRelease) {
// Long press shift key will register {@link Constants#CODE_CAPS_LOCK}. See
// {@link R.xml#key_styles_common} and its baseForShiftKeyStyle. We thus emulate the
// behavior that is implemented in {@link MainKeyboardView#onLongPress(PointerTracker)}.
longPressShiftKey(afterPress, afterLongPress);
releaseKey(Constants.CODE_CAPSLOCK, afterRelease);
}
/**
* Emulate the second press of the double tap.
*
* @param code the key code to double tap.
* @param afterPress the keyboard state after pressing the second tap.
*/
public void secondPressKey(final int code, final int afterPress) {
pressKeyWithoutTimerExpire(code, true, afterPress);
}
/**
* Emulate the second tap of the double tap.
*
* @param code the key code to double tap.
* @param afterPress the keyboard state after pressing the second tap.
* @param afterRelease the keyboard state after releasing the second tap.
*/
public void secondPressAndReleaseKey(final int code, final int afterPress,
final int afterRelease) {
secondPressKey(code, afterPress);
releaseKey(code, afterRelease);
}
}
|