تست انیمیشن ها
با مجموعهها، منظم بمانید
ذخیره و طبقهبندی محتوا براساس اولویتهای شما.
Compose 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()
}
{% کلمه به کلمه %}
{% آخر کلمه %} برای شما توصیه می شود
{% کلمه به کلمه %} {% آخر کلمه %}
محتوا و نمونه کدها در این صفحه مشمول پروانههای توصیفشده در پروانه محتوا هستند. جاوا و OpenJDK علامتهای تجاری یا علامتهای تجاری ثبتشده Oracle و/یا وابستههای آن هستند.
تاریخ آخرین بهروزرسانی 2025-08-27 بهوقت ساعت هماهنگ جهانی.
[[["درک آسان","easyToUnderstand","thumb-up"],["مشکلم را برطرف کرد","solvedMyProblem","thumb-up"],["غیره","otherUp","thumb-up"]],[["اطلاعاتی که نیاز دارم وجود ندارد","missingTheInformationINeed","thumb-down"],["بیشازحد پیچیده/ مراحل بسیار زیاد","tooComplicatedTooManySteps","thumb-down"],["قدیمی","outOfDate","thumb-down"],["مشکل ترجمه","translationIssue","thumb-down"],["مشکل کد / نمونهها","samplesCodeIssue","thumb-down"],["غیره","otherDown","thumb-down"]],["تاریخ آخرین بهروزرسانی 2025-08-27 بهوقت ساعت هماهنگ جهانی."],[],[],null,["Compose offers `ComposeTestRule` that allows you to write tests for animations\nin a deterministic manner with full control over the test clock. This allows you\nto verify intermediate animation values. In addition, a test can run quicker\nthan the actual duration of the animation.\n\n`ComposeTestRule` exposes its test clock as `mainClock`. You can set the\n`autoAdvance` property to false to control the clock in your test code. After\ninitiating the animation you want to test, the clock can be moved forward with\n`advanceTimeBy`.\n\nOne thing to note here is that `advanceTimeBy` doesn't move the clock exactly by\nthe specified duration. Rather, it rounds it up to the nearest duration that is\na multiplier of the frame duration.\n\n\n```kotlin\n@get:Rule\nval rule = createComposeRule()\n\n@Test\nfun testAnimationWithClock() {\n // Pause animations\n rule.mainClock.autoAdvance = false\n var enabled by mutableStateOf(false)\n rule.setContent {\n val color by animateColorAsState(\n targetValue = if (enabled) Color.Red else Color.Green,\n animationSpec = tween(durationMillis = 250)\n )\n Box(Modifier.size(64.dp).background(color))\n }\n\n // Initiate the animation.\n enabled = true\n\n // Let the animation proceed.\n rule.mainClock.advanceTimeBy(50L)\n\n // Compare the result with the image showing the expected result.\n // `assertAgainGolden` needs to be implemented in your code.\n rule.onRoot().captureToImage().assertAgainstGolden()\n}https://github.com/android/snippets/blob/5673ffc60b614daf028ee936227128eb8c4f9781/compose/snippets/src/androidTest/java/com/example/compose/snippets/animation/AnimationTestingSnippets.kt#L57-L82\n```\n\n\u003cbr /\u003e\n\nRecommended for you\n\n- Note: link text is displayed when JavaScript is off\n- [Testing your Compose layout](/develop/ui/compose/testing)\n- [Other considerations](/develop/ui/compose/migrate/other-considerations)\n- [Customize animations {:#customize-animations}](/develop/ui/compose/animation/customize)"]]