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

Support verify gifs #902

Closed
wants to merge 3 commits into from
Closed
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 @@ -101,7 +101,7 @@ public class HtmlReportWriter @JvmOverloads constructor(
val shot = if (hashes.size == 1) {
val original = File(imagesDirectory, "${hashes[0]}.png")
if (isRecording) {
val goldenFile = File(goldenImagesDirectory, snapshot.toFileName("_", "png"))
val goldenFile = snapshot.goldenFile(goldenImagesDirectory)
original.copyTo(goldenFile, overwrite = true)
}
snapshot.copy(file = original.toJsonPath())
Expand All @@ -110,11 +110,10 @@ public class HtmlReportWriter @JvmOverloads constructor(

if (isRecording) {
for ((index, frameHash) in hashes.withIndex()) {
val originalFrame = File(imagesDirectory, "$frameHash.png")
val frameSnapshot = snapshot.copy(name = "${snapshot.name} $index")
val goldenFile = File(goldenImagesDirectory, frameSnapshot.toFileName("_", "png"))
val goldenFile = snapshot.goldenFile(goldenImagesDirectory, frame = index)
if (!goldenFile.exists()) {
originalFrame.copyTo(goldenFile)
val original = File(imagesDirectory, "$frameHash.png")
original.copyTo(goldenFile)
}
}
}
Expand Down
10 changes: 10 additions & 0 deletions paparazzi/src/main/java/app/cash/paparazzi/Snapshot.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package app.cash.paparazzi

import dev.drewhamilton.poko.Poko
import java.io.File
import java.util.Date
import java.util.Locale

Expand Down Expand Up @@ -47,3 +48,12 @@ internal fun Snapshot.toFileName(
}
return "${testName.packageName}${delimiter}${testName.className}${delimiter}${testName.methodName}$formattedLabel.$extension"
}

internal fun Snapshot.goldenFile(goldenImagesDirectory: File, frame: Int? = null): File {
return if (frame == null) {
File(goldenImagesDirectory, toFileName("_", "png"))
} else {
val frameSnapshot = copy(name = "$name $frame")
File(goldenImagesDirectory, frameSnapshot.toFileName("_", "png"))
}
}
13 changes: 10 additions & 3 deletions paparazzi/src/main/java/app/cash/paparazzi/SnapshotVerifier.kt
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,14 @@ public class SnapshotVerifier @JvmOverloads constructor(
fps: Int
): FrameHandler {
return object : FrameHandler {
var frame = 0
override fun handle(image: BufferedImage) {
// Note: does not handle videos or its frames at the moment
val expected = File(imagesDirectory, snapshot.toFileName(extension = "png"))
// Note: does not handle videos at the moment
val expected = if (frameCount == 1) {
snapshot.goldenFile(imagesDirectory)
} else {
snapshot.goldenFile(imagesDirectory, frame++)
}
if (!expected.exists()) {
throw AssertionError("File $expected does not exist")
}
Expand All @@ -55,7 +60,9 @@ public class SnapshotVerifier @JvmOverloads constructor(
)
}

override fun close() = Unit
override fun close() {
frame = 0
}
}
}

Expand Down
59 changes: 59 additions & 0 deletions paparazzi/src/test/java/app/cash/paparazzi/SnapshotKtTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package app.cash.paparazzi

import com.google.common.truth.Truth.assertThat
import org.junit.Test
import java.io.File
import java.time.Instant
import java.util.Date

class SnapshotKtTest {
@Test
fun withName() {
val path = Snapshot(
name = "loading",
testName = TestName("app.cash.paparazzi", "CelebrityTest", "testSettings"),
timestamp = Instant.parse("2019-03-20T10:27:43Z").toDate()
).goldenFile(File("/a/path"))

assertThat(path)
.isEqualTo(File("/a/path/app.cash.paparazzi_CelebrityTest_testSettings_loading.png"))
}

@Test
fun withoutName() {
val path = Snapshot(
name = null,
testName = TestName("app.cash.paparazzi", "CelebrityTest", "testSettings"),
timestamp = Instant.parse("2019-03-20T10:27:43Z").toDate()
).goldenFile(File("/a/path"))

assertThat(path)
.isEqualTo(File("/a/path/app.cash.paparazzi_CelebrityTest_testSettings.png"))
}

@Test
fun withNameAndFrame() {
val path = Snapshot(
name = "loading",
testName = TestName("app.cash.paparazzi", "CelebrityTest", "testSettings"),
timestamp = Instant.parse("2019-03-20T10:27:43Z").toDate()
).goldenFile(File("/a/path"), 0)

assertThat(path)
.isEqualTo(File("/a/path/app.cash.paparazzi_CelebrityTest_testSettings_loading_0.png"))
}

@Test
fun withoutNameAndWithFrame() {
val path = Snapshot(
name = null,
testName = TestName("app.cash.paparazzi", "CelebrityTest", "testSettings"),
timestamp = Instant.parse("2019-03-20T10:27:43Z").toDate()
).goldenFile(File("/a/path"), 1)

assertThat(path)
.isEqualTo(File("/a/path/app.cash.paparazzi_CelebrityTest_testSettings_null_1.png"))
}
}

private fun Instant.toDate() = Date(toEpochMilli())
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,7 @@ class KeypadViewTest {
}
rotation.start()

// Uncomment once snapshot verification supports videos
// paparazzi.gif(root, "spin", start = 500, end = 1500, fps = 30)
paparazzi.gif(root, "spin", start = 500, end = 1500, fps = 30)
}
}
}