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

Implement diff. percentage #480

Merged
merged 2 commits into from
Sep 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.github.takahirom.roborazzi

import com.dropbox.differ.ImageComparator
import java.io.File
import kotlin.properties.Delegates

fun interface EmptyCanvasFactory {
operator fun invoke(
Expand Down Expand Up @@ -84,17 +85,23 @@ fun processOutputImageAndReport(
bufferedImageType = recordOptions.pixelBitConfig.toBufferedImageType()
)
}

// Only used by CaptureResult.Changed
var diffPercentage by Delegates.notNull<Float>()
Copy link
Owner

Choose a reason for hiding this comment

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

I just want to know this: the reason you use Delegates.notNull<Float>() instead of var diffPercentage = NaN is that you want to trigger an error when the value is not set, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, I think it is a little more robust to force the developer to set the diffPercentage in each case separately than to set a default and only change it in the cases where it should be different. But it is mostly a matter of taste, in my opinion. I can change it if you wish.


val changed = if (height == goldenRoboCanvas.height && width == goldenRoboCanvas.width) {
val comparisonResult: ImageComparator.ComparisonResult =
newRoboCanvas.differ(
other = goldenRoboCanvas,
resizeScale = resizeScale,
imageComparator = roborazziOptions.compareOptions.imageComparator
)
diffPercentage = comparisonResult.pixelDifferences.toFloat() / comparisonResult.pixelCount
val changed = !roborazziOptions.compareOptions.resultValidator(comparisonResult)
reportLog("${goldenFile.name} The differ result :$comparisonResult changed:$changed")
changed
} else {
diffPercentage = Float.NaN // diff. percentage is not defined if new canvas and golden canvas dimensions differ
reportLog("${goldenFile.name} The image size is changed. actual = (${goldenRoboCanvas.width}, ${goldenRoboCanvas.height}), golden = (${newRoboCanvas.croppedWidth}, ${newRoboCanvas.croppedHeight})")
true
}
Expand Down Expand Up @@ -147,6 +154,7 @@ fun processOutputImageAndReport(
actualFile = actualFile.absolutePath,
goldenFile = goldenFile.absolutePath,
timestampNs = System.nanoTime(),
diffPercentage = diffPercentage,
contextData = contextData,
)
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ sealed interface CaptureResult {
override val actualFile:@Contextual String,
@SerialName("timestamp")
override val timestampNs: Long,
@SerialName("diff_percentage")
val diffPercentage: Float,
@SerialName("context_data")
override val contextData: Map<String,@Contextual Any>
) : CaptureResult {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
package io.github.takahirom.roborazzi

import com.github.takahirom.roborazzi.CaptureResult
import com.github.takahirom.roborazzi.CaptureResults.Companion.json
import com.github.takahirom.roborazzi.CaptureResults
import com.github.takahirom.roborazzi.CaptureResults.Companion.json
import com.github.takahirom.roborazzi.ResultSummary
import com.github.takahirom.roborazzi.absolutePath
import kotlinx.io.files.Path
import kotlinx.serialization.json.JsonObject
import kotlinx.serialization.json.boolean
import kotlinx.serialization.json.double
import kotlinx.serialization.json.doubleOrNull
import kotlinx.serialization.json.encodeToJsonElement
import kotlinx.serialization.json.float
import kotlinx.serialization.json.int
import kotlinx.serialization.json.intOrNull
import kotlinx.serialization.json.jsonArray
Expand Down Expand Up @@ -47,6 +46,7 @@ class CaptureResultTest {
goldenFile = "/golden_file",
actualFile = "/actual_file",
timestampNs = 123456789,
diffPercentage = 0.123f,
contextData = mapOf("key" to Long.MAX_VALUE - 100),
),
CaptureResult.Unchanged(
Expand Down Expand Up @@ -95,6 +95,12 @@ class CaptureResultTest {
expectedCaptureResult.timestampNs,
actualJsonResult["timestamp"]?.jsonPrimitive?.long
)
if (expectedCaptureResult is CaptureResult.Changed) {
assertEquals(
expectedCaptureResult.diffPercentage,
actualJsonResult["diff_percentage"]?.jsonPrimitive?.float
)
}
assertEquals(
expectedCaptureResult.contextData.entries.map { it.key to it.value },
(actualJsonResult["context_data"]?.jsonObject?.entries
Expand Down Expand Up @@ -154,6 +160,7 @@ class CaptureResultTest {
"actual_file_path": "actual_file",
"golden_file_path": "golden_file",
"timestamp": 123456789,
"diff_percentage": 0.123,
"context_data": {
"key": 9223372036854775707
}
Expand Down Expand Up @@ -201,6 +208,7 @@ class CaptureResultTest {
assertEquals("actual_file", actualChangedResult.actualFile)
assertEquals("golden_file", actualChangedResult.goldenFile)
assertEquals(123456789, actualChangedResult.timestampNs)
assertEquals(0.123f, actualChangedResult.diffPercentage)
assertEquals(9223372036854775707, actualChangedResult.contextData["key"])

val actualUnchangedResult = actualCaptureResultList[3] as CaptureResult.Unchanged
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,7 @@ fun SemanticsNodeInteraction.captureRoboImage(
actualFile = actualFilePath,
goldenFile = goldenFilePath,
timestampNs = getNanoTime(),
diffPercentage = Float.NaN,
contextData = emptyMap()
)
writeJson(result, resultsDir, nameWithoutExtension)
Expand Down Expand Up @@ -581,6 +582,7 @@ fun SemanticsNodeInteraction.captureRoboImage(
actualFile = actualFilePath,
goldenFile = goldenFilePath,
timestampNs = getNanoTime(),
diffPercentage = Float.NaN,
contextData = emptyMap()
)
writeJson(result, resultsDir, nameWithoutExtension)
Expand Down Expand Up @@ -623,6 +625,7 @@ fun SemanticsNodeInteraction.captureRoboImage(
actualFile = goldenFilePath,
goldenFile = goldenFilePath,
timestampNs = getNanoTime(),
diffPercentage = Float.NaN,
contextData = emptyMap()
)
writeJson(result, resultsDir, nameWithoutExtension)
Expand Down Expand Up @@ -665,6 +668,7 @@ fun SemanticsNodeInteraction.captureRoboImage(
actualFile = goldenFilePath,
goldenFile = goldenFilePath,
timestampNs = getNanoTime(),
diffPercentage = Float.NaN,
contextData = emptyMap()
)
writeJson(result, resultsDir, nameWithoutExtension)
Expand Down
Loading