Skip to content

Instantly share code, notes, and snippets.

@ngallazzi
Last active December 29, 2023 16:10
Show Gist options
  • Save ngallazzi/cd336f3c0f84e12e3bed8c1cd028c1a6 to your computer and use it in GitHub Desktop.
Save ngallazzi/cd336f3c0f84e12e3bed8c1cd028c1a6 to your computer and use it in GitHub Desktop.
class SwitchLayoutKtTest {
// Create test rule, we need android context so we use createAndroidComposeRule factory method
@get:Rule
val composeTestRule = createAndroidComposeRule<ComponentActivity>()
@Test
fun testButtonOn_setsOnState() {
val context = composeTestRule.activity
val onButtonNodeIdentifier = context.getString(R.string.on_button_description)
val currentStatusNodeIdentifier = context.getString(R.string.current_status_description)
// Start the app
composeTestRule.setContent {
SemanticsTestingPlaygroundTheme {
SwitchLayout()
}
}
// Perform click action on "on button", using its semantics matcher
composeTestRule.onNodeWithContentDescription(onButtonNodeIdentifier).performClick()
// Assert status text switches to "on status"
composeTestRule.onNodeWithContentDescription(currentStatusNodeIdentifier)
.assertTextEquals("Status is: ${SwitchStatus.ON.name}")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment