Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Composing With Confidence

Adam McNeilly
September 01, 2022

Composing With Confidence

Presentation about testing in Jetpack Compose at Droidcon NYC 2022.

Adam McNeilly

September 01, 2022
Tweet

More Decks by Adam McNeilly

Other Decks in Programming

Transcript

  1. Compose Rule Setup class PrimaryButtonTest { /** * Use createComposeRule

    to test individual composable functions. */ @get:Rule val composeTestRule = createComposeRule() } @AdamMc331 #DCNYC22 5
  2. Compose Rule Setup class PrimaryButtonTest { /** * Use createAndroidComposeRule

    to start up a specific activity. */ @get:Rule val composeTestRule = createAndroidComposeRule<MainActivity>() } @AdamMc331 #DCNYC22 6
  3. Test Recipe // Find component composeTestRule.onNodeWithText("Test Button") // Make assertion

    composeTestRule.onNode(...).assertIsEnabled() // Perform action composeTestRule.onNode(...).performClick() @AdamMc331 #DCNYC22 8
  4. Test Recipe // Find component composeTestRule.onNodeWithText("Test Button") // Make assertion

    composeTestRule.onNode(...).assertIsEnabled() // Perform action composeTestRule.onNode(...).performClick() @AdamMc331 #DCNYC22 8
  5. Test Recipe // Find component composeTestRule.onNodeWithText("Test Button") // Make assertion

    composeTestRule.onNode(...).assertIsEnabled() // Perform action composeTestRule.onNode(...).performClick() @AdamMc331 #DCNYC22 8
  6. // In app PrimaryButton( modifier = Modifier.testTag("login_button") ) // In

    test composeTestRule.onNodeWithTag("login_button") @AdamMc331 #DCNYC22 20
  7. Render Content var wasClicked = false composeTestRule.setContent { PrimaryButton( text

    = "Test Button", onClick = { wasClicked = true }, enabled = true, ) } @AdamMc331 #DCNYC22 24
  8. @Test fun successfulLogin() { composeTestRule .onNodeWithTag("login_button") .assertIsNotEnabled() composeTestRule .onNodeWithTag("username_text_field") .performTextInput("adammc331")

    composeTestRule .onNodeWithTag("login_button") .assertIsNotEnabled() composeTestRule .onNodeWithTag("password_text_field") .performTextInput("Hunter2") composeTestRule .onNodeWithTag("login_button") .assertIsEnabled() composeTestRule .onNodeWithTag("login_button") .performClick() composeTestRule .onNodeWithTag("home_screen_label") .assertIsDisplayed() } @AdamMc331 #DCNYC22 35
  9. LoginScreenRobot class LoginScreenRobot( composeTestRule: ComposeTestRule, ) { private val usernameInput

    = composeTestRule.onNodeWithTag("username_text_field") private val passwordInput = composeTestRule.onNodeWithTag("password_text_field") private val loginButton = composeTestRule.onNodeWithTag("login_button") } @AdamMc331 #DCNYC22 37
  10. LoginScreenRobot class LoginScreenRobot { fun enterUsername(username: String) { usernameInput.performTextInput(username) }

    fun enterPassword(password: String) { passwordInput.performTextInput(password) } } @AdamMc331 #DCNYC22 38
  11. Kotlin Magic fun loginScreenRobot( composeTestRule: ComposeTestRule, block: LoginScreenRobot.() -> Unit,

    ) { LoginScreenRobot(composeTestRule).apply(block) } @AdamMc331 #DCNYC22 39