From f2a4ceceffebe1956f07237463a9546f23dbef1d Mon Sep 17 00:00:00 2001 From: Bradlee Speice Date: Sat, 19 Jul 2014 01:44:22 -0400 Subject: [PATCH] Tests still fail, but look prettier! --- .../manager/InstalledManagerTest.java | 95 ++++++++----------- 1 file changed, 38 insertions(+), 57 deletions(-) diff --git a/app/src/androidTest/java/org/bspeice/minimalbible/test/activity/downloader/manager/InstalledManagerTest.java b/app/src/androidTest/java/org/bspeice/minimalbible/test/activity/downloader/manager/InstalledManagerTest.java index 14b7f71..057cc75 100644 --- a/app/src/androidTest/java/org/bspeice/minimalbible/test/activity/downloader/manager/InstalledManagerTest.java +++ b/app/src/androidTest/java/org/bspeice/minimalbible/test/activity/downloader/manager/InstalledManagerTest.java @@ -6,17 +6,18 @@ 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.InstalledManager; 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.BooksEvent; +import org.crosswire.jsword.book.BooksListener; import org.crosswire.jsword.book.install.InstallManager; import org.crosswire.jsword.book.install.Installer; -import org.crosswire.jsword.book.sword.SwordBook; import java.util.Collection; import java.util.List; +import java.util.concurrent.Callable; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicBoolean; @@ -27,7 +28,6 @@ import dagger.Module; import dagger.ObjectGraph; import dagger.Provides; import rx.Observable; -import rx.functions.Action0; import rx.functions.Action1; import rx.functions.Func1; @@ -83,32 +83,25 @@ public class InstalledManagerTest extends TestCase implements Injector { mObjectGraph.inject(this); // Guarantee something is installed - getInstalledBooks() - .count() - .subscribe(new Action1() { - @Override - public void call(Integer count) { - if (count <= 0) { - Log.i("InstalledManagerTest", "Nothing installed!"); - final AtomicBoolean isInstalled = new AtomicBoolean(false); - final Book toInstall = rM.getAvailableModulesFlattened().toBlocking().first(); - bDM.installBook(toInstall); - bDM.getDownloadEvents() - .subscribe(new Action1() { - @Override - public void call(DLProgressEvent dlProgressEvent) { - if (dlProgressEvent.getProgress() == DLProgressEvent.PROGRESS_COMPLETE && - dlProgressEvent.getB().getName().equals(toInstall.getName())) { + int count = getInstalledBooks().count().toBlocking().first(); - isInstalled.set(true); - } - } - }); - await().atMost(30, TimeUnit.SECONDS) - .untilTrue(isInstalled); + if (count <= 0) { + Log.i("InstalledManagerTest", "Nothing installed!"); + final Book toInstall = rM.getAvailableModulesFlattened() + .toBlocking().first(); + bDM.installBook(toInstall); + + await().atMost(30, TimeUnit.SECONDS) + .until(new Callable() { + @Override + public Boolean call() throws Exception { + return installedBooks.getBooks() + .contains(toInstall); } - } - }); + }); + Log.e("setUp", Boolean.toString(toInstall.getDriver().isDeletable(toInstall))); + Log.e("setUp", "Found the book!: " + toInstall.getInitials()); + } } public Observable getInstalledBooks() { @@ -119,12 +112,6 @@ public class InstalledManagerTest extends TestCase implements Injector { */ // TODO: Guarantee that we return newly-installed books return Observable.from(installedBooks.getBooks()) - .filter(new Func1() { - @Override - public Boolean call(Book book) { - return book.getDriver().isDeletable(book); - } - }) .filter(new Func1() { @Override public Boolean call(Book book) { @@ -157,30 +144,24 @@ public class InstalledManagerTest extends TestCase implements Injector { * @throws Exception */ public void testRemoveBook() throws Exception { - final AtomicBoolean isRemoved = new AtomicBoolean(false); - getInstalledBooks() - .first() - .subscribe(new Action1() { - @Override - public void call(Book book) { - iM.removeBook(book); + final Book book = getInstalledBooks().toBlocking().first(); - // The AbstractBook returns false all the time, make sure we have - // an actual implementation - Log.w("testRemoveBook", book.getInitials()); - isRemoved.set(!book.getDriver().isDeletable(book) && - book instanceof SwordBook); - } - }, new Action1() { - @Override - public void call(Throwable throwable) { - fail(throwable.getLocalizedMessage()); - } - }, new Action0() { - @Override - public void call() { - assertTrue(isRemoved.get()); - } - }); + final AtomicBoolean didRemove = new AtomicBoolean(false); + + installedBooks.addBooksListener(new BooksListener() { + @Override + public void bookAdded(BooksEvent ev) { + + } + @Override + public void bookRemoved(BooksEvent ev) { + if (ev.getBook().equals(book)) { + didRemove.set(true); + } + } + }); + + iM.removeBook(book); + await().untilTrue(didRemove); } } \ No newline at end of file