2

Problem

Dialog composables seem to be untestable in Jetpack Compose on the Desktop platform. Any action triggered from the test that adds a Dialog to the composition seems to immediately close the box again (when running the test, the dialog box itself actually opens for a brief second, which I don't expect to be happening), and the test fails to detect any element which is inside the dialog.

This does not happen on the actual production run -- we only see this problem in the tests. I have not tested this on other platforms, but (at least from the documentation) it seems to me that it works on Android.

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")
            }
        }
    }
}

Test Result

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)
...

Screenshots (directly run from main())

Before opening dialog

With dialog open

Version Info

  • Kotlin: 1.6.10
  • Jetpack Compose: 1.1.1
  • JVM Target: 11
Mindstormer619
  • 2,706
  • 1
  • 16
  • 16
  • 1
    I run this test on Android and it passes, looks like you're doing it correctly, at this point I suggest you [reporting](https://github.com/JetBrains/compose-jb/issues/new) this issue – Phil Dukhov Jun 06 '22 at 04:03
  • @PylypDukhov Thanks for confirming it on Android! I think it might actually be an issue with Jetpack Compose Desktop (though I couldn't find any documentation or runtime errors with this suggesting it wouldn't work — on calling some test methods they literally call TODO() internally, so you know for a fact they're not implemented. This one doesn't.) I'll double-check once on the official Kotlin Slack, and if it's an open problem I'll self-answer here and post an issue as you suggested. – Mindstormer619 Jun 07 '22 at 12:39

0 Answers0