Skip to content

Commit

Permalink
Consider pixel order when comparing images (#227)
Browse files Browse the repository at this point in the history
* Consider pixel order when comparing images

* Fix typo on README
  • Loading branch information
andreroggeri authored Jul 1, 2021
1 parent deb900d commit 1394350
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 15 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ This is the list of most useful Gradle tasks you might need divided by type of p
* ``./gradlew publishToMavenLocal``: Install Shot in your local gradle repository.
* ``./gradlew test``: Execute all the tests related to the Gradle plugin.
* ``./gradlew scalafmtAll`: Review the Gradle Plugin's checkstyle.
* ``./gradlew scalafmtAll``: Review the Gradle Plugin's checkstyle.
* Consumers related tasks:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ class ScreenshotsComparator {
}

private def compareScreenshot(
screenshot: Screenshot,
tolerance: Double
): Option[ScreenshotComparisonError] = {
screenshot: Screenshot,
tolerance: Double
): Option[ScreenshotComparisonError] = {
val recordedScreenshotFile = new File(screenshot.recordedScreenshotPath)
if (!recordedScreenshotFile.exists()) {
Some(ScreenshotNotFound(screenshot))
Expand All @@ -39,23 +39,23 @@ class ScreenshotsComparator {
}

private def imagesAreDifferent(
screenshot: Screenshot,
oldScreenshot: Image,
newScreenshot: Image,
tolerance: Double
) = {
screenshot: Screenshot,
oldScreenshot: Image,
newScreenshot: Image,
tolerance: Double
) = {
if (oldScreenshot == newScreenshot) {
false
} else {
val oldScreenshotPixels = oldScreenshot.pixels
val newScreenshotPixels = newScreenshot.pixels

val differentPixels =
oldScreenshotPixels.diff(newScreenshotPixels).length
val percentageOfDifferentPixels =
differentPixels.toDouble / oldScreenshotPixels.length.toDouble
val percentageOutOf100 = percentageOfDifferentPixels * 100.0
val imagesAreDifferent = percentageOutOf100 >= tolerance
val imagesAreConsideredEquals = !imagesAreDifferent
oldScreenshotPixels.zip(newScreenshotPixels).filter { case (a, b) => a != b }
val percentageOfDifferentPixels = differentPixels.length. toDouble / oldScreenshotPixels.length.toDouble
val percentageOutOf100 = percentageOfDifferentPixels * 100.0
val imagesAreDifferent = percentageOutOf100 >= tolerance
val imagesAreConsideredEquals = !imagesAreDifferent
if (imagesAreConsideredEquals && tolerance != Config.defaultTolerance) {
val screenshotName = screenshot.name
println(
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added core/src/test/resources/images/sample.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.karumi.shot.screenshots

import com.karumi.shot.Resources
import com.karumi.shot.domain.{Dimension, Screenshot}
import com.karumi.shot.domain.model.ScreenshotsSuite
import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.should

class ScreenshotsComparatorTest extends AnyFlatSpec with should.Matchers with Resources {

private val sampleImage = getClass.getClassLoader.getResource("images/sample.png").getPath
private val sampleImageScrambled =
getClass.getClassLoader.getResource("images/sample-scrambled.png").getPath

it should "not match image with same pixels but different order" in {
val comparator = new ScreenshotsComparator()
val screenshot = Screenshot("test",
sampleImage,
sampleImageScrambled,
"SomeClass",
"ShoudFail",
Dimension(768, 1280),
null,
null,
null,
List(sampleImageScrambled),
Dimension(768, 1280))
val suite: ScreenshotsSuite = List(screenshot)

val result = comparator.compare(suite, 0.01)

result.hasErrors shouldBe true
}
}

0 comments on commit 1394350

Please sign in to comment.