下面列出了怎么用android.support.test.espresso.UiController的API类实例代码及写法,或者点击链接到github查看源代码。
public void perform(UiController uiController, View view) {
RecyclerView recyclerView = (RecyclerView) view;
(new ScrollToPositionViewAction(this.position)).perform(uiController, view);
uiController.loopMainThreadUntilIdle();
View targetView = recyclerView.getChildAt(this.position).findViewById(this.viewId);
if (targetView == null) {
throw (new PerformException.Builder()).withActionDescription(this.toString())
.withViewDescription(
HumanReadables.describe(view))
.withCause(new IllegalStateException(
"No view with id "
+ this.viewId
+ " found at position: "
+ this.position))
.build();
} else {
this.viewAction.perform(uiController, targetView);
}
}
private static ViewAction showControls() {
return new ViewAction() {
@Override
public Matcher<View> getConstraints() {
return isAssignableFrom(MovieView.class);
}
@Override
public String getDescription() {
return "Show controls of MovieView";
}
@Override
public void perform(UiController uiController, View view) {
uiController.loopMainThreadUntilIdle();
((MovieView) view).showControls();
uiController.loopMainThreadUntilIdle();
}
};
}
private static ViewAction showControls() {
return new ViewAction() {
@Override
public Matcher<View> getConstraints() {
return isAssignableFrom(MovieView.class);
}
@Override
public String getDescription() {
return "Show controls of MovieView";
}
@Override
public void perform(UiController uiController, View view) {
uiController.loopMainThreadUntilIdle();
((MovieView) view).showControls();
uiController.loopMainThreadUntilIdle();
}
};
}
/**
* Test if the manual input activity opens.
*/
@Test
public void testGoToManual() {
onView(withId(R.id.manual_input_button)).check(matches(allOf( isEnabled(), isClickable()))).perform(
new ViewAction() {
@Override
public Matcher<View> getConstraints() {
return isEnabled(); // no constraints, they are checked above
}
@Override
public String getDescription() {
return "click manual input button";
}
@Override
public void perform(UiController uiController, View view) {
view.performClick();
}
}
);
intended(hasComponent(ManualInputActivity.class.getName()));
}
public static ViewAction typeQuery(final String value) {
return new ViewAction() {
@Override
public Matcher<View> getConstraints() {
return allOf(isDisplayed(), isAssignableFrom(SearchView.class));
}
@Override
public void perform(UiController uiController, View view) {
((SearchView) view).setQuery(value, false);
}
@Override
public String getDescription() {
return String.format("type query(%s)", value);
}
};
}
public static String getTextFromMatcher(final Matcher<View> matcher) {
final String[] stringHolder = { null };
onView(matcher).perform(new ViewAction() {
@Override
public Matcher<View> getConstraints() {
return isAssignableFrom(TextView.class);
}
@Override
public String getDescription() {
return "getting text from a TextView";
}
@Override
public void perform(final UiController uiController, final View view) {
final TextView tv = (TextView) view;
stringHolder[0] = tv.getText().toString();
}
});
return stringHolder[0];
}
public static ViewAction waitFor(final long millis) {
return new ViewAction() {
@Override
public Matcher<View> getConstraints() {
return isRoot();
}
@Override
public String getDescription() {
return "Wait for " + millis + " milliseconds.";
}
@Override
public void perform(UiController uiController, final View view) {
uiController.loopMainThreadForAtLeast(millis);
}
};
}
public static ViewAction clickChildViewWithId(final int id) {
return new ViewAction() {
@Override
public Matcher<View> getConstraints() {
return null;
}
@Override
public String getDescription() {
return "Click on a child view with specified id.";
}
@Override
public void perform(UiController uiController, View view) {
View v = view.findViewById(id);
if (v != null) {
v.performClick();
}
}
};
}
public static ViewAction clickAnecdoteTextViewWithId(final int id) {
return new ViewAction() {
@Override
public Matcher<View> getConstraints() {
return null;
}
@Override
public String getDescription() {
return "Click on a child view with specified id.";
}
@Override
public void perform(UiController uiController, View view) {
View v = view.findViewById(id);
if (v != null) {
v.performClick();
}
}
};
}
public static ViewAction collapse() {
return new ViewAction() {
@Override
public Matcher<View> getConstraints() {
return ViewMatchers.isAssignableFrom(AppBarLayout.class);
}
@Override
public String getDescription() {
return "Collapse AppBarLayout inside of a CollapsingToolbarLayout.";
}
@Override
public void perform(UiController uiController, View view) {
AppBarLayout appBarLayout = (AppBarLayout) view;
appBarLayout.setExpanded(false, false);
}
};
}
public static ViewAction expand() {
return new ViewAction() {
@Override
public Matcher<View> getConstraints() {
return ViewMatchers.isAssignableFrom(AppBarLayout.class);
}
@Override
public String getDescription() {
return "Expand AppBarLayout inside of a CollapsingToolbarLayout.";
}
@Override
public void perform(UiController uiController, View view) {
AppBarLayout appBarLayout = (AppBarLayout) view;
appBarLayout.setExpanded(true, false);
}
};
}
public static void forIdle() {
onView(isRoot()).perform(new ViewAction() {
@Override
public Matcher<View> getConstraints() {
return isRoot();
}
@Override
public String getDescription() {
return null;
}
@Override
public void perform(UiController uiController, View view) {
uiController.loopMainThreadForAtLeast(1000);
uiController.loopMainThreadUntilIdle();
}
});
}
public static ViewAction setProgress(int progress) {
return new ViewAction() {
@Override
public void perform(UiController uiController, View view) {
SeekBar seekBar = (SeekBar) view;
seekBar.setProgress(progress);
}
@Override
public String getDescription() {
return "Set a progress on a SeekBar";
}
@Override
public Matcher<View> getConstraints() {
return ViewMatchers.isAssignableFrom(SeekBar.class);
}
};
}
public static ViewAction setProgress(int progress) {
return new ViewAction() {
@Override
public void perform(UiController uiController, View view) {
SeekBar seekBar = (SeekBar) view;
seekBar.setProgress(progress);
}
@Override
public String getDescription() {
return "Set a progress on a SeekBar";
}
@Override
public Matcher<View> getConstraints() {
return ViewMatchers.isAssignableFrom(SeekBar.class);
}
};
}
@Test
public void testTakePicture() throws Exception {
TakePictureIdlingResource resource = new TakePictureIdlingResource(
(CameraView) rule.getActivity().findViewById(R.id.camera));
onView(withId(R.id.camera))
.perform(new AnythingAction("take picture") {
@Override
public void perform(UiController uiController, View view) {
CameraView cameraView = (CameraView) view;
cameraView.takePicture();
}
});
try {
IdlingRegistry.getInstance().register(resource);
onView(withId(R.id.camera))
.perform(waitFor(1000))
.check(showingPreview());
assertThat("Didn't receive valid JPEG data.", resource.receivedValidJpeg(), is(true));
} finally {
IdlingRegistry.getInstance().unregister(resource);
resource.close();
}
}
static ViewAction setAspectRatio(@NonNull final AspectRatio ratio) {
return new ViewAction() {
@Override
public Matcher<View> getConstraints() {
return isAssignableFrom(CameraView.class);
}
@Override
public String getDescription() {
return "Set aspect ratio to " + ratio;
}
@Override
public void perform(UiController controller, View view) {
((CameraView) view).setAspectRatio(ratio);
}
};
}
public static ViewAction clickChildViewWithId(final int id) {
return new ViewAction() {
@Override
public Matcher<View> getConstraints() {
return null;
}
@Override
public String getDescription() {
return null;
}
@Override
public void perform(UiController uiController, View view) {
View v = view.findViewById(id);
v.performClick();
}
};
}
public void perform(UiController uiController, View view) {
RecyclerView recyclerView = (RecyclerView) view;
(new ScrollToPositionViewAction(this.position)).perform(uiController, view);
uiController.loopMainThreadUntilIdle();
View targetView = recyclerView.getChildAt(this.position).findViewById(this.viewId);
if (targetView == null) {
throw (new PerformException.Builder()).withActionDescription(this.toString())
.withViewDescription(
HumanReadables.describe(view))
.withCause(new IllegalStateException(
"No view with id "
+ this.viewId
+ " found at position: "
+ this.position))
.build();
} else {
this.viewAction.perform(uiController, targetView);
}
}
public void perform(UiController uiController, View view) {
RecyclerView recyclerView = (RecyclerView) view;
(new ScrollToPositionViewAction(this.position)).perform(uiController, view);
uiController.loopMainThreadUntilIdle();
View targetView = recyclerView.getChildAt(this.position).findViewById(this.viewId);
if (targetView == null) {
throw (new PerformException.Builder()).withActionDescription(this.toString())
.withViewDescription(
HumanReadables.describe(view))
.withCause(new IllegalStateException(
"No view with id "
+ this.viewId
+ " found at position: "
+ this.position))
.build();
} else {
this.viewAction.perform(uiController, targetView);
}
}
/**
* Perform action of waiting for a specific time. Useful when you need
* to wait for animations to end and Espresso fails at waiting.
* <p/>
* E.g.:
* onView(isRoot()).perform(waitAtLeast(Sampling.SECONDS_15));
*
* @param millis
* @return
*/
public static ViewAction waitAtLeast(final long millis) {
return new ViewAction() {
@Override
public Matcher<View> getConstraints() {
return anything();
}
@Override
public String getDescription() {
return "wait for at least " + millis + " millis.";
}
@Override
public void perform(final UiController uiController, final View view) {
uiController.loopMainThreadUntilIdle();
uiController.loopMainThreadForAtLeast(millis);
}
};
}
/**
* Perform action of waiting until UI thread is free.
* <p/>
* E.g.:
* onView(isRoot()).perform(waitUntilIdle());
*
* @return
*/
public static ViewAction waitUntilIdle() {
return new ViewAction() {
@Override
public Matcher<View> getConstraints() {
return anything();
}
@Override
public String getDescription() {
return "wait until UI thread is free";
}
@Override
public void perform(final UiController uiController, final View view) {
uiController.loopMainThreadUntilIdle();
}
};
}
private String getText(final Matcher<View> matcher) {
final String[] stringHolder = {null};
onView(matcher).perform(new ViewAction() {
@Override
public Matcher<View> getConstraints() {
return isAssignableFrom(TextView.class);
}
@Override
public String getDescription() {
return "getting text from a TextView";
}
@Override
public void perform(UiController uiController, View view) {
TextView tv = (TextView) view;
stringHolder[0] = tv.getText().toString();
}
});
return stringHolder[0];
}
public static ViewAction clickEventItemAvatar() {
return new ViewAction() {
@Override
public Matcher<View> getConstraints() {
return null;
}
@Override
public String getDescription() {
return null;
}
@Override
public void perform(UiController uiController, View view) {
view.findViewById(R.id.event_list_item_avatar).callOnClick();
}
};
}
/**
* Clicks on the tab for the provided Unit Type name. Note that the Unit Type
* doesn't need to be visible.
*/
public static void selectUnitTypeDirect(final String unitTypeName) {
onView(allOf(withText(unitTypeName))).perform(
new ViewAction() {
@Override
public Matcher<View> getConstraints() {
return isEnabled(); // no constraints, they are checked above
}
@Override
public String getDescription() {
return "click unit type" + unitTypeName;
}
@Override
public void perform(UiController uiController, View view) {
view.performClick();
}
}
);
}
/**
* Perform action of waiting for a specific time. Useful when you need
* to wait for animations to end and Espresso fails at waiting.
* <p/>
* E.g.:
* onView(isRoot()).perform(waitAtLeast(Sampling.SECONDS_15));
*
* @param millis
* @return
*/
public static ViewAction waitAtLeast(final long millis) {
return new ViewAction() {
@Override
public Matcher<View> getConstraints() {
return anything();
}
@Override
public String getDescription() {
return "wait for at least " + millis + " millis.";
}
@Override
public void perform(final UiController uiController, final View view) {
uiController.loopMainThreadUntilIdle();
uiController.loopMainThreadForAtLeast(millis);
}
};
}
/**
* Perform action of waiting until UI thread is free.
* <p/>
* E.g.:
* onView(isRoot()).perform(waitUntilIdle());
*
* @return
*/
public static ViewAction waitUntilIdle() {
return new ViewAction() {
@Override
public Matcher<View> getConstraints() {
return anything();
}
@Override
public String getDescription() {
return "wait until UI thread is free";
}
@Override
public void perform(final UiController uiController, final View view) {
uiController.loopMainThreadUntilIdle();
}
};
}
@Override
public final void perform(UiController uiController, View view) {
uiController.loopMainThreadUntilIdle();
long finishTime = System.currentTimeMillis() + duration;
while (System.currentTimeMillis() < finishTime) {
if (isConditionMet(view)) {
return;
}
uiController.loopMainThreadForAtLeast(50L);
}
throw new PerformException.Builder()
.withActionDescription(this.getDescription())
.withViewDescription(HumanReadables.describe(view))
.withCause(new TimeoutException())
.build();
}
public static ViewAction forceComposingText(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.setComposingText(text, -1);
}
};
}
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);
}
};
}
@Override
public void perform(UiController uiController, View view) {
uiController.loopMainThreadUntilIdle();
Activity activity = getActivity(view.getContext());
if (activity == null && view instanceof ViewGroup) {
ViewGroup viewGroup = (ViewGroup) view;
int childCount = viewGroup.getChildCount();
for (int i = 0; i < childCount && activity == null; ++i) {
activity = getActivity(viewGroup.getChildAt(i).getContext());
}
}
activity.setRequestedOrientation(orientation);
}