takahirom / roborazzi

Make JVM Android integration test visible 🤖📸
https://takahirom.github.io/roborazzi/
Apache License 2.0
654 stars 25 forks source link

Support themed AndroidViews inside compose #279

Closed fcastagnozzi-ta closed 3 months ago

fcastagnozzi-ta commented 3 months ago

Using captureRoboImage with something like

    MaterialTheme {
        Column {
            Button(onClick = { }) {

            }
            AndroidView(factory = {
                MaterialButton(it)
            })
        }
    }

doesn't work because the MaterialButton button expects to get its attributes from the activity's theme, but the capture happens on RoborazziTransparentActivity which has its own theme. Would need a way to provide our own activity or theme.

takahirom commented 3 months ago

@fcastagnozzi-ta Thank you for reporting this issue. There are a few options to resolve this. You can use ComposeRule and specify the Activity. However, I think it would be easier to provide LocalContext in the test. You can use ContextThemeWrapper to wrap the existing context. It appears that Compose's AndroidView uses LocalContext to create the View. https://cs.android.com/androidx/platform/frameworks/support/+/androidx-main:compose/ui/ui/src/androidMain/kotlin/androidx/compose/ui/viewinterop/AndroidView.android.kt;l=269?q=AndroidView

fcastagnozzi-ta commented 3 months ago

I see, that looks like it should work. Thanks!