testcase for jokeloadertask

This commit is contained in:
danijoo
2015-11-05 23:02:53 +01:00
parent cf0ac3ba67
commit d03291918b
2 changed files with 67 additions and 0 deletions

View File

@@ -0,0 +1,32 @@
package com.udacity.gradle.builditbigger;
import android.test.AndroidTestCase;
import net.headlezz.jokesbackend.myApi.model.Joke;
public class JokeLoaderTaskTest extends AndroidTestCase implements JokeLoaderTask.JokeLoaderCallback {
SyncronizeTalker talker;
public void testJokeLoader() {
JokeLoaderTask task = new JokeLoaderTask(this);
talker = new SyncronizeTalker();
task.execute();
talker.doWait();
}
@Override
public void onJokeLoaded(Joke joke) {
assertNotNull(joke);
assertTrue(joke.getJoke() != null && !joke.getJoke().isEmpty());
talker.doNotify();
}
@Override
public void onError() {
assertTrue(false);
talker.doNotify();
}
}

View File

@@ -0,0 +1,35 @@
package com.udacity.gradle.builditbigger;
/**
* Helper class to wait and release in a testcase
* Source by Maxim Shoustin
* http://stackoverflow.com/questions/2321829/android-asynctask-testing-with-android-test-framework
*/
public class SyncronizeTalker {
public void doWait(long l){
synchronized(this){
try {
this.wait(l);
} catch(InterruptedException e) {
}
}
}
public void doNotify() {
synchronized(this) {
this.notify();
}
}
public void doWait() {
synchronized(this){
try {
this.wait();
} catch(InterruptedException e) {
}
}
}
}