如何测试AlertDialog的onDismissRequest属性?

Ali*_*lix 7 robolectric android-alertdialog android-jetpack-compose

最简单的形式是这样的对话框:


@Composable
fun MyDialog(
    showDialogState: MutableState<Boolean>
) {
    if (showDialogState.value) {
        AlertDialog(onDismissRequest = { showDialogState.value = false },
            // Other irrelevant attributes have been omitted
        )
    }
}
Run Code Online (Sandbox Code Playgroud)

如何在 Robolectric 中的此可组合项上触发“onDismissRequest”?

这通常是我构建可组合测试的方式:

@Config(sdk = [Build.VERSION_CODES.O_MR1])
@RunWith(AndroidJUnit4::class)
@LooperMode(LooperMode.Mode.PAUSED)
class MyDialogTest {

    @get:Rule
    val composeTestRule = createComposeRule()

    @Test
    fun `MyDialog - when showing state and dismissed - changes showing state`() {
        val state = mutableStateOf(true)
        composeTestRule.setContent {
            MyDialog(
                showDialogState = state
            )
        }

        // TODO: How do I trigger dismiss!?
        
        assertFalse(state.value)
    }

}
Run Code Online (Sandbox Code Playgroud)

撰写版本:1.1.0-rc01

Android Gradle 插件版本:7.0.4

Robolectric 版本: 4.7.3

Dan*_*lia 0

我引用官方文档:

当用户单击对话框外部或后退按钮时关闭对话框。如果您想禁用该功能,只需使用空的 onCloseRequest 即可。

https://foso.github.io/Jetpack-Compose-Playground/material/alertdialog/