带有循环动画的 Espresso 冻结视图
Posted
技术标签:
【中文标题】带有循环动画的 Espresso 冻结视图【英文标题】:Espresso freezing on view with looping animation 【发布时间】:2015-04-09 22:59:56 【问题描述】:我有一个视图,其中一个元素在无限循环中使用以下动画:
<translate
android:fromXDelta="0%"
android:toXDelta="100%"
android:duration="10000"
android:repeatCount="-1"
android:repeatMode="reverse"/>
当 Espresso 打开我的活动时,它能够执行一些操作,但很快就会冻结。我想 Espresso 正在等待 UI 线程空闲,这在这种情况下永远不会发生。
是我测试此视图以实现禁用动画机制的唯一方法吗?我可以让另一个类处理动画,这将被模拟以进行测试。或者构建时间条件。
编辑: 我还没有创建一个示例项目来尝试重新创建问题,但同时这里有一些进一步的细节:
1) 我正在使用 Jake Wharton 的 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)
【问题讨论】:
我的理解是MessageQueue
如果现在没有消息准备好,则认为自己处于空闲状态,因此无限动画不应阻止这种情况。我会使用monitor
工具对您的应用程序进行分析,以查看主线程上还发生了什么。也许一些糟糕的代码会毫无延迟地重复调度自己。
UI 线程被认为是空闲的,如果它的消息队列是空的,或者它的 getWhen() - now() 小于 17ms。所以动画会阻塞ui线程。你所描述的仍然不符合通常的症状。如果 UI 线程在 60 秒内没有空闲,则 Espresso 将通过异常通知测试失败。 @mieroy:你等了这么久吗?如果您在调试模式下运行测试并在调试器中中断执行,您获得的测试执行线程的堆栈跟踪是什么?应用程序的主线程是什么?
@haffax,我在我的问题中添加了 3 个堆栈跟踪:执行期间的测试运行程序和主线程(第 4 点)以及 60 秒后的错误(第 3 点)。我必须承认我不太了解动画事件是如何安排的,以及空闲检测机制是如何工作的。
【参考方案1】:
是的,您在测试中遇到的问题是动画造成的。
我能想到的唯一解决方案确实是关闭无限动画。
一般来说,在运行功能测试时关闭动画是个好主意,因为它们总是会引入一些片状。
一点背景:
从 Espresso 的角度来看,UI 线程永远不会空闲,因为消息队列中总是包含一个事件,该事件被安排在比用于确定线程是否空闲的阈值更近的时间处理。
当您查看QueueInterrogator
时,您可以看到它是determineQueueState()
返回TASK_DUE_SOON
,当消息队列包含计划在不到16 毫秒内处理的事件时。 UiController
将仅在满足所有空闲条件时继续执行,在 QueueInterrogator
的情况下,仅当消息队列为空或下一条消息计划在 16 毫秒或更长时间内处理时才会出现这种情况。
动画将使它们正在转换的View
无效,这将触发Choreographer
对新视图层次结构的遍历。而这条从ViewRoot
到Choreographer
的触发消息是保持你的UI线程消息队列不空闲的原因。
【讨论】:
感谢您的详细调查。我得出的结论是,这是真的 ;) 有趣的是,将我的动画从使用动画“框架”更改为 Animator 消除了这个问题。现在我遇到了相反的问题,我无法让 Espresso 等待其他一些(有限)动画完成,然后再进行评估:) 我最终仍然不得不实现一种方法来禁用 (Espresso) 测试的动画。谢谢!以上是关于带有循环动画的 Espresso 冻结视图的主要内容,如果未能解决你的问题,请参考以下文章
Espresso Android,点击 WebView 元素错误
Android Espresso 测试:找不到类:...空测试套件