Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
629 views
in Technique[技术] by (71.8m points)

android - How do I make Espresso wait until Data Binding has updated the View with the data-model?

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(...)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...