Skip to content

Instantly share code, notes, and snippets.

@moltak
Created December 2, 2015 04:34
Show Gist options
  • Save moltak/a8d1d5a7aca45875e760 to your computer and use it in GitHub Desktop.
Save moltak/a8d1d5a7aca45875e760 to your computer and use it in GitHub Desktop.
Matcher that is Toast window
/**
* Matcher that is Toast window.
*/
public static Matcher<Root> isToast() {
return new TypeSafeMatcher<Root>() {
@Override
public void describeTo(Description description) {
description.appendText("is toast");
}
@Override
public boolean matchesSafely(Root root) {
int type = root.getWindowLayoutParams().get().type;
if ((type == WindowManager.LayoutParams.TYPE_TOAST)) {
IBinder windowToken = root.getDecorView().getWindowToken();
IBinder appToken = root.getDecorView().getApplicationWindowToken();
if (windowToken == appToken) {
// windowToken == appToken means this window isn't contained by any other windows.
// if it was a window for an activity, it would have TYPE_BASE_APPLICATION.
return true;
}
}
return false;
}
};
}
// How to use
onView(isRoot()).inRoot(isToast()).check(matches(withText("Request has been sent.")));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment