Skip to content

Commit

Permalink
Add a test, remove an invalid test and add sample animations to the i…
Browse files Browse the repository at this point in the history
…ssue repro apps (#1992)
  • Loading branch information
gpeal committed Jan 14, 2022
1 parent f79c12f commit b348862
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ import android.os.Bundle
import androidx.activity.compose.setContent
import androidx.appcompat.app.AppCompatActivity
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import com.airbnb.lottie.compose.LottieAnimation
import com.airbnb.lottie.compose.LottieCompositionSpec
import com.airbnb.lottie.compose.animateLottieCompositionAsState
import com.airbnb.lottie.compose.rememberLottieComposition

class ComposeIssueReproActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
Expand All @@ -16,6 +20,8 @@ class ComposeIssueReproActivity : AppCompatActivity() {

@Composable
fun Content() {
LottieAnimation(null)
val composition by rememberLottieComposition(LottieCompositionSpec.RawRes(R.raw.heart))
val progress by animateLottieCompositionAsState(composition)
LottieAnimation(composition, progress)
}
}
1 change: 1 addition & 0 deletions issue-repro-compose/src/main/res/raw/heart.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions issue-repro/src/main/res/raw/heart.json

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -105,14 +105,6 @@ public void testNullMultipleTimesSync() {
assertNotSame(task1, task2);
}

@Test
public void testCacheWorks() {
JsonReader reader = JsonReader.of(buffer(source(getNeverCompletingInputStream())));
LottieTask<LottieComposition> task1 = LottieCompositionFactory.fromJsonReader(reader, "foo");
LottieTask<LottieComposition> task2 = LottieCompositionFactory.fromJsonReader(reader, "foo");
assertSame(task1, task2);
}

@Test
public void testZeroCacheWorks() {
JsonReader reader = JsonReader.of(buffer(source(getNeverCompletingInputStream())));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,51 @@ class FragmentVisibilityTests {
LottieCompositionCache.getInstance().clear()
}

@Test
fun testRecyclerViewCanAutoPlayInOnBindRebind() {
class TestFragment : Fragment() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return RecyclerView(requireContext()).apply {
layoutManager = LinearLayoutManager(requireContext(), LinearLayoutManager.VERTICAL, false)
// Setting itemAnimator to null is important for this test in order to
// prevent the recyclerview from creating an additional viewholder for the
// purposes of animation.
itemAnimator = null
adapter = object : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
return object : RecyclerView.ViewHolder(LottieAnimationView(parent.context).apply {
id = R.id.animation_view
setAnimation(R.raw.heart)
IdlingRegistry.getInstance().register(LottieIdlingResource(this))
}) {}
}

override fun getItemCount(): Int = 1

override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
(holder.itemView as LottieAnimationView).apply {
// Cancel first, then play. This prevents the animation from
// carrying over from the initial binding.
cancelAnimation()
playAnimation()
}
}
}
}
}
}

val fragmentScenario = launchFragmentInContainer<TestFragment>()
// I wasn't able to figure out exactly what was needed to create an idling resource.
// Waiting for [RecyclerView.doOnLayout] was insufficient.
Thread.sleep(500)
fragmentScenario.onFragment { fragment ->
(fragment.view as RecyclerView).adapter!!.notifyItemChanged(0)
}
Thread.sleep(500)
onView(withId(R.id.animation_view)).check(matches(isAnimating()))
}

@Test
fun testAutoPlay() {
class TestFragment : Fragment() {
Expand Down Expand Up @@ -458,7 +503,8 @@ class FragmentVisibilityTests {

private fun <T : Fragment> FragmentScenario<T>.onAnimationEnded() {
onFragment { fragment ->
IdlingRegistry.getInstance().register(LottieIdlingAnimationResource(fragment.animationView!!, name = "Lottie finished animation ${Random.nextFloat()}"))
IdlingRegistry.getInstance()
.register(LottieIdlingAnimationResource(fragment.animationView!!, name = "Lottie finished animation ${Random.nextFloat()}"))
}
onIdle()
}
Expand Down

0 comments on commit b348862

Please sign in to comment.