runComposeSwingTest

fun runComposeSwingTest(rootSize: Dimension = Dimension(800, 600), timeout: Duration = 60.seconds, block: suspend ComposeSwingTest.() -> Unit): TestResult

Sets up an isolated Swing-Compose composition, runs the suspending block against it on the EDT, and tears everything down.

The whole block executes as a coroutine on Dispatchers.Swing (the EDT); the calling (JUnit) thread is blocked until it completes or timeout elapses, whichever comes first. Frames are produced under test control rather than automatically, so the composition advances only across idle/await calls. Because the body runs on the EDT, queries and actions read and write the AWT tree directly, and ComposeSwingTest.awaitIdle suspends rather than blocking.

Runs with or without a display: the root is never attached to a java.awt.Window, so no native peer is realized and no UI is shown. The root is given a fixed size and laid out synchronously on every idle pass, so tree/text/constraint AND bounds-based assertions (see SwingNodeInteraction.assertIsDisplayed) all work off-screen.

Content may also compose real top-level peers (Window { }, Dialog { }); those are found with ComposeSwingTest.onWindow and are torn down with the composition when the block completes. Realizing them requires a display - declare that requirement with a JUnit assumption (org.junit.jupiter.api.Assumptions.assumeFalse(GraphicsEnvironment.isHeadless(), ...)) at the top of the block, so the test reports SKIPPED rather than failing on headless environments.

Parameters

rootSize

the off-screen ComposeSwingTest.root's fixed size. The default is large enough that realistic test layouts get sensible, non-zero child bounds under a forced layout pass.

timeout

the wall-clock deadline after which an unfinished block fails the test instead of hanging it. The frame caps inside ComposeSwingTest.awaitIdle and ComposeSwingTest.waitUntil only bound the work those gates drive themselves; this bounds the test as a whole.

block

the test body, run on the Event Dispatch Thread against a fresh ComposeSwingTest.