android - 等待动画完成并在单元测试中检查结果
问题描述
我在 kotlin 中有一个单元测试,它执行 longClick,然后检查视图的宽度是否发生了变化。宽度应该在半秒内改变。但无论我等多久,收到的宽度都不像预期的那样。
@Test
fun testLongClick() {
var initialWidth = 0
val listener = mock<TestInterface>(TestInterface::class.java)
view?.let {
it.findViewById<ImpulsesGroup>(R.id.test_default_impulse).getChildAt(2).setOnLongClickListener { child ->
(child as ImpulsesButton).onLongClick(child, it.findViewById<ImpulsesGroup>(R.id.test_default_impulse), listener)
}
initialWidth = it.findViewById<ImpulsesGroup>(R.id.test_default_impulse).width
assertThat("initial width", initialWidth, greaterThan(0))
runOnUiThread {
it.findViewById<ImpulsesGroup>(R.id.test_default_impulse).getChildAt(2).performLongClick()
}
await().atLeast(5, TimeUnit.SECONDS)
assertThat(view!!.findViewById<ImpulsesGroup>(R.id.test_default_impulse).width, Matchers.equalTo(3 * initialWidth))
}
}
似乎在动画完成之前检查了最终的assetThat()!
解决方案
private static class AsynchroniClass extends AsyncTask<Void, Void, Void> {
protected Void doInBackground(Void... voids) {
//what functions you need to do in the background
//in your case get Current width of a view
}
@Override
protected void onPostExecute(Void result) {
//now after you have the width call your function
}
}
然后不要忘记AsynchroniClass
像onCreateView
这样调用:
new AsynchroniClass().execute();
推荐阅读
- homebrew - youtube-dl 的自制软件和 wget 安装有什么区别?
- python - TypeError 返回模型条目
- r - 使用 TukeyHSD 的输出自动将重要字母添加到 ggplot 条形图
- python - 将 Excel 日期更改为数字范围
- deployment - 使用 gulp 部署到 gh 页面时遇到问题
- xml - 如何在android studio中制作这样的按钮
- google-sheets - 在谷歌表格中,当单元格的值为“保持”时,它会变成奇怪的绿色
- python - 有没有一种简单的方法来输出行数,包括每个组的缺失值,而不聚合它们?
- python - 由于值小,填充颜色未在 Plotly 中显示
- html - 为什么在 react-bootstrap 中映射图像显示错误(重复输出)