Compose UI test + coroutines issues

I’m setting up an androidTest to run a click test to make sure the login flow is always working.

The test is done in terms of opening the screen, press login button, enter email/password and then the login for real button is called. But here is the issue. My test don’t wait for the viewModel to call .login(email, pass).collect() stuff. So my sessionManager that should have a session now always respond with false.
So I tried setting up a test rule for a “MainDispatcherRule” to make sure all coroutines used that dispatcher. But doing that just make the Compose code fall in to a infinity never being idle state.

I’ve tried to use the advanceUntilIdle and different delays before the assertTrue in the end. I’ve not tested to mock the viewModel. Since all my API calls are fake with the MockApi anyway.

Any help would be awesome!

This is the test

@OptIn(ExperimentalCoroutinesApi::class)
@RunWith(AndroidJUnit4::class)
class LoginUserTest : BaseTest(hasValidSession = false) {
    private val sessionManager: SessionManager by inject()

    /*@get:Rule
    val mainRule = MainDispatcherRule(UnconfinedTestDispatcher())
     */

    @Test
    fun start_app_login_user() = runTest {
        // Start screen login button
        composeTestRule.onNodeWithTag(TestTagID.StartLoginButton).performClick()

        // Login email screen
        composeTestRule.onNodeWithTag(TestTagID.EmailLoginEmail).apply {
            performTextInput("[email protected]")
            performImeAction()
        }

        composeTestRule.onNodeWithTag(TestTagID.EmailLoginPassword).apply {
            performTextInput("Hello123!!")
            performImeAction()
        }

        // Make sure button is enabled and press it (viewModel.login will be called)
        composeTestRule.onNodeWithTag(TestTagID.EmailLoginButton).apply {
            assertIsEnabled()
            performClick()
        }

        assertTrue { sessionManager.hasValidSession() }
    }
}

As it is above the test will run, clicks will work etc but the last check for assertTrue { sessionManager.hasValidSession() } returns false.

But If I enable the MainDispatcherRule then the Compose UI part never get’s idle and a timeout happens.

The BaseTest is pretty basic

open class BaseTest(private val hasValidSession: Boolean = true) : AutoCloseKoinTest() {

    private var scenario: ActivityScenario<MainActivity>? = null

    @get:Rule
    val composeTestRule = createAndroidComposeRule<ComposeTestActivity>()

    @get:Rule
    val mockProvider = MockProviderRule.create { mockkClass(it, relaxed = true) }

    private val sessionManager: SessionManager by inject()
    private val loadableRepository: LoadableRepo by inject()
    private val idlingResources: Array<EspressoIdlingResource> by lazy { arrayOf(loadableRepository) }
    private val composeIdlingResources: List<ComposeIdlingResource> by lazy {
        idlingResources.map {
            object : ComposeIdlingResource {
                override val isIdleNow: Boolean get() = it.isIdleNow
            }
        }
    }

    @Before
    open fun setUp() {
        declare<MainApi> { MockApi(get()) } // Make sure all api calls in test is running mock endpoints

        sessionManager.setAuthToken(AuthResponse.mock().takeIf { hasValidSession })
        Locale.setDefault(Locale.US)

        IdlingRegistry.getInstance().register(*idlingResources)
        composeIdlingResources.forEach { composeTestRule.registerIdlingResource(it) }
        scenario = ActivityScenario.launch(MainActivity::class.java)
    }

    @After
    open fun tearDown() {
        scenario?.close()
        IdlingRegistry.getInstance().unregister(*idlingResources)
        composeIdlingResources.forEach { composeTestRule.unregisterIdlingResource(it) }
    }
} 

The build.gradle.kts test options..

testOptions {
        animationsDisabled = true
        unitTests.apply {
            isIncludeAndroidResources = true
            isReturnDefaultValues = true
        }
    }

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật