测试
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
测试无障碍功能的一种基本方法是手动测试:开启 TalkBack 或开关控制等无障碍服务,然后检查一切是否按预期运行。这样,您就可以直接了解具有无障碍功能需求的用户可能会如何体验您的应用。
除了手动验证之外,您还应使用自动化测试,在不断更改应用时标记任何可能影响用户体验的潜在问题。
借助现有的 Compose 测试 API,您可以编写与语义元素交互的自动化测试,并断言界面中定义的属性。
无障碍检查
对于自动化无障碍功能测试,您还可以使用无障碍功能测试框架(即 Espresso 中的无障碍功能扫描器和无障碍功能检查所依赖的底层框架)自动执行一些与无障碍功能相关的检查,从 Compose 1.8.0 开始。
如需启用这些检查,请添加 ui-test-junit4-accessibility
依赖项,在 AndroidComposeTestRule
中调用 enableAccessibilityChecks()
,然后触发操作或 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()
}
此特定测试会失败并显示异常,并显示一条消息,指出相应项可能没有可供无障碍服务读取的标签。
其他可用的检查会查找色彩对比度、触摸目标大小过小或元素的遍历顺序方面的无障碍功能问题。
如果您将 View 与 Compose 混合使用,并且已经在使用 AccessibilityValidator
,或者需要配置 AccessibilityValidator
,则可以在规则中进行设置:
@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()
}
结合使用手动测试和同时使用 Compose API 和无障碍功能测试框架的自动化测试,有助于您在开发过程中尽早发现可能存在的问题。
为您推荐
本页面上的内容和代码示例受内容许可部分所述许可的限制。Java 和 OpenJDK 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-08-23。
[null,null,["最后更新时间 (UTC):2025-08-23。"],[],[],null,["# Testing\n\nAn essential way of testing accessibility is a form of manual testing: by\nturning accessibility services, like TalkBack or Switch Access, on, and checking\nif everything works as expected. This provides direct insight into how users\nwith accessibility needs might experience your application.\n\nIn conjunction with manual verification, you should also use automated testing\nto flag any potential issues that could impact user experience as you make\ncontinual changes to your app.\n\n[Existing Compose testing APIs](/develop/ui/compose/testing) allow you to write automated tests that\ninteract with semantic elements and to [assert the properties](/develop/ui/compose/testing/apis#assertions) defined in\nyour UI.\n\nAccessibility checks\n--------------------\n\nFor automated accessibility testing, you can also use the\n[Accessibility Test Framework](https://github.com/google/Accessibility-Test-Framework-for-Android)---the same underlying framework that powers\nAccessibility Scanner and accessibility checks in Espresso---to perform some\naccessibility-related checks automatically, starting with Compose 1.8.0.\n\nTo enable the checks, add the `ui-test-junit4-accessibility` dependency,\ncall [`enableAccessibilityChecks()`](/reference/kotlin/androidx/compose/ui/test/ComposeUiTest#(androidx.compose.ui.test.ComposeUiTest).enableAccessibilityChecks(com.google.android.apps.common.testing.accessibility.framework.integrations.espresso.AccessibilityValidator)) in the [`AndroidComposeTestRule`](/reference/kotlin/androidx/compose/ui/test/junit4/AndroidComposeTestRule),\nand trigger an action or [`tryPerformAccessibilityChecks`](https://cs.android.com/androidx/platform/frameworks/support/+/androidx-main:compose/ui/ui-test/src/androidMain/kotlin/androidx/compose/ui/test/Actions.android.kt;drc=808c430f1ac6028d33902e3d685720f2b96f7aee;l=27):\n\n\n```kotlin\n@Rule\n@JvmField\nval composeTestRule = createAndroidComposeRule\u003cComponentActivity\u003e()\n\n@Test\nfun noAccessibilityLabel() {\n composeTestRule.setContent {\n Box(\n modifier = Modifier\n .size(50.dp, 50.dp)\n .background(color = Color.Gray)\n .clickable { }\n .semantics {\n contentDescription = \"\"\n }\n )\n }\n\n composeTestRule.enableAccessibilityChecks()\n\n // Any action (such as performClick) will perform accessibility checks too:\n composeTestRule.onRoot().tryPerformAccessibilityChecks()\n}https://github.com/android/snippets/blob/dd30aee903e8c247786c064faab1a9ca8d10b46e/compose/snippets/src/androidTest/java/com/example/compose/snippets/accessibility/AccessibilitySnippets.kt#L49-L71\n```\n\n\u003cbr /\u003e\n\nThis specific test fails with an exception and a message that the item may not\nhave a label readable by accessibility services.\n\nOther available checks look for accessibility issues with color contrast,\nsmall touch target size, or elements' traversal order.\n\nIf you're mixing Views with Compose and you're already using an\n`AccessibilityValidator`, or you need to configure one, you can set it in the\nrule:\n\n\n```kotlin\n@Test\nfun lowContrastScreen() {\n composeTestRule.setContent {\n Box(\n modifier = Modifier\n .fillMaxSize()\n .background(color = Color(0xFFFAFBFC)),\n contentAlignment = Alignment.Center\n ) {\n Text(text = \"Hello\", color = Color(0xFFB0B1B2))\n }\n }\n\n // Optionally, set AccessibilityValidator manually\n val accessibilityValidator = AccessibilityValidator()\n .setThrowExceptionFor(\n AccessibilityCheckResult.AccessibilityCheckResultType.WARNING\n )\n\n composeTestRule.enableAccessibilityChecks(accessibilityValidator)\n\n composeTestRule.onRoot().tryPerformAccessibilityChecks()\n}https://github.com/android/snippets/blob/dd30aee903e8c247786c064faab1a9ca8d10b46e/compose/snippets/src/androidTest/java/com/example/compose/snippets/accessibility/AccessibilitySnippets.kt#L94-L116\n```\n\n\u003cbr /\u003e\n\nIn combination with manual testing, automated tests using both Compose APIs as\nwell as the Accessibility Test Framework can help you detect possible problems\nearly on in the development process.\n\nRecommended for you\n-------------------\n\n- Note: link text is displayed when JavaScript is off\n- [Accessibility in Compose](/develop/ui/compose/testing)\n- \\[Material Design 2 in Compose\\]\\[19\\]\n- [Testing your Compose layout](/develop/ui/compose/testing/apis#assertions)"]]