Skip to content

Commit 03c5d9c

Browse files
soulcramerAhmed Elshahawi
andauthored
chore: reduce the amount of screenshots by component by grouping them into one (#927)
Co-authored-by: Ahmed Elshahawi <ahmd.shahawi@gmail.com>
1 parent ef56659 commit 03c5d9c

File tree

144 files changed

+2253
-1065
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

144 files changed

+2253
-1065
lines changed

gradle/libs.versions.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,9 @@ lint-api = { module = "com.android.tools.lint:lint-api", version.ref = "lint" }
104104
lint-checks = { module = "com.android.tools.lint:lint-checks", version.ref = "lint" }
105105
lint-tests = { module = "com.android.tools.lint:lint-tests", version.ref = "lint" }
106106

107+
# https://github.com/robolectric/robolectric
108+
robolectric = { module = "org.robolectric:robolectric", version = "4.11.1" }
109+
107110
slack-lint-compose = { module = "com.slack.lint.compose:compose-lint-checks", version.ref = "slackLint" }
108111

109112
# https://github.com/google/TestParameterInjector
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
3+
<!--
4+
Copyright (c) 2024 Adevinta
5+
6+
Permission is hereby granted, free of charge, to any person obtaining a copy
7+
of this software and associated documentation files (the "Software"), to deal
8+
in the Software without restriction, including without limitation the rights
9+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
copies of the Software, and to permit persons to whom the Software is
11+
furnished to do so, subject to the following conditions:
12+
13+
The above copyright notice and this permission notice shall be included in all
14+
copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
SOFTWARE.
23+
-->
24+
<resources xmlns:tools="http://schemas.android.com/tools" tools:locale="fr">
25+
<string name="spark_text_link_button_example">Tappe moi 👁️🫦👁️</string>
26+
<string name="spark_text_link_short_example"><annotation color="neutral" typography="display2">Découvre ce formidable language de programation qu’est Kotlin </annotation><annotation color="success" typography="display3"><u><b>https://kotlinlang.org</b></u></annotation></string>
27+
</resources>

spark-screenshot-testing/src/main/res/values/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,6 @@
2222
SOFTWARE.
2323
-->
2424
<resources>
25+
<string name="spark_text_link_button_example">Tap me 👁️🫦👁️</string>
2526
<string name="spark_text_link_short_example"><annotation color="neutral" typography="display2">Learn Kotlin Programming </annotation><annotation color="success" typography="display3"><u><b>https://kotlinlang.org</b></u></annotation></string>
2627
</resources>
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
/*
2+
* Copyright (c) 2023 Adevinta
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy
5+
* of this software and associated documentation files (the "Software"), to deal
6+
* in the Software without restriction, including without limitation the rights
7+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
* copies of the Software, and to permit persons to whom the Software is
9+
* furnished to do so, subject to the following conditions:
10+
*
11+
* The above copyright notice and this permission notice shall be included in all
12+
* copies or substantial portions of the Software.
13+
*
14+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20+
* SOFTWARE.
21+
*/
22+
package com.adevinta.spark
23+
24+
import app.cash.paparazzi.DeviceConfig
25+
import app.cash.paparazzi.Paparazzi
26+
import app.cash.paparazzi.RenderExtension
27+
import app.cash.paparazzi.detectEnvironment
28+
import com.android.ide.common.rendering.api.SessionParams
29+
import com.android.resources.Density
30+
import com.android.resources.Keyboard
31+
import com.android.resources.KeyboardState
32+
import com.android.resources.Navigation
33+
import com.android.resources.ScreenOrientation
34+
import com.android.resources.ScreenRatio
35+
import com.android.resources.ScreenSize
36+
import com.android.resources.TouchScreen
37+
38+
fun paparazziRule(
39+
renderingMode: SessionParams.RenderingMode = SessionParams.RenderingMode.SHRINK,
40+
deviceConfig: DeviceConfig = DefaultTestDevices.Phone,
41+
renderExtensions: Set<RenderExtension> = setOf(),
42+
) = Paparazzi(
43+
environment = patchedEnvironment(),
44+
deviceConfig = deviceConfig,
45+
renderingMode = renderingMode,
46+
renderExtensions = renderExtensions,
47+
maxPercentDifference = MaxPercentDifference,
48+
theme = PaparazziTheme,
49+
)
50+
51+
/**
52+
* Defaults devices configuration taken from NiA
53+
*/
54+
object DefaultTestDevices {
55+
/**
56+
* Pixel 6 Pro
57+
*/
58+
val Phone = DeviceConfig(
59+
screenHeight = 3120,
60+
screenWidth = 1440,
61+
xdpi = 512,
62+
ydpi = 512,
63+
orientation = ScreenOrientation.PORTRAIT,
64+
density = Density.DPI_560,
65+
ratio = ScreenRatio.LONG,
66+
size = ScreenSize.NORMAL,
67+
keyboard = Keyboard.NOKEY,
68+
touchScreen = TouchScreen.FINGER,
69+
keyboardState = KeyboardState.SOFT,
70+
softButtons = true,
71+
navigation = Navigation.NONAV,
72+
released = "October 28, 2021",
73+
locale = "en-rXA",
74+
)
75+
val Foldable = DeviceConfig(
76+
screenHeight = 2480,
77+
screenWidth = 2200,
78+
xdpi = 420,
79+
ydpi = 420,
80+
orientation = ScreenOrientation.PORTRAIT,
81+
density = Density.DPI_420,
82+
ratio = ScreenRatio.NOTLONG,
83+
size = ScreenSize.XLARGE,
84+
keyboard = Keyboard.NOKEY,
85+
touchScreen = TouchScreen.FINGER,
86+
keyboardState = KeyboardState.SOFT,
87+
softButtons = true,
88+
navigation = Navigation.NONAV,
89+
released = "October 31, 2013",
90+
locale = "en-rXA",
91+
)
92+
93+
/**
94+
* Pixel C
95+
*/
96+
val Tablet = DeviceConfig(
97+
screenHeight = 1800,
98+
screenWidth = 2560,
99+
xdpi = 308,
100+
ydpi = 308,
101+
orientation = ScreenOrientation.LANDSCAPE,
102+
density = Density.XHIGH,
103+
ratio = ScreenRatio.NOTLONG,
104+
size = ScreenSize.XLARGE,
105+
keyboard = Keyboard.QWERTY,
106+
touchScreen = TouchScreen.FINGER,
107+
keyboardState = KeyboardState.SOFT,
108+
softButtons = true,
109+
navigation = Navigation.NONAV,
110+
released = "December 8, 2015",
111+
locale = "en-rXA",
112+
)
113+
internal val devices = listOf(Phone, Foldable, Tablet)
114+
}
115+
116+
data class TestDeviceSpecs(val width: Int, val height: Int, val dpi: Int)
117+
118+
/**
119+
* Lower the current Paparazzi Environment from API level 34 to 33 to work around new resource conflicts:
120+
*
121+
* ```
122+
* SEVERE: resources.format: Hexadecimal color expected, found Color State List for @android:color/system_bar_background_semi_transparent
123+
* java.lang.NumberFormatException: Color value '/usr/local/lib/android/sdk/platforms/android-34/data/res/color/system_bar_background_semi_transparent.xml' has wrong size. Format is either#AARRGGBB, #RRGGBB, #RGB, or #ARGB
124+
* ```
125+
*
126+
* GitHub issue: https://github.com/cashapp/paparazzi/issues/1025
127+
*/
128+
internal fun patchedEnvironment() = with(detectEnvironment()) {
129+
copy(compileSdkVersion = 33, platformDir = platformDir.replace("34", "33"))
130+
}
131+
132+
internal const val MaxPercentDifference: Double = 0.01
133+
internal const val PaparazziTheme: String = "android:Theme.MaterialComponent.Light.NoActionBar"

spark-screenshot-testing/src/test/kotlin/com/adevinta/spark/PaparazziUtils.kt

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,22 @@ import androidx.compose.ui.Modifier
2929
import androidx.compose.ui.graphics.Color
3030
import androidx.compose.ui.platform.LocalInspectionMode
3131
import app.cash.paparazzi.Paparazzi
32-
import app.cash.paparazzi.detectEnvironment
32+
import com.adevinta.spark.tokens.darkSparkColors
33+
import com.adevinta.spark.tokens.lightSparkColors
34+
import com.adevinta.spark.tools.preview.ThemeVariant
3335

3436
internal fun Paparazzi.sparkSnapshot(
3537
name: String? = null,
3638
drawBackground: Boolean = true,
39+
isDark: Boolean = false,
3740
composable: @Composable () -> Unit,
3841
): Unit = snapshot(name) {
3942
// Behave like in Android Studio Preview renderer
4043
CompositionLocalProvider(LocalInspectionMode provides true) {
41-
SparkTheme(useLegacyStyle = false) {
44+
SparkTheme(
45+
useLegacyStyle = false,
46+
colors = if (isDark) darkSparkColors() else lightSparkColors(),
47+
) {
4248
// The first box acts as a shield from ComposeView which forces the first layout node
4349
// to match it's size. This allows the content below to wrap as needed.
4450
Box {
@@ -54,18 +60,31 @@ internal fun Paparazzi.sparkSnapshot(
5460
}
5561

5662
/**
57-
* Lower the current Paparazzi Environment from API level 34 to 33 to work around new resource conflicts:
58-
*
59-
* ```
60-
* SEVERE: resources.format: Hexadecimal color expected, found Color State List for @android:color/system_bar_background_semi_transparent
61-
* java.lang.NumberFormatException: Color value '/usr/local/lib/android/sdk/platforms/android-34/data/res/color/system_bar_background_semi_transparent.xml' has wrong size. Format is either#AARRGGBB, #RRGGBB, #RGB, or #ARGB
62-
* ```
63-
*
64-
* GitHub issue: https://github.com/cashapp/paparazzi/issues/1025
63+
* Generate 3 screenshots for each device: phone, tablet and foldable
6564
*/
66-
internal fun patchedEnvironment() = with(detectEnvironment()) {
67-
copy(compileSdkVersion = 33, platformDir = platformDir.replace("34", "33"))
65+
internal fun Paparazzi.sparkSnapshotDevices(
66+
name: String? = null,
67+
drawBackground: Boolean = true,
68+
isDark: Boolean = false,
69+
composable: @Composable () -> Unit,
70+
) {
71+
DefaultTestDevices.devices.forEach { deviceConfig ->
72+
unsafeUpdateConfig(
73+
deviceConfig = deviceConfig,
74+
)
75+
sparkSnapshot(name.orEmpty() + "_${deviceConfig.screenWidth}", drawBackground, isDark, composable)
76+
}
6877
}
6978

70-
internal const val MaxPercentDifference: Double = 0.01
71-
internal const val PaparazziTheme: String = "android:Theme.MaterialComponent.Light.NoActionBar"
79+
/**
80+
* Generate 2 screenshots for each theme: light and dark
81+
*/
82+
internal fun Paparazzi.sparkSnapshotNightMode(
83+
name: String? = null,
84+
drawBackground: Boolean = true,
85+
composable: @Composable () -> Unit,
86+
) {
87+
ThemeVariant.entries.forEach {
88+
sparkSnapshot(name.orEmpty() + "_${it.name}", drawBackground, it == ThemeVariant.Dark, composable)
89+
}
90+
}

spark-screenshot-testing/src/test/kotlin/com/adevinta/spark/PreviewScreenshotTests.kt

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,8 @@
2222
package com.adevinta.spark
2323

2424
import app.cash.paparazzi.DeviceConfig
25-
import app.cash.paparazzi.Paparazzi
2625
import com.airbnb.android.showkase.models.Showkase
2726
import com.airbnb.android.showkase.models.ShowkaseBrowserComponent
28-
import com.android.ide.common.rendering.api.SessionParams
2927
import org.junit.Rule
3028
import org.junit.Test
3129

@@ -43,13 +41,7 @@ internal class PreviewScreenshotTests {
4341
}
4442

4543
@get:Rule
46-
val paparazzi = Paparazzi(
47-
maxPercentDifference = MaxPercentDifference,
48-
theme = PaparazziTheme,
49-
renderingMode = SessionParams.RenderingMode.SHRINK,
50-
showSystemUi = false,
51-
environment = patchedEnvironment(),
52-
)
44+
val paparazzi = paparazziRule()
5345

5446
@Test
5547
fun preview_tests() {

0 commit comments

Comments
 (0)