Espresso不能与NineOldAndroids动画一起使用?

3
我正在尝试使用Espresso测试我的活动(HomeActivity),该活动基于NineOldAndroids库具有重复的动画。如此处所述,我关闭了系统动画,但这并没有帮助我,而是导致了一个错误(请见下文)。唯一有用的方法是手动删除动画。那么问题是,我是否需要手动关闭动画(使用BuildConfig似乎更方便),或者我做错了什么?预先感谢你的回答!
 java.lang.RuntimeException: Could not launch intent Intent {
 act=android.intent.action.MAIN flg=0x14000000
 cmp=com.package.en/com.package.ui.HomeActivity } within 45 seconds.
 Perhaps the main thread has not gone idle within a reasonable amount
 of time? There could be an animation or something constantly
 repainting the screen. Or the activity is doing network calls on
 creation? See the threaddump logs. For your reference the last time
 the event queue was idle before your activity launch request was
 1392052899081 and and now the last time the queue went idle was:
 1392052899081. If these numbers are the same your activity might be hogging the event 
 queue.

http://stackoverflow.com/questions/19607703/nineoldandroids-animation-not-working-on-api10 - Rajendra arora
2个回答

3

问题修复:

    @Before
    public void setUp() throws Exception {
        super.setUp();
        injectInstrumentation(InstrumentationRegistry.getInstrumentation());
        Intent intent = getIntent();
        if (intent == null) {
            intent = new Intent();
        }
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
        setActivityIntent(intent);
    }

    @SuppressLint("NewApi")
    @Override
    public T getActivity() {
        final T activity = super.getActivity();
        activity.overridePendingTransition(0, 0);
        return activity;
    }


考虑使用ActivityTestRule来避免使用过时的API并减少样板代码。 - Nick Korostelev

1
我不太了解9olddroids,但对于Espresso,您应该禁用动画以使测试更可靠,您可能已经这样做了。
因此,也许增加一些代码来禁用动画可以提高您的应用程序的“可测试性”。例如,您的活动可以具有一个禁用动画的方法,如下所示:
 public void disableAnimations() {
     this.mAnimationsEnabled = false;
 }

在每次动画之前,您需要检查它们是否启用。一旦您的测试开始,您需要禁用动画:

 public void setUp () {
    super.setUp();
     YourActivity activity = getActivity();
     activity.disableAnimations();
 }

 public void testXYZ() {
     // your test code
 }

我希望这个能够运行,因为9OldDroids将不再干扰Espresso。


感谢建议。我认为 GoogleInstrumentationTestRunner 在系统级别上处理它。 - Roman
1
实际上不是这样的,你必须在模拟器或者程序中禁用动画(使用原生动画)。我认为当使用第三方库时,这一点尤其重要。这对你有帮助吗? - Bolhoso
1
我采用了稍微不同的方法,但主要思路是相同的。问题在于,如果你有复杂的动画和监听器等,它需要大量的样板代码。我会等几天,如果没有人提出更好的解决方案,我就接受你的答案。 - Roman
太好了!之后,如果你编辑你的问题并发布你的解决方案,我会很感激,这样我就可以学到一点 :) - Bolhoso
1
我只是在调试模式下使用了BuildConfig.DEBUG来关闭动画。 - Roman
2
需要将代码添加到您的应用程序中,仅用于测试目的,这是非常糟糕的。这是一种不良现象。 - Christopher Perry

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接