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
The lifecycle state the content this test composes reads through androidx.lifecycle.compose.LocalLifecycleOwner, for content that inherits no owner of its own.
Manual control over the frames this composition is sent. See MainTestClock.
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
Captures the whole composition root and asserts it matches the stored golden image identified by goldenIdentifier.
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.
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.
Suspends until every AWT notification already queued on the event dispatch thread has been dispatched, without producing a composition frame.
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.
Finds all nodes matching matcher.
Finds all nodes of type T. Convenience for onAllNodes(SwingMatcher.isOfType<T>()).
Finds all nodes tagged with tag via SwingModifier.testTag.
Finds all realized windows (see ComposeSwingTest.onWindow).
Finds the single node matching matcher.
Finds the single node of type T. Convenience for onNode(SwingMatcher.isOfType<T>()).
Finds the single node whose Component.getName equals name.
Finds the single node tagged with tag via SwingModifier.testTag.
Returns an interaction targeting the composition root itself.
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.
Finds the single realized window (see ComposeSwingTest.onWindow). Convenience for the common one-window composition:
Finds the single realized window titled title. Convenience for onWindow(SwingMatcher.hasTitle(title)).
Removes and returns the failures raised by callbacks this test supplied and contained by the composition, oldest first.