Merge "Rework the logic that tells if the cursor touches words"

main
Jean Chalard 2014-03-20 14:35:19 +00:00 committed by Android (Google) Code Review
commit 1dda9107ca
2 changed files with 105 additions and 10 deletions

View File

@ -687,14 +687,24 @@ public final class RichInputConnection {
} }
public boolean isCursorTouchingWord(final SpacingAndPunctuations spacingAndPunctuations) { public boolean isCursorTouchingWord(final SpacingAndPunctuations spacingAndPunctuations) {
final int codePointBeforeCursor = getCodePointBeforeCursor(); if (isCursorFollowedByWordCharacter(spacingAndPunctuations)) {
if (Constants.NOT_A_CODE == codePointBeforeCursor // If what's after the cursor is a word character, then we're touching a word.
|| spacingAndPunctuations.isWordSeparator(codePointBeforeCursor)
|| spacingAndPunctuations.isWordConnector(codePointBeforeCursor)) {
return isCursorFollowedByWordCharacter(spacingAndPunctuations);
}
return true; return true;
} }
final String textBeforeCursor = mCommittedTextBeforeComposingText.toString();
int indexOfCodePointInJavaChars = textBeforeCursor.length();
int consideredCodePoint = 0 == indexOfCodePointInJavaChars ? Constants.NOT_A_CODE
: textBeforeCursor.codePointBefore(indexOfCodePointInJavaChars);
// Search for the first non word-connector char
if (spacingAndPunctuations.isWordConnector(consideredCodePoint)) {
indexOfCodePointInJavaChars -= Character.charCount(consideredCodePoint);
consideredCodePoint = 0 == indexOfCodePointInJavaChars ? Constants.NOT_A_CODE
: textBeforeCursor.codePointBefore(indexOfCodePointInJavaChars);
}
return !(Constants.NOT_A_CODE == consideredCodePoint
|| spacingAndPunctuations.isWordSeparator(consideredCodePoint)
|| spacingAndPunctuations.isWordConnector(consideredCodePoint));
}
public boolean isCursorFollowedByWordCharacter( public boolean isCursorFollowedByWordCharacter(
final SpacingAndPunctuations spacingAndPunctuations) { final SpacingAndPunctuations spacingAndPunctuations) {

View File

@ -89,6 +89,10 @@ public class RichInputConnectionAndTextRangeTests extends AndroidTestCase {
mExtractedText = extractedText; mExtractedText = extractedText;
} }
public int cursorPos() {
return mTextBefore.length();
}
/* (non-Javadoc) /* (non-Javadoc)
* @see android.view.inputmethod.InputConnectionWrapper#getTextBeforeCursor(int, int) * @see android.view.inputmethod.InputConnectionWrapper#getTextBeforeCursor(int, int)
*/ */
@ -131,13 +135,16 @@ public class RichInputConnectionAndTextRangeTests extends AndroidTestCase {
} }
private class MockInputMethodService extends InputMethodService { private class MockInputMethodService extends InputMethodService {
InputConnection mInputConnection; private MockConnection mMockConnection;
public void setInputConnection(final InputConnection inputConnection) { public void setInputConnection(final MockConnection mockConnection) {
mInputConnection = inputConnection; mMockConnection = mockConnection;
}
public int cursorPos() {
return mMockConnection.cursorPos();
} }
@Override @Override
public InputConnection getCurrentInputConnection() { public InputConnection getCurrentInputConnection() {
return mInputConnection; return mMockConnection;
} }
} }
@ -336,4 +343,82 @@ public class RichInputConnectionAndTextRangeTests extends AndroidTestCase {
suggestions = r.getSuggestionSpansAtWord(); suggestions = r.getSuggestionSpansAtWord();
assertEquals(suggestions.length, 0); assertEquals(suggestions.length, 0);
} }
public void testCursorTouchingWord() {
final MockInputMethodService ims = new MockInputMethodService();
final RichInputConnection ic = new RichInputConnection(ims);
final SpacingAndPunctuations sap = mSpacingAndPunctuations;
ims.setInputConnection(new MockConnection("users", 5));
ic.resetCachesUponCursorMoveAndReturnSuccess(ims.cursorPos(), ims.cursorPos(), true);
assertTrue(ic.isCursorTouchingWord(sap));
ims.setInputConnection(new MockConnection("users'", 5));
ic.resetCachesUponCursorMoveAndReturnSuccess(ims.cursorPos(), ims.cursorPos(), true);
assertTrue(ic.isCursorTouchingWord(sap));
ims.setInputConnection(new MockConnection("users'", 6));
ic.resetCachesUponCursorMoveAndReturnSuccess(ims.cursorPos(), ims.cursorPos(), true);
assertTrue(ic.isCursorTouchingWord(sap));
ims.setInputConnection(new MockConnection("'users'", 6));
ic.resetCachesUponCursorMoveAndReturnSuccess(ims.cursorPos(), ims.cursorPos(), true);
assertTrue(ic.isCursorTouchingWord(sap));
ims.setInputConnection(new MockConnection("'users'", 7));
ic.resetCachesUponCursorMoveAndReturnSuccess(ims.cursorPos(), ims.cursorPos(), true);
assertTrue(ic.isCursorTouchingWord(sap));
ims.setInputConnection(new MockConnection("users '", 6));
ic.resetCachesUponCursorMoveAndReturnSuccess(ims.cursorPos(), ims.cursorPos(), true);
assertFalse(ic.isCursorTouchingWord(sap));
ims.setInputConnection(new MockConnection("users '", 7));
ic.resetCachesUponCursorMoveAndReturnSuccess(ims.cursorPos(), ims.cursorPos(), true);
assertFalse(ic.isCursorTouchingWord(sap));
ims.setInputConnection(new MockConnection("re-", 3));
ic.resetCachesUponCursorMoveAndReturnSuccess(ims.cursorPos(), ims.cursorPos(), true);
assertTrue(ic.isCursorTouchingWord(sap));
ims.setInputConnection(new MockConnection("re--", 4));
ic.resetCachesUponCursorMoveAndReturnSuccess(ims.cursorPos(), ims.cursorPos(), true);
assertFalse(ic.isCursorTouchingWord(sap));
ims.setInputConnection(new MockConnection("-", 1));
ic.resetCachesUponCursorMoveAndReturnSuccess(ims.cursorPos(), ims.cursorPos(), true);
assertFalse(ic.isCursorTouchingWord(sap));
ims.setInputConnection(new MockConnection("--", 2));
ic.resetCachesUponCursorMoveAndReturnSuccess(ims.cursorPos(), ims.cursorPos(), true);
assertFalse(ic.isCursorTouchingWord(sap));
ims.setInputConnection(new MockConnection(" -", 2));
ic.resetCachesUponCursorMoveAndReturnSuccess(ims.cursorPos(), ims.cursorPos(), true);
assertFalse(ic.isCursorTouchingWord(sap));
ims.setInputConnection(new MockConnection(" --", 3));
ic.resetCachesUponCursorMoveAndReturnSuccess(ims.cursorPos(), ims.cursorPos(), true);
assertFalse(ic.isCursorTouchingWord(sap));
ims.setInputConnection(new MockConnection(" users '", 1));
ic.resetCachesUponCursorMoveAndReturnSuccess(ims.cursorPos(), ims.cursorPos(), true);
assertTrue(ic.isCursorTouchingWord(sap));
ims.setInputConnection(new MockConnection(" users '", 3));
ic.resetCachesUponCursorMoveAndReturnSuccess(ims.cursorPos(), ims.cursorPos(), true);
assertTrue(ic.isCursorTouchingWord(sap));
ims.setInputConnection(new MockConnection(" users '", 7));
ic.resetCachesUponCursorMoveAndReturnSuccess(ims.cursorPos(), ims.cursorPos(), true);
assertFalse(ic.isCursorTouchingWord(sap));
ims.setInputConnection(new MockConnection(" users are", 7));
ic.resetCachesUponCursorMoveAndReturnSuccess(ims.cursorPos(), ims.cursorPos(), true);
assertTrue(ic.isCursorTouchingWord(sap));
ims.setInputConnection(new MockConnection(" users 'are", 7));
ic.resetCachesUponCursorMoveAndReturnSuccess(ims.cursorPos(), ims.cursorPos(), true);
assertFalse(ic.isCursorTouchingWord(sap));
}
} }