diff --git a/MinimalBible/src/main/java/org/bspeice/minimalbible/activities/downloader/BookListFragment.java b/MinimalBible/src/main/java/org/bspeice/minimalbible/activities/downloader/BookListFragment.java index 846fdac..bf2942d 100644 --- a/MinimalBible/src/main/java/org/bspeice/minimalbible/activities/downloader/BookListFragment.java +++ b/MinimalBible/src/main/java/org/bspeice/minimalbible/activities/downloader/BookListFragment.java @@ -118,11 +118,7 @@ public class BookListFragment extends BaseFragment { } // Listen for the books! - refreshManager.getAvailableModules() - // First flatten the Map to its lists - .flatMap((books) -> Observable.from(books.values())) - // Then flatten the lists - .flatMap(Observable::from) + refreshManager.getAvailableModulesFlattened() .filter((book) -> book.getBookCategory() == BookCategory.fromString(getArguments().getString(ARG_BOOK_CATEGORY))) // Repack all the books diff --git a/MinimalBible/src/main/java/org/bspeice/minimalbible/activities/downloader/manager/BookDownloadManager.java b/MinimalBible/src/main/java/org/bspeice/minimalbible/activities/downloader/manager/BookDownloadManager.java index 021b502..663b558 100644 --- a/MinimalBible/src/main/java/org/bspeice/minimalbible/activities/downloader/manager/BookDownloadManager.java +++ b/MinimalBible/src/main/java/org/bspeice/minimalbible/activities/downloader/manager/BookDownloadManager.java @@ -56,6 +56,7 @@ public class BookDownloadManager implements WorkListener, BooksListener { BookDownloadThread dlThread = dlThreadProvider.get(); dlThread.downloadBook(b); addJob(BookDownloadThread.getJobId(b), b); + downloadEvents.onNext(new DLProgressEvent(DLProgressEvent.PROGRESS_BEGINNING, b)); } public void addJob(String jobId, Book b) { diff --git a/MinimalBible/src/main/java/org/bspeice/minimalbible/activities/downloader/manager/InstalledManager.java b/MinimalBible/src/main/java/org/bspeice/minimalbible/activities/downloader/manager/InstalledManager.java index b17a136..dd9d4ec 100644 --- a/MinimalBible/src/main/java/org/bspeice/minimalbible/activities/downloader/manager/InstalledManager.java +++ b/MinimalBible/src/main/java/org/bspeice/minimalbible/activities/downloader/manager/InstalledManager.java @@ -63,6 +63,9 @@ public class InstalledManager implements BooksListener { } public void removeBook(Book b) { + if (installedBooks == null) { + initialize(); + } try { // This worked in the past, but isn't now... // installedBooks.remove(b); diff --git a/MinimalBible/src/main/java/org/bspeice/minimalbible/activities/downloader/manager/RefreshManager.java b/MinimalBible/src/main/java/org/bspeice/minimalbible/activities/downloader/manager/RefreshManager.java index 3b94175..b4abeb9 100644 --- a/MinimalBible/src/main/java/org/bspeice/minimalbible/activities/downloader/manager/RefreshManager.java +++ b/MinimalBible/src/main/java/org/bspeice/minimalbible/activities/downloader/manager/RefreshManager.java @@ -61,6 +61,14 @@ public class RefreshManager { return availableModules; } + public Observable getAvailableModulesFlattened() { + return availableModules + // First flatten the Map to its lists + .flatMap((books) -> Observable.from(books.values())) + // Then flatten the lists + .flatMap(Observable::from); + } + /** * Get the cached book list * @return The cached book list, or null @@ -90,7 +98,7 @@ public class RefreshManager { } return false; }) - .take(1) + .first() .map(element -> element.entrySet().iterator().next().getKey()); } diff --git a/MinimalBible/src/test/java/org/bspeice/minimalbible/test/DownloadActivityTest.java b/MinimalBible/src/test/java/org/bspeice/minimalbible/test/DownloadActivityTest.java index e61586d..09d06d7 100644 --- a/MinimalBible/src/test/java/org/bspeice/minimalbible/test/DownloadActivityTest.java +++ b/MinimalBible/src/test/java/org/bspeice/minimalbible/test/DownloadActivityTest.java @@ -1,24 +1,35 @@ package org.bspeice.minimalbible.test; import android.test.InstrumentationTestCase; +import android.util.Log; import org.bspeice.minimalbible.MinimalBible; import org.bspeice.minimalbible.MinimalBibleModules; +import org.bspeice.minimalbible.activities.downloader.manager.BookDownloadManager; +import org.bspeice.minimalbible.activities.downloader.manager.DLProgressEvent; import org.bspeice.minimalbible.activities.downloader.manager.DownloadManager; import org.bspeice.minimalbible.activities.downloader.manager.InstalledManager; +import org.bspeice.minimalbible.activities.downloader.manager.RefreshManager; import org.crosswire.jsword.book.Book; import org.crosswire.jsword.book.BookException; import org.crosswire.jsword.book.Books; +import org.crosswire.jsword.book.install.InstallException; import org.crosswire.jsword.book.install.Installer; import org.crosswire.jsword.passage.NoSuchKeyException; +import java.util.HashMap; +import java.util.List; +import java.util.Map; import java.util.concurrent.Callable; +import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicBoolean; import javax.inject.Inject; import dagger.Module; import dagger.ObjectGraph; +import rx.Observable; import static com.jayway.awaitility.Awaitility.await; @@ -33,6 +44,8 @@ public class DownloadActivityTest extends InstrumentationTestCase { @Inject DownloadManager dm; @Inject InstalledManager im; + @Inject RefreshManager rm; + @Inject BookDownloadManager bdm; public void setUp() { MinimalBible application = MinimalBible.getApplication(); @@ -45,42 +58,6 @@ public class DownloadActivityTest extends InstrumentationTestCase { assertEquals(true, true); } - /** - * When we start a download, make sure a progress event of 0 is triggered. - */ - /* - public void testInitialProgressEventOnDownload() throws InterruptedException { - final CountDownLatch signal = new CountDownLatch(1); - - // Need to make sure we've refreshed the refreshmanager first - Installer i = (Installer) dm.getInstallers().values().toArray()[0]; - final Book testBook = i.getBooks().get(0); - await().atMost(30, TimeUnit.SECONDS).until(new Callable() { - @Override - public Boolean call() throws Exception { - return rm.installerFromBook(testBook) != null; - } - }); - - // And wait for the actual download - dm.getDownloadBus().register(new Object() { - public void onEvent(DLProgressEvent event) { - if (event.getProgress() == 0) { - signal.countDown(); - } - } - }); - - BookDownloadThread thread = bookDownloadThreadProvider.get(); - thread.downloadBook(testBook); - - signal.await(10, TimeUnit.SECONDS); - if (signal.getCount() != 0) { - fail("Event did not trigger!"); - } - } - */ - /** * Test that we can successfully download and remove a book */ @@ -88,7 +65,9 @@ public class DownloadActivityTest extends InstrumentationTestCase { // Install a book Installer i = (Installer) dm.getInstallers().values().toArray()[0]; final Book testBook = i.getBooks().get(0); - await().atMost(30, TimeUnit.SECONDS).until(() -> Books.installed().getBooks().contains(testBook)); + bdm.installBook(testBook); + await().atMost(30, TimeUnit.SECONDS) + .until(() -> Books.installed().getBooks().contains(testBook)); // Validate that we can actually do something with the book // TODO: Validate that the book exists on the filesystem too @@ -103,7 +82,8 @@ public class DownloadActivityTest extends InstrumentationTestCase { // Remove the book and make sure it's gone // TODO: Validate that the book is off the filesystem im.removeBook(testBook); - assertFalse(Books.installed().getBooks().contains(testBook)); + await().atMost(10, TimeUnit.SECONDS) + .until(() -> !Books.installed().getBooks().contains(testBook)); } }