Skip to content

Instantly share code, notes, and snippets.

@digitalbuddha
Created March 17, 2016 19:22
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 digitalbuddha/5d6f2c9f26620078ba80 to your computer and use it in GitHub Desktop.
Save digitalbuddha/5d6f2c9f26620078ba80 to your computer and use it in GitHub Desktop.
protected void clickButton(int id) {
onView(withId(id)).perform(click());
}
protected void clickOKButton() {
onView(withId(R.id.yes)).perform(click());
}
protected void clickYesButton() {
onView(withText("Yes")).perform(click());
}
protected void clickButton(final String text) {
onView(withText(text)).perform(click());
}
protected boolean isAvailable(final int id) {
return (getActivity().findViewById(id) != null && (getActivity().findViewById(id) instanceof View));
//return onView(withId(id)).exists();
}
protected static Matcher<View> withoutProgress(final int expectedProgress) {
return new BoundedMatcher<View, SeekBar>(SeekBar.class) {
@Override
public void describeTo(Description description) {
description.appendText("expected: ");
description.appendText("" + expectedProgress);
}
@Override
public boolean matchesSafely(SeekBar seekBar) {
int progress = seekBar.getProgress();
return progress == expectedProgress;
}
};
}
protected static Matcher<View> withProgress(final int expectedProgress) {
return new BoundedMatcher<View, SeekBar>(SeekBar.class) {
@Override
public void describeTo(Description description) {
description.appendText("expected: ");
description.appendText("" + expectedProgress);
}
@Override
public boolean matchesSafely(SeekBar seekBar) {
int progress = seekBar.getProgress();
return progress != expectedProgress;
}
};
}
protected static Matcher<View> withRotation(final int expectedRotation) {
return new BoundedMatcher<View, ImageView>(ImageView.class) {
@Override
public void describeTo(Description description) {
description.appendText("expected: ");
description.appendText("" + expectedRotation);
}
@Override
public boolean matchesSafely(ImageView needle) {
float rotation = needle.getRotation();
return rotation == expectedRotation;
}
};
}
public static ViewAction setProgress(final int progress) {
return new ViewAction() {
@Override
public void perform(UiController uiController, View view) {
SeekBar seekBar = (SeekBar) view;
seekBar.setProgress(progress);
}
@Override
public Matcher<View> getConstraints() {
return ViewMatchers.isAssignableFrom(SeekBar.class);
}
@Override
public String getDescription() {
return "Set progress";
}
};
}
protected void openDrawer(final int id) {
DrawerActions.openDrawer(id);
}
protected void closeDrawer(final int id) {
DrawerActions.closeDrawer(id);
}
protected void swipeLeftOne(int id) {
onView(withId(id)).perform(swipeLeft());
}
protected void swipeRightOne(int id) {
onView(withId(id)).perform(swipeRight());
}
protected void assertIsVisible(int id) {
onView(withId(id)).check(matches(isCompletelyDisplayed()));
}
protected void assertIsPartiallyVisible(int id) {
onView(withId(id)).check(matches(isDisplayed()));
}
protected void assertIsVisibleWithScroll(int id) {
onView(withId(id)).perform(scrollTo());
onView(withId(id)).check(matches(isCompletelyDisplayed()));
}
protected void assertTextShouldMatch(int id, String text) {
assertIsVisible(id);
onView(withId(id)).check(matches(withText(text)));
}
protected void assertTextShouldMatchWithin(int id, int lowerid, String text) {
assertIsVisibleWithScroll(id);
onView(AllOf.allOf(withId(lowerid), ViewMatchers.isDescendantOfA(withId(id)))).check(matches(withText(text)));
onView(AllOf.allOf(withId(lowerid), ViewMatchers.isDescendantOfA(withId(id)))).check(matches(isCompletelyDisplayed()));
}
protected void assertTextShouldMatchWithinWithEllipsis(int id, int lowerid, String text) {
assertIsVisibleWithScroll(id);
onView(AllOf.allOf(withId(lowerid), ViewMatchers.isDescendantOfA(withId(id)))).check(matches(withTextEllipses(text)));
onView(AllOf.allOf(withId(lowerid), ViewMatchers.isDescendantOfA(withId(id)))).check(matches(isCompletelyDisplayed()));
}
protected void assertShouldBeInvisible(int id) {
onView(withId(id)).check(matches(withEffectiveVisibility(ViewMatchers.Visibility.INVISIBLE)));
}
protected void assertShouldNotExist(int id) {
onView(withId(id)).check(doesNotExist());
}
protected void assertShouldBeVisible(final String text) {
//Used for finding loose buttons
onView(withText(text)).check(matches(isCompletelyDisplayed()));
}
protected void assertShouldBeInvisible(final String text) {
//Used for finding loose buttons
onView(withText(text)).check(doesNotExist());
}
protected void waitFor(long time) {
synchronized (this) {
try {
this.wait(time);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
protected String getString(int id) {
return getActivity().getResources().getString(id);
}
protected Matcher<View> withHint(final String expectedHint) {
return new TypeSafeMatcher<View>() {
@Override
public boolean matchesSafely(View view) {
if (!(view instanceof EditText)) {
return false;
}
String hint = ((EditText) view).getHint().toString();
return expectedHint.equals(hint);
}
@Override
public void describeTo(Description description) {
}
};
}
protected Matcher<View> withChild(final String expectedText, final int childid) {
return new TypeSafeMatcher<View>() {
@Override
public boolean matchesSafely(View view) {
if (!(view instanceof View) || view.findViewById(childid) == null) {
return false;
}
TextView childText = (TextView) view.findViewById(childid);
String match = childText.getText().toString();
return expectedText.equals(match);
}
@Override
public void describeTo(Description description) {
}
};
}
public static Matcher<View> isDisplaying100Percent(final int childId) {
final int areaPercentage = 100;
return new TypeSafeMatcher<View>() {
@Override
public void describeTo(Description description) {
description.appendText(String.format(
"at least %s percent of the view's area is displayed to the user.", areaPercentage));
}
@Override
public boolean matchesSafely(View pView) {
if (!(pView instanceof View) || pView.findViewById(childId) == null) {
return false;
}
View view = pView.findViewById(childId);
Rect visibleParts = new Rect();
boolean visibleAtAll = view.getGlobalVisibleRect(visibleParts);
if (!visibleAtAll) {
return false;
}
double maxArea = view.getHeight() * view.getWidth();
double visibleArea = visibleParts.height() * visibleParts.width();
int displayedPercentage = (int) ((visibleArea / maxArea) * 100);
return displayedPercentage >= areaPercentage
&& withEffectiveVisibility(ViewMatchers.Visibility.VISIBLE).matches(view);
}
};
}
public static Matcher<View> withTextEllipses(final String stringMatcher) {
Preconditions.checkNotNull(stringMatcher);
final int leastMatch = 15;
return new BoundedMatcher<View, TextView>(TextView.class) {
@Override
public void describeTo(Description description) {
description.appendText("with text ellipses: ");
}
@Override
public boolean matchesSafely(TextView textView) {
if (stringMatcher.matches(textView.getText().toString())) {
return true;
} else {
if (stringMatcher.length() >= leastMatch && textView.getText().toString().length() >= leastMatch) {
return stringMatcher.substring(0, leastMatch - 1).equals(textView.getText().toString().substring(0, leastMatch - 1));
}
}
return false;
}
};
}
protected Matcher<View> withError(final String expectedHint) {
return new TypeSafeMatcher<View>() {
@Override
public boolean matchesSafely(View view) {
if (!(view instanceof EditText)) {
return false;
}
String error = ((EditText) view).getError().toString();
return expectedHint.equals(error);
}
@Override
public void describeTo(Description description) {
}
};
}
protected void progressPosition(int id, int position) {
onView(withId(id)).perform(setProgress(position));
}
protected Matcher<View> withSelectedTab(final int tab) {
return new TypeSafeMatcher<View>() {
@Override
public boolean matchesSafely(View view) {
if (!(view instanceof ViewPager)) {
return false;
}
int tabNum = ((ViewPager) view).getCurrentItem();
return tabNum == tab;
}
@Override
public void describeTo(Description description) {
}
};
}
protected void hasCurrentPage(int id, int page) {
onView(withId(id)).check(matches(withSelectedTab(page)));
}
protected void hasDialog(String text) {
onView(withText(text)).check(matches(isDisplayed()));
}
public void assertIsInBackground() {
ActivityManager am = (ActivityManager) getActivity().getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningTaskInfo> tasks = am.getRunningTasks(1);
ComponentName topActivity = tasks.get(0).topActivity;
assertNotSame(topActivity.getPackageName(), (getActivity().getPackageName()));
}
public void assertIsSomethingInForeground(final String activity) {
ActivityManager am = (ActivityManager) getActivity().getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningTaskInfo> tasks = am.getRunningTasks(1);
ComponentName topActivity = tasks.get(0).topActivity;
assertEquals(topActivity.getPackageName(), activity);
}
public void assertIsInForeground() {
ActivityManager am = (ActivityManager) getActivity().getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningTaskInfo> tasks = am.getRunningTasks(1);
ComponentName topActivity = tasks.get(0).topActivity;
assertEquals(topActivity.getPackageName(), getActivity().getPackageName());
}
protected void assertHasHint(int id, String hint) {
onView(withId(id)).check(matches(withHint(hint)));
}
protected void assertHasError(int id, String error) {
onView(withId(id)).check(matches(withError(error)));
}
protected void assertHasNotChanged(int id, int expectedProgress) {
onView(withId(id)).check(matches(withoutProgress(expectedProgress)));
}
protected void assertHasReset(int id, int expected) {
onView(withId(id)).check((matches(withProgress(expected))));
}
protected void typeIn(int id, String text) {
onView(withId(id)).perform(typeText(text));
assertHasTypedText(id, text);
}
protected void assertHasTypedText(int id, String text) {
onView(withId(id)).check(matches(withText(text)));
}
protected void assertAngleIsCorrect(int id, int rotation) {
onView(withId(id)).check(matches(withRotation(rotation)));
}
protected void emptyText(int id) {
onView(withId(id)).perform(clearText());
assertHasTypedText(id, "");
}
protected void pressSoftKey(int id, int key) {
onView(withId(id)).perform(pressKey(key));
}
protected void assertDrawerIsClosed(final int id) {
onView(withId(id)).check(matches(DrawerMatchers.isClosed()));
}
protected void assertDrawerIsOpen(final int id) {
onView(withId(id)).check(matches(DrawerMatchers.isOpen()));
}
protected void assertItemVisibleInDrawer(final String item) {
onView(withText(item)).check(matches(isCompletelyDisplayed()));
}
protected void assertItemNotVisibleInDrawer(final String item) {
onView(withText(item)).check(doesNotExist());
}
protected void clickDrawerItem(final String item) {
onView(withText(item)).perform(click());
}
public Activity getCurrentActivity() {
final Activity[] currentActivity = new Activity[1];
getInstrumentation().runOnMainSync(new Runnable() {
public void run() {
Collection<Activity> resumedActivities = ActivityLifecycleMonitorRegistry.getInstance().getActivitiesInStage(Stage.RESUMED);
for (Activity act : resumedActivities) {
Log.d("Your current activity: ", act.getClass().getName());
currentActivity[0] = act;
break;
}
}
});
return currentActivity[0];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment