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

Expose ShowkaseBrowserComponent (and color and typography) in PaparazziShowkaseTestPreview #320

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ public class MyScreenshotTest_PaparazziShowkaseTest : MyScreenshotTest() {
uiMode: PaparazziShowkaseUIMode,
): Unit {
paparazzi.unsafeUpdateConfig(config.deviceConfig.copy(softButtons = false))
takePaparazziSnapshot(paparazzi, elementPreview, direction, uiMode)
if (shouldTakeScreenshot(config.deviceConfig, elementPreview.group)) {
takePaparazziSnapshot(paparazzi, elementPreview, direction, uiMode)
}
}

private object PaparazziShowkasePreviewProvider : TestParameter.TestParameterValuesProvider {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ public class MyScreenshotTest_PaparazziShowkaseTest : MyScreenshotTest() {
uiMode: PaparazziShowkaseUIMode,
): Unit {
paparazzi.unsafeUpdateConfig(config.deviceConfig.copy(softButtons = false))
takePaparazziSnapshot(paparazzi, elementPreview, direction, uiMode)
if (shouldTakeScreenshot(config.deviceConfig, elementPreview.group)) {
takePaparazziSnapshot(paparazzi, elementPreview, direction, uiMode)
}
}

private object PaparazziShowkasePreviewProvider : TestParameter.TestParameterValuesProvider {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ public class MyPaparazziScreenshotTest_PaparazziShowkaseTest : MyPaparazziScreen
uiMode: PaparazziShowkaseUIMode,
): Unit {
paparazzi.unsafeUpdateConfig(config.deviceConfig.copy(softButtons = false))
takePaparazziSnapshot(paparazzi, elementPreview, direction, uiMode)
if (shouldTakeScreenshot(config.deviceConfig, elementPreview.group)) {
takePaparazziSnapshot(paparazzi, elementPreview, direction, uiMode)
}
}

private object PaparazziShowkasePreviewProvider : TestParameter.TestParameterValuesProvider {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,19 +198,24 @@ class PaparazziShowkaseScreenshotTestWriter(private val environment: XProcessing
)
.build()
)
.addCode(
.addStatement(
"%N.unsafeUpdateConfig(%N.deviceConfig.copy(softButtons = false))",
"paparazzi",
"config"
)
.addCode("\n")
.addCode(
.beginControlFlow(
"if (shouldTakeScreenshot(%N.deviceConfig, %N.group))",
"config",
"elementPreview"
)
.addStatement(
"takePaparazziSnapshot(%N, %N, %N, %N)",
"paparazzi",
"elementPreview",
"direction",
"uiMode"
)
.endControlFlow()
.build()
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.unit.dp
import com.airbnb.android.showkase.annotation.ShowkaseComposable
Expand Down Expand Up @@ -69,6 +70,16 @@ fun PreviewCustomButtonSmall() {
)
}

@ShowkaseComposable(name = "CustomButton", group = "Localised", styleName = "Small")
@Composable
fun PreviewCustomButtonLocalised() {
CustomButton(
text = stringResource(id = R.string.button),
onClick = { },
size = ButtonSize.Small
)
}


@Suppress("MatchingDeclarationName")
enum class ButtonSize {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="button">Knapp</string>
</resources>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="button">Button</string>
</resources>
Original file line number Diff line number Diff line change
@@ -1,9 +1,23 @@
package com.airbnb.android.showkase.screenshot.testing.paparazzi.sample

import app.cash.paparazzi.DeviceConfig
import com.airbnb.android.showkase.annotation.ShowkaseScreenshot
import com.airbnb.android.showkase.screenshot.testing.paparazzi.PaparazziShowkaseDeviceConfig
import com.airbnb.android.showkase.screenshot.testing.paparazzi.PaparazziShowkaseScreenshotTest

@ShowkaseScreenshot(rootShowkaseClass = PaparazziSampleRootModule::class)
abstract class MyPaparazziShowkaseScreenshotTest: PaparazziShowkaseScreenshotTest {
companion object: PaparazziShowkaseScreenshotTest.CompanionObject
abstract class MyPaparazziShowkaseScreenshotTest : PaparazziShowkaseScreenshotTest {
companion object : PaparazziShowkaseScreenshotTest.CompanionObject {
override fun deviceConfigs(): List<PaparazziShowkaseDeviceConfig> {
return listOf("en-rGB", "nb-rNO").map { locale ->
andersu marked this conversation as resolved.
Show resolved Hide resolved
PaparazziShowkaseDeviceConfig(
deviceConfig = DeviceConfig.PIXEL_5.copy(locale = locale)
)
}
}
}

override fun shouldTakeScreenshot(deviceConfig: DeviceConfig, group: String): Boolean {
return deviceConfig.locale == "en-rGB" || group == "Localised"
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,12 @@ interface PaparazziShowkaseScreenshotTest {
fun uiModes(): List<PaparazziShowkaseUIMode> = listOf(PaparazziShowkaseUIMode.DEFAULT)
}

/**
* Can be overridden to add logic to decide if a screenshot should be taken
* based on the device config and group.
*/
fun shouldTakeScreenshot(deviceConfig: DeviceConfig, group: String) : Boolean = true

fun takePaparazziSnapshot(
paparazzi: Paparazzi,
testPreview: PaparazziShowkaseTestPreview,
Expand Down Expand Up @@ -133,6 +139,8 @@ interface PaparazziShowkaseScreenshotTest {
}

interface PaparazziShowkaseTestPreview {
val group: String

@Composable
fun Content()
}
Expand All @@ -142,9 +150,11 @@ private const val DELIM = "**"
class ComponentPaparazziShowkaseTestPreview(
private val showkaseBrowserComponent: ShowkaseBrowserComponent,
) : PaparazziShowkaseTestPreview {
override val group: String = showkaseBrowserComponent.group

@Composable
override fun Content() = showkaseBrowserComponent.component()

override fun toString(): String =
"${showkaseBrowserComponent.group}${DELIM}${showkaseBrowserComponent.componentName}${DELIM}" +
"${showkaseBrowserComponent.styleName}"
Expand All @@ -153,6 +163,8 @@ class ComponentPaparazziShowkaseTestPreview(
class ColorPaparazziShowkaseTestPreview(
private val showkaseBrowserColor: ShowkaseBrowserColor
) : PaparazziShowkaseTestPreview {
override val group: String = showkaseBrowserColor.colorGroup

@Composable
override fun Content() {
Box(
Expand All @@ -170,6 +182,8 @@ class ColorPaparazziShowkaseTestPreview(
class TypographyPaparazziShowkaseTestPreview(
private val showkaseBrowserTypography: ShowkaseBrowserTypography
) : PaparazziShowkaseTestPreview {
override val group: String = showkaseBrowserTypography.typographyGroup

@Composable
override fun Content() {
BasicText(
Expand Down