آزمایش پیمایش کانونی

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

راه‌اندازی آزمایش‌های تمرکز

برای نوشتن آزمایش‌های تمرکز، از 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()
}

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

فشردن کلیدهای Tab و Shift+Tab را بااستفاده از performKeyInput شبیه‌سازی کنید تا مطمئن شوید که تمرکز به‌ترتیب ظاهر شدن در واسط کاربر پیش می‌رود. به‌عنوان بهترین روش، هم پیمایش روبه‌جلو (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()
}

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

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

آزمایش رفتار تمرکز فیلد نوشتاری

بررسی کنید که فیلدهای نوشتاری تک‌خطی با کلید جهش تمرکز را جلو ببرند، درحالی‌که فیلدهای نوشتاری چندخطی تمرکز را حفظ کنند:

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