title: How to use Espresso Idling Resource for network calls
body: <p>Im trying to use Espresso to test my UI. When i login into my application, i do a call to Parse API (network call) to verify the username and password. If all is well the user gets directed to a new activity. I want to test this, but i cant seems to work with the idle resource thing. </p>
  
  <p>Code: </p>
  
  <pre><code>public class ApplicationTest extends ActivityInstrumentationTestCase2&lt;LoginActivity&gt; {
  
  
  private CountingIdlingResource fooServerIdlingResource;
  
  public ApplicationTest() {
      super(LoginActivity.class);
  }
  
  @Before
  public void setUp() throws Exception {
      super.setUp();
      injectInstrumentation(InstrumentationRegistry.getInstrumentation());
      getActivity();
      CountingIdlingResource countingResource = new CountingIdlingResource("FooServerCalls");
      this.fooServerIdlingResource = countingResource;
      Espresso.registerIdlingResources(countingResource);
  }
  
  
  public void testChangeText_sameActivity() {
      // Type text and then press the button.
      onView(withId(R.id.username))
              .perform(typeText("s@s.nl"), closeSoftKeyboard());
      onView(withId(R.id.password))
              .perform(typeText("s"), closeSoftKeyboard());
  
      if(performClick())
          onView(withId(R.id.main_relative_layout))
                  .check(matches(isDisplayed()));
      // Check that the text was changed.
  }
  
  public boolean performClick(){
      fooServerIdlingResource.increment();
      try {
          onView(withId(R.id.login)).perform(click());
          return true;
      } finally {
          fooServerIdlingResource.decrement();
      }
  }
  
  
  @SuppressWarnings("javadoc")
  public final class CountingIdlingResource implements IdlingResource {
      private static final String TAG = "CountingIdlingResource";
      private final String resourceName;
      private final AtomicInteger counter = new AtomicInteger(0);
      private final boolean debugCounting;
  
      // written from main thread, read from any thread.
      private volatile ResourceCallback resourceCallback;
  
      // read/written from any thread - used for debugging messages.
      private volatile long becameBusyAt = 0;
      private volatile long becameIdleAt = 0;
  
      /**
       * Creates a CountingIdlingResource without debug tracing.
       *
       * @param resourceName the resource name this resource should report to Espresso.
       */
      public CountingIdlingResource(String resourceName) {
          this(resourceName, false);
      }
  
      /**
       * Creates a CountingIdlingResource.
       *
       * @param resourceName  the resource name this resource should report to Espresso.
       * @param debugCounting if true increment &amp; decrement calls will print trace information to logs.
       */
      public CountingIdlingResource(String resourceName, boolean debugCounting) {
          this.resourceName = checkNotNull(resourceName);
          this.debugCounting = debugCounting;
      }
  
      @Override
      public String getName() {
          return resourceName;
      }
  
      @Override
      public boolean isIdleNow() {
          return counter.get() == 0;
      }
  
      @Override
      public void registerIdleTransitionCallback(ResourceCallback resourceCallback) {
          this.resourceCallback = resourceCallback;
      }
  
      /**
       * Increments the count of in-flight transactions to the resource being monitored.
       * &lt;p/&gt;
       * This method can be called from any thread.
       */
      public void increment() {
          int counterVal = counter.getAndIncrement();
          if (0 == counterVal) {
              becameBusyAt = SystemClock.uptimeMillis();
          }
  
          if (debugCounting) {
              Log.i(TAG, "Resource: " + resourceName + " in-use-count incremented to: " + (counterVal + 1));
          }
      }
  
      /**
       * Decrements the count of in-flight transactions to the resource being monitored.
       * &lt;p/&gt;
       * If this operation results in the counter falling below 0 - an exception is raised.
       *
       * @throws IllegalStateException if the counter is below 0.
       */
      public void decrement() {
          int counterVal = counter.decrementAndGet();
  
          if (counterVal == 0) {
              // we've gone from non-zero to zero. That means we're idle now! Tell espresso.
              if (null != resourceCallback) {
                  resourceCallback.onTransitionToIdle();
              }
              becameIdleAt = SystemClock.uptimeMillis();
          }
  
          if (debugCounting) {
              if (counterVal == 0) {
                  Log.i(TAG, "Resource: " + resourceName + " went idle! (Time spent not idle: " +
                          (becameIdleAt - becameBusyAt) + ")");
              } else {
                  Log.i(TAG, "Resource: " + resourceName + " in-use-count decremented to: " + counterVal);
              }
          }
          checkState(counterVal &gt; -1, "Counter has been corrupted!");
      }
  
      /**
       * Prints the current state of this resource to the logcat at info level.
       */
      public void dumpStateToLogs() {
          StringBuilder message = new StringBuilder("Resource: ")
                  .append(resourceName)
                  .append(" inflight transaction count: ")
                  .append(counter.get());
          if (0 == becameBusyAt) {
              Log.i(TAG, message.append(" and has never been busy!").toString());
          } else {
              message.append(" and was last busy at: ")
                      .append(becameBusyAt);
              if (0 == becameIdleAt) {
                  Log.w(TAG, message.append(" AND NEVER WENT IDLE!").toString());
              } else {
                  message.append(" and last went idle at: ")
                          .append(becameIdleAt);
                  Log.i(TAG, message.toString());
              }
          }
      }
  }
  </code></pre>
  
  <p>}</p>
  
  <p>The exception i get now is the following: </p>
  
  <pre><code>ndroid.support.test.espresso.IdlingResourceTimeoutException: Wait for [FooServerCalls] to become idle timed out
  </code></pre>
  
  <p>When i run the test, the username and password are getting filled in but the perform click is never called and i get the exception after a few seconds. How should i implement the idle resource correctly?</p>
  
  <p>EDIT --</p>
  
  <p>I would recommend using Calabash for Android. Calabash works similar but doesn't need you to change your app code for testing. </p>