Testing progress bar on Android with Espresso : <p>The workflow should be the following:</p>

<ol>
<li>Activity starts</li>
<li>Progress bar is visible</li>
<li>Network request fires (idling resource is already registered so espresso knows how to wait for it).</li>
<li>Progress bar is hidden</li>
<li>Text from network is shown.</li>
</ol>

<p>Up to this point, I have written assertions for steps <strong>1, 3, 5</strong> and it works perfectly:</p>

<pre><code>onView(withText("foo 1"))
    .check(matches(isDisplayed()));
</code></pre>

<p>Problem is, I have no idea how to let espresso know to verify the visibility of progress bar <strong>before</strong> the request is made and <strong>after</strong> the request is made.</p>

<p>Consider the <code>onCreate()</code> method is the following:</p>

<pre><code>super.onCreate(...);
setContentView(...);

showProgressBar(true);
apiClient.getStuff(new Callback() {
    public void onSuccess() {
        showProgressBar(false);
    }
});
</code></pre>

<p>I have tried the following but it doesn't work:</p>

<pre><code>// Activity is launched at this point.
activityRule.launchActivity(new Intent());

// Up to this point, the request has been fired and response was 
// returned, so the progress bar is now GONE.
onView(withId(R.id.progress_bar))
   .check(matches(isDisplayed()));

onView(withId(R.id.progress_bar))
    .check(matches(not(isDisplayed())));
</code></pre>

<p>The reason this is happening is because, since the client is registered as an idling resource, espresso will wait until it is <strong>idle</strong> again before running the first <code>onView(...progressbar...)...</code> so I need a way to let espresso know to run that <em>BEFORE</em> going to idle.</p>

<p><strong>EDIT:</strong> this doesn't work either:</p>

<pre><code>idlingResource.registerIdleTransitionCallback(new IdlingResource.ResourceCallback() {
        @Override
        public void onTransitionToIdle() {
            onView(withId(R.id.progress_bar))
                    .check(matches(isDisplayed()));
        }
    });
</code></pre>
