Skip to content

Commit

Permalink
Update FragmentVisibilityTests (#1980)
Browse files Browse the repository at this point in the history
No behavior changes but in an upcoming PR, pausing/resuming on attach/detach will happen in LottieDrawable instead of LottieAnimationView. The drawable gets notified slightly later than the view so I've changed the idle resource lifecycle to make it work with both.
  • Loading branch information
gpeal committed Jan 9, 2022
1 parent 3ee8813 commit 8096678
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 19 deletions.
Expand Up @@ -119,7 +119,7 @@ class FragmentVisibilityTests {
val animationView = fragment.requireView().findViewById<LottieAnimationView>(R.id.animation_view)
// Wait for the animation view.
// We have to use a property reference because the Fragment isn't resumed.
assertTrue(fragment.animationView.isAnimating)
assertTrue(animationView.isAnimating)
}
}

Expand Down Expand Up @@ -331,8 +331,6 @@ class FragmentVisibilityTests {
animationWasPlayed = true
IdlingRegistry.getInstance().register(LottieIdlingResource(this, name = "Lottie ${Random.nextFloat()}"))
}
} else {
IdlingRegistry.getInstance().register(LottieIdlingAnimationResource(animationView, name = "Lottie finished animation ${Random.nextFloat()}"))
}
}

Expand All @@ -352,7 +350,10 @@ class FragmentVisibilityTests {
scenario.onFragment { it.requireView().scrollBy(0, -10_000) }
scenario.onFragment { assertTrue(it.animationView!!.isAnimating) }
onView(withId(R.id.animation_view)).check(matches(isDisplayed()))
scenario.onFragment { assertFalse(it.animationView!!.isAnimating) }
scenario.onAnimationEnded()
scenario.onFragment { fragment ->
assertFalse(fragment.animationView!!.isAnimating)
}
}

@Test
Expand Down Expand Up @@ -395,8 +396,6 @@ class FragmentVisibilityTests {
animationWasPlayed = true
IdlingRegistry.getInstance().register(LottieIdlingResource(this, name = "Lottie ${Random.nextFloat()}"))
}
} else {
IdlingRegistry.getInstance().register(LottieIdlingAnimationResource(animationView, name = "Lottie finished animation ${Random.nextFloat()}"))
}
}

Expand All @@ -415,7 +414,7 @@ class FragmentVisibilityTests {
scenario.onFragment { assertFalse(it.animationView!!.isAnimating) }
scenario.onFragment { it.requireView().scrollBy(0, -10_000) }
scenario.onFragment { assertTrue(it.animationView!!.isAnimating) }
onView(withId(R.id.animation_view)).check(matches(isDisplayed()))
scenario.onAnimationEnded()
scenario.onFragment { assertFalse(it.animationView!!.isAnimating) }
scenario.onFragment { it.requireView().scrollBy(0, 10_000) }
scenario.onFragment { it.requireView().scrollBy(0, -10_000) }
Expand Down Expand Up @@ -460,5 +459,12 @@ class FragmentVisibilityTests {
}
}

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

private val Fragment.animationView get() = requireView().findViewById<LottieAnimationView>(R.id.animation_view)
}
Expand Up @@ -5,33 +5,29 @@ import androidx.test.espresso.IdlingRegistry
import androidx.test.espresso.IdlingResource
import com.airbnb.lottie.LottieAnimationView

class LottieIdlingAnimationResource(animationView: LottieAnimationView?, private val name: String = "Lottie") : IdlingResource {
class LottieIdlingAnimationResource(animationView: LottieAnimationView, private val name: String = "Lottie") : IdlingResource {

init {
animationView?.addAnimatorListener(object : AnimatorListenerAdapter() {
override fun onAnimationStart(animation: Animator) {
isIdle = false
}
private var hasEnded = false
private var callback: IdlingResource.ResourceCallback? = null

init {
animationView.addAnimatorListener(object : AnimatorListenerAdapter() {
override fun onAnimationEnd(animation: Animator) {
isIdle = true
hasEnded = true
callback?.onTransitionToIdle()
animationView.removeAllAnimatorListeners()
IdlingRegistry.getInstance().unregister(this@LottieIdlingAnimationResource)
}
})
}

private var callback: IdlingResource.ResourceCallback? = null
private var isIdle = animationView?.isAnimating?.not() ?: true


override fun getName() = name

override fun isIdleNow() = isIdle
override fun isIdleNow() = hasEnded

override fun registerIdleTransitionCallback(callback: IdlingResource.ResourceCallback?) {
this.callback = callback
if (isIdle) callback?.onTransitionToIdle()
if (isIdleNow) callback?.onTransitionToIdle()
}
}

0 comments on commit 8096678

Please sign in to comment.