mirror of
https://github.com/meshtastic/Meshtastic-Android.git
synced 2026-04-20 22:23:37 +00:00
feat: Enhance test coverage (#4847)
Signed-off-by: James Rich <2199651+jamesarich@users.noreply.github.com>
This commit is contained in:
parent
1b0dc75dfe
commit
06b9f8c77a
41 changed files with 1715 additions and 502 deletions
|
|
@ -33,6 +33,8 @@ import kotlinx.coroutines.test.advanceUntilIdle
|
|||
import kotlinx.coroutines.test.resetMain
|
||||
import kotlinx.coroutines.test.runTest
|
||||
import kotlinx.coroutines.test.setMain
|
||||
import org.meshtastic.core.model.ConnectionState
|
||||
import org.meshtastic.core.model.ContactSettings
|
||||
import org.meshtastic.core.model.service.ServiceAction
|
||||
import org.meshtastic.core.repository.CustomEmojiPrefs
|
||||
import org.meshtastic.core.repository.HomoglyphPrefs
|
||||
|
|
@ -71,14 +73,10 @@ class MessageViewModelTest {
|
|||
|
||||
private val testDispatcher = StandardTestDispatcher()
|
||||
|
||||
private val connectionStateFlow =
|
||||
MutableStateFlow<org.meshtastic.core.model.ConnectionState>(
|
||||
org.meshtastic.core.model.ConnectionState.Disconnected,
|
||||
)
|
||||
private val connectionStateFlow = MutableStateFlow<ConnectionState>(ConnectionState.Disconnected)
|
||||
private val showQuickChatFlow = MutableStateFlow(false)
|
||||
private val customEmojiFrequencyFlow = MutableStateFlow<String?>(null)
|
||||
private val contactSettingsFlow =
|
||||
MutableStateFlow<Map<String, org.meshtastic.core.model.ContactSettings>>(emptyMap())
|
||||
private val contactSettingsFlow = MutableStateFlow<Map<String, ContactSettings>>(emptyMap())
|
||||
|
||||
@BeforeTest
|
||||
fun setUp() {
|
||||
|
|
@ -86,7 +84,7 @@ class MessageViewModelTest {
|
|||
savedStateHandle = SavedStateHandle(mapOf("contactKey" to "0!12345678"))
|
||||
nodeRepository = FakeNodeRepository()
|
||||
|
||||
connectionStateFlow.value = org.meshtastic.core.model.ConnectionState.Disconnected
|
||||
connectionStateFlow.value = ConnectionState.Disconnected
|
||||
showQuickChatFlow.value = false
|
||||
customEmojiFrequencyFlow.value = null
|
||||
contactSettingsFlow.value = emptyMap()
|
||||
|
|
@ -149,9 +147,9 @@ class MessageViewModelTest {
|
|||
@Test
|
||||
fun testConnectionState() = runTest {
|
||||
viewModel.connectionState.test {
|
||||
assertEquals(org.meshtastic.core.model.ConnectionState.Disconnected, awaitItem())
|
||||
connectionStateFlow.value = org.meshtastic.core.model.ConnectionState.Connected
|
||||
assertEquals(org.meshtastic.core.model.ConnectionState.Connected, awaitItem())
|
||||
assertEquals(ConnectionState.Disconnected, awaitItem())
|
||||
connectionStateFlow.value = ConnectionState.Connected
|
||||
assertEquals(ConnectionState.Connected, awaitItem())
|
||||
cancelAndIgnoreRemainingEvents()
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,93 @@
|
|||
/*
|
||||
* Copyright (c) 2026 Meshtastic LLC
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package org.meshtastic.feature.messaging
|
||||
|
||||
import app.cash.turbine.test
|
||||
import dev.mokkery.MockMode
|
||||
import dev.mokkery.answering.returns
|
||||
import dev.mokkery.every
|
||||
import dev.mokkery.everySuspend
|
||||
import dev.mokkery.matcher.any
|
||||
import dev.mokkery.mock
|
||||
import dev.mokkery.verifySuspend
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.ExperimentalCoroutinesApi
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.test.UnconfinedTestDispatcher
|
||||
import kotlinx.coroutines.test.resetMain
|
||||
import kotlinx.coroutines.test.runTest
|
||||
import kotlinx.coroutines.test.setMain
|
||||
import org.meshtastic.core.database.entity.QuickChatAction
|
||||
import org.meshtastic.core.repository.QuickChatActionRepository
|
||||
import kotlin.test.AfterTest
|
||||
import kotlin.test.BeforeTest
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertEquals
|
||||
import kotlin.test.assertNotNull
|
||||
|
||||
@OptIn(ExperimentalCoroutinesApi::class)
|
||||
class QuickChatViewModelTest {
|
||||
|
||||
private val testDispatcher = UnconfinedTestDispatcher()
|
||||
private lateinit var viewModel: QuickChatViewModel
|
||||
private val quickChatActionRepository: QuickChatActionRepository = mock(MockMode.autofill)
|
||||
|
||||
@BeforeTest
|
||||
fun setUp() {
|
||||
Dispatchers.setMain(testDispatcher)
|
||||
every { quickChatActionRepository.getAllActions() } returns MutableStateFlow(emptyList())
|
||||
viewModel = QuickChatViewModel(quickChatActionRepository)
|
||||
}
|
||||
|
||||
@AfterTest
|
||||
fun tearDown() {
|
||||
Dispatchers.resetMain()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testInitialization() {
|
||||
assertNotNull(viewModel)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `quickChatActions reflects repository updates`() = runTest(testDispatcher) {
|
||||
val actionsFlow = MutableStateFlow<List<QuickChatAction>>(emptyList())
|
||||
every { quickChatActionRepository.getAllActions() } returns actionsFlow
|
||||
|
||||
// Re-init
|
||||
viewModel = QuickChatViewModel(quickChatActionRepository)
|
||||
|
||||
viewModel.quickChatActions.test {
|
||||
assertEquals(emptyList(), awaitItem())
|
||||
val action = QuickChatAction(uuid = 1L, name = "Test", message = "Hello", position = 0)
|
||||
actionsFlow.value = listOf(action)
|
||||
assertEquals(listOf(action), awaitItem())
|
||||
cancelAndIgnoreRemainingEvents()
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `addQuickChatAction delegates to repository`() = runTest(testDispatcher) {
|
||||
val action = QuickChatAction(uuid = 1L, name = "Test", message = "Hello", position = 0)
|
||||
everySuspend { quickChatActionRepository.upsert(any()) } returns Unit
|
||||
|
||||
val job = viewModel.addQuickChatAction(action)
|
||||
job.join()
|
||||
|
||||
verifySuspend { quickChatActionRepository.upsert(action) }
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,100 @@
|
|||
/*
|
||||
* Copyright (c) 2026 Meshtastic LLC
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package org.meshtastic.feature.messaging.ui.contact
|
||||
|
||||
import app.cash.turbine.test
|
||||
import dev.mokkery.MockMode
|
||||
import dev.mokkery.answering.returns
|
||||
import dev.mokkery.every
|
||||
import dev.mokkery.mock
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.ExperimentalCoroutinesApi
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.test.UnconfinedTestDispatcher
|
||||
import kotlinx.coroutines.test.resetMain
|
||||
import kotlinx.coroutines.test.runTest
|
||||
import kotlinx.coroutines.test.setMain
|
||||
import org.meshtastic.core.model.ConnectionState
|
||||
import org.meshtastic.core.repository.NodeRepository
|
||||
import org.meshtastic.core.repository.PacketRepository
|
||||
import org.meshtastic.core.repository.RadioConfigRepository
|
||||
import org.meshtastic.core.repository.ServiceRepository
|
||||
import org.meshtastic.proto.ChannelSet
|
||||
import kotlin.test.AfterTest
|
||||
import kotlin.test.BeforeTest
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertEquals
|
||||
import kotlin.test.assertNotNull
|
||||
|
||||
@OptIn(ExperimentalCoroutinesApi::class)
|
||||
class ContactsViewModelTest {
|
||||
|
||||
private val testDispatcher = UnconfinedTestDispatcher()
|
||||
private lateinit var viewModel: ContactsViewModel
|
||||
private val nodeRepository: NodeRepository = mock(MockMode.autofill)
|
||||
private val packetRepository: PacketRepository = mock(MockMode.autofill)
|
||||
private val radioConfigRepository: RadioConfigRepository = mock(MockMode.autofill)
|
||||
private val serviceRepository: ServiceRepository = mock(MockMode.autofill)
|
||||
|
||||
@BeforeTest
|
||||
fun setUp() {
|
||||
Dispatchers.setMain(testDispatcher)
|
||||
|
||||
every { nodeRepository.ourNodeInfo } returns MutableStateFlow(null)
|
||||
every { nodeRepository.myNodeInfo } returns MutableStateFlow(null)
|
||||
every { nodeRepository.myId } returns MutableStateFlow(null)
|
||||
every { nodeRepository.getNodes() } returns MutableStateFlow(emptyList())
|
||||
|
||||
every { serviceRepository.connectionState } returns MutableStateFlow(ConnectionState.Disconnected)
|
||||
every { packetRepository.getUnreadCountTotal() } returns MutableStateFlow(0)
|
||||
every { radioConfigRepository.channelSetFlow } returns MutableStateFlow(ChannelSet())
|
||||
|
||||
viewModel =
|
||||
ContactsViewModel(
|
||||
nodeRepository = nodeRepository,
|
||||
packetRepository = packetRepository,
|
||||
radioConfigRepository = radioConfigRepository,
|
||||
serviceRepository = serviceRepository,
|
||||
)
|
||||
}
|
||||
|
||||
@AfterTest
|
||||
fun tearDown() {
|
||||
Dispatchers.resetMain()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testInitialization() {
|
||||
assertNotNull(viewModel)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `unreadCountTotal reflects updates from repository`() = runTest(testDispatcher) {
|
||||
val countFlow = MutableStateFlow(0)
|
||||
every { packetRepository.getUnreadCountTotal() } returns countFlow
|
||||
|
||||
// Re-init VM
|
||||
viewModel = ContactsViewModel(nodeRepository, packetRepository, radioConfigRepository, serviceRepository)
|
||||
|
||||
viewModel.unreadCountTotal.test {
|
||||
assertEquals(0, awaitItem())
|
||||
countFlow.value = 5
|
||||
assertEquals(5, awaitItem())
|
||||
cancelAndIgnoreRemainingEvents()
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue