如何让Espresso等到数据绑定已使用数据模型更新了View之后? [英] How do I make Espresso wait until Data Binding has updated the View with the data-model?

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

问题描述

我正在我的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漂亮的示例中找到如何实现自定义 解决方案

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆