Update General Error Handling Tests

pull/1461/head
TM 5 months ago
parent d25152b8d1
commit 1d5f10a76b

@ -23,14 +23,41 @@ import kotlinx.coroutines.flow.flowOf
import javax.inject.Inject import javax.inject.Inject
class FakeErrorMonitor @Inject constructor() : ErrorMonitor { class FakeErrorMonitor @Inject constructor() : ErrorMonitor {
override fun addErrorMessage(error: String): String? {
override var offlineMessage: String? = "offline"
override val isOffline: Flow<Boolean>
get() = flowOf(false)
override fun addShortErrorMessage(
error: String,
label: String?,
successAction: (() -> Unit)?,
failureAction: (() -> Unit)?,
): String? {
return "1" return "1"
} }
override fun addLongErrorMessage(
error: String,
label: String?,
successAction: (() -> Unit)?,
failureAction: (() -> Unit)?,
): String? {
return "2"
}
override fun addIndefiniteErrorMessage(
error: String,
label: String?,
successAction: (() -> Unit)?,
failureAction: (() -> Unit)?,
): String? {
return "3"
}
override fun clearErrorMessage(id: String) { override fun clearErrorMessage(id: String) {
// Do nothing // Do nothing
} }
override val errorMessages: Flow<List<ErrorMessage?>> override val errorMessage: Flow<ErrorMessage?>
get() = flowOf(emptyList()) get() = flowOf(null)
} }

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

@ -22,14 +22,40 @@ import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flowOf import kotlinx.coroutines.flow.flowOf
class TestErrorMonitor : ErrorMonitor { class TestErrorMonitor : ErrorMonitor {
override fun addErrorMessage(error: String): String? { override var offlineMessage: String? = "offline"
override val isOffline: Flow<Boolean>
get() = flowOf(false)
override fun addShortErrorMessage(
error: String,
label: String?,
successAction: (() -> Unit)?,
failureAction: (() -> Unit)?,
): String? {
return "1" return "1"
} }
override fun addLongErrorMessage(
error: String,
label: String?,
successAction: (() -> Unit)?,
failureAction: (() -> Unit)?,
): String? {
return "2"
}
override fun addIndefiniteErrorMessage(
error: String,
label: String?,
successAction: (() -> Unit)?,
failureAction: (() -> Unit)?,
): String? {
return "3"
}
override fun clearErrorMessage(id: String) { override fun clearErrorMessage(id: String) {
// Do nothing // Do nothing
} }
override val errorMessages: Flow<List<ErrorMessage?>> override val errorMessage: Flow<ErrorMessage?>
get() = flowOf(listOf(ErrorMessage("Error Message", "1"))) get() = flowOf(ErrorMessage("Error Message", "1"))
} }

Loading…
Cancel
Save