Merge "Rewrite SuggestionsPopupWindowTest using espresso." into nyc-dev

This commit is contained in:
Keisuke Kuroyanagi
2016-04-07 04:33:10 +00:00
committed by Android (Google) Code Review
5 changed files with 445 additions and 144 deletions

View File

@@ -16,13 +16,35 @@
package android.widget; package android.widget;
import android.app.Activity; import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.Espresso.pressBack;
import static android.support.test.espresso.action.ViewActions.clearText;
import static android.support.test.espresso.action.ViewActions.click;
import static android.support.test.espresso.action.ViewActions.replaceText;
import static android.support.test.espresso.assertion.ViewAssertions.matches;
import static android.support.test.espresso.matcher.RootMatchers.withDecorView;
import static android.support.test.espresso.matcher.ViewMatchers.withId;
import static android.support.test.espresso.matcher.ViewMatchers.withText;
import static android.widget.espresso.DragHandleUtils.onHandleView;
import static android.widget.espresso.FloatingToolbarEspressoUtils.assertFloatingToolbarContainsItem;
import static android.widget.espresso.FloatingToolbarEspressoUtils.clickFloatingToolbarItem;
import static android.widget.espresso.FloatingToolbarEspressoUtils.sleepForFloatingToolbarPopup;
import static android.widget.espresso.SuggestionsPopupwindowUtils.assertSuggestionsPopupContainsItem;
import static android.widget.espresso.SuggestionsPopupwindowUtils.assertSuggestionsPopupIsDisplayed;
import static android.widget.espresso.SuggestionsPopupwindowUtils.assertSuggestionsPopupIsNotDisplayed;
import static android.widget.espresso.SuggestionsPopupwindowUtils.clickSuggestionsPopupItem;
import static android.widget.espresso.SuggestionsPopupwindowUtils.onSuggestionsPopup;
import static android.widget.espresso.TextViewActions.clickOnTextAtIndex;
import static android.widget.espresso.TextViewActions.longPressOnTextAtIndex;
import static org.hamcrest.Matchers.is;
import android.content.res.TypedArray; import android.content.res.TypedArray;
import android.support.test.espresso.NoMatchingViewException;
import android.support.test.espresso.ViewAssertion;
import android.test.ActivityInstrumentationTestCase2; import android.test.ActivityInstrumentationTestCase2;
import android.test.suitebuilder.annotation.SmallTest; import android.test.suitebuilder.annotation.SmallTest;
import android.test.suitebuilder.annotation.Suppress; import android.test.suitebuilder.annotation.Suppress;
import android.text.Selection; import android.text.Selection;
import android.text.SpannableStringBuilder; import android.text.Spannable;
import android.text.Spanned; import android.text.Spanned;
import android.text.TextPaint; import android.text.TextPaint;
import android.text.style.SuggestionSpan; import android.text.style.SuggestionSpan;
@@ -42,55 +64,215 @@ public class SuggestionsPopupWindowTest extends ActivityInstrumentationTestCase2
super(TextViewActivity.class); super(TextViewActivity.class);
} }
@Override
protected void setUp() throws Exception {
super.setUp();
getActivity();
}
private void setSuggestionSpan(SuggestionSpan span, int start, int end) {
final TextView textView = (TextView) getActivity().findViewById(R.id.textview);
textView.post(
() -> {
final Spannable text = (Spannable) textView.getText();
text.setSpan(span, start, end, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
Selection.setSelection(text, (start + end) / 2);
});
getInstrumentation().waitForIdleSync();
}
@SmallTest
public void testOnTextContextMenuItem() {
final String text = "abc def ghi";
onView(withId(R.id.textview)).perform(click());
onView(withId(R.id.textview)).perform(replaceText(text));
final SuggestionSpan suggestionSpan = new SuggestionSpan(getActivity(),
new String[]{"DEF", "Def"}, SuggestionSpan.FLAG_AUTO_CORRECTION);
setSuggestionSpan(suggestionSpan, text.indexOf('d'), text.indexOf('f') + 1);
final TextView textView = (TextView) getActivity().findViewById(R.id.textview);
textView.post(() -> textView.onTextContextMenuItem(TextView.ID_REPLACE));
getInstrumentation().waitForIdleSync();
assertSuggestionsPopupIsDisplayed();
}
@SmallTest
public void testSelectionActionMode() {
final String text = "abc def ghi";
onView(withId(R.id.textview)).perform(click());
onView(withId(R.id.textview)).perform(replaceText(text));
final SuggestionSpan suggestionSpan = new SuggestionSpan(getActivity(),
new String[]{"DEF", "Def"}, SuggestionSpan.FLAG_AUTO_CORRECTION);
setSuggestionSpan(suggestionSpan, text.indexOf('d'), text.indexOf('f') + 1);
onView(withId(R.id.textview)).perform(longPressOnTextAtIndex(text.indexOf('e')));
sleepForFloatingToolbarPopup();
assertFloatingToolbarContainsItem(
getActivity().getString(com.android.internal.R.string.replace));
sleepForFloatingToolbarPopup();
clickFloatingToolbarItem(
getActivity().getString(com.android.internal.R.string.replace));
assertSuggestionsPopupIsDisplayed();
}
@SmallTest
public void testInsertionActionMode() {
final String text = "abc def ghi";
onView(withId(R.id.textview)).perform(click());
onView(withId(R.id.textview)).perform(replaceText(text));
final SuggestionSpan suggestionSpan = new SuggestionSpan(getActivity(),
new String[]{"DEF", "Def"}, SuggestionSpan.FLAG_AUTO_CORRECTION);
setSuggestionSpan(suggestionSpan, text.indexOf('d'), text.indexOf('f') + 1);
onView(withId(R.id.textview)).perform(clickOnTextAtIndex(text.indexOf('e')));
onHandleView(com.android.internal.R.id.insertion_handle).perform(click());
sleepForFloatingToolbarPopup();
assertFloatingToolbarContainsItem(
getActivity().getString(com.android.internal.R.string.replace));
clickFloatingToolbarItem(
getActivity().getString(com.android.internal.R.string.replace));
assertSuggestionsPopupIsDisplayed();
}
private void showSuggestionsPopup() {
final TextView textView = (TextView) getActivity().findViewById(R.id.textview);
textView.post(() -> textView.onTextContextMenuItem(TextView.ID_REPLACE));
getInstrumentation().waitForIdleSync();
assertSuggestionsPopupIsDisplayed();
}
@SmallTest
public void testSuggestionItems() {
final String text = "abc def ghi";
onView(withId(R.id.textview)).perform(click());
onView(withId(R.id.textview)).perform(replaceText(text));
final SuggestionSpan suggestionSpan = new SuggestionSpan(getActivity(),
new String[]{"DEF", "Def"}, SuggestionSpan.FLAG_AUTO_CORRECTION);
setSuggestionSpan(suggestionSpan, text.indexOf('d'), text.indexOf('f') + 1);
showSuggestionsPopup();
assertSuggestionsPopupIsDisplayed();
assertSuggestionsPopupContainsItem("DEF");
assertSuggestionsPopupContainsItem("Def");
assertSuggestionsPopupContainsItem(
getActivity().getString(com.android.internal.R.string.delete));
// Select an item.
clickSuggestionsPopupItem("DEF");
assertSuggestionsPopupIsNotDisplayed();
onView(withId(R.id.textview)).check(matches(withText("abc DEF ghi")));
showSuggestionsPopup();
assertSuggestionsPopupIsDisplayed();
assertSuggestionsPopupContainsItem("def");
assertSuggestionsPopupContainsItem("Def");
assertSuggestionsPopupContainsItem(
getActivity().getString(com.android.internal.R.string.delete));
// Delete
clickSuggestionsPopupItem(
getActivity().getString(com.android.internal.R.string.delete));
assertSuggestionsPopupIsNotDisplayed();
onView(withId(R.id.textview)).check(matches(withText("abc ghi")));
}
@SmallTest
public void testMisspelled() {
final String text = "abc def ghi";
onView(withId(R.id.textview)).perform(click());
onView(withId(R.id.textview)).perform(replaceText(text));
final SuggestionSpan suggestionSpan = new SuggestionSpan(getActivity(),
new String[]{"DEF", "Def"}, SuggestionSpan.FLAG_MISSPELLED);
setSuggestionSpan(suggestionSpan, text.indexOf('d'), text.indexOf('f') + 1);
showSuggestionsPopup();
assertSuggestionsPopupIsDisplayed();
assertSuggestionsPopupContainsItem("DEF");
assertSuggestionsPopupContainsItem("Def");
assertSuggestionsPopupContainsItem(
getActivity().getString(com.android.internal.R.string.addToDictionary));
assertSuggestionsPopupContainsItem(
getActivity().getString(com.android.internal.R.string.delete));
// Click "Add to dictionary".
clickSuggestionsPopupItem(
getActivity().getString(com.android.internal.R.string.addToDictionary));
// TODO: Check if add to dictionary dialog is displayed.
}
@SmallTest
public void testEasyCorrect() {
final String text = "abc def ghi";
onView(withId(R.id.textview)).perform(click());
onView(withId(R.id.textview)).perform(replaceText(text));
final SuggestionSpan suggestionSpan = new SuggestionSpan(getActivity(),
new String[]{"DEF", "Def"},
SuggestionSpan.FLAG_EASY_CORRECT | SuggestionSpan.FLAG_MISSPELLED);
setSuggestionSpan(suggestionSpan, text.indexOf('d'), text.indexOf('f') + 1);
onView(withId(R.id.textview)).perform(clickOnTextAtIndex(text.indexOf('e')));
assertSuggestionsPopupIsDisplayed();
assertSuggestionsPopupContainsItem("DEF");
assertSuggestionsPopupContainsItem("Def");
assertSuggestionsPopupContainsItem(
getActivity().getString(com.android.internal.R.string.delete));
// Select an item.
clickSuggestionsPopupItem("DEF");
assertSuggestionsPopupIsNotDisplayed();
onView(withId(R.id.textview)).check(matches(withText("abc DEF ghi")));
onView(withId(R.id.textview)).perform(clickOnTextAtIndex(text.indexOf('e')));
assertSuggestionsPopupIsNotDisplayed();
showSuggestionsPopup();
assertSuggestionsPopupIsDisplayed();
assertSuggestionsPopupContainsItem("def");
assertSuggestionsPopupContainsItem("Def");
assertSuggestionsPopupContainsItem(
getActivity().getString(com.android.internal.R.string.delete));
}
@SmallTest @SmallTest
@Suppress
public void testTextAppearanceInSuggestionsPopup() { public void testTextAppearanceInSuggestionsPopup() {
final Activity activity = getActivity(); final String text = "abc def ghi";
final String sampleText = "abc def ghi";
final String[] singleWordCandidates = {"DEF", "Def"}; final String[] singleWordCandidates = {"DEF", "Def"};
final SuggestionSpan singleWordSuggestionSpan = new SuggestionSpan(activity, final SuggestionSpan suggestionSpan = new SuggestionSpan(getActivity(),
singleWordCandidates, SuggestionSpan.FLAG_AUTO_CORRECTION); singleWordCandidates, SuggestionSpan.FLAG_MISSPELLED);
final int singleWordSpanStart = 4;
final int singleWordSpanEnd = 7;
final String[] multiWordCandidates = {"ABC DEF GHI", "Abc Def Ghi"}; final String[] multiWordCandidates = {"ABC DEF GHI", "Abc Def Ghi"};
final SuggestionSpan multiWordSuggestionSpan = new SuggestionSpan(activity, final SuggestionSpan multiWordSuggestionSpan = new SuggestionSpan(getActivity(),
multiWordCandidates, SuggestionSpan.FLAG_AUTO_CORRECTION); multiWordCandidates, SuggestionSpan.FLAG_MISSPELLED);
final int multiWordSpanStart = 0;
final int multiWordSpanEnd = 11;
TypedArray array = activity.obtainStyledAttributes(com.android.internal.R.styleable.Theme); final TypedArray array =
int id = array.getResourceId( getActivity().obtainStyledAttributes(com.android.internal.R.styleable.Theme);
final int id = array.getResourceId(
com.android.internal.R.styleable.Theme_textEditSuggestionHighlightStyle, 0); com.android.internal.R.styleable.Theme_textEditSuggestionHighlightStyle, 0);
array.recycle(); array.recycle();
final TextAppearanceSpan expectedSpan = new TextAppearanceSpan(getActivity(), id);
TextAppearanceSpan expectedSpan = new TextAppearanceSpan(activity, id); final TextPaint tmpTp = new TextPaint();
TextPaint tmpTp = new TextPaint();
expectedSpan.updateDrawState(tmpTp); expectedSpan.updateDrawState(tmpTp);
final int expectedHighlightTextColor = tmpTp.getColor(); final int expectedHighlightTextColor = tmpTp.getColor();
final float expectedHighlightTextSize = tmpTp.getTextSize(); final float expectedHighlightTextSize = tmpTp.getTextSize();
final TextView textView = (TextView) getActivity().findViewById(R.id.textview);
final EditText editText = (EditText) activity.findViewById(R.id.textview);
final Editor editor = editText.getEditorForTesting();
assertNotNull(editor);
// Request to show SuggestionsPopupWindow.
Runnable showSuggestionWindowRunner = new Runnable() {
@Override
public void run() {
SpannableStringBuilder ssb = new SpannableStringBuilder();
ssb.append(sampleText);
ssb.setSpan(singleWordSuggestionSpan, singleWordSpanStart, singleWordSpanEnd,
Spanned.SPAN_INCLUSIVE_INCLUSIVE);
ssb.setSpan(multiWordSuggestionSpan, multiWordSpanStart, multiWordSpanEnd,
Spanned.SPAN_INCLUSIVE_INCLUSIVE);
editText.setText(ssb);
Selection.setSelection(editText.getText(), singleWordSpanStart, singleWordSpanEnd);
editText.onTextContextMenuItem(TextView.ID_REPLACE);
}
};
// In this test, the SuggestionsPopupWindow looks like // In this test, the SuggestionsPopupWindow looks like
// abc def ghi // abc def ghi
@@ -103,96 +285,74 @@ public class SuggestionsPopupWindowTest extends ActivityInstrumentationTestCase2
// | DELETE | // | DELETE |
// ----------------- // -----------------
// *XX* means that XX is highlighted. // *XX* means that XX is highlighted.
Runnable popupVaridator = new Runnable() { for (int i = 0; i < 2; i++) {
@Override onView(withId(R.id.textview)).perform(click());
public void run() { onView(withId(R.id.textview)).perform(replaceText(text));
Editor.SuggestionsPopupWindow popupWindow = setSuggestionSpan(suggestionSpan, text.indexOf('d'), text.indexOf('f') + 1);
editor.getSuggestionsPopupWindowForTesting(); setSuggestionSpan(multiWordSuggestionSpan, 0, text.length());
assertNotNull(popupWindow);
LinearLayout linearLayout = (LinearLayout) popupWindow.getContentViewForTesting(); showSuggestionsPopup();
assertNotNull(linearLayout); assertSuggestionsPopupIsDisplayed();
assertSuggestionsPopupContainsItem("abc DEF ghi");
assertSuggestionsPopupContainsItem("abc Def ghi");
assertSuggestionsPopupContainsItem("ABC DEF GHI");
assertSuggestionsPopupContainsItem("Abc Def Ghi");
assertSuggestionsPopupContainsItem(
getActivity().getString(com.android.internal.R.string.delete));
ListView listView = (ListView)linearLayout.findViewById( onSuggestionsPopup().check(new ViewAssertion() {
com.android.internal.R.id.suggestionContainer); @Override
assertNotNull(listView); public void check(View view, NoMatchingViewException e) {
final ListView listView = (ListView) view.findViewById(
com.android.internal.R.id.suggestionContainer);
assertNotNull(listView);
final int childNum = listView.getChildCount();
assertEquals(singleWordCandidates.length + multiWordCandidates.length,
childNum);
int childNum = listView.getChildCount(); for (int j = 0; j < childNum; j++) {
assertEquals(singleWordCandidates.length + multiWordCandidates.length, childNum); final TextView suggestion = (TextView) listView.getChildAt(j);
assertNotNull(suggestion);
final Spanned spanned = (Spanned) suggestion.getText();
assertNotNull(spanned);
for (int i = 0; i < singleWordCandidates.length; ++i) { // Check that the suggestion item order is kept.
TextView textView = (TextView) listView.getChildAt(i); final String expectedText;
assertNotNull(textView); if (j < singleWordCandidates.length) {
expectedText = "abc " + singleWordCandidates[j] + " ghi";
} else {
expectedText = multiWordCandidates[j - singleWordCandidates.length];
}
assertEquals(expectedText, spanned.toString());
Spanned spanned = (Spanned) textView.getText(); // Check that the text is highlighted with correct color and text size.
assertNotNull(spanned); final TextAppearanceSpan[] taSpan = spanned.getSpans(
text.indexOf('d'), text.indexOf('f') + 1, TextAppearanceSpan.class);
assertEquals(1, taSpan.length);
TextPaint tp = new TextPaint();
taSpan[0].updateDrawState(tp);
assertEquals(expectedHighlightTextColor, tp.getColor());
assertEquals(expectedHighlightTextSize, tp.getTextSize());
// Check that the suggestion item order is kept. // Check the correct part of the text is highlighted.
String expectedText = "abc " + singleWordCandidates[i] + " ghi"; final int expectedStart;
assertEquals(expectedText, spanned.toString()); final int expectedEnd;
if (j < singleWordCandidates.length) {
// Check that the text is highlighted with correct color and text size. expectedStart = text.indexOf('d');
TextAppearanceSpan[] taSpan = spanned.getSpans(singleWordSpanStart, expectedEnd = text.indexOf('f') + 1;
singleWordSpanEnd, TextAppearanceSpan.class); } else {
assertEquals(1, taSpan.length); expectedStart = 0;
TextPaint tp = new TextPaint(); expectedEnd = text.length();
taSpan[0].updateDrawState(tp); }
assertEquals(expectedHighlightTextColor, tp.getColor()); assertEquals(expectedStart, spanned.getSpanStart(taSpan[0]));
assertEquals(expectedHighlightTextSize, tp.getTextSize()); assertEquals(expectedEnd, spanned.getSpanEnd(taSpan[0]));
}
// Check only center word is highlighted.
assertEquals(singleWordSpanStart, spanned.getSpanStart(taSpan[0]));
assertEquals(singleWordSpanEnd, spanned.getSpanEnd(taSpan[0]));
} }
});
for (int i = 0; i < multiWordCandidates.length; ++i) { pressBack();
int indexInListView = singleWordCandidates.length + i; onView(withId(R.id.textview))
TextView textView = (TextView) listView.getChildAt(indexInListView); .inRoot(withDecorView(is(getActivity().getWindow().getDecorView())))
assertNotNull(textView); .perform(clearText());
}
Spanned spanned = (Spanned) textView.getText();
assertNotNull(spanned);
// Check that the suggestion item order is kept.
assertEquals(multiWordCandidates[i], spanned.toString());
// Check that the text is highlighted with correct color and text size.
TextAppearanceSpan[] taSpan = spanned.getSpans(
0, multiWordCandidates[i].length(), TextAppearanceSpan.class);
assertEquals(1, taSpan.length);
TextPaint tp = new TextPaint();
taSpan[0].updateDrawState(tp);
assertEquals(expectedHighlightTextColor, tp.getColor());
assertEquals(expectedHighlightTextSize, tp.getTextSize());
// Check the whole text is highlighted.
assertEquals(multiWordSpanStart, spanned.getSpanStart(taSpan[0]));
assertEquals(multiWordSpanEnd, spanned.getSpanEnd(taSpan[0]));
}
TextView deleteButton = (TextView)linearLayout.findViewById(
com.android.internal.R.id.deleteButton);
assertEquals(View.VISIBLE, deleteButton.getWindowVisibility());
}
};
// Show the SuggestionWindow and verify the contents.
activity.runOnUiThread(showSuggestionWindowRunner);
getInstrumentation().waitForIdleSync();
activity.runOnUiThread(popupVaridator);
// Request to hide the SuggestionPopupWindow and wait until it is hidden.
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
editText.setText("");
}
});
getInstrumentation().waitForIdleSync();
// Show and verify the contents again.
activity.runOnUiThread(showSuggestionWindowRunner);
getInstrumentation().waitForIdleSync();
activity.runOnUiThread(popupVaridator);
} }
} }

View File

@@ -16,6 +16,10 @@
package android.widget; package android.widget;
import static android.widget.espresso.ContextMenuUtils.assertContextMenuContainsItemDisabled;
import static android.widget.espresso.ContextMenuUtils.assertContextMenuContainsItemEnabled;
import static android.widget.espresso.ContextMenuUtils.assertContextMenuIsNotDisplayed;
import static android.widget.espresso.DragHandleUtils.assertNoSelectionHandles; import static android.widget.espresso.DragHandleUtils.assertNoSelectionHandles;
import static android.widget.espresso.DragHandleUtils.onHandleView; import static android.widget.espresso.DragHandleUtils.onHandleView;
import static android.widget.espresso.TextViewActions.mouseClickOnTextAtIndex; import static android.widget.espresso.TextViewActions.mouseClickOnTextAtIndex;
@@ -41,11 +45,9 @@ import static android.support.test.espresso.matcher.ViewMatchers.withText;
import com.android.frameworks.coretests.R; import com.android.frameworks.coretests.R;
import android.support.test.espresso.Espresso;
import android.test.ActivityInstrumentationTestCase2; import android.test.ActivityInstrumentationTestCase2;
import android.test.suitebuilder.annotation.SmallTest; import android.test.suitebuilder.annotation.SmallTest;
import android.view.MotionEvent; import android.view.MotionEvent;
import android.widget.espresso.ContextMenuUtils;
/** /**
* Tests mouse interaction of the TextView widget from an Activity * Tests mouse interaction of the TextView widget from an Activity
@@ -57,7 +59,8 @@ public class TextViewActivityMouseTest extends ActivityInstrumentationTestCase2<
} }
@Override @Override
public void setUp() { public void setUp() throws Exception {
super.setUp();
getActivity(); getActivity();
} }
@@ -102,28 +105,28 @@ public class TextViewActivityMouseTest extends ActivityInstrumentationTestCase2<
onView(withId(R.id.textview)).perform(click()); onView(withId(R.id.textview)).perform(click());
onView(withId(R.id.textview)).perform(typeTextIntoFocusedView(text)); onView(withId(R.id.textview)).perform(typeTextIntoFocusedView(text));
ContextMenuUtils.assertContextMenuIsNotDisplayed(); assertContextMenuIsNotDisplayed();
onView(withId(R.id.textview)).perform( onView(withId(R.id.textview)).perform(
mouseClickOnTextAtIndex(text.indexOf("d"), MotionEvent.BUTTON_SECONDARY)); mouseClickOnTextAtIndex(text.indexOf("d"), MotionEvent.BUTTON_SECONDARY));
ContextMenuUtils.assertContextMenuContainsItemDisabled( assertContextMenuContainsItemDisabled(
getActivity().getString(com.android.internal.R.string.copy)); getActivity().getString(com.android.internal.R.string.copy));
ContextMenuUtils.assertContextMenuContainsItemEnabled( assertContextMenuContainsItemEnabled(
getActivity().getString(com.android.internal.R.string.undo)); getActivity().getString(com.android.internal.R.string.undo));
// Hide context menu. // Hide context menu.
pressBack(); pressBack();
ContextMenuUtils.assertContextMenuIsNotDisplayed(); assertContextMenuIsNotDisplayed();
onView(withId(R.id.textview)).perform( onView(withId(R.id.textview)).perform(
mouseDragOnText(text.indexOf("c"), text.indexOf("h"))); mouseDragOnText(text.indexOf("c"), text.indexOf("h")));
onView(withId(R.id.textview)).perform( onView(withId(R.id.textview)).perform(
mouseClickOnTextAtIndex(text.indexOf("d"), MotionEvent.BUTTON_SECONDARY)); mouseClickOnTextAtIndex(text.indexOf("d"), MotionEvent.BUTTON_SECONDARY));
ContextMenuUtils.assertContextMenuContainsItemEnabled( assertContextMenuContainsItemEnabled(
getActivity().getString(com.android.internal.R.string.copy)); getActivity().getString(com.android.internal.R.string.copy));
ContextMenuUtils.assertContextMenuContainsItemEnabled( assertContextMenuContainsItemEnabled(
getActivity().getString(com.android.internal.R.string.undo)); getActivity().getString(com.android.internal.R.string.undo));
// Hide context menu. // Hide context menu.
@@ -133,9 +136,9 @@ public class TextViewActivityMouseTest extends ActivityInstrumentationTestCase2<
onView(withId(R.id.textview)).perform( onView(withId(R.id.textview)).perform(
mouseClickOnTextAtIndex(text.indexOf("i"), MotionEvent.BUTTON_SECONDARY)); mouseClickOnTextAtIndex(text.indexOf("i"), MotionEvent.BUTTON_SECONDARY));
ContextMenuUtils.assertContextMenuContainsItemDisabled( assertContextMenuContainsItemDisabled(
getActivity().getString(com.android.internal.R.string.copy)); getActivity().getString(com.android.internal.R.string.copy));
ContextMenuUtils.assertContextMenuContainsItemEnabled( assertContextMenuContainsItemEnabled(
getActivity().getString(com.android.internal.R.string.undo)); getActivity().getString(com.android.internal.R.string.undo));
// Hide context menu. // Hide context menu.

View File

@@ -19,7 +19,6 @@ package android.widget;
import static android.support.test.espresso.action.ViewActions.longClick; import static android.support.test.espresso.action.ViewActions.longClick;
import static android.widget.espresso.DragHandleUtils.assertNoSelectionHandles; import static android.widget.espresso.DragHandleUtils.assertNoSelectionHandles;
import static android.widget.espresso.DragHandleUtils.onHandleView; import static android.widget.espresso.DragHandleUtils.onHandleView;
import static android.widget.espresso.FloatingToolbarEspressoUtils.onFloatingToolBarItem;
import static android.widget.espresso.TextViewActions.clickOnTextAtIndex; import static android.widget.espresso.TextViewActions.clickOnTextAtIndex;
import static android.widget.espresso.TextViewActions.doubleTapAndDragOnText; import static android.widget.espresso.TextViewActions.doubleTapAndDragOnText;
import static android.widget.espresso.TextViewActions.doubleClickOnTextAtIndex; import static android.widget.espresso.TextViewActions.doubleClickOnTextAtIndex;
@@ -31,9 +30,10 @@ import static android.widget.espresso.TextViewAssertions.hasInsertionPointerAtIn
import static android.widget.espresso.TextViewAssertions.hasSelection; import static android.widget.espresso.TextViewAssertions.hasSelection;
import static android.widget.espresso.FloatingToolbarEspressoUtils.assertFloatingToolbarIsDisplayed; import static android.widget.espresso.FloatingToolbarEspressoUtils.assertFloatingToolbarIsDisplayed;
import static android.widget.espresso.FloatingToolbarEspressoUtils.assertFloatingToolbarIsNotDisplayed; import static android.widget.espresso.FloatingToolbarEspressoUtils.assertFloatingToolbarIsNotDisplayed;
import static android.widget.espresso.FloatingToolbarEspressoUtils.sleepForFloatingToolbarPopup;
import static android.widget.espresso.FloatingToolbarEspressoUtils.assertFloatingToolbarContainsItem; import static android.widget.espresso.FloatingToolbarEspressoUtils.assertFloatingToolbarContainsItem;
import static android.widget.espresso.FloatingToolbarEspressoUtils.assertFloatingToolbarDoesNotContainItem; import static android.widget.espresso.FloatingToolbarEspressoUtils.assertFloatingToolbarDoesNotContainItem;
import static android.widget.espresso.FloatingToolbarEspressoUtils.clickFloatingToolbarItem;
import static android.widget.espresso.FloatingToolbarEspressoUtils.sleepForFloatingToolbarPopup;
import static android.support.test.espresso.Espresso.onView; import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.action.ViewActions.click; import static android.support.test.espresso.action.ViewActions.click;
import static android.support.test.espresso.action.ViewActions.pressKey; import static android.support.test.espresso.action.ViewActions.pressKey;
@@ -67,7 +67,8 @@ public class TextViewActivityTest extends ActivityInstrumentationTestCase2<TextV
} }
@Override @Override
public void setUp() { public void setUp() throws Exception {
super.setUp();
getActivity(); getActivity();
} }
@@ -256,7 +257,8 @@ public class TextViewActivityTest extends ActivityInstrumentationTestCase2<TextV
onView(withId(R.id.textview)).perform(typeTextIntoFocusedView("test")); onView(withId(R.id.textview)).perform(typeTextIntoFocusedView("test"));
onView(withId(R.id.textview)).perform(longPressOnTextAtIndex(1)); onView(withId(R.id.textview)).perform(longPressOnTextAtIndex(1));
onFloatingToolBarItem(withText(com.android.internal.R.string.cut)).perform(click()); clickFloatingToolbarItem(
getActivity().getString(com.android.internal.R.string.cut));
onView(withId(R.id.textview)).perform(longClick()); onView(withId(R.id.textview)).perform(longClick());
sleepForFloatingToolbarPopup(); sleepForFloatingToolbarPopup();

View File

@@ -17,6 +17,7 @@
package android.widget.espresso; package android.widget.espresso;
import static android.support.test.espresso.Espresso.onView; import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.action.ViewActions.click;
import static android.support.test.espresso.assertion.ViewAssertions.matches; import static android.support.test.espresso.assertion.ViewAssertions.matches;
import static android.support.test.espresso.matcher.RootMatchers.withDecorView; import static android.support.test.espresso.matcher.RootMatchers.withDecorView;
import static android.support.test.espresso.matcher.ViewMatchers.hasDescendant; import static android.support.test.espresso.matcher.ViewMatchers.hasDescendant;
@@ -24,6 +25,7 @@ import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed;
import static android.support.test.espresso.matcher.ViewMatchers.isRoot; import static android.support.test.espresso.matcher.ViewMatchers.isRoot;
import static android.support.test.espresso.matcher.ViewMatchers.withTagValue; import static android.support.test.espresso.matcher.ViewMatchers.withTagValue;
import static android.support.test.espresso.matcher.ViewMatchers.withId; import static android.support.test.espresso.matcher.ViewMatchers.withId;
import static android.support.test.espresso.matcher.ViewMatchers.withText;
import static org.hamcrest.Matchers.allOf; import static org.hamcrest.Matchers.allOf;
import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.is;
@@ -34,8 +36,6 @@ import android.support.test.espresso.NoMatchingViewException;
import android.support.test.espresso.UiController; import android.support.test.espresso.UiController;
import android.support.test.espresso.ViewAction; import android.support.test.espresso.ViewAction;
import android.support.test.espresso.ViewInteraction; import android.support.test.espresso.ViewInteraction;
import android.support.test.espresso.action.ViewActions;
import android.support.test.espresso.matcher.ViewMatchers;
import android.view.View; import android.view.View;
import com.android.internal.widget.FloatingToolbar; import com.android.internal.widget.FloatingToolbar;
@@ -90,7 +90,7 @@ public class FloatingToolbarEspressoUtils {
final int id = com.android.internal.R.id.overflow; final int id = com.android.internal.R.id.overflow;
onView(allOf(withId(id), isDisplayed())) onView(allOf(withId(id), isDisplayed()))
.inRoot(withDecorView(hasDescendant(withId(id)))) .inRoot(withDecorView(hasDescendant(withId(id))))
.perform(ViewActions.click()); .perform(click());
onView(isRoot()).perform(SLEEP); onView(isRoot()).perform(SLEEP);
} }
@@ -106,7 +106,7 @@ public class FloatingToolbarEspressoUtils {
*/ */
public static void assertFloatingToolbarContainsItem(String itemLabel) { public static void assertFloatingToolbarContainsItem(String itemLabel) {
try{ try{
onFloatingToolBar().check(matches(hasDescendant(ViewMatchers.withText(itemLabel)))); onFloatingToolBar().check(matches(hasDescendant(withText(itemLabel))));
} catch (AssertionError e) { } catch (AssertionError e) {
try{ try{
toggleOverflow(); toggleOverflow();
@@ -115,7 +115,7 @@ public class FloatingToolbarEspressoUtils {
throw e; throw e;
} }
try{ try{
onFloatingToolBar().check(matches(hasDescendant(ViewMatchers.withText(itemLabel)))); onFloatingToolBar().check(matches(hasDescendant(withText(itemLabel))));
} finally { } finally {
toggleOverflow(); toggleOverflow();
} }
@@ -137,6 +137,21 @@ public class FloatingToolbarEspressoUtils {
throw new AssertionError("Floating toolbar contains " + itemLabel); throw new AssertionError("Floating toolbar contains " + itemLabel);
} }
/**
* Click specified item on the floating tool bar.
*
* @param itemLabel label of the item.
*/
public static void clickFloatingToolbarItem(String itemLabel) {
try{
onFloatingToolBarItem(withText(itemLabel)).check(matches(isDisplayed()));
} catch (AssertionError e) {
// Try to find the item in the overflow menu.
toggleOverflow();
}
onFloatingToolBarItem(withText(itemLabel)).perform(click());
}
/** /**
* ViewAction to sleep to wait floating toolbar's animation. * ViewAction to sleep to wait floating toolbar's animation.
*/ */

View File

@@ -0,0 +1,121 @@
/*
* Copyright (C) 2016 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License
*/
package android.widget.espresso;
import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.assertion.ViewAssertions.matches;
import static android.support.test.espresso.matcher.RootMatchers.withDecorView;
import static android.support.test.espresso.matcher.ViewMatchers.hasDescendant;
import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed;
import static android.support.test.espresso.matcher.ViewMatchers.withId;
import static android.support.test.espresso.matcher.ViewMatchers.withText;
import org.hamcrest.Matcher;
import android.support.test.espresso.NoMatchingRootException;
import android.support.test.espresso.NoMatchingViewException;
import android.support.test.espresso.UiController;
import android.support.test.espresso.ViewAction;
import android.support.test.espresso.ViewInteraction;
import android.support.test.espresso.action.GeneralLocation;
import android.support.test.espresso.action.Press;
import android.support.test.espresso.action.Tap;
import android.view.View;
public final class SuggestionsPopupwindowUtils {
private static final int id = com.android.internal.R.id.suggestionWindowContainer;
private SuggestionsPopupwindowUtils() {};
public static ViewInteraction onSuggestionsPopup() {
return onView(withId(id)).inRoot(withDecorView(hasDescendant(withId(id))));
}
private static ViewInteraction onSuggestionsPopupItem(Matcher<View> matcher) {
return onView(matcher).inRoot(withDecorView(hasDescendant(withId(id))));
}
/**
* Asserts that the suggestions popup is displayed on screen.
*
* @throws AssertionError if the assertion fails
*/
public static void assertSuggestionsPopupIsDisplayed() {
onSuggestionsPopup().check(matches(isDisplayed()));
}
/**
* Asserts that the suggestions popup is not displayed on screen.
*
* @throws AssertionError if the assertion fails
*/
public static void assertSuggestionsPopupIsNotDisplayed() {
try {
onSuggestionsPopup().check(matches(isDisplayed()));
} catch (NoMatchingRootException | NoMatchingViewException | AssertionError e) {
return;
}
throw new AssertionError("Suggestions popup is displayed");
}
/**
* Asserts that the suggestions popup contains the specified item.
*
* @param itemLabel label of the item.
* @throws AssertionError if the assertion fails
*/
public static void assertSuggestionsPopupContainsItem(String itemLabel) {
onSuggestionsPopupItem(withText(itemLabel)).check(matches(isDisplayed()));
}
/**
* Click on the specified item in the suggestions popup.
*
* @param itemLabel label of the item.
*/
public static void clickSuggestionsPopupItem(String itemLabel) {
onSuggestionsPopupItem(withText(itemLabel)).perform(new SuggestionItemClickAction());
}
/**
* Click action to avoid checking ViewClickAction#getConstraints().
* TODO: Use Espresso.onData instead of this.
*/
private static final class SuggestionItemClickAction implements ViewAction {
private final ViewClickAction mViewClickAction;
public SuggestionItemClickAction() {
mViewClickAction =
new ViewClickAction(Tap.SINGLE, GeneralLocation.VISIBLE_CENTER, Press.FINGER);
}
@Override
public Matcher<View> getConstraints() {
return isDisplayed();
}
@Override
public String getDescription() {
return mViewClickAction.getDescription();
}
@Override
public void perform(UiController uiController, View view) {
mViewClickAction.perform(uiController, view);
}
}
}