تتيح لك 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() }
أفلام مُقترَحة لك
- ملاحظة: يتم عرض نص الرابط عندما تكون JavaScript غير مفعّلة
- اختبار تنسيق "أنا من كتب هذا المحتوى"
- اعتبارات أخرى
- تخصيص الصور المتحركة {:#customize-animations}