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

Support @Preview params "widthDp" and "heightDp" #559

Closed
sergio-sastre opened this issue Nov 17, 2024 · 3 comments
Closed

Support @Preview params "widthDp" and "heightDp" #559

sergio-sastre opened this issue Nov 17, 2024 · 3 comments

Comments

@sergio-sastre
Copy link
Contributor

I'd like to add support for "widthDp" and "heightDp" params

For that, I've tried the solution described in this page

if (preview.previewInfo.widthDp != -1 && preview.previewInfo.heightDp != -1) {
        setDisplaySize(preview.previewInfo.widthDp, preview.previewInfo.heightDp)
}

private fun setDisplaySize(widthDp: Int, heightDp: Int) {
    val context = ApplicationProvider.getApplicationContext<Context>()
    val display = ShadowDisplay.getDefaultDisplay()
    val density = context.resources.displayMetrics.density
    widthDp.let {
        val widthPx = (widthDp * density).roundToInt()
        Shadows.shadowOf(display).setWidth(widthPx)
    }
    heightDp.let {
        val heightPx = (heightDp * density).roundToInt()
        Shadows.shadowOf(display).setHeight(heightPx)
    }
}

but it does not seem to work. Could be that I am missing something?

@sergio-sastre
Copy link
Contributor Author

So I got it working in a private project, it seems to work but one has to call composeTestRule.activityRule.scenario.recreate() afterwards.

I'll try to create a PR to add this functionality to Roborazzi this week

@sergio-sastre
Copy link
Contributor Author

Seems 2 or 3 more adjustments were necessary to make the Views look exactly as in the Previews and avoid using a ComposeTestRule, which was making the test execution considerably slower.

One point to discuss will be the RoborazziTransparentTheme, which does
android:windowActionBar = false and that influences the size set via Robolectric qualifiers or the ShadowDisplay. It should be wither set to true or use another Activity.

I’ll create a PR today or tomorrow when I find time, which will also include support for “showBackground” and “backgroundColor”.

@sergio-sastre
Copy link
Contributor Author

@takahirom

Draft PR #576

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant