Skip to content

Commit

Permalink
Add DroidKaigiKmpPreviewTester
Browse files Browse the repository at this point in the history
  • Loading branch information
takahirom committed Jul 19, 2024
1 parent a7b71e9 commit 1b0818c
Show file tree
Hide file tree
Showing 12 changed files with 58 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,6 @@ class AndroidRoborazziPlugin : Plugin<Project> {
}
}
}
project.extensions.getByType<RoborazziExtension>().apply {
generateComposePreviewRobolectricTests {
enable.set(true)
packages.add("io.github.droidkaigi.confsched")
}
}
dependencies {
testImplementation(libs.library("androidxTestEspressoEspressoCore"))
testImplementation(libs.library("junit"))
Expand All @@ -50,6 +44,7 @@ class AndroidRoborazziPlugin : Plugin<Project> {
testImplementation(libs.library("roborazzi"))
testImplementation(libs.library("roborazziCompose"))
testImplementation(libs.library("composablePreviewScanner"))
testImplementation(libs.library("composablePreviewScannerJvm"))
testImplementation(libs.library("roborazziPreviewScannerSupport"))
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ package io.github.droidkaigi.confsched.primitive
import io.github.takahirom.roborazzi.RoborazziExtension
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.internal.artifacts.dependencies.DefaultExternalModuleDependency
import org.gradle.api.tasks.testing.logging.TestExceptionFormat.FULL
import org.gradle.api.tasks.testing.logging.TestLogEvent.FAILED
import org.gradle.api.tasks.testing.logging.TestLogEvent.PASSED
import org.gradle.api.tasks.testing.logging.TestLogEvent.SKIPPED
import org.gradle.kotlin.dsl.get
import org.gradle.kotlin.dsl.getByType
import kotlin.collections.listOf
import kotlin.collections.set

@Suppress("unused")
class KmpRoborazziPlugin : Plugin<Project> {
Expand All @@ -26,7 +26,8 @@ class KmpRoborazziPlugin : Plugin<Project> {
all { test ->
test.jvmArgs("-noverify")
test.systemProperties["robolectric.graphicsMode"] = "NATIVE"
test.systemProperties["robolectric.pixelCopyRenderMode"] = "hardware"
test.systemProperties["robolectric.pixelCopyRenderMode"] =
"hardware"

test.maxParallelForks = Runtime.getRuntime().availableProcessors()
test.testLogging {
Expand All @@ -43,19 +44,21 @@ class KmpRoborazziPlugin : Plugin<Project> {
project.extensions.getByType<RoborazziExtension>().apply {
generateComposePreviewRobolectricTests {
enable.set(true)
packages.add("io.github.droidkaigi.confsched")
testerQualifiedClassName.set("io.github.droidkaigi.confsched.testing.DroidKaigiKmpPreviewTester")
}
}
kotlin {
if (plugins.hasPlugin("com.android.library")) {
sourceSets.getByName("androidUnitTest") {
dependencies {
implementation(project(":core:testing"))
implementation(libs.library("androidxTestEspressoEspressoCore"))
implementation(libs.library("junit"))
implementation(libs.library("robolectric"))
implementation(libs.library("androidxTestExtJunit"))
implementation(libs.library("roborazzi"))
implementation(libs.library("roborazziCompose"))
implementation(libs.library("composablePreviewScannerJvm"))
implementation(libs.library("composablePreviewScanner"))
implementation(libs.library("roborazziPreviewScannerSupport"))
}
Expand Down
2 changes: 2 additions & 0 deletions core/designsystem/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ plugins {
id("droidkaigi.primitive.kmp.ios")
id("droidkaigi.primitive.kmp.compose")
id("droidkaigi.primitive.kmp.android.hilt")
id("droidkaigi.primitive.kmp.roborazzi")
id("droidkaigi.primitive.detekt")
}

android.namespace = "io.github.droidkaigi.confsched.core.designsystem"
roborazzi.generateComposePreviewRobolectricTests.packages = listOf("io.github.droidkaigi.confsched.designsystem")

kotlin {
sourceSets {
Expand Down
5 changes: 5 additions & 0 deletions core/testing/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,12 @@ dependencies {
api(projects.core.testingManifest)
api(libs.composeNavigation)
api(libs.roborazziRule)
api(libs.roborazziCompose)
api(libs.robolectric)
api(libs.composeUiTestJunit4)
implementation(libs.composeMaterialWindowSize)
implementation(libs.composablePreviewScanner)
implementation(libs.composablePreviewScannerJvm)
implementation(libs.roborazziPreviewScannerSupport)
implementation(libs.kotlinReflect)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package io.github.droidkaigi.confsched.testing

import com.github.takahirom.roborazzi.ComposePreviewTester
import com.github.takahirom.roborazzi.captureRoboImage
import sergio.sastre.composable.preview.scanner.core.preview.ComposablePreview
import sergio.sastre.composable.preview.scanner.jvm.JvmAnnotationScanner
import kotlin.reflect.jvm.kotlinFunction

class DroidKaigiKmpPreviewTester : ComposePreviewTester<JvmAnnotationScanner.DesktopPreviewInfo> {
override fun previews(vararg packages: String): List<ComposablePreview<JvmAnnotationScanner.DesktopPreviewInfo>> {
return JvmAnnotationScanner("org.jetbrains.compose.ui.tooling.preview.Preview")
.scanPackageTrees(*packages)
.getPreviews()
.filter { preview ->
System.getProperties()
Class.forName(preview.declaringClass).declaredMethods.first { it.name == preview.methodName }.kotlinFunction?.visibility == kotlin.reflect.KVisibility.PUBLIC

}
}

override fun test(preview: ComposablePreview<JvmAnnotationScanner.DesktopPreviewInfo>) {
captureRoboImage {
preview()
}
}
}
1 change: 1 addition & 0 deletions feature/contributors/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ plugins {
}

android.namespace = "io.github.droidkaigi.confsched.feature.contributors"
roborazzi.generateComposePreviewRobolectricTests.packages = listOf("io.github.droidkaigi.confsched.contributors")
kotlin {
sourceSets {
commonMain {
Expand Down
3 changes: 2 additions & 1 deletion feature/eventmap/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ plugins {
id("droidkaigi.convention.kmpfeature")
}

android.namespace = "io.github.droidkaigi.confsched.feature.eventMap"
android.namespace = "io.github.droidkaigi.confsched.feature.eventmap"
roborazzi.generateComposePreviewRobolectricTests.packages = listOf("io.github.droidkaigi.confsched.eventmap")
kotlin {
sourceSets {
commonMain {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ import io.github.droidkaigi.confsched.model.EventMapEvent
import io.github.droidkaigi.confsched.ui.SnackbarMessageEffect
import io.github.droidkaigi.confsched.ui.UserMessageStateHolder
import io.github.droidkaigi.confsched.ui.handleOnClickIfNotNavigating
import io.github.droidkaigi.confsched.ui.rememberUserMessageStateHolder
import kotlinx.collections.immutable.PersistentList
import kotlinx.collections.immutable.persistentListOf
import org.jetbrains.compose.ui.tooling.preview.Preview

const val eventMapScreenRoute = "eventMap"
Expand Down Expand Up @@ -193,7 +195,10 @@ private fun EventMap(
@Preview
fun PreviewEventMapScreen() {
EventMapScreen(
onNavigationIconClick = { },
onEventMapItemClick = { },
uiState = EventMapUiState(persistentListOf(), rememberUserMessageStateHolder()),
snackbarHostState = SnackbarHostState(),
isTopAppBarHidden = false,
onBackClick = {},
onEventMapItemClick = {},
)
}
1 change: 1 addition & 0 deletions feature/main/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ plugins {
}

android.namespace = "io.github.droidkaigi.confsched.feature.main"
roborazzi.generateComposePreviewRobolectricTests.packages = listOf("io.github.droidkaigi.confsched.main")

kotlin {
sourceSets {
Expand Down
1 change: 1 addition & 0 deletions feature/profilecard/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ plugins {
}

android.namespace = "io.github.droidkaigi.confsched.feature.profilecard"
roborazzi.generateComposePreviewRobolectricTests.packages = listOf("io.github.droidkaigi.confsched.profilecard")
kotlin {
sourceSets {
commonMain {
Expand Down
2 changes: 2 additions & 0 deletions feature/sessions/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ plugins {
}

android.namespace = "io.github.droidkaigi.confsched.feature.sessions"
roborazzi.generateComposePreviewRobolectricTests.packages = listOf("io.github.droidkaigi.confsched.sessions")

kotlin {
sourceSets {
commonMain {
Expand Down
4 changes: 3 additions & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ koin = "3.4.3"
ktlint = "0.49.1"
kotlinxSerialization = "1.6.3"
ktor = "2.3.10"
roborazzi = "1.22.2"
roborazzi = "1.23.0"
ksp = "1.9.24-1.0.20"
firebaseBom = "33.1.0"
multiplatformFirebase = "1.8.1"
Expand Down Expand Up @@ -172,6 +172,8 @@ roborazziIos = { module = "io.github.takahirom.roborazzi:roborazzi-compose-ios",
roborazziRule = { module = "io.github.takahirom.roborazzi:roborazzi-junit-rule", version.ref = "roborazzi" }
roborazziPreviewScannerSupport = { module = "io.github.takahirom.roborazzi:roborazzi-compose-preview-scanner-support", version.ref = "roborazzi" }
composablePreviewScanner = { module = "com.github.sergio-sastre.ComposablePreviewScanner:android", version.ref = "composablePreviewScanner" }
composablePreviewScannerJvm = { module = "com.github.sergio-sastre.ComposablePreviewScanner:jvm", version.ref = "composablePreviewScanner" }
kotlinReflect = { module = "org.jetbrains.kotlin:kotlin-reflect", version.ref = "kotlin" }

[plugins]
androidGradlePlugin = { id = "com.android.application", version.ref = "androidGradlePlugin" }
Expand Down

0 comments on commit 1b0818c

Please sign in to comment.