diff --git a/app-test/src/test/kotlin/org/bspeice/minimalbible/activity/search/MBIndexManagerSpek.kt b/app-test/src/test/kotlin/org/bspeice/minimalbible/activity/search/MBIndexManagerSpek.kt new file mode 100644 index 0000000..1413be8 --- /dev/null +++ b/app-test/src/test/kotlin/org/bspeice/minimalbible/activity/search/MBIndexManagerSpek.kt @@ -0,0 +1,78 @@ +package org.bspeice.minimalbible.activity.search + +import org.jetbrains.spek.api.Spek +import org.mockito.Mockito +import org.crosswire.jsword.index.IndexManager +import org.crosswire.jsword.book.Book +import org.crosswire.jsword.index.IndexStatus +import java.util.concurrent.atomic.AtomicBoolean +import java.util.concurrent.atomic.AtomicReference +import kotlin.test.assertEquals +import com.jayway.awaitility.Awaitility +import java.util.concurrent.TimeUnit + +/** + * Created by bspeice on 2/16/15. + */ +class MBIndexManagerSpek() : Spek() {{ + + given("a mock IndexManager, Book, and real MBIndexManager") { + val returnDelay: Long = 1000 + val mockIndex = Mockito.mock(javaClass()) + val mockBook = Mockito.mock(javaClass()) + val indexManager = MBIndexManager(mockIndex) + + val firstStatus = IndexStatus.UNDONE + val secondStatus = IndexStatus.DONE + + // We sleep the first response to give us time to actually subscribe + Mockito.`when`(mockBook.getIndexStatus()) + .thenAnswer { Thread.sleep(returnDelay); firstStatus } + .thenReturn(secondStatus) + + on("attempting to create the index") { + val firstNext = AtomicReference() + val secondNext = AtomicReference() + val completedReference = AtomicBoolean(false) + + val subject = indexManager.buildIndex(mockBook) + + subject.subscribe({ + if (firstNext.get() == null) + firstNext.set(it) + else + secondNext.set(it) + }, + {}, + { completedReference.set(true) }) + + it("should fire an onComplete so we can continue further validation") { + Awaitility.waitAtMost(returnDelay * 2, TimeUnit.MILLISECONDS) + .untilTrue(completedReference) + } + + it("should fire the correct first status") { + assertEquals(firstStatus, firstNext.get()) + } + + it("should fire the correct second status") { + assertEquals(secondStatus, secondNext.get()) + } + } + } + + given("a mock IndexManager, Book, and real MBIndexManager") { + val indexManager = Mockito.mock(javaClass()) + val book = Mockito.mock(javaClass()) + val mbIndex = MBIndexManager(indexManager) + + on("trying to remove a book's index") { + mbIndex.removeIndex(book) + + it("should call the IndexManager.deleteIndex() function") { + Mockito.verify(indexManager, Mockito.times(1)) deleteIndex book + } + } + } +} +} diff --git a/app/src/main/kotlin/org/bspeice/minimalbible/activity/search/MBIndexManager.kt b/app/src/main/kotlin/org/bspeice/minimalbible/activity/search/MBIndexManager.kt index 6885950..a2a3316 100644 --- a/app/src/main/kotlin/org/bspeice/minimalbible/activity/search/MBIndexManager.kt +++ b/app/src/main/kotlin/org/bspeice/minimalbible/activity/search/MBIndexManager.kt @@ -5,27 +5,34 @@ import org.crosswire.jsword.book.Book import android.util.Log import rx.schedulers.Schedulers import rx.Observable +import rx.subjects.PublishSubject +import org.crosswire.jsword.index.IndexStatus /** * There's already an IndexManager, that's why the funky name */ class MBIndexManager(val indexManager: IndexManager) { - fun buildIndex(b: Book) { + /** + * Do the hard work of actually building the book index. + * Returns a PublishSubject<> that completes when the + * index is complete. Also is nice enough to broadcast + * what work is being done when. + */ + fun buildIndex(b: Book): PublishSubject { + val indexStatus: PublishSubject = PublishSubject.create(); Observable.just(b) .observeOn(Schedulers.computation()) .subscribe({ - try { - Log.e("MBIndexManager", "Beginning index status: ${b.getIndexStatus()}") - indexManager scheduleIndexCreation b - Log.e("MBIndexManager", "Ending index status: ${b.getIndexStatus()}") - } catch (e: Exception) { - Log.e("MBIndexManager", "Exception building index: ${e}", e) - } - }, { - Log.e("MBIndexManager", "Exception building index: $it", it) - }) - Log.d("MBIndexManager", "Building index for ${b.getInitials()}") + indexStatus.onNext(b.getIndexStatus()) + + indexManager scheduleIndexCreation b + + indexStatus.onNext(b.getIndexStatus()) + indexStatus.onCompleted() + }, { Log.e("MBIndexManager", "Exception building index: $it", it) }) + + return indexStatus } fun removeIndex(b: Book) = indexManager.deleteIndex(b)