Espresso test error: AppNotIdleException

11,293

Solution 1

I have been struggling with this problem for the last few days.
Here is a method that I used to identify "violators":

private void dumpThreads() {
    int activeCount = Thread.activeCount();
    Thread[] threads = new Thread[activeCount];
    Thread.enumerate(threads);
    for (Thread thread : threads) {
        System.err.println(thread.getName() + ": " + thread.getState());
        for (StackTraceElement stackTraceElement : thread.getStackTrace()) {
            System.err.println("\t" + stackTraceElement);
        }
    }
}

In my case, Facebook SDK was using the AsyncTask thread pool.

Solution 2

As per answer by MaciejGórski to the similar question:

It was a bug in my app code, where SwipeRefreshLayout animated itself indefinitely. Due to a bug in this component, the refresh state was not even showing.

Solution 3

In my case this problem was happening with AnimatedVectorDrawable and caused by an objectAnimator that was set to repeat the animation infinitely (android:repeatCount="infinite"). .

The problem was also present only on older platform versions. Tests were perfectly working on Android 9 while the problem was reproducible on Android 5 and 6 (not sure about 7 and 8 at the moment).

I believe, the root cause of the problem is the same as for indeterminate progress bars (covered in this SO question). However, I haven't found any nice solution, only workarounds.

One of the workarounds is to detect that the animation is turned off (animator duration is 0) in the setting and don't start the animation. Of course, this only works for platform versions where the animation does not autostart.

private fun startIconAnimation(imageView: ImageView) {
    if (areAnimationsEnabled()) {
        (imageView.drawable as Animatable).start()
    }
}

private fun areAnimationsEnabled(): Boolean {
    val animatorDurationScale = Settings.Global.getFloat(
        requireContext().contentResolver,
        Settings.Global.ANIMATOR_DURATION_SCALE,
        1.0f
    )
    return animatorDurationScale != 0.0f
}

Note: API level 26 introduced a static method ValueAnimator.areAnimatorsEnabled() which would have been handy if the problem was not happening only on the older platform versions.

Share:
11,293

Related videos on Youtube

user5174803
Author by

user5174803

Updated on June 14, 2022

Comments

  • user5174803
    user5174803 about 2 years

    I turned off all animations on developer options. But I still get this exception when trying to click on one of the buttons.

    My app is indeed active and not idle entirely, but I can't change it.

    android.support.test.espresso.AppNotIdleException: Looped for 6930
    iterations over 60 SECONDS. The following Idle Conditions failed .
     at dalvik.system.VMStack.getThreadStackTrace(Native Method)
     at java.lang.Thread.getStackTrace(Thread.java:580)
     at android.support.test.espresso.base.DefaultFailureHandler.getUserFriendlyError(DefaultFailureHandler.java:92)
     at android.support.test.espresso.base.DefaultFailureHandler.handle(DefaultFailureHandler.java:56)
     at android.support.test.espresso.ViewInteraction.runSynchronouslyOnUiThread(ViewInteraction.java:184)
     at android.support.test.espresso.ViewInteraction.doPerform(ViewInteraction.java:115)
     at android.support.test.espresso.ViewInteraction.perform(ViewInteraction.java:87)
    
  • W4R10CK
    W4R10CK almost 8 years
    @Be_Negative yes, it has to go to its rightful owner. ~Arigato
  • Krishnakant
    Krishnakant almost 8 years
    @Be_Negative But I don't have any SwipeRefereshLayout or RecyclerView in my layout.
  • Apurva Sharma
    Apurva Sharma over 7 years
    u can refer to this answer as well stackoverflow.com/questions/38797738/…
  • Rich Ehmer
    Rich Ehmer about 7 years
    You can also pause the app (in debug mode) when it hangs and look at all the AsyncTasks that are in the 'RUNNING' state.
  • Gapp
    Gapp about 5 years
    How did you deal with this issue? I have a similar problem that I believe is the FacebookInitProvider making a call that stalls and causes the exception but have been unable to find away to avoid the call being made.