Last active
October 7, 2017 05:09
-
-
Save bolhoso/8403087 to your computer and use it in GitHub Desktop.
An Android Espresso IdlingResource to monitor Volley requests (bot not working yet..)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
Thanks, I've updated the gist!
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
Espresso needs to be notified that the resource is idle with resourceCallback.onTransitionToIdle().