Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support completable animations in Compose tests #2051

Merged
merged 1 commit into from
Apr 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.runtime.withFrameNanos
import com.airbnb.lottie.LottieComposition
import kotlinx.coroutines.CancellationException
import kotlinx.coroutines.NonCancellable
Expand Down Expand Up @@ -255,10 +256,22 @@ private class LottieAnimatableImpl : LottieAnimatable {
}
}

// We use withInfiniteAnimationFrameNanos because it allows tests to add a CoroutineContext
// element that will cancel infinite transitions instead of preventing composition from ever going idle.
private suspend fun doFrame(iterations: Int): Boolean = withInfiniteAnimationFrameNanos { frameNanos ->
val composition = composition ?: return@withInfiniteAnimationFrameNanos true
private suspend fun doFrame(iterations: Int): Boolean {
return if (iterations == LottieConstants.IterateForever) {
// We use withInfiniteAnimationFrameNanos because it allows tests to add a CoroutineContext
// element that will cancel infinite transitions instead of preventing composition from ever going idle.
withInfiniteAnimationFrameNanos { frameNanos ->
onFrame(iterations, frameNanos)
}
} else {
withFrameNanos { frameNanos ->
onFrame(iterations, frameNanos)
}
}
}

private fun onFrame(iterations: Int, frameNanos: Long): Boolean {
val composition = composition ?: return true
val dNanos = if (lastFrameNanos == AnimationConstants.UnspecifiedTime) 0L else (frameNanos - lastFrameNanos)
lastFrameNanos = frameNanos

Expand All @@ -279,7 +292,7 @@ private class LottieAnimatableImpl : LottieAnimatable {
if (iteration + dIterations > iterations) {
progress = endProgress
iteration = iterations
return@withInfiniteAnimationFrameNanos false
return false
}
iteration += dIterations
val progressPastEndRem = progressPastEndOfIteration - (dIterations - 1) * durationProgress
Expand All @@ -289,7 +302,7 @@ private class LottieAnimatableImpl : LottieAnimatable {
}
}

true
return true
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.airbnb.lottie.samples

import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.runtime.getValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.test.junit4.createAndroidComposeRule
import com.airbnb.lottie.LottieCompositionFactory
import com.airbnb.lottie.compose.LottieAnimation
import com.airbnb.lottie.compose.animateLottieCompositionAsState
import com.airbnb.lottie.sample.compose.ComposeActivity
import com.airbnb.lottie.sample.compose.R
import org.junit.Rule
import org.junit.Test

class WalkthroughAnimationTest {

@get:Rule
val composeTestRule = createAndroidComposeRule(ComposeActivity::class.java)

@Test
fun testWalkthroughCompletes() {
val composition = LottieCompositionFactory.fromRawResSync(composeTestRule.activity, R.raw.walkthrough).value!!
var animationCompleted = true

composeTestRule.setContent {
val progress by animateLottieCompositionAsState(composition, iterations = 1)

Box(
modifier = Modifier
.fillMaxSize()
) {
LottieAnimation(
composition,
progress,
)
}

if (progress == 1f) {
animationCompleted = true
}
}

composeTestRule.mainClock.advanceTimeUntil { animationCompleted }
}
}