在初始化数据库时尝试测试对话框的存在。初始化过程由LocalBroadcasts标记。
测试代码。
private var listActivity: ListActivity? = null
@get:Rule
val activityTestRule = ActivityTestRule<ListActivity>(ListActivity::class.java)
@Before
fun setUp() {
listActivity = activityTestRule.activity
}
@Test
fun testCheckActivityNotNull() {
Assert.assertNotNull(listActivity)
}
/**
* Test that we show a progress dialog while database init/migration is in process.
* Test that we remove that on db init/migration success
*/
@UiThreadTest
@Test
fun testCheckProgressBarShownOnBroadcast() {
Assert.assertNotNull(listActivity)
var dbIntent = Intent()
dbIntent.putExtra(DB_INIT_STATUS, DB_INIT_START)
dbIntent.action = DB_INIT_STATUS
LocalBroadcastManager.getInstance(ApplicationProvider.getApplicationContext()).sendBroadcast(dbIntent)
Espresso.onView(withText(R.string.please_wait)).check(matches(isDisplayed()))
Espresso.onView(withText(R.string.migrating_data)).check(matches(isDisplayed()))
Thread.sleep(5000) // keeping a waiting time to check the view
dbIntent = Intent()
dbIntent.putExtra(DB_INIT_STATUS, DB_INIT_END)
dbIntent.action = DB_INIT_STATUS
LocalBroadcastManager.getInstance(ApplicationProvider.getApplicationContext()).sendBroadcast(dbIntent)
Espresso.onView(withText(R.string.please_wait)).check(doesNotExist())
Espresso.onView(withText(R.string.migrating_data)).check(doesNotExist())
}
无法充分利用logcat。如果需要可以共享。
我最近从androidx.test.rule.ActivityTestRule
转到了ActivityInstrumentationTestCase2
,我的考试挂在了testCheckProgressBarShownOnBroadcast
上。当我使用ActivityInstrumentationTestCase2
错误keyDispatchTimeout
使我感到我没有正确进行测试。任何帮助表示赞赏。
编辑:
当我尝试与UI交互时,抛出'Instrumentation run failed due to 'keyDispatchingTimedOut''
。否则,将转到ANR。 onView
可能陷入僵局。