Skip to content

Commit

Permalink
Merge pull request #480 from vladcudoidem/feature/#460
Browse files Browse the repository at this point in the history
Implement diff. percentage
  • Loading branch information
takahirom authored Sep 21, 2024
2 parents 0b0143e + 46cd0fa commit fe9d08a
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 3 deletions.
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>()

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

0 comments on commit fe9d08a

Please sign in to comment.