[英]How to check/test Snackbar dismisses on Android Espresso
I wanted to test if at all possible Espresso knows how to check Snackbar
dismissal? 我想测试一下Espresso是否知道如何检查
Snackbar
解雇? I am currently deleting a tag and shows a Snackbar
giving the user a chance to undo. 我目前正在删除标签并显示
Snackbar
让用户有机会撤消。 I delete the tag after a Snackbar.LEGNTH_LONG
and I wanted to test if my tag is still on the list after Snackbar.LEGNTH_LONG
. 我删除标签后
Snackbar.LEGNTH_LONG
,我想测试,如果我的标签之后仍然是名单上Snackbar.LEGNTH_LONG
。
I haven't figured this out yet. 我还没想出来。 Is this possible on Espresso?
Espresso有可能吗?
You can add this class to your project: https://android.googlesource.com/platform/frameworks/support/+/a1de3ee/design/tests/src/android/support/design/testutils/SnackbarUtils.java 您可以将此类添加到项目中: https : //android.googlesource.com/platform/frameworks/support/+/a1de3ee/design/tests/src/android/support/design/testutils/SnackbarUtils.java
it's an utils class which contains this method: 它是一个包含此方法的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);
}
}
and then execute: 然后执行:
SnackbarUtils.dismissSnackbarAndWaitUntilFullyDismissed(snackbar);
in your test before checking if your tag is still on the list. 在您的测试中,检查您的标签是否仍在列表中。 I haven't tested this but I think it should work.
我没有测试过这个,但我认为它应该可行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.