如何在Android Espresso上检查/测试Snackbar解雇

时间:2016-10-16 06:05:05

标签: android android-espresso

我想测试Espresso是否知道如何检查Snackbar被解雇?我目前正在删除标记并显示Snackbar,让用户有机会撤消。我在Snackbar.LEGNTH_LONG之后删除了代码,我想在Snackbar.LEGNTH_LONG之后测试我的代码是否仍在列表中。

我还没想到这一点。这可以在Espresso上使用吗?

1 个答案:

答案 0 :(得分:2)

您可以将此课程添加到您的项目中:https://android.googlesource.com/platform/frameworks/support/+/a1de3ee/design/tests/src/android/support/design/testutils/SnackbarUtils.java

它是包含此方法的utils类:

/**
 * Helper method that dismissed that specified {@link Snackbar} and waits until
 * it has been fully dismissed. Note that calling this method will reset the currently
 * set {@link Snackbar.Callback}.
 */
public static void dismissSnackbarAndWaitUntilFullyDismissed(Snackbar snackbar) {
    SnackbarDismissedCallback snackbarCallback = new SnackbarDismissedCallback();
    snackbar.setCallback(snackbarCallback);
    try {
        // Register our listener as idling resource so that Espresso waits until the
        // the snackbar has been fully dismissed
        Espresso.registerIdlingResources(snackbarCallback);
        // Dismiss the snackbar
        snackbar.dismiss();
        // Mark the callback to require waiting for idle state
        snackbarCallback.mNeedsIdle = true;
        // Perform a dummy Espresso action that loops until the UI thread is idle. This
        // effectively blocks us until the Snackbar has completed its sliding animation.
        onView(isRoot()).perform(waitUntilIdle());
        snackbarCallback.mNeedsIdle = false;
    } finally {
        // Unregister our idling resource
        Espresso.unregisterIdlingResources(snackbarCallback);
        // And remove our tracker listener from Snackbar
        snackbar.setCallback(null);
    }
}

然后执行:

SnackbarUtils.dismissSnackbarAndWaitUntilFullyDismissed(snackbar);
在检查您的标签是否仍在列表中之前,在测试中

。我还没有对此进行测试,但我认为它应该可行。