android - 根据条件禁用 Espresso IdlingResource
问题描述
我有以下浓缩咖啡测试:
@Test
fun testSearchItemFound() {
onView(withId(R.id.action_search)).perform(click())
onView(isAssignableFrom(EditText::class.java))
.perform(typeText("Harry Potter"),
pressImeActionButton())
// Check error message view is not displayed
onView(withId(R.id.error_msg)).check(matches(not<View>(isDisplayed())))
// Check the item we are looking for is in the search result list.
onData(allOf(`is`<Any>(instanceOf<Any>(String::class.java)),
withItemContent("Harry Potter")))
.check(matches(isDisplayed()))
}
第一行代码:
onView(withId(R.id.action_search)).perform(click())
将在 EspressoIdlingResource 空闲时正确执行。
但是最后一行代码:
onData(allOf(`is`<Any>(instanceOf<Any>(String::class.java)),
withItemContent("Harry Potter")))
.check(matches(isDisplayed()))
当我们在 MainFragment 中有以下代码时将执行:
// The network request might be handled in a different thread so make sure Espresso knows
// that the app is busy until the response is handled.
EspressoIdlingResource.increment() // App is busy until further notice
model.movies.observe(this@MainFragment, Observer<PagedList<Movie>> {
// This callback may be called twice, once for the cache and once for loading
// the data from the server API, so we check before decrementing, otherwise
// it throws "Counter has been corrupted!" exception.
if (!EspressoIdlingResource.getIdlingResource().isIdleNow) {
EspressoIdlingResource.decrement() // Set app as idle.
}
adapter.submitList(it)
})
如果我禁用:
EspressoIdlingResource.increment() // App is busy until further notice
和 :
if (!EspressoIdlingResource.getIdlingResource().isIdleNow) {
EspressoIdlingResource.decrement() // Set app as idle.
}
第一行正常工作:
onView(withId(R.id.action_search)).perform(click())
有没有办法处理这种特殊情况?
解决方案
推荐阅读
- json - 动态解码 JSON 单个对象或对象数组
- reactjs - ReactNative Pull To Refresh for iOS platform
- html - 如何获取不是文档子级的div
- python-3.x - Tensorflow - 来自神经网络的预测通常适用于分类中的全 0
- quartz-scheduler - 如何调度在不同调度程序实例中创建的作业
- node.js - 使用 mongoose 设置数据库和集合
- regex - 如果修改的文件来自给定目录,则解析 git 状态日志并退出
- azure - 关于在文件存储 Azure 上为静态 Web 设置自定义域的问题
- c# - 你如何将模型/控制器链接到身份?ASP.NET 核心
- python - 如何将 VS Code 连接到 Google Cloud Platform 上的 GPU 实例?