Modularize settings code (#3355)

Co-authored-by: Ben Meadors <benmmeadors@gmail.com>
This commit is contained in:
Phil Oliver 2025-10-06 13:20:03 -04:00 committed by GitHub
parent 4613a26c9d
commit 95ec4877df
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
75 changed files with 444 additions and 358 deletions

View file

@ -1,133 +0,0 @@
/*
* Copyright (c) 2025 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 com.geeksville.mesh.compose
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.padding
import androidx.compose.runtime.getValue
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.test.assertIsDisplayed
import androidx.compose.ui.test.junit4.createComposeRule
import androidx.compose.ui.test.onNodeWithContentDescription
import androidx.compose.ui.test.onNodeWithText
import androidx.compose.ui.test.performClick
import androidx.compose.ui.test.performTextInput
import androidx.compose.ui.unit.dp
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.platform.app.InstrumentationRegistry
import com.geeksville.mesh.ui.debug.FilterMode
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import org.meshtastic.core.strings.R
@RunWith(AndroidJUnit4::class)
class DebugFiltersTest {
@get:Rule val composeTestRule = createComposeRule()
@Test
fun debugFilterBar_showsFilterButtonAndMenu() {
val context = InstrumentationRegistry.getInstrumentation().targetContext
val filterLabel = context.getString(R.string.debug_filters)
composeTestRule.setContent {
var filterTexts by
androidx.compose.runtime.remember { androidx.compose.runtime.mutableStateOf(listOf<String>()) }
var customFilterText by androidx.compose.runtime.remember { androidx.compose.runtime.mutableStateOf("") }
val presetFilters = listOf("Error", "Warning", "Info")
val logs =
listOf(
com.geeksville.mesh.model.DebugViewModel.UiMeshLog(
uuid = "1",
messageType = "Info",
formattedReceivedDate = "2024-01-01 12:00:00",
logMessage = "Sample log message",
),
)
com.geeksville.mesh.ui.debug.DebugFilterBar(
filterTexts = filterTexts,
onFilterTextsChange = { filterTexts = it },
customFilterText = customFilterText,
onCustomFilterTextChange = { customFilterText = it },
presetFilters = presetFilters,
logs = logs,
)
}
// The filter button should be visible
composeTestRule.onNodeWithText(filterLabel).assertIsDisplayed()
}
@Test
fun debugFilterBar_addCustomFilter_displaysActiveFilter() {
val context = InstrumentationRegistry.getInstrumentation().targetContext
val activeFiltersLabel = context.getString(R.string.debug_active_filters)
composeTestRule.setContent {
var filterTexts by
androidx.compose.runtime.remember { androidx.compose.runtime.mutableStateOf(listOf<String>()) }
var customFilterText by androidx.compose.runtime.remember { androidx.compose.runtime.mutableStateOf("") }
Column(modifier = Modifier.padding(16.dp)) {
com.geeksville.mesh.ui.debug.DebugActiveFilters(
filterTexts = filterTexts,
onFilterTextsChange = { filterTexts = it },
filterMode = FilterMode.OR,
onFilterModeChange = {},
)
com.geeksville.mesh.ui.debug.DebugCustomFilterInput(
customFilterText = customFilterText,
onCustomFilterTextChange = { customFilterText = it },
filterTexts = filterTexts,
onFilterTextsChange = { filterTexts = it },
)
}
}
with(composeTestRule) {
// Add a custom filter
onNodeWithText("Add custom filter").performTextInput("MyFilter")
onNodeWithContentDescription("Add filter").performClick()
// The active filters label and the filter chip should be visible
onNodeWithText(activeFiltersLabel).assertIsDisplayed()
onNodeWithText("MyFilter").assertIsDisplayed()
}
}
@Test
fun debugActiveFilters_clearAllFilters_removesFilters() {
val context = InstrumentationRegistry.getInstrumentation().targetContext
val activeFiltersLabel = context.getString(R.string.debug_active_filters)
composeTestRule.setContent {
var filterTexts by
androidx.compose.runtime.remember { androidx.compose.runtime.mutableStateOf(listOf("A", "B")) }
com.geeksville.mesh.ui.debug.DebugActiveFilters(
filterTexts = filterTexts,
onFilterTextsChange = { filterTexts = it },
filterMode = FilterMode.OR,
onFilterModeChange = {},
)
}
// The active filters label and chips should be visible
composeTestRule.onNodeWithText(activeFiltersLabel).assertIsDisplayed()
composeTestRule.onNodeWithText("A").assertIsDisplayed()
composeTestRule.onNodeWithText("B").assertIsDisplayed()
// Click the clear all filters button
composeTestRule.onNodeWithContentDescription("Clear all filters").performClick()
// The filter chips should no longer be visible
composeTestRule.onNodeWithText("A").assertDoesNotExist()
composeTestRule.onNodeWithText("B").assertDoesNotExist()
}
}

View file

@ -1,198 +0,0 @@
/*
* Copyright (c) 2025 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 com.geeksville.mesh.compose
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.padding
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.test.assertIsDisplayed
import androidx.compose.ui.test.junit4.createComposeRule
import androidx.compose.ui.test.onNodeWithContentDescription
import androidx.compose.ui.test.onNodeWithText
import androidx.compose.ui.test.performClick
import androidx.compose.ui.test.performTextInput
import androidx.compose.ui.unit.dp
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.platform.app.InstrumentationRegistry
import com.geeksville.mesh.model.LogSearchManager.SearchState
import com.geeksville.mesh.ui.debug.DebugSearchBar
import com.geeksville.mesh.ui.debug.FilterMode
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import org.meshtastic.core.strings.R
@RunWith(AndroidJUnit4::class)
class DebugSearchTest {
@get:Rule val composeTestRule = createComposeRule()
@Test
fun debugSearchBar_showsPlaceholder() {
val context = InstrumentationRegistry.getInstrumentation().targetContext
val placeholder = context.getString(R.string.debug_default_search)
composeTestRule.setContent {
DebugSearchBar(
searchState = SearchState(),
onSearchTextChange = {},
onNextMatch = {},
onPreviousMatch = {},
onClearSearch = {},
)
}
composeTestRule.onNodeWithText(placeholder).assertIsDisplayed()
}
@Test
fun debugSearchBar_showsClearButtonWhenTextEntered() {
val context = InstrumentationRegistry.getInstrumentation().targetContext
val placeholder = context.getString(R.string.debug_default_search)
composeTestRule.setContent {
var searchText by androidx.compose.runtime.remember { mutableStateOf("test") }
DebugSearchBar(
searchState = SearchState(searchText = searchText),
onSearchTextChange = { searchText = it },
onNextMatch = {},
onPreviousMatch = {},
onClearSearch = { searchText = "" },
)
}
composeTestRule.onNodeWithContentDescription("Clear search").assertIsDisplayed().performClick()
composeTestRule.onNodeWithText(placeholder).assertIsDisplayed()
}
@Test
fun debugSearchBar_searchFor_showsArrowsClearAndValues() {
val searchText = "test"
val matchCount = 3
val currentMatchIndex = 1
composeTestRule.setContent {
DebugSearchBar(
searchState =
SearchState(
searchText = searchText,
currentMatchIndex = currentMatchIndex,
allMatches =
List(matchCount) {
com.geeksville.mesh.model.LogSearchManager.SearchMatch(it, 0, 6, "Packet")
},
hasMatches = true,
),
onSearchTextChange = {},
onNextMatch = {},
onPreviousMatch = {},
onClearSearch = {},
)
}
// Check the match count display (e.g., '2/3')
composeTestRule.onNodeWithText("${currentMatchIndex + 1}/$matchCount").assertIsDisplayed()
// Check the navigation arrows
composeTestRule.onNodeWithContentDescription("Previous match").assertIsDisplayed()
composeTestRule.onNodeWithContentDescription("Next match").assertIsDisplayed()
// Check the clear button
composeTestRule.onNodeWithContentDescription("Clear search").assertIsDisplayed()
}
@Test
fun debugFilterBar_showsFilterButtonAndMenu() {
val context = InstrumentationRegistry.getInstrumentation().targetContext
val filterLabel = context.getString(R.string.debug_filters)
composeTestRule.setContent {
var filterTexts by androidx.compose.runtime.remember { mutableStateOf(listOf<String>()) }
var customFilterText by androidx.compose.runtime.remember { mutableStateOf("") }
val presetFilters = listOf("Error", "Warning", "Info")
val logs =
listOf(
com.geeksville.mesh.model.DebugViewModel.UiMeshLog(
uuid = "1",
messageType = "Info",
formattedReceivedDate = "2024-01-01 12:00:00",
logMessage = "Sample log message",
),
)
com.geeksville.mesh.ui.debug.DebugFilterBar(
filterTexts = filterTexts,
onFilterTextsChange = { filterTexts = it },
customFilterText = customFilterText,
onCustomFilterTextChange = { customFilterText = it },
presetFilters = presetFilters,
logs = logs,
)
}
// The filter button should be visible
composeTestRule.onNodeWithText(filterLabel).assertIsDisplayed()
}
@Test
fun debugFilterBar_addCustomFilter_displaysActiveFilter() {
val context = InstrumentationRegistry.getInstrumentation().targetContext
val activeFiltersLabel = context.getString(R.string.debug_active_filters)
composeTestRule.setContent {
var filterTexts by androidx.compose.runtime.remember { mutableStateOf(listOf<String>()) }
var customFilterText by androidx.compose.runtime.remember { mutableStateOf("") }
Column(modifier = Modifier.padding(16.dp)) {
com.geeksville.mesh.ui.debug.DebugActiveFilters(
filterTexts = filterTexts,
onFilterTextsChange = { filterTexts = it },
filterMode = FilterMode.OR,
onFilterModeChange = {},
)
com.geeksville.mesh.ui.debug.DebugCustomFilterInput(
customFilterText = customFilterText,
onCustomFilterTextChange = { customFilterText = it },
filterTexts = filterTexts,
onFilterTextsChange = { filterTexts = it },
)
}
}
with(composeTestRule) {
onNodeWithText("Add custom filter").performTextInput("MyFilter")
onNodeWithContentDescription("Add filter").performClick()
onNodeWithText(activeFiltersLabel).assertIsDisplayed()
onNodeWithText("MyFilter").assertIsDisplayed()
}
}
@Test
fun debugActiveFilters_clearAllFilters_removesFilters() {
val context = InstrumentationRegistry.getInstrumentation().targetContext
val activeFiltersLabel = context.getString(R.string.debug_active_filters)
composeTestRule.setContent {
var filterTexts by androidx.compose.runtime.remember { mutableStateOf(listOf("A", "B")) }
com.geeksville.mesh.ui.debug.DebugActiveFilters(
filterTexts = filterTexts,
onFilterTextsChange = { filterTexts = it },
filterMode = FilterMode.OR,
onFilterModeChange = {},
)
}
// The active filters label and chips should be visible
composeTestRule.onNodeWithText(activeFiltersLabel).assertIsDisplayed()
composeTestRule.onNodeWithText("A").assertIsDisplayed()
composeTestRule.onNodeWithText("B").assertIsDisplayed()
// Click the clear all filters button
composeTestRule.onNodeWithContentDescription("Clear all filters").performClick()
// The filter chips should no longer be visible
composeTestRule.onNodeWithText("A").assertDoesNotExist()
composeTestRule.onNodeWithText("B").assertDoesNotExist()
}
}

View file

@ -1,112 +0,0 @@
/*
* Copyright (c) 2025 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 com.geeksville.mesh.compose
import androidx.compose.ui.test.assertIsDisplayed
import androidx.compose.ui.test.junit4.createComposeRule
import androidx.compose.ui.test.onNodeWithText
import androidx.compose.ui.test.performClick
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.platform.app.InstrumentationRegistry
import com.geeksville.mesh.ClientOnlyProtos.DeviceProfile
import com.geeksville.mesh.deviceProfile
import com.geeksville.mesh.position
import com.geeksville.mesh.ui.settings.radio.components.EditDeviceProfileDialog
import org.junit.Assert
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import org.meshtastic.core.strings.R
@RunWith(AndroidJUnit4::class)
class EditDeviceProfileDialogTest {
@get:Rule val composeTestRule = createComposeRule()
private fun getString(id: Int): String = InstrumentationRegistry.getInstrumentation().targetContext.getString(id)
private val title = "Export configuration"
private val deviceProfile = deviceProfile {
longName = "Long name"
shortName = "Short name"
channelUrl = "https://meshtastic.org/e/#CgMSAQESBggBQANIAQ"
fixedPosition = position {
latitudeI = 327766650
longitudeI = -967969890
altitude = 138
}
}
private fun testEditDeviceProfileDialog(onDismiss: () -> Unit = {}, onConfirm: (DeviceProfile) -> Unit = {}) =
composeTestRule.setContent {
EditDeviceProfileDialog(
title = title,
deviceProfile = deviceProfile,
onConfirm = onConfirm,
onDismiss = onDismiss,
)
}
@Test
fun testEditDeviceProfileDialog_showsDialogTitle() {
composeTestRule.apply {
testEditDeviceProfileDialog()
// Verify that the dialog title is displayed
onNodeWithText(title).assertIsDisplayed()
}
}
@Test
fun testEditDeviceProfileDialog_showsCancelAndSaveButtons() {
composeTestRule.apply {
testEditDeviceProfileDialog()
// Verify the "Cancel" and "Save" buttons are displayed
onNodeWithText(getString(R.string.cancel)).assertIsDisplayed()
onNodeWithText(getString(R.string.save)).assertIsDisplayed()
}
}
@Test
fun testEditDeviceProfileDialog_clickCancelButton() {
var onDismissClicked = false
composeTestRule.apply {
testEditDeviceProfileDialog(onDismiss = { onDismissClicked = true })
// Click the "Cancel" button
onNodeWithText(getString(R.string.cancel)).performClick()
}
// Verify onDismiss is called
Assert.assertTrue(onDismissClicked)
}
@Test
fun testEditDeviceProfileDialog_addChannels() {
var actualDeviceProfile: DeviceProfile? = null
composeTestRule.apply {
testEditDeviceProfileDialog(onConfirm = { actualDeviceProfile = it })
onNodeWithText(getString(R.string.save)).performClick()
}
// Verify onConfirm is called with the correct DeviceProfile
Assert.assertEquals(deviceProfile, actualDeviceProfile)
}
}

View file

@ -1,98 +0,0 @@
/*
* Copyright (c) 2025 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 com.geeksville.mesh.compose
import androidx.compose.foundation.layout.Column
import androidx.compose.ui.platform.LocalFocusManager
import androidx.compose.ui.test.assertIsDisplayed
import androidx.compose.ui.test.assertIsNotDisplayed
import androidx.compose.ui.test.junit4.createComposeRule
import androidx.compose.ui.test.onNodeWithText
import androidx.compose.ui.test.performClick
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.platform.app.InstrumentationRegistry
import com.geeksville.mesh.ui.settings.radio.components.MapReportingPreference
import org.junit.Assert
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import org.meshtastic.core.strings.R
@RunWith(AndroidJUnit4::class)
class MapReportingPreferenceTest {
@get:Rule val composeTestRule = createComposeRule()
private fun getString(id: Int): String = InstrumentationRegistry.getInstrumentation().targetContext.getString(id)
var mapReportingEnabled = false
var shouldReportLocation = false
var positionPrecision = 5
var positionReportingInterval = 60
var mapReportingEnabledChanged = { enabled: Boolean -> mapReportingEnabled = enabled }
var shouldReportLocationChanged = { enabled: Boolean -> shouldReportLocation = enabled }
var positionPrecisionChanged = { precision: Int -> positionPrecision = precision }
var positionReportingIntervalChanged = { interval: Int -> positionReportingInterval = interval }
private fun testMapReportingPreference() = composeTestRule.setContent {
Column {
MapReportingPreference(
mapReportingEnabled = mapReportingEnabled,
shouldReportLocation = shouldReportLocation,
positionPrecision = positionPrecision,
onMapReportingEnabledChanged = mapReportingEnabledChanged,
onShouldReportLocationChanged = shouldReportLocationChanged,
onPositionPrecisionChanged = positionPrecisionChanged,
publishIntervalSecs = positionReportingInterval,
onPublishIntervalSecsChanged = positionReportingIntervalChanged,
enabled = true,
focusManager = LocalFocusManager.current,
)
}
}
@Test
fun testMapReportingPreference_showsText() {
composeTestRule.apply {
testMapReportingPreference()
// Verify that the dialog title is displayed
onNodeWithText(getString(R.string.map_reporting)).assertIsDisplayed()
onNodeWithText(getString(R.string.map_reporting_summary)).assertIsDisplayed()
}
}
@Test
fun testMapReportingPreference_toggleMapReporting() {
composeTestRule.apply {
testMapReportingPreference()
onNodeWithText(getString(R.string.i_agree)).assertIsNotDisplayed()
onNodeWithText(getString(R.string.map_reporting)).performClick()
Assert.assertFalse(mapReportingEnabled)
Assert.assertFalse(shouldReportLocation)
onNodeWithText(getString(R.string.i_agree)).assertIsDisplayed()
onNodeWithText(getString(R.string.i_agree)).performClick()
Assert.assertTrue(shouldReportLocation)
Assert.assertTrue(mapReportingEnabled)
onNodeWithText(getString(R.string.map_reporting)).performClick()
onNodeWithText(getString(R.string.i_agree)).assertIsNotDisplayed()
Assert.assertTrue(shouldReportLocation)
Assert.assertFalse(mapReportingEnabled)
}
}
}