Mengotomatiskan pengujian navigasi fokus memastikan aplikasi Anda memberikan pengalaman pengguna yang konsisten dan dapat diprediksi di seluruh keyboard hardware, D-pad, dan alat aksesibilitas. Compose menyediakan API pengujian bawaan untuk menyimulasikan input tombol dan memverifikasi status fokus.
Menyiapkan uji fokus
Untuk menulis pengujian fokus, gunakan createComposeRule() dari library pengujian Compose:
@RunWith(AndroidJUnit4::class) class FocusNavigationTest { @get:Rule val composeTestRule = createComposeRule()
Memverifikasi target fokus
Anda dapat memverifikasi apakah elemen UI dapat menerima fokus dengan melakukan klik atau
meminta fokus, lalu menegaskan statusnya menggunakan assertIsFocused() dan
assertIsNotFocused().
@Test fun interactiveElement_isFocusTarget() { composeTestRule.setContent { AppTheme { CardListScreen() } } val firstCard = composeTestRule.onNodeWithTag("card_1") val secondCard = composeTestRule.onNodeWithTag("card_2") // Focus the first element firstCard.performClick() firstCard.assertIsFocused() secondCard.assertIsNotFocused() }
Menguji traversal fokus satu dimensi
Simulasikan penekanan tombol Tab dan Shift+Tab
menggunakan performKeyInput untuk memverifikasi bahwa fokus berpindah melalui UI
dalam urutan tampilan. Sebagai praktik terbaik,
verifikasi navigasi maju (Key.Tab) dan navigasi mundur
(Key.Tab dengan Key.ShiftLeft ditahan) untuk memastikan integritas
traversal dua arah:
@Test fun tabKey_navigatesInAppearanceOrder() { composeTestRule.setContent { AppTheme { CardListScreen() } } val firstCard = composeTestRule.onNodeWithTag("card_1") val secondCard = composeTestRule.onNodeWithTag("card_2") val thirdCard = composeTestRule.onNodeWithTag("card_3") firstCard.performClick() firstCard.assertIsFocused() // Press Tab -> Moves to second card firstCard.performKeyInput { pressKey(Key.Tab) } secondCard.assertIsFocused() firstCard.assertIsNotFocused() // Press Tab -> Moves to third card secondCard.performKeyInput { pressKey(Key.Tab) } thirdCard.assertIsFocused() // Press Shift + Tab -> Moves backward to second card thirdCard.performKeyInput { withKeyDown(Key.ShiftLeft) { pressKey(Key.Tab) } } secondCard.assertIsFocused() }
Menguji penelusuran terarah dua dimensi
Simulasikan tombol panah atau tombol arah D-pad menggunakan Key.DirectionDown,
Key.DirectionUp, Key.DirectionRight, dan Key.DirectionLeft.
@Test fun arrowKeys_moveFocusTwoDimensionallyWithoutWrap() { composeTestRule.setContent { AppTheme { GridLayoutScreen() } } val topLeftButton = composeTestRule.onNodeWithTag("btn_top_left") val bottomLeftButton = composeTestRule.onNodeWithTag("btn_bottom_left") topLeftButton.performClick() topLeftButton.assertIsFocused() // Down arrow moves focus downward to bottom-left button topLeftButton.performKeyInput { pressKey(Key.DirectionDown) } bottomLeftButton.assertIsFocused() // Directional keys do not wrap around: pressing Down on bottom element stays focused bottomLeftButton.performKeyInput { pressKey(Key.DirectionDown) } bottomLeftButton.assertIsFocused() }
Menguji perilaku fokus kolom teks
Pastikan kolom teks satu baris memajukan fokus pada Tab, sementara kolom teks multi-baris mempertahankan fokus:
@Test fun textField_handlesTabAccordingToLineLimits() { composeTestRule.setContent { AppTheme { FormScreen() } } val singleLineNode = composeTestRule.onNodeWithTag("single_line_field") val multiLineNode = composeTestRule.onNodeWithTag("multi_line_field") val submitButtonNode = composeTestRule.onNodeWithTag("submit_button") // Single-line field advances focus on Tab singleLineNode.performClick() singleLineNode.assertIsFocused() singleLineNode.performKeyInput { pressKey(Key.Tab) } multiLineNode.assertIsFocused() // Multi-line field keeps focus and inserts '\t' multiLineNode.performKeyInput { pressKey(Key.Tab) } multiLineNode.assertIsFocused() submitButtonNode.assertIsNotFocused() // Shift + Tab escapes multi-line field to previous target multiLineNode.performKeyInput { withKeyDown(Key.ShiftLeft) { pressKey(Key.Tab) } } singleLineNode.assertIsFocused() }
Direkomendasikan untuk Anda
- Catatan: teks link ditampilkan saat JavaScript nonaktif
- Target fokus
- Urutan traversal fokus
- Fokus di kolom teks
- Mengelompokkan target fokus