Espresso不适用于NineOldAndroids动画?

时间:2014-02-11 08:50:08

标签: android android-testing android-espresso

我正在尝试测试我的活动(HomeActivity),它基于使用Espresso的NineOldAndroids lib重复动画。我按照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.

2 个答案:

答案 0 :(得分: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;
    }

答案 1 :(得分: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