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