简体   繁体   English

是否可以禁用Toasts或等待吐司在测试时消失

[英]Is it possible to disable Toasts or wait until toast disappears while testing

I am testing an app with Espresso . 我正在使用Espresso测试应用程序。 I have one question is it possible to wait until there are no toast are currently being showed. 我有一个问题是有可能等到目前没有吐司出现。 I have a lot of different toast in my app, but while testing I have problems with them because as far as I can guess focus is gone to the toast and I am getting quite another view hierarchy as I can see in error logs. 我的应用程序中有很多不同的吐司,但是在测试时我遇到了问题,因为据我所知,焦点已经转向吐司,我正在获得另一个视图层次结构,我可以在错误日志中看到。
So my question is it possible to hide all (system wide with root access) or just wait until there are any toasts on the screen or maybe if it is possible to set focus to the activity view hierarchy manually. 所以我的问题是可以隐藏所有(系统范围内的root访问权限)或只是等到屏幕上有任何toast,或者是否可以手动将焦点设置到活动视图层次结构。
I would be grateful for any help with this problem. 如果对这个问题有任何帮助,我将不胜感激。
Thank you. 谢谢。

PS Disabling toast directly somewhere in my app is not an option because it brings some extra logic into the app which is only required while testing. PS禁用toast直接在我的应用程序中的某个地方不是一个选项,因为它为应用程序带来了一些额外的逻辑,这只是在测试时需要。

You can let Espresso wait until all toasts are disappeared with a custom idling resource . 你可以让Espresso等到所有的吐司都用自定义的空闲资源消失。

Here I use CountingIdlingResource which is a idling resource managing a counter: when the counter changes from non-zero to zero it notifies the transition callback. 这里我使用CountingIdlingResource ,这是一个管理计数器的空闲资源:当计数器从非零变为零时,它会通知转换回调。

Here is a complete example; 是一个完整的例子; the key points follow: 关键点如下:

public final class ToastManager {
    private static final CountingIdlingResource idlingResource = new CountingIdlingResource("toast");
    private static final View.OnAttachStateChangeListener listener = new View.OnAttachStateChangeListener() {
        @Override
        public void onViewAttachedToWindow(final View v) {
            idlingResource.increment();
        }

        @Override
        public void onViewDetachedFromWindow(final View v) {
            idlingResource.decrement();
        }
    };

    private ToastManager() { }

    public static Toast makeText(final Context context, final CharSequence text, final int duration) {
        Toast t = Toast.makeText(context, text, duration);
        t.getView().addOnAttachStateChangeListener(listener);
        return t;
    }

    // For testing
    public static IdlingResource getIdlingResource() {
        return idlingResource;
    }
}

How to show the toast: 如何展示吐司:

ToastManager.makeText(this, "Third", Toast.LENGTH_SHORT).show();

How to set-up/tear-down a test: 如何设置/拆除测试:

@Before
public void setUp() throws Exception {
    super.setUp();
    injectInstrumentation(InstrumentationRegistry.getInstrumentation());
    Espresso.registerIdlingResources(ToastManager.getIdlingResource());
    getActivity();
}

@After
public void tearDown() throws Exception {
    super.tearDown();
    Espresso.unregisterIdlingResources(ToastManager.getIdlingResource());
}

I have not found any perfect solution to this, but the best is to make a mToast member variable visible for testing, and use that to cancel any active toast in @After , like this: 我没有找到任何完美的解决方案,但最好是使mToast成员变量可见以进行测试,并使用它来取消@After任何活动吐司,如下所示:

When showing toast (the production code for the Activity under test): 显示toast时(正在测试的Activity的生产代码):

@VisibleForTesting(otherwise = VisibleForTesting.PRIVATE)
Toast mToast;

private void showToast(final String text) {
    mToast = Toast.makeText(this, text, Toast.LENGTH_LONG);
    mToast.show();
}

The test code (in the same package as the code under test): 测试代码(与测试代码在同一个包中):

    @After
    public void tearDown() {
        // Remove any toast message that is still shown:
        Toast toast = mActivityRule.getActivity().mToast;
        if (toast != null) {
            toast.cancel();
        }
    }

This will require you to change the production code a tiny bit, but using @VisibleForTesting in the latest version of Android Studio will give error if you use the member variable elsewhere. 要求您稍微更改生产代码,但如果您在其他地方使用成员变量,则在最新版本的Android Studio中使用@VisibleForTesting会出错。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM