From 365cf0dccb48a9dec016aa352b32f505954b56a5 Mon Sep 17 00:00:00 2001 From: Bradlee Speice Date: Thu, 10 Jul 2014 22:37:37 -0400 Subject: [PATCH] Add the first full-scale test case Also demonstrates some advanced Dagger usage --- app/build.gradle | 6 +- .../manager/BookDownloadManagerTest.java | 107 ++++++++++++++++++ 2 files changed, 109 insertions(+), 4 deletions(-) create mode 100644 app/src/androidTest/java/org/bspeice/minimalbible/test/activity/downloader/manager/BookDownloadManagerTest.java diff --git a/app/build.gradle b/app/build.gradle index 139c313..8cfdb63 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -51,16 +51,14 @@ android { dependencies { compile project(path: ':jsword-minimalbible', configuration: 'buildJSword') + compile 'com.squareup.dagger:dagger:1.2.1' provided 'com.squareup.dagger:dagger-compiler:1.2.1' - provided 'de.devland.esperandro:esperandro:1.1.2' - androidTestCompile 'com.jayway.awaitility:awaitility:1.6.0' - androidTestProvided 'com.squareup.dagger:dagger-compiler:1.2.0' + // androidTestProvided 'com.squareup.dagger:dagger-compiler:1.2.0' - compile 'com.squareup.dagger:dagger:1.2.1' compile 'com.jakewharton:butterknife:5.0.1' compile 'de.devland.esperandro:esperandro-api:1.1.2' compile 'com.readystatesoftware.systembartint:systembartint:1.0.3' diff --git a/app/src/androidTest/java/org/bspeice/minimalbible/test/activity/downloader/manager/BookDownloadManagerTest.java b/app/src/androidTest/java/org/bspeice/minimalbible/test/activity/downloader/manager/BookDownloadManagerTest.java new file mode 100644 index 0000000..3b5202c --- /dev/null +++ b/app/src/androidTest/java/org/bspeice/minimalbible/test/activity/downloader/manager/BookDownloadManagerTest.java @@ -0,0 +1,107 @@ +package org.bspeice.minimalbible.test.activity.downloader.manager; + +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; +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; +import rx.functions.Action1; +import rx.functions.Func1; + +import static com.jayway.awaitility.Awaitility.*; + +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 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); + } + + public void testInstallBook() throws Exception { + final Book toInstall = refreshManager.getAvailableModulesFlattened() + .filter(new Func1() { + @Override + public Boolean call(Book book) { + // First uninstalled book + return !installedBooks.getBooks().contains(book); + } + }) + .toBlocking() + .first(); + + bookDownloadManager.installBook(toInstall); + + final AtomicBoolean signal = new AtomicBoolean(false); + bookDownloadManager.getDownloadEvents() + .subscribe(new Action1() { + @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); + } +} \ No newline at end of file