java - Espresso 单元测试 : How to disable animation in code

标签 java android android-animation android-espresso

我已经完成了这些步骤

To avoid flakiness, we highly recommend that you turn off system animations on the virtual or physical device(s) used for testing.

On your device, under Settings->Developer options disable the following 3 settings:
Window animation scale
Transition animation scale
Animator duration scale

但是在我的代码中的某个地方我使用了这个

animation = new RotateAnimation(0f, 360f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
animation.setInterpolator(new LinearInterpolator());
animation.setRepeatCount(Animation.INFINITE);
animation.setDuration(5000);
imageScanLogo.startAnimation(animation);

在我的 Espresso 测试类(class)中,我单击该 View

onView(withId(R.id.imageScanLogo)).perform(click());

之后它就会卡在那个点上并且不再前进。当我从代码中注释掉动画时,它工作得很好。 这是堆栈跟踪,它说他无法单击该 View ,但实际上他单击了它并且在测试中没有取得任何进展。

android.support.test.espresso.PerformException: Error performing 'single click - At Coordinates: 539, 946 and precision: 16, 16' on view 'with id: com.android.x.y/imageScanLogo'.
at android.support.test.espresso.PerformException$Builder.build(PerformException.java:83)
at android.support.test.espresso.base.DefaultFailureHandler.getUserFriendlyError(DefaultFailureHandler.java:80)
at android.support.test.espresso.base.DefaultFailureHandler.handle(DefaultFailureHandler.java:56)
at android.support.test.espresso.ViewInteraction.runSynchronouslyOnUiThread(ViewInteraction.java:184)
at android.support.test.espresso.ViewInteraction.doPerform(ViewInteraction.java:115)
at android.support.test.espresso.ViewInteraction.perform(ViewInteraction.java:87)
at com.android.x.y.BaseTests.scanTagsAndUpload(BaseTests.java:88)
at com.android.x.y.CleanScanTest.scan(CleanScanTest.java:61)
at java.lang.reflect.Method.invoke(Native Method)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at android.support.test.internal.statement.UiThreadStatement.evaluate(UiThreadStatement.java:55)
at android.support.test.rule.ActivityTestRule$ActivityStatement.evaluate(ActivityTestRule.java:270)
at org.junit.rules.RunRules.evaluate(RunRules.java:20)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runners.Suite.runChild(Suite.java:128)
at org.junit.runners.Suite.runChild(Suite.java:27)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
at android.support.test.internal.runner.TestExecutor.execute(TestExecutor.java:59)
at android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:262)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1879)
Caused by: android.support.test.espresso.AppNotIdleException: Looped for 4864 iterations over 30 SECONDS. The following Idle Conditions failed .
at android.support.test.espresso.IdlingPolicy.handleTimeout(IdlingPolicy.java:61)
at android.support.test.espresso.base.UiControllerImpl.loopUntil(UiControllerImpl.java:480)
at android.support.test.espresso.base.UiControllerImpl.loopUntil(UiControllerImpl.java:411)
at android.support.test.espresso.base.UiControllerImpl.injectMotionEvent(UiControllerImpl.java:229)
at android.support.test.espresso.action.MotionEvents.sendUp(MotionEvents.java:138)
at android.support.test.espresso.action.MotionEvents.sendUp(MotionEvents.java:118)
at android.support.test.espresso.action.Tap.sendSingleTap(Tap.java:135)
at android.support.test.espresso.action.Tap.access$100(Tap.java:35)
at android.support.test.espresso.action.Tap$1.sendTap(Tap.java:40)
at android.support.test.espresso.action.GeneralClickAction.perform(GeneralClickAction.java:98)
at android.support.test.espresso.ViewInteraction$1.run(ViewInteraction.java:144)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:423)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

我的超时策略是这样设置的

@Before
    public void resetTimeout() {
        IdlingPolicies.setMasterPolicyTimeout(30, TimeUnit.SECONDS);
        IdlingPolicies.setIdlingResourceTimeout(20, TimeUnit.SECONDS);
    }

我不想对代码中的每个动画进行注释,也不想开始构建一些标志来确定是否启用/禁用测试动画。

最佳答案

尝试使用Animator API 而是 the preferred animation method since Honeycomb (3.0) .

ObjectAnimator anim = ObjectAnimator.ofInt(imageScanLogo, "rotation", 0, 360);
anim.setRepeatCount(ValueAnimator.INFINITE);
animation.setInterpolator(new LinearInterpolator());
animation.setDuration(5000);
anim.start();

也许 Espresso 不会检查旧的动画 API,或者在开发人员设置中禁用动画不适用于旧的动画。

关于java - Espresso 单元测试 : How to disable animation in code,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39015350/

相关文章:

java - 如何将字符串转换为数学方程式?

java - 将通用列表从 XML 反序列化/解码为 Android 中的列表

java - 从失败中恢复的数据库错误 Qualtz

java - 如何强制日期在时区更改后不更改日期?

android - IonDrawable 无法转换为 android.graphics.drawable.BitmapDrawable

android - 滑动屏幕时带有移动物体的欢迎屏幕

Android - 通过点击另一个布局来动 Canvas 局

java - JRuby 调用了错误的方法

android - 使用支持库删除操作栏下方的阴影和线条

android - 恢复应用程序时如何禁用DialogFragment过渡动画?