Android Espresso IdlingResources 和 Fragment/Activity Transitions [英] Android Espresso IdlingResources and Fragment/Activity Transitions

查看:35
本文介绍了Android Espresso IdlingResources 和 Fragment/Activity Transitions的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个托管片段 F1 的活动.单击按钮后,F1 被另一个片段 F2 替换.当按下后退按钮时,应用程序通过退出转换动画从 F2 返回到 F1.

I have an activity that hosts a fragment F1. Upon a button click, F1 is replaced by another fragment F2. When the back button is pressed, the app returns from F2 to F1 via an exit transition animation.

我的 Espresso 测试用例大致如下所示:

My Espresso test case looks roughly like the following:

@Test
public void pressingBackRestorePreviousFragment() {
   // we are in F1 and about to switch to F2
   onView(withId(R.id.the_button)).perform(click());

   // we are now in F2, pressing back should "return" to F1
   Espresso.pressBack();

   onView(withText("A specific text in F1")).check(matches(isDisplayed());
}

当测试用例在逐步调试模式下运行时,上述测试通过.但是在正常运行模式下,它失败了.只有当我在 onView(withText(__)) 之前插入 Thread.sleep(___) 时,测试才会通过.

When the test case runs in step-by-step debug mode, the above test passes. But in normal run mode, it fails. Only when I inserted Thread.sleep(___) before onView(withText(__)) the test would pass.

我相信更好的技术是用 Espresso IdlingResource 替换 Thread.sleep(),但我不确定如何将它与视图动画线程合并.理想情况下,我想将上述测试用例重写为

I believe a better technique is to replace Thread.sleep() with Espresso IdlingResource, but I am not sure how to incorporate it with the view animation thread. Ideally, I'd like to rewrite the above test case to

@Test
public void pressingBackRestorePreviousFragment() {
   // we are in F1 and about to switch to F2
   onView(withId(R.id.the_button)).perform(click());

   // we are now in F2, pressing back should "return" to F1
   Espresso.pressBack();

   onView(isRoot()).perform(waitForAnimCompletion());

   onView(withText("A specific text in F1")).check(matches(isDisplayed());
}

推荐答案

Espresso 在执行操作之前会等待主线程空闲.FragmentActivity 转换发生在主线程上,因此您不需要实现任何 IdlingResource .这里发生的事情是,Espresso 的主线程和测试线程的同步导致了过渡动画的不稳定.正如Espresso 设置说明中所述,您应该在用于测试的设备上禁用系统动画.

Espresso will wait for the main thread to become idle before performing operations. Fragment and Activity transitions happen on the main thread, so you do not need to implement any IdlingResources. What's happening here is that the transition animation is causing flakiness with Espresso's synchronization of the main thread and testing thread. As mentioned in the Espresso setup instructions, you should disable system animations on the device used for testing.

一般来说,Thread.sleep 语句对于测试来说是不好的做法,会导致测试不稳定和缓慢.Espresso 正是为了解决 Android 上的这个问题而设计的.查看 Valera Zakharov 在 GTAC 2013 上介绍 Espresso 时对问题的解释.

In general, Thread.sleep statements are bad practice for testing, and will result in unstable and slow tests. Espresso was designed precisely to address this problem on Android. Checkout Valera Zakharov's explanation of the problem when introducing Espresso at GTAC 2013.

这篇关于Android Espresso IdlingResources 和 Fragment/Activity Transitions的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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