2014-07-10 22:37:37 -04:00
|
|
|
package org.bspeice.minimalbible.test.activity.downloader.manager;
|
|
|
|
|
2014-07-17 17:42:31 -04:00
|
|
|
import android.util.Log;
|
|
|
|
|
2014-07-10 22:37:37 -04:00
|
|
|
import junit.framework.TestCase;
|
|
|
|
|
|
|
|
import org.bspeice.minimalbible.Injector;
|
|
|
|
import org.bspeice.minimalbible.activity.downloader.manager.BookDownloadManager;
|
|
|
|
import org.bspeice.minimalbible.activity.downloader.manager.DLProgressEvent;
|
|
|
|
import org.bspeice.minimalbible.activity.downloader.manager.RefreshManager;
|
2014-07-17 17:42:31 -04:00
|
|
|
import org.crosswire.common.progress.JobManager;
|
|
|
|
import org.crosswire.common.progress.WorkEvent;
|
|
|
|
import org.crosswire.common.progress.WorkListener;
|
2014-07-10 22:37:37 -04:00
|
|
|
import org.crosswire.jsword.book.Book;
|
|
|
|
import org.crosswire.jsword.book.Books;
|
|
|
|
import org.crosswire.jsword.book.install.InstallManager;
|
|
|
|
import org.crosswire.jsword.book.install.Installer;
|
|
|
|
|
|
|
|
import java.util.Collection;
|
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
import java.util.concurrent.atomic.AtomicBoolean;
|
|
|
|
|
|
|
|
import javax.inject.Inject;
|
|
|
|
import javax.inject.Singleton;
|
|
|
|
|
|
|
|
import dagger.Module;
|
|
|
|
import dagger.ObjectGraph;
|
|
|
|
import dagger.Provides;
|
2014-07-17 17:42:31 -04:00
|
|
|
import rx.Observable;
|
2014-07-10 22:37:37 -04:00
|
|
|
import rx.functions.Action1;
|
|
|
|
import rx.functions.Func1;
|
|
|
|
|
2014-07-19 00:32:26 -04:00
|
|
|
import static com.jayway.awaitility.Awaitility.await;
|
2014-07-10 22:37:37 -04:00
|
|
|
|
|
|
|
public class BookDownloadManagerTest extends TestCase implements Injector {
|
|
|
|
|
|
|
|
ObjectGraph mObjectGraph;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Modules needed for this test case
|
|
|
|
*/
|
|
|
|
@Module(injects = {BookDownloadManager.class,
|
|
|
|
RefreshManager.class,
|
|
|
|
BookDownloadManagerTest.class})
|
|
|
|
public static class BookDownloadManagerTestModules {
|
|
|
|
Injector i;
|
|
|
|
|
|
|
|
BookDownloadManagerTestModules(Injector i) {
|
|
|
|
this.i = i;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Provides
|
|
|
|
@Singleton
|
|
|
|
Injector provideInjector() {
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Provides @Singleton
|
|
|
|
Books provideBooks() {
|
|
|
|
return Books.installed();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Provides @Singleton
|
|
|
|
Collection<Installer> provideInstallers() {
|
|
|
|
return new InstallManager().getInstallers().values();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Inject BookDownloadManager bookDownloadManager;
|
|
|
|
@Inject RefreshManager refreshManager;
|
|
|
|
@Inject Books installedBooks;
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void inject(Object o) {
|
|
|
|
mObjectGraph.inject(o);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setUp() {
|
|
|
|
BookDownloadManagerTestModules modules = new BookDownloadManagerTestModules(this);
|
|
|
|
mObjectGraph = ObjectGraph.create(modules);
|
|
|
|
mObjectGraph.inject(this);
|
|
|
|
}
|
|
|
|
|
2014-07-17 17:42:31 -04:00
|
|
|
Observable<Book> installableBooks() {
|
|
|
|
return refreshManager.getAvailableModulesFlattened()
|
2014-07-10 22:37:37 -04:00
|
|
|
.filter(new Func1<Book, Boolean>() {
|
|
|
|
@Override
|
|
|
|
public Boolean call(Book book) {
|
|
|
|
return !installedBooks.getBooks().contains(book);
|
|
|
|
}
|
2014-07-17 17:42:31 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public void testInstallBook() throws Exception {
|
|
|
|
final Book toInstall = installableBooks().toBlocking().first();
|
2014-07-10 22:37:37 -04:00
|
|
|
|
|
|
|
bookDownloadManager.installBook(toInstall);
|
|
|
|
|
|
|
|
final AtomicBoolean signal = new AtomicBoolean(false);
|
|
|
|
bookDownloadManager.getDownloadEvents()
|
|
|
|
.subscribe(new Action1<DLProgressEvent>() {
|
|
|
|
@Override
|
|
|
|
public void call(DLProgressEvent dlProgressEvent) {
|
|
|
|
if (dlProgressEvent.getB().getInitials().equals(toInstall.getInitials())
|
|
|
|
&& dlProgressEvent.getProgress() == DLProgressEvent.PROGRESS_COMPLETE) {
|
|
|
|
signal.set(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
await().atMost(60, TimeUnit.SECONDS)
|
|
|
|
.untilTrue(signal);
|
|
|
|
}
|
2014-07-17 17:42:31 -04:00
|
|
|
|
|
|
|
public void testJobIdMatch() {
|
|
|
|
final Book toInstall = installableBooks().toBlocking().first();
|
|
|
|
final String jobName = BookDownloadManager.getJobId(toInstall);
|
|
|
|
final AtomicBoolean jobNameMatch = new AtomicBoolean(false);
|
|
|
|
|
|
|
|
JobManager.addWorkListener(new WorkListener() {
|
|
|
|
@Override
|
|
|
|
public void workProgressed(WorkEvent ev) {
|
|
|
|
Log.d("testJobIdMatch", ev.getJob().getJobID() + " " + jobName);
|
|
|
|
if (ev.getJob().getJobID().equals(jobName)) {
|
|
|
|
jobNameMatch.set(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void workStateChanged(WorkEvent ev) {
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
bookDownloadManager.installBook(toInstall);
|
|
|
|
await().atMost(1, TimeUnit.SECONDS)
|
|
|
|
.untilTrue(jobNameMatch);
|
|
|
|
}
|
2014-07-10 22:37:37 -04:00
|
|
|
}
|