Created
October 23, 2013 09:27
-
-
Save mediavrog/7115422 to your computer and use it in GitHub Desktop.
Make Volley RequestQueue / ThreadPool work with Espresso Testing library
This file contains 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
package com.vuzz.snapdish.test.functional; | |
import android.os.Handler; | |
import com.android.volley.Cache; | |
import com.android.volley.Request; | |
import com.android.volley.RequestQueue; | |
import com.google.android.apps.common.testing.ui.espresso.contrib.CountingIdlingResource; | |
/** | |
* A dummy request queue which enables us to use the Volley Library properly with Espresso. | |
* Just switch the RequestQueue of your application to this one and it will take care of the rest. | |
* <p/> | |
* Implementation note: | |
* Unfortunately, Volley doesn't provide us with an interface for the RequestQueue, | |
* so we have to setup a 'null' request queue (see call to super() in the constructor) | |
* and use the composition pattern with the original queue. | |
* <p/> | |
* Usage: | |
* // get the main applications request queue (assuming a singleton RequestManager pattern) | |
* RequestQueue realRequestQueue = RequestManager.getRequestQueue(); | |
* // setup a countingidling resource as described in https://code.google.com/p/android-test-kit/wiki/EspressoSamples#Using_registerIdlingResource_to_synchronize_with_custom_resource | |
* CountingIdlingResource countingResource = new CountingIdlingResource("Volley"); | |
* // create the decorator and configur our ap to use it | |
* RequestManager.INSTANCE.setRequestQueue(new DecoratedRequestQueue(realRequestQueue, countingResource)); | |
* // finally register the idling resource with Espresso, so it will wait for the volley requests to finish | |
* Espresso.registerIdlingResources(countingResource); | |
* <p/> | |
* @author maikvlcek | |
* @since 5:14 PM - 10/23/13 | |
*/ | |
public class DecoratedRequestQueue extends RequestQueue { | |
private RequestQueue volleyRequestQueue; | |
private CountingIdlingResource volleyIdlingResource; | |
public DecoratedRequestQueue(RequestQueue requestQueue, CountingIdlingResource countingIdlingResource) { | |
super(null, null, 0, null); | |
volleyRequestQueue = requestQueue; | |
volleyIdlingResource = countingIdlingResource; | |
} | |
@Override | |
public Request add(final Request request) { | |
volleyIdlingResource.increment(); | |
new Handler().post(new Runnable() { | |
@Override | |
public void run() { | |
int pollInterval = 100; | |
int timeout = request.getTimeoutMs(); | |
for (int i = 0; i < timeout; i += pollInterval) { | |
if (request.hasHadResponseDelivered() || request.isCanceled()) break; | |
try { | |
Thread.sleep(pollInterval); | |
} catch (InterruptedException e) { | |
e.printStackTrace(); | |
} | |
} | |
volleyIdlingResource.decrement(); | |
} | |
}); | |
return volleyRequestQueue.add(request); | |
} | |
@Override | |
public int getSequenceNumber() { | |
return volleyRequestQueue.getSequenceNumber(); | |
} | |
@Override | |
public void start() { | |
volleyRequestQueue.start(); | |
} | |
@Override | |
public void stop() { | |
volleyRequestQueue.stop(); | |
} | |
@Override | |
public Cache getCache() { | |
return volleyRequestQueue.getCache(); | |
} | |
@Override | |
public void cancelAll(RequestFilter filter) { | |
volleyRequestQueue.cancelAll(filter); | |
} | |
@Override | |
public void cancelAll(final Object tag) { | |
volleyRequestQueue.cancelAll(tag); | |
} | |
} |
This file contains 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
/** | |
* Wraps the volley request queue. | |
* <p/> | |
* Usage: | |
* RequestManager.getRequestQueue().add(request); | |
* <p/> | |
* Created by maikvlcek on 6/19/13. | |
*/ | |
public enum RequestManager { | |
INSTANCE; | |
public final static String TAG = RequestManager.class.getName(); | |
private static RequestQueue sRequestQueue; | |
public void setRequestQueue(RequestQueue requestQueue) { | |
sRequestQueue = requestQueue; | |
} | |
public static RequestQueue getRequestQueue() { | |
if (sRequestQueue != null) { | |
return sRequestQueue; | |
} else { | |
throw new IllegalStateException("Not initialized"); | |
} | |
} | |
} |
Instead of polling, what do you think about this solution? https://github.com/bolhoso/espresso-volley-tests/blob/master/EspressoTest/src/com/example/espressovolley/test/MyTest.java (with the addition of wrapping isIdleNow
with synchronized (set)
?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Do I have to do all this stuff in my test classes setUp() methode?
Usage: