question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Dialog boxes are untestable on Compose Desktop

See original GitHub issue

Problem

Using the Compose UI Testing library, we cannot test dialog boxes on Compose Desktop. The following example construct works on Android, but not on Desktop.

Example Code

// Test Logic
    @Test
    fun `reproduce dialog test failure`() {
        ui(compose) {
            setContent {
                var isDialogOpen by remember { mutableStateOf(false) }
                DialogHolder(isDialogOpen) {v -> isDialogOpen = v}
            }

            onNodeWithText("Open").performClick()
            awaitIdle()

            onNodeWithText("Close").assertExists()
        }
    } }
// --------------
@Composable fun DialogHolder(isDialogOpen: Boolean, updateDialog: (Boolean) -> Unit) {
    Button(onClick = { updateDialog(true) }) {
        Text("Open")
    }

    if (isDialogOpen) {
        Dialog(onCloseRequest = { updateDialog(false) }) {
            Button(onClick = { updateDialog(false) }) {
                Text("Close")
            }
        }
    }
}

Expectation

Test passes. Content within dialog box is detected.

Actual Result

Test fails with the following error:

Failed: assertExists.
Reason: Expected exactly '1' node but could not find any node that satisfies: (Text + EditableText contains 'Close' (ignoreCase: false))

java.lang.AssertionError: Failed: assertExists.
Reason: Expected exactly '1' node but could not find any node that satisfies: (Text + EditableText contains 'Close' (ignoreCase: false))

    at androidx.compose.ui.test.SemanticsNodeInteraction.fetchOneOrDie(SemanticsNodeInteraction.kt:162)
    at androidx.compose.ui.test.SemanticsNodeInteraction.assertExists(SemanticsNodeInteraction.kt:137)
...

Version Info

  • Kotlin: 1.6.10
  • Jetpack Compose: 1.1.1
  • JVM Target: 11

Links

Relevant Stackoverflow question (asked by me): https://stackoverflow.com/q/72507535/3477606.

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:2
  • Comments:6

github_iconTop GitHub Comments

1reaction
Mindstormer619commented, Aug 31, 2022

Bumping this for attention. Can I at least have an idea of when/if this is planned to be fixed?

1reaction
jamesreprisecommented, Jun 28, 2022

I think this goes beyond dialog boxes, Windows of any kind can’t be ‘seen through’.

    @Test
    fun `compose test with a window`() {
        runComposeRule(composeRule) {
            setContent {
                Window({}) {
                    Row {
                        Text("Test.")
                    }
                }
            }
...
composeRule.onRoot(useUnmergedTree = true).printToString(5)

returns

Printing with useUnmergedTree = 'true'
Node #1 at (l=0.0, t=0.0, r=0.0, b=0.0)px

For comparison, removing the Window

    @Test
    fun `compose test without a window`() {
        runComposeRule(composeRule) {
            setContent {
                  Row {
                      Text("Test.")
                  }
            }
...
composeRule.onRoot(useUnmergedTree = true).printToString(5)

returns

Printing with useUnmergedTree = 'true'
Node #1 at (l=0.0, t=0.0, r=30.0, b=17.0)px
 |-Node #2 at (l=0.0, t=0.0, r=30.0, b=17.0)px
   Text = '[Test.]'
   Actions = [GetTextLayoutResult]

Is anyone aware whether end to end testing is workable by any means right now?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Dialog boxes cannot be tested in Jetpack Compose Desktop
Dialog composables seem to be untestable in Jetpack Compose on the Desktop platform. Any action triggered from the test that adds a Dialog...
Read more >
Customize a Compose for Desktop AlertDialog
Let's take a look at the AlertDialog composable when used in Compose for Desktop. A rather basic form of an AlertDialog is created...
Read more >
Custom UI test steps - ServiceNow Docs
Install the ServiceNow CLI on Windows ... Set up Box spoke ... Write a script for an automation message variable in Automation Center....
Read more >
Miguel on Twitter: "Two significant downsides of Jetpack ...
Two significant downsides of Jetpack Compose are the super slow previews and that it's untestable on the jvm. They are probably related ...
Read more >
Compose for Desktop: Milestone 4 Released | The Kotlin Blog
compose.ui.window.Dialog. Being able to use a declarative approach to defining how your windows, dialogs, menu bars and tray icons behave makes ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found