浓缩咖啡冻结视图与循环动画

时间:2015-04-09 22:59:57

标签: android android-espresso

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

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

当Espresso打开我的活动时,它可以执行一些操作但很快冻结。我想Espresso正在等待UI线程变为空闲,在这种情况下永远不会发生。

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

编辑: 我还没有创建一个示例项目来尝试重新创建问题,但在此期间还有一些进一步的细节:

1)我正在使用杰克沃顿的ActivityRule让我的活动自动启动(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$1.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$2.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$3.run(ParentRunner.java:231)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
at org.junit.runners.ParentRunner$2.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$3.run(ParentRunner.java:231)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
at org.junit.runners.ParentRunner$2.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.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: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.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: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)

1 个答案:

答案 0 :(得分:15)

是的,您在测试时遇到的问题是动画造成的。

我能想到的唯一解决方案就是关闭无限动画。

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

一些背景知识:

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

当您查看QueueInterrogator时,当消息队列包含计划在16ms内处理的事件时,您可以看到它determineQueueState()返回TASK_DUE_SOONUiController仅在满足所有空闲条件时继续执行,如果QueueInterrogator仅在情况下,如果消息队列为空或下一条消息计划在16ms或更晚的时间内处理。< / p>

动画将使它们正在转换的View无效,这将触发Choreographer遍历的新视图层次结构。此触发器消息从ViewRoot发送到Choreographer,这是保持UI线程消息队列不空闲的原因。