Search code examples
androidandroid-testingcomposableandroid-jetpack-compose-testing

Compose android testing fails: Idling resource timed out


I'm trying to write the test for my composes. So I have a test class put in AndroidTest just like this:

@HiltAndroidTest
@UninstallModules(AuthenticationModule::class, AppModule::class)
class AuthenticationScreenTest {

    @get:Rule(order = 0)
    val hiltRule = HiltAndroidRule(this)

    @get:Rule(order = 1)
    val composeRule = createAndroidComposeRule<MainActivity>()

    @Inject
    lateinit var setting: Setting

    @Before
    fun setup() {
        hiltRule.inject()
        composeRule.setContent {
            val navController = rememberNavController()
            RefectoryTheme {
                NavHost(
                    navController = navController,
                    startDestination = AuthenticationNavGraph.AuthenticationScreen.route
                ) {
                    composable(AuthenticationNavGraph.AuthenticationScreen.route) {
                        AuthenticationScreen(navController = navController, setting = setting)
                    }
                }
            }
        }
    }


    @Test
    fun checkLoadingButtonExpantion() {
        composeRule.onNodeWithTag(testTag = AUTHENTICATION_SCREEN_LOGIN_BUTTON)
        .assertIsDisplayed()
    }
}

but I keep getting the error:

androidx.compose.ui.test.junit4.android.ComposeNotIdleException: Idling resource timed out: 
possibly due to compose being busy.
IdlingResourceRegistry has the following idling resources registered:
- [busy] androidx.compose.ui.test.junit4.ComposeIdlingResource@a005df5
All registered idling resources: Compose-Espresso link

The android emulator is launched, test compiles successfully, but it seems it can't find the object. I also have added a test tag to the modifier of the object:

 LoadingButton(
                    buttonText = stringResource(id = R.string.login),
                    isExpanded = state.isLoginExpanded,
                    modifier = Modifier
                        .padding(MaterialTheme.spacing.medium)
                        .align(Alignment.CenterHorizontally)
                        .testTag(AUTHENTICATION_SCREEN_LOGIN_BUTTON)
                ) {
                    viewModel.onEvent(AuthenticationEvent.Login)
                }

But after 28 seconds, I got the error as mentioned above. What am I missing?


Solution

  • I just realized what the problem was. I am using Lottie on my screen, and the animation is infinity repeating. So I don't know why but it seems that it doesn't allow testing to go through. The tests ran without any problem when I commented the Lottie section.