Loading...
Loading...
Use this skill to choose between host (Robolectric/JVM) and device (instrumentation) tests for Jetpack Compose, and to configure each correctly. Covers the `androidHostTest` (a.k.a. `src/test/`) vs `androidDeviceTest` (a.k.a. `src/androidTest/`) source set split, what each flavor can and cannot drive (RenderThread, screenshots, accessibility), the `@RunWith(AndroidJUnit4::class) @Config(minSdk = 23)` setup for Robolectric, and why `Thread.sleep` is forbidden everywhere except screenshot tests waiting on the RenderThread. Use when the user reports "tests pass locally but fail on CI", asks "Robolectric vs instrumentation", mentions screenshot tests, ripple animations, accessibility checks throwing on Robolectric, or "should this test live in test/ or androidTest/".
npx skill4agent add skydoves/android-testing-skills setting-up-host-vs-device-testsrunComposeUiTest { setContent { … } }LooperChoreographerThread.sleepBuild.FINGERPRINT == "robolectric"enableAccessibilityChecks(...)pressInteractionThread.sleep(1000)androidDeviceTestandroidHostTestandroidx./configuring-test-dependencies/SKILL.mdcreateComposeRule()runComposeUiTest { }./choosing-test-rule-vs-runtest/SKILL.md../../synchronization/synchronizing-with-idle/SKILL.md../../synchronization/testing-animations-deterministically/SKILL.mdandroidx.compose.ui:ui-testui-test-junit4ui-test-manifest./configuring-test-dependencies/SKILL.mdorg.robolectric:robolectrictestImplementationtestOptions { unitTests.isIncludeAndroidResources = true }androidx.test.runner.AndroidJUnitRunnertestInstrumentationRunnerMainTestClock| Capability | Host (Robolectric, | Device (instrumentation, |
|---|---|---|
| Recomposition + state changes | Works | Works |
| Layout + measurement | Works | Works |
Touch input via | Works (synthetic events) | Works (real input pipeline) |
| No (RenderThread not driven) | Works |
Screenshot capture ( | No (no real surface) | Works |
| Inconclusive — | Works ( |
| Gesture detectors needing real-time clock changes (double-tap, long-press timing) | Works only with manual | Works automatically |
Activity lifecycle (real | Approximated | Real |
| Min SDK | 23 ( | The module's |
| Speed (rough order) | Seconds | Tens of seconds + emulator boot |
| CI footprint | JVM only | Emulator service or Firebase Test Lab |
Modifier.indicationsrc/
├── androidHostTest/ # = test/ — Robolectric on JVM
│ └── kotlin/.../FooTest.kt
├── androidDeviceTest/ # = androidTest/ — instrumentation
│ └── kotlin/.../FooScreenshotTest.kt
└── androidCommonTest/ # helpers shared by both
└── kotlin/.../FooTestHelpers.ktsrc/test/src/androidTest/runComposeUiTest { setContent { … } }compose/ui/ui-test/src/androidHostTest/kotlin/androidx/compose/ui/test/RobolectricComposeTest.kt@RunWith(AndroidJUnit4::class)
@Config(minSdk = 23)
@OptIn(ExperimentalTestApi::class)
class MyHostTest {
@Before
fun setup() {
// capture the master timeout so we can restore it afterwards
masterTimeout = IdlingPolicies.getMasterIdlingPolicy()
}
@After
fun tearDown() {
masterTimeout?.let {
IdlingPolicies.setMasterPolicyTimeout(it.idleTimeout, it.idleTimeoutUnit)
}
}
@Test
fun stateChange() = runComposeUiTest {
setContent { ClickCounter() }
onNodeWithText("Click me").performClick()
onNodeWithText("Click count", substring = true).assertTextEquals("Click count: 1")
}
private var masterTimeout: IdlingPolicy? = null
}AndroidJUnit4::classAndroidJUnit4ClassRunner@RunWith(RobolectricTestRunner::class)@RunWith(AndroidJUnit4::class)
class MyDeviceTest {
@get:Rule val rule = createComposeRule() // or v2 import — see ./choosing-test-rule-vs-runtest/
@Test
fun stateChange() {
rule.setContent { ClickCounter() }
rule.onNodeWithText("Click me").performClick()
rule.onNodeWithText("Click count", substring = true).assertTextEquals("Click count: 1")
}
}mainClock.advanceTimeBy(...)RobolectricComposeTest.ktmainClock.advanceTimeBy(...)animateFloatAsState../../synchronization/testing-animations-deterministically/SKILL.mdThread.sleepIdlingResourceModifier.indicationmainClock.advanceTimeBywaitForIdlecompose/material3/material3/src/androidDeviceTest/.../ToggleButtonScreenshotTest.kt:115-123rule.mainClock.autoAdvance = false
rule.onNode(isToggleable()).performTouchInput { down(center) }
rule.mainClock.advanceTimeByFrame()
rule.waitForIdle() // wait for measure
rule.mainClock.advanceTimeBy(milliseconds = 200)
// Ripples are drawn on the RenderThread, not the main (UI) thread, so we can't wait for
// synchronization. Instead just wait until after the ripples are finished animating.
Thread.sleep(300)
assertAgainstGolden("toggleButton_lightTheme_defaultToPressed")Thread.sleepMainTestClockmainClock.advanceTimeBy(durationMs)rule.waitUntil(timeoutMillis = …) { … }Thread.sleepThread.sleep(N)mainClock.advanceTimeBy(N)rule.waitUntil { … }captureToImage()SurfaceModifier.indicationenableAccessibilityChecks(...)@RequiresApi(34)Build.FINGERPRINT.lowercase() == "robolectric"Log.wStateRestorationTester.emulateSavedInstanceStateRestore()assertTextEqualsassertIsOnmainClock.advanceTimeBy(durationMs)// WRONG — placed in src/test/ (Robolectric)
@RunWith(AndroidJUnit4::class)
@Config(minSdk = 23)
class ButtonScreenshotTest {
@Test
fun pressed() = runComposeUiTest {
setContent { Button(onClick = {}) { Text("OK") } }
onNode(hasText("OK")).performTouchInput { down(center) }
onNode(hasText("OK")).captureToImage().assertAgainstGolden("pressed")
}
}
// WRONG because: Robolectric has no real Surface, so captureToImage() returns black/empty
// pixels and the ripple from Modifier.indication never draws (RenderThread is not driven).
// The test will produce a false-positive pass or a meaningless golden file.// RIGHT — placed in src/androidTest/ (device)
@RunWith(AndroidJUnit4::class)
class ButtonScreenshotTest {
@get:Rule val rule = createComposeRule()
@Test
fun pressed() {
rule.setContent { Button(onClick = {}) { Text("OK") } }
rule.mainClock.autoAdvance = false
rule.onNode(hasText("OK")).performTouchInput { down(center) }
rule.mainClock.advanceTimeByFrame()
rule.waitForIdle()
rule.mainClock.advanceTimeBy(milliseconds = 200)
Thread.sleep(300) // legitimate — RenderThread for ripples
rule.onNode(hasText("OK")).captureToImage().assertAgainstGolden("pressed")
}
}Thread.sleep// WRONG — host test
@Test
fun fadeIn() = runComposeUiTest {
val target = mutableStateOf(0f)
setContent { Box(Modifier.alpha(animateFloatAsState(target.value).value)) }
target.value = 1f
Thread.sleep(500) // smell
onNode(isRoot()).captureToImage() // also broken on host
}
// WRONG because: Thread.sleep desyncs from MainTestClock. The test clock has not advanced,
// the recomposer has not run withFrameNanos, and the animation made zero observable progress.// RIGHT — host test (recomposition only — no pixel capture)
@Test
fun fadeIn() = runComposeUiTest {
mainClock.autoAdvance = false
val target = mutableStateOf(0f)
setContent { Box(Modifier.alpha(animateFloatAsState(target.value).value).testTag("box")) }
runOnUiThread { target.value = 1f }
mainClock.advanceTimeByFrame() // kick off
mainClock.advanceTimeBy(durationMillis = 500) // step forward by test clock
onNodeWithTag("box").assertIsDisplayed()
}captureToImage()// WRONG — host
@Test
fun submitIsLabelled() = runComposeUiTest {
enableAccessibilityChecks() // logs warning, runs no checks
setContent { Submit() }
onNodeWithTag("submit").performClick()
}
// WRONG because: both ui-test-accessibility and ui-test-junit4-accessibility detect
// Build.FINGERPRINT.lowercase() == "robolectric", emit a Log.w, AND still install the
// validator — but Robolectric does not faithfully drive accessibility services, so the
// result is inconclusive (b/332778271). Run accessibility checks on a real device.// RIGHT — device
@RunWith(AndroidJUnit4::class)
class SubmitA11yTest {
@get:Rule val rule = createAndroidComposeRule<MainActivity>()
@Test fun submitIsLabelled() {
rule.enableAccessibilityChecks()
rule.setContent { Submit() }
rule.onNodeWithTag("submit").performClick() // accessibility checks run automatically
}
}runComposeUiTest { ... }androidCommonTest/androidHostTest/@RunWith(AndroidJUnit4::class) @Config(minSdk = 23)androidDeviceTest/@ConfigcaptureToImageModifier.indicationenableAccessibilityChecks(...)Build.FINGERPRINT.lowercase() == "robolectric"Thread.sleepThread.sleepThread.sleepmainClock.advanceTimeBy(durationMs)rule.waitUntil(timeoutMillis) { ... }../../synchronization/synchronizing-with-idle/SKILL.md@Config(minSdk = 23)internal const val RobolectricMinSdk = 23mainClock.advanceTimeBy(...)testOptions { unitTests.isIncludeAndroidResources = true }android { }androidCommonTest/./gradlew :<module>:testDebugUnitTest./gradlew :<module>:connectedDebugAndroidTestThread.sleepgrep -r "Thread.sleep" src/test src/androidHostTestcaptureToImageenableAccessibilityChecksModifier.indicationThread.sleep@RunWith(AndroidJUnit4::class)@RunWith(RobolectricTestRunner::class)@Config(minSdk = 23)mainClock.autoAdvance = falsemainClock.advanceTimeBy(durationMs)compose/ui/ui-test/src/androidHostTest/kotlin/androidx/compose/ui/test/RobolectricComposeTest.kt@RunWith(AndroidJUnit4::class) @Config(minSdk = RobolectricMinSdk)IdlingPoliciescompose/ui/ui-test/src/androidHostTest/kotlin/androidx/compose/ui/test/Constants.ktinternal const val RobolectricMinSdk = 23compose/material3/material3/src/androidDeviceTest/kotlin/androidx/compose/material3/ToggleButtonScreenshotTest.kt:115-123Thread.sleep(300)assertAgainstGoldencompose/ui/ui-test/src/androidMain/kotlin/androidx/compose/ui/test/RobolectricIdlingStrategy.android.ktcompose/ui/ui-test/src/androidMain/kotlin/androidx/compose/ui/test/ComposeIdlingResource.android.ktThread.sleep