Skip to content

Instantly share code, notes, and snippets.

@araujotadeu
Created July 28, 2017 14:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save araujotadeu/59cf02c8ae88f3ac512c3fee6c814ff6 to your computer and use it in GitHub Desktop.
Save araujotadeu/59cf02c8ae88f3ac512c3fee6c814ff6 to your computer and use it in GitHub Desktop.
public abstract class ViewActions {
public static final int DELAY = 100;
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)) {
if (viewMatcher.matches(child) && child.getVisibility() == View.VISIBLE) {
return;
}
}
uiController.loopMainThreadForAtLeast(DELAY);
}
while (System.currentTimeMillis() < endTime);
throw new PerformException.Builder()
.withActionDescription(this.getDescription())
.withViewDescription(HumanReadables.describe(view))
.withCause(new TimeoutException())
.build();
}
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment