Testing

An essential way of testing accessibility is a form of manual testing: by turning accessibility services, like TalkBack or Switch Access, on, and checking if everything works as expected. This provides direct insight into how users with accessibility needs might experience your application.

In conjunction with manual verification, you should also use automated testing to flag any potential issues that could impact user experience as you make continual changes to your app.

Existing Compose testing APIs allow you to write automated tests that interact with semantic elements and to assert the properties defined in your UI.

Accessibility checks

For automated accessibility testing, you can also use the Accessibility Test Framework—the same underlying framework that powers Accessibility Scanner and accessibility checks in Espresso—to perform some accessibility-related checks automatically, starting with Compose 1.8.0.

To enable the checks, add the ui-test-junit4-accessibility dependency, call enableAccessibilityChecks() in the AndroidComposeTestRule, and trigger an action or tryPerformAccessibilityChecks:

@Rule
@JvmField
val composeTestRule = createAndroidComposeRule<ComponentActivity>()

@Test
fun noAccessibilityLabel() {
    composeTestRule.setContent {
        Box(
            modifier = Modifier
                .size(50.dp, 50.dp)
                .background(color = Color.Gray)
                .clickable { }
                .semantics {
                    contentDescription = ""
                }
        )
    }

    composeTestRule.enableAccessibilityChecks()

    // Any action (such as performClick) will perform accessibility checks too:
    composeTestRule.onRoot().tryPerformAccessibilityChecks()
}

This specific test fails with an exception and a message that the item may not have a label readable by accessibility services.

Other available checks look for accessibility issues with color contrast, small touch target size, or elements' traversal order.

If you're mixing Views with Compose and you're already using an AccessibilityValidator, or you need to configure one, you can set it in the rule:

@Test
fun lowContrastScreen() {
    composeTestRule.setContent {
        Box(
            modifier = Modifier
                .fillMaxSize()
                .background(color = Color(0xFFFAFBFC)),
            contentAlignment = Alignment.Center
        ) {
            Text(text = "Hello", color = Color(0xFFB0B1B2))
        }
    }

    // Optionally, set AccessibilityValidator manually
    val accessibilityValidator = AccessibilityValidator()
        .setThrowExceptionFor(
            AccessibilityCheckResult.AccessibilityCheckResultType.WARNING
        )

    composeTestRule.enableAccessibilityChecks(accessibilityValidator)

    composeTestRule.onRoot().tryPerformAccessibilityChecks()
}

In combination with manual testing, automated tests using both Compose APIs as well as the Accessibility Test Framework can help you detect possible problems early on in the development process.