|
|
@ -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(
|
|
|
|