Skip to content

Instantly share code, notes, and snippets.

@Neferetheka
Created February 9, 2017 16:15
Show Gist options
  • Save Neferetheka/e955e3d127f3f3a577437e048211c7f7 to your computer and use it in GitHub Desktop.
Save Neferetheka/e955e3d127f3f3a577437e048211c7f7 to your computer and use it in GitHub Desktop.
BaseActivityTest
/**
* Base class for all Espresso tests
*/
public abstract class BaseActivityTest {
protected boolean doesViewExist(@IdRes int id) {
try {
onView(withId(id)).check(matches(isDisplayed()));
return true;
} catch (AssertionFailedError er){
return false;
}catch (Exception e){
return false;
}
}
/**
* Perform action of waiting for a specific time.
*/
protected 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, View view) {
uiController.loopMainThreadForAtLeast(millis);
}
};
}
protected static Matcher<View> childAtPosition(
final Matcher<View> parentMatcher, final int position) {
return new TypeSafeMatcher<View>() {
@Override
public void describeTo(Description description) {
description.appendText("Child at position " + position + " in parent ");
parentMatcher.describeTo(description);
}
@Override
public boolean matchesSafely(View view) {
ViewParent parent = view.getParent();
return (parent instanceof ViewGroup) && parentMatcher.matches(parent)
&& view.equals(((ViewGroup) parent).getChildAt(position));
}
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment