ndtp / android-testify

Add screenshots to your Android tests
Other
101 stars 5 forks source link

Orientation for Compose screenshots is broken by incorrect lifecycle callback to beforeScreenshot() #178

Open sergio-sastre opened 12 months ago

sergio-sastre commented 12 months ago

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:

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)
DanielJette commented 12 months ago

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.