テスト
コレクションでコンテンツを整理
必要に応じて、コンテンツの保存と分類を行います。
ユーザー補助機能をテストする重要な方法は、手動テストです。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()
}
この特定のテストは、例外と、アイテムにユーザー補助サービスで読み取り可能なラベルがない可能性があるというメッセージで失敗します。
その他のチェックでは、色のコントラスト、タップ ターゲットのサイズが小さい、要素の移動順序に関するユーザー補助機能の問題を探します。
ビューと 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-27 UTC。
[[["わかりやすい","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-27 UTC。"],[],[],null,["An 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\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/5673ffc60b614daf028ee936227128eb8c4f9781/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/5673ffc60b614daf028ee936227128eb8c4f9781/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- 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)"]]