Last active
November 15, 2017 19:37
-
-
Save forceLain/d4dd90ef7ebdf3c8b72b to your computer and use it in GitHub Desktop.
Espresso custom actions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
}; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@fede1608 i know this is a little late, but this trick worked for me