Created
February 9, 2017 16:15
-
-
Save Neferetheka/e955e3d127f3f3a577437e048211c7f7 to your computer and use it in GitHub Desktop.
BaseActivityTest
This file contains hidden or 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
/** | |
* 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