ทดสอบการไปยังส่วนต่างๆ ด้วยโฟกัส

การทดสอบการไปยังส่วนต่างๆ โดยใช้โฟกัสแบบอัตโนมัติช่วยให้มั่นใจได้ว่าแอปจะมอบประสบการณ์ของผู้ใช้ที่สอดคล้องและคาดการณ์ได้ในแป้นพิมพ์ฮาร์ดแวร์, D-pad และเครื่องมือช่วยเหลือพิเศษ Compose มี API การทดสอบในตัวเพื่อจำลองการป้อนข้อมูลด้วยแป้นพิมพ์และยืนยันสถานะที่มีการโฟกัส

ตั้งค่าการทดสอบที่มุ่งเน้น

หากต้องการเขียนการทดสอบโฟกัส ให้ใช้ createComposeRule() จากไลบรารีการทดสอบ Compose ดังนี้

@RunWith(AndroidJUnit4::class)
class FocusNavigationTest {
    @get:Rule
    val composeTestRule = createComposeRule()

ยืนยันเป้าหมายโฟกัส

คุณสามารถยืนยันได้ว่าองค์ประกอบ UI สามารถรับโฟกัสได้หรือไม่โดยการคลิกหรือขอโฟกัส จากนั้นยืนยันสถานะโดยใช้ assertIsFocused() และ 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()
}

ทดสอบการข้ามโฟกัสแบบมิติเดียว

จำลองการกดแป้น Tab และ Shift+Tab โดยใช้ performKeyInput เพื่อยืนยันว่าโฟกัสเลื่อนผ่าน UI ตามลำดับที่ปรากฏ แนวทางปฏิบัติแนะนำคือให้ตรวจสอบทั้งการไปยังส่วนต่างๆ แบบไปข้างหน้า (Key.Tab) และการไปยังส่วนต่างๆ แบบย้อนกลับ (Key.Tab โดยกด Key.ShiftLeft ค้างไว้) เพื่อให้มั่นใจว่าการไปยังส่วนต่างๆ แบบ 2 ทางจะทำงานได้อย่างสมบูรณ์

@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()
}

ทดสอบการไปยังส่วนต่างๆ แบบ 2 มิติในทิศทางต่างๆ

จำลองปุ่มลูกศรหรือปุ่มทิศทาง D-pad โดยใช้ Key.DirectionDown, Key.DirectionUp, Key.DirectionRight และ 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()
}

ทดสอบลักษณะการทำงานของโฟกัสในช่องข้อความ

ตรวจสอบว่าช่องข้อความบรรทัดเดียวจะเลื่อนโฟกัสเมื่อกด Tab ขณะที่ช่องข้อความหลายบรรทัดจะยังคงโฟกัสอยู่

@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()
}