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

ফোকাস নেভিগেশন টেস্ট অটোমেট করা নিশ্চিত করে যে আপনার অ্যাপ হার্ডওয়্যার কীবোর্ড, D-প্যাড ও অ্যাক্সেসিবিলিটি টুল জুড়ে একই রকম ও অনুমানযোগ্য ব্যবহারকারীর অভিজ্ঞতা প্রদান করে। 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()
}

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

Tab এবং Shift+Tab কী প্রেস করার সিমুলেশন করুন performKeyInput ব্যবহার করে এটি যাচাই করুন যে 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 ব্যবহার করে তীরচিহ্ন কী বা D-প্যাড দিকনির্দেশক কী সিমুলেট করুন।

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