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

chore: reduce the amount of screenshots by component by grouping them into one #927

Merged
merged 4 commits into from
Feb 5, 2024
Merged
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
3 changes: 3 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@ lint-api = { module = "com.android.tools.lint:lint-api", version.ref = "lint" }
lint-checks = { module = "com.android.tools.lint:lint-checks", version.ref = "lint" }
lint-tests = { module = "com.android.tools.lint:lint-tests", version.ref = "lint" }

# https://github.com/robolectric/robolectric
robolectric = { module = "org.robolectric:robolectric", version = "4.11.1" }

slack-lint-compose = { module = "com.slack.lint.compose:compose-lint-checks", version.ref = "slackLint" }

# https://github.com/google/TestParameterInjector
Expand Down
27 changes: 27 additions & 0 deletions spark-screenshot-testing/src/main/res/values-fr/strings.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="utf-8"?>

<!--
Copyright (c) 2024 Adevinta

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
-->
<resources xmlns:tools="http://schemas.android.com/tools" tools:locale="fr">
<string name="spark_text_link_button_example">Tappe moi πŸ‘οΈπŸ«¦πŸ‘οΈ</string>
<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>
</resources>
1 change: 1 addition & 0 deletions spark-screenshot-testing/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,6 @@
SOFTWARE.
-->
<resources>
<string name="spark_text_link_button_example">Tap me πŸ‘οΈπŸ«¦πŸ‘οΈ</string>
<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>
</resources>
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
/*
* Copyright (c) 2023 Adevinta
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package com.adevinta.spark

import app.cash.paparazzi.DeviceConfig
import app.cash.paparazzi.Paparazzi
import app.cash.paparazzi.RenderExtension
import app.cash.paparazzi.detectEnvironment
import com.android.ide.common.rendering.api.SessionParams
import com.android.resources.Density
import com.android.resources.Keyboard
import com.android.resources.KeyboardState
import com.android.resources.Navigation
import com.android.resources.ScreenOrientation
import com.android.resources.ScreenRatio
import com.android.resources.ScreenSize
import com.android.resources.TouchScreen

fun paparazziRule(
renderingMode: SessionParams.RenderingMode = SessionParams.RenderingMode.SHRINK,
deviceConfig: DeviceConfig = DefaultTestDevices.Phone,
renderExtensions: Set<RenderExtension> = setOf(),
) = Paparazzi(
environment = patchedEnvironment(),
deviceConfig = deviceConfig,
renderingMode = renderingMode,
renderExtensions = renderExtensions,
maxPercentDifference = MaxPercentDifference,
theme = PaparazziTheme,
)

/**
* Defaults devices configuration taken from NiA
*/
object DefaultTestDevices {
/**
* Pixel 6 Pro
*/
val Phone = DeviceConfig(
screenHeight = 3120,
screenWidth = 1440,
xdpi = 512,
ydpi = 512,
orientation = ScreenOrientation.PORTRAIT,
density = Density.DPI_560,
ratio = ScreenRatio.LONG,
size = ScreenSize.NORMAL,
keyboard = Keyboard.NOKEY,
touchScreen = TouchScreen.FINGER,
keyboardState = KeyboardState.SOFT,
softButtons = true,
navigation = Navigation.NONAV,
released = "October 28, 2021",
locale = "en-rXA",
)
val Foldable = DeviceConfig(
screenHeight = 2480,
screenWidth = 2200,
xdpi = 420,
ydpi = 420,
orientation = ScreenOrientation.PORTRAIT,
density = Density.DPI_420,
ratio = ScreenRatio.NOTLONG,
size = ScreenSize.XLARGE,
keyboard = Keyboard.NOKEY,
touchScreen = TouchScreen.FINGER,
keyboardState = KeyboardState.SOFT,
softButtons = true,
navigation = Navigation.NONAV,
released = "October 31, 2013",
locale = "en-rXA",
)

/**
* Pixel C
*/
val Tablet = DeviceConfig(
screenHeight = 1800,
screenWidth = 2560,
xdpi = 308,
ydpi = 308,
orientation = ScreenOrientation.LANDSCAPE,
density = Density.XHIGH,
ratio = ScreenRatio.NOTLONG,
size = ScreenSize.XLARGE,
keyboard = Keyboard.QWERTY,
touchScreen = TouchScreen.FINGER,
keyboardState = KeyboardState.SOFT,
softButtons = true,
navigation = Navigation.NONAV,
released = "December 8, 2015",
locale = "en-rXA",
)
internal val devices = listOf(Phone, Foldable, Tablet)
}

data class TestDeviceSpecs(val width: Int, val height: Int, val dpi: Int)

/**
* Lower the current Paparazzi Environment from API level 34 to 33 to work around new resource conflicts:
*
* ```
* SEVERE: resources.format: Hexadecimal color expected, found Color State List for @android:color/system_bar_background_semi_transparent
* 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
* ```
*
* GitHub issue: https://github.com/cashapp/paparazzi/issues/1025
*/
internal fun patchedEnvironment() = with(detectEnvironment()) {
copy(compileSdkVersion = 33, platformDir = platformDir.replace("34", "33"))
}

internal const val MaxPercentDifference: Double = 0.01
internal const val PaparazziTheme: String = "android:Theme.MaterialComponent.Light.NoActionBar"
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,22 @@ import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalInspectionMode
import app.cash.paparazzi.Paparazzi
import app.cash.paparazzi.detectEnvironment
import com.adevinta.spark.tokens.darkSparkColors
import com.adevinta.spark.tokens.lightSparkColors
import com.adevinta.spark.tools.preview.ThemeVariant

internal fun Paparazzi.sparkSnapshot(
name: String? = null,
drawBackground: Boolean = true,
isDark: Boolean = false,
composable: @Composable () -> Unit,
): Unit = snapshot(name) {
// Behave like in Android Studio Preview renderer
CompositionLocalProvider(LocalInspectionMode provides true) {
SparkTheme(useLegacyStyle = false) {
SparkTheme(
useLegacyStyle = false,
colors = if (isDark) darkSparkColors() else lightSparkColors(),
) {
// The first box acts as a shield from ComposeView which forces the first layout node
// to match it's size. This allows the content below to wrap as needed.
Box {
Expand All @@ -54,18 +60,31 @@ internal fun Paparazzi.sparkSnapshot(
}

/**
* Lower the current Paparazzi Environment from API level 34 to 33 to work around new resource conflicts:
*
* ```
* SEVERE: resources.format: Hexadecimal color expected, found Color State List for @android:color/system_bar_background_semi_transparent
* 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
* ```
*
* GitHub issue: https://github.com/cashapp/paparazzi/issues/1025
* Generate 3 screenshots for each device: phone, tablet and foldable
*/
internal fun patchedEnvironment() = with(detectEnvironment()) {
copy(compileSdkVersion = 33, platformDir = platformDir.replace("34", "33"))
internal fun Paparazzi.sparkSnapshotDevices(
name: String? = null,
drawBackground: Boolean = true,
isDark: Boolean = false,
composable: @Composable () -> Unit,
) {
DefaultTestDevices.devices.forEach { deviceConfig ->
unsafeUpdateConfig(
deviceConfig = deviceConfig,
)
sparkSnapshot(name.orEmpty() + "_${deviceConfig.screenWidth}", drawBackground, isDark, composable)
}
}

internal const val MaxPercentDifference: Double = 0.01
internal const val PaparazziTheme: String = "android:Theme.MaterialComponent.Light.NoActionBar"
/**
* Generate 2 screenshots for each theme: light and dark
*/
internal fun Paparazzi.sparkSnapshotNightMode(
name: String? = null,
drawBackground: Boolean = true,
composable: @Composable () -> Unit,
) {
ThemeVariant.entries.forEach {
sparkSnapshot(name.orEmpty() + "_${it.name}", drawBackground, it == ThemeVariant.Dark, composable)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,8 @@
package com.adevinta.spark

import app.cash.paparazzi.DeviceConfig
import app.cash.paparazzi.Paparazzi
import com.airbnb.android.showkase.models.Showkase
import com.airbnb.android.showkase.models.ShowkaseBrowserComponent
import com.android.ide.common.rendering.api.SessionParams
import org.junit.Rule
import org.junit.Test

Expand All @@ -43,13 +41,7 @@ internal class PreviewScreenshotTests {
}

@get:Rule
val paparazzi = Paparazzi(
maxPercentDifference = MaxPercentDifference,
theme = PaparazziTheme,
renderingMode = SessionParams.RenderingMode.SHRINK,
showSystemUi = false,
environment = patchedEnvironment(),
)
val paparazzi = paparazziRule()

@Test
fun preview_tests() {
Expand Down
Loading
Loading