mattinger
12/02/2024, 7:51 PM@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun ModalBottomSheetExample(
visible: Boolean,
onVisibleChange: (Boolean) -> Unit
) {
if (visible) {
ModalBottomSheet(
onDismissRequest = {
onVisibleChange(false)
},
dragHandle = {
Row {
IconButton(onClick = {
onVisibleChange(false)
}) {
Icon(imageVector = Icons.Default.Close, contentDescription = "close")
}
}
}
) {
Text("Content")
}
}
}
If i put this in a preview function, everything works just fine. However, when i try to use this in a compose unit test, the "onClick" of the IconButton is never triggered, and as a result, the "onVisibleChange" function is never called and i can't get the dialog to go awaymattinger
12/02/2024, 7:58 PM@RunWith(AndroidJUnit4::class)
class ModalBottomSheetTest {
@OptIn(ExperimentalTestApi::class)
@Test
fun x() {
runComposeUiTest {
var visible by mutableStateOf(true)
val mockOnClose = mock<() -> Unit>()
setContent {
ModalBottomSheetExample(
visible = visible,
onVisibleChange = {
mockOnClose()
visible = it
}
)
}
onNodeWithContentDescription("close")
.assertExists()
.performClick()
waitForIdle()
verify(mockOnClose)()
}
}
}
Wout Werkman
12/02/2024, 8:49 PMmutableStateOf(true)
in a remember { ... }
mattinger
12/03/2024, 1:41 PMmattinger
12/03/2024, 1:43 PMmattinger
12/03/2024, 1:46 PMmattinger
12/03/2024, 8:28 PM@Test
fun modalDialogClickIssue() {
runComposeUiTest {
setContent {
var visible by remember { mutableStateOf(true) }
if (visible) {
ModalBottomSheet(
onDismissRequest = {
visible = false
},
) {
Button(
modifier = Modifier.testTag("hide"),
onClick = {
visible = false
}
) {
Text("Hide")
}
}
}
}
waitUntilNodeCount(isDialog(), 1)
// This does not trigger the onClick callbacks of the button
onNodeWithTag("hide", useUnmergedTree = true)
.assertExists()
.performClick()
// This works though
/*
onNode(keyIsDefined(SemanticsActions.Dismiss))
.performSemanticsAction(SemanticsActions.Dismiss)
*/
waitUntilNodeCount(isDialog(), 0)
}
}
mattinger
12/03/2024, 9:41 PMmattinger
12/09/2024, 2:21 PM