測試
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
測試無障礙功能的必要方式是採用人工測試:開啟 TalkBack 或切換控制等無障礙服務,然後檢查所有功能是否正常運作。這可讓您直接瞭解有無障礙需求的使用者如何體驗應用程式。
除了手動驗證之外,您還應使用自動化測試,在持續變更應用程式時標示可能影響使用者體驗的任何潛在問題。
現有的 Compose 測試 API 可讓您編寫自動化測試,與語意元素互動,並斷言 UI 中定義的屬性。
無障礙檢查
如要進行自動化無障礙程度測試,您也可以使用無障礙功能測試架構 (與 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 和/或其關係企業的商標或註冊商標。
上次更新時間:2025-08-23 (世界標準時間)。
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["缺少我需要的資訊","missingTheInformationINeed","thumb-down"],["過於複雜/步驟過多","tooComplicatedTooManySteps","thumb-down"],["過時","outOfDate","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["示例/程式碼問題","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間: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)"]]