https://kotlinlang.org logo
#compose
Title
# compose
j

João Silva

03/08/2024, 4:26 PM
Hi everyone! I'm currently integrating Roborazzi in my app to start having a golden for our design system. I'm facing an issue where I'm not being able to capture a screenshot for the "Pressed" mode. Has anyone done this? I'll leave more details on the 🧵
Test:
Copy code
@Test
    fun primaryButtonPressed() {
        composeTestRule.setTestContent {
            TextButton(
                onClick = { },
                modifier = Modifier.testTag("tag")
            ) {
                Text(text = "Button")
            }
        }

        composeTestRule
            .onNodeWithTag("tag")
            .performTouchInput {
                down(Offset.Zero)
            }
            .captureRoboImage()
    }
Expected image is the first, what I actually get from roborazzi is the second.
Is this possible to achieve? I was hoping to be able to screenshot the pressed state. We have our custom behaviour, but the example uses the default material components
d

dai

04/07/2024, 8:22 AM
The best way to do this is to use a MutableInteraction source and emit a Press event via a LaunchedEffect on the screenshot test. Roborazzi should wait for idle so this will update before the screenshot is taken
Copy code
val interactionSource = remember { MutableInteractionSource() }
        
        Button(onClick = {}, interactionSource = interactionSource) {
            Text(text = "Label")
        }
        
        LaunchedEffect(Unit) {
            interactionSource.emit(PressInteraction.Press(Offset(0f, 0f)))
        }
j

João Silva

04/08/2024, 1:32 PM
This works perfectly. Thank you so much for the input!
2 Views