Skip to content

Commit 9e745d6

Browse files
committed
test: move Roborazzi results into subfolders
Previously all `_actual.png` and `_compare.png` were in the root of the Roborazzi output folder, which risked conflicts between different *Test.kt output screenshots. `Anki-Android/AnkiDroid/build/outputs/roborazzi` Moving these files into `/StudyScreenScreenshotTest` meant that correct and incorrect baselines were located in the same folder, which added noise to the comparison Structure: ``` roborazzi/StudyScreenScreenshotTest/ Phone_BOX_false.png Phone_BOX_true.png Tablet_BOX_false.png diffs/ # one file from the root is copied, along with diagnostics Phone_BOX_false.png Phone_BOX_false_actual.png Phone_BOX_false_compare.png ``` Requested: https://redirect.github .com/ankidroid/pull/20943#pullrequestreview-4224241914 Somewhat related to 20942 Assisted-by: Claude Opus 4.7 - diagnostics, some implementation
1 parent 6e6dcf4 commit 9e745d6

3 files changed

Lines changed: 44 additions & 8 deletions

File tree

AnkiDroid/src/test/java/com/ichi2/anki/ScreenshotTest.kt

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,13 @@
1515
*/
1616
package com.ichi2.anki
1717

18+
import com.github.takahirom.roborazzi.ExperimentalRoborazziApi
19+
import com.github.takahirom.roborazzi.RoborazziOptions
20+
import com.github.takahirom.roborazzi.captureScreenRoboImage
21+
import com.github.takahirom.roborazzi.provideRoborazziContext
1822
import org.junit.experimental.categories.Category
1923
import org.robolectric.annotation.GraphicsMode
24+
import java.io.File
2025

2126
interface ScreenshotTestCategory
2227

@@ -25,4 +30,36 @@ interface ScreenshotTestCategory
2530
*/
2631
@Category(ScreenshotTestCategory::class)
2732
@GraphicsMode(GraphicsMode.Mode.NATIVE)
28-
abstract class ScreenshotTest : RobolectricTest()
33+
abstract class ScreenshotTest : RobolectricTest() {
34+
/**
35+
* Captures a screenshot to `build/outputs/roborazzi/<TestClass>/<name>.png`.
36+
*
37+
* Writes to /diffs/ if there is an issue.
38+
*/
39+
@OptIn(ExperimentalRoborazziApi::class)
40+
protected fun captureScreen(name: String) {
41+
// Note: this.javaClass should not be used inside a lambda, as 'this' will be unnamed
42+
val classDir = "build/outputs/roborazzi/${this.javaClass.simpleName}"
43+
val diffDir = File("$classDir/diffs")
44+
// baseline is always in the root for the class, copied to /diffs/ if a change occurred
45+
val baseline = File("$classDir/$name.png")
46+
captureScreenRoboImage(
47+
filePath = baseline.path,
48+
roborazziOptions = provideRoborazziContext().options.withCompareOutputDir(diffDir.path),
49+
)
50+
51+
// copy the baseline into /diffs (if it exists)
52+
// /diffs/ is used so 'clean' baselines are not mixed with diffs to inspect
53+
val diffWritten =
54+
File(diffDir, "${name}_compare.png").exists() ||
55+
File(diffDir, "${name}_actual.png").exists()
56+
if (diffWritten && baseline.isFile) {
57+
baseline.copyTo(File(diffDir, baseline.name), overwrite = true)
58+
}
59+
}
60+
}
61+
62+
/** Sets the directory for _actual.png and _compare.png */
63+
@OptIn(ExperimentalRoborazziApi::class)
64+
private fun RoborazziOptions.withCompareOutputDir(dir: String): RoborazziOptions =
65+
copy(compareOptions = compareOptions.copy(outputDirectoryPath = dir))

AnkiDroid/src/test/java/com/ichi2/anki/ui/windows/reviewer/StudyScreenScreenshotTest.kt

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616
package com.ichi2.anki.ui.windows.reviewer
1717

1818
import androidx.test.core.app.ActivityScenario
19-
import com.github.takahirom.roborazzi.ExperimentalRoborazziApi
20-
import com.github.takahirom.roborazzi.captureScreenRoboImage
2119
import com.ichi2.anki.ScreenshotTest
2220
import com.ichi2.anki.previewer.CardViewerActivity
2321
import com.ichi2.anki.settings.Prefs
@@ -40,15 +38,14 @@ class StudyScreenScreenshotTest(
4038
RuntimeEnvironment.setQualifiers(config.qualifier.toString())
4139
}
4240

43-
@OptIn(ExperimentalRoborazziApi::class)
4441
@Test
4542
fun captureScreenshot() {
4643
ActivityScenario
4744
.launch<CardViewerActivity>(
4845
ReviewerFragment.getIntent(targetContext),
4946
).use { scenario ->
5047
scenario.onActivity {
51-
captureScreenRoboImage(filePath = "build/outputs/roborazzi/${this.javaClass.simpleName}/$config.png")
48+
captureScreen(config.toString())
5249
}
5350
}
5451
}

tools/compare-screenshot-test.sh

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -154,9 +154,11 @@ gradle_quiet "${REPO_ROOT}/gradlew" -p "$REPO_ROOT" \
154154
${GRADLE_TESTS_ARG[@]+"${GRADLE_TESTS_ARG[@]}"}
155155

156156

157-
shopt -s nullglob
158-
diffs=("${OUT_DIR}"/*_compare.png)
159-
shopt -u nullglob
157+
# Recurse — Roborazzi writes *_compare.png inside directories
158+
diffs=()
159+
while IFS= read -r line; do
160+
diffs+=("$line")
161+
done < <(find "$OUT_DIR" -type f -name '*_compare.png')
160162

161163
if [ ${#diffs[@]} -eq 0 ]; then
162164
label="${TEST_FILTER:-all screenshot tests}"

0 commit comments

Comments
 (0)