gpt4 book ai didi

android - Espresso 在 View 中卡住并带有循环动画

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:59:20 25 4
gpt4 key购买 nike

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

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

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

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

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

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$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)

最佳答案

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

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

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

一些背景知识:

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

当您查看 QueueInterrogator 时,您可以看到它是 determineQueueState() 返回 TASK_DUE_SOON 当消息队列包含计划在 16 毫秒内处理的事件时。 UiController只有当所有空闲条件都满足时才会继续执行,对于 QueueInterrogator 来说,只有当消息队列为空或下一条消息计划在 16 毫秒或更晚的时间内处理时才会出现这种情况。

动画将使它们正在转换的 View 无效,这将触发 Choreographer 的新 View 层次结构遍历。这条从 ViewRootChoreographer 的触发消息使您的 UI 线程消息队列保持空闲状态。

关于android - Espresso 在 View 中卡住并带有循环动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29550508/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com