Posted on

Android Testing pt 3.2 | Espresso Idling Resources With Firebase

android idling resources firebase

Designed by Freepik


Most Espresso UI verification works just as you would do yourself, but some needs extra work from you. This post is about Espresso Idling Resources

Idling Resources

Let’s say we have this test code

Verification happens instantaneously, but UI cannot possibly respond that quickly, even though it might look like. But Espresso handles that by default for us and waits UI to reflect changes, only then verify it

Espresso even waits for  Handler#postDelayed method

But stuff like networking doesn’t work out of the box: OkHttp, Firebase etc. In those cases, you need explicitly tell Espresso when to wait


Add those dependencies

And I copied the code from this tutorial. The way it works is Espresso constantly calls IdlingResource#isIdleNow method. And you return true if you want Espresso to wait (when you’re doing some networking), false if Espresso should continue running tests


Now the downside of it is that you have to put test-related code to your application’s code. Here’s an example of Firebase sign up with email. When complete, it changes the button text

And then register this idling resource in our test

If we don’t use Idling resource – tests would just call Firebase methods and without waiting for results start UI verification, which will fail the test

Another way

Now there’s another much simpler way to solve this problem, without any Idling resources. By using Thread.sleep method in your test

This way there’s no test-related code in your application and it’s very fast to add. But there’re few problems here:

  1. You don’t know what time you should wait. 100 or 500 ms might be not enough (your test will fail) or even seconds. So I would set about 5 seconds to be sure. Which leads to the second problem
  2. Your tests will get much slower because you take extra sleep time when you don’t need
  3. And again, what if you set your sleep time for not enough time (your internet was slow) –  tests aren’t reliable

What’s going on

Obviously, there’s a lot of changes going on in this area, that’s just the current state of this problem. For example with OkHttp – there’s no need for you to write any increment/decrement, implement your own Idling Resource, put it in your application code

You just add those lines of code in your test. Nothing else. I didn’t find any such solution for Firebase, so maybe it will come later. For now, if you want to test Firebase in your Espresso tests – you need that counter

Ok, I hope it helps. You can get the source code here, subscribe. And here’re some useful resources: