Skip to content

Instantly share code, notes, and snippets.

@suclike
Forked from bolhoso/gist:8403087
Created October 21, 2015 17:10
Show Gist options
  • Save suclike/eca6a587abfb187a310d to your computer and use it in GitHub Desktop.
Save suclike/eca6a587abfb187a310d to your computer and use it in GitHub Desktop.
An Android Espresso IdlingResource to monitor Volley requests (bot not working yet..)
public final class VolleyIdlingResource implements IdlingResource {
private static final String TAG = "VolleyIdlingResource";
private final String resourceName;
// written from main thread, read from any thread.
private volatile ResourceCallback resourceCallback;
private Field mCurrentRequests;
private RequestQueue mVolleyRequestQueue;
public VolleyIdlingResource(String resourceName) throws SecurityException, NoSuchFieldException {
this.resourceName = checkNotNull(resourceName);
mVolleyRequestQueue = MyApplication.getRequestQueue();
mCurrentRequests = RequestQueue.class.getDeclaredField("mCurrentRequests");
mCurrentRequests.setAccessible(true);
}
@Override
public String getName() {
return resourceName;
}
@Override
public boolean isIdleNow() {
try {
synchronized (set) {
Set<Request> set = (Set<Request>) mCurrentRequests.get(mVolleyRequestQueue);
int count = set.size();
if (set != null) {
if (count == 0) {
Log.d(TAG, "Volley is idle now! with: " + count);
resourceCallback.onTransitionToIdle();
} else {
Log.d(TAG, "Not idle... " +count);
}
return count == 0;
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Log.d(TAG, "something wrong :D.. ");
return true;
}
@Override
public void registerIdleTransitionCallback(ResourceCallback resourceCallback) {
this.resourceCallback = resourceCallback;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment