Skip to content

Commit e9b381b

Browse files
authored
Composable Preferences rewrite (#1277)
* Remove ContributionManager and ContributionPane UI files Deleted ContributionManager.kt and ContributionPane.kt from the contrib/ui directory. This removes the Compose-based contributions manager and its detail pane prototypes which got merged unnecessarily * Enhance Preferences reactivity and test coverage Refactored ReactiveProperties to use snapshotStateMap for Compose reactivity. Improved PreferencesProvider and watchFile composables with better file watching, override support via system properties, and added documentation. Updated PreferencesKtTest to use temporary files and verify file-to-UI reactivity. * Small bugfix for removed function * Add compose ui test to the deps
1 parent 7965931 commit e9b381b

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package processing.app
2+
3+
import androidx.compose.material.Button
4+
import androidx.compose.material.Text
5+
import androidx.compose.ui.Modifier
6+
import androidx.compose.ui.platform.testTag
7+
import androidx.compose.ui.test.*
8+
import java.util.Properties
9+
import kotlin.io.path.createFile
10+
import kotlin.io.path.createTempDirectory
11+
import kotlin.test.Test
12+
13+
class PreferencesKtTest{
14+
@OptIn(ExperimentalTestApi::class)
15+
@Test
16+
fun testKeyReactivity() = runComposeUiTest {
17+
val directory = createTempDirectory("preferences")
18+
val tempPreferences = directory
19+
.resolve("preferences.txt")
20+
.createFile()
21+
.toFile()
22+
23+
// Set system properties for testing
24+
System.setProperty("processing.app.preferences.file", tempPreferences.absolutePath)
25+
System.setProperty("processing.app.preferences.debounce", "0")
26+
System.setProperty("processing.app.watchfile.forced", "true")
27+
28+
val newValue = (0..Int.MAX_VALUE).random().toString()
29+
val testKey = "test.preferences.reactivity"
30+
31+
setContent {
32+
PreferencesProvider {
33+
val preferences = LocalPreferences.current
34+
Text(preferences[testKey] ?: "default", modifier = Modifier.testTag("text"))
35+
36+
Button(onClick = {
37+
preferences[testKey] = newValue
38+
}, modifier = Modifier.testTag("button")) {
39+
Text("Change")
40+
}
41+
}
42+
}
43+
44+
onNodeWithTag("text").assertTextEquals("default")
45+
onNodeWithTag("button").performClick()
46+
onNodeWithTag("text").assertTextEquals(newValue)
47+
48+
val preferences = Properties()
49+
preferences.load(tempPreferences.inputStream().reader(Charsets.UTF_8))
50+
51+
// Check if the preference was saved to file
52+
assert(preferences[testKey] == newValue)
53+
54+
55+
val nextValue = (0..Int.MAX_VALUE).random().toString()
56+
// Overwrite the file to see if the UI updates
57+
tempPreferences.writeText("$testKey=${nextValue}")
58+
59+
onNodeWithTag("text").assertTextEquals(nextValue)
60+
}
61+
}

0 commit comments

Comments
 (0)