androidx.a2ui.compose:compose-ui-testing टेस्टिंग लाइब्रेरी, टेस्टिंग एपीआई उपलब्ध कराती है. ये एपीआई, Jetpack टेस्टिंग लाइब्रेरी के लिए कंट्रोलर पैटर्न का इस्तेमाल करते हैं. जैसे, Navigation की TestNavHostController.
स्टैंडर्ड Jetpack Compose कॉम्पोनेंट, स्टैटिक पैरामीटर लेते हैं और यूज़र इंटरफ़ेस (यूआई) दिखाते हैं. इसके उलट, A2UI कॉम्पोनेंट कॉन्टेक्स्ट के हिसाब से काम करते हैं. ये A2uiComponentScope पर निर्भर होते हैं. इनकी मदद से, डाइनैमिक डेटा बाइंडिंग का आकलन किया जाता है, एजेंट को आउटबाउंड कार्रवाइयां भेजी जाती हैं, दोनों तरफ़ से डेटा बाइंडिंग की जाती है, और डाइनैमिक चाइल्ड टेंप्लेट को बड़ा किया जाता है.
टेस्टिंग एपीआई, टेस्ट सेटअप को आसान बनाते हैं. साथ ही, ये A2uiMessageProcessor के असली इंस्टेंस उपलब्ध कराते हैं और Compose के टेस्ट एनवायरमेंट से जुड़ी अपनी को-रूटीन चलाते हैं.
आइसोलेट किए गए कॉम्पोनेंट
आपके पास यह पुष्टि करने का विकल्प होता है कि कोई कॉम्पोनेंट, अपने डेटा को हल करता है, कार्रवाइयां भेजता है, और आपके डिज़ाइन सिस्टम थीम में सही तरीके से रेंडर करता है या नहीं:
@Test
fun button_resolvesStubChildAndDispatchesAction() = runComposeUiTest {
// 1. Create the test controller
val controller = A2uiTestController(
// Provide a catalog containing the component under test
catalog = CustomComponentCatalog,
// Configure the component under test with concrete properties
initialComponents = listOf(
A2uiComponentPayload(
id = "root",
type = "Button",
properties = mapOf(
"child" to "btn_text",
"variant" to "primary",
"action" to mapOf(
"event" to mapOf(
"name" to "submit_form",
"context" to mapOf("username" to mapOf("path" to "/user/name")),
),
),
),
),
A2uiComponentPayload("btn_text"),
),
// Stub the required child component
componentStubs = listOf(
A2uiComponentStub.withId("btn_text") { _, modifier ->
Text("Submit", modifier = modifier)
},
),
// Provide initial dynamic data
initialData = mapOf("user" to mapOf("name" to "Test User")),
)
// 2. Start background processing and initialize the surface
val surface = controller.start()
// 3. Mount the UI
setContent {
A2uiTestSurface(surface)
}
// 4. Interact using standard Compose UI semantics
onNodeWithText("Submit").performClick()
// 5. Wait for Compose and A2UI background processes to settle
waitForIdle()
controller.waitForIdle()
// 6. Assert outbound actions were correctly evaluated and intercepted
val action = controller.dispatchedActions.single() as A2uiEventAction
assertEquals("submit_form", action.eventName)
assertEquals("Test User", action.context["username"])
}
सरफ़ेस के स्टेटस
A2uiSurface जैसे सर्फ़ेस होस्ट की जांच की जा सकती है. इसमें उनकी स्थितियां और ट्रांज़िशन शामिल हैं:
@Test
fun surface_displaysLoading_thenTransitionsToContent() = runComposeUiTest {
// 1. Create an empty controller to simulate a pending network request
val controller = A2uiTestController(
catalog = CustomComponentCatalog,
// Pre-register a stub for the expected root component type
componentStubs = listOf(
A2uiComponentStub.withType("RootLayout") { _, modifier ->
Text("Content Ready", modifier = modifier)
},
),
)
val surface = controller.start()
// 2. Mount the surface UI
setContent {
A2uiSurface(surfaceModel = surface)
}
// 3. Assert the loading placeholder is active
onNode(hasProgressBarRangeInfo(ProgressBarRangeInfo.Indeterminate)).assertExists()
// 4. Simulate the agent pushing the layout payload over the network
controller.updateComponent(
id = "root",
type = "RootLayout",
properties = emptyMap(),
)
// 5. Wait for the data layer and animation to settle
controller.waitForIdle()
waitForIdle()
// 6. Assert the loading state is gone and content is visible
onNode(hasProgressBarRangeInfo(ProgressBarRangeInfo.Indeterminate)).assertDoesNotExist()
onNodeWithText("Content Ready").assertIsDisplayed()
}
दोनों तरफ़ से बाइंडिंग
उपयोगकर्ता के इनपुट के दौरान, डेटा मॉडल में वापस लिखने वाले टेक्स्ट फ़ील्ड जैसे कॉम्पोनेंट की जांच की जा सकती है. साथ ही, जब एजेंट डेटा मॉडल में बदलाव करता है, तब अपडेट की पुष्टि की जा सकती है:
@Test
fun textField_writesToDataModelAndReactsToAgent() = runComposeUiTest {
val controller = A2uiTestController(
catalog = CustomComponentCatalog,
initialComponents = listOf(
A2uiComponentPayload(
id = "root",
type = "TextField",
properties = mapOf(
"label" to "Username",
"value" to mapOf("path" to "/form/username"),
),
),
),
initialData = mapOf("form" to mapOf("username" to "Initial")),
)
val surface = controller.start()
setContent {
A2uiTestSurface(surface)
}
// 1. User interaction updates the global DataModel locally
onNodeWithText("Initial").performTextReplacement("LocallyTyped")
waitForIdle()
// 2. Assert the component wrote back to the DataModel
assertEquals("LocallyTyped", controller.getData<String>("/form/username"))
// 3. Simulate the agent pushing a data update for the same path
controller.updateData("/form/username", "ServerOverridden")
controller.waitForIdle()
// 4. Assert the component reactively updated the UI
onNodeWithText("ServerOverridden").assertIsDisplayed()
}
टेंप्लेट वाले चाइल्ड कॉम्पोनेंट
A2UI ChildList टेंप्लेट का इस्तेमाल करके, बच्चों के लिए डिज़ाइन किए गए कॉम्पोनेंट की जांच की जा सकती है. इन कॉम्पोनेंट में बच्चों के लिए कॉन्टेंट के कलेक्शन दिखाए जाते हैं:
@Test
fun column_rendersDynamicChildTemplates() = runComposeUiTest {
val controller = A2uiTestController(
catalog = CustomComponentCatalog,
initialData = mapOf(
"catalog" to mapOf(
"products" to listOf(
mapOf("title" to "Camera"),
mapOf("title" to "Laptop"),
),
),
),
initialComponents = listOf(
A2uiComponentPayload(
id = "root",
type = "Column",
properties = mapOf(
"children" to mapOf(
"path" to "/catalog/products",
"componentId" to "product_template",
),
),
),
// Bind the initial properties for the dynamically instantiated
// template stub.
A2uiComponentPayload(
id = "product_template",
properties = mapOf("title" to mapOf("path" to "title")),
),
),
componentStubs = listOf(
A2uiComponentStub.withId(id = "product_template") { props, modifier ->
val titleProp = remember { A2uiProperty.dynamicString("title") }
val title = props.bind(titleProp) ?: "Unknown"
Text(text = "Stubbed: $title", modifier = modifier)
},
),
)
val surface = controller.start()
setContent { A2uiTestSurface(surface) }
// Verify the template was instantiated twice with relative data
onNodeWithText("Stubbed: Camera").assertExists()
onNodeWithText("Stubbed: Laptop").assertExists()
// Simulate appending a new item to the data model array
controller.updateData("/catalog/products/-", mapOf("title" to "Tablet"))
controller.waitForIdle()
// Verify the Column dynamically instantiated a new child stub
onNodeWithText("Stubbed: Tablet").assertExists()
}
एजेंट से जुड़ी गड़बड़ियों के लिए, गड़बड़ी के बाद की कार्रवाई
इस बात की पुष्टि की जा सकती है कि प्लैटफ़ॉर्म और कॉम्पोनेंट, एजेंट की गड़बड़ियों को आसानी से ठीक कर सकते हैं. जैसे, तथ्यों का गलत होना:
@Test
fun surface_displaysErrorFallback_onAgentHallucination() = runComposeUiTest {
val controller = A2uiTestController(catalog = CustomComponentCatalog)
val surface = controller.start()
// 1. Mount the surface orchestrator with error boundaries
setContent { A2uiSurface(surfaceModel = surface) }
// 2. Simulate an agent hallucinating a broken component layout
controller.failComponent(
id = "root",
exception = A2uiException.A2uiValidationException(
message = "HallucinatedType",
path = "/components/root"
),
)
controller.waitForIdle()
// 3. Assert the surface displayed the fallback error state
onNodeWithText("Failed to load: HallucinatedType").assertIsDisplayed()
// 4. Assert the core layer dispatched an error to the server
val errorMsg = controller.outboundErrors.single()
assertEquals("VALIDATION_FAILED", errorMsg.code)
}
प्रोग्रेसिव रेंडरिंग
आपके पास ऐसी इंटरमीडिएट स्थितियों को टेस्ट करने का विकल्प होता है जहां पैरंट कॉम्पोनेंट लोड हो गया है, लेकिन चाइल्ड कॉम्पोनेंट अब भी लोड हो रहे हैं:
@Test
fun progressiveRendering_parentRendersWhileChildIsPending() = runComposeUiTest {
// 1. Mount the parent, omitting the child instance
val controller = A2uiTestController(
catalog = CustomComponentCatalog,
initialComponents = listOf(
A2uiComponentPayload(
id = "root",
type = "Button",
properties = mapOf(
"child" to "delayed_text_id",
"action" to mapOf("event" to mapOf("name" to "click")),
),
),
),
)
val surface = controller.start()
setContent {
A2uiTestSurface(surface)
}
// 2. Initial state: parent is rendered, child displays loading state
onNodeWithText("Submit").assertDoesNotExist()
onNode(hasProgressBarRangeInfo(ProgressBarRangeInfo.Indeterminate)).assertExists()
// 3. Simulate arrival of the child component
controller.updateComponent(
id = "delayed_text_id",
type = "Text",
properties = mapOf("text" to "Submit"),
)
controller.waitForIdle()
// 4. Assert that progressive rendering completed
onNode(hasProgressBarRangeInfo(ProgressBarRangeInfo.Indeterminate)).assertDoesNotExist()
onNodeWithText("Submit").assertIsDisplayed()
}
लागू करने से जुड़ी जानकारी
यहां दिए गए सेक्शन में, टेस्ट फ़्रेमवर्क में कॉम्पोनेंट ओवरराइड, स्कीमा की पुष्टि, और कोरूटीन सिंक्रनाइज़ेशन के बारे में बताया गया है.
टेस्टिंग लाइब्रेरी में ये मुख्य एपीआई शामिल हैं:
A2uiTestController: एक्सटेंशन कंस्ट्रक्टर फ़ंक्शन और मुख्य टेस्ट कंट्रोलर इंटरफ़ेस.A2uiComponentStub: चाइल्ड और कैटलॉग कॉम्पोनेंट के लिए स्टब और ओवरराइड.A2uiTestSurface: यह एक कंपोज़ेबल यूटिलिटी है, जो टेस्ट सरफेस को माउंट करती है.
कॉम्पोनेंट ओवरराइड बनाम स्टैंडर्ड मॉक
थर्ड-पार्टी के भारी-भरकम मॉक फ़्रेमवर्क को हटाने के लिए, यूज़र इंटरफ़ेस (यूआई) स्टब (A2uiComponentStub) का इस्तेमाल करके चाइल्ड कॉम्पोनेंट और बाहरी डिपेंडेंसी को बायपास किया जाता है.
A2uiComponentStub.withId, आईडी के हिसाब से किसी कॉम्पोनेंट इंस्टेंस को इंटरसेप्ट करता है. वहीं, A2uiComponentStub.withType पूरे कैटलॉग टाइप के लिए रेंडरिंग को बदल देता है.
स्कीमा की पुष्टि करने की सुविधा
टेस्ट फ़्रेमवर्क, A2UI प्रोटोकॉल के कानूनी समझौते को सिंक्रोनस तरीके से लागू करता है. जब कंट्रोलर कॉम्पोनेंट को शुरू करता है या अपडेट करता है, तब वह दिए गए पेलोड के ख़िलाफ़ A2uiCoreSchemaValidator को चलाता है. अगर कोई अमान्य प्रॉपर्टी सेट की जाती है, जैसे कि ज़रूरी फ़ील्ड मौजूद न होना या टाइप मैच न होना, तो टेस्ट तुरंत क्रैश हो जाता है और A2uiValidationException दिखता है.
Coroutine synchronization
A2uiTestController.start, runComposeUiTest() के ज़रिए उपलब्ध कराए गए टेस्ट कोरूटीन कॉन्टेक्स्ट में हुक करता है. यह currentCoroutineContext() को निकालता है, मैप करता है, और बैकग्राउंड लूप को अलग किए गए Job पर मैप करता है. साथ ही, टेस्ट ब्लॉक पूरा होने पर यह अपने-आप रद्द हो जाता है. इससे टेस्ट के ऐसे एक्ज़ीक्यूशन नहीं होते जो किसी अन्य टेस्ट से जुड़े न हों. waitForIdle()
बैकग्राउंड में चल रही सभी कोराउटीन के पूरा होने का इंतज़ार करता है.