cashapp / paparazzi

Render your Android screens without a physical device or emulator
https://cashapp.github.io/paparazzi/
Apache License 2.0
2.31k stars 215 forks source link

How to pass Composable Preview parameters to the Snapshot tests #1687

Open Ruthvik-t opened 1 week ago

Ruthvik-t commented 1 week ago

Hi Team, I have been using Paparazzi for snapshot tests of our views built using Jetpack compose. We are using Reflections API to scan the code to find the method annotated with previews. These methods are then passed to Paparazzi for snapshot tests. Can we have capability of passing parameters to previews using ParameterPreviewProvider class from previews to snapshot tests. Is it possible ?. What about different previews that got generated with annotations like @PreviewScreenSizes, @PreviewFontScales, @PreviewLightDark, and @PreviewDynamicColors .

Here is my paparazzi snapshot test class

`class PaparazziTest {

@get:Rule
val paparazzi: Paparazzi = Paparazzi()

@Test
fun generateSnapshots() {
    val reflections = Reflections("xxx.zpreview", Scanners.MethodsAnnotated)
    val methods = reflections.getMethodsAnnotatedWith(Preview::class.java)
        .filterNot { java.lang.reflect.Modifier.isPrivate(it.modifiers) }

    val composeProxy = ComposeProxy()
    for (method in methods) {
        val name = "${method.declaringClass.simpleName}_${method.name}"

        try {
            snapshot(name = name) {
                composeProxy.wrappedInstance(method).GenerateSnapshot()
            }
        } catch (e: Throwable) {
            throw RuntimeException("Error generating snapshot $name", e)
        }
    }
}

private fun snapshot(name: String, composable: @Composable () -> Unit) {
    paparazzi.snapshot(name) {
        Box {
            composable()
        }
    }
}

private interface ComposeProxyInterface {

    @Composable
    fun GenerateSnapshot()
}

@Suppress("EmptyFunctionBlock")
private class ComposeProxy : ComposeProxyInterface {
    @Composable
    override fun GenerateSnapshot() {
    }

    fun wrappedInstance(composableFunction: Method): ComposeProxyInterface {
        val proxyClass = Proxy.getProxyClass(
            ComposeProxy::class.java.classLoader,
            ComposeProxyInterface::class.java
        )

        val invocationHandler = InvocationHandler { _, _, args: Array<out Any>? ->
            composableFunction.invoke(null, *args!!)
        }

        return proxyClass
            .getConstructor(InvocationHandler::class.java)
            .newInstance(invocationHandler) as ComposeProxyInterface
    }
}

}`

geoff-powell commented 1 week ago

This will be possible with the issue: https://github.com/cashapp/paparazzi/issues/973

We have some prs we hope to get in for the next alpha version soon.