mirror of
https://github.com/meshtastic/Meshtastic-Android.git
synced 2026-03-14 03:46:25 -04:00
feat: add nodelist sort options
This commit is contained in:
@@ -5,10 +5,13 @@ import androidx.test.ext.junit.runners.AndroidJUnit4
|
||||
import androidx.test.platform.app.InstrumentationRegistry
|
||||
import com.geeksville.mesh.database.MeshtasticDatabase
|
||||
import com.geeksville.mesh.database.dao.NodeInfoDao
|
||||
import com.geeksville.mesh.model.NodeSortOption
|
||||
import kotlinx.coroutines.flow.first
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import org.junit.After
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertFalse
|
||||
import org.junit.Assert.assertTrue
|
||||
import org.junit.Before
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
@@ -18,20 +21,20 @@ class NodeDBTest {
|
||||
private lateinit var database: MeshtasticDatabase
|
||||
private lateinit var nodeInfoDao: NodeInfoDao
|
||||
|
||||
private val testNodeNoPosition = NodeInfo(
|
||||
8,
|
||||
MeshUser(
|
||||
private val ourNodeInfo = NodeInfo(
|
||||
num = 8,
|
||||
user = MeshUser(
|
||||
"+16508765308".format(8),
|
||||
"Kevin MesterNoLoc",
|
||||
"Kevin Mester",
|
||||
"KLO",
|
||||
MeshProtos.HardwareModel.ANDROID_SIM,
|
||||
false
|
||||
),
|
||||
null
|
||||
position = Position(30.267153, -97.743057, 35, 123), // Austin
|
||||
)
|
||||
|
||||
private val myNodeInfo: MyNodeInfo = MyNodeInfo(
|
||||
myNodeNum = testNodeNoPosition.num,
|
||||
myNodeNum = ourNodeInfo.num,
|
||||
hasGPS = false,
|
||||
model = null,
|
||||
firmwareVersion = null,
|
||||
@@ -47,22 +50,29 @@ class NodeDBTest {
|
||||
)
|
||||
|
||||
private val testPositions = arrayOf(
|
||||
Position(32.776665, -96.796989, 35, 123), // dallas
|
||||
Position(32.960758, -96.733521, 35, 456), // richardson
|
||||
Position(32.912901, -96.781776, 35, 789), // north dallas
|
||||
Position(32.776665, -96.796989, 35, 123), // Dallas
|
||||
Position(32.960758, -96.733521, 35, 456), // Richardson
|
||||
Position(32.912901, -96.781776, 35, 789), // North Dallas
|
||||
Position(29.760427, -95.369804, 35, 123), // Houston
|
||||
Position(33.748997, -84.387985, 35, 456), // Atlanta
|
||||
Position(34.052235, -118.243683, 35, 789), // Los Angeles
|
||||
Position(40.712776, -74.005974, 35, 123), // New York City
|
||||
Position(41.878113, -87.629799, 35, 456), // Chicago
|
||||
Position(39.952583, -75.165222, 35, 789), // Philadelphia
|
||||
)
|
||||
|
||||
private val testNodes = listOf(testNodeNoPosition) + testPositions.mapIndexed { index, it ->
|
||||
private val testNodes = listOf(ourNodeInfo) + testPositions.mapIndexed { index, it ->
|
||||
NodeInfo(
|
||||
9 + index,
|
||||
MeshUser(
|
||||
num = 9 + index,
|
||||
user = MeshUser(
|
||||
"+165087653%02d".format(9 + index),
|
||||
"Kevin Mester$index",
|
||||
"KM$index",
|
||||
MeshProtos.HardwareModel.ANDROID_SIM,
|
||||
if (index == 2) MeshProtos.HardwareModel.UNSET else MeshProtos.HardwareModel.ANDROID_SIM,
|
||||
false
|
||||
),
|
||||
it
|
||||
position = it,
|
||||
lastHeard = 9 + index,
|
||||
)
|
||||
}
|
||||
|
||||
@@ -72,7 +82,7 @@ class NodeDBTest {
|
||||
database = Room.inMemoryDatabaseBuilder(context, MeshtasticDatabase::class.java).build()
|
||||
nodeInfoDao = database.nodeInfoDao()
|
||||
|
||||
nodeInfoDao.apply{
|
||||
nodeInfoDao.apply {
|
||||
putAll(testNodes)
|
||||
setMyNodeInfo(myNodeInfo)
|
||||
}
|
||||
@@ -83,22 +93,83 @@ class NodeDBTest {
|
||||
database.close()
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves a list of nodes based on [sort], [filter] and [includeUnknown] parameters.
|
||||
* The list excludes [ourNodeInfo] (our NodeInfo) to ensure consistency in the results.
|
||||
*/
|
||||
private suspend fun getNodes(
|
||||
sort: NodeSortOption = NodeSortOption.LAST_HEARD,
|
||||
filter: String = "",
|
||||
includeUnknown: Boolean = true,
|
||||
) = nodeInfoDao.getNodes(
|
||||
sort = sort.sqlValue,
|
||||
filter = filter,
|
||||
includeUnknown = includeUnknown,
|
||||
unknownHwModel = MeshProtos.HardwareModel.UNSET
|
||||
).first().filter { it != ourNodeInfo }
|
||||
|
||||
@Test // node list size
|
||||
fun testNodeListSize() = runBlocking {
|
||||
val nodes = nodeInfoDao.nodeDBbyNum().first()
|
||||
assertEquals(nodes.size, 4)
|
||||
assertEquals(10, nodes.size)
|
||||
}
|
||||
|
||||
@Test // nodeDBbyNum() re-orders our node at the top of the list
|
||||
fun testOurNodeIntoIsFirst() = runBlocking {
|
||||
fun testOurNodeInfoIsFirst() = runBlocking {
|
||||
val nodes = nodeInfoDao.nodeDBbyNum().first()
|
||||
assertEquals(nodes.values.first(), testNodeNoPosition)
|
||||
assertEquals(ourNodeInfo, nodes.values.first())
|
||||
}
|
||||
|
||||
@Test // getNodeInfo()
|
||||
fun testGetNodeInfo() = runBlocking {
|
||||
for (node in nodeInfoDao.getNodes().first()) {
|
||||
assertEquals(nodeInfoDao.getNodeInfo(node.num), node)
|
||||
@Test
|
||||
fun testSortByLastHeard() = runBlocking {
|
||||
val nodes = getNodes(sort = NodeSortOption.LAST_HEARD)
|
||||
val sortedNodes = nodes.sortedByDescending { it.lastHeard }
|
||||
assertEquals(sortedNodes, nodes)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testSortByAlpha() = runBlocking {
|
||||
val nodes = getNodes(sort = NodeSortOption.ALPHABETICAL)
|
||||
val sortedNodes = nodes.sortedBy { it.user?.longName?.uppercase() }
|
||||
assertEquals(sortedNodes, nodes)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testSortByDistance() = runBlocking {
|
||||
val nodes = getNodes(sort = NodeSortOption.DISTANCE)
|
||||
val sortedNodes = nodes.sortedBy { it.distance(ourNodeInfo) }
|
||||
assertEquals(sortedNodes, nodes)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testSortByChannel() = runBlocking {
|
||||
val nodes = getNodes(sort = NodeSortOption.CHANNEL)
|
||||
val sortedNodes = nodes.sortedBy { it.channel }
|
||||
assertEquals(sortedNodes, nodes)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testSortByViaMqtt() = runBlocking {
|
||||
val nodes = getNodes(sort = NodeSortOption.VIA_MQTT)
|
||||
val sortedNodes = nodes.sortedBy { it.user?.longName?.contains("(MQTT)") == true }
|
||||
assertEquals(sortedNodes, nodes)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testIncludeUnknownIsFalse() = runBlocking {
|
||||
val nodes = getNodes(includeUnknown = false)
|
||||
val containsUnsetNode = nodes.any { node ->
|
||||
node.user?.hwModel == MeshProtos.HardwareModel.UNSET
|
||||
}
|
||||
assertFalse(containsUnsetNode)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testIncludeUnknownIsTrue() = runBlocking {
|
||||
val nodes = getNodes(includeUnknown = true)
|
||||
val containsUnsetNode = nodes.any { node ->
|
||||
node.user?.hwModel == MeshProtos.HardwareModel.UNSET
|
||||
}
|
||||
assertTrue(containsUnsetNode)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user