Skip to content

Commit 8cabe3b

Browse files
committed
test: device and theme screenshot properties
add -Ptheme=dark to use dark theme and -Pdevice=tablet to use a tablet frame
1 parent 33ca703 commit 8cabe3b

5 files changed

Lines changed: 44 additions & 40 deletions

File tree

AnkiDroid/build.gradle

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,11 @@ android {
260260
// ./gradlew testFullDebugUnitTest -Pscreenshot
261261
if (project.hasProperty("screenshot")) {
262262
includeTags "com.ichi2.anki.ScreenshotTestCategory"
263+
264+
// ./gradlew :AnkiDroid:recordRoborazziPlayDebug -Pscreenshot -Ptheme=dark
265+
systemProperty "screenshot.theme", project.findProperty("theme") ?: "light"
266+
// ./gradlew :AnkiDroid:recordRoborazziPlayDebug -Pscreenshot -Pdevice=tablet
267+
systemProperty "screenshot.device", project.findProperty("device") ?: "phone"
263268
} else {
264269
excludeTags "com.ichi2.anki.ScreenshotTestCategory"
265270
}

AnkiDroid/src/main/java/com/ichi2/anki/settings/Prefs.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,7 @@ open class PrefsRepository(
360360

361361
//region Appearance
362362

363-
val appTheme: AppTheme by enumPref(R.string.app_theme_key, AppTheme.FOLLOW_SYSTEM)
363+
var appTheme: AppTheme by enumPref(R.string.app_theme_key, AppTheme.FOLLOW_SYSTEM)
364364
val dayTheme: DayTheme by enumPref(R.string.day_theme_key, DayTheme.LIGHT)
365365
val nightTheme: NightTheme by enumPref(R.string.night_theme_key, NightTheme.BLACK)
366366

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,6 @@ import org.robolectric.ParameterizedRobolectricTestRunner
4040
*/
4141
@RunWith(ParameterizedRobolectricTestRunner::class)
4242
class AllActivitiesScreenshotTest : ScreenshotTest() {
43-
init {
44-
setPhoneQualifiers()
45-
}
46-
4743
@ParameterizedRobolectricTestRunner.Parameter
4844
@JvmField
4945
var launcher: ActivityLaunchParam? = null

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

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ import com.github.takahirom.roborazzi.RobolectricDeviceQualifiers
2020
import com.github.takahirom.roborazzi.RoborazziOptions
2121
import com.github.takahirom.roborazzi.captureScreenRoboImage
2222
import com.github.takahirom.roborazzi.provideRoborazziContext
23+
import com.ichi2.anki.settings.Prefs
24+
import com.ichi2.anki.settings.enums.AppTheme
25+
import org.junit.Before
2326
import org.junit.experimental.categories.Category
2427
import org.robolectric.RuntimeEnvironment
2528
import org.robolectric.annotation.GraphicsMode
@@ -33,6 +36,31 @@ interface ScreenshotTestCategory
3336
@Category(ScreenshotTestCategory::class)
3437
@GraphicsMode(GraphicsMode.Mode.NATIVE)
3538
abstract class ScreenshotTest : RobolectricTest() {
39+
var fileNamePrefix = ""
40+
41+
@Before
42+
open fun applyGlobalConfig() {
43+
applyDeviceConfig()
44+
applyThemeConfig()
45+
}
46+
47+
protected open fun applyDeviceConfig() {
48+
if (System.getProperty("screenshot.device") == "tablet") {
49+
setTabletQualifiers()
50+
fileNamePrefix += "tablet_"
51+
} else {
52+
setPhoneQualifiers()
53+
}
54+
}
55+
56+
protected open fun applyThemeConfig() {
57+
if (System.getProperty("screenshot.theme") == "dark") {
58+
RuntimeEnvironment.setQualifiers("+night")
59+
Prefs.appTheme = AppTheme.NIGHT
60+
fileNamePrefix += "dark_"
61+
}
62+
}
63+
3664
/** Pixel-class phone in portrait, light theme. */
3765
protected fun setPhoneQualifiers() {
3866
RuntimeEnvironment.setQualifiers(RobolectricDeviceQualifiers.MediumPhone)
@@ -54,7 +82,8 @@ abstract class ScreenshotTest : RobolectricTest() {
5482
val classDir = "build/outputs/roborazzi/${this.javaClass.simpleName}"
5583
val diffDir = File("$classDir/diffs")
5684
// baseline is always in the root for the class, copied to /diffs/ if a change occurred
57-
val baseline = File("$classDir/$name.png")
85+
val fileName = "$fileNamePrefix$name.png"
86+
val baseline = File(classDir, fileName)
5887
captureScreenRoboImage(
5988
filePath = baseline.path,
6089
roborazziOptions = provideRoborazziContext().options.withCompareOutputDir(diffDir.path),

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

Lines changed: 8 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import com.ichi2.anki.settings.enums.ToolbarPosition
2424
import org.junit.Test
2525
import org.junit.runner.RunWith
2626
import org.robolectric.ParameterizedRobolectricTestRunner
27-
import org.robolectric.RuntimeEnvironment
2827

2928
@RunWith(ParameterizedRobolectricTestRunner::class)
3029
class StudyScreenScreenshotTest(
@@ -35,7 +34,6 @@ class StudyScreenScreenshotTest(
3534
Prefs.toolbarPosition = config.toolbarPosition
3635
Prefs.showAnswerButtons = config.showAnswerButtons
3736
Prefs.frameStyle = config.frameStyle
38-
RuntimeEnvironment.setQualifiers(config.qualifier.toString())
3937
}
4038

4139
@Test
@@ -64,27 +62,17 @@ class StudyScreenScreenshotTest(
6462
val styles = FrameStyle.entries
6563
// val styles = listOf(FrameStyle.BOX)
6664

67-
val devices =
68-
listOf(
69-
Qualifier("Phone", 411, 914, "420dpi"),
70-
Qualifier("Tablet", 1280, 800, "xhdpi"),
71-
)
72-
// val devices = listOf(Qualifier("Phone", 411, 914, "420dpi"))
73-
7465
val configs = mutableListOf<Array<TestConfig>>()
7566
for (pos in positions) {
7667
for (btn in buttonStates) {
7768
for (style in styles) {
78-
for (dev in devices) {
79-
val config =
80-
TestConfig(
81-
pos,
82-
btn,
83-
style,
84-
dev,
85-
)
86-
configs.add(arrayOf(config))
87-
}
69+
val config =
70+
TestConfig(
71+
pos,
72+
btn,
73+
style,
74+
)
75+
configs.add(arrayOf(config))
8876
}
8977
}
9078
}
@@ -100,23 +88,9 @@ class StudyScreenScreenshotTest(
10088
val toolbarPosition: ToolbarPosition,
10189
val showAnswerButtons: Boolean,
10290
val frameStyle: FrameStyle,
103-
val qualifier: Qualifier,
10491
) {
10592
override fun toString(): String =
106-
"${qualifier.name}_toolbar=${toolbarPosition.name}_" +
93+
"toolbar=${toolbarPosition.name}_" +
10794
"frameStyle=${frameStyle.name}_buttons=$showAnswerButtons"
10895
}
10996
}
110-
111-
data class Qualifier(
112-
val name: String,
113-
val widthDp: Int,
114-
val heightDp: Int,
115-
val dpi: String,
116-
val nightMode: Boolean = false,
117-
) {
118-
override fun toString(): String {
119-
val nightString = if (nightMode) "night" else "notnight"
120-
return "w${widthDp}dp-h${heightDp}dp-$nightString-$dpi"
121-
}
122-
}

0 commit comments

Comments
 (0)