Reputation: 31
I'm testing an Android application with a meter graphic where an element on the screen (the meter) is redrawn every second. The idea is that I tap a start button and the meter turns on, and when I tap a stop button the meter turns off.
Manually it works fine, but when I run my Espresso tests, the test hangs after I tap start because the app never idles, as it is being redrawn every second. As expected, I get:
android.support.test.espresso.AppNotIdleException: Looped for 13452 iterations over 60 SECONDS. The following Idle Conditions failed .
I found noActivity() in the Android Developers docs, thinking that may be helpful, but I can't figure out how to use it properly nor can I find any good examples. The stop button is clearly on the page but this step is bypassed and it goes straight to tearDown(), which tells me that I'm not using it correctly or I don't understand what it's doing:
onView(withId(R.id.stop_button)).noActivity().perform()
So how do I overcome this? Is there an easy way to tell Espresso to not use idling resources for a certain test, or part of it? Do I need to write an idling resource that says when the stop button is found, the app is idle? Is there another method I could be missing?
FWIW I'm using Kotlin, but I welcome Java answers as well since they're so similar. Thanks!
Upvotes: 1
Views: 589
Reputation: 14700
In your custom view, retrieve the system setting ANIMATOR_DURATION_SCALE
:
private fun getAnimatorSpeed() =
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
getAnimatorSpeedNew()
} else {
getAnimatorSpeedOld()
}
@RequiresApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
private fun getAnimatorSpeedNew() = Settings.Global.getFloat(
context.contentResolver,
Settings.Global.ANIMATOR_DURATION_SCALE,
1f)
@Suppress("DEPRECATION")
private fun getAnimatorSpeedOld() = Settings.System.getFloat(
context.contentResolver,
Settings.System.ANIMATOR_DURATION_SCALE,
1f)
Disable redrawing in your custom view when animator speed is zero. While you're at it, you may want to adjust your animation to respect the system animator speed.
Before you run tests, set system animator speed to zero by going to Settings > Developer options on your device.
Upvotes: 1