diff --git a/paparazzi-annotations/build.gradle b/paparazzi-annotations/build.gradle index d809997126..d5acfb4b71 100644 --- a/paparazzi-annotations/build.gradle +++ b/paparazzi-annotations/build.gradle @@ -1,2 +1,7 @@ apply plugin: 'org.jetbrains.kotlin.jvm' +apply plugin: 'org.jetbrains.kotlin.plugin.compose' apply plugin: 'com.vanniktech.maven.publish' + +dependencies { + implementation libs.compose.runtime +} diff --git a/paparazzi-gradle-plugin/src/main/java/app/cash/paparazzi/gradle/PaparazziPlugin.kt b/paparazzi-gradle-plugin/src/main/java/app/cash/paparazzi/gradle/PaparazziPlugin.kt index 4d2a190763..57d4f60b5f 100644 --- a/paparazzi-gradle-plugin/src/main/java/app/cash/paparazzi/gradle/PaparazziPlugin.kt +++ b/paparazzi-gradle-plugin/src/main/java/app/cash/paparazzi/gradle/PaparazziPlugin.kt @@ -60,6 +60,7 @@ public class PaparazziPlugin @Inject constructor( private val buildOperationRunner: BuildOperationRunner, private val buildOperationExecutor: BuildOperationExecutor ) : Plugin { + override fun apply(project: Project) { val supportedPlugins = listOf("com.android.application", "com.android.library", "com.android.dynamic-feature") project.afterEvaluate { diff --git a/paparazzi-preview-runtime/api/paparazzi-preview-runtime.api b/paparazzi-preview-runtime/api/paparazzi-preview-runtime.api index ea4295b5ac..778d7c4826 100644 --- a/paparazzi-preview-runtime/api/paparazzi-preview-runtime.api +++ b/paparazzi-preview-runtime/api/paparazzi-preview-runtime.api @@ -1,10 +1,13 @@ -public final class app/cash/paparazzi/preview/runtime/PaparazziPreviewData { +public abstract interface class app/cash/paparazzi/preview/runtime/PaparazziPreviewData { +} + +public final class app/cash/paparazzi/preview/runtime/PaparazziPreviewData$Default : app/cash/paparazzi/preview/runtime/PaparazziPreviewData { public static final field $stable I public fun (Ljava/lang/String;Lkotlin/jvm/functions/Function2;)V public final fun component1 ()Ljava/lang/String; public final fun component2 ()Lkotlin/jvm/functions/Function2; - public final fun copy (Ljava/lang/String;Lkotlin/jvm/functions/Function2;)Lapp/cash/paparazzi/preview/runtime/PaparazziPreviewData; - public static synthetic fun copy$default (Lapp/cash/paparazzi/preview/runtime/PaparazziPreviewData;Ljava/lang/String;Lkotlin/jvm/functions/Function2;ILjava/lang/Object;)Lapp/cash/paparazzi/preview/runtime/PaparazziPreviewData; + public final fun copy (Ljava/lang/String;Lkotlin/jvm/functions/Function2;)Lapp/cash/paparazzi/preview/runtime/PaparazziPreviewData$Default; + public static synthetic fun copy$default (Lapp/cash/paparazzi/preview/runtime/PaparazziPreviewData$Default;Ljava/lang/String;Lkotlin/jvm/functions/Function2;ILjava/lang/Object;)Lapp/cash/paparazzi/preview/runtime/PaparazziPreviewData$Default; public fun equals (Ljava/lang/Object;)Z public final fun getComposable ()Lkotlin/jvm/functions/Function2; public final fun getSnapshotName ()Ljava/lang/String; diff --git a/paparazzi-preview-runtime/src/main/java/app/cash/paparazzi/preview/runtime/PaparazziPreviewData.kt b/paparazzi-preview-runtime/src/main/java/app/cash/paparazzi/preview/runtime/PaparazziPreviewData.kt index 9246724f1b..73724cc0fd 100644 --- a/paparazzi-preview-runtime/src/main/java/app/cash/paparazzi/preview/runtime/PaparazziPreviewData.kt +++ b/paparazzi-preview-runtime/src/main/java/app/cash/paparazzi/preview/runtime/PaparazziPreviewData.kt @@ -2,7 +2,17 @@ package app.cash.paparazzi.preview.runtime import androidx.compose.runtime.Composable -public data class PaparazziPreviewData( - val snapshotName: String, - val composable: @Composable () -> Unit -) +/** + * Represents composables annotated with @Paparazzi annotation + * + * Default - Represents a composable with no parameters + */ +public sealed interface PaparazziPreviewData { + + public data class Default( + val snapshotName: String, + val composable: @Composable () -> Unit + ) : PaparazziPreviewData { + override fun toString(): String = snapshotName + } +} diff --git a/paparazzi-preview-test-junit/api/paparazzi-preview-test-junit.api b/paparazzi-preview-test-junit/api/paparazzi-preview-test-junit.api new file mode 100644 index 0000000000..4e37a72bd3 --- /dev/null +++ b/paparazzi-preview-test-junit/api/paparazzi-preview-test-junit.api @@ -0,0 +1,11 @@ +public class app/cash/paparazzi/preview/PaparazziValuesProvider : com/google/testing/junit/testparameterinjector/TestParameterValuesProvider { + public static final field $stable I + public fun (Ljava/util/List;)V + protected fun provideValues (Lcom/google/testing/junit/testparameterinjector/TestParameterValuesProvider$Context;)Ljava/util/List; +} + +public final class app/cash/paparazzi/preview/SnapshotKt { + public static final fun snapshot (Lapp/cash/paparazzi/Paparazzi;Lapp/cash/paparazzi/preview/runtime/PaparazziPreviewData;Ljava/lang/String;)V + public static synthetic fun snapshot$default (Lapp/cash/paparazzi/Paparazzi;Lapp/cash/paparazzi/preview/runtime/PaparazziPreviewData;Ljava/lang/String;ILjava/lang/Object;)V +} + diff --git a/paparazzi-preview-test-junit/build.gradle b/paparazzi-preview-test-junit/build.gradle new file mode 100644 index 0000000000..4abda1a212 --- /dev/null +++ b/paparazzi-preview-test-junit/build.gradle @@ -0,0 +1,10 @@ +apply plugin: 'org.jetbrains.kotlin.jvm' +apply plugin: 'org.jetbrains.kotlin.plugin.compose' +apply plugin: 'com.vanniktech.maven.publish' + +dependencies { + compileOnly libs.compose.runtime + implementation projects.paparazzi + implementation projects.paparazziPreviewRuntime + api libs.testParameterInjector +} diff --git a/paparazzi-preview-test-junit/gradle.properties b/paparazzi-preview-test-junit/gradle.properties new file mode 100644 index 0000000000..389476211e --- /dev/null +++ b/paparazzi-preview-test-junit/gradle.properties @@ -0,0 +1,4 @@ +POM_ARTIFACT_ID=paparazzi-preview-test-junit +POM_NAME=Paparazzi Preview Test JUnit +POM_DESCRIPTION=Adds helpers to use in code generated from Paparazzi Gradle Plugin for composables annotated with @Paparazzi +POM_PACKAGING=jar diff --git a/paparazzi-preview-test-junit/src/main/java/app/cash/paparazzi/preview/Snapshot.kt b/paparazzi-preview-test-junit/src/main/java/app/cash/paparazzi/preview/Snapshot.kt new file mode 100644 index 0000000000..1ebea5dba1 --- /dev/null +++ b/paparazzi-preview-test-junit/src/main/java/app/cash/paparazzi/preview/Snapshot.kt @@ -0,0 +1,29 @@ +// Copyright Square, Inc. +package app.cash.paparazzi.preview + +import app.cash.paparazzi.Paparazzi +import app.cash.paparazzi.preview.runtime.PaparazziPreviewData +import com.google.testing.junit.testparameterinjector.TestParameterValuesProvider + +/** + * Take a snapshot of the given [previewData]. + */ +public fun Paparazzi.snapshot(previewData: PaparazziPreviewData, name: String? = null) { + when (previewData) { + is PaparazziPreviewData.Default -> snapshotDefault(previewData, name) + } +} + +/** + * A `@TestParameter` values provider for the given [annotations]. + * + * Example usage: + * ``` + * private class ValuesProvider : PaparazziValuesProvider(paparazziAnnotations) + * ``` + */ +public open class PaparazziValuesProvider( + private val annotations: List +) : TestParameterValuesProvider() { + override fun provideValues(context: Context?): MutableList<*> = annotations.toMutableList() +} diff --git a/paparazzi-preview-test-junit/src/main/java/app/cash/paparazzi/preview/Utils.kt b/paparazzi-preview-test-junit/src/main/java/app/cash/paparazzi/preview/Utils.kt new file mode 100644 index 0000000000..74807c92d6 --- /dev/null +++ b/paparazzi-preview-test-junit/src/main/java/app/cash/paparazzi/preview/Utils.kt @@ -0,0 +1,11 @@ +// Copyright Square, Inc. +package app.cash.paparazzi.preview + +import app.cash.paparazzi.Paparazzi +import app.cash.paparazzi.preview.runtime.PaparazziPreviewData + +internal fun Paparazzi.snapshotDefault(previewData: PaparazziPreviewData.Default, name: String?) { + snapshot(name) { + previewData.composable() + } +} diff --git a/settings.gradle b/settings.gradle index 801e643f68..b694e0bf0b 100644 --- a/settings.gradle +++ b/settings.gradle @@ -22,6 +22,7 @@ include ':paparazzi-gradle-plugin' include ':paparazzi-preview-lints' include ':paparazzi-preview-processor' include ':paparazzi-preview-runtime' +include ':paparazzi-preview-test-junit' include ':sample'