Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.