फ़ोकस नेविगेशन टेस्ट को ऑटोमेट करने से यह पक्का होता है कि आपका ऐप्लिकेशन, हार्डवेयर कीबोर्ड, डी-पैड, और सुलभता टूल पर, उपयोगकर्ताओं को एक जैसा और अनुमानित अनुभव दे. Compose में पहले से मौजूद टेस्टिंग एपीआई होते हैं. इनकी मदद से, कीबोर्ड से मिलने वाले इनपुट को सिम्युलेट किया जा सकता है और फ़ोकस की स्थितियों की पुष्टि की जा सकती है.
फ़ोकस टेस्ट सेट अप करना
फ़ोकस टेस्ट लिखने के लिए, Compose टेस्टिंग लाइब्रेरी से createComposeRule() का इस्तेमाल करें:
@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() }
टू-डाइमेंशनल डायरेक्शनल ट्रैवर्सल की जांच करना
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() }
आपके लिए सुझाव
- ध्यान दें: JavaScript बंद होने पर, लिंक टेक्स्ट दिखता है
- फ़ोकस टारगेट
- फ़ोकस ट्रैवर्सल ऑर्डर
- टेक्स्ट फ़ील्ड पर फ़ोकस करना
- ग्रुप फ़ोकस टारगेट