Skip to content

Instantly share code, notes, and snippets.

@forceLain
Last active November 15, 2017 19:37
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 forceLain/d4dd90ef7ebdf3c8b72b to your computer and use it in GitHub Desktop.
Save forceLain/d4dd90ef7ebdf3c8b72b to your computer and use it in GitHub Desktop.
Espresso custom actions
import android.view.View;
import com.google.android.apps.common.testing.ui.espresso.PerformException;
import com.google.android.apps.common.testing.ui.espresso.UiController;
import com.google.android.apps.common.testing.ui.espresso.ViewAction;
import com.google.android.apps.common.testing.ui.espresso.util.HumanReadables;
import com.google.android.apps.common.testing.ui.espresso.util.TreeIterables;
import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.hamcrest.StringDescription;
import java.util.concurrent.TimeoutException;
import static com.google.android.apps.common.testing.ui.espresso.matcher.ViewMatchers.withId;
import static org.hamcrest.Matchers.anything;
public class CustomActions {
public static ViewAction waitId(final int viewId, final long millis) {
return wait(withId(viewId), millis);
}
public static ViewAction wait(final Matcher<View> matcher, final long millis) {
return new ViewAction() {
@Override
public Matcher<View> getConstraints() {
return anything();
}
@Override
public String getDescription() {
Description d = new StringDescription();
matcher.describeTo(d);
d.appendText("wait for a specific matcher <");
matcher.describeTo(d);
d.appendText("> during ").appendValue(millis).appendText(" millis.");
return d.toString();
}
@Override
public void perform(final UiController uiController, final View view) {
uiController.loopMainThreadUntilIdle();
final long startTime = System.currentTimeMillis();
final long endTime = startTime + millis;
do {
for (View child : TreeIterables.breadthFirstViewTraversal(view)) {
// found view with required ID
if (matcher.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 waitView(final Matcher<View> viewMatcher, final long millis) {
return new ViewAction() {
@Override
public Matcher<View> getConstraints() {
return anything();
}
@Override
public String getDescription() {
return "wait for a specific view with matcher <" + viewMatcher + "> 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;
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 loopMainThread(final long millis) {
return new ViewAction() {
@Override
public Matcher<View> getConstraints() {
return anything();
}
@Override
public String getDescription() {
return "loop main thread";
}
@Override
public void perform(UiController uiController, View view) {
uiController.loopMainThreadForAtLeast(millis);
}
};
}
}
@fede1608
Copy link

fede1608 commented Feb 3, 2016

public Matcher<View> getConstraints() {
                return anything();
}

The anything method is not working, it returns a Matcher<Object> instead of matcher<View>

@stephengroat
Copy link

stephengroat commented Nov 15, 2017

@fede1608 i know this is a little late, but this trick worked for me

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment