浓缩咖啡在视图中冻结并带有循环动画

Espresso freezing on view with looping animation

我有一个视图,其中一个元素在无限循环中使用以下动画:

<translate
    android:fromXDelta="0%"
    android:toXDelta="100%"
    android:duration="10000"
    android:repeatCount="-1"
    android:repeatMode="reverse"/>

当 Espresso 打开我的 activity 时,它能够执行一些操作但很快就死机了。我想 Espresso 正在等待 UI 线程空闲,但在这种情况下从未发生过。

我测试此视图以实现禁用动画的机制是唯一的方法吗?我可以有另一个 class 处理动画,这将被模拟以进行测试。或者构建时间条件。

编辑: 我还没有创建一个示例项目来尝试重现问题,但同时这里有一些进一步的细节:

1) 我正在使用 Jake Wharton 的 ActivityRule 让我的 activity 自动启动 (https://gist.github.com/JakeWharton/1c2f2cadab2ddd97f9fb)。

2) 这是我的测试:

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

3) 这是完整的堆栈跟踪。注意 AppNotIdleException:

Running tests
Test running started
android.support.test.espresso.PerformException: Error performing 'single click' on view 'with id: com.myapp:id/btn_yes'.
at android.support.test.espresso.PerformException$Builder.build(PerformException.java:83)
at android.support.test.espresso.base.DefaultFailureHandler.getUserFriendlyError(DefaultFailureHandler.java:70)
at android.support.test.espresso.base.DefaultFailureHandler.handle(DefaultFailureHandler.java:53)
at android.support.test.espresso.ViewInteraction.runSynchronouslyOnUiThread(ViewInteraction.java:185)
at android.support.test.espresso.ViewInteraction.doPerform(ViewInteraction.java:115)
at android.support.test.espresso.ViewInteraction.perform(ViewInteraction.java:87)
at com.myapp.espresso.MyActivityTest.yesButtonTest(MyActivityTest.java:53)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at org.junit.runners.model.FrameworkMethod.runReflectiveCall(FrameworkMethod.java:45)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at com.myapp.espresso.ActivityRule.evaluate(ActivityRule.java:129)
at org.junit.rules.RunRules.evaluate(RunRules.java:18)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
at org.junit.runners.ParentRunner.run(ParentRunner.java:231)
at org.junit.runners.ParentRunner.schedule(ParentRunner.java:60)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
at org.junit.runners.ParentRunner.access[=12=]0(ParentRunner.java:50)
at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:222)
at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
at org.junit.runners.Suite.runChild(Suite.java:128)
at org.junit.runners.Suite.runChild(Suite.java:24)
at org.junit.runners.ParentRunner.run(ParentRunner.java:231)
at org.junit.runners.ParentRunner.schedule(ParentRunner.java:60)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
at org.junit.runners.ParentRunner.access[=12=]0(ParentRunner.java:50)
at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:222)
at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
at org.junit.runner.JUnitCore.run(JUnitCore.java:157)
at org.junit.runner.JUnitCore.run(JUnitCore.java:136)
at android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:270)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1837)
Caused by: android.support.test.espresso.AppNotIdleException: Looped for 3580 iterations over 60 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:471)
at android.support.test.espresso.base.UiControllerImpl.loopUntil(UiControllerImpl.java:402)
at android.support.test.espresso.base.UiControllerImpl.injectMotionEvent(UiControllerImpl.java:226)
at android.support.test.espresso.action.MotionEvents.sendDown(MotionEvents.java:78)
at android.support.test.espresso.action.Tap.sendSingleTap(Tap.java:133)
at android.support.test.espresso.action.Tap.access0(Tap.java:35)
at android.support.test.espresso.action.Tap.sendTap(Tap.java:40)
at android.support.test.espresso.action.GeneralClickAction.perform(GeneralClickAction.java:98)
at android.support.test.espresso.ViewInteraction.run(ViewInteraction.java:144)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:422)
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:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

4) 冻结时暂停执行测试时的堆栈跟踪:

话题 #1:

"Instr: android.support.test.runner.AndroidJUnitRunner@4549" prio=5 waiting
  java.lang.Thread.State: WAITING
     blocks Instr: android.support.test.runner.AndroidJUnitRunner@4549
      at java.lang.Object.wait(Object.java:-1)
      at java.lang.Thread.parkFor(Thread.java:1220)
      - locked <0x13a3> (a java.lang.Object)
      at sun.misc.Unsafe.park(Unsafe.java:299)
      at java.util.concurrent.locks.LockSupport.park(LockSupport.java:157)
      at java.util.concurrent.FutureTask.awaitDone(FutureTask.java:400)
      at java.util.concurrent.FutureTask.get(FutureTask.java:162)
      at android.support.test.espresso.ViewInteraction.runSynchronouslyOnUiThread(ViewInteraction.java:181)
      at android.support.test.espresso.ViewInteraction.doPerform(ViewInteraction.java:115)
      at android.support.test.espresso.ViewInteraction.perform(ViewInteraction.java:87)

话题#2:

"main@4663" prio=5 runnable
  java.lang.Thread.State: RUNNABLE
      at android.view.ThreadedRenderer.nSyncAndDrawFrame(ThreadedRenderer.java:-1)
      at android.view.ThreadedRenderer.draw(ThreadedRenderer.java:333)
      at android.view.ViewRootImpl.draw(ViewRootImpl.java:2492)
      at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:2337)
      at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1968)
      at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1054)
      at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5779)
      at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
      at android.view.Choreographer.doCallbacks(Choreographer.java:580)
      at android.view.Choreographer.doFrame(Choreographer.java:550)
      at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
      at android.os.Handler.handleCallback(Handler.java:739)
      at android.os.Handler.dispatchMessage(Handler.java:95)
      at android.support.test.espresso.base.UiControllerImpl.loopUntil(UiControllerImpl.java:461)
      at android.support.test.espresso.base.UiControllerImpl.loopUntil(UiControllerImpl.java:402)
      at android.support.test.espresso.base.UiControllerImpl.injectMotionEvent(UiControllerImpl.java:226)
      at android.support.test.espresso.action.MotionEvents.sendDown(MotionEvents.java:78)
      at android.support.test.espresso.action.Tap.sendSingleTap(Tap.java:133)
      at android.support.test.espresso.action.Tap.access0(Tap.java:35)
      at android.support.test.espresso.action.Tap.sendTap(Tap.java:40)
      at android.support.test.espresso.action.GeneralClickAction.perform(GeneralClickAction.java:98)
      at android.support.test.espresso.ViewInteraction.run(ViewInteraction.java:144)
      at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:422)
      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:135)
      at android.app.ActivityThread.main(ActivityThread.java:5221)
      at java.lang.reflect.Method.invoke(Method.java:-1)
      at java.lang.reflect.Method.invoke(Method.java:372)
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

是的,您在测试中遇到的问题是由动画引起的。

我能想到的唯一解决办法确实是关闭无限动画。

一般来说,在 运行 功能测试时关闭动画是个好主意,因为它们总是会引入一些不稳定因素。

一些背景知识:

从 Espresso 的角度来看,UI 线程永远不会空闲,因为消息队列总是包含一个事件,该事件的处理时间比用于确定线程是否空闲的阈值更近。

当你看QueueInterrogator, you can see that it's determineQueueState() returns TASK_DUE_SOON when message queue contains an event that is scheduled for handling in less than 16ms. UiController只有当所有空闲条件都满足时才会继续执行,在QueueInterrogator的情况下只有消息队列为空或下一条消息是这种情况计划在 16 毫秒或更晚的时间内处理。

动画将使它们正在转换的 View 无效,这将触发 Choreographer 的新视图层次结构遍历。这个从 ViewRootChoreographer 的触发消息是让你的 UI 线程消息队列不空闲的原因。