mirror of
https://github.com/afollestad/nock-nock.git
synced 2025-08-04 07:08:42 +00:00
AddSiteViewModelTest
This commit is contained in:
parent
98327c8c5b
commit
b57f645c98
4 changed files with 423 additions and 295 deletions
|
@ -16,6 +16,8 @@
|
||||||
package com.afollestad.nocknock.ui.addsite
|
package com.afollestad.nocknock.ui.addsite
|
||||||
|
|
||||||
import androidx.annotation.CheckResult
|
import androidx.annotation.CheckResult
|
||||||
|
import androidx.annotation.VisibleForTesting
|
||||||
|
import androidx.annotation.VisibleForTesting.PRIVATE
|
||||||
import androidx.lifecycle.LifecycleObserver
|
import androidx.lifecycle.LifecycleObserver
|
||||||
import androidx.lifecycle.LiveData
|
import androidx.lifecycle.LiveData
|
||||||
import androidx.lifecycle.MutableLiveData
|
import androidx.lifecycle.MutableLiveData
|
||||||
|
@ -74,10 +76,7 @@ class AddSiteViewModel(
|
||||||
@CheckResult fun onUrlWarningVisibility(): LiveData<Boolean> {
|
@CheckResult fun onUrlWarningVisibility(): LiveData<Boolean> {
|
||||||
return url.map {
|
return url.map {
|
||||||
val parsed = HttpUrl.parse(it)
|
val parsed = HttpUrl.parse(it)
|
||||||
return@map it.isNotEmpty() &&
|
return@map it.isNotEmpty() && parsed == null
|
||||||
parsed != null &&
|
|
||||||
parsed.scheme() != "http" &&
|
|
||||||
parsed.scheme() != "https"
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -85,11 +84,10 @@ class AddSiteViewModel(
|
||||||
|
|
||||||
@CheckResult fun onValidationModeDescription(): LiveData<Int> {
|
@CheckResult fun onValidationModeDescription(): LiveData<Int> {
|
||||||
return validationMode.map {
|
return validationMode.map {
|
||||||
when (it) {
|
when (it!!) {
|
||||||
STATUS_CODE -> R.string.validation_mode_status_desc
|
STATUS_CODE -> R.string.validation_mode_status_desc
|
||||||
TERM_SEARCH -> R.string.validation_mode_term_desc
|
TERM_SEARCH -> R.string.validation_mode_term_desc
|
||||||
JAVASCRIPT -> R.string.validation_mode_javascript_desc
|
JAVASCRIPT -> R.string.validation_mode_javascript_desc
|
||||||
else -> throw IllegalStateException("Unknown validation mode: $it")
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -127,13 +125,15 @@ class AddSiteViewModel(
|
||||||
}
|
}
|
||||||
|
|
||||||
// Utilities
|
// Utilities
|
||||||
private fun getCheckIntervalMs(): Long {
|
@VisibleForTesting(otherwise = PRIVATE)
|
||||||
|
fun getCheckIntervalMs(): Long {
|
||||||
val value = checkIntervalValue.value ?: return 0
|
val value = checkIntervalValue.value ?: return 0
|
||||||
val unit = checkIntervalUnit.value ?: return 0
|
val unit = checkIntervalUnit.value ?: return 0
|
||||||
return value * unit
|
return value * unit
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun getValidationArgs(): String? {
|
@VisibleForTesting(otherwise = PRIVATE)
|
||||||
|
fun getValidationArgs(): String? {
|
||||||
return when (validationMode.value) {
|
return when (validationMode.value) {
|
||||||
TERM_SEARCH -> validationSearchTerm.value
|
TERM_SEARCH -> validationSearchTerm.value
|
||||||
JAVASCRIPT -> validationScript.value
|
JAVASCRIPT -> validationScript.value
|
||||||
|
@ -184,13 +184,13 @@ class AddSiteViewModel(
|
||||||
}
|
}
|
||||||
|
|
||||||
// Validate arguments
|
// Validate arguments
|
||||||
if (validationMode == TERM_SEARCH &&
|
if (validationMode.value == TERM_SEARCH &&
|
||||||
validationSearchTerm.value.isNullOrEmpty()
|
validationSearchTerm.value.isNullOrEmpty()
|
||||||
) {
|
) {
|
||||||
errorCount++
|
errorCount++
|
||||||
validationSearchTermError.value = R.string.please_enter_search_term
|
validationSearchTermError.value = R.string.please_enter_search_term
|
||||||
validationScriptError.value = null
|
validationScriptError.value = null
|
||||||
} else if (validationMode == JAVASCRIPT &&
|
} else if (validationMode.value == JAVASCRIPT &&
|
||||||
validationScript.value.isNullOrEmpty()
|
validationScript.value.isNullOrEmpty()
|
||||||
) {
|
) {
|
||||||
errorCount++
|
errorCount++
|
||||||
|
|
|
@ -25,7 +25,9 @@ import com.google.common.truth.Truth.assertWithMessage
|
||||||
class TestLiveData<T>(data: LiveData<T>) {
|
class TestLiveData<T>(data: LiveData<T>) {
|
||||||
|
|
||||||
private val receivedValues = mutableListOf<T>()
|
private val receivedValues = mutableListOf<T>()
|
||||||
private val observer = Observer<T> { receivedValues.add(it) }
|
private val observer = Observer<T> { emission ->
|
||||||
|
emission?.let { receivedValues.add(it) }
|
||||||
|
}
|
||||||
|
|
||||||
init {
|
init {
|
||||||
data.observeForever(observer)
|
data.observeForever(observer)
|
||||||
|
|
|
@ -15,287 +15,411 @@
|
||||||
*/
|
*/
|
||||||
package com.afollestad.nocknock.ui.addsite
|
package com.afollestad.nocknock.ui.addsite
|
||||||
|
|
||||||
//import com.afollestad.nocknock.R.string
|
import androidx.arch.core.executor.testing.InstantTaskExecutorRule
|
||||||
//import com.afollestad.nocknock.data.model.Site
|
import com.afollestad.nocknock.R
|
||||||
//import com.afollestad.nocknock.data.model.SiteSettings
|
import com.afollestad.nocknock.data.model.Site
|
||||||
//import com.afollestad.nocknock.data.model.ValidationMode.JAVASCRIPT
|
import com.afollestad.nocknock.data.model.SiteSettings
|
||||||
//import com.afollestad.nocknock.data.model.ValidationMode.STATUS_CODE
|
import com.afollestad.nocknock.data.model.ValidationMode.JAVASCRIPT
|
||||||
//import com.afollestad.nocknock.data.model.ValidationMode.TERM_SEARCH
|
import com.afollestad.nocknock.data.model.ValidationMode.STATUS_CODE
|
||||||
//import com.afollestad.nocknock.engine.validation.ValidationManager
|
import com.afollestad.nocknock.data.model.ValidationMode.TERM_SEARCH
|
||||||
//import com.afollestad.nocknock.mockDatabase
|
import com.afollestad.nocknock.engine.validation.ValidationManager
|
||||||
//import com.afollestad.nocknock.ui.addsite.AddSiteView
|
import com.afollestad.nocknock.mockDatabase
|
||||||
//import com.afollestad.nocknock.ui.addsite.InputErrors
|
import com.afollestad.nocknock.test
|
||||||
//import com.afollestad.nocknock.ui.addsite.RealAddSitePresenter
|
import com.google.common.truth.Truth.assertThat
|
||||||
//import com.afollestad.nocknock.utilities.ext.ScopeReceiver
|
import com.nhaarman.mockitokotlin2.any
|
||||||
//import com.google.common.truth.Truth.assertThat
|
import com.nhaarman.mockitokotlin2.argumentCaptor
|
||||||
//import com.nhaarman.mockitokotlin2.any
|
import com.nhaarman.mockitokotlin2.mock
|
||||||
//import com.nhaarman.mockitokotlin2.argumentCaptor
|
import com.nhaarman.mockitokotlin2.never
|
||||||
//import com.nhaarman.mockitokotlin2.doAnswer
|
import com.nhaarman.mockitokotlin2.verify
|
||||||
//import com.nhaarman.mockitokotlin2.mock
|
import kotlinx.coroutines.Dispatchers
|
||||||
//import com.nhaarman.mockitokotlin2.never
|
import kotlinx.coroutines.ExperimentalCoroutinesApi
|
||||||
//import com.nhaarman.mockitokotlin2.times
|
import kotlinx.coroutines.runBlocking
|
||||||
//import com.nhaarman.mockitokotlin2.verify
|
import org.junit.After
|
||||||
//import com.nhaarman.mockitokotlin2.verifyNoMoreInteractions
|
import org.junit.Rule
|
||||||
//import com.nhaarman.mockitokotlin2.whenever
|
import org.junit.Test
|
||||||
//import kotlinx.coroutines.runBlocking
|
|
||||||
//import org.junit.After
|
/** @author Aidan Follestad (@afollestad) */
|
||||||
//import org.junit.Before
|
@ExperimentalCoroutinesApi
|
||||||
//import org.junit.Test
|
class AddSiteViewModelTest {
|
||||||
//
|
|
||||||
//class AddSiteViewModelTest {
|
private val database = mockDatabase()
|
||||||
//
|
private val validationManager = mock<ValidationManager>()
|
||||||
// private val database = mockDatabase()
|
|
||||||
// private val checkStatusManager = mock<ValidationManager>()
|
@Rule @JvmField val rule = InstantTaskExecutorRule()
|
||||||
// private val view = mock<AddSiteView>()
|
|
||||||
//
|
private val viewModel = AddSiteViewModel(
|
||||||
// private val presenter = RealAddSitePresenter(
|
database,
|
||||||
// database,
|
validationManager,
|
||||||
// checkStatusManager
|
Dispatchers.Unconfined,
|
||||||
// )
|
Dispatchers.Unconfined
|
||||||
//
|
)
|
||||||
// @Before fun setup() {
|
|
||||||
// doAnswer {
|
@After fun tearDown() = viewModel.destroy()
|
||||||
// val exec = it.getArgument<ScopeReceiver>(1)
|
|
||||||
// runBlocking { exec() }
|
@Test fun onUrlWarningVisibility() {
|
||||||
// Unit
|
val urlWarningVisibility = viewModel.onUrlWarningVisibility()
|
||||||
// }.whenever(view)
|
.test()
|
||||||
// .scopeWhileAttached(any(), any())
|
|
||||||
//
|
viewModel.url.value = ""
|
||||||
// presenter.takeView(view)
|
urlWarningVisibility.assertValues(false)
|
||||||
// }
|
|
||||||
//
|
viewModel.url.value = "helloworld"
|
||||||
// @After fun destroy() {
|
urlWarningVisibility.assertValues(true)
|
||||||
// presenter.dropView()
|
|
||||||
// }
|
viewModel.url.value = "http://helloworld.com"
|
||||||
//
|
urlWarningVisibility.assertValues(false)
|
||||||
// @Test fun onUrlInputFocusChange_focused() {
|
|
||||||
// presenter.onUrlInputFocusChange(true, "hello")
|
viewModel.url.value = "ftp://helloworld.com"
|
||||||
// verifyNoMoreInteractions(view)
|
urlWarningVisibility.assertValues(true)
|
||||||
// }
|
}
|
||||||
//
|
|
||||||
// @Test fun onUrlInputFocusChange_empty() {
|
@Test fun onValidationModeDescription() {
|
||||||
// presenter.onUrlInputFocusChange(false, "")
|
val description = viewModel.onValidationModeDescription()
|
||||||
// verifyNoMoreInteractions(view)
|
.test()
|
||||||
// }
|
|
||||||
//
|
viewModel.validationMode.value = STATUS_CODE
|
||||||
// @Test fun onUrlInputFocusChange_notHttpHttps() {
|
description.assertValues(R.string.validation_mode_status_desc)
|
||||||
// presenter.onUrlInputFocusChange(false, "ftp://hello.com")
|
|
||||||
// verify(view).showOrHideUrlSchemeWarning(true)
|
viewModel.validationMode.value = TERM_SEARCH
|
||||||
// }
|
description.assertValues(R.string.validation_mode_term_desc)
|
||||||
//
|
|
||||||
// @Test fun onUrlInputFocusChange_isHttpOrHttps() {
|
viewModel.validationMode.value = JAVASCRIPT
|
||||||
// presenter.onUrlInputFocusChange(false, "http://hello.com")
|
description.assertValues(R.string.validation_mode_javascript_desc)
|
||||||
// presenter.onUrlInputFocusChange(false, "https://hello.com")
|
}
|
||||||
// verify(view, times(2)).showOrHideUrlSchemeWarning(false)
|
|
||||||
// }
|
@Test fun onValidationSearchTermVisibility() {
|
||||||
//
|
val visibility = viewModel.onValidationSearchTermVisibility()
|
||||||
// @Test fun onValidationModeSelected_statusCode() {
|
.test()
|
||||||
// presenter.onValidationModeSelected(0)
|
|
||||||
// verify(view).showOrHideValidationSearchTerm(false)
|
viewModel.validationMode.value = STATUS_CODE
|
||||||
// verify(view).showOrHideScriptInput(false)
|
visibility.assertValues(false)
|
||||||
// verify(view).setValidationModeDescription(
|
|
||||||
// string.validation_mode_status_desc
|
viewModel.validationMode.value = TERM_SEARCH
|
||||||
// )
|
visibility.assertValues(true)
|
||||||
// }
|
|
||||||
//
|
viewModel.validationMode.value = JAVASCRIPT
|
||||||
// @Test fun onValidationModeSelected_termSearch() {
|
visibility.assertValues(false)
|
||||||
// presenter.onValidationModeSelected(1)
|
}
|
||||||
// verify(view).showOrHideValidationSearchTerm(true)
|
|
||||||
// verify(view).showOrHideScriptInput(false)
|
@Test fun onValidationScriptVisibility() {
|
||||||
// verify(view).setValidationModeDescription(
|
val visibility = viewModel.onValidationScriptVisibility()
|
||||||
// string.validation_mode_term_desc
|
.test()
|
||||||
// )
|
|
||||||
// }
|
viewModel.validationMode.value = STATUS_CODE
|
||||||
//
|
visibility.assertValues(false)
|
||||||
// @Test fun onValidationModeSelected_javaScript() {
|
|
||||||
// presenter.onValidationModeSelected(2)
|
viewModel.validationMode.value = TERM_SEARCH
|
||||||
// verify(view).showOrHideValidationSearchTerm(false)
|
visibility.assertValues(false)
|
||||||
// verify(view).showOrHideScriptInput(true)
|
|
||||||
// verify(view).setValidationModeDescription(
|
viewModel.validationMode.value = JAVASCRIPT
|
||||||
// string.validation_mode_javascript_desc
|
visibility.assertValues(true)
|
||||||
// )
|
}
|
||||||
// }
|
|
||||||
//
|
@Test fun getCheckIntervalMs() {
|
||||||
// @Test(expected = IllegalStateException::class)
|
viewModel.checkIntervalValue.value = 3
|
||||||
// fun onValidationModeSelected_other() {
|
viewModel.checkIntervalUnit.value = 200
|
||||||
// presenter.onValidationModeSelected(3)
|
assertThat(viewModel.getCheckIntervalMs()).isEqualTo(600L)
|
||||||
// }
|
}
|
||||||
//
|
|
||||||
// @Test fun commit_nameError() {
|
@Test fun getValidationArgs() {
|
||||||
// presenter.commit(
|
viewModel.validationSearchTerm.value = "One"
|
||||||
// "",
|
viewModel.validationScript.value = "Two"
|
||||||
// "https://test.com",
|
|
||||||
// 1,
|
viewModel.validationMode.value = STATUS_CODE
|
||||||
// STATUS_CODE,
|
assertThat(viewModel.getValidationArgs()).isNull()
|
||||||
// null,
|
|
||||||
// 60000
|
viewModel.validationMode.value = TERM_SEARCH
|
||||||
// )
|
assertThat(viewModel.getValidationArgs()).isEqualTo("One")
|
||||||
//
|
|
||||||
// val inputErrorsCaptor = argumentCaptor<InputErrors>()
|
viewModel.validationMode.value = JAVASCRIPT
|
||||||
// verify(view).setInputErrors(inputErrorsCaptor.capture())
|
assertThat(viewModel.getValidationArgs()).isEqualTo("Two")
|
||||||
// verify(checkStatusManager, never())
|
}
|
||||||
// .scheduleCheck(any(), any(), any(), any())
|
|
||||||
//
|
@Test fun commit_nameError() {
|
||||||
// val errors = inputErrorsCaptor.firstValue
|
val onNameError = viewModel.onNameError()
|
||||||
// assertThat(errors.name).isEqualTo(string.please_enter_name)
|
.test()
|
||||||
// }
|
val onUrlError = viewModel.onUrlError()
|
||||||
//
|
.test()
|
||||||
// @Test fun commit_urlEmptyError() {
|
val onTimeoutError = viewModel.onTimeoutError()
|
||||||
// presenter.commit(
|
.test()
|
||||||
// "Testing",
|
val onCheckIntervalError = viewModel.onCheckIntervalError()
|
||||||
// "",
|
.test()
|
||||||
// 1,
|
val onSearchTermError = viewModel.onValidationSearchTermError()
|
||||||
// STATUS_CODE,
|
.test()
|
||||||
// null,
|
val onScriptError = viewModel.onValidationScriptError()
|
||||||
// 60000
|
.test()
|
||||||
// )
|
|
||||||
//
|
fillInModel().apply {
|
||||||
// val inputErrorsCaptor = argumentCaptor<InputErrors>()
|
name.value = ""
|
||||||
// verify(view).setInputErrors(inputErrorsCaptor.capture())
|
}
|
||||||
// verify(checkStatusManager, never())
|
val onDone = mock<() -> Unit>()
|
||||||
// .scheduleCheck(any(), any(), any(), any())
|
viewModel.commit(onDone)
|
||||||
//
|
|
||||||
// val errors = inputErrorsCaptor.firstValue
|
verify(validationManager, never())
|
||||||
// assertThat(errors.url).isEqualTo(string.please_enter_url)
|
.scheduleCheck(any(), any(), any(), any())
|
||||||
// }
|
onNameError.assertValues(R.string.please_enter_name)
|
||||||
//
|
onUrlError.assertNoValues()
|
||||||
// @Test fun commit_urlFormatError() {
|
onTimeoutError.assertNoValues()
|
||||||
// presenter.commit(
|
onCheckIntervalError.assertNoValues()
|
||||||
// "Testing",
|
onSearchTermError.assertNoValues()
|
||||||
// "ftp://hello.com",
|
onScriptError.assertNoValues()
|
||||||
// 1,
|
|
||||||
// STATUS_CODE,
|
verify(onDone, never()).invoke()
|
||||||
// null,
|
}
|
||||||
// 60000
|
|
||||||
// )
|
@Test fun commit_urlEmptyError() {
|
||||||
//
|
val onNameError = viewModel.onNameError()
|
||||||
// val inputErrorsCaptor = argumentCaptor<InputErrors>()
|
.test()
|
||||||
// verify(view).setInputErrors(inputErrorsCaptor.capture())
|
val onUrlError = viewModel.onUrlError()
|
||||||
// verify(checkStatusManager, never())
|
.test()
|
||||||
// .scheduleCheck(any(), any(), any(), any())
|
val onTimeoutError = viewModel.onTimeoutError()
|
||||||
//
|
.test()
|
||||||
// val errors = inputErrorsCaptor.firstValue
|
val onCheckIntervalError = viewModel.onCheckIntervalError()
|
||||||
// assertThat(errors.url).isEqualTo(string.please_enter_valid_url)
|
.test()
|
||||||
// }
|
val onSearchTermError = viewModel.onValidationSearchTermError()
|
||||||
//
|
.test()
|
||||||
// @Test fun commit_checkIntervalError() {
|
val onScriptError = viewModel.onValidationScriptError()
|
||||||
// presenter.commit(
|
.test()
|
||||||
// "Testing",
|
|
||||||
// "https://hello.com",
|
fillInModel().apply {
|
||||||
// -1,
|
url.value = ""
|
||||||
// STATUS_CODE,
|
}
|
||||||
// null,
|
val onDone = mock<() -> Unit>()
|
||||||
// 60000
|
viewModel.commit(onDone)
|
||||||
// )
|
|
||||||
//
|
verify(validationManager, never())
|
||||||
// val inputErrorsCaptor = argumentCaptor<InputErrors>()
|
.scheduleCheck(any(), any(), any(), any())
|
||||||
// verify(view).setInputErrors(inputErrorsCaptor.capture())
|
onNameError.assertNoValues()
|
||||||
// verify(checkStatusManager, never())
|
onUrlError.assertValues(R.string.please_enter_url)
|
||||||
// .scheduleCheck(any(), any(), any(), any())
|
onTimeoutError.assertNoValues()
|
||||||
//
|
onCheckIntervalError.assertNoValues()
|
||||||
// val errors = inputErrorsCaptor.firstValue
|
onSearchTermError.assertNoValues()
|
||||||
// assertThat(errors.checkInterval).isEqualTo(
|
onScriptError.assertNoValues()
|
||||||
// string.please_enter_check_interval
|
|
||||||
// )
|
verify(onDone, never()).invoke()
|
||||||
// }
|
}
|
||||||
//
|
|
||||||
// @Test fun commit_termSearchError() {
|
@Test fun commit_urlFormatError() {
|
||||||
// presenter.commit(
|
val onNameError = viewModel.onNameError()
|
||||||
// "Testing",
|
.test()
|
||||||
// "https://hello.com",
|
val onUrlError = viewModel.onUrlError()
|
||||||
// 1,
|
.test()
|
||||||
// TERM_SEARCH,
|
val onTimeoutError = viewModel.onTimeoutError()
|
||||||
// null,
|
.test()
|
||||||
// 60000
|
val onCheckIntervalError = viewModel.onCheckIntervalError()
|
||||||
// )
|
.test()
|
||||||
//
|
val onSearchTermError = viewModel.onValidationSearchTermError()
|
||||||
// val inputErrorsCaptor = argumentCaptor<InputErrors>()
|
.test()
|
||||||
// verify(view).setInputErrors(inputErrorsCaptor.capture())
|
val onScriptError = viewModel.onValidationScriptError()
|
||||||
// verify(checkStatusManager, never())
|
.test()
|
||||||
// .scheduleCheck(any(), any(), any(), any())
|
|
||||||
//
|
fillInModel().apply {
|
||||||
// val errors = inputErrorsCaptor.firstValue
|
url.value = "ftp://www.idk.com"
|
||||||
// assertThat(errors.termSearch).isEqualTo(
|
}
|
||||||
// string.please_enter_search_term
|
val onDone = mock<() -> Unit>()
|
||||||
// )
|
viewModel.commit(onDone)
|
||||||
// }
|
|
||||||
//
|
verify(validationManager, never())
|
||||||
// @Test fun commit_networkTimeout_error() {
|
.scheduleCheck(any(), any(), any(), any())
|
||||||
// presenter.commit(
|
onNameError.assertNoValues()
|
||||||
// "Testing",
|
onUrlError.assertValues(R.string.please_enter_valid_url)
|
||||||
// "https://hello.com",
|
onTimeoutError.assertNoValues()
|
||||||
// 1,
|
onCheckIntervalError.assertNoValues()
|
||||||
// STATUS_CODE,
|
onSearchTermError.assertNoValues()
|
||||||
// null,
|
onScriptError.assertNoValues()
|
||||||
// 0
|
|
||||||
// )
|
verify(onDone, never()).invoke()
|
||||||
//
|
}
|
||||||
// val inputErrorsCaptor = argumentCaptor<InputErrors>()
|
|
||||||
// verify(view).setInputErrors(inputErrorsCaptor.capture())
|
@Test fun commit_networkTimeout_error() {
|
||||||
// verify(checkStatusManager, never())
|
val onNameError = viewModel.onNameError()
|
||||||
// .scheduleCheck(any(), any(), any(), any())
|
.test()
|
||||||
//
|
val onUrlError = viewModel.onUrlError()
|
||||||
// val errors = inputErrorsCaptor.firstValue
|
.test()
|
||||||
// assertThat(errors.networkTimeout).isEqualTo(
|
val onTimeoutError = viewModel.onTimeoutError()
|
||||||
// string.please_enter_networkTimeout
|
.test()
|
||||||
// )
|
val onCheckIntervalError = viewModel.onCheckIntervalError()
|
||||||
// }
|
.test()
|
||||||
//
|
val onSearchTermError = viewModel.onValidationSearchTermError()
|
||||||
// @Test fun commit_javaScript_error() {
|
.test()
|
||||||
// presenter.commit(
|
val onScriptError = viewModel.onValidationScriptError()
|
||||||
// "Testing",
|
.test()
|
||||||
// "https://hello.com",
|
|
||||||
// 1,
|
fillInModel().apply {
|
||||||
// JAVASCRIPT,
|
timeout.value = 0
|
||||||
// null,
|
}
|
||||||
// 60000
|
val onDone = mock<() -> Unit>()
|
||||||
// )
|
viewModel.commit(onDone)
|
||||||
//
|
|
||||||
// val inputErrorsCaptor = argumentCaptor<InputErrors>()
|
verify(validationManager, never())
|
||||||
// verify(view).setInputErrors(inputErrorsCaptor.capture())
|
.scheduleCheck(any(), any(), any(), any())
|
||||||
// verify(checkStatusManager, never())
|
onNameError.assertNoValues()
|
||||||
// .scheduleCheck(any(), any(), any(), any())
|
onUrlError.assertNoValues()
|
||||||
//
|
onTimeoutError.assertValues(R.string.please_enter_networkTimeout)
|
||||||
// val errors = inputErrorsCaptor.firstValue
|
onCheckIntervalError.assertNoValues()
|
||||||
// assertThat(errors.javaScript).isEqualTo(
|
onSearchTermError.assertNoValues()
|
||||||
// string.please_enter_javaScript
|
onScriptError.assertNoValues()
|
||||||
// )
|
|
||||||
// }
|
verify(onDone, never()).invoke()
|
||||||
//
|
}
|
||||||
// @Test fun commit_success() = runBlocking {
|
|
||||||
// presenter.commit(
|
@Test fun commit_checkIntervalError() {
|
||||||
// "Testing",
|
val onNameError = viewModel.onNameError()
|
||||||
// "https://hello.com",
|
.test()
|
||||||
// 1,
|
val onUrlError = viewModel.onUrlError()
|
||||||
// STATUS_CODE,
|
.test()
|
||||||
// null,
|
val onTimeoutError = viewModel.onTimeoutError()
|
||||||
// 60000
|
.test()
|
||||||
// )
|
val onCheckIntervalError = viewModel.onCheckIntervalError()
|
||||||
//
|
.test()
|
||||||
// val siteCaptor = argumentCaptor<Site>()
|
val onSearchTermError = viewModel.onValidationSearchTermError()
|
||||||
// val settingsCaptor = argumentCaptor<SiteSettings>()
|
.test()
|
||||||
//
|
val onScriptError = viewModel.onValidationScriptError()
|
||||||
// verify(view).setLoading()
|
.test()
|
||||||
// verify(database.siteDao()).insert(siteCaptor.capture())
|
|
||||||
// verify(database.siteSettingsDao()).insert(settingsCaptor.capture())
|
fillInModel().apply {
|
||||||
// verify(database.validationResultsDao(), never()).insert(any())
|
checkIntervalValue.value = 0
|
||||||
//
|
}
|
||||||
// val settings = settingsCaptor.firstValue
|
val onDone = mock<() -> Unit>()
|
||||||
// val model = siteCaptor.firstValue.copy(
|
viewModel.commit(onDone)
|
||||||
// id = 1, // fill it in because our insert captor doesn't catch this
|
|
||||||
// settings = settings,
|
verify(validationManager, never())
|
||||||
// lastResult = null
|
.scheduleCheck(any(), any(), any(), any())
|
||||||
// )
|
onNameError.assertNoValues()
|
||||||
//
|
onUrlError.assertNoValues()
|
||||||
// verify(view, never()).setInputErrors(any())
|
onTimeoutError.assertNoValues()
|
||||||
// verify(checkStatusManager).scheduleCheck(
|
onCheckIntervalError.assertValues(R.string.please_enter_check_interval)
|
||||||
// site = model,
|
onSearchTermError.assertNoValues()
|
||||||
// rightNow = true,
|
onScriptError.assertNoValues()
|
||||||
// cancelPrevious = true,
|
|
||||||
// fromFinishingJob = false
|
verify(onDone, never()).invoke()
|
||||||
// )
|
}
|
||||||
//
|
|
||||||
// verify(view).setDoneLoading()
|
@Test fun commit_termSearchError() {
|
||||||
// verify(view).onSiteAdded()
|
val onNameError = viewModel.onNameError()
|
||||||
// }
|
.test()
|
||||||
//}
|
val onUrlError = viewModel.onUrlError()
|
||||||
|
.test()
|
||||||
|
val onTimeoutError = viewModel.onTimeoutError()
|
||||||
|
.test()
|
||||||
|
val onCheckIntervalError = viewModel.onCheckIntervalError()
|
||||||
|
.test()
|
||||||
|
val onSearchTermError = viewModel.onValidationSearchTermError()
|
||||||
|
.test()
|
||||||
|
val onScriptError = viewModel.onValidationScriptError()
|
||||||
|
.test()
|
||||||
|
|
||||||
|
fillInModel().apply {
|
||||||
|
validationMode.value = TERM_SEARCH
|
||||||
|
validationSearchTerm.value = ""
|
||||||
|
}
|
||||||
|
val onDone = mock<() -> Unit>()
|
||||||
|
viewModel.commit(onDone)
|
||||||
|
|
||||||
|
verify(validationManager, never())
|
||||||
|
.scheduleCheck(any(), any(), any(), any())
|
||||||
|
onNameError.assertNoValues()
|
||||||
|
onUrlError.assertNoValues()
|
||||||
|
onTimeoutError.assertNoValues()
|
||||||
|
onCheckIntervalError.assertNoValues()
|
||||||
|
onSearchTermError.assertValues(R.string.please_enter_search_term)
|
||||||
|
onScriptError.assertNoValues()
|
||||||
|
|
||||||
|
verify(onDone, never()).invoke()
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test fun commit_javaScript_error() {
|
||||||
|
val onNameError = viewModel.onNameError()
|
||||||
|
.test()
|
||||||
|
val onUrlError = viewModel.onUrlError()
|
||||||
|
.test()
|
||||||
|
val onTimeoutError = viewModel.onTimeoutError()
|
||||||
|
.test()
|
||||||
|
val onCheckIntervalError = viewModel.onCheckIntervalError()
|
||||||
|
.test()
|
||||||
|
val onSearchTermError = viewModel.onValidationSearchTermError()
|
||||||
|
.test()
|
||||||
|
val onScriptError = viewModel.onValidationScriptError()
|
||||||
|
.test()
|
||||||
|
|
||||||
|
fillInModel().apply {
|
||||||
|
validationMode.value = JAVASCRIPT
|
||||||
|
validationScript.value = ""
|
||||||
|
}
|
||||||
|
val onDone = mock<() -> Unit>()
|
||||||
|
viewModel.commit(onDone)
|
||||||
|
|
||||||
|
verify(validationManager, never())
|
||||||
|
.scheduleCheck(any(), any(), any(), any())
|
||||||
|
onNameError.assertNoValues()
|
||||||
|
onUrlError.assertNoValues()
|
||||||
|
onTimeoutError.assertNoValues()
|
||||||
|
onCheckIntervalError.assertNoValues()
|
||||||
|
onSearchTermError.assertNoValues()
|
||||||
|
onScriptError.assertValues(R.string.please_enter_javaScript)
|
||||||
|
|
||||||
|
verify(onDone, never()).invoke()
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test fun commit_success() = runBlocking {
|
||||||
|
val isLoading = viewModel.onIsLoading()
|
||||||
|
.test()
|
||||||
|
val onNameError = viewModel.onNameError()
|
||||||
|
.test()
|
||||||
|
val onUrlError = viewModel.onUrlError()
|
||||||
|
.test()
|
||||||
|
val onTimeoutError = viewModel.onTimeoutError()
|
||||||
|
.test()
|
||||||
|
val onSearchTermError = viewModel.onValidationSearchTermError()
|
||||||
|
.test()
|
||||||
|
val onScriptError = viewModel.onValidationScriptError()
|
||||||
|
.test()
|
||||||
|
val onCheckIntervalError = viewModel.onCheckIntervalError()
|
||||||
|
.test()
|
||||||
|
|
||||||
|
fillInModel()
|
||||||
|
val onDone = mock<() -> Unit>()
|
||||||
|
viewModel.commit(onDone)
|
||||||
|
|
||||||
|
val siteCaptor = argumentCaptor<Site>()
|
||||||
|
val settingsCaptor = argumentCaptor<SiteSettings>()
|
||||||
|
|
||||||
|
isLoading.assertValues(true, false)
|
||||||
|
verify(database.siteDao()).insert(siteCaptor.capture())
|
||||||
|
verify(database.siteSettingsDao()).insert(settingsCaptor.capture())
|
||||||
|
verify(database.validationResultsDao(), never()).insert(any())
|
||||||
|
|
||||||
|
val settings = settingsCaptor.firstValue
|
||||||
|
val model = siteCaptor.firstValue.copy(
|
||||||
|
id = 1, // fill it in because our insert captor doesn't catch this
|
||||||
|
settings = settings,
|
||||||
|
lastResult = null
|
||||||
|
)
|
||||||
|
|
||||||
|
verify(validationManager).scheduleCheck(
|
||||||
|
site = model,
|
||||||
|
rightNow = true,
|
||||||
|
cancelPrevious = true,
|
||||||
|
fromFinishingJob = false
|
||||||
|
)
|
||||||
|
onNameError.assertNoValues()
|
||||||
|
onUrlError.assertNoValues()
|
||||||
|
onTimeoutError.assertNoValues()
|
||||||
|
onCheckIntervalError.assertNoValues()
|
||||||
|
onSearchTermError.assertNoValues()
|
||||||
|
onScriptError.assertNoValues()
|
||||||
|
|
||||||
|
verify(onDone).invoke()
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun fillInModel() = viewModel.apply {
|
||||||
|
name.value = "Welcome to Wakanda"
|
||||||
|
url.value = "https://www.wakanda.gov"
|
||||||
|
timeout.value = 10000
|
||||||
|
validationMode.value = TERM_SEARCH
|
||||||
|
validationSearchTerm.value = "T'Challa"
|
||||||
|
validationScript.value = null
|
||||||
|
checkIntervalValue.value = 60
|
||||||
|
checkIntervalUnit.value = 1000
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -27,12 +27,14 @@ import com.afollestad.nocknock.test
|
||||||
import com.nhaarman.mockitokotlin2.mock
|
import com.nhaarman.mockitokotlin2.mock
|
||||||
import com.nhaarman.mockitokotlin2.verify
|
import com.nhaarman.mockitokotlin2.verify
|
||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.Dispatchers
|
||||||
|
import kotlinx.coroutines.ExperimentalCoroutinesApi
|
||||||
import kotlinx.coroutines.runBlocking
|
import kotlinx.coroutines.runBlocking
|
||||||
import org.junit.After
|
import org.junit.After
|
||||||
import org.junit.Rule
|
import org.junit.Rule
|
||||||
import org.junit.Test
|
import org.junit.Test
|
||||||
|
|
||||||
/** @author Aidan Follestad (@afollestad) */
|
/** @author Aidan Follestad (@afollestad) */
|
||||||
|
@ExperimentalCoroutinesApi
|
||||||
class MainViewModelTest {
|
class MainViewModelTest {
|
||||||
|
|
||||||
private val database = mockDatabase()
|
private val database = mockDatabase()
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue