Skip to content

Commit

Permalink
Cleanup preview param
Browse files Browse the repository at this point in the history
  • Loading branch information
geoff-powell committed Aug 26, 2024
1 parent d7be11c commit 67418ba
Show file tree
Hide file tree
Showing 7 changed files with 6 additions and 336 deletions.
9 changes: 0 additions & 9 deletions buildSrc/build.gradle

This file was deleted.

9 changes: 0 additions & 9 deletions buildSrc/settings.gradle

This file was deleted.

114 changes: 0 additions & 114 deletions gradle/aarAsJar.gradle

This file was deleted.

2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ bytebuddy-agent = { module = "net.bytebuddy:byte-buddy-agent", version.ref = "by
bytebuddy-core = { module = "net.bytebuddy:byte-buddy", version.ref = "bytebuddy" }

compose-runtime = { module = "androidx.compose.runtime:runtime", version.ref = "compose" }
composeUi-foundation = { module = "androidx.compose.foundation:foundation" }
composeUi-foundation = { module = "androidx.compose.foundation:foundation", version.ref = "compose" }
composeUi-material = { module = "androidx.compose.material:material", version.ref = "compose" }
composeUi-uiTooling = { module = "androidx.compose.ui:ui-tooling" }

Expand Down
17 changes: 2 additions & 15 deletions paparazzi/api/paparazzi.api
Original file line number Diff line number Diff line change
Expand Up @@ -245,17 +245,8 @@ public final class app/cash/paparazzi/preview/ComposableSingletons$SnapshotKt {
public final class app/cash/paparazzi/preview/ComposableSingletons$UtilsKt {
public static final field INSTANCE Lapp/cash/paparazzi/preview/ComposableSingletons$UtilsKt;
public static field lambda-1 Lkotlin/jvm/functions/Function3;
public static field lambda-2 Lkotlin/jvm/functions/Function3;
public fun <init> ()V
public final fun getLambda-1$paparazzi ()Lkotlin/jvm/functions/Function3;
public final fun getLambda-2$paparazzi ()Lkotlin/jvm/functions/Function3;
}

public final class app/cash/paparazzi/preview/DefaultLocaleRule : org/junit/rules/TestRule {
public static final field $stable I
public fun <init> (Ljava/lang/String;)V
public fun apply (Lorg/junit/runners/model/Statement;Lorg/junit/runner/Description;)Lorg/junit/runners/model/Statement;
public final fun getLocale ()Ljava/lang/String;
}

public class app/cash/paparazzi/preview/PaparazziValuesProvider : com/google/testing/junit/testparameterinjector/TestParameter$TestParameterValuesProvider {
Expand All @@ -265,11 +256,7 @@ public class app/cash/paparazzi/preview/PaparazziValuesProvider : com/google/tes
}

public final class app/cash/paparazzi/preview/SnapshotKt {
public static final fun deviceConfig (Lapp/cash/paparazzi/annotations/PaparazziPreviewData;Lapp/cash/paparazzi/DeviceConfig;)Lapp/cash/paparazzi/DeviceConfig;
public static synthetic fun deviceConfig$default (Lapp/cash/paparazzi/annotations/PaparazziPreviewData;Lapp/cash/paparazzi/DeviceConfig;ILjava/lang/Object;)Lapp/cash/paparazzi/DeviceConfig;
public static final fun flatten (Ljava/util/List;)Ljava/util/List;
public static final fun locale (Lapp/cash/paparazzi/annotations/PaparazziPreviewData;)Ljava/lang/String;
public static final fun snapshot (Lapp/cash/paparazzi/Paparazzi;Lapp/cash/paparazzi/annotations/PaparazziPreviewData;Ljava/lang/String;ZLkotlin/jvm/functions/Function3;)V
public static synthetic fun snapshot$default (Lapp/cash/paparazzi/Paparazzi;Lapp/cash/paparazzi/annotations/PaparazziPreviewData;Ljava/lang/String;ZLkotlin/jvm/functions/Function3;ILjava/lang/Object;)V
public static final fun snapshot (Lapp/cash/paparazzi/Paparazzi;Lapp/cash/paparazzi/annotations/PaparazziPreviewData;Ljava/lang/String;Lkotlin/jvm/functions/Function3;)V
public static synthetic fun snapshot$default (Lapp/cash/paparazzi/Paparazzi;Lapp/cash/paparazzi/annotations/PaparazziPreviewData;Ljava/lang/String;Lkotlin/jvm/functions/Function3;ILjava/lang/Object;)V
}

72 changes: 2 additions & 70 deletions paparazzi/src/main/java/app/cash/paparazzi/preview/Snapshot.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,70 +2,25 @@
package app.cash.paparazzi.preview

import androidx.compose.runtime.Composable
import app.cash.paparazzi.DeviceConfig
import app.cash.paparazzi.Paparazzi
import app.cash.paparazzi.annotations.PaparazziPreviewData
import com.google.testing.junit.testparameterinjector.TestParameter.TestParameterValuesProvider
import org.junit.rules.TestRule
import org.junit.runner.Description
import org.junit.runners.model.Statement
import java.util.Locale

/**
* Take a snapshot of the given [previewData].
*/
public fun Paparazzi.snapshot(
previewData: PaparazziPreviewData,
name: String? = null,
localInspectionMode: Boolean = true,
wrapper: @Composable (@Composable () -> Unit) -> Unit = { it() }
) {
when (previewData) {
is PaparazziPreviewData.Default -> snapshotDefault(previewData, name, localInspectionMode, wrapper)
is PaparazziPreviewData.Provider<*> -> snapshotProvider(previewData, name, localInspectionMode, wrapper)
is PaparazziPreviewData.Default -> snapshotDefault(previewData, name, wrapper)
is PaparazziPreviewData.Empty -> Unit
is PaparazziPreviewData.Error -> throw Exception(previewData.message)
}
}

/**
* Generate a Paparazzi DeviceConfig for the given preview
* using the given [default] DeviceConfig.
*
* default: The IDE renders a preview with a higher resolution than
* the default device set by Paparazzi (which is currently Nexus 5). Defaulting to
* a larger device brings the previews and snapshots closer in parity.
*/
public fun PaparazziPreviewData.deviceConfig(
default: DeviceConfig = DeviceConfig.PIXEL_5
): DeviceConfig = when (this) {
is PaparazziPreviewData.Default -> preview.deviceConfig(default)
is PaparazziPreviewData.Provider<*> -> preview.deviceConfig(default)
else -> default
}

/**
* Returns a locale for the given preview, or null if error or empty.
*/
public fun PaparazziPreviewData.locale(): String? = when (this) {
is PaparazziPreviewData.Default -> preview.locale
is PaparazziPreviewData.Provider<*> -> preview.locale
else -> null
}

/**
* Convert a list of generated [PaparazziPreviewData]
* to a flat list of [PaparazziPreviewData]s.
*/
public fun List<PaparazziPreviewData>.flatten(): List<PaparazziPreviewData> = flatMap {
when (it) {
is PaparazziPreviewData.Provider<*> -> List(it.previewParameter.values.count()) { i ->
it.withPreviewParameterIndex(i)
}
else -> listOf(it)
}
}

/**
* A `@TestParameter` values provider for the given [annotations].
*
Expand All @@ -77,28 +32,5 @@ public fun List<PaparazziPreviewData>.flatten(): List<PaparazziPreviewData> = fl
public open class PaparazziValuesProvider(
private val annotations: List<PaparazziPreviewData>
) : TestParameterValuesProvider {
override fun provideValues(): List<PaparazziPreviewData> = annotations.flatten()
}

/**
* Enforce a particular default locale for a test. Resets back to default on completion.
*/
public class DefaultLocaleRule(public val locale: String?) : TestRule {
override fun apply(
base: Statement,
description: Description
): Statement {
return object : Statement() {
override fun evaluate() {
val default = Locale.getDefault()

try {
locale?.let { Locale.setDefault(Locale.forLanguageTag(it)) }
base.evaluate()
} finally {
Locale.setDefault(default)
}
}
}
}
override fun provideValues(): List<PaparazziPreviewData> = annotations
}
Loading

0 comments on commit 67418ba

Please sign in to comment.