Espresso 不适用于 NineOldAndroids 动画?

Posted

技术标签:

【中文标题】Espresso 不适用于 NineOldAndroids 动画?【英文标题】:Espresso doesn't work with NineOldAndroids animation? 【发布时间】:2014-03-08 22:54:16 【问题描述】:

我正在尝试测试我的活动 (HomeActivity),该活动具有基于 NineOldandroids 库和 Espresso 的重复动画。我按照here 的描述关闭了系统动画,但它没有帮助,并且出现错误(见下文)。唯一有用的是手动删除动画。 所以问题是我是否需要手动关闭动画(使用 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.

【问题讨论】:

***.com/questions/19607703/… 【参考方案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 在系统级别处理它 实际上不,您必须禁用模拟器上的动画或以编程方式(使用股票动画)。现在我认为在使用第三方库时尤其如此。这对你有用吗? 我使用了一些不同的方法,但主要思想是一样的。问题是,如果你有复杂的监听器动画等,它需要大量的样板代码。我会等几天,如果没有人提出更好的解决方案,我接受你的回答。 我只是在调试模式下使用了BuildConfig.DEBUG来关闭动画。 必须将代码添加到仅用于测试目的的应用程序中,这太糟糕了。这是一种气味。【参考方案2】:

修复问题:

    @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 并减少样板代码。

以上是关于Espresso 不适用于 NineOldAndroids 动画?的主要内容,如果未能解决你的问题,请参考以下文章

用于 viewpager 列表视图的 Espresso 2.0 AmbiguousViewMatcherException

用于嵌套 RecyclerView 的 Espresso Matcher

Appium v​​s Espresso 用于自动化测试框架

使用 espresso 运行的 UIAutomator

Espresso 不使用 Gif 动画运行 Activity 测试

Espresso Test 2: Espresso_simple