MentionsEditText extends EditText implements TokenSource { @Override public boolean onTouchEvent(@NonNull MotionEvent event) { final MentionSpan touchedSpan = getTouchedSpan(event); boolean superResult; if (android.os.Build.VERSION.SDK_INT == Build.VERSION_CODES.M && event.getActionMasked() == MotionEvent.ACTION_UP) { try { superResult = super.onTouchEvent(event); } catch (NullPointerException ignored) { clearFocus(); superResult = true; } } else { superResult = super.onTouchEvent(event); } if (event.getAction() == MotionEvent.ACTION_UP) { if (!isLongPressed && touchedSpan != null) { touchedSpan.onClick(this); Context context = getContext(); if (context != null) { InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE); imm.showSoftInput(this, 0); } return true; } } else if (event.getAction() == MotionEvent.ACTION_DOWN) { isLongPressed = false; if (isLongClickable() && touchedSpan != null) { if (longClickRunnable == null) { longClickRunnable = new CheckLongClickRunnable(); } longClickRunnable.touchedSpan = touchedSpan; removeCallbacks(longClickRunnable); postDelayed(longClickRunnable, ViewConfiguration.getLongPressTimeout()); } } else if (event.getAction() == MotionEvent.ACTION_CANCEL) { isLongPressed = false; } if (mAvoidPrefixOnTap && mSuggestionsVisibilityManager != null && mSuggestionsVisibilityManager.isDisplayingSuggestions()) { mSuggestionsVisibilityManager.displaySuggestions(false); String keywords = getCurrentKeywordsString(); String[] words = keywords.split(" "); if (words.length > 0) { String prefix = words[words.length - 1]; if (prefix.length() > 0) { setAvoidedPrefix(prefix); } } } return superResult; } MentionsEditText(@NonNull Context context);  MentionsEditText(@NonNull Context context, @Nullable AttributeSet attrs);  MentionsEditText(@NonNull Context context, @Nullable AttributeSet attrs, int defStyle); @Override @NonNull String getCurrentTokenString(); @Override @Nullable QueryToken getQueryTokenIfValid(); @Override boolean onTouchEvent(@NonNull MotionEvent event); @Override boolean onTextContextMenuItem(@MenuRes int id); @NonNull String getCurrentKeywordsString(); void updateSpan(@NonNull MentionSpan span); void deselectAllSpans(); void insertMention(@NonNull Mentionable mention); void insertMentionWithoutToken(@NonNull Mentionable mention); boolean isCurrentlyExplicit(); @Override void onPopulateAccessibilityEvent(@NonNull AccessibilityEvent event); @Override void addTextChangedListener(@NonNull TextWatcher watcher); @Override void removeTextChangedListener(@NonNull TextWatcher watcher); @SuppressWarnings("unused") void addMentionWatcher(@NonNull MentionWatcher watcher); @SuppressWarnings("unused") void removeMentionWatcher(@NonNull MentionWatcher watcher); @NonNull MentionsEditable getMentionsText(); @Nullable Tokenizer getTokenizer(); void setTokenizer(@Nullable final Tokenizer tokenizer); void setQueryTokenReceiver(@Nullable final QueryTokenReceiver queryTokenReceiver); void setSuggestionsVisibilityManager(@Nullable final SuggestionsVisibilityManager suggestionsVisibilityManager); void setMentionSpanFactory(@NonNull final MentionSpanFactory factory); void setMentionSpanConfig(@NonNull final MentionSpanConfig config); void setAvoidedPrefix(@Nullable String prefix); void setAvoidPrefixOnTap(boolean avoidPrefixOnTap); @NonNull @Override Parcelable onSaveInstanceState(); @Override void onRestoreInstanceState(Parcelable state);  }
@Test public void testOnTouchEvent() { MotionEvent event = mock(MotionEvent.class); doReturn(null).when(mEditText).getTouchedSpan(event); doReturn(true).when(mRichEditor).isDisplayingSuggestions(); doReturn("").when(mEditText).getCurrentKeywordsString(); mEditText.onTouchEvent(event); verify(mEditText, never()).setAvoidedPrefix(""); doReturn("a").when(mEditText).getCurrentKeywordsString(); mEditText.onTouchEvent(event); verify(mEditText).setAvoidedPrefix("a"); }