Skip to content

Instantly share code, notes, and snippets.

@nalitzis
Created May 12, 2012 15:11
Show Gist options
  • Save nalitzis/2667108 to your computer and use it in GitHub Desktop.
Save nalitzis/2667108 to your computer and use it in GitHub Desktop.
Unit test with AsyncTask
public class NetworkTasksTest extends InstrumentationTestCase{
public void testGetMedia() throws Throwable{
final Context context = getInstrumentation().getTargetContext();
final CountDownLatch signal = new CountDownLatch(1);
final NetworkTasks networkTasks = new NetworkTasks(context, new GetMediaListener(signal));
runTestOnUiThread(new Runnable() {
public void run() {
Media media = new Media();
media.setId("179");
networkTasks.getMedia(media);
}
});
signal.await(30, TimeUnit.SECONDS);
}
private abstract class CallbackListener implements INetworkTasksListener{
private CountDownLatch signal;
CallbackListener(CountDownLatch signal){
this.signal = signal;
}
public void onSuccess(Media... medias) {
signal.countDown();
}
public void onFail(Throwable t) {
signal.countDown();
}
}
private abstract class CallbackListener implements INetworkTasksListener{
private CountDownLatch signal;
CallbackListener(CountDownLatch signal){
this.signal = signal;
}
public void onSuccess(Media... medias) {
signal.countDown();
}
public void onFail(Throwable t) {
signal.countDown();
}
}
private class GetMediaListener extends CallbackListener{
GetMediaListener(CountDownLatch signal){
super(signal);
}
public void onSuccess(Media... medias) {
Media m = medias[0];
Log.d(TAG, "media uri path is "+m.getMediaUrl());
Assert.assertTrue(true);
super.onSuccess(medias);
}
public void onFail(Throwable t) {
Assert.assertTrue(false);
super.onFail(t);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment