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 thoughtIdlingRegistry
and waits until everyIdlingResource
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 usesChoreographer.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 ingooglesamples/android-architecture-components
nice sample how to implement customDataBindingIdlingResource
andDataBindingIdlingResourceRule
that will sets the idle resource before executing tests.So you have to copy these classes
DataBindingIdlingResourceRule
andDataBindingIdlingResource
into your tests.And add the following rule into your test class:
Edit: This is an old answer. Please use Roshak's
The bug report mentioned using reflection to change
ViewDataBinding.USE_CHOREOGRAPHER
tofalse
for the tests, so here is the solution I came up with:Then, just define an
ActivityTestRule
for the Activity under test, and override itsbeforeActivityLaunched()
. It is necessary to do this before the activity is launched (as opposed to in a@Before
annotation) becauseViewDataBinding
will initialize aLooper
if it doesn't useCHOREOGRAPHER
.This way, you can get rid of that
Thread.sleep()