Loading...
Loading...
Use this skill to structure a Jetpack Compose UI test class the way androidx itself writes them — `@MediumTest` + `@RunWith(AndroidJUnit4::class)`, a single `createComposeRule(StandardTestDispatcher())` rule, hoisted `mutableStateOf` declared above `setContent { }`, and the Test → Find → Assert → Act → Re-assert flow. Covers when to use `createAndroidComposeRule<MyActivity>()` for custom Activities, how to drive state from outside the composition via `runOnIdle { }`, why state must NOT be hoisted inside `setContent`, and why `@get:Rule createComposeRule()` and `runComposeUiTest { }` MUST NOT both appear in the same test. Use when the developer asks "how do I write a Compose test", "where do I declare state", "how do I name the test class", "MediumTest vs LargeTest", "rule.setContent inside @Before", or shows a test class that won't compile or doesn't drive state from outside.
npx skill4agent add skydoves/android-testing-skills structuring-a-compose-testsetContent { }setContent@Before@get:RulerunComposeUiTest { }androidx.compose.material3androidx.compose.foundation*Test.ktsetContentsetContent@BeforesetContent { }@get:Rule createComposeRule()runComposeUiTest { }runComposeUiTest../../setup/choosing-test-rule-vs-runtest/SKILL.mdcreateComposeRule../../setup/configuring-test-dependencies/SKILL.md../../setup/setting-up-host-vs-device-tests/SKILL.mdLazyColumnLazyRow../testing-lazy-lists/SKILL.mdandroidx.compose.ui:ui-test-junit4androidTestImplementationandroidx.compose.ui:ui-test-manifestdebugImplementation../../setup/configuring-test-dependencies/SKILL.mdandroidx.activity.ComponentActivityui-test-manifestandroidTestAndroidManifest.xmlsrc/androidTest/kotlin/...src/test/kotlin/...MyButtonTest.ktMyButton.kt<ProductionFile>TestSwitchTest.ktLazyListTest.kt@MediumTest@LargeTestperformTouchInput { longClick() }@RunWith(AndroidJUnit4::class)StandardTestDispatcherkotlinx.coroutines.test.runTestimport androidx.compose.ui.test.junit4.v2.createComposeRule
import kotlinx.coroutines.test.StandardTestDispatcher
@MediumTest
@RunWith(AndroidJUnit4::class)
class MyButtonTest {
@get:Rule val rule = createComposeRule(StandardTestDispatcher())
}FragmentActivityimport androidx.compose.ui.test.junit4.v2.createAndroidComposeRule
@get:Rule val rule = createAndroidComposeRule<FragmentActivity>(StandardTestDispatcher())compose/foundation/foundation/src/androidDeviceTest/kotlin/androidx/compose/foundation/textfield/TextFieldFocusCustomDialogTest.kt:60setContentsetContent { }@Test@BeforesetContent@Beforecompose/material3/material3/src/androidDeviceTest/kotlin/androidx/compose/material3/SwitchTest.kt:240-262switch_stateChange_movesThumb@Test
fun switch_stateChange_movesThumb() {
var checked by mutableStateOf(false) // STATE (hoisted)
rule.setMaterialContent(lightColorScheme()) { // SET CONTENT (in test body)
val spacer = @Composable { Spacer(Modifier.size(16.dp).testTag("spacer")) }
Switch(
modifier = Modifier.testTag(defaultSwitchTag),
checked = checked,
thumbContent = spacer,
onCheckedChange = { checked = it },
)
}
rule.onNodeWithTag("spacer", useUnmergedTree = true) // FIND + ASSERT
.assertLeftPositionInRootIsEqualTo(8.dp)
rule.runOnIdle { checked = true } // ACT (mutate from outside)
rule.onNodeWithTag("spacer", useUnmergedTree = true) // RE-ASSERT
.assertLeftPositionInRootIsEqualTo(28.dp)
rule.runOnIdle { checked = false }
rule.onNodeWithTag("spacer", useUnmergedTree = true).assertLeftPositionInRootIsEqualTo(8.dp)
}rule.runOnIdle { … }waitForIdle()val (index, offset) = rule.runOnIdle {
state.firstVisibleItemIndex to state.firstVisibleItemScrollOffset
}compose/foundation/foundation/integration-tests/lazy-tests/src/androidTest/kotlin/androidx/compose/foundation/lazy/list/LazyListTest.kt:903-904rule.runOnIdle { … }rule.runOnIdle { count = 5 }setContent// WRONG
@Test
fun toggle() {
rule.setContent {
var checked by remember { mutableStateOf(false) } // <-- trapped inside composition
Switch(checked = checked, onCheckedChange = { checked = it })
}
rule.runOnIdle { checked = true } // does not compile / has no reference
}
// WRONG because: the test thread has no reference to `checked`. The state cannot be driven
// from outside, and the test can only verify the very first frame.// RIGHT
@Test
fun toggle() {
var checked by mutableStateOf(false) // <-- hoisted to test scope
rule.setContent {
Switch(checked = checked, onCheckedChange = { checked = it })
}
rule.onNodeWithTag("switch").assertIsOff()
rule.runOnIdle { checked = true } // mutate via runOnIdle
rule.onNodeWithTag("switch").assertIsOn()
}setContent@Before// WRONG
@Before
fun setUp() {
rule.setContent { MyScreen(state) } // <-- shared across all tests
}
@Test fun stateA() { /* mutate `state` here, but it was already composed without this case */ }// RIGHT
@Test
fun stateA() {
val state = ScreenState(initial = "A")
rule.setContent { MyScreen(state) } // <-- per-test composition
rule.onNodeWithTag("title").assertTextEquals("A")
}runComposeUiTest// WRONG
class MyTest {
@get:Rule val rule = createComposeRule(StandardTestDispatcher())
@Test fun a() = runComposeUiTest { // <-- second test environment
setContent { /* … */ }
}
}
// WRONG because: createComposeRule and runComposeUiTest each manage an independent test
// environment (composition + MainTestClock + IdlingResourceRegistry). Mixing them produces
// undefined behavior. KDoc warns explicitly: see the ComposeUiTest.android.kt KDoc on
// runComposeUiTest, runAndroidComposeUiTest, runEmptyComposeUiTest, and the v2 variants.// RIGHT — pick exactly one entry point per class
class MyRuleTest {
@get:Rule val rule = createComposeRule(StandardTestDispatcher())
@Test fun a() { rule.setContent { /* … */ } }
}
// or
class MyFunctionTest {
@Test fun a() = runComposeUiTest { setContent { /* … */ } }
}// RIGHT — ComponentActivity comes free from ui-test-manifest
@get:Rule val rule = createComposeRule(StandardTestDispatcher())// RIGHT — a custom Activity must be declared in src/androidTest/AndroidManifest.xml
@get:Rule val rule = createAndroidComposeRule<MyActivity>(StandardTestDispatcher())<!-- src/androidTest/AndroidManifest.xml -->
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<application>
<activity android:name=".MyActivity" />
</application>
</manifest>MediumTestLargeTest@MediumTest // default — sub-second tests
class SwitchTest { /* … */ }
@LargeTest // long-clicks, multi-second animations
class CombinedClickableTest { /* … */ }@MediumTestSwitchTest.kt:76@LargeTest@RunWith(AndroidJUnit4::class)@MediumTestcreateComposeRule(...)createAndroidComposeRule<A>(...)createEmptyComposeRule()@get:RulerunComposeUiTest { }compose/ui/ui-test/src/androidMain/kotlin/androidx/compose/ui/test/ComposeUiTest.android.kt:158, 204, 247, 338androidx.compose.ui.test.junit4.v2.createComposeRulecreateAndroidComposeRule@Deprecated(level = WARNING)UnconfinedTestDispatcherStandardTestDispatchervar x by mutableStateOf(...)setContent { }rule.setContent { }@Test@Beforerule.runOnIdle { state = … }rule.runOnUiThread { … }mainClock.autoAdvance = false../../synchronization/synchronizing-with-idle/SKILL.mdrule.runOnIdle { state.value }Modifier.testTag(...)const../../finders/finding-nodes-by-tag-text-content/SKILL.mdSwitchTest.kt:140-147@RunWith(AndroidJUnit4::class)@MediumTest@LargeTest@get:RulecreateComposeRule(...)createAndroidComposeRule(...)createEmptyComposeRule()runComposeUiTest { }setContent { }@Test@Before@BeforeEachvar x by mutableStateOf(...)setContentsetContentrule.runOnIdle { }rule.runOnUiThread { }rule.runOnIdle { }Thread.sleep../../synchronization/synchronizing-with-idle/SKILL.mdsrc/androidTest/AndroidManifest.xmlAndroidManifest.xmlcompose/material3/material3/src/androidDeviceTest/kotlin/androidx/compose/material3/SwitchTest.kt:76-263compose/foundation/foundation/src/androidDeviceTest/kotlin/androidx/compose/foundation/textfield/TextFieldFocusCustomDialogTest.kt:57-60compose/ui/ui-test-junit4/src/androidMain/kotlin/androidx/compose/ui/test/junit4/v2/AndroidComposeTestRule.android.ktcompose/ui/ui-test/src/androidMain/kotlin/androidx/compose/ui/test/ComposeUiTest.android.kt:158, 204, 247, 338runOnIdlecompose/ui/ui-test-junit4/src/jvmAndAndroidMain/kotlin/androidx/compose/ui/test/junit4/ComposeTestRule.jvmAndAndroid.kt