diff --git a/core/data-test/src/main/kotlin/com/google/samples/apps/nowinandroid/core/data/test/FakeErrorMonitor.kt b/core/data-test/src/main/kotlin/com/google/samples/apps/nowinandroid/core/data/test/FakeErrorMonitor.kt index cafa1d84a..2b6681aed 100644 --- a/core/data-test/src/main/kotlin/com/google/samples/apps/nowinandroid/core/data/test/FakeErrorMonitor.kt +++ b/core/data-test/src/main/kotlin/com/google/samples/apps/nowinandroid/core/data/test/FakeErrorMonitor.kt @@ -23,14 +23,41 @@ import kotlinx.coroutines.flow.flowOf import javax.inject.Inject class FakeErrorMonitor @Inject constructor() : ErrorMonitor { - override fun addErrorMessage(error: String): String? { + + override var offlineMessage: String? = "offline" + override val isOffline: Flow + get() = flowOf(false) + override fun addShortErrorMessage( + error: String, + label: String?, + successAction: (() -> Unit)?, + failureAction: (() -> Unit)?, + ): String? { 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) { // Do nothing } - override val errorMessages: Flow> - get() = flowOf(emptyList()) + override val errorMessage: Flow + get() = flowOf(null) } diff --git a/core/data/src/test/kotlin/com/google/samples/apps/nowinandroid/core/data/util/SnackbarErrorMonitorTest.kt b/core/data/src/test/kotlin/com/google/samples/apps/nowinandroid/core/data/util/SnackbarErrorMonitorTest.kt index 639b89092..1faac7f5b 100644 --- a/core/data/src/test/kotlin/com/google/samples/apps/nowinandroid/core/data/util/SnackbarErrorMonitorTest.kt +++ b/core/data/src/test/kotlin/com/google/samples/apps/nowinandroid/core/data/util/SnackbarErrorMonitorTest.kt @@ -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( diff --git a/core/testing/src/main/kotlin/com/google/samples/apps/nowinandroid/core/testing/util/TestErrorMonitor.kt b/core/testing/src/main/kotlin/com/google/samples/apps/nowinandroid/core/testing/util/TestErrorMonitor.kt index b63bf2c02..c6f4ad3fa 100644 --- a/core/testing/src/main/kotlin/com/google/samples/apps/nowinandroid/core/testing/util/TestErrorMonitor.kt +++ b/core/testing/src/main/kotlin/com/google/samples/apps/nowinandroid/core/testing/util/TestErrorMonitor.kt @@ -22,14 +22,40 @@ import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.flowOf class TestErrorMonitor : ErrorMonitor { - override fun addErrorMessage(error: String): String? { + override var offlineMessage: String? = "offline" + override val isOffline: Flow + get() = flowOf(false) + override fun addShortErrorMessage( + error: String, + label: String?, + successAction: (() -> Unit)?, + failureAction: (() -> Unit)?, + ): String? { 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) { // Do nothing } - override val errorMessages: Flow> - get() = flowOf(listOf(ErrorMessage("Error Message", "1"))) + override val errorMessage: Flow + get() = flowOf(ErrorMessage("Error Message", "1")) }