Android Espresso Test Hangs With Indeterminate ProgressBar

I encountered a fun problem today... My Espresso tests started hanging after I added a ProgressDialog into my layout that was visible when the Activity started.

This was the error in the logs:

Could not launch intent Intent { act=android.intent.action.MAIN flg=0x14000000 cmp=co.blah/.ui.activity.MainActivity } within 45 seconds. Perhaps the main thread has not gone idle within a reasonable amount of time? There could be an animation or something constantly repainting the screen. Or the activity is doing network calls on creation? See the threaddump logs. For your reference the last time the event queue was idle before your activity launch request was 1476277650154 and now the last time the queue went idle was: 1476277650154. If these numbers are the same your activity might be hogging the event queue.

It was only happening on devices running Lollipop (Android 5.0, 5.1) or higher. So Marshmallow (6.0) and Nougat (7.0) as well.

Apparently the indeterminate ProgressBar animation causes Espresso to think things are still happening. And it just waits. And waits. Until it gets killed after 45 seconds. Great. This happens even though animations are turned off in the developer options.

There were a couple of workarounds I found on the internet, but I felt they had their own issues so I've come up with my own.

Just replace uses of ProgressDialog with TestableIndeterminateProgressBar from this gist (and make sure you have animations disabled in the developer options, which of course you're already doing).