InputLogicTests.java revision fa4c59f9b6663e5de08692f853e968603dfc10e9
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 com.android.inputmethod.latin.settings.Settings; 20 21import android.test.suitebuilder.annotation.LargeTest; 22import android.text.TextUtils; 23import android.view.inputmethod.BaseInputConnection; 24 25@LargeTest 26public class InputLogicTests extends InputTestsBase { 27 28 public void testTypeWord() { 29 final String WORD_TO_TYPE = "abcd"; 30 type(WORD_TO_TYPE); 31 assertEquals("type word", WORD_TO_TYPE, mEditText.getText().toString()); 32 } 33 34 public void testPickSuggestionThenBackspace() { 35 final String WORD_TO_TYPE = "this"; 36 final String EXPECTED_RESULT = "thi"; 37 type(WORD_TO_TYPE); 38 pickSuggestionManually(WORD_TO_TYPE); 39 mLatinIME.onUpdateSelection(0, 0, WORD_TO_TYPE.length(), WORD_TO_TYPE.length(), -1, -1); 40 type(Constants.CODE_DELETE); 41 assertEquals("press suggestion then backspace", EXPECTED_RESULT, 42 mEditText.getText().toString()); 43 } 44 45 public void testPickAutoCorrectionThenBackspace() { 46 final String WORD_TO_TYPE = "tgis"; 47 final String WORD_TO_PICK = "this"; 48 final String EXPECTED_RESULT = "thi"; 49 type(WORD_TO_TYPE); 50 // Choose the auto-correction. For "tgis", the auto-correction should be "this". 51 pickSuggestionManually(WORD_TO_PICK); 52 mLatinIME.onUpdateSelection(0, 0, WORD_TO_TYPE.length(), WORD_TO_TYPE.length(), -1, -1); 53 assertEquals("pick typed word over auto-correction then backspace", WORD_TO_PICK, 54 mEditText.getText().toString()); 55 type(Constants.CODE_DELETE); 56 assertEquals("pick typed word over auto-correction then backspace", EXPECTED_RESULT, 57 mEditText.getText().toString()); 58 } 59 60 public void testPickTypedWordOverAutoCorrectionThenBackspace() { 61 final String WORD_TO_TYPE = "tgis"; 62 final String EXPECTED_RESULT = "tgi"; 63 type(WORD_TO_TYPE); 64 // Choose the typed word. 65 pickSuggestionManually(WORD_TO_TYPE); 66 mLatinIME.onUpdateSelection(0, 0, WORD_TO_TYPE.length(), WORD_TO_TYPE.length(), -1, -1); 67 assertEquals("pick typed word over auto-correction then backspace", WORD_TO_TYPE, 68 mEditText.getText().toString()); 69 type(Constants.CODE_DELETE); 70 assertEquals("pick typed word over auto-correction then backspace", EXPECTED_RESULT, 71 mEditText.getText().toString()); 72 } 73 74 public void testPickDifferentSuggestionThenBackspace() { 75 final String WORD_TO_TYPE = "tgis"; 76 final String WORD_TO_PICK = "thus"; 77 final String EXPECTED_RESULT = "thu"; 78 type(WORD_TO_TYPE); 79 // Choose the second suggestion, which should be "thus" when "tgis" is typed. 80 pickSuggestionManually(WORD_TO_PICK); 81 mLatinIME.onUpdateSelection(0, 0, WORD_TO_TYPE.length(), WORD_TO_TYPE.length(), -1, -1); 82 assertEquals("pick different suggestion then backspace", WORD_TO_PICK, 83 mEditText.getText().toString()); 84 type(Constants.CODE_DELETE); 85 assertEquals("pick different suggestion then backspace", EXPECTED_RESULT, 86 mEditText.getText().toString()); 87 } 88 89 public void testDeleteSelection() { 90 final String STRING_TO_TYPE = "some text delete me some text"; 91 final int typedLength = STRING_TO_TYPE.length(); 92 final int SELECTION_START = 10; 93 final int SELECTION_END = 19; 94 final String EXPECTED_RESULT = "some text some text"; 95 type(STRING_TO_TYPE); 96 // There is no IMF to call onUpdateSelection for us so we must do it by hand. 97 // Send once to simulate the cursor actually responding to the move caused by typing. 98 // This is necessary because LatinIME is bookkeeping to avoid confusing a real cursor 99 // move with a move triggered by LatinIME inputting stuff. 100 mLatinIME.onUpdateSelection(0, 0, typedLength, typedLength, -1, -1); 101 mInputConnection.setSelection(SELECTION_START, SELECTION_END); 102 // And now we simulate the user actually selecting some text. 103 mLatinIME.onUpdateSelection(typedLength, typedLength, 104 SELECTION_START, SELECTION_END, -1, -1); 105 type(Constants.CODE_DELETE); 106 assertEquals("delete selection", EXPECTED_RESULT, mEditText.getText().toString()); 107 } 108 109 public void testDeleteSelectionTwice() { 110 final String STRING_TO_TYPE = "some text delete me some text"; 111 final int typedLength = STRING_TO_TYPE.length(); 112 final int SELECTION_START = 10; 113 final int SELECTION_END = 19; 114 final String EXPECTED_RESULT = "some text some text"; 115 type(STRING_TO_TYPE); 116 // There is no IMF to call onUpdateSelection for us so we must do it by hand. 117 // Send once to simulate the cursor actually responding to the move caused by typing. 118 // This is necessary because LatinIME is bookkeeping to avoid confusing a real cursor 119 // move with a move triggered by LatinIME inputting stuff. 120 mLatinIME.onUpdateSelection(0, 0, typedLength, typedLength, -1, -1); 121 mInputConnection.setSelection(SELECTION_START, SELECTION_END); 122 // And now we simulate the user actually selecting some text. 123 mLatinIME.onUpdateSelection(typedLength, typedLength, 124 SELECTION_START, SELECTION_END, -1, -1); 125 type(Constants.CODE_DELETE); 126 type(Constants.CODE_DELETE); 127 assertEquals("delete selection twice", EXPECTED_RESULT, mEditText.getText().toString()); 128 } 129 130 public void testAutoCorrect() { 131 final String STRING_TO_TYPE = "tgis "; 132 final String EXPECTED_RESULT = "this "; 133 type(STRING_TO_TYPE); 134 assertEquals("simple auto-correct", EXPECTED_RESULT, mEditText.getText().toString()); 135 } 136 137 public void testAutoCorrectWithQuote() { 138 final String STRING_TO_TYPE = "didn' "; 139 final String EXPECTED_RESULT = "didn't "; 140 type(STRING_TO_TYPE); 141 assertEquals("auto-correct with quote", EXPECTED_RESULT, mEditText.getText().toString()); 142 } 143 144 public void testAutoCorrectWithPeriod() { 145 final String STRING_TO_TYPE = "tgis."; 146 final String EXPECTED_RESULT = "this."; 147 type(STRING_TO_TYPE); 148 assertEquals("auto-correct with period", EXPECTED_RESULT, mEditText.getText().toString()); 149 } 150 151 public void testAutoCorrectWithPeriodThenRevert() { 152 final String STRING_TO_TYPE = "tgis."; 153 final String EXPECTED_RESULT = "tgis."; 154 type(STRING_TO_TYPE); 155 mLatinIME.onUpdateSelection(0, 0, STRING_TO_TYPE.length(), STRING_TO_TYPE.length(), -1, -1); 156 type(Constants.CODE_DELETE); 157 assertEquals("auto-correct with period then revert", EXPECTED_RESULT, 158 mEditText.getText().toString()); 159 } 160 161 public void testAutoCorrectWithSpaceThenRevert() { 162 final String STRING_TO_TYPE = "tgis "; 163 final String EXPECTED_RESULT = "tgis "; 164 type(STRING_TO_TYPE); 165 mLatinIME.onUpdateSelection(0, 0, STRING_TO_TYPE.length(), STRING_TO_TYPE.length(), -1, -1); 166 type(Constants.CODE_DELETE); 167 assertEquals("auto-correct with space then revert", EXPECTED_RESULT, 168 mEditText.getText().toString()); 169 } 170 171 public void testAutoCorrectToSelfDoesNotRevert() { 172 final String STRING_TO_TYPE = "this "; 173 final String EXPECTED_RESULT = "this"; 174 type(STRING_TO_TYPE); 175 mLatinIME.onUpdateSelection(0, 0, STRING_TO_TYPE.length(), STRING_TO_TYPE.length(), -1, -1); 176 type(Constants.CODE_DELETE); 177 assertEquals("auto-correct with space does not revert", EXPECTED_RESULT, 178 mEditText.getText().toString()); 179 } 180 181 public void testDoubleSpace() { 182 // Set default pref just in case 183 setBooleanPreference(Settings.PREF_KEY_USE_DOUBLE_SPACE_PERIOD, true, true); 184 // U+1F607 is an emoji 185 final String[] STRINGS_TO_TYPE = 186 new String[] { "this ", "a+ ", "\u1F607 ", ".. ", ") ", "( ", "% " }; 187 final String[] EXPECTED_RESULTS = 188 new String[] { "this. ", "a+. ", "\u1F607. ", ".. ", "). ", "( ", "%. " }; 189 for (int i = 0; i < STRINGS_TO_TYPE.length; ++i) { 190 mEditText.setText(""); 191 type(STRINGS_TO_TYPE[i]); 192 assertEquals("double space processing", EXPECTED_RESULTS[i], 193 mEditText.getText().toString()); 194 } 195 } 196 197 public void testCancelDoubleSpace() { 198 final String STRING_TO_TYPE = "this "; 199 final String EXPECTED_RESULT = "this "; 200 type(STRING_TO_TYPE); 201 type(Constants.CODE_DELETE); 202 assertEquals("double space make a period", EXPECTED_RESULT, mEditText.getText().toString()); 203 } 204 205 private void testDoubleSpacePeriodWithSettings(final boolean expectsPeriod, 206 final Object... settingsKeysValues) { 207 final Object[] oldSettings = new Object[settingsKeysValues.length / 2]; 208 final String STRING_WITHOUT_PERIOD = "this "; 209 final String STRING_WITH_PERIOD = "this. "; 210 final String EXPECTED_RESULT = expectsPeriod ? STRING_WITH_PERIOD : STRING_WITHOUT_PERIOD; 211 try { 212 for (int i = 0; i < settingsKeysValues.length; i += 2) { 213 if (settingsKeysValues[i + 1] instanceof String) { 214 oldSettings[i / 2] = setStringPreference((String)settingsKeysValues[i], 215 (String)settingsKeysValues[i + 1], "0"); 216 } else { 217 oldSettings[i / 2] = setBooleanPreference((String)settingsKeysValues[i], 218 (Boolean)settingsKeysValues[i + 1], false); 219 } 220 } 221 mLatinIME.loadSettings(); 222 mEditText.setText(""); 223 type(STRING_WITHOUT_PERIOD); 224 assertEquals("double-space-to-period with specific settings " 225 + TextUtils.join(" ", settingsKeysValues), 226 EXPECTED_RESULT, mEditText.getText().toString()); 227 } finally { 228 // Restore old settings 229 for (int i = 0; i < settingsKeysValues.length; i += 2) { 230 if (null == oldSettings[i / 2]) { 231 break; 232 } if (oldSettings[i / 2] instanceof String) { 233 setStringPreference((String)settingsKeysValues[i], (String)oldSettings[i / 2], 234 ""); 235 } else { 236 setBooleanPreference((String)settingsKeysValues[i], (Boolean)oldSettings[i / 2], 237 false); 238 } 239 } 240 } 241 } 242 243 public void testDoubleSpacePeriod() { 244 // Reset settings to default, else these tests will go flaky. 245 setStringPreference(Settings.PREF_SHOW_SUGGESTIONS_SETTING, "0", "0"); 246 setStringPreference(Settings.PREF_AUTO_CORRECTION_THRESHOLD, "1", "1"); 247 setBooleanPreference(Settings.PREF_KEY_USE_DOUBLE_SPACE_PERIOD, true, true); 248 testDoubleSpacePeriodWithSettings(true /* expectsPeriod */); 249 // "Suggestion visibility" to "always hide" 250 testDoubleSpacePeriodWithSettings(true, Settings.PREF_SHOW_SUGGESTIONS_SETTING, "2"); 251 // "Suggestion visibility" to "portrait only" 252 testDoubleSpacePeriodWithSettings(true, Settings.PREF_SHOW_SUGGESTIONS_SETTING, "1"); 253 // "Suggestion visibility" to "always show" 254 testDoubleSpacePeriodWithSettings(true, Settings.PREF_SHOW_SUGGESTIONS_SETTING, "0"); 255 256 // "Double-space period" to "off" 257 testDoubleSpacePeriodWithSettings(false, Settings.PREF_KEY_USE_DOUBLE_SPACE_PERIOD, false); 258 259 // "Auto-correction" to "off" 260 testDoubleSpacePeriodWithSettings(true, Settings.PREF_AUTO_CORRECTION_THRESHOLD, "0"); 261 // "Auto-correction" to "modest" 262 testDoubleSpacePeriodWithSettings(true, Settings.PREF_AUTO_CORRECTION_THRESHOLD, "1"); 263 // "Auto-correction" to "very aggressive" 264 testDoubleSpacePeriodWithSettings(true, Settings.PREF_AUTO_CORRECTION_THRESHOLD, "3"); 265 266 // "Suggestion visibility" to "always hide" and "Auto-correction" to "off" 267 testDoubleSpacePeriodWithSettings(true, Settings.PREF_SHOW_SUGGESTIONS_SETTING, "0", 268 Settings.PREF_AUTO_CORRECTION_THRESHOLD, "0"); 269 // "Suggestion visibility" to "always hide" and "Auto-correction" to "off" 270 testDoubleSpacePeriodWithSettings(false, Settings.PREF_SHOW_SUGGESTIONS_SETTING, "0", 271 Settings.PREF_AUTO_CORRECTION_THRESHOLD, "0", 272 Settings.PREF_KEY_USE_DOUBLE_SPACE_PERIOD, false); 273 } 274 275 public void testBackspaceAtStartAfterAutocorrect() { 276 final String STRING_TO_TYPE = "tgis "; 277 final int typedLength = STRING_TO_TYPE.length(); 278 final String EXPECTED_RESULT = "this "; 279 final int NEW_CURSOR_POSITION = 0; 280 type(STRING_TO_TYPE); 281 mLatinIME.onUpdateSelection(0, 0, typedLength, typedLength, -1, -1); 282 mInputConnection.setSelection(NEW_CURSOR_POSITION, NEW_CURSOR_POSITION); 283 mLatinIME.onUpdateSelection(typedLength, typedLength, 284 NEW_CURSOR_POSITION, NEW_CURSOR_POSITION, -1, -1); 285 type(Constants.CODE_DELETE); 286 assertEquals("auto correct then move cursor to start of line then backspace", 287 EXPECTED_RESULT, mEditText.getText().toString()); 288 } 289 290 public void testAutoCorrectThenMoveCursorThenBackspace() { 291 final String STRING_TO_TYPE = "and tgis "; 292 final int typedLength = STRING_TO_TYPE.length(); 293 final String EXPECTED_RESULT = "andthis "; 294 final int NEW_CURSOR_POSITION = STRING_TO_TYPE.indexOf('t'); 295 type(STRING_TO_TYPE); 296 mLatinIME.onUpdateSelection(0, 0, typedLength, typedLength, -1, -1); 297 mInputConnection.setSelection(NEW_CURSOR_POSITION, NEW_CURSOR_POSITION); 298 mLatinIME.onUpdateSelection(typedLength, typedLength, 299 NEW_CURSOR_POSITION, NEW_CURSOR_POSITION, -1, -1); 300 type(Constants.CODE_DELETE); 301 assertEquals("auto correct then move cursor then backspace", 302 EXPECTED_RESULT, mEditText.getText().toString()); 303 } 304 305 public void testNoSpaceAfterManualPick() { 306 final String WORD_TO_TYPE = "this"; 307 final String EXPECTED_RESULT = WORD_TO_TYPE; 308 type(WORD_TO_TYPE); 309 pickSuggestionManually(WORD_TO_TYPE); 310 assertEquals("no space after manual pick", EXPECTED_RESULT, 311 mEditText.getText().toString()); 312 } 313 314 public void testManualPickThenType() { 315 final String WORD1_TO_TYPE = "this"; 316 final String WORD2_TO_TYPE = "is"; 317 final String EXPECTED_RESULT = "this is"; 318 type(WORD1_TO_TYPE); 319 pickSuggestionManually(WORD1_TO_TYPE); 320 type(WORD2_TO_TYPE); 321 assertEquals("manual pick then type", EXPECTED_RESULT, mEditText.getText().toString()); 322 } 323 324 public void testManualPickThenSeparator() { 325 final String WORD1_TO_TYPE = "this"; 326 final String WORD2_TO_TYPE = "!"; 327 final String EXPECTED_RESULT = "this!"; 328 type(WORD1_TO_TYPE); 329 pickSuggestionManually(WORD1_TO_TYPE); 330 type(WORD2_TO_TYPE); 331 assertEquals("manual pick then separator", EXPECTED_RESULT, mEditText.getText().toString()); 332 } 333 334 // This test matches the one in InputLogicTestsNonEnglish. In some non-English languages, 335 // ! and ? are clustering punctuation signs. 336 public void testClusteringPunctuation() { 337 final String WORD1_TO_TYPE = "test"; 338 final String WORD2_TO_TYPE = "!!?!:!"; 339 final String EXPECTED_RESULT = "test!!?!:!"; 340 type(WORD1_TO_TYPE); 341 pickSuggestionManually(WORD1_TO_TYPE); 342 type(WORD2_TO_TYPE); 343 assertEquals("clustering punctuation", EXPECTED_RESULT, mEditText.getText().toString()); 344 } 345 346 public void testManualPickThenStripperThenPick() { 347 final String WORD_TO_TYPE = "this"; 348 final String STRIPPER = "\n"; 349 final String EXPECTED_RESULT = "this\nthis"; 350 type(WORD_TO_TYPE); 351 pickSuggestionManually(WORD_TO_TYPE); 352 type(STRIPPER); 353 type(WORD_TO_TYPE); 354 pickSuggestionManually(WORD_TO_TYPE); 355 assertEquals("manual pick then \\n then manual pick", EXPECTED_RESULT, 356 mEditText.getText().toString()); 357 } 358 359 public void testManualPickThenSpaceThenType() { 360 final String WORD1_TO_TYPE = "this"; 361 final String WORD2_TO_TYPE = " is"; 362 final String EXPECTED_RESULT = "this is"; 363 type(WORD1_TO_TYPE); 364 pickSuggestionManually(WORD1_TO_TYPE); 365 type(WORD2_TO_TYPE); 366 assertEquals("manual pick then space then type", EXPECTED_RESULT, 367 mEditText.getText().toString()); 368 } 369 370 public void testManualPickThenManualPick() { 371 final String WORD1_TO_TYPE = "this"; 372 final String WORD2_TO_PICK = "is"; 373 final String EXPECTED_RESULT = "this is"; 374 type(WORD1_TO_TYPE); 375 pickSuggestionManually(WORD1_TO_TYPE); 376 // Here we fake picking a word through bigram prediction. 377 pickSuggestionManually(WORD2_TO_PICK); 378 assertEquals("manual pick then manual pick", EXPECTED_RESULT, 379 mEditText.getText().toString()); 380 } 381 382 public void testDeleteWholeComposingWord() { 383 final String WORD_TO_TYPE = "this"; 384 type(WORD_TO_TYPE); 385 for (int i = 0; i < WORD_TO_TYPE.length(); ++i) { 386 type(Constants.CODE_DELETE); 387 } 388 assertEquals("delete whole composing word", "", mEditText.getText().toString()); 389 } 390 391 public void testResumeSuggestionOnBackspace() { 392 final String STRING_TO_TYPE = "and this "; 393 final int typedLength = STRING_TO_TYPE.length(); 394 type(STRING_TO_TYPE); 395 assertEquals("resume suggestion on backspace", -1, 396 BaseInputConnection.getComposingSpanStart(mEditText.getText())); 397 assertEquals("resume suggestion on backspace", -1, 398 BaseInputConnection.getComposingSpanEnd(mEditText.getText())); 399 mLatinIME.onUpdateSelection(0, 0, typedLength, typedLength, -1, -1); 400 type(Constants.CODE_DELETE); 401 assertEquals("resume suggestion on backspace", 4, 402 BaseInputConnection.getComposingSpanStart(mEditText.getText())); 403 assertEquals("resume suggestion on backspace", 8, 404 BaseInputConnection.getComposingSpanEnd(mEditText.getText())); 405 } 406 407 private void helperTestComposing(final String wordToType, final boolean shouldBeComposing) { 408 mEditText.setText(""); 409 type(wordToType); 410 assertEquals("start composing inside text", shouldBeComposing ? 0 : -1, 411 BaseInputConnection.getComposingSpanStart(mEditText.getText())); 412 assertEquals("start composing inside text", shouldBeComposing ? wordToType.length() : -1, 413 BaseInputConnection.getComposingSpanEnd(mEditText.getText())); 414 } 415 416 public void testStartComposing() { 417 // Should start composing on a letter 418 helperTestComposing("a", true); 419 type(" "); // To reset the composing state 420 // Should not start composing on quote 421 helperTestComposing("'", false); 422 type(" "); 423 helperTestComposing("'-", false); 424 type(" "); 425 // Should not start composing on dash 426 helperTestComposing("-", false); 427 type(" "); 428 helperTestComposing("-'", false); 429 type(" "); 430 helperTestComposing("a-", true); 431 type(" "); 432 helperTestComposing("a'", true); 433 } 434 // TODO: Add some tests for non-BMP characters 435 436 public void testAutoCorrectByUserHistory() { 437 final String WORD_TO_BE_CORRECTED = "qpmx"; 438 final String NOT_CORRECTED_RESULT = "qpmx "; 439 final String DESIRED_WORD = "qpmz"; 440 final String CORRECTED_RESULT = "qpmz "; 441 final int typeCountNotToAutocorrect = 1; 442 final int typeCountToAutoCorrect = 16; 443 int startIndex = 0; 444 int endIndex = 0; 445 446 for (int i = 0; i < typeCountNotToAutocorrect; i++) { 447 type(DESIRED_WORD); 448 type(Constants.CODE_SPACE); 449 } 450 startIndex = mEditText.getText().length(); 451 type(WORD_TO_BE_CORRECTED); 452 type(Constants.CODE_SPACE); 453 endIndex = mEditText.getText().length(); 454 assertEquals("not auto-corrected by user history", NOT_CORRECTED_RESULT, 455 mEditText.getText().subSequence(startIndex, endIndex).toString()); 456 for (int i = typeCountNotToAutocorrect; i < typeCountToAutoCorrect; i++) { 457 type(DESIRED_WORD); 458 type(Constants.CODE_SPACE); 459 } 460 startIndex = mEditText.getText().length(); 461 type(WORD_TO_BE_CORRECTED); 462 type(Constants.CODE_SPACE); 463 endIndex = mEditText.getText().length(); 464 assertEquals("auto-corrected by user history", 465 CORRECTED_RESULT, mEditText.getText().subSequence(startIndex, endIndex).toString()); 466 } 467 468 public void testPredictionsAfterSpace() { 469 final String WORD_TO_TYPE = "Barack "; 470 type(WORD_TO_TYPE); 471 sleep(DELAY_TO_WAIT_FOR_PREDICTIONS); 472 runMessages(); 473 // Test the first prediction is displayed 474 final SuggestedWords suggestedWords = mLatinIME.getSuggestedWordsForTest(); 475 assertEquals("predictions after space", "Obama", 476 suggestedWords.size() > 0 ? suggestedWords.getWord(0) : null); 477 } 478 479 public void testPredictionsWithDoubleSpaceToPeriod() { 480 mLatinIME.clearPersonalizedDictionariesForTest(); 481 final String WORD_TO_TYPE = "Barack "; 482 type(WORD_TO_TYPE); 483 sleep(DELAY_TO_WAIT_FOR_PREDICTIONS); 484 runMessages(); 485 // No need to test here, testPredictionsAfterSpace is testing it already 486 type(" "); 487 sleep(DELAY_TO_WAIT_FOR_PREDICTIONS); 488 runMessages(); 489 // Test the predictions have been cleared 490 SuggestedWords suggestedWords = mLatinIME.getSuggestedWordsForTest(); 491 assertEquals("predictions cleared after double-space-to-period", suggestedWords.size(), 0); 492 type(Constants.CODE_DELETE); 493 sleep(DELAY_TO_WAIT_FOR_PREDICTIONS); 494 runMessages(); 495 // Test the first prediction is displayed 496 suggestedWords = mLatinIME.getSuggestedWordsForTest(); 497 assertEquals("predictions after cancel double-space-to-period", "Obama", 498 suggestedWords.size() > 0 ? suggestedWords.getWord(0) : null); 499 } 500 501 public void testPredictionsAfterManualPick() { 502 final String WORD_TO_TYPE = "Barack"; 503 type(WORD_TO_TYPE); 504 // Choose the auto-correction. For "Barack", the auto-correction should be "Barack". 505 pickSuggestionManually(WORD_TO_TYPE); 506 sleep(DELAY_TO_WAIT_FOR_PREDICTIONS); 507 runMessages(); 508 // Test the first prediction is displayed 509 final SuggestedWords suggestedWords = mLatinIME.getSuggestedWordsForTest(); 510 assertEquals("predictions after manual pick", "Obama", 511 suggestedWords.size() > 0 ? suggestedWords.getWord(0) : null); 512 } 513 514 public void testPredictionsAfterPeriod() { 515 mLatinIME.clearPersonalizedDictionariesForTest(); 516 final String WORD_TO_TYPE = "Barack. "; 517 type(WORD_TO_TYPE); 518 sleep(DELAY_TO_WAIT_FOR_PREDICTIONS); 519 runMessages(); 520 SuggestedWords suggestedWords = mLatinIME.getSuggestedWordsForTest(); 521 assertEquals("No prediction after period after inputting once.", 0, suggestedWords.size()); 522 523 type(WORD_TO_TYPE); 524 sleep(DELAY_TO_WAIT_FOR_PREDICTIONS); 525 runMessages(); 526 suggestedWords = mLatinIME.getSuggestedWordsForTest(); 527 assertEquals("Beginning-of-Sentence prediction after inputting 2 times.", "Barack", 528 suggestedWords.size() > 0 ? suggestedWords.getWord(0) : null); 529 } 530 531 public void testPredictionsAfterRecorrection() { 532 final String PREFIX = "A "; 533 final String WORD_TO_TYPE = "Barack"; 534 final String FIRST_NON_TYPED_SUGGESTION = "Barrack"; 535 final int endOfPrefix = PREFIX.length(); 536 final int endOfWord = endOfPrefix + WORD_TO_TYPE.length(); 537 final int endOfSuggestion = endOfPrefix + FIRST_NON_TYPED_SUGGESTION.length(); 538 final int indexForManualCursor = endOfPrefix + 3; // +3 because it's after "Bar" in "Barack" 539 type(PREFIX); 540 mLatinIME.onUpdateSelection(0, 0, endOfPrefix, endOfPrefix, -1, -1); 541 type(WORD_TO_TYPE); 542 pickSuggestionManually(FIRST_NON_TYPED_SUGGESTION); 543 mLatinIME.onUpdateSelection(endOfPrefix, endOfPrefix, endOfSuggestion, endOfSuggestion, 544 -1, -1); 545 runMessages(); 546 type(" "); 547 mLatinIME.onUpdateSelection(endOfSuggestion, endOfSuggestion, 548 endOfSuggestion + 1, endOfSuggestion + 1, -1, -1); 549 sleep(DELAY_TO_WAIT_FOR_PREDICTIONS); 550 runMessages(); 551 // Simulate a manual cursor move 552 mInputConnection.setSelection(indexForManualCursor, indexForManualCursor); 553 mLatinIME.onUpdateSelection(endOfSuggestion + 1, endOfSuggestion + 1, 554 indexForManualCursor, indexForManualCursor, -1, -1); 555 sleep(DELAY_TO_WAIT_FOR_PREDICTIONS); 556 runMessages(); 557 pickSuggestionManually(WORD_TO_TYPE); 558 mLatinIME.onUpdateSelection(indexForManualCursor, indexForManualCursor, 559 endOfWord, endOfWord, -1, -1); 560 sleep(DELAY_TO_WAIT_FOR_PREDICTIONS); 561 runMessages(); 562 // Test the first prediction is displayed 563 final SuggestedWords suggestedWords = mLatinIME.getSuggestedWordsForTest(); 564 assertEquals("predictions after recorrection", "Obama", 565 suggestedWords.size() > 0 ? suggestedWords.getWord(0) : null); 566 } 567 568 public void testComposingMultipleBackspace() { 569 final String WORD_TO_TYPE = "radklro"; 570 final int TIMES_TO_TYPE = 3; 571 final int TIMES_TO_BACKSPACE = 8; 572 type(WORD_TO_TYPE); 573 type(Constants.CODE_DELETE); 574 type(Constants.CODE_DELETE); 575 type(Constants.CODE_DELETE); 576 type(WORD_TO_TYPE); 577 type(Constants.CODE_DELETE); 578 type(Constants.CODE_DELETE); 579 type(WORD_TO_TYPE); 580 type(Constants.CODE_DELETE); 581 type(Constants.CODE_DELETE); 582 type(Constants.CODE_DELETE); 583 assertEquals("composing with multiple backspace", 584 WORD_TO_TYPE.length() * TIMES_TO_TYPE - TIMES_TO_BACKSPACE, 585 mEditText.getText().length()); 586 } 587 588 public void testManySingleQuotes() { 589 final String WORD_TO_AUTOCORRECT = "i"; 590 final String WORD_AUTOCORRECTED = "I"; 591 final String QUOTES = "''''''''''''''''''''"; 592 final String WORD_TO_TYPE = WORD_TO_AUTOCORRECT + QUOTES + " "; 593 final String EXPECTED_RESULT = WORD_AUTOCORRECTED + QUOTES + " "; 594 type(WORD_TO_TYPE); 595 assertEquals("auto-correct with many trailing single quotes", EXPECTED_RESULT, 596 mEditText.getText().toString()); 597 } 598 599 public void testManySingleQuotesOneByOne() { 600 final String WORD_TO_AUTOCORRECT = "i"; 601 final String WORD_AUTOCORRECTED = "I"; 602 final String QUOTES = "''''''''''''''''''''"; 603 final String WORD_TO_TYPE = WORD_TO_AUTOCORRECT + QUOTES + " "; 604 final String EXPECTED_RESULT = WORD_AUTOCORRECTED + QUOTES + " "; 605 606 for (int i = 0; i < WORD_TO_TYPE.length(); ++i) { 607 type(WORD_TO_TYPE.substring(i, i+1)); 608 sleep(DELAY_TO_WAIT_FOR_PREDICTIONS); 609 runMessages(); 610 } 611 assertEquals("type many trailing single quotes one by one", EXPECTED_RESULT, 612 mEditText.getText().toString()); 613 } 614 615 public void testTypingSingleQuotesOneByOne() { 616 final String WORD_TO_TYPE = "it's "; 617 final String EXPECTED_RESULT = WORD_TO_TYPE; 618 for (int i = 0; i < WORD_TO_TYPE.length(); ++i) { 619 type(WORD_TO_TYPE.substring(i, i+1)); 620 sleep(DELAY_TO_WAIT_FOR_PREDICTIONS); 621 runMessages(); 622 } 623 assertEquals("type words letter by letter", EXPECTED_RESULT, 624 mEditText.getText().toString()); 625 } 626 627 public void testSwitchLanguages() { 628 final String WORD_TO_TYPE_FIRST_PART = "com"; 629 final String WORD_TO_TYPE_SECOND_PART = "md"; 630 final String EXPECTED_RESULT = "comme"; 631 changeLanguage("en"); 632 type(WORD_TO_TYPE_FIRST_PART); 633 changeLanguage("fr"); 634 runMessages(); 635 type(WORD_TO_TYPE_SECOND_PART); 636 sleep(DELAY_TO_WAIT_FOR_UNDERLINE); 637 runMessages(); 638 final SuggestedWords suggestedWords = mLatinIME.getSuggestedWordsForTest(); 639 assertEquals("Suggestions updated after switching languages", 640 EXPECTED_RESULT, suggestedWords.size() > 0 ? suggestedWords.getWord(1) : null); 641 } 642} 643