Add tests for type through recorrection

Also introduce some helper methods

Change-Id: I0f99733a4b7f0318b2b07217b614c19ea98859fc
This commit is contained in:
Jean Chalard 2014-10-15 17:20:16 +09:00
parent 1249395563
commit 7e930bfb3a
2 changed files with 95 additions and 31 deletions

View file

@ -37,7 +37,7 @@ public class InputLogicTests extends InputTestsBase {
final String EXPECTED_RESULT = "thi"; final String EXPECTED_RESULT = "thi";
type(WORD_TO_TYPE); type(WORD_TO_TYPE);
pickSuggestionManually(WORD_TO_TYPE); pickSuggestionManually(WORD_TO_TYPE);
mLatinIME.onUpdateSelection(0, 0, WORD_TO_TYPE.length(), WORD_TO_TYPE.length(), -1, -1); sendUpdateForCursorMoveTo(WORD_TO_TYPE.length());
type(Constants.CODE_DELETE); type(Constants.CODE_DELETE);
assertEquals("press suggestion then backspace", EXPECTED_RESULT, assertEquals("press suggestion then backspace", EXPECTED_RESULT,
mEditText.getText().toString()); mEditText.getText().toString());
@ -50,7 +50,7 @@ public class InputLogicTests extends InputTestsBase {
type(WORD_TO_TYPE); type(WORD_TO_TYPE);
// Choose the auto-correction. For "tgis", the auto-correction should be "this". // Choose the auto-correction. For "tgis", the auto-correction should be "this".
pickSuggestionManually(WORD_TO_PICK); pickSuggestionManually(WORD_TO_PICK);
mLatinIME.onUpdateSelection(0, 0, WORD_TO_TYPE.length(), WORD_TO_TYPE.length(), -1, -1); sendUpdateForCursorMoveTo(WORD_TO_TYPE.length());
assertEquals("pick typed word over auto-correction then backspace", WORD_TO_PICK, assertEquals("pick typed word over auto-correction then backspace", WORD_TO_PICK,
mEditText.getText().toString()); mEditText.getText().toString());
type(Constants.CODE_DELETE); type(Constants.CODE_DELETE);
@ -64,7 +64,7 @@ public class InputLogicTests extends InputTestsBase {
type(WORD_TO_TYPE); type(WORD_TO_TYPE);
// Choose the typed word. // Choose the typed word.
pickSuggestionManually(WORD_TO_TYPE); pickSuggestionManually(WORD_TO_TYPE);
mLatinIME.onUpdateSelection(0, 0, WORD_TO_TYPE.length(), WORD_TO_TYPE.length(), -1, -1); sendUpdateForCursorMoveTo(WORD_TO_TYPE.length());
assertEquals("pick typed word over auto-correction then backspace", WORD_TO_TYPE, assertEquals("pick typed word over auto-correction then backspace", WORD_TO_TYPE,
mEditText.getText().toString()); mEditText.getText().toString());
type(Constants.CODE_DELETE); type(Constants.CODE_DELETE);
@ -79,7 +79,7 @@ public class InputLogicTests extends InputTestsBase {
type(WORD_TO_TYPE); type(WORD_TO_TYPE);
// Choose the second suggestion, which should be "thus" when "tgis" is typed. // Choose the second suggestion, which should be "thus" when "tgis" is typed.
pickSuggestionManually(WORD_TO_PICK); pickSuggestionManually(WORD_TO_PICK);
mLatinIME.onUpdateSelection(0, 0, WORD_TO_TYPE.length(), WORD_TO_TYPE.length(), -1, -1); sendUpdateForCursorMoveTo(WORD_TO_TYPE.length());
assertEquals("pick different suggestion then backspace", WORD_TO_PICK, assertEquals("pick different suggestion then backspace", WORD_TO_PICK,
mEditText.getText().toString()); mEditText.getText().toString());
type(Constants.CODE_DELETE); type(Constants.CODE_DELETE);
@ -94,7 +94,8 @@ public class InputLogicTests extends InputTestsBase {
final int SELECTION_END = 19; final int SELECTION_END = 19;
final String EXPECTED_RESULT = "some text some text"; final String EXPECTED_RESULT = "some text some text";
type(STRING_TO_TYPE); type(STRING_TO_TYPE);
// There is no IMF to call onUpdateSelection for us so we must do it by hand. // Don't use the sendUpdateForCursorMove* family of methods here because they
// don't handle selections.
// Send once to simulate the cursor actually responding to the move caused by typing. // Send once to simulate the cursor actually responding to the move caused by typing.
// This is necessary because LatinIME is bookkeeping to avoid confusing a real cursor // This is necessary because LatinIME is bookkeeping to avoid confusing a real cursor
// move with a move triggered by LatinIME inputting stuff. // move with a move triggered by LatinIME inputting stuff.
@ -114,7 +115,8 @@ public class InputLogicTests extends InputTestsBase {
final int SELECTION_END = 19; final int SELECTION_END = 19;
final String EXPECTED_RESULT = "some text some text"; final String EXPECTED_RESULT = "some text some text";
type(STRING_TO_TYPE); type(STRING_TO_TYPE);
// There is no IMF to call onUpdateSelection for us so we must do it by hand. // Don't use the sendUpdateForCursorMove* family of methods here because they
// don't handle selections.
// Send once to simulate the cursor actually responding to the move caused by typing. // Send once to simulate the cursor actually responding to the move caused by typing.
// This is necessary because LatinIME is bookkeeping to avoid confusing a real cursor // This is necessary because LatinIME is bookkeeping to avoid confusing a real cursor
// move with a move triggered by LatinIME inputting stuff. // move with a move triggered by LatinIME inputting stuff.
@ -153,7 +155,7 @@ public class InputLogicTests extends InputTestsBase {
final String STRING_TO_TYPE = "tgis."; final String STRING_TO_TYPE = "tgis.";
final String EXPECTED_RESULT = "tgis."; final String EXPECTED_RESULT = "tgis.";
type(STRING_TO_TYPE); type(STRING_TO_TYPE);
mLatinIME.onUpdateSelection(0, 0, STRING_TO_TYPE.length(), STRING_TO_TYPE.length(), -1, -1); sendUpdateForCursorMoveTo(STRING_TO_TYPE.length());
type(Constants.CODE_DELETE); type(Constants.CODE_DELETE);
assertEquals("auto-correct with period then revert", EXPECTED_RESULT, assertEquals("auto-correct with period then revert", EXPECTED_RESULT,
mEditText.getText().toString()); mEditText.getText().toString());
@ -166,7 +168,7 @@ public class InputLogicTests extends InputTestsBase {
final String STRING_TO_TYPE = "tgis "; final String STRING_TO_TYPE = "tgis ";
final String EXPECTED_RESULT = "tgis"; final String EXPECTED_RESULT = "tgis";
type(STRING_TO_TYPE); type(STRING_TO_TYPE);
mLatinIME.onUpdateSelection(0, 0, STRING_TO_TYPE.length(), STRING_TO_TYPE.length(), -1, -1); sendUpdateForCursorMoveTo(STRING_TO_TYPE.length());
type(Constants.CODE_DELETE); type(Constants.CODE_DELETE);
assertEquals("auto-correct with space then revert", EXPECTED_RESULT, assertEquals("auto-correct with space then revert", EXPECTED_RESULT,
mEditText.getText().toString()); mEditText.getText().toString());
@ -177,15 +179,12 @@ public class InputLogicTests extends InputTestsBase {
final String STRING_TO_TYPE_SECOND = "a"; final String STRING_TO_TYPE_SECOND = "a";
final String EXPECTED_RESULT = "tgis a"; final String EXPECTED_RESULT = "tgis a";
type(STRING_TO_TYPE_FIRST); type(STRING_TO_TYPE_FIRST);
mLatinIME.onUpdateSelection(0, 0, sendUpdateForCursorMoveTo(STRING_TO_TYPE_FIRST.length());
STRING_TO_TYPE_FIRST.length(), STRING_TO_TYPE_FIRST.length(), -1, -1);
type(Constants.CODE_DELETE); type(Constants.CODE_DELETE);
type(STRING_TO_TYPE_SECOND); type(STRING_TO_TYPE_SECOND);
mLatinIME.onUpdateSelection(STRING_TO_TYPE_FIRST.length(), STRING_TO_TYPE_FIRST.length(), sendUpdateForCursorMoveTo(STRING_TO_TYPE_FIRST.length() - 1
STRING_TO_TYPE_FIRST.length() - 1 + STRING_TO_TYPE_SECOND.length(), + STRING_TO_TYPE_SECOND.length());
STRING_TO_TYPE_FIRST.length() - 1 + STRING_TO_TYPE_SECOND.length(),
-1, -1);
assertEquals("auto-correct with space then revert then type more", EXPECTED_RESULT, assertEquals("auto-correct with space then revert then type more", EXPECTED_RESULT,
mEditText.getText().toString()); mEditText.getText().toString());
} }
@ -194,7 +193,7 @@ public class InputLogicTests extends InputTestsBase {
final String STRING_TO_TYPE = "this "; final String STRING_TO_TYPE = "this ";
final String EXPECTED_RESULT = "this"; final String EXPECTED_RESULT = "this";
type(STRING_TO_TYPE); type(STRING_TO_TYPE);
mLatinIME.onUpdateSelection(0, 0, STRING_TO_TYPE.length(), STRING_TO_TYPE.length(), -1, -1); sendUpdateForCursorMoveTo(STRING_TO_TYPE.length());
type(Constants.CODE_DELETE); type(Constants.CODE_DELETE);
assertEquals("auto-correct with space does not revert", EXPECTED_RESULT, assertEquals("auto-correct with space does not revert", EXPECTED_RESULT,
mEditText.getText().toString()); mEditText.getText().toString());
@ -298,10 +297,9 @@ public class InputLogicTests extends InputTestsBase {
final String EXPECTED_RESULT = "this "; final String EXPECTED_RESULT = "this ";
final int NEW_CURSOR_POSITION = 0; final int NEW_CURSOR_POSITION = 0;
type(STRING_TO_TYPE); type(STRING_TO_TYPE);
mLatinIME.onUpdateSelection(0, 0, typedLength, typedLength, -1, -1); sendUpdateForCursorMoveTo(typedLength);
mInputConnection.setSelection(NEW_CURSOR_POSITION, NEW_CURSOR_POSITION); mInputConnection.setSelection(NEW_CURSOR_POSITION, NEW_CURSOR_POSITION);
mLatinIME.onUpdateSelection(typedLength, typedLength, sendUpdateForCursorMoveTo(NEW_CURSOR_POSITION);
NEW_CURSOR_POSITION, NEW_CURSOR_POSITION, -1, -1);
type(Constants.CODE_DELETE); type(Constants.CODE_DELETE);
assertEquals("auto correct then move cursor to start of line then backspace", assertEquals("auto correct then move cursor to start of line then backspace",
EXPECTED_RESULT, mEditText.getText().toString()); EXPECTED_RESULT, mEditText.getText().toString());
@ -313,10 +311,9 @@ public class InputLogicTests extends InputTestsBase {
final String EXPECTED_RESULT = "andthis "; final String EXPECTED_RESULT = "andthis ";
final int NEW_CURSOR_POSITION = STRING_TO_TYPE.indexOf('t'); final int NEW_CURSOR_POSITION = STRING_TO_TYPE.indexOf('t');
type(STRING_TO_TYPE); type(STRING_TO_TYPE);
mLatinIME.onUpdateSelection(0, 0, typedLength, typedLength, -1, -1); sendUpdateForCursorMoveTo(typedLength);
mInputConnection.setSelection(NEW_CURSOR_POSITION, NEW_CURSOR_POSITION); mInputConnection.setSelection(NEW_CURSOR_POSITION, NEW_CURSOR_POSITION);
mLatinIME.onUpdateSelection(typedLength, typedLength, sendUpdateForCursorMoveTo(NEW_CURSOR_POSITION);
NEW_CURSOR_POSITION, NEW_CURSOR_POSITION, -1, -1);
type(Constants.CODE_DELETE); type(Constants.CODE_DELETE);
assertEquals("auto correct then move cursor then backspace", assertEquals("auto correct then move cursor then backspace",
EXPECTED_RESULT, mEditText.getText().toString()); EXPECTED_RESULT, mEditText.getText().toString());
@ -416,7 +413,7 @@ public class InputLogicTests extends InputTestsBase {
BaseInputConnection.getComposingSpanStart(mEditText.getText())); BaseInputConnection.getComposingSpanStart(mEditText.getText()));
assertEquals("resume suggestion on backspace", -1, assertEquals("resume suggestion on backspace", -1,
BaseInputConnection.getComposingSpanEnd(mEditText.getText())); BaseInputConnection.getComposingSpanEnd(mEditText.getText()));
mLatinIME.onUpdateSelection(0, 0, typedLength, typedLength, -1, -1); sendUpdateForCursorMoveTo(typedLength);
type(Constants.CODE_DELETE); type(Constants.CODE_DELETE);
assertEquals("resume suggestion on backspace", 4, assertEquals("resume suggestion on backspace", 4,
BaseInputConnection.getComposingSpanStart(mEditText.getText())); BaseInputConnection.getComposingSpanStart(mEditText.getText()));
@ -557,26 +554,22 @@ public class InputLogicTests extends InputTestsBase {
final int endOfSuggestion = endOfPrefix + FIRST_NON_TYPED_SUGGESTION.length(); final int endOfSuggestion = endOfPrefix + FIRST_NON_TYPED_SUGGESTION.length();
final int indexForManualCursor = endOfPrefix + 3; // +3 because it's after "Bar" in "Barack" final int indexForManualCursor = endOfPrefix + 3; // +3 because it's after "Bar" in "Barack"
type(PREFIX); type(PREFIX);
mLatinIME.onUpdateSelection(0, 0, endOfPrefix, endOfPrefix, -1, -1); sendUpdateForCursorMoveTo(endOfPrefix);
type(WORD_TO_TYPE); type(WORD_TO_TYPE);
pickSuggestionManually(FIRST_NON_TYPED_SUGGESTION); pickSuggestionManually(FIRST_NON_TYPED_SUGGESTION);
mLatinIME.onUpdateSelection(endOfPrefix, endOfPrefix, endOfSuggestion, endOfSuggestion, sendUpdateForCursorMoveTo(endOfSuggestion);
-1, -1);
runMessages(); runMessages();
type(" "); type(" ");
mLatinIME.onUpdateSelection(endOfSuggestion, endOfSuggestion, sendUpdateForCursorMoveBy(1);
endOfSuggestion + 1, endOfSuggestion + 1, -1, -1);
sleep(DELAY_TO_WAIT_FOR_PREDICTIONS_MILLIS); sleep(DELAY_TO_WAIT_FOR_PREDICTIONS_MILLIS);
runMessages(); runMessages();
// Simulate a manual cursor move // Simulate a manual cursor move
mInputConnection.setSelection(indexForManualCursor, indexForManualCursor); mInputConnection.setSelection(indexForManualCursor, indexForManualCursor);
mLatinIME.onUpdateSelection(endOfSuggestion + 1, endOfSuggestion + 1, sendUpdateForCursorMoveTo(indexForManualCursor);
indexForManualCursor, indexForManualCursor, -1, -1);
sleep(DELAY_TO_WAIT_FOR_PREDICTIONS_MILLIS); sleep(DELAY_TO_WAIT_FOR_PREDICTIONS_MILLIS);
runMessages(); runMessages();
pickSuggestionManually(WORD_TO_TYPE); pickSuggestionManually(WORD_TO_TYPE);
mLatinIME.onUpdateSelection(indexForManualCursor, indexForManualCursor, sendUpdateForCursorMoveTo(endOfWord);
endOfWord, endOfWord, -1, -1);
sleep(DELAY_TO_WAIT_FOR_PREDICTIONS_MILLIS); sleep(DELAY_TO_WAIT_FOR_PREDICTIONS_MILLIS);
runMessages(); runMessages();
// Test the first prediction is displayed // Test the first prediction is displayed
@ -679,4 +672,38 @@ public class InputLogicTests extends InputTestsBase {
MoreAsserts.assertNotEqual("gesture twice the same thing", "this", MoreAsserts.assertNotEqual("gesture twice the same thing", "this",
mEditText.getText().toString()); mEditText.getText().toString());
} }
private void typeWordAndPutCursorInside(final String word, final int startPos) {
final int END_OF_WORD = startPos + word.length();
final int NEW_CURSOR_POSITION = startPos + word.length() / 2;
type(word);
sendUpdateForCursorMoveTo(END_OF_WORD);
runMessages();
sendUpdateForCursorMoveTo(NEW_CURSOR_POSITION);
sleep(DELAY_TO_WAIT_FOR_UNDERLINE_MILLIS);
runMessages();
ensureComposingSpanPos("move cursor inside word leaves composing span in the right place",
startPos, END_OF_WORD);
}
private void ensureComposingSpanPos(final String message, final int from, final int to) {
assertEquals(message, from, BaseInputConnection.getComposingSpanStart(mEditText.getText()));
assertEquals(message, to, BaseInputConnection.getComposingSpanEnd(mEditText.getText()));
}
public void testTypeWithinComposing() {
final String WORD_TO_TYPE = "something";
final String EXPECTED_RESULT = "some thing";
typeWordAndPutCursorInside(WORD_TO_TYPE, 0 /* startPos */);
type(" ");
ensureComposingSpanPos("space while in the middle of a word cancels composition", -1, -1);
assertEquals("space in the middle of a composing word", EXPECTED_RESULT,
mEditText.getText().toString());
int cursorPos = sendUpdateForCursorMoveToEndOfLine();
runMessages();
type(" ");
typeWordAndPutCursorInside(WORD_TO_TYPE, cursorPos + 1 /* startPos */);
type(Constants.CODE_DELETE);
ensureComposingSpanPos("space while in the middle of a word cancels composition", -1, -1);
}
} }

View file

@ -192,6 +192,7 @@ public class InputTestsBase extends ServiceTestCase<LatinIMEForTests> {
| InputType.TYPE_TEXT_FLAG_MULTI_LINE; | InputType.TYPE_TEXT_FLAG_MULTI_LINE;
mEditText.setInputType(inputType); mEditText.setInputType(inputType);
mEditText.setEnabled(true); mEditText.setEnabled(true);
mLastCursorPos = 0;
if (null == Looper.myLooper()) { if (null == Looper.myLooper()) {
Looper.prepare(); Looper.prepare();
} }
@ -413,4 +414,40 @@ public class InputTestsBase extends ServiceTestCase<LatinIMEForTests> {
Thread.sleep(milliseconds); Thread.sleep(milliseconds);
} catch (InterruptedException e) {} } catch (InterruptedException e) {}
} }
// Some helper methods to manage the mock cursor position
// DO NOT CALL LatinIME#onUpdateSelection IF YOU WANT TO USE THOSE
int mLastCursorPos = 0;
/**
* Move the cached cursor position to the passed position and send onUpdateSelection to LatinIME
*/
protected int sendUpdateForCursorMoveTo(final int position) {
mInputConnection.setSelection(position, position);
mLatinIME.onUpdateSelection(mLastCursorPos, mLastCursorPos, position, position, -1, -1);
mLastCursorPos = position;
return position;
}
/**
* Move the cached cursor position by the passed amount and send onUpdateSelection to LatinIME
*/
protected int sendUpdateForCursorMoveBy(final int offset) {
final int lastPos = mEditText.getText().length();
final int requestedPosition = mLastCursorPos + offset;
if (requestedPosition < 0) {
return sendUpdateForCursorMoveTo(0);
} else if (requestedPosition > lastPos) {
return sendUpdateForCursorMoveTo(lastPos);
} else {
return sendUpdateForCursorMoveTo(requestedPosition);
}
}
/**
* Move the cached cursor position to the end of the line and send onUpdateSelection to LatinIME
*/
protected int sendUpdateForCursorMoveToEndOfLine() {
final int lastPos = mEditText.getText().length();
return sendUpdateForCursorMoveTo(lastPos);
}
} }