Compose offers ComposeTestRule, जिसकी मदद से ऐनिमेशन के लिए टेस्ट लिखे जा सकते हैं. साथ ही, टेस्ट क्लॉक पर पूरा कंट्रोल रखा जा सकता है. इससे आपको एनिमेशन की इंटरमीडिएट वैल्यू की पुष्टि करने में मदद मिलती है. इसके अलावा, टेस्ट, ऐनिमेशन की असल अवधि से कम समय में भी पूरा हो सकता है.
ComposeTestRule, अपनी टेस्ट क्लॉक को mainClock के तौर पर दिखाता है. अपने टेस्ट कोड में घड़ी को कंट्रोल करने के लिए, autoAdvance प्रॉपर्टी को false पर सेट किया जा सकता है. जिस ऐनिमेशन को टेस्ट करना है उसे शुरू करने के बाद, advanceTimeBy की मदद से घड़ी को आगे बढ़ाया जा सकता है.
यहां ध्यान देने वाली बात यह है कि advanceTimeBy, घड़ी को तय की गई अवधि के हिसाब से ठीक-ठीक आगे नहीं बढ़ाता है. इसके बजाय, यह उसे फ़्रेम की अवधि के मल्टीप्लायर के तौर पर सबसे नज़दीकी अवधि तक राउंड अप करता है.
@get:Rule val rule = createComposeRule() @Test fun testAnimationWithClock() { // Pause animations rule.mainClock.autoAdvance = false var enabled by mutableStateOf(false) rule.setContent { val color by animateColorAsState( targetValue = if (enabled) Color.Red else Color.Green, animationSpec = tween(durationMillis = 250) ) Box(Modifier.size(64.dp).background(color)) } // Initiate the animation. enabled = true // Let the animation proceed. rule.mainClock.advanceTimeBy(50L) // Compare the result with the image showing the expected result. // `assertAgainGolden` needs to be implemented in your code. rule.onRoot().captureToImage().assertAgainstGolden() }
ऐनिमेशन टेस्ट को ऑप्टिमाइज़ करना
When testing high-fidelity animations, you often need to disable auto-advance
and manually step through frames to assert intermediate UI states. For these
specific frame-by-frame loops, use the runWithoutImplicitWait method to
execute your assertions. Standard node queries (like onNodeWithTag or
fetchSemanticsNode) trigger implicit synchronizations that are redundant
when you are manually controlling the clock, so bypassing them significantly
speeds up your test runtimes.
Usage guidelines
- Manual clock management: Use this API when
mainClock.autoAdvanceis set tofalseand the UI is in a known, stable state for the current frame. - UI thread execution: To ensure the stability of the UI tree, call
runWithoutImplicitWaiton the UI thread, such as withrunOnUiThread. Running it off the UI thread exposes your test to race conditions and stale state reads. - Read-only assertions: The block should strictly contain read-only assertions. Any actions that mutate state should be performed outside of this block.
Example
@Test fun runWithoutImplicitWaitSample() = runComposeUiTest { setContent { MainScreen() } mainClock.autoAdvance = false // Trigger an animation onNodeWithText("Start Animation").performClick() // Step through the animation frame-by-frame while (hasPendingWork()) { mainClock.advanceTimeByFrame() waitForIdle() runOnUiThread { // Suppress implicit synchronization inside this block to avoid redundant // waits on each node query, making the frame assertions execute much faster. runWithoutImplicitWait { val box1 = onNodeWithTag("Box1").fetchSemanticsNode() val box2 = onNodeWithTag("Box2").fetchSemanticsNode() val box3 = onNodeWithTag("Box3").fetchSemanticsNode() // Assert the exact intermediate state of all three properties for this frame assert(box1.boundsInRoot.right <= box2.boundsInRoot.left) assert(box2.boundsInRoot.right <= box3.boundsInRoot.left) } } } }
आपके लिए सुझाव
- ध्यान दें: JavaScript बंद होने पर लिंक का टेक्स्ट दिखता है
- लिखे जा रहे ईमेल के लेआउट की जांच करना
- अन्य बातें
- ऐनिमेशन को पसंद के मुताबिक बनाना