【问题标题】:Is it possible to disable Toasts or wait until toast disappears while testing测试时是否可以禁用 Toast 或等到 toast 消失
【发布时间】:2015-11-07 00:14:13
【问题描述】:

我正在使用Espresso 测试一个应用程序。我有一个问题是可以等到当前没有吐司显示。我的应用程序中有很多不同的吐司,但是在测试时我遇到了问题,因为据我所知,焦点已经转移到吐司上,而且我得到了另一个视图层次结构,正如我在错误日志中看到的那样。
所以我的问题是可以隐藏所有内容(系统范围内具有 root 访问权限)或者只是等到屏幕上有任何 toast 或者是否可以手动将焦点设置到活动视图层次结构。
对于这个问题的任何帮助,我将不胜感激。
谢谢。

附:在我的应用程序中直接禁用 toast 不是一个选项,因为它会给应用程序带来一些额外的逻辑,这些逻辑仅在测试时需要。

【问题讨论】:

  • 你的 P.S 让我很开心,继续加油。至于问题,Espresso 是否提供任何类型的 waitForCondition 子句,以便您可以轻松地让 toast 消失?
  • Thread.sleep 仅适用于一个吐司,AFAIK LONG 时间是 3.5 秒,但是如果依次显示多个吐司并且需要更多时间怎么办,如果有是否可以将焦点重新集中到活动上
  • 如果您执行类似stackoverflow.com/questions/21417954/espresso-thread-sleep 的操作,您可能会有更长的超时时间,但不一定会一直持续。
  • 我会看看这个答案:stackoverflow.com/a/33387980/654026

标签: android unit-testing android-espresso android-toast


【解决方案1】:

我还没有找到任何完美的解决方案,但最好的办法是让 mToast 成员变量对测试可见,并使用它来取消 @After 中的任何活动 toast,如下所示:

显示 toast 时(被测 Activity 的生产代码):

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

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

测试代码(与被测代码在同一个包中):

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

要求您稍微更改生产代码,但如果您在其他地方使用成员变量,则在最新版本的 Android Studio 中使用 @VisibleForTesting 会出错。

【讨论】:

    【解决方案2】:

    您可以使用自定义空闲资源让 Espresso 等到所有 toast 消失。

    这里我使用CountingIdlingResource,这是一个管理计数器的空闲资源:当计数器从非零变为零时,它会通知转换回调。

    Here 是一个完整的例子;重点如下:

    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;
        }
    }
    

    如何展示吐司:

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

    如何设置/拆除测试:

    @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());
    }
    

    【讨论】:

      猜你喜欢
      • 2019-04-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-25
      • 1970-01-01
      • 2016-07-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多