Skip to content

Commit

Permalink
Handle odd keyboard behaviors
Browse files Browse the repository at this point in the history
- Correctly tokenize when ", " is used instead of "," (or any split char)
- Fix an issue where completing a 3 letter token could re-add the 3 letters
	on the next letter added
  • Loading branch information
mgod committed Dec 5, 2017
1 parent d448601 commit 554cd7b
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@
import static android.support.test.espresso.matcher.ViewMatchers.isAssignableFrom;
import static android.support.test.espresso.matcher.ViewMatchers.withId;
import static android.support.test.espresso.matcher.ViewMatchers.withText;
import static com.tokenautocomplete.TokenMatchers.emailForPerson;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;

/**
Expand Down Expand Up @@ -66,6 +68,35 @@ public void keepsCloseNonHintComposingText() throws Exception {
.check(matches(withText(containsString("To:"))));
}

@Test
public void suppressesErroneousThreeLetterPreviousCompletions() throws Exception {
onView(withId(R.id.searchView))
.perform(typeText("mar,"))
.check(matches(emailForPerson(0, is("marshall@example.com"))))
.perform(forceComposingText("marz"))
.check(matches(withText(not(containsString("mar")))))
.check(matches(withText(containsString("z"))));
}

@Test
public void keepsReasonableThreeLetterCompletions() throws Exception {
onView(withId(R.id.searchView))
.perform(typeText("mar,"))
.check(matches(emailForPerson(0, is("marshall@example.com"))))
.perform(forceComposingText("maaz"))
.check(matches(withText(containsString("maaz"))));
}

@Test
public void ignoresSpacesWhenTheyAreAddedToSplitChars() {
//Many keyboards add "helpful" spaces after punctuation and we detect single characters
//for completions, so we need to ignore these spaces when looking for split characters
onView(withId(R.id.searchView))
.perform(typeText("mar"))
.perform(forceCommitText(", "))
.check(matches(emailForPerson(0, is("marshall@example.com"))));
}

//This is to emulate the behavior of some keyboards (Google Android O) to choose unusual text
public static ViewAction forceComposingText(final String text) {
return new ViewAction() {
Expand All @@ -87,4 +118,26 @@ public void perform(UiController uiController, View view) {
}
};
}

//This is to emulate the comma + space behavior of the Fleksy keyboard
public static ViewAction forceCommitText(final String text) {
return new ViewAction() {
@Override
public Matcher<View> getConstraints() {
return isAssignableFrom(ContactsCompletionView.class);
}

@Override
public String getDescription() {
return null;
}

@Override
public void perform(UiController uiController, View view) {
ContactsCompletionView completionView = (ContactsCompletionView)view;
InputConnection connection = completionView.testAccessibleInputConnection;
connection.commitText(text, 1);
}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ public boolean isSelectable() {

private int tokenLimit = -1;

private transient String lastCompletionText = null;

/**
* Add the TextChangedListeners
*/
Expand Down Expand Up @@ -172,9 +174,9 @@ public CharSequence filter(CharSequence source, int start, int end,
// Token limit check
if (tokenLimit != -1 && objects.size() == tokenLimit) {
return "";
} else if (source.length() == 1) {
} else if (source.toString().trim().length() == 1) {
//Detect split characters, remove them and complete the current token instead
if (isSplitChar(source.charAt(0))) {
if (isSplitChar(source.toString().trim().charAt(0))) {
performCompletion();
return "";
}
Expand Down Expand Up @@ -952,6 +954,11 @@ protected void replaceText(CharSequence text) {

String original = TextUtils.substring(editable, start, end);

//Keep track of replacements for a bug workaround
if (original.length() > 0) {
lastCompletionText = original;
}

if (editable != null) {
if (tokenSpan == null) {
editable.replace(start, end, "");
Expand Down Expand Up @@ -1692,6 +1699,14 @@ public boolean setComposingText(CharSequence text, int newCursorPosition) {
}
}

//Also, some keyboards don't correctly respect the replacement if the replacement
//is the same number of characters as the replacement span (",, "), so 3 letters
//We need to ignore this value if it's available
if (lastCompletionText != null && text.length() == lastCompletionText.length() + 1 &&
text.toString().startsWith(lastCompletionText)) {
text = text.subSequence(text.length() - 1, text.length());

This comment has been minimized.

Copy link
@micaelomota

micaelomota Jun 20, 2019

Contributor

This is resulting in a bug. You can reproduce it doing:
1- Set threshold 1
2- Run example app, write and select marshall@example.com as the first token.
3- Then write "max"

When you type the "x" the "ma" is removed from the text.

}

return super.setComposingText(text, newCursorPosition);
}
}
Expand Down

0 comments on commit 554cd7b

Please sign in to comment.