From 247ca039f8b5dcd7893f66d5013fa4391c338485 Mon Sep 17 00:00:00 2001 From: Chris Banes Date: Wed, 3 Jan 2024 16:39:13 +0000 Subject: [PATCH] Add screenshot tests to `haze-jetpack-compose` --- haze-jetpack-compose/build.gradle.kts | 13 +++ .../android/HazeScreenshotTest.creditCard.png | 3 + ...eenshotTest.creditCard_transparentTint.png | 3 + .../kotlin/dev/chrisbanes/haze/HazeNode31.kt | 11 ++- .../dev/chrisbanes/haze/HazeScreenshotTest.kt | 26 ++++++ .../chrisbanes/haze/ScreenshotTestContent.kt | 81 +++++++++++++++++++ .../dev/chrisbanes/haze/HazeScreenshotTest.kt | 2 +- .../chrisbanes/haze/test/ScreenshotTest.kt | 3 + .../haze/test/HazeRoborazziDefaults.kt | 3 + .../chrisbanes/haze/test/ScreenshotTest.kt | 3 + .../chrisbanes/haze/test/ScreenshotTest.kt | 3 + .../chrisbanes/haze/test/ScreenshotTest.kt | 3 + 12 files changed, 152 insertions(+), 2 deletions(-) create mode 100644 haze-jetpack-compose/screenshots/android/HazeScreenshotTest.creditCard.png create mode 100644 haze-jetpack-compose/screenshots/android/HazeScreenshotTest.creditCard_transparentTint.png create mode 100644 haze-jetpack-compose/src/test/kotlin/dev/chrisbanes/haze/HazeScreenshotTest.kt create mode 100644 haze-jetpack-compose/src/test/kotlin/dev/chrisbanes/haze/ScreenshotTestContent.kt diff --git a/haze-jetpack-compose/build.gradle.kts b/haze-jetpack-compose/build.gradle.kts index 3f4de74c..690f296f 100644 --- a/haze-jetpack-compose/build.gradle.kts +++ b/haze-jetpack-compose/build.gradle.kts @@ -8,11 +8,21 @@ plugins { id("org.jetbrains.dokka") id("com.vanniktech.maven.publish") id("me.tylerbwong.gradle.metalava") + id("io.github.takahirom.roborazzi") } android { namespace = "dev.chrisbanes.haze.jetpackcompose" + defaultConfig { + testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner" + } + + testOptions { + unitTests.isIncludeAndroidResources = true + } + testBuildType = "release" + buildFeatures { compose = true } @@ -24,6 +34,9 @@ android { dependencies { api(libs.androidx.compose.ui) + + testImplementation(kotlin("test")) + testImplementation(projects.internal.screenshotTest) } metalava { diff --git a/haze-jetpack-compose/screenshots/android/HazeScreenshotTest.creditCard.png b/haze-jetpack-compose/screenshots/android/HazeScreenshotTest.creditCard.png new file mode 100644 index 00000000..2135b807 --- /dev/null +++ b/haze-jetpack-compose/screenshots/android/HazeScreenshotTest.creditCard.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:e56e562874c4db6ca4d05bde25b4123c6a78f298ce572c518e2e503d97f38d10 +size 886985 diff --git a/haze-jetpack-compose/screenshots/android/HazeScreenshotTest.creditCard_transparentTint.png b/haze-jetpack-compose/screenshots/android/HazeScreenshotTest.creditCard_transparentTint.png new file mode 100644 index 00000000..2135b807 --- /dev/null +++ b/haze-jetpack-compose/screenshots/android/HazeScreenshotTest.creditCard_transparentTint.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:e56e562874c4db6ca4d05bde25b4123c6a78f298ce572c518e2e503d97f38d10 +size 886985 diff --git a/haze-jetpack-compose/src/main/kotlin/dev/chrisbanes/haze/HazeNode31.kt b/haze-jetpack-compose/src/main/kotlin/dev/chrisbanes/haze/HazeNode31.kt index fdfd8232..6dc9b6b6 100644 --- a/haze-jetpack-compose/src/main/kotlin/dev/chrisbanes/haze/HazeNode31.kt +++ b/haze-jetpack-compose/src/main/kotlin/dev/chrisbanes/haze/HazeNode31.kt @@ -110,9 +110,18 @@ internal class HazeNode31( if (currentValueOf(LocalInspectionMode)) { // If LocalInspectionMode is true, we're likely running in a preview/screenshot test // and therefore don't have full access to Android drawing APIs. To avoid crashing we - // no-op and return early. + // just draw the content and return early. + drawContent() return } + drawIntoCanvas { canvas -> + // Similar to above, drawRenderNode is only available on hw-accelerated canvases. + // To avoid crashing we just draw the content and return early. + if (!canvas.nativeCanvas.isHardwareAccelerated) { + drawContent() + return@draw + } + } val contentDrawScope = this diff --git a/haze-jetpack-compose/src/test/kotlin/dev/chrisbanes/haze/HazeScreenshotTest.kt b/haze-jetpack-compose/src/test/kotlin/dev/chrisbanes/haze/HazeScreenshotTest.kt new file mode 100644 index 00000000..1eddac00 --- /dev/null +++ b/haze-jetpack-compose/src/test/kotlin/dev/chrisbanes/haze/HazeScreenshotTest.kt @@ -0,0 +1,26 @@ +// Copyright 2024, Christopher Banes and the Haze project contributors +// SPDX-License-Identifier: Apache-2.0 + +package dev.chrisbanes.haze + +import androidx.compose.material3.MaterialTheme +import androidx.compose.ui.graphics.Color +import dev.chrisbanes.haze.test.ScreenshotTest +import dev.chrisbanes.haze.test.screenshotTest +import kotlin.test.Test + +class HazeScreenshotTest : ScreenshotTest() { + @Test + fun creditCard() = screenshotTest { + MaterialTheme { + CreditCardSample() + } + } + + @Test + fun creditCard_transparentTint() = screenshotTest { + MaterialTheme { + CreditCardSample(tint = Color.Transparent) + } + } +} diff --git a/haze-jetpack-compose/src/test/kotlin/dev/chrisbanes/haze/ScreenshotTestContent.kt b/haze-jetpack-compose/src/test/kotlin/dev/chrisbanes/haze/ScreenshotTestContent.kt new file mode 100644 index 00000000..e8fa1dea --- /dev/null +++ b/haze-jetpack-compose/src/test/kotlin/dev/chrisbanes/haze/ScreenshotTestContent.kt @@ -0,0 +1,81 @@ +// Copyright 2024, Christopher Banes and the Haze project contributors +// SPDX-License-Identifier: Apache-2.0 + +package dev.chrisbanes.haze + +import androidx.compose.foundation.background +import androidx.compose.foundation.layout.Box +import androidx.compose.foundation.layout.Column +import androidx.compose.foundation.layout.Spacer +import androidx.compose.foundation.layout.aspectRatio +import androidx.compose.foundation.layout.fillMaxSize +import androidx.compose.foundation.layout.fillMaxWidth +import androidx.compose.foundation.layout.padding +import androidx.compose.foundation.shape.RoundedCornerShape +import androidx.compose.material3.LocalContentColor +import androidx.compose.material3.Text +import androidx.compose.runtime.Composable +import androidx.compose.runtime.remember +import androidx.compose.ui.Alignment +import androidx.compose.ui.Modifier +import androidx.compose.ui.graphics.Brush +import androidx.compose.ui.graphics.Color +import androidx.compose.ui.unit.dp + +@Composable +fun CreditCardSample(tint: Color? = null) { + val hazeState = remember { HazeState() } + + Box { + // Background content + Box( + Modifier + .fillMaxSize() + .haze( + state = hazeState, + backgroundColor = Color.Blue, + tint = tint ?: Color.White.copy(alpha = 0.1f), + blurRadius = 8.dp, + ), + ) { + Spacer( + Modifier + .fillMaxSize() + .background(brush = Brush.linearGradient(colors = listOf(Color.Blue, Color.Cyan))), + ) + + Text( + text = LorumIspum, + color = LocalContentColor.current.copy(alpha = 0.2f), + modifier = Modifier.padding(24.dp), + ) + } + + // Our card + Box( + modifier = Modifier + .fillMaxWidth(0.7f) + .aspectRatio(16 / 9f) + .align(Alignment.Center) + .hazeChild(state = hazeState, shape = RoundedCornerShape(16.dp)), + ) { + Column(Modifier.padding(32.dp)) { + Text("Bank of Haze") + } + } + } +} + +val LorumIspum by lazy { + """ +Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras sit amet congue mauris, iaculis accumsan eros. Aliquam pulvinar est ac elit vulputate egestas. Vestibulum consequat libero at sem varius, vitae semper urna rhoncus. Aliquam mollis, ipsum a convallis scelerisque, sem dui consequat leo, in tempor risus est ac mi. Nam vel tellus dolor. Nunc lobortis bibendum fermentum. Mauris sed mollis justo, eu tristique elit. Cras semper augue a tortor tempor, vitae vestibulum eros convallis. Curabitur id justo eget tortor iaculis lobortis. Integer pharetra augue ac elit porta iaculis non vitae libero. Nam eros turpis, suscipit at iaculis vitae, malesuada vel arcu. Donec tincidunt porttitor iaculis. Pellentesque non augue magna. Mauris mattis purus vitae mi maximus, id molestie ipsum facilisis. Donec bibendum gravida dolor nec suscipit. Pellentesque tempus felis iaculis, porta diam sed, tristique tortor. + +Sed vel tellus vel augue pulvinar semper sit amet eu est. In porta arcu eu sapien luctus scelerisque. In hac habitasse platea dictumst. Aenean varius lobortis malesuada. Sed vitae ornare arcu. Nunc maximus lectus purus, vel aliquet velit facilisis a. Nulla maximus bibendum magna id vulputate. Mauris volutpat lorem et risus porta dignissim. In at elit a est vulputate tincidunt. + +Nulla facilisi. Curabitur gravida quam nec massa tempus, sed placerat nunc hendrerit. Duis sit amet cursus ipsum. Phasellus eget congue lacus. Duis vehicula venenatis posuere. Morbi non tempor risus. Aenean bibendum efficitur tortor, eu interdum velit gravida rutrum. Sed tempus elementum libero. Suspendisse dapibus lorem vitae justo congue pellentesque. Phasellus et tellus sagittis, blandit nibh a, porta felis. Proin ornare eget odio eget laoreet. Cras id augue fringilla, molestie ligula sit amet, sollicitudin neque. + +Suspendisse vitae bibendum justo, nec egestas mauris. Mauris id metus mi. Morbi ut maximus ex, eu consequat elit. Sed malesuada pellentesque mauris vel molestie. Nulla facilisi. Cras pellentesque metus id nibh sodales gravida. Vivamus a feugiat felis. Vivamus et justo libero. Maecenas ac augue viverra, blandit diam sed, porttitor sapien. Proin eu eros mollis, commodo lectus nec, imperdiet nisi. Proin nulla nulla, vehicula a faucibus sit amet, auctor sed lorem. Mauris ut ipsum sit amet massa posuere maximus eget porttitor nisl. Quisque nunc dolor, pharetra id nunc sit amet, maximus convallis nunc. + +Ut magna diam, ullamcorper vel imperdiet at, dignissim sit amet turpis. Duis ut enim eu sapien fringilla placerat. Integer at dui eget leo tincidunt iaculis. Fusce nec elementum turpis. Aenean gravida, ipsum sit amet varius hendrerit, elit nisi hendrerit ex, et porta enim lorem eget mi. Duis convallis dolor a lacinia aliquam. Aliquam erat volutpat. +""".trim() +} diff --git a/haze/src/commonTest/kotlin/dev/chrisbanes/haze/HazeScreenshotTest.kt b/haze/src/commonTest/kotlin/dev/chrisbanes/haze/HazeScreenshotTest.kt index 71b00ab0..1eddac00 100644 --- a/haze/src/commonTest/kotlin/dev/chrisbanes/haze/HazeScreenshotTest.kt +++ b/haze/src/commonTest/kotlin/dev/chrisbanes/haze/HazeScreenshotTest.kt @@ -9,7 +9,7 @@ import dev.chrisbanes.haze.test.ScreenshotTest import dev.chrisbanes.haze.test.screenshotTest import kotlin.test.Test -class HazeScreenshotTest: ScreenshotTest() { +class HazeScreenshotTest : ScreenshotTest() { @Test fun creditCard() = screenshotTest { MaterialTheme { diff --git a/internal/screenshot-test/src/androidMain/kotlin/dev/chrisbanes/haze/test/ScreenshotTest.kt b/internal/screenshot-test/src/androidMain/kotlin/dev/chrisbanes/haze/test/ScreenshotTest.kt index 53b0733e..39602df9 100644 --- a/internal/screenshot-test/src/androidMain/kotlin/dev/chrisbanes/haze/test/ScreenshotTest.kt +++ b/internal/screenshot-test/src/androidMain/kotlin/dev/chrisbanes/haze/test/ScreenshotTest.kt @@ -1,3 +1,6 @@ +// Copyright 2024, Christopher Banes and the Haze project contributors +// SPDX-License-Identifier: Apache-2.0 + package dev.chrisbanes.haze.test import androidx.activity.ComponentActivity diff --git a/internal/screenshot-test/src/commonJvmMain/kotlin/dev/chrisbanes/haze/test/HazeRoborazziDefaults.kt b/internal/screenshot-test/src/commonJvmMain/kotlin/dev/chrisbanes/haze/test/HazeRoborazziDefaults.kt index 40a2027c..02b1e3dd 100644 --- a/internal/screenshot-test/src/commonJvmMain/kotlin/dev/chrisbanes/haze/test/HazeRoborazziDefaults.kt +++ b/internal/screenshot-test/src/commonJvmMain/kotlin/dev/chrisbanes/haze/test/HazeRoborazziDefaults.kt @@ -1,3 +1,6 @@ +// Copyright 2024, Christopher Banes and the Haze project contributors +// SPDX-License-Identifier: Apache-2.0 + package dev.chrisbanes.haze.test import com.dropbox.differ.SimpleImageComparator diff --git a/internal/screenshot-test/src/commonMain/kotlin/dev/chrisbanes/haze/test/ScreenshotTest.kt b/internal/screenshot-test/src/commonMain/kotlin/dev/chrisbanes/haze/test/ScreenshotTest.kt index 037736d7..62dba521 100644 --- a/internal/screenshot-test/src/commonMain/kotlin/dev/chrisbanes/haze/test/ScreenshotTest.kt +++ b/internal/screenshot-test/src/commonMain/kotlin/dev/chrisbanes/haze/test/ScreenshotTest.kt @@ -1,3 +1,6 @@ +// Copyright 2024, Christopher Banes and the Haze project contributors +// SPDX-License-Identifier: Apache-2.0 + package dev.chrisbanes.haze.test import androidx.compose.runtime.Composable diff --git a/internal/screenshot-test/src/iosMain/kotlin/dev/chrisbanes/haze/test/ScreenshotTest.kt b/internal/screenshot-test/src/iosMain/kotlin/dev/chrisbanes/haze/test/ScreenshotTest.kt index f7144daa..8b6c0588 100644 --- a/internal/screenshot-test/src/iosMain/kotlin/dev/chrisbanes/haze/test/ScreenshotTest.kt +++ b/internal/screenshot-test/src/iosMain/kotlin/dev/chrisbanes/haze/test/ScreenshotTest.kt @@ -1,3 +1,6 @@ +// Copyright 2024, Christopher Banes and the Haze project contributors +// SPDX-License-Identifier: Apache-2.0 + package dev.chrisbanes.haze.test import androidx.compose.runtime.Composable diff --git a/internal/screenshot-test/src/jvmMain/kotlin/dev/chrisbanes/haze/test/ScreenshotTest.kt b/internal/screenshot-test/src/jvmMain/kotlin/dev/chrisbanes/haze/test/ScreenshotTest.kt index 8abe2666..302340c8 100644 --- a/internal/screenshot-test/src/jvmMain/kotlin/dev/chrisbanes/haze/test/ScreenshotTest.kt +++ b/internal/screenshot-test/src/jvmMain/kotlin/dev/chrisbanes/haze/test/ScreenshotTest.kt @@ -1,3 +1,6 @@ +// Copyright 2024, Christopher Banes and the Haze project contributors +// SPDX-License-Identifier: Apache-2.0 + package dev.chrisbanes.haze.test import androidx.compose.runtime.Composable