Compose cung cấp ComposeTestRule cho phép bạn viết các phép kiểm thử cho ảnh động theo cách thức xác định với toàn quyền kiểm soát trên đồng hồ kiểm thử. Tính năng này cho phép bạn xác minh các giá trị ảnh động trung gian. Ngoài ra, một thử nghiệm ảnh động có thể chạy nhanh hơn
thời lượng thực tế của ảnh động.
ComposeTestRule cho thấy đồng hồ thử nghiệm là mainClock. Bạn có thể thiết lập thuộc tính
autoAdvance thành false để kiểm soát đồng hồ trong mã thử nghiệm. Sau khi
bắt đầu ảnh động mà bạn muốn thử nghiệm, đồng hồ có thể được di chuyển về phía trước bằng
advanceTimeBy.
Cần lưu ý là advanceTimeBy không di chuyển đồng hồ một cách chính xác theo
thời lượng đã chỉ định. Thay vào đó, hệ thống sẽ làm tròn lên thời lượng gần nhất bằng
hệ số thời gian kết xuất khung hình.
@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() }
Tối ưu hoá các thử nghiệm ảnh động
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) } } } }
Đề xuất cho bạn
- Lưu ý: văn bản có đường liên kết sẽ hiện khi JavaScript tắt
- Kiểm thử bố cục Compose
- Những điểm khác cần cân nhắc
- Tuỳ chỉnh ảnh động