ArunL
ArunL

Reputation: 358

How to disable transition for testing Android Jetpack Compose?

According to the doc [Testing your Compose layout][1], we can disable transition as following:

@get:Rule
val composeTestRule = AndroidComposeTestRule<MyActivity>(disableTransitions = true)

However, there's no such parameter available in AndroidComposeTestRule. Testing with Snackbars is failing using SnackbarHost since it has animations. Is there a way to disable transitions?

Upvotes: 1

Views: 1880

Answers (2)

Barry Irvine
Barry Irvine

Reputation: 13867

I notice that the rule is missing from the answers here. This is the implementation that I have:

class DisableAnimationsRule : TestRule {
override fun apply(base: Statement, description: Description): Statement {
    return object : Statement() {
        @Throws(Throwable::class)
        override fun evaluate() {
            // disable animations for test run
            changeAnimationStatus(enable = false)
            try {
                base.evaluate()
            } finally {
                // enable after test run
                changeAnimationStatus(enable = true)
            }
        }
    }
}

@Throws(IOException::class)
private fun changeAnimationStatus(enable: Boolean = true) {
    with(UiDevice.getInstance(InstrumentationRegistry.getInstrumentation())) {
        executeShellCommand("settings put global transition_animation_scale ${if (enable) 1 else 0}")
        executeShellCommand("settings put global window_animation_scale ${if (enable) 1 else 0}")
        executeShellCommand("settings put global animator_duration_scale ${if (enable) 1 else 0}")
    }
}

}

Upvotes: 0

ArunL
ArunL

Reputation: 358

According to review.googlesource.com, the parameter disableTransitions is not longer available in AndroidComposeTestRule. We can use DisableTransitionsTestRule to disable transitions.

Upvotes: 0

Related Questions