Trying to test the presence of a dialog while the database is being initialized. The process of initialization is marked by LocalBroadcasts.
Test code.
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())
}
Can't make out much of the logcat. Can share if needed.
I have recently shifted to androidx.test.rule.ActivityTestRule from ActivityInstrumentationTestCase2 and my test hungs on testCheckProgressBarShownOnBroadcast. Was working fine when I was on ActivityInstrumentationTestCase2
The error keyDispatchTimeout makes me feel I am not doing the test correctly.
Edit
'Instrumentation run failed due to 'keyDispatchingTimedOut'' is thrown when I try to interact with the UI. Otherwise, it goes to ANR. The onView is probably going into a deadlock.