Compose-Espresso link to become idle timed out With JetpackCompose LazyColumn

Viewed 2195

I am trying to test my lazy column in JetpackCompose and I keep getting this error: [Compose-Espresso link] to become idle timed out

I tried using composeTestRule.waitforIdle() but it doesn't work. What am I missing here?

@HiltAndroidTest
class MainTest {

    @get:Rule(order = 1)
    var hiltTestRule = HiltAndroidRule(this)

    @get:Rule(order = 2)
    var composeTestRule = createAndroidComposeRule<MainActivity>()

    private val context = InstrumentationRegistry.getInstrumentation().context

    @Before
    fun setup() {
        hiltTestRule.inject()
        composeTestRule.setContent {
            HomePage(
                context = context,
            viewModel = composeTestRule.activity.viewModels<MarvelViewModel>().value,
            onClick = {}
            )
        }
        composeTestRule.onRoot().printToLog("currentLabelExists")

    }


    @Test
    fun isResultDisplayedOnLazyColumn() {
        composeTestRule.waitForIdle()
        composeTestRule.onNode(hasImeAction(ImeAction.Done)).performTextInput("iron man")
        composeTestRule.onNode(hasImeAction(ImeAction.Done)).performImeAction()

        composeTestRule.onNodeWithTag(TAG_LAZY_COLUMN, useUnmergedTree = true).assertIsDisplayed()
3 Answers

I've got this issue too. The root cause was having my composeview GONE. It seems ComposeTestRule IdlingResource don't like then you call setContent on a Gone ComposeView

After several hours of trials and experimentation, l found a way to fix this issue in a recent project. I would advise to try the following:

Update compose version to 1.2.0-Alpha06 as Skav mentioned. For me that implies bumping my kotlin version to 1.7.0 which my team is not ready for.

So I did some digging around the IdlingRegistry and found that unregistering Espresso-Compose Idling resource at the right spot in the test fixed the issue and my UI test passed afterwards.

IdlingRegistry.getInstance().resources.forEach {
   Timber.e("resource ${it.name}    idleNow: ${it.isIdleNow}")
   if( it.name == "Compose-Espresso link" ) {
       IdlingRegistry.getInstance().unregister(it)
   }
}

It is important to note that unregistering Espresso-Compose too early can make your compose ui to fail to display. In my case, l ran this code after the UI is ready before testing for specific UI functionalities.

Building on top of the answer from @leeCoder, I've discovered that upgrading to Compose 1.2.0 (alpha no longer necessary) fixed my problem and there was no need to unregister anything (which was the part that made me the most nervous about his answer).

Related