本文介绍了如何让 Espresso 等到数据绑定更新了带有数据模型的视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在我的 Android 应用程序上运行 Espresso 测试.测试是片状的.它可以可靠地断言数据模型已更新.我的问题是 ViewMatchers 无法匹配视图中的相同值,因为 ViewDataBinding 尚未更新视图.(至少在测试运行的大部分时间.)

当 ViewDataBinding 在视图上没有挂起的更改时,是否存在 IdlingResource 变为空闲的情况?

我的解决方法是结合调用 executePendingBindings() 和一个小的 Thread.sleep(...)

解决方案

Espresso 在执行视图检查之前会执行 waitForIdle.waitForIdle 考虑 IdlingRegistry 并等到每个 IdlingResource 空闲.

LoopingIdlingResource 默认在 Espresso 中使用.它一直等到looper队列中没有消息,这意味着它处于空闲状态.

然而,DataBinding 使用不同的方法来安排更新,它使用 Choreographer.postFrameCallback.因此更新不会发布到 Looper 队列中,Espresso 也不会等待它们.

在这种情况下,您应该注册自己的IdlingResource.您可以在 googlesamples/android-architecture-components 中找到如何实现自定义的好示例/app/src/androidTest/java/com/android/example/github/util/DataBindingIdlingResource.kt" rel="noreferrer">DataBindingIdlingResource 和 将在执行测试之前设置空闲资源.

所以你必须复制这些类 DataBindingIdlingResourceRule数据测试BindingIdling>/p>

并将以下规则添加到您的测试类中:

@Rule@JvmFieldval 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 等到数据绑定更新了带有数据模型的视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 20:14