Skip to content

Commit

Permalink
Merge pull request #87 from ZacSweers/z/modernizeAndroid
Browse files Browse the repository at this point in the history
Modernize AGP integration
  • Loading branch information
takahirom authored Jul 10, 2023
2 parents b3a6c5c + 61f2057 commit f3ccbee
Show file tree
Hide file tree
Showing 3 changed files with 205 additions and 106 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,17 @@ class RoborazziGradleProject(val testProjectDir: TemporaryFolder) {
return runTask(task, BuildType.BuildAndFail)
}

fun verifyAndRecord(): BuildResult {
val task = "verifyAndRecordRoborazziDebug"
return runTask(task)
}

fun verifyAndRecordAndFail(): BuildResult {
val task = "verifyAndRecordRoborazziDebug"
return runTask(task, BuildType.BuildAndFail)
}


fun compare(): BuildResult {
val task = "compareRoborazziDebug"
return runTask(task)
Expand Down Expand Up @@ -169,14 +180,40 @@ dependencies {
}
}

fun checkCompareFileNotExists() {
val recordedFile =
testProjectDir.root.resolve("app/build/test-results/roborazzi/compare-report.json")
assert(!recordedFile.exists()) {
"File exists: ${recordedFile.absolutePath}"
}
}

fun checkCompareFileExists() {
val recordedFile =
testProjectDir.root.resolve("app/build/test-results/roborazzi/compare-report.json")
assert(recordedFile.exists()) {
"File not exists: ${recordedFile.absolutePath}"
}
}

fun checkRecordedFileExists(path: String) {
val recordedFile = testProjectDir.root.resolve(path)
assert(recordedFile.exists())
assert(recordedFile.exists()) {
"File not exists: $path"
}
}

fun getFileHash(path: String): Int {
val recordedFile = testProjectDir.root.resolve(path)
return recordedFile.readBytes().contentHashCode()
}


fun checkRecordedFileNotExists(path: String) {
val recordedFile = testProjectDir.root.resolve(path)
assert(!recordedFile.exists())
assert(!recordedFile.exists()) {
"File exists: $path"
}
}

fun changeScreen() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.github.takahirom.roborazzi

import org.junit.Assert.assertEquals
import org.junit.Assert.assertNotEquals
import org.junit.Rule
import org.junit.Test
import org.junit.rules.TemporaryFolder
Expand All @@ -8,17 +10,19 @@ class RoborazziGradleProjectTest {

@get:Rule
val testProjectDir = TemporaryFolder()
private val pathAndName =

private val screenshotAndName =
"app/build/outputs/roborazzi/com.github.takahirom.integration_test_project.RoborazziTest"

@Test
fun record() {
RoborazziGradleProject(testProjectDir).apply {
record()

checkRecordedFileExists("$pathAndName.testCapture.png")
checkRecordedFileNotExists("$pathAndName.testCapture_compare.png")
checkRecordedFileNotExists("$pathAndName.testCapture_actual.png")
checkCompareFileNotExists()
checkRecordedFileExists("$screenshotAndName.testCapture.png")
checkRecordedFileNotExists("$screenshotAndName.testCapture_compare.png")
checkRecordedFileNotExists("$screenshotAndName.testCapture_actual.png")
}
}

Expand All @@ -29,9 +33,10 @@ class RoborazziGradleProjectTest {
// Record task shouldn't be skipped even after unit test
recordWithSystemParameter()

checkRecordedFileExists("$pathAndName.testCapture.png")
checkRecordedFileNotExists("$pathAndName.testCapture_compare.png")
checkRecordedFileNotExists("$pathAndName.testCapture_actual.png")
checkCompareFileNotExists()
checkRecordedFileExists("$screenshotAndName.testCapture.png")
checkRecordedFileNotExists("$screenshotAndName.testCapture_compare.png")
checkRecordedFileNotExists("$screenshotAndName.testCapture_actual.png")
}
}

Expand All @@ -42,9 +47,10 @@ class RoborazziGradleProjectTest {
// Record task shouldn't be skipped even after unit test
record()

checkRecordedFileExists("$pathAndName.testCapture.png")
checkRecordedFileNotExists("$pathAndName.testCapture_compare.png")
checkRecordedFileNotExists("$pathAndName.testCapture_actual.png")
checkCompareFileNotExists()
checkRecordedFileExists("$screenshotAndName.testCapture.png")
checkRecordedFileNotExists("$screenshotAndName.testCapture_compare.png")
checkRecordedFileNotExists("$screenshotAndName.testCapture_actual.png")
}
}

Expand All @@ -53,11 +59,16 @@ class RoborazziGradleProjectTest {
RoborazziGradleProject(testProjectDir).apply {
record()
changeScreen()
val recordFileHash1 = getFileHash("$screenshotAndName.testCapture.png")

verifyAndFail()

checkRecordedFileExists("$pathAndName.testCapture.png")
checkRecordedFileExists("$pathAndName.testCapture_compare.png")
checkRecordedFileExists("$pathAndName.testCapture_actual.png")
val recordFileHash2 = getFileHash("$screenshotAndName.testCapture.png")
assertEquals(recordFileHash1, recordFileHash2)
checkCompareFileNotExists()
checkRecordedFileExists("$screenshotAndName.testCapture.png")
checkRecordedFileExists("$screenshotAndName.testCapture_compare.png")
checkRecordedFileExists("$screenshotAndName.testCapture_actual.png")
}
}

Expand All @@ -68,9 +79,43 @@ class RoborazziGradleProjectTest {
record()
verify()

checkRecordedFileExists("$pathAndName.testCapture.png")
checkRecordedFileNotExists("$pathAndName.testCapture_compare.png")
checkRecordedFileNotExists("$pathAndName.testCapture_actual.png")
checkCompareFileNotExists()
checkRecordedFileExists("$screenshotAndName.testCapture.png")
checkRecordedFileNotExists("$screenshotAndName.testCapture_compare.png")
checkRecordedFileNotExists("$screenshotAndName.testCapture_actual.png")
}
}


@Test
fun verifyAndRecord_changeDetect() {
RoborazziGradleProject(testProjectDir).apply {
record()
val recordFileHash1 = getFileHash("$screenshotAndName.testCapture.png")
changeScreen()

verifyAndRecordAndFail()

val recordFileHash2 = getFileHash("$screenshotAndName.testCapture.png")
assertNotEquals(recordFileHash1, recordFileHash2)
checkCompareFileNotExists()
checkRecordedFileExists("$screenshotAndName.testCapture.png")
checkRecordedFileExists("$screenshotAndName.testCapture_compare.png")
checkRecordedFileNotExists("$screenshotAndName.testCapture_actual.png")
}
}


@Test
fun verifyAndRecord_nochange() {
RoborazziGradleProject(testProjectDir).apply {
record()
verifyAndRecord()

checkCompareFileNotExists()
checkRecordedFileExists("$screenshotAndName.testCapture.png")
checkRecordedFileNotExists("$screenshotAndName.testCapture_compare.png")
checkRecordedFileNotExists("$screenshotAndName.testCapture_actual.png")
}
}

Expand All @@ -81,9 +126,11 @@ class RoborazziGradleProjectTest {
changeScreen()
compare()

checkRecordedFileExists("$pathAndName.testCapture.png")
checkRecordedFileExists("$pathAndName.testCapture_compare.png")
checkRecordedFileExists("$pathAndName.testCapture_actual.png")
checkCompareFileExists()
checkRecordedFileExists("$screenshotAndName.testCapture.png")
checkRecordedFileExists("$screenshotAndName.testCapture.png")
checkRecordedFileExists("$screenshotAndName.testCapture_compare.png")
checkRecordedFileExists("$screenshotAndName.testCapture_actual.png")
}
}

Expand All @@ -93,9 +140,10 @@ class RoborazziGradleProjectTest {
record()
compare()

checkRecordedFileExists("$pathAndName.testCapture.png")
checkRecordedFileNotExists("$pathAndName.testCapture_compare.png")
checkRecordedFileNotExists("$pathAndName.testCapture_actual.png")
checkCompareFileExists()
checkRecordedFileExists("$screenshotAndName.testCapture.png")
checkRecordedFileNotExists("$screenshotAndName.testCapture_compare.png")
checkRecordedFileNotExists("$screenshotAndName.testCapture_actual.png")
}
}
}
Loading

0 comments on commit f3ccbee

Please sign in to comment.