diff --git a/README.md b/README.md index aa563c61..74cd54d6 100644 --- a/README.md +++ b/README.md @@ -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: diff --git a/core/src/main/scala/com/karumi/shot/screenshots/ScreenshotsComparator.scala b/core/src/main/scala/com/karumi/shot/screenshots/ScreenshotsComparator.scala index 85353ded..91fdbd22 100644 --- a/core/src/main/scala/com/karumi/shot/screenshots/ScreenshotsComparator.scala +++ b/core/src/main/scala/com/karumi/shot/screenshots/ScreenshotsComparator.scala @@ -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)) @@ -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( diff --git a/core/src/test/resources/images/sample-scrambled.png b/core/src/test/resources/images/sample-scrambled.png new file mode 100644 index 00000000..773be775 Binary files /dev/null and b/core/src/test/resources/images/sample-scrambled.png differ diff --git a/core/src/test/resources/images/sample.png b/core/src/test/resources/images/sample.png new file mode 100644 index 00000000..c9df3773 Binary files /dev/null and b/core/src/test/resources/images/sample.png differ diff --git a/core/src/test/scala/com/karumi/shot/screenshots/ScreenshotsComparatorTest.scala b/core/src/test/scala/com/karumi/shot/screenshots/ScreenshotsComparatorTest.scala new file mode 100644 index 00000000..b8b76a92 --- /dev/null +++ b/core/src/test/scala/com/karumi/shot/screenshots/ScreenshotsComparatorTest.scala @@ -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 + } +}