Get the unit tests passing again

Note: I need to write more. Lots more.
Rx/Retrolambda
Bradlee Speice 2014-06-14 16:25:13 -04:00
parent 04fe4d13b4
commit e945ef51a7
5 changed files with 32 additions and 44 deletions

View File

@ -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

View File

@ -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) {

View File

@ -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);

View File

@ -61,6 +61,14 @@ public class RefreshManager {
return availableModules;
}
public Observable<Book> 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());
}

View File

@ -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<Boolean>() {
@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));
}
}