title: Delay test in Espresso Android without freezing main thread
body: <p>I am new in Espresso testing framework. Now I have a task to test some application which works with async backend. While the first activity starts some fragments appear only after they load. That can take several seconds, so the easiest way is just to wait 5-7 seconds. However using IdlingResource freezes main thread, so my backend data cannot load until waiting timeout is over.</p>
  
  <p>That's how I use IdlingResource:</p>
  
  <pre><code>public static class ElapsedTimeIdlingResource implements IdlingResource {
      private final long startTime;
      private final long waitingTime;
      private ResourceCallback resourceCallback;
  
      ElapsedTimeIdlingResource(long waitingTime) {
          this.startTime = System.currentTimeMillis();
          this.waitingTime = waitingTime;
      }
  
      @Override
      public String getName() {
          return ElapsedTimeIdlingResource.class.getName() + ":" + waitingTime;
      }
  
      @Override
      public boolean isIdleNow() {
          long elapsed = System.currentTimeMillis() - startTime;
          boolean idle = (elapsed &gt;= waitingTime);
          if (idle) resourceCallback.onTransitionToIdle();
          return idle;
      }
  
      @Override
      public void registerIdleTransitionCallback(ResourceCallback resourceCallback) {
          this.resourceCallback = resourceCallback;
      }
  }
  </code></pre>
  
  <p>That how I call it:</p>
  
  <pre><code>    long waitingTime = 5000;
  
      onView(withId(R.id.row_content)).check(matches(isDisplayed())).perform(click());
  
      IdlingPolicies.setMasterPolicyTimeout(waitingTime * 2, TimeUnit.MILLISECONDS);
      IdlingPolicies.setIdlingResourceTimeout(waitingTime * 2, TimeUnit.MILLISECONDS);
  
      IdlingResource idlingResource = new ElapsedTimeIdlingResource(waitingTime);
      IdlingRegistry.getInstance().register(idlingResource);
  
     // .... do some tests
  
     IdlingRegistry.getInstance().unregister(idlingResource);
  </code></pre>
  
  <p>How to delay test execution without blocking main thread?</p>