Skip to content

Instantly share code, notes, and snippets.

@uqmessias
Created June 22, 2016 17:31
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 uqmessias/6c57b6177f373fcc275828758124ff02 to your computer and use it in GitHub Desktop.
Save uqmessias/6c57b6177f373fcc275828758124ff02 to your computer and use it in GitHub Desktop.
package br.com.uilquemessias.utils;
import android.os.IBinder;
import android.support.test.espresso.Root;
import android.view.WindowManager;
import org.androidannotations.annotations.res.StringRes;
import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.hamcrest.TypeSafeMatcher;
import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.assertion.ViewAssertions.matches;
import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed;
import static android.support.test.espresso.matcher.ViewMatchers.withText;
public final class CustomMatcher {
private CustomMatcher() {
}
public static void isToastMessageDisplayed(@StringRes int textId) {
onView(withText(textId)).inRoot(isToast()).check(matches(isDisplayed()));
}
public static void isToastMessageDisplayed(String text) {
onView(withText(text)).inRoot(isToast()).check(matches(isDisplayed()));
}
public static Matcher<Root> isToast() {
return new ToastMatcher();
}
public static class ToastMatcher extends 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();
// 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.
if (windowToken == appToken)
return true;
}
return false;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment