mirror of
https://github.com/MinimalBible/MinimalBible
synced 2025-07-03 06:44:48 -04:00
Plenty o' updates: Kotlin 12, new JSword build
This commit is contained in:
@ -0,0 +1,25 @@
|
||||
package org.bspeice.minimalbible
|
||||
|
||||
import org.junit.Before
|
||||
import org.junit.Test
|
||||
import kotlin.properties.Delegates
|
||||
|
||||
class SafeValDelegateKotlinTest {
|
||||
|
||||
var delegate: SafeValDelegate<String> by Delegates.notNull()
|
||||
|
||||
Before fun setUp() {
|
||||
delegate = SafeValDelegate()
|
||||
}
|
||||
|
||||
Test(expected = IllegalStateException::class)
|
||||
fun testDelegateNullSafety() {
|
||||
delegate.get(null, PropertyMetadataImpl(""))
|
||||
}
|
||||
|
||||
Test(expected = IllegalStateException::class)
|
||||
fun testDelegateAssignOnce() {
|
||||
delegate.set(null, PropertyMetadataImpl(""), "")
|
||||
delegate.set(null, PropertyMetadataImpl(""), "")
|
||||
}
|
||||
}
|
@ -0,0 +1,82 @@
|
||||
package org.bspeice.minimalbible.activity.downloader
|
||||
|
||||
import android.content.DialogInterface
|
||||
import org.jetbrains.spek.api.Spek
|
||||
import kotlin.test.assertTrue
|
||||
|
||||
/**
|
||||
* Created by bspeice on 11/22/14.
|
||||
*/
|
||||
|
||||
class BookListFragmentSpek : Spek() {init {
|
||||
|
||||
given("A BookListFragment with showDialog() mocked out") {
|
||||
val fragment = object : BookListFragment() {
|
||||
var condition = false
|
||||
override fun showDialog() {
|
||||
condition = true
|
||||
}
|
||||
}
|
||||
|
||||
on("attempting to display modules with the dialog not shown already") {
|
||||
fragment.displayModules(false)
|
||||
|
||||
it("should show the download dialog") {
|
||||
assertTrue(fragment.condition)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
given("a BookListFragment with displayLanguageSpinner() mocked out") {
|
||||
val fragment = object : BookListFragment() {
|
||||
var condition = false
|
||||
|
||||
override fun displayLanguageSpinner() {
|
||||
condition = true
|
||||
}
|
||||
}
|
||||
|
||||
on("attempting to display modules with the dialog already shown") {
|
||||
fragment.displayModules(true)
|
||||
|
||||
it("should show the available languages spinner") {
|
||||
assertTrue(fragment.condition)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
given("a DownloadDialogListener with with buttonPositive() mocked out") {
|
||||
val listener = object : BookListFragment.DownloadDialogListener(null, null) {
|
||||
var condition = false
|
||||
override fun buttonPositive() {
|
||||
condition = true
|
||||
}
|
||||
}
|
||||
|
||||
on("handling a positive button press") {
|
||||
listener.handleButton(DialogInterface.BUTTON_POSITIVE)
|
||||
|
||||
it("should call the proper handler") {
|
||||
assertTrue(listener.condition)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
given("A DownloadDialogListener with buttonNegative() mocked out") {
|
||||
val listener = object : BookListFragment.DownloadDialogListener(null, null) {
|
||||
var condition = false
|
||||
override fun buttonNegative() {
|
||||
condition = true
|
||||
}
|
||||
}
|
||||
|
||||
on("handling a negative button press") {
|
||||
listener.handleButton(DialogInterface.BUTTON_NEGATIVE)
|
||||
|
||||
it("should call the proper handler") {
|
||||
assertTrue(listener.condition)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,27 @@
|
||||
/**
|
||||
* Created by bspeice on 11/20/14.
|
||||
*/
|
||||
|
||||
import org.bspeice.minimalbible.activity.downloader.manager.DLProgressEvent
|
||||
import org.crosswire.jsword.book.Book
|
||||
import org.jetbrains.spek.api.Spek
|
||||
import org.mockito.Mockito.mock
|
||||
import kotlin.test.assertEquals
|
||||
|
||||
|
||||
class DLProgressEventSpek : Spek() {init {
|
||||
|
||||
given("a DLProgressEvent created with 50% progress and a mock book") {
|
||||
val mockBook = mock(javaClass<Book>())
|
||||
val dlEvent = DLProgressEvent(50, mockBook)
|
||||
|
||||
on("getting the progress in degrees") {
|
||||
val progressDegrees = dlEvent.toCircular()
|
||||
|
||||
it("should be 180 degrees") {
|
||||
assertEquals(180, progressDegrees)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,66 @@
|
||||
package org.bspeice.minimalbible.activity.downloader.manager
|
||||
|
||||
import org.crosswire.common.util.Language
|
||||
import org.jetbrains.spek.api.Spek
|
||||
|
||||
/**
|
||||
* Created by bspeice on 12/14/14.
|
||||
*/
|
||||
|
||||
class LocaleManagerSpek() : Spek() {init {
|
||||
|
||||
given("some example language objects") {
|
||||
val english = Language("en")
|
||||
val russian = Language("ru")
|
||||
val french = Language("fr");
|
||||
|
||||
on("sorting between english and russian with current as english") {
|
||||
val result = LocaleManager.compareLanguages(english, russian, english)
|
||||
|
||||
it("should prioritize english") {
|
||||
assert(result < 0)
|
||||
}
|
||||
}
|
||||
|
||||
on("sorting between russian and english with current as english") {
|
||||
val result = LocaleManager.compareLanguages(russian, english, english)
|
||||
|
||||
it("should prioritize english") {
|
||||
assert(result > 0)
|
||||
}
|
||||
}
|
||||
|
||||
on("sorting between russian and english with current as french") {
|
||||
val result = LocaleManager.compareLanguages(russian, english, french)
|
||||
|
||||
it("should inform us that russian is greater") {
|
||||
assert(result > 0)
|
||||
}
|
||||
}
|
||||
|
||||
on("sorting between english and russian with current as french") {
|
||||
val result = LocaleManager.compareLanguages(english, russian, french)
|
||||
|
||||
it("should inform us that english is lesser") {
|
||||
assert(result < 0)
|
||||
}
|
||||
}
|
||||
|
||||
on("comparing the same languages with current language as the language being compared") {
|
||||
val result = LocaleManager.compareLanguages(english, english, english)
|
||||
|
||||
it("should report that the languages are duplicate") {
|
||||
assert(result == 0)
|
||||
}
|
||||
}
|
||||
|
||||
on("comparing the same languages with current language as something different") {
|
||||
val result = LocaleManager.compareLanguages(english, english, russian)
|
||||
|
||||
it("should report that the languages are duplicate") {
|
||||
assert(result == 0)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,108 @@
|
||||
package org.bspeice.minimalbible.activity.downloader.manager
|
||||
|
||||
import com.jayway.awaitility.Awaitility
|
||||
import org.bspeice.minimalbible.activity.downloader.DownloadPrefs
|
||||
import org.crosswire.jsword.book.Book
|
||||
import org.crosswire.jsword.book.install.Installer
|
||||
import org.jetbrains.spek.api.Spek
|
||||
import org.mockito.Matchers.anyLong
|
||||
import org.mockito.Mockito.*
|
||||
import rx.Subscriber
|
||||
import rx.schedulers.Schedulers
|
||||
import java.util.Calendar
|
||||
import java.util.concurrent.TimeUnit
|
||||
import java.util.concurrent.atomic.AtomicBoolean
|
||||
|
||||
/**
|
||||
* Created by bspeice on 1/3/15.
|
||||
*/
|
||||
|
||||
class RefreshManagerSpek() : Spek() {init {
|
||||
|
||||
fun buildRefreshmanager(installers: List<Installer>, prefs: DownloadPrefs) =
|
||||
RefreshManager(installers, listOf(""), prefs, null)
|
||||
|
||||
fun buildMockPrefs(): DownloadPrefs {
|
||||
val currentTime = Calendar.getInstance().getTime().getTime()
|
||||
val eighteenDaysAgo = currentTime - 1555200
|
||||
val mockPrefs = mock(javaClass<DownloadPrefs>())
|
||||
`when`(mockPrefs.downloadRefreshedOn())
|
||||
.thenReturn(eighteenDaysAgo)
|
||||
|
||||
return mockPrefs
|
||||
}
|
||||
|
||||
given("a mock installer") {
|
||||
val installer = mock(javaClass<Installer>())
|
||||
|
||||
on("creating a new RefreshManager and mock preferences") {
|
||||
val mockPrefs = buildMockPrefs()
|
||||
buildRefreshmanager(listOf(installer, installer), mockPrefs)
|
||||
|
||||
it("should not have updated the prefs as part of the constructor") {
|
||||
verify(mockPrefs, never())
|
||||
.downloadRefreshedOn(anyLong())
|
||||
}
|
||||
}
|
||||
|
||||
on("creating a new RefreshManager and mock preferences") {
|
||||
val mockPrefs = buildMockPrefs()
|
||||
val rM = buildRefreshmanager(listOf(installer, installer), mockPrefs)
|
||||
reset(mockPrefs)
|
||||
|
||||
it("should not update the prefs after the first installer") {
|
||||
// The process to do actually validate this is tricky. We have to block
|
||||
// the Observable from producing before we can validate the preferences -
|
||||
// I don't want to race the Observable since it's possible it's on another thread.
|
||||
// So, we use backpressure (request(1)) to force the observable to
|
||||
// produce only one result.
|
||||
val success = AtomicBoolean(false)
|
||||
rM.availableModules
|
||||
.subscribe(object : Subscriber<Map<Installer, List<Book>>>() {
|
||||
override fun onCompleted() {
|
||||
}
|
||||
|
||||
override fun onError(e: Throwable?) {
|
||||
}
|
||||
|
||||
override fun onStart() {
|
||||
super.onStart()
|
||||
request(1)
|
||||
}
|
||||
|
||||
override fun onNext(t: Map<Installer, List<Book>>?) {
|
||||
// Verify the mock - if verification doesn't pass, we won't reach
|
||||
// the end of this method and set our AtomicBoolean to true
|
||||
verify(mockPrefs, never())
|
||||
.downloadRefreshedOn(anyLong())
|
||||
success.set(true)
|
||||
}
|
||||
})
|
||||
|
||||
Awaitility.waitAtMost(2, TimeUnit.SECONDS)
|
||||
.untilTrue(success)
|
||||
}
|
||||
}
|
||||
|
||||
on("creating a new RefreshManager and mock preferences") {
|
||||
val mockPrefs = buildMockPrefs()
|
||||
val rM = buildRefreshmanager(listOf(installer, installer), mockPrefs)
|
||||
reset(mockPrefs)
|
||||
|
||||
it("should update the prefs after completed") {
|
||||
val complete = AtomicBoolean(false)
|
||||
rM.availableModules.observeOn(Schedulers.immediate())
|
||||
.subscribe({}, {}, {
|
||||
complete.set(true)
|
||||
})
|
||||
|
||||
Awaitility.waitAtMost(3, TimeUnit.SECONDS)
|
||||
.untilTrue(complete)
|
||||
|
||||
verify(mockPrefs, times(1))
|
||||
.downloadRefreshedOn(anyLong())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,187 @@
|
||||
package org.bspeice.minimalbible.activity.search
|
||||
|
||||
import com.jayway.awaitility.Awaitility
|
||||
import org.crosswire.jsword.book.Book
|
||||
import org.crosswire.jsword.index.IndexManager
|
||||
import org.crosswire.jsword.index.IndexStatus
|
||||
import org.jetbrains.spek.api.Spek
|
||||
import org.mockito.Mockito
|
||||
import java.util.concurrent.TimeUnit
|
||||
import java.util.concurrent.atomic.AtomicBoolean
|
||||
import java.util.concurrent.atomic.AtomicReference
|
||||
import kotlin.test.assertEquals
|
||||
import kotlin.test.assertFalse
|
||||
import kotlin.test.assertTrue
|
||||
import kotlin.test.fail
|
||||
|
||||
/**
|
||||
* Created by bspeice on 2/16/15.
|
||||
*/
|
||||
data class Mocks() {
|
||||
val mockBook = Mockito.mock(javaClass<Book>())
|
||||
val mockIndex = Mockito.mock(javaClass<IndexManager>())
|
||||
val indexManager = MBIndexManager(mockIndex)
|
||||
}
|
||||
|
||||
class MBIndexManagerSpek() : Spek() {init {
|
||||
|
||||
given("a mock IndexManager, Book, and real MBIndexManager") {
|
||||
val mocks = Mocks()
|
||||
val mockBook = mocks.mockBook
|
||||
val indexManager = mocks.indexManager
|
||||
|
||||
val firstStatus = IndexStatus.UNDONE
|
||||
val secondStatus = IndexStatus.DONE
|
||||
|
||||
on("setting up the book and attempting to create the index") {
|
||||
val firstNext = AtomicReference<IndexStatus>()
|
||||
val secondNext = AtomicReference<IndexStatus>()
|
||||
val completedReference = AtomicBoolean(false)
|
||||
|
||||
Mockito.`when`(mockBook.getIndexStatus())
|
||||
// MBIndexManager checks status
|
||||
.thenReturn(firstStatus)
|
||||
// First actual status
|
||||
.thenReturn(firstStatus)
|
||||
// Second actual status
|
||||
.thenReturn(secondStatus)
|
||||
|
||||
val subject = indexManager.buildIndex(mockBook)
|
||||
|
||||
subject.subscribe({
|
||||
if (firstNext.get() == null)
|
||||
firstNext.set(it)
|
||||
else
|
||||
secondNext.set(it)
|
||||
},
|
||||
{},
|
||||
{ completedReference.set(true) })
|
||||
|
||||
// Wait until completed
|
||||
Awaitility.waitAtMost(1, TimeUnit.SECONDS).untilTrue(completedReference);
|
||||
|
||||
it("should fire the correct first status") {
|
||||
assertEquals(firstStatus, firstNext.get())
|
||||
}
|
||||
|
||||
it("should fire the correct second status") {
|
||||
assertEquals(secondStatus, secondNext.get())
|
||||
}
|
||||
|
||||
it("should fire the onCompleted event") {
|
||||
assertTrue(completedReference.get())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
given("a mock IndexManager, Book, and real MBIndexManager") {
|
||||
val mocks = Mocks()
|
||||
val indexManager = mocks.mockIndex
|
||||
val book = mocks.mockBook
|
||||
val mbIndex = mocks.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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
given("a Book that is indexed and real MBIndexManager") {
|
||||
val mocks = Mocks()
|
||||
val book = mocks.mockBook
|
||||
val indexManager = mocks.indexManager
|
||||
|
||||
Mockito.`when`(book.getIndexStatus())
|
||||
.thenReturn(IndexStatus.DONE)
|
||||
|
||||
on("trying to determine whether we should index") {
|
||||
it("should not try to index") {
|
||||
assertFalse(indexManager shouldIndex book)
|
||||
}
|
||||
}
|
||||
|
||||
on("trying to determine if an index is ready") {
|
||||
it("should let us know that everything is ready") {
|
||||
assertTrue(indexManager indexReady book)
|
||||
}
|
||||
}
|
||||
|
||||
on("attempting to index anyway") {
|
||||
it("should throw an error") {
|
||||
try {
|
||||
indexManager buildIndex book
|
||||
fail()
|
||||
} catch (e: IllegalStateException) {
|
||||
// Intentionally empty body
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
given("a Book with an indexing error") {
|
||||
val mocks = Mocks()
|
||||
val book = mocks.mockBook
|
||||
val mockIndex = mocks.mockIndex
|
||||
val indexManager = mocks.indexManager
|
||||
|
||||
Mockito.`when`(book.getIndexStatus())
|
||||
.thenReturn(IndexStatus.INVALID)
|
||||
|
||||
on("trying to determine whether we should index") {
|
||||
it("should try to index again and over-write the original") {
|
||||
assertTrue(indexManager shouldIndex book)
|
||||
}
|
||||
}
|
||||
|
||||
on("trying to determine if an index is ready") {
|
||||
it("should inform us that the index is most certainly not ready") {
|
||||
assertFalse(indexManager indexReady book)
|
||||
}
|
||||
}
|
||||
|
||||
on("attempting to index") {
|
||||
indexManager buildIndex book
|
||||
|
||||
it("should run the index") {
|
||||
Mockito.verify(mockIndex, Mockito.times(1))
|
||||
.scheduleIndexCreation(book);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
given("a Book in process of being indexed") {
|
||||
val mocks = Mocks()
|
||||
val book = mocks.mockBook
|
||||
val indexManager = mocks.indexManager
|
||||
|
||||
Mockito.`when`(book.getIndexStatus())
|
||||
.thenReturn(IndexStatus.CREATING)
|
||||
|
||||
on("trying to determine whether we should index") {
|
||||
it("should not create a second indexing thread") {
|
||||
assertFalse(indexManager shouldIndex book)
|
||||
}
|
||||
}
|
||||
|
||||
on("trying to determine if the index is ready") {
|
||||
it("should let us know that the index is still in progress") {
|
||||
assertFalse(indexManager shouldIndex book)
|
||||
}
|
||||
}
|
||||
|
||||
on("attempting to index anyway") {
|
||||
it("should throw an error to let us know it will not index") {
|
||||
try {
|
||||
indexManager buildIndex book
|
||||
fail()
|
||||
} catch (e: IllegalStateException) {
|
||||
// Intentionally empty body
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user