Skip to content

Conversation

rqcai
Copy link
Collaborator

@rqcai rqcai commented Sep 10, 2025

No description provided.

@rqcai rqcai force-pushed the rcai_web_snapshot branch 9 times, most recently from 769e0f1 to edae224 Compare September 11, 2025 22:04
@rqcai rqcai changed the title test Generate diff images for web snapshots Sep 11, 2025
@@ -69,6 +72,7 @@ public class DomSnapshotter @PublishedApi internal constructor(
this.canvasWidth = this.width
this.canvasHeight = this.height
this.pixelRatio = frame.pixelRatio
this.backgroundColor = "transparent"
Copy link
Collaborator Author

@rqcai rqcai Sep 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

redwood-layout-dom ci was failing because the default background color is white instead of transparent. Here I'm explicitly setting the background color to transparent so the snapshot background color would be consistent.

testBothSpacer diff from https://github.com/cashapp/redwood/actions/runs/17656504943/artifacts/3990317185

@rqcai rqcai marked this pull request as ready for review September 11, 2025 22:45
Copy link
Collaborator

@JakeWharton JakeWharton left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So cool!

ctx.putImageData(deltaData, maxWidth.toDouble(), 0.0)

// Convert canvas to blob
val deltaBlob = suspendCancellableCoroutine<Blob> { continuation ->
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don’t see why this call is necessary?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we need to convert the delta image to blob so we can write to file.

val total = maxHeight.toLong() * maxWidth.toLong() * 3L * 256L
var percentDifference = (delta * 100 / total.toDouble()).toFloat()

// Fallback to pixel difference if color delta is 0 but pixels are different
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This case is for when it’s only different on the alpha channel?


// Fallback to pixel difference if color delta is 0 but pixels are different
if (differentPixels > 0 && percentDifference == 0f) {
percentDifference = (differentPixels * 100 / (maxWidth * maxHeight).toDouble()).toFloat()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should percentDifference be the max of these two measurements?

val percentDifferenceRgb = ...
val percentDifferenceAlpha = ...
val percentDifference = maxOf(percentDifferenceRgb, percentDifferenceAlpha)

?

As is if the alpha difference is 100% but the pixel difference is 0.1%, we’ll report 0.1%.

(That gets to be a practical problem when we support stuff like fuzzy-matching and accept a tolerance of .1%)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you meant take percentDifferenceRgb if it's not 0 else percentDifferenceAlpha

Copy link
Contributor

@squarejesse squarejesse left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I love it.

In a follow-up would you mind extracting this new code into a new class like ImageDiffer or something, and writing a couple test cases for it directly? I know testing tests is a bit yak-shaving, but in this case the code is subtle enough that I think it’s warranted. Plus I want to extract this code into a standalone library for everyone to use!

@squarejesse
Copy link
Contributor

squarejesse commented Sep 12, 2025

In particular I’d like test cases to cover things like

  • Images match exactly
  • Images differ on RGB by 10%, and confirm we get a 10% back on percent difference
  • Images match on alpha by 10%, and confirm we get a 10% back on percent difference

You can probably use code like this to create sample images, but altering sizes or whatever to create diffs.

    val yellowRect = document.createElement("div").apply {
      setAttribute(
        "style",
        """
        |background-color: #ffff00;
        |width: 200px;
        |height: 100px;
        |position: relative
        """.trimMargin(),
      )
      appendChild(
        document.createElement("div").apply {
          setAttribute(
            "style",
            """
            |background-color: #0000ff;
            |position: absolute;
            |width: 50px;
            |height: 25px;
            |top: 10px;
            |right: 20px;
            """.trimMargin(),
          )
        },
      )
    }

@rqcai rqcai merged commit a7f727c into trunk Sep 12, 2025
14 checks passed
@rqcai rqcai deleted the rcai_web_snapshot branch September 12, 2025 17:29
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

Successfully merging this pull request may close these issues.

3 participants