InputLogicTests.java revision 7b8b6e4b42710a04dbda19a16b57f44a0c3caca9
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.latin;
18
19import android.test.suitebuilder.annotation.LargeTest;
20
21@LargeTest
22public class InputLogicTests extends InputTestsBase {
23
24    public void testTypeWord() {
25        final String WORD_TO_TYPE = "abcd";
26        type(WORD_TO_TYPE);
27        assertEquals("type word", WORD_TO_TYPE, mEditText.getText().toString());
28    }
29
30    public void testPickSuggestionThenBackspace() {
31        final String WORD_TO_TYPE = "this";
32        final String EXPECTED_RESULT = "thi";
33        type(WORD_TO_TYPE);
34        pickSuggestionManually(0, WORD_TO_TYPE);
35        mLatinIME.onUpdateSelection(0, 0, WORD_TO_TYPE.length(), WORD_TO_TYPE.length(), -1, -1);
36        type(Constants.CODE_DELETE);
37        assertEquals("press suggestion then backspace", EXPECTED_RESULT,
38                mEditText.getText().toString());
39    }
40
41    public void testPickAutoCorrectionThenBackspace() {
42        final String WORD_TO_TYPE = "tgis";
43        final String WORD_TO_PICK = "this";
44        final String EXPECTED_RESULT = "thi";
45        type(WORD_TO_TYPE);
46        // Choose the auto-correction, which is always in position 0. For "tgis", the
47        // auto-correction should be "this".
48        pickSuggestionManually(0, WORD_TO_PICK);
49        mLatinIME.onUpdateSelection(0, 0, WORD_TO_TYPE.length(), WORD_TO_TYPE.length(), -1, -1);
50        assertEquals("pick typed word over auto-correction then backspace", WORD_TO_PICK,
51                mEditText.getText().toString());
52        type(Constants.CODE_DELETE);
53        assertEquals("pick typed word over auto-correction then backspace", EXPECTED_RESULT,
54                mEditText.getText().toString());
55    }
56
57    public void testPickTypedWordOverAutoCorrectionThenBackspace() {
58        final String WORD_TO_TYPE = "tgis";
59        final String EXPECTED_RESULT = "tgi";
60        type(WORD_TO_TYPE);
61        // Choose the typed word, which should be in position 1 (because position 0 should
62        // be occupied by the "this" auto-correction, as checked by testAutoCorrect())
63        pickSuggestionManually(1, WORD_TO_TYPE);
64        mLatinIME.onUpdateSelection(0, 0, WORD_TO_TYPE.length(), WORD_TO_TYPE.length(), -1, -1);
65        assertEquals("pick typed word over auto-correction then backspace", WORD_TO_TYPE,
66                mEditText.getText().toString());
67        type(Constants.CODE_DELETE);
68        assertEquals("pick typed word over auto-correction then backspace", EXPECTED_RESULT,
69                mEditText.getText().toString());
70    }
71
72    public void testPickDifferentSuggestionThenBackspace() {
73        final String WORD_TO_TYPE = "tgis";
74        final String WORD_TO_PICK = "thus";
75        final String EXPECTED_RESULT = "thu";
76        type(WORD_TO_TYPE);
77        // Choose the second suggestion, which should be in position 2 and should be "thus"
78        // when "tgis is typed.
79        pickSuggestionManually(2, WORD_TO_PICK);
80        mLatinIME.onUpdateSelection(0, 0, WORD_TO_TYPE.length(), WORD_TO_TYPE.length(), -1, -1);
81        assertEquals("pick different suggestion then backspace", WORD_TO_PICK,
82                mEditText.getText().toString());
83        type(Constants.CODE_DELETE);
84        assertEquals("pick different suggestion then backspace", EXPECTED_RESULT,
85                mEditText.getText().toString());
86    }
87
88    public void testDeleteSelection() {
89        final String STRING_TO_TYPE = "some text delete me some text";
90        final int typedLength = STRING_TO_TYPE.length();
91        final int SELECTION_START = 10;
92        final int SELECTION_END = 19;
93        final String EXPECTED_RESULT = "some text  some text";
94        type(STRING_TO_TYPE);
95        // There is no IMF to call onUpdateSelection for us so we must do it by hand.
96        // Send once to simulate the cursor actually responding to the move caused by typing.
97        // This is necessary because LatinIME is bookkeeping to avoid confusing a real cursor
98        // move with a move triggered by LatinIME inputting stuff.
99        mLatinIME.onUpdateSelection(0, 0, typedLength, typedLength, -1, -1);
100        mInputConnection.setSelection(SELECTION_START, SELECTION_END);
101        // And now we simulate the user actually selecting some text.
102        mLatinIME.onUpdateSelection(typedLength, typedLength,
103                SELECTION_START, SELECTION_END, -1, -1);
104        type(Constants.CODE_DELETE);
105        assertEquals("delete selection", EXPECTED_RESULT, mEditText.getText().toString());
106    }
107
108    public void testDeleteSelectionTwice() {
109        final String STRING_TO_TYPE = "some text delete me some text";
110        final int typedLength = STRING_TO_TYPE.length();
111        final int SELECTION_START = 10;
112        final int SELECTION_END = 19;
113        final String EXPECTED_RESULT = "some text some text";
114        type(STRING_TO_TYPE);
115        // There is no IMF to call onUpdateSelection for us so we must do it by hand.
116        // Send once to simulate the cursor actually responding to the move caused by typing.
117        // This is necessary because LatinIME is bookkeeping to avoid confusing a real cursor
118        // move with a move triggered by LatinIME inputting stuff.
119        mLatinIME.onUpdateSelection(0, 0, typedLength, typedLength, -1, -1);
120        mInputConnection.setSelection(SELECTION_START, SELECTION_END);
121        // And now we simulate the user actually selecting some text.
122        mLatinIME.onUpdateSelection(typedLength, typedLength,
123                SELECTION_START, SELECTION_END, -1, -1);
124        type(Constants.CODE_DELETE);
125        type(Constants.CODE_DELETE);
126        assertEquals("delete selection twice", EXPECTED_RESULT, mEditText.getText().toString());
127    }
128
129    public void testAutoCorrect() {
130        final String STRING_TO_TYPE = "tgis ";
131        final String EXPECTED_RESULT = "this ";
132        type(STRING_TO_TYPE);
133        assertEquals("simple auto-correct", EXPECTED_RESULT, mEditText.getText().toString());
134    }
135
136    public void testAutoCorrectWithPeriod() {
137        final String STRING_TO_TYPE = "tgis.";
138        final String EXPECTED_RESULT = "this.";
139        type(STRING_TO_TYPE);
140        assertEquals("auto-correct with period", EXPECTED_RESULT, mEditText.getText().toString());
141    }
142
143    public void testAutoCorrectWithPeriodThenRevert() {
144        final String STRING_TO_TYPE = "tgis.";
145        final String EXPECTED_RESULT = "tgis.";
146        type(STRING_TO_TYPE);
147        mLatinIME.onUpdateSelection(0, 0, STRING_TO_TYPE.length(), STRING_TO_TYPE.length(), -1, -1);
148        type(Constants.CODE_DELETE);
149        assertEquals("auto-correct with period then revert", EXPECTED_RESULT,
150                mEditText.getText().toString());
151    }
152
153    public void testAutoCorrectWithSpaceThenRevert() {
154        final String STRING_TO_TYPE = "tgis ";
155        final String EXPECTED_RESULT = "tgis ";
156        type(STRING_TO_TYPE);
157        mLatinIME.onUpdateSelection(0, 0, STRING_TO_TYPE.length(), STRING_TO_TYPE.length(), -1, -1);
158        type(Constants.CODE_DELETE);
159        assertEquals("auto-correct with space then revert", EXPECTED_RESULT,
160                mEditText.getText().toString());
161    }
162
163    public void testAutoCorrectToSelfDoesNotRevert() {
164        final String STRING_TO_TYPE = "this ";
165        final String EXPECTED_RESULT = "this";
166        type(STRING_TO_TYPE);
167        mLatinIME.onUpdateSelection(0, 0, STRING_TO_TYPE.length(), STRING_TO_TYPE.length(), -1, -1);
168        type(Constants.CODE_DELETE);
169        assertEquals("auto-correct with space does not revert", EXPECTED_RESULT,
170                mEditText.getText().toString());
171    }
172
173    public void testDoubleSpace() {
174        final String STRING_TO_TYPE = "this  ";
175        final String EXPECTED_RESULT = "this. ";
176        type(STRING_TO_TYPE);
177        assertEquals("double space make a period", EXPECTED_RESULT, mEditText.getText().toString());
178    }
179
180    public void testCancelDoubleSpace() {
181        final String STRING_TO_TYPE = "this  ";
182        final String EXPECTED_RESULT = "this ";
183        type(STRING_TO_TYPE);
184        type(Constants.CODE_DELETE);
185        assertEquals("double space make a period", EXPECTED_RESULT, mEditText.getText().toString());
186    }
187
188    public void testBackspaceAtStartAfterAutocorrect() {
189        final String STRING_TO_TYPE = "tgis ";
190        final int typedLength = STRING_TO_TYPE.length();
191        final String EXPECTED_RESULT = "this ";
192        final int NEW_CURSOR_POSITION = 0;
193        type(STRING_TO_TYPE);
194        mLatinIME.onUpdateSelection(0, 0, typedLength, typedLength, -1, -1);
195        mInputConnection.setSelection(NEW_CURSOR_POSITION, NEW_CURSOR_POSITION);
196        mLatinIME.onUpdateSelection(typedLength, typedLength,
197                NEW_CURSOR_POSITION, NEW_CURSOR_POSITION, -1, -1);
198        type(Constants.CODE_DELETE);
199        assertEquals("auto correct then move cursor to start of line then backspace",
200                EXPECTED_RESULT, mEditText.getText().toString());
201    }
202
203    public void testAutoCorrectThenMoveCursorThenBackspace() {
204        final String STRING_TO_TYPE = "and tgis ";
205        final int typedLength = STRING_TO_TYPE.length();
206        final String EXPECTED_RESULT = "andthis ";
207        final int NEW_CURSOR_POSITION = STRING_TO_TYPE.indexOf('t');
208        type(STRING_TO_TYPE);
209        mLatinIME.onUpdateSelection(0, 0, typedLength, typedLength, -1, -1);
210        mInputConnection.setSelection(NEW_CURSOR_POSITION, NEW_CURSOR_POSITION);
211        mLatinIME.onUpdateSelection(typedLength, typedLength,
212                NEW_CURSOR_POSITION, NEW_CURSOR_POSITION, -1, -1);
213        type(Constants.CODE_DELETE);
214        assertEquals("auto correct then move cursor then backspace",
215                EXPECTED_RESULT, mEditText.getText().toString());
216    }
217
218    public void testNoSpaceAfterManualPick() {
219        final String WORD_TO_TYPE = "this";
220        final String EXPECTED_RESULT = WORD_TO_TYPE;
221        type(WORD_TO_TYPE);
222        pickSuggestionManually(0, WORD_TO_TYPE);
223        assertEquals("no space after manual pick", EXPECTED_RESULT,
224                mEditText.getText().toString());
225    }
226
227    public void testManualPickThenType() {
228        final String WORD1_TO_TYPE = "this";
229        final String WORD2_TO_TYPE = "is";
230        final String EXPECTED_RESULT = "this is";
231        type(WORD1_TO_TYPE);
232        pickSuggestionManually(0, WORD1_TO_TYPE);
233        type(WORD2_TO_TYPE);
234        assertEquals("manual pick then type", EXPECTED_RESULT, mEditText.getText().toString());
235    }
236
237    public void testManualPickThenSeparator() {
238        final String WORD1_TO_TYPE = "this";
239        final String WORD2_TO_TYPE = "!";
240        final String EXPECTED_RESULT = "this!";
241        type(WORD1_TO_TYPE);
242        pickSuggestionManually(0, WORD1_TO_TYPE);
243        type(WORD2_TO_TYPE);
244        assertEquals("manual pick then separator", EXPECTED_RESULT, mEditText.getText().toString());
245    }
246
247    public void testManualPickThenStripperThenPick() {
248        final String WORD_TO_TYPE = "this";
249        final String STRIPPER = "\n";
250        final String EXPECTED_RESULT = "this\nthis";
251        type(WORD_TO_TYPE);
252        pickSuggestionManually(0, WORD_TO_TYPE);
253        type(STRIPPER);
254        type(WORD_TO_TYPE);
255        pickSuggestionManually(0, WORD_TO_TYPE);
256        assertEquals("manual pick then \\n then manual pick", EXPECTED_RESULT,
257                mEditText.getText().toString());
258    }
259
260    public void testManualPickThenSpaceThenType() {
261        final String WORD1_TO_TYPE = "this";
262        final String WORD2_TO_TYPE = " is";
263        final String EXPECTED_RESULT = "this is";
264        type(WORD1_TO_TYPE);
265        pickSuggestionManually(0, WORD1_TO_TYPE);
266        type(WORD2_TO_TYPE);
267        assertEquals("manual pick then space then type", EXPECTED_RESULT,
268                mEditText.getText().toString());
269    }
270
271    public void testManualPickThenManualPick() {
272        final String WORD1_TO_TYPE = "this";
273        final String WORD2_TO_PICK = "is";
274        final String EXPECTED_RESULT = "this is";
275        type(WORD1_TO_TYPE);
276        pickSuggestionManually(0, WORD1_TO_TYPE);
277        // Here we fake picking a word through bigram prediction. This test is taking
278        // advantage of the fact that Latin IME blindly trusts the caller of #pickSuggestionManually
279        // to actually pass the right string.
280        pickSuggestionManually(1, WORD2_TO_PICK);
281        assertEquals("manual pick then manual pick", EXPECTED_RESULT,
282                mEditText.getText().toString());
283    }
284
285    public void testDeleteWholeComposingWord() {
286        final String WORD_TO_TYPE = "this";
287        type(WORD_TO_TYPE);
288        for (int i = 0; i < WORD_TO_TYPE.length(); ++i) {
289            type(Constants.CODE_DELETE);
290        }
291        assertEquals("delete whole composing word", "", mEditText.getText().toString());
292    }
293    // TODO: Add some tests for non-BMP characters
294}
295