ناوبری فوکوس را آزمایش کنید

خودکارسازی تست‌های ناوبری فوکوس، تضمین می‌کند که برنامه شما یک تجربه کاربری سازگار و قابل پیش‌بینی را در سراسر صفحه‌کلیدهای سخت‌افزاری، کلیدهای جهت‌نما و ابزارهای دسترسی ارائه می‌دهد. Compose APIهای تست داخلی را برای شبیه‌سازی ورودی کلید و تأیید حالت‌های فوکوس فراهم می‌کند.

تست‌های تمرکز را تنظیم کنید

برای نوشتن تست‌های فوکوس، از createComposeRule() از کتابخانه تست Compose استفاده کنید:

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

اهداف تمرکز را تأیید کنید

شما می‌توانید با انجام یک کلیک یا درخواست فوکوس، و سپس اعلام وضعیت آن با استفاده از 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()
}

پیمایش فوکوس یک بعدی را آزمایش کنید

با استفاده از performKeyInput فشردن کلیدهای Tab و Shift+Tab را شبیه‌سازی کنید تا تأیید کنید که فوکوس به ترتیب ظاهر در رابط کاربری پیش می‌رود. به عنوان یک روش عالی، هم پیمایش رو به جلو ( Key.Tab ) و هم پیمایش معکوس ( Key.Tab با نگه داشتن Key.ShiftLeft ) را تأیید کنید تا از یکپارچگی پیمایش دو طرفه اطمینان حاصل شود:

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

پیمایش جهت‌دار دوبعدی را آزمایش کنید

کلیدهای جهت‌نما یا کلیدهای جهت‌نمای 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()
}

{% کلمه به کلمه %} {% فعل کمکی %} {% کلمه به کلمه %} {% فعل کمکی %}