Skip to content

Commit c8c849e

Browse files
CopilotbedaHovorka
andauthored
test: adapt SimulationController tests to callback-based UI wiring
Agent-Logs-Url: https://github.com/bedaHovorka/interlockSim/sessions/01419796-a691-46e1-ae10-8c1373a9ee18 Co-authored-by: bedaHovorka <5263405+bedaHovorka@users.noreply.github.com>
1 parent eb9f941 commit c8c849e

3 files changed

Lines changed: 23 additions & 6 deletions

File tree

desktop-ui/src/main/kotlin/cz/vutbr/fit/interlockSim/gui/Frame.kt

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import javax.swing.JFrame
2626
import javax.swing.JOptionPane
2727
import javax.swing.JPanel
2828
import javax.swing.JScrollPane
29+
import javax.swing.SwingUtilities
2930
import javax.swing.Timer
3031

3132
/**
@@ -419,11 +420,17 @@ class Frame : JFrame(PROGRAM_FULL_NAME) {
419420
}
420421
}
421422

423+
/**
424+
* Execute [action] on EDT.
425+
*
426+
* Runs immediately if already on EDT; otherwise schedules asynchronously via
427+
* [javax.swing.SwingUtilities.invokeLater] to avoid blocking monitor/background threads.
428+
*/
422429
private fun runOnEdt(action: () -> Unit) {
423-
if (javax.swing.SwingUtilities.isEventDispatchThread()) {
430+
if (SwingUtilities.isEventDispatchThread()) {
424431
action()
425432
} else {
426-
javax.swing.SwingUtilities.invokeLater(action)
433+
SwingUtilities.invokeLater(action)
427434
}
428435
}
429436

desktop-ui/src/main/kotlin/cz/vutbr/fit/interlockSim/gui/SimulationController.kt

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,13 @@ import java.beans.PropertyChangeListener
3535
* nulls it.
3636
* - Callbacks are invoked on whichever thread performs the lifecycle change.
3737
*
38-
* @param onStateChanged Callback for lifecycle state updates.
39-
* @param onSpeedChanged Callback for speed indicator updates.
40-
* @param onCompleted Callback invoked when the simulation finishes naturally.
41-
* Defaults to a no-op.
38+
* @param onStateChanged Callback for lifecycle state updates. Invoked on the same
39+
* thread that performs the state change (caller thread for [start]/[stop], monitor
40+
* thread for natural completion).
41+
* @param onSpeedChanged Callback for speed indicator updates. Invoked on the thread
42+
* that emits the speed change; callers are responsible for EDT marshalling as needed.
43+
* @param onCompleted Callback invoked when the simulation finishes naturally on the
44+
* monitor thread. Defaults to a no-op.
4245
* @since 2026-04-20 (extracted from Frame for testability)
4346
* @see Frame
4447
*/

desktop-ui/src/test/kotlin/cz/vutbr/fit/interlockSim/gui/SimulationControllerTest.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -680,6 +680,13 @@ class SimulationControllerTest {
680680
onCompleted = onCompleted
681681
)
682682

683+
/**
684+
* Execute [action] on EDT synchronously for deterministic assertions in unit tests.
685+
*
686+
* Unlike the production callback wiring pattern (which dispatches asynchronously),
687+
* this helper blocks until EDT work completes. That makes follow-up assertions
688+
* observe final state without timing races.
689+
*/
683690
private fun runOnEdtSync(action: () -> Unit) {
684691
if (SwingUtilities.isEventDispatchThread()) {
685692
action()

0 commit comments

Comments
 (0)