ফোকাস নেভিগেশন পরীক্ষা করা

ফোকাস নেভিগেশন টেস্ট অটোমেট করলে, আপনার অ্যাপ হার্ডওয়্যার কীবোর্ড, ডি-প্যাড ও অ্যাক্সেসিবিলিটি টুল জুড়ে ব্যবহারকারীকে একই ধরনের ও প্রত্যাশিত অভিজ্ঞতা প্রদান করতে পারে। Compose-এ বিল্ট-ইন টেস্টিং API আছে যা কী ইনপুট সিমুলেট করে এবং ফোকাস স্টেট যাচাই করে।

ফোকাস টেস্ট সেট-আপ করুন

ফোকাস টেস্ট লিখতে, Compose টেস্টিং লাইব্রেরি থেকে createComposeRule() ব্যবহার করুন:

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

এক-মাত্রিক ফোকাস ট্রাভার্সাল পরীক্ষা করুন

performKeyInput ব্যবহার করে Tab এবং Shift+Tab কী প্রেস করার সিমুলেশন করুন যাতে যাচাই করা যায় যে ফোকাস UI-এর মাধ্যমে উপস্থিতির ক্রম অনুযায়ী অগ্রসর হয়। সবচেয়ে ভালো পদ্ধতি হিসেবে, দুই-পথের ট্রাভার্সাল ইন্টিগ্রিটি নিশ্চিত করতে, ফরোয়ার্ড নেভিগেশন (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()
}