Merge b566b07b35
into e846078f9d
commit
519b7ca4b1
@ -0,0 +1,45 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2025 The Android Open Source Project
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* https://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package com.google.samples.apps.nowinandroid.core.data.test
|
||||||
|
|
||||||
|
import com.google.samples.apps.nowinandroid.core.data.util.ErrorMonitor
|
||||||
|
import com.google.samples.apps.nowinandroid.core.model.data.MessageData
|
||||||
|
import kotlinx.coroutines.flow.Flow
|
||||||
|
import kotlinx.coroutines.flow.flowOf
|
||||||
|
import javax.inject.Inject
|
||||||
|
|
||||||
|
class EmptyErrorMonitor @Inject constructor() : ErrorMonitor {
|
||||||
|
|
||||||
|
override fun addMessageByString(message: String): MessageData {
|
||||||
|
TODO("Not yet implemented")
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun addMessageByData(message: MessageData) {
|
||||||
|
TODO("Not yet implemented")
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun clearMessage(message: MessageData) {
|
||||||
|
TODO("Not yet implemented")
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun clearAllMessages() {
|
||||||
|
TODO("Not yet implemented")
|
||||||
|
}
|
||||||
|
|
||||||
|
override val messages: Flow<List<MessageData?>>
|
||||||
|
get() = flowOf(emptyList())
|
||||||
|
}
|
@ -0,0 +1,35 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2024 The Android Open Source Project
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* https://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package com.google.samples.apps.nowinandroid.core.data.util
|
||||||
|
|
||||||
|
import com.google.samples.apps.nowinandroid.core.model.data.MessageData
|
||||||
|
import kotlinx.coroutines.flow.Flow
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Interface for handling messages.
|
||||||
|
*/
|
||||||
|
interface ErrorMonitor {
|
||||||
|
fun addMessageByString(message: String): MessageData
|
||||||
|
|
||||||
|
fun addMessageByData(message: MessageData)
|
||||||
|
|
||||||
|
fun clearMessage(message: MessageData)
|
||||||
|
|
||||||
|
fun clearAllMessages()
|
||||||
|
|
||||||
|
val messages: Flow<List<MessageData?>>
|
||||||
|
}
|
@ -0,0 +1,67 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2024 The Android Open Source Project
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* https://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package com.google.samples.apps.nowinandroid.core.data.util
|
||||||
|
|
||||||
|
import com.google.samples.apps.nowinandroid.core.model.data.MessageData
|
||||||
|
import com.google.samples.apps.nowinandroid.core.model.data.MessageType
|
||||||
|
import kotlinx.coroutines.flow.MutableStateFlow
|
||||||
|
import kotlinx.coroutines.flow.update
|
||||||
|
import javax.inject.Inject
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Interface implementation for handling general errors.
|
||||||
|
*/
|
||||||
|
|
||||||
|
class StateErrorMonitor @Inject constructor() : ErrorMonitor {
|
||||||
|
/**
|
||||||
|
* List of [MessageData] to be shown
|
||||||
|
*/
|
||||||
|
override val messages = MutableStateFlow<List<MessageData>>(emptyList())
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a [MessageData] and adds it to the list.
|
||||||
|
* @param message: String value for message to add.
|
||||||
|
*/
|
||||||
|
override fun addMessageByString(message: String): MessageData {
|
||||||
|
val data = MessageData(type = MessageType.MESSAGE(message))
|
||||||
|
messages.update { it + data }
|
||||||
|
|
||||||
|
return data
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add a [MessageData] to the list.
|
||||||
|
* @param message: [MessageData] to add.
|
||||||
|
*/
|
||||||
|
override fun addMessageByData(message: MessageData) {
|
||||||
|
messages.update { it + message }
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Removes the [MessageData] from the list.
|
||||||
|
*/
|
||||||
|
override fun clearMessage(message: MessageData) {
|
||||||
|
messages.update { list -> list.filterNot { it == message } }
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Remove all from list, reset to empty list
|
||||||
|
*/
|
||||||
|
override fun clearAllMessages() {
|
||||||
|
messages.update { emptyList() }
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,147 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2024 The Android Open Source Project
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* https://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
import com.google.samples.apps.nowinandroid.core.data.util.StateErrorMonitor
|
||||||
|
import com.google.samples.apps.nowinandroid.core.model.data.MessageData
|
||||||
|
import com.google.samples.apps.nowinandroid.core.model.data.MessageType
|
||||||
|
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
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Unit tests for [StateErrorMonitor].
|
||||||
|
*/
|
||||||
|
|
||||||
|
class StateErrorMonitorTest {
|
||||||
|
|
||||||
|
// Subject under test.
|
||||||
|
private lateinit var state: StateErrorMonitor
|
||||||
|
|
||||||
|
private lateinit var messages: List<MessageData?>
|
||||||
|
|
||||||
|
private val testString = "Test Error Message"
|
||||||
|
private val testData = MessageData(MessageType.MESSAGE("Test Error Message 1"))
|
||||||
|
private val testData2 = MessageData(MessageType.MESSAGE("Test Error Message 2"))
|
||||||
|
|
||||||
|
@Before
|
||||||
|
fun setup() {
|
||||||
|
state = StateErrorMonitor()
|
||||||
|
messages = emptyList()
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun whenErrorIsNotAdded_NullIsPresent() = runTest(UnconfinedTestDispatcher()) {
|
||||||
|
backgroundScope.launch {
|
||||||
|
state.messages.collect {
|
||||||
|
messages = it
|
||||||
|
}
|
||||||
|
}
|
||||||
|
assertEquals(
|
||||||
|
emptyList(),
|
||||||
|
messages,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun whenErrorIsAddedByString_ErrorMessageIsPresent() = runTest(UnconfinedTestDispatcher()) {
|
||||||
|
backgroundScope.launch {
|
||||||
|
state.messages.collect {
|
||||||
|
messages = it
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
val expect = state.addMessageByString(testString)
|
||||||
|
|
||||||
|
assertEquals(
|
||||||
|
expect,
|
||||||
|
messages.firstOrNull(),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun whenErrorIsAddedByData_ErrorMessageIsPresent() = runTest(UnconfinedTestDispatcher()) {
|
||||||
|
backgroundScope.launch {
|
||||||
|
state.messages.collect {
|
||||||
|
messages = it
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
state.addMessageByData(testData)
|
||||||
|
|
||||||
|
assertEquals(
|
||||||
|
testData,
|
||||||
|
messages.firstOrNull(),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun whenErrorsAreAdded_FirstErrorMessageIsPresent() =
|
||||||
|
runTest(UnconfinedTestDispatcher()) {
|
||||||
|
state.addMessageByData(testData)
|
||||||
|
state.addMessageByString(testString)
|
||||||
|
|
||||||
|
backgroundScope.launch {
|
||||||
|
state.messages.collect {
|
||||||
|
messages = it
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
assertEquals(
|
||||||
|
testData,
|
||||||
|
messages.firstOrNull(),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun whenErrorIsCleared_ErrorMessageIsNotPresent() =
|
||||||
|
runTest(UnconfinedTestDispatcher()) {
|
||||||
|
backgroundScope.launch {
|
||||||
|
state.messages.collect {
|
||||||
|
messages = it
|
||||||
|
}
|
||||||
|
}
|
||||||
|
state.addMessageByData(testData)
|
||||||
|
state.clearMessage(testData)
|
||||||
|
|
||||||
|
assertEquals(
|
||||||
|
emptyList(),
|
||||||
|
messages,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun whenErrorsAreCleared_NextErrorMessageIsPresent() =
|
||||||
|
runTest(UnconfinedTestDispatcher()) {
|
||||||
|
backgroundScope.launch {
|
||||||
|
state.messages.collect {
|
||||||
|
messages = it
|
||||||
|
}
|
||||||
|
}
|
||||||
|
state.addMessageByData(testData)
|
||||||
|
state.addMessageByData(testData2)
|
||||||
|
|
||||||
|
state.clearMessage(testData)
|
||||||
|
|
||||||
|
assertEquals(
|
||||||
|
testData2,
|
||||||
|
messages.firstOrNull(),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,37 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2024 The Android Open Source Project
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* https://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package com.google.samples.apps.nowinandroid.core.model.data
|
||||||
|
|
||||||
|
/**
|
||||||
|
* [MessageData]
|
||||||
|
* Class to hold messages type objects with actions
|
||||||
|
*/
|
||||||
|
data class MessageData(
|
||||||
|
val type: MessageType,
|
||||||
|
val label: String? = null,
|
||||||
|
val onConfirm: (() -> Unit)? = null,
|
||||||
|
val onDelay: (() -> Unit)? = null,
|
||||||
|
)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Specified Errors
|
||||||
|
*/
|
||||||
|
sealed class MessageType {
|
||||||
|
data object OFFLINE : MessageType()
|
||||||
|
data class MESSAGE(val value: String) : MessageType()
|
||||||
|
data object UNKNOWN : MessageType()
|
||||||
|
}
|
@ -0,0 +1,71 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2025 The Android Open Source Project
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* https://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package com.google.samples.apps.nowinandroid.core.testing.util
|
||||||
|
|
||||||
|
import com.google.samples.apps.nowinandroid.core.data.util.ErrorMonitor
|
||||||
|
import com.google.samples.apps.nowinandroid.core.model.data.MessageData
|
||||||
|
import com.google.samples.apps.nowinandroid.core.model.data.MessageType.MESSAGE
|
||||||
|
import com.google.samples.apps.nowinandroid.core.model.data.MessageType.OFFLINE
|
||||||
|
import com.google.samples.apps.nowinandroid.core.model.data.MessageType.UNKNOWN
|
||||||
|
import kotlinx.coroutines.flow.Flow
|
||||||
|
import kotlinx.coroutines.flow.MutableStateFlow
|
||||||
|
|
||||||
|
class TestErrorMonitor : ErrorMonitor {
|
||||||
|
|
||||||
|
private val _messages = MutableStateFlow<List<MessageData>>(emptyList())
|
||||||
|
|
||||||
|
override fun addMessageByString(message: String): MessageData {
|
||||||
|
TODO("Not yet implemented")
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun addMessageByData(message: MessageData) {
|
||||||
|
TODO("Not yet implemented")
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun clearMessage(message: MessageData) {
|
||||||
|
TODO("Not yet implemented")
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun clearAllMessages() {
|
||||||
|
_messages.value = emptyList()
|
||||||
|
}
|
||||||
|
|
||||||
|
override val messages: Flow<List<MessageData?>>
|
||||||
|
get() = _messages
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test-only API to add message types
|
||||||
|
*/
|
||||||
|
|
||||||
|
fun setOfflineMessage() {
|
||||||
|
_messages.value = listOf(OFFLINE_MESSAGE)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun addMessage() {
|
||||||
|
_messages.value.plus(MESSAGE_MESSAGE)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun addUnknownMessage() {
|
||||||
|
_messages.value.plus(UNKNOWN_MESSAGE)
|
||||||
|
}
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
val OFFLINE_MESSAGE = MessageData(OFFLINE)
|
||||||
|
val MESSAGE_MESSAGE = MessageData(MESSAGE("Message"), "Title", {}, {})
|
||||||
|
val UNKNOWN_MESSAGE = MessageData(UNKNOWN)
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in new issue