Orientation for Compose screenshots is broken by incorrect lifecycle callback to beforeScreenshot()
Is your feature request related to a problem? Please describe. I’d like to take screenshots of composables in portrait as well as in landscape orientation
This feedback relates to:
- [x] The Kotlin library
- [ ] The Grade plugin
- [ ] The IntelliJ Platform plugin
- [ ] The sample code
- [ ] The documentation
Describe the solution you'd like That ComposableScreenshotTestRule supports orientation, dame as ScreenshotTestRule
Describe alternatives you've considered I’ve considered using ScreenshotTestRule and inflating the Composable inside the activity, what actually might work but looks like sth that I should not do as user of the library.
Test
@ScreenshotInstrumentation
@Test
fun landscape() {
rule
.configure {
orientation = SCREEN_ORIENTATION_LANDSCAPE
}
.setCompose {
val orientation = "Landscape".takeIf { rule.activity.isLandscape } ?: "Portrait"
Text(
text = "Orientation is $orientation",
fontSize = 16.sp
)
}
.assertSame()
}
Error
dev.testify.sample.compose.ComposableScreenshotTest:
Error in landscape(dev.testify.sample.compose.ComposableScreenshotTest):
java.lang.IllegalStateException: Target view has 0 size. Verify if you have provided a ComposeTestRule instance to ComposableScreenshotRule.
at dev.testify.ComposableScreenshotRule.beforeScreenshot(ComposableScreenshotRule.kt:145)
at dev.testify.ScreenshotRule.assertSame(ScreenshotRule.kt:455)
at dev.testify.sample.compose.ComposableScreenshotTest.landscape(ComposableScreenshotTest.kt:206)
Hi @sergio-sastre
Thanks for the request. In theory, ComposableScreenshotRule does support changing the orientation in the same was as the base ScreenshotTestRule.
This code should work:
@ScreenshotInstrumentation
@Test
fun landscape() {
rule
.configure {
orientation = SCREEN_ORIENTATION_LANDSCAPE
}
.setCompose {
val orientation = "Landscape".takeIf { rule.activity.isLandscape } ?: "Portrait"
Text(
text = "Orientation is $orientation",
fontSize = 16.sp
)
}
.assertSame()
}
However, it appears that there is a lifecyle bug in the beforeScreenshot() callback and it's not providing the correct Activity for Compose to draw its composables.
I'm going to change this ticket to a bug and try to get a fix in for this soon.