如何让Espresso等到数据绑定已使用数据模型更新了View之后? [英] How do I make Espresso wait until Data Binding has updated the View with the data-model?
问题描述
我正在我的Android应用程序上运行Espresso测试.该测试是片状的.可以可靠地断言数据模型已更新.我的问题是ViewMatchers不能匹配View中的相同值,因为ViewDataBinding尚未更新View. (至少在大多数时间运行测试.)
当ViewDataBinding在视图上没有挂起的更改时,是否存在诸如IdlingResource之类的东西变为空闲状态?
我的解决方法是调用executePendingBindings()和一个小的Thread.sleep(...)
的组合 Espresso在执行视图检查之前会先执行waitForIdle
. waitForIdle
进入IdlingRegistry
的状态,并等待直到每个IdlingResource
都空闲.
LoopingIdlingResource
在Espresso中使用.它一直等待,直到循环器没有消息在队列中为止,这意味着它处于空闲状态.
但是DataBinding
使用不同的方法来计划更新,但是使用Choreographer.postFrameCallback
.因此,更新不会发布到循环器队列中,并且Espresso将不会等待它们.
在这种情况下,您应该注册自己的 IdlingResource
.您可以在googlesamples/android-architecture-components
漂亮的示例中找到如何实现自定义 DataBindingIdlingResourceRule
,它将在执行测试之前设置空闲资源.>
所以您必须复制这些类 DataBindingIdlingResource
放入您的测试中.
并将以下规则添加到测试类中:
@Rule
@JvmField
val dataBindingIdlingResourceRule = DataBindingIdlingResourceRule(activityRule)
I am running Espresso tests on my Android application. The test is flaky. It can reliable assert that the data-model is updated. My problem is that the ViewMatchers can't match the same value in the View because the ViewDataBinding has not yet updated the Views. (At least most of the time the test runs. )
Is there such a thing as an IdlingResource that becomes idle when the ViewDataBinding has no pending changes on the view?
My work-around is a combination of calling executePendingBindings() and a small Thread.sleep(...)
Espresso does waitForIdle
before executing view checks. waitForIdle
goes thought IdlingRegistry
and waits until every IdlingResource
is idle.
LoopingIdlingResource
is used in Espresso by default. It waits until looper doesn't have messages in queue, which means that it is idle.
However DataBinding
uses different approach to schedule an update, it uses Choreographer.postFrameCallback
. So updates are not posted into looper queue and Espresso will not wait for them.
In such cases you should register your own IdlingResource
. You can find in googlesamples/android-architecture-components
nice sample how to implement custom DataBindingIdlingResource
and DataBindingIdlingResourceRule
that will sets the idle resource before executing tests.
So you have to copy these classes DataBindingIdlingResourceRule
and DataBindingIdlingResource
into your tests.
And add the following rule into your test class:
@Rule
@JvmField
val dataBindingIdlingResourceRule = DataBindingIdlingResourceRule(activityRule)
这篇关于如何让Espresso等到数据绑定已使用数据模型更新了View之后?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!