下面列出了android.support.test.espresso.UiController#loopMainThreadUntilIdle ( ) 实例代码,或者点击链接到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();
}
};
}
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();
}
};
}
/**
* 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();
}
@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);
}
@Override
public void perform(UiController uiController, View view) {
RecyclerView recyclerView = (RecyclerView) view;
int itemCount = recyclerView.getAdapter().getItemCount();
try {
recyclerView.scrollToPosition(itemCount - 1);
uiController.loopMainThreadUntilIdle();
} catch (RuntimeException e) {
throw new PerformException.Builder().withActionDescription(this.getDescription())
.withViewDescription(HumanReadables.describe(view)).withCause(e).build();
}
}
@Override
public final void perform(UiController uiController, View view) {
final StepperLayout stepperLayout = (StepperLayout) view;
final ViewPager viewPager = (ViewPager) stepperLayout.findViewById(com.stepstone.stepper.R.id.ms_stepPager);
// Add a custom tracker listener
final CustomViewPagerListener customListener = new CustomViewPagerListener();
viewPager.addOnPageChangeListener(customListener);
// Note that we're running the following block in a try-finally construct. This
// is needed since some of the actions are going to throw (expected) exceptions. If that
// happens, we still need to clean up after ourselves to leave the system (Espresso) in a good
// state.
try {
// Register our listener as idling resource so that Espresso waits until the
// wrapped action results in the view pager getting to the STATE_IDLE state
Espresso.registerIdlingResources(customListener);
uiController.loopMainThreadUntilIdle();
performAction(stepperLayout);
uiController.loopMainThreadUntilIdle();
customListener.mNeedsIdle = true;
uiController.loopMainThreadUntilIdle();
customListener.mNeedsIdle = false;
} finally {
// Unregister our idling resource
Espresso.unregisterIdlingResources(customListener);
// And remove our tracker listener from ViewPager
viewPager.removeOnPageChangeListener(customListener);
}
}
public static ViewAction waitId(final int viewId, final long millis) {
return new ViewAction() {
@Override
public Matcher<View> getConstraints() {
return isRoot();
}
@Override
public String getDescription() {
return "wait for a specific view with id <" + viewId + "> during " + millis + " millis.";
}
@Override
public void perform(final UiController uiController, final View view) {
uiController.loopMainThreadUntilIdle();
final long startTime = System.currentTimeMillis();
final long endTime = startTime + millis;
final Matcher<View> viewMatcher = withId(viewId);
do {
for (View child : TreeIterables.breadthFirstViewTraversal(view)) {
// found view with required ID
if (viewMatcher.matches(child)) {
return;
}
}
uiController.loopMainThreadForAtLeast(50);
}
while (System.currentTimeMillis() < endTime);
// timeout happens
throw new PerformException.Builder()
.withActionDescription(this.getDescription())
.withViewDescription(HumanReadables.describe(view))
.withCause(new TimeoutException())
.build();
}
};
}
public static ViewAction nestedScrollTo() {
return new ViewAction() {
@Override
public Matcher<View> getConstraints() {
return allOf(isDescendantOfA(isAssignableFrom(NestedScrollView.class)), withEffectiveVisibility(
ViewMatchers.Visibility.VISIBLE));
}
@Override
public String getDescription() {
return "Scrolling to view inside NestedScrollView";
}
@Override
public void perform(final UiController uiController, final View view) {
try {
final NestedScrollView nestedScrollView = (NestedScrollView)
findFirstParentLayoutOfClass(view, NestedScrollView.class);
if (nestedScrollView != null) {
final CoordinatorLayout coordinatorLayout =
(CoordinatorLayout) findFirstParentLayoutOfClass(view, CoordinatorLayout.class);
if (coordinatorLayout != null) {
final CollapsingToolbarLayout collapsingToolbarLayout =
findCollapsingToolbarLayoutChildIn(coordinatorLayout);
if (collapsingToolbarLayout != null) {
final int toolbarHeight = collapsingToolbarLayout.getHeight();
nestedScrollView.startNestedScroll(ViewCompat.SCROLL_AXIS_VERTICAL);
nestedScrollView.dispatchNestedPreScroll(0, toolbarHeight, null, null);
}
}
nestedScrollView.scrollTo(0, view.getTop());
} else {
throw new Exception("Unable to find NestedScrollView parent.");
}
} catch (final Exception e) {
throw new PerformException.Builder()
.withActionDescription(this.getDescription())
.withViewDescription(HumanReadables.describe(view))
.withCause(e)
.build();
}
uiController.loopMainThreadUntilIdle();
}
};
}
@Override
public void perform(UiController uiController, View view) {
uiController.loopMainThreadUntilIdle();
final Activity activity = (Activity) view.getContext();
activity.setRequestedOrientation(orientation);
}
/**
* Perform action of waiting for a specific view id.
* <p/>
* E.g.:
* onView(isRoot()).perform(waitId(R.id.dialogEditor, Sampling.SECONDS_15));
*
* @param viewId
* @param millis
* @return
*/
public static ViewAction waitId(final int viewId, final long millis) {
return new ViewAction() {
@Override
public Matcher<View> getConstraints() {
return isRoot();
}
@Override
public String getDescription() {
return "wait for a specific view with id <" + viewId + "> during " + millis + " millis.";
}
@Override
public void perform(final UiController uiController, final View view) {
uiController.loopMainThreadUntilIdle();
final long startTime = System.currentTimeMillis();
final long endTime = startTime + millis;
final Matcher<View> viewMatcher = withId(viewId);
do {
for (View child : TreeIterables.breadthFirstViewTraversal(view)) {
// found view with required ID
if (viewMatcher.matches(child)) {
return;
}
}
uiController.loopMainThreadForAtLeast(50);
}
while (System.currentTimeMillis() < endTime);
// timeout happens
throw new PerformException.Builder()
.withActionDescription(this.getDescription())
.withViewDescription(HumanReadables.describe(view))
.withCause(new TimeoutException())
.build();
}
};
}
/**
* Perform action of waiting for a specific view id.
* <p/>
* E.g.:
* onView(isRoot()).perform(waitId(R.id.dialogEditor, Sampling.SECONDS_15));
*
* @param viewId
* @param millis
* @return
*/
public static ViewAction waitId(final int viewId, final long millis) {
return new ViewAction() {
@Override
public Matcher<View> getConstraints() {
return isRoot();
}
@Override
public String getDescription() {
return "wait for a specific view with id <" + viewId + "> during " + millis + " millis.";
}
@Override
public void perform(final UiController uiController, final View view) {
uiController.loopMainThreadUntilIdle();
final long startTime = System.currentTimeMillis();
final long endTime = startTime + millis;
final Matcher<View> viewMatcher = withId(viewId);
do {
for (View child : TreeIterables.breadthFirstViewTraversal(view)) {
// found view with required ID
if (viewMatcher.matches(child)) {
return;
}
}
uiController.loopMainThreadForAtLeast(50);
}
while (System.currentTimeMillis() < endTime);
// timeout happens
throw new PerformException.Builder()
.withActionDescription(this.getDescription())
.withViewDescription(HumanReadables.describe(view))
.withCause(new TimeoutException())
.build();
}
};
}