fun TextView.setTextWithFade(text: String, duration: Long = 300) {
this.animate().alpha(0f).setDuration(duration).withEndAction {
this.text = text
this.animate().alpha(1f).setDuration(duration).start()
}.start()
}
Hello, I have a simple Kotlin extension function for a TextView to change text with animation. There is three steps in this animation:
- Fades out old text
- Change the text in a TextView
- Fades the text back in
What is the best way to write Unit Test that checks, whether the text was changed after the fade out animation?
@Test
fun setTextWithFadeTest() {
val context = ApplicationProvider.getApplicationContext<Context>()
val textView = TextView(context)
textView.text = "test1"
textView.setTextWithFade("test2", duration = 10)
val handler = Handler()
handler.postDelayed({
println("Text: ${textView.text}, alpha: ${textView.alpha}")
assert(textView.text == "test2")
}, 300)
Robolectric.getForegroundThreadScheduler().advanceBy(300, TimeUnit.MILLISECONDS)
}
This is a code how I try to achieve it. That handler prints:
Text: test1, alpha: 1.0
So it seems that animation don't even started.