takahirom / roborazzi

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

Unexpected results when using with Compose Modifier.drawWithContent and BlendMode #352

Closed adrinieto closed 4 months ago

adrinieto commented 4 months ago

When using Modifier.drawWithContent and BlendMode.BlendMode.SrcIn I get a different result than in a real device or emulator. For example, this is the Compose code to build a partially filled star icon:

Icon(
    painterResource(R.drawable.ic_star), contentDescription = null,
    modifier = Modifier
        .size(200.dp)
        .drawWithContent {
            drawContent()
            drawRect(Color.Yellow, size = Size(size.width * 0.7f, size.height), blendMode = BlendMode.SrcIn)
        },
    )

Here are some results I got using the sample Android app in ComposeTest.

If I use createComposeRule, that internally uses a ComponentActivity I get a square image.

  @get:Rule
  val composeTestRule = createComposeRule()

image

But, If I use RoborazziTransparentActivity it works.

 @get:Rule
  val composeTestRule = createAndroidComposeRule<RoborazziTransparentActivity>()

image

After further investigation, it seems related with the colorBackground. It only works with a transparent background:

    <item name="android:colorBackground">@android:color/transparent</item>

When used in Compose Previews it works as expected, so I'm not sure what's going on.

Is this a problem of Roborazzi or a limitation of using Layoutlib?

Thanks in advance.

adrinieto commented 4 months ago

In case it helps, to make BlendMode.SrcIn work in Compose .graphicsLayer(compositingStrategy = CompositingStrategy.Offscreen) is also needed. But the result is the same with Roborazzi.

https://stackoverflow.com/a/69790654/1518996

takahirom commented 4 months ago

Cloud you try to use hardware rendering mode? https://github.com/takahirom/roborazzi/issues/255#issuecomment-1972838571

adrinieto commented 4 months ago

Thanks for your fast response. It works! 🙌

Should we enable this flag for all the project? Or only for specific screens? I see it also solves issues with elevations, shadows, and gradients. I guess there are some side effects and that's why it's not enabled by default.

takahirom commented 4 months ago

Good question! I think you can check this issue. I think we can enable the flag in the next Robolectric version for all projects https://github.com/takahirom/roborazzi/issues/296