Android Espresso:如何检查 Toast 消息未显示? [英] Android Espresso: How to check that Toast message is NOT shown?

查看:29
本文介绍了Android Espresso:如何检查 Toast 消息未显示?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在正在我的功能测试中工作,在其中一个我必须测试没有显示 toast 消息.考虑到这是我用来检查 Toast 消息是否显示的代码(此代码有效):

I'm working now in my functional tests and in one of them I have to test that a toast message is NOT shown. Considering this is the code I'm using to check if the toast message is shown (this code works):

onView(withText(R.string.my_toast_message))
        .inRoot(withDecorView(not(getActivity().getWindow().getDecorView())))
        .check(matches(isDisplayed()));

您可以在下面找到我用来检查 toast 消息是否未显示的代码(它们都不起作用):

below you can find the code I'm using to check that a toast message is NOT shown (none of them work):

方法一:

onView(withText(R.string.error_invalid_login))
        .inRoot(withDecorView(not(getActivity().getWindow().getDecorView())))
        .check(matches(not(isDisplayed())));

方法二:

onView(withText(R.string.error_invalid_login))
        .inRoot(withDecorView(not(getActivity().getWindow().getDecorView())))
        .check(doesNotExist());

关于如何检查 toast 消息未显示的任何想法将非常感激:)

Any idea about how can I check that a toast message is not shown would be really appreciated :)

推荐答案

需要捕获toast不存在的情况,抛出NoMatchingRootException.下面显示了捕捉它的浓缩咖啡方式".

It is required to catch the case when the toast does not exist, for which a NoMatchingRootException is thrown. Below shows the "Espresso way" of catching that.

public static Matcher<Root> isToast() {
    return new WindowManagerLayoutParamTypeMatcher("is toast", WindowManager.LayoutParams.TYPE_TOAST);
}
public static void assertNoToastIsDisplayed() {
    onView(isRoot())
            .inRoot(isToast())
            .withFailureHandler(new PassMissingRoot())
            .check(matches(not(anything("toast root existed"))))
    ;
}

使用上述内容的快速(自我)测试:

A quick (self-)test that uses the above:

@Test public void testToastMessage() {
    Toast toast = createToast("Hello Toast!");
    assertNoToastIsDisplayed();
    toast.show();
    onView(withId(android.R.id.message))
            .inRoot(isToast())
            .check(matches(withText(containsStringIgnoringCase("hello"))));
    toast.cancel();
    assertNoToastIsDisplayed();
}

private Toast createToast(final String message) {
    final AtomicReference<Toast> toast = new AtomicReference<>();
    InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable() {
        @SuppressLint("ShowToast") // will be shown later
        @Override public void run() {
            toast.set(Toast.makeText(InstrumentationRegistry.getContext(), message, Toast.LENGTH_LONG));
        }
    });
    return toast.get();
}

神奇的可重用辅助类:

public class PassMissingRoot implements FailureHandler {
    private final FailureHandler defaultHandler
            = new DefaultFailureHandler(InstrumentationRegistry.getTargetContext());
    @Override public void handle(Throwable error, Matcher<View> viewMatcher) {
        if (!(error instanceof NoMatchingRootException)) {
            defaultHandler.handle(error, viewMatcher);
        }
    }
}

public class WindowManagerLayoutParamTypeMatcher extends TypeSafeMatcher<Root> {
    private final String description;
    private final int type;
    private final boolean expectedWindowTokenMatch;
    public WindowManagerLayoutParamTypeMatcher(String description, int type) {
        this(description, type, true);
    }
    public WindowManagerLayoutParamTypeMatcher(String description, int type, boolean expectedWindowTokenMatch) {
        this.description = description;
        this.type = type;
        this.expectedWindowTokenMatch = expectedWindowTokenMatch;
    }
    @Override public void describeTo(Description description) {
        description.appendText(this.description);
    }
    @Override public boolean matchesSafely(Root root) {
        if (type == root.getWindowLayoutParams().get().type) {
            IBinder windowToken = root.getDecorView().getWindowToken();
            IBinder appToken = root.getDecorView().getApplicationWindowToken();
            if (windowToken == appToken == expectedWindowTokenMatch) {
                // 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;
    }
}

这篇关于Android Espresso:如何检查 Toast 消息未显示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆