Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
1 of 5 tasks
sergio-sastre opened this issue Oct 7, 2023 · 1 comment
Labels
Bug Any behaviour the deviates from the documentation, expected outcome or API contract.

Comments

@sergio-sastre
Copy link

sergio-sastre commented Oct 7, 2023

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:

  • 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)
@DanielJette DanielJette added Bug Any behaviour the deviates from the documentation, expected outcome or API contract. and removed Feedback labels Oct 7, 2023
@DanielJette
Copy link
Contributor

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.

@DanielJette DanielJette changed the title Support orientation for Compose screenshots Orientation for Compose screenshots is broken by incorrect lifecycle callback to beforeScreenshot() Oct 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Any behaviour the deviates from the documentation, expected outcome or API contract.
Projects
None yet
Development

No branches or pull requests

2 participants