Open the setup wizard as a modal dialog

Written-by: DeepSeek V4 Pro
Written-by: Claude Fable 5.2
This commit is contained in:
Rsclub2_2 committed 2026-11-18 21:32:10 +01:00
1 parent 64a6eab35f
commit 3575473fa9
3 files changed
+352

No files matched your search

@@ -0,0 +1,118 @@
package kst4contest.view.compose.wizard
import kst4contest.model.ChatPreferences
import org.junit.jupiter.api.Assertions.assertArrayEquals
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Assertions.assertNull
import org.junit.jupiter.api.Assertions.assertTrue
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.io.TempDir
import java.nio.file.Files
import java.nio.file.Path
import java.nio.file.StandardCopyOption
/** When the wizard opens at all, decided without a window. */
class SetupWizardLauncherTest(@TempDir private val directory: Path) {
private fun templateFile(): Path {
val file = directory.resolve("preferences.xml")
ChatPreferences::class.java.getResourceAsStream(ChatPreferences.PREFERENCE_RESOURCE).use { template ->
Files.copy(requireNotNull(template), file, StandardCopyOption.REPLACE_EXISTING)
}
return file
}
private fun configuredFile(): Path {
val file = templateFile()
ChatPreferences(file).apply {
assertTrue(readPreferencesFromXmlFile())
setStn_loginCallSign("DL0TEST")
setStn_loginPassword("secret")
assertTrue(writePreferencesToXmlFile())
}
return file
}
private val neverShown: (SetupWizardState, ChatPreferences) -> SetupWizardOutcome =
{ _, _ -> throw AssertionError("the wizard must not open here") }
@Test
fun anUnconfiguredProfileOpensTheWizard() {
var shown = 0
val outcome = SetupWizardLauncher.run(ChatPreferences(templateFile()), forced = false) { _, _ ->
shown++
SetupWizardOutcome.LATER
}
assertEquals(1, shown)
assertEquals(SetupWizardOutcome.LATER, outcome)
}
@Test
fun aConfiguredProfileStartsWithoutTheWizard() {
assertEquals(
SetupWizardOutcome.NOT_SHOWN,
SetupWizardLauncher.run(ChatPreferences(configuredFile()), forced = false, neverShown),
)
}
@Test
fun theMenuOpensItOnAConfiguredProfileToo() {
val outcome = SetupWizardLauncher.run(ChatPreferences(configuredFile()), forced = true) { state, _ ->
assertEquals(listOf(WizardSource.NEW, WizardSource.KEEP), state.loginSources)
SetupWizardOutcome.SAVED
}
assertEquals(SetupWizardOutcome.SAVED, outcome)
}
@Test
fun anUnreadableFileNeverOpensTheWizard() {
val garbage = directory.resolve("broken.xml")
Files.writeString(garbage, "this is not xml <<<")
val before = Files.readAllBytes(garbage)
// Forced as well: a wizard over a file it could not read would save over it.
assertEquals(
SetupWizardOutcome.NOT_SHOWN,
SetupWizardLauncher.run(ChatPreferences(garbage), forced = true, neverShown),
)
assertArrayEquals(before, Files.readAllBytes(garbage), "the unreadable file was touched")
}
@Test
fun aFailedSaveKeepsTheWizardOpen() {
val source = ChatPreferences(templateFile()).apply { assertTrue(readPreferencesFromXmlFile()) }
val state = SetupWizardState(source).apply {
name = "Philipp"
next()
callsign = "DL0TEST"
password = "secret"
next()
locator = "JO50AA"
setBand(WizardBand.B144, true)
}
val unwritable = ChatPreferences(Files.createDirectory(directory.resolve("not-a-file")))
assertNull(saveFromWizard(state, unwritable, connect = true), "a failed write must not close the wizard")
}
@Test
fun aSaveReportsWhetherToConnect() {
val file = templateFile()
val prefs = ChatPreferences(file).apply { assertTrue(readPreferencesFromXmlFile()) }
val state = SetupWizardState(prefs).apply {
name = "Philipp"
next()
callsign = "DL0TEST"
password = "secret"
next()
locator = "JO50AA"
setBand(WizardBand.B144, true)
}
assertEquals(SetupWizardOutcome.SAVED, saveFromWizard(state, prefs, connect = false))
assertEquals(SetupWizardOutcome.SAVED_AND_CONNECT, saveFromWizard(state, prefs, connect = true))
}
}
@@ -0,0 +1,78 @@
package kst4contest.view.compose.wizard
import kst4contest.model.ChatPreferences
import kst4contest.testing.Await.awaitUntil
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Assumptions.assumeFalse
import org.junit.jupiter.api.Test
import java.awt.EventQueue
import java.awt.GraphicsEnvironment
import java.awt.Window
import java.nio.file.Path
import java.time.Duration
import java.util.concurrent.Executors
import java.util.concurrent.Future
import java.util.concurrent.TimeUnit
/**
* The wizard's dialog must open from both threads that open it: `main` at startup, and the
* event thread when the menu rebuilds the runtime. A latch-based wait deadlocked the profile
* picker in exactly the second case; see OperatorProfilePickerWindow.
*/
class SetupWizardWindowTest {
private fun prefs() = ChatPreferences(Path.of("setup-wizard-window-test-unused.xml"))
private fun showingDialog(): Window? =
Window.getWindows().firstOrNull { it.isShowing && it.javaClass.name.contains("ComposeDialog") }
private val closer = Executors.newSingleThreadExecutor()
/** Closes the dialog the way its X does, as soon as it is on screen. */
private fun disposeOnceShowing(): Future<*> =
closer.submit {
awaitUntil(Duration.ofSeconds(10), "the wizard's dialog never appeared") {
showingDialog() != null
}
EventQueue.invokeLater { showingDialog()?.dispose() }
}
@Test
fun closingTheWindowCountsAsLater() {
assumeFalse(GraphicsEnvironment.isHeadless(), "needs a display")
try {
val closing = disposeOnceShowing()
val prefs = prefs()
val outcome = SetupWizardWindow.show(SetupWizardState(prefs), prefs)
assertEquals(SetupWizardOutcome.LATER, outcome)
assertEquals("", prefs.getStn_loginCallSign(), "closing the window wrote something")
closing.get(1, TimeUnit.SECONDS)
} finally {
closer.shutdownNow()
}
}
@Test
fun opensAndReturnsWhenCalledOnTheEventThread() {
assumeFalse(GraphicsEnvironment.isHeadless(), "needs a display")
val worker = Executors.newSingleThreadExecutor()
try {
val closing = disposeOnceShowing()
val prefs = prefs()
val call = worker.submit {
EventQueue.invokeAndWait { SetupWizardWindow.show(SetupWizardState(prefs), prefs) }
}
// A deadlock here has no timeout of its own: without one this never completes.
call.get(20, TimeUnit.SECONDS)
closing.get(1, TimeUnit.SECONDS)
} finally {
worker.shutdownNow()
closer.shutdownNow()
}
}
}