|
|
|
@ -15,11 +15,14 @@
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
import com.google.samples.apps.nowinandroid.core.data.util.ErrorMessage
|
|
|
|
|
import com.google.samples.apps.nowinandroid.core.data.util.NetworkMonitor
|
|
|
|
|
import com.google.samples.apps.nowinandroid.core.data.util.SnackbarErrorMonitor
|
|
|
|
|
import com.google.samples.apps.nowinandroid.core.testing.util.TestNetworkMonitor
|
|
|
|
|
import kotlinx.coroutines.flow.collect
|
|
|
|
|
import kotlinx.coroutines.launch
|
|
|
|
|
import kotlinx.coroutines.test.UnconfinedTestDispatcher
|
|
|
|
|
import kotlinx.coroutines.test.runTest
|
|
|
|
|
import org.junit.Before
|
|
|
|
|
import org.junit.Test
|
|
|
|
|
import kotlin.test.assertEquals
|
|
|
|
|
|
|
|
|
@ -29,14 +32,22 @@ import kotlin.test.assertEquals
|
|
|
|
|
|
|
|
|
|
class SnackbarErrorMonitorTest {
|
|
|
|
|
|
|
|
|
|
lateinit var networkMonitor: NetworkMonitor
|
|
|
|
|
|
|
|
|
|
// Subject under test.
|
|
|
|
|
private var state = SnackbarErrorMonitor()
|
|
|
|
|
private lateinit var state: SnackbarErrorMonitor
|
|
|
|
|
|
|
|
|
|
private var message: ErrorMessage? = null
|
|
|
|
|
|
|
|
|
|
@Before
|
|
|
|
|
fun setup() {
|
|
|
|
|
networkMonitor = TestNetworkMonitor()
|
|
|
|
|
state = SnackbarErrorMonitor(networkMonitor)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
fun whenErrorIsNotAdded_NullIsPresent() = runTest(UnconfinedTestDispatcher()) {
|
|
|
|
|
backgroundScope.launch { state.errorMessages.collect() }
|
|
|
|
|
backgroundScope.launch { state.errorMessage.collect() }
|
|
|
|
|
assertEquals(
|
|
|
|
|
null,
|
|
|
|
|
message,
|
|
|
|
@ -46,12 +57,12 @@ class SnackbarErrorMonitorTest {
|
|
|
|
|
@Test
|
|
|
|
|
fun whenErrorIsAdded_ErrorMessageIsPresent() = runTest(UnconfinedTestDispatcher()) {
|
|
|
|
|
backgroundScope.launch {
|
|
|
|
|
state.errorMessages.collect {
|
|
|
|
|
message = it.firstOrNull()
|
|
|
|
|
state.errorMessage.collect {
|
|
|
|
|
message = it
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
val id = state.addErrorMessage("Test Error Message")
|
|
|
|
|
val id = state.addShortErrorMessage("Test Error Message")
|
|
|
|
|
|
|
|
|
|
assertEquals(
|
|
|
|
|
id,
|
|
|
|
@ -62,12 +73,12 @@ class SnackbarErrorMonitorTest {
|
|
|
|
|
@Test
|
|
|
|
|
fun whenErrorsAreAdded_FirstErrorMessageIsPresent() =
|
|
|
|
|
runTest(UnconfinedTestDispatcher()) {
|
|
|
|
|
val id1 = state.addErrorMessage("Test Error Message 1")
|
|
|
|
|
state.addErrorMessage("Test Error Message 2")
|
|
|
|
|
val id1 = state.addShortErrorMessage("Test Error Message 1")
|
|
|
|
|
state.addShortErrorMessage("Test Error Message 2")
|
|
|
|
|
|
|
|
|
|
backgroundScope.launch {
|
|
|
|
|
state.errorMessages.collect {
|
|
|
|
|
message = it.firstOrNull()
|
|
|
|
|
state.errorMessage.collect {
|
|
|
|
|
message = it
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -81,11 +92,11 @@ class SnackbarErrorMonitorTest {
|
|
|
|
|
fun whenErrorIsCleared_ErrorMessageIsNotPresent() =
|
|
|
|
|
runTest(UnconfinedTestDispatcher()) {
|
|
|
|
|
backgroundScope.launch {
|
|
|
|
|
state.errorMessages.collect {
|
|
|
|
|
message = it.firstOrNull()
|
|
|
|
|
state.errorMessage.collect {
|
|
|
|
|
message = it
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
val id = state.addErrorMessage("Test Error Message 1")
|
|
|
|
|
val id = state.addShortErrorMessage("Test Error Message 1")
|
|
|
|
|
if (id != null) {
|
|
|
|
|
state.clearErrorMessage(id)
|
|
|
|
|
assertEquals(
|
|
|
|
@ -99,12 +110,12 @@ class SnackbarErrorMonitorTest {
|
|
|
|
|
fun whenErrorsAreCleared_NextErrorMessageIsPresent() =
|
|
|
|
|
runTest(UnconfinedTestDispatcher()) {
|
|
|
|
|
backgroundScope.launch {
|
|
|
|
|
state.errorMessages.collect {
|
|
|
|
|
message = it.firstOrNull()
|
|
|
|
|
state.errorMessage.collect {
|
|
|
|
|
message = it
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
val id1 = state.addErrorMessage("Test Error Message 1")
|
|
|
|
|
val id2 = state.addErrorMessage("Test Error Message 2")
|
|
|
|
|
val id1 = state.addShortErrorMessage("Test Error Message 1")
|
|
|
|
|
val id2 = state.addShortErrorMessage("Test Error Message 2")
|
|
|
|
|
if (id1 != null) {
|
|
|
|
|
state.clearErrorMessage(id1)
|
|
|
|
|
assertEquals(
|
|
|
|
|