ComposeSwingTest

The user-facing handle for driving a single isolated Swing-Compose composition under test.

An instance is created by runComposeSwingTest and is only valid for the duration of the supplied test block. The test body runs on the AWT event dispatch thread (EDT), so every query, action and assertion reads and writes the real AWT component tree directly, with no thread hop:

@Test
fun clickingTheButtonUpdatesTheLabel() = runComposeSwingTest {
var clicks by mutableStateOf(0)
setContent {
Button(text = "Clicks: $clicks", onClick = { clicks++ })
}
onNodeWithText("Clicks: 0").performClick()
onNodeWithText("Clicks: 1").assertExists()
}

The composition runs on Dispatchers.Swing (the EDT) with frames produced under test control; frames are never produced automatically.

Properties

Link copied to clipboard
abstract var lifecycleState: Lifecycle.State

The lifecycle state the content this test composes reads through androidx.lifecycle.compose.LocalLifecycleOwner, for content that inherits no owner of its own.

Link copied to clipboard
abstract val mainClock: MainTestClock

Manual control over the frames this composition is sent. See MainTestClock.

Link copied to clipboard
abstract val root: Container

The root Container hosting the composition. Useful for advanced assertions, e.g. inspecting a child's layout constraint via the parent's java.awt.LayoutManager.

Functions

Link copied to clipboard
fun ComposeSwingTest.assertImageAgainstGolden(goldenIdentifier: String, threshold: Double = MSSIMMatcher.DEFAULT_THRESHOLD)

Captures the whole composition root and asserts it matches the stored golden image identified by goldenIdentifier.

fun ComposeSwingTest.assertImageAgainstGolden(image: BufferedImage, goldenIdentifier: String, threshold: Double = MSSIMMatcher.DEFAULT_THRESHOLD)

Asserts an already-captured image against the golden image identified by goldenIdentifier. Use this when the image was produced by captureToImage and possibly post-processed before comparing.

Link copied to clipboard
fun ComposeSwingTest.assertImageMatches(expected: BufferedImage, image: BufferedImage, threshold: Double = MSSIMMatcher.DEFAULT_THRESHOLD)

Asserts an already-captured image matches expected by structural similarity at threshold, without involving any golden file. Useful for comparing two captured or synthesized images directly inside a test.

Link copied to clipboard
fun ComposeSwingTest.assertImagesPixelPerfect(expected: BufferedImage, image: BufferedImage, maxDifferentPixels: Int = 0)

Asserts image matches expected pixel-for-pixel, allowing at most maxDifferentPixels differing pixels (default 0, i.e. byte-exact). Use this to prove two independently rendered components rasterize identically, where the tolerance of assertImageMatches would miss a stray border, margin or alignment shift.

Link copied to clipboard
abstract suspend fun awaitEventsDelivered()

Suspends until every AWT notification already queued on the event dispatch thread has been dispatched, without producing a composition frame.

Link copied to clipboard
abstract suspend fun awaitIdle()

Suspends until the composition is idle, making the AWT tree reflect the latest state.

Link copied to clipboard

Renders the whole composition root, together with everything drawn inside it, to an off-screen image. Equivalent to capturing the node returned by ComposeSwingTest.onRoot.

Link copied to clipboard

Finds all nodes matching matcher.

Link copied to clipboard

Finds all nodes of type T. Convenience for onAllNodes(SwingMatcher.isOfType<T>()).

Link copied to clipboard

Finds all nodes tagged with tag via SwingModifier.testTag.

Link copied to clipboard
abstract fun onAllNodesWithText(text: @Nls String, substring: Boolean = false): SwingNodeInteractionCollection<Component>

Finds all nodes whose text equals text (or contains it when substring is true).

Link copied to clipboard

Finds all currently realized windows matching matcher (see onWindow for the match set).

Link copied to clipboard
Link copied to clipboard

Finds the single node matching matcher.

Link copied to clipboard

Finds the single node of type T. Convenience for onNode(SwingMatcher.isOfType<T>()).

Link copied to clipboard

Finds the single node whose Component.getName equals name.

Link copied to clipboard

Finds the single node tagged with tag via SwingModifier.testTag.

Link copied to clipboard
abstract fun onNodeWithText(text: @Nls String, substring: Boolean = false): SwingNodeInteraction<Component>

Finds the single node whose text equals text (or contains it when substring is true). The match is resolved lazily when the returned interaction is first used.

Link copied to clipboard

Returns an interaction targeting the composition root itself.

Link copied to clipboard

Finds the single window matching matcher among every window currently realized in the test JVM, whether or not it is shown. A window realized by a org.jetbrains.compose.swing.window.Window or org.jetbrains.compose.swing.window.Dialog composable stays realized while that composable is in the composition and leaves the match set once it is disposed on leaving the composition. The match is resolved lazily when the returned interaction is first used.

Link copied to clipboard

Finds the single realized window (see ComposeSwingTest.onWindow). Convenience for the common one-window composition:

Link copied to clipboard

Finds the single realized window titled title. Convenience for onWindow(SwingMatcher.hasTitle(title)).

Link copied to clipboard
abstract fun setContent(content: @Composable () -> Unit)

Sets the composable content of the test root and settles the composition so the AWT tree reflects the initial state before returning. May be called only once per test.

Link copied to clipboard

Removes and returns the failures raised by callbacks this test supplied and contained by the composition, oldest first.

Link copied to clipboard
abstract suspend fun waitUntil(timeout: Duration = 1.seconds, condition: () -> Boolean)

Suspends until condition returns true, driving frames between checks.