Skip to content

Instantly share code, notes, and snippets.

@bolhoso
Last active October 7, 2017 05:09
Show Gist options
  • Save bolhoso/8403087 to your computer and use it in GitHub Desktop.
Save bolhoso/8403087 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;
}
}
@grujo
Copy link

grujo commented Jan 16, 2014

Espresso needs to be notified that the resource is idle with resourceCallback.onTransitionToIdle().

@bolhoso
Copy link
Author

bolhoso commented Jan 27, 2014

Thanks, I've updated the gist!

@joelpet
Copy link

joelpet commented Jan 20, 2015

Currently synchronizing on the unknown symbol set, i.e. won't compile.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment