Skip to content

Commit

Permalink
Add androidx compose screenshot tests. (#2260)
Browse files Browse the repository at this point in the history
  • Loading branch information
colinrtwhite committed May 15, 2024
1 parent 893c3f7 commit c247a60
Show file tree
Hide file tree
Showing 12 changed files with 126 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ jobs:
gradle-home-cache-cleanup: true

- name: Unit tests
run: ./gradlew allTests testDebugUnitTest verifyPaparazziDebug verifyRoborazziDebug
run: ./gradlew allTests testDebugUnitTest validateDebugScreenshotTest verifyPaparazziDebug verifyRoborazziDebug

instrumentation-tests:
name: Instrumentation tests
Expand Down
10 changes: 10 additions & 0 deletions .idea/inspectionProfiles/Project_Default.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions coil-compose/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ plugins {
id("com.android.library")
id("kotlin-multiplatform")
id("kotlinx-atomicfu")
id("org.jetbrains.compose")
id("org.jetbrains.kotlin.plugin.compose")
}

Expand Down
1 change: 1 addition & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
org.gradle.jvmargs=-Xmx8g -Xms2g -XX:MaxMetaspaceSize=2g -XX:+UseParallelGC -Dfile.encoding=UTF-8

# Android
android.experimental.enableScreenshotTest=true
android.useAndroidX=true

# Disable welcome message.
Expand Down
4 changes: 3 additions & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@ baselineProfile = { id = "androidx.baselineprofile", version.ref = "androidx-ben
binaryCompatibility = "org.jetbrains.kotlinx.binary-compatibility-validator:0.14.0"
dokka = "org.jetbrains.dokka:1.9.20"
poko = "dev.drewhamilton.poko:0.16.0-SNAPSHOT"
screenshot = "com.android.compose.screenshot:0.0.1-alpha01"
spotless = "com.diffplug.spotless:6.25.0"

[libraries]
gradlePlugin-android = "com.android.tools.build:gradle:8.4.0"
gradlePlugin-android = "com.android.tools.build:gradle:8.5.0-beta01"
gradlePlugin-atomicFu = "org.jetbrains.kotlinx:atomicfu-gradle-plugin:0.24.0"
gradlePlugin-jetbrainsCompose = { module = "org.jetbrains.compose:compose-gradle-plugin", version.ref = "jetbrains-compose" }
gradlePlugin-composeCompiler = { module = "org.jetbrains.kotlin.plugin.compose:org.jetbrains.kotlin.plugin.compose.gradle.plugin", version.ref = "kotlin" }
Expand All @@ -39,6 +40,7 @@ androidx-appcompat-resources = "androidx.appcompat:appcompat-resources:1.6.1"
androidx-annotation = "androidx.annotation:annotation:1.8.0"
androidx-benchmark-macro = "androidx.benchmark:benchmark-macro-junit4:1.2.4"
androidx-compose-runtime-tracing = { module = "androidx.compose.runtime:runtime-tracing", version.ref = "androidx-runtime-tracing" }
androidx-compose-ui-tooling = { group = "androidx.compose.ui", name = "ui-tooling" }
androidx-constraintlayout = "androidx.constraintlayout:constraintlayout:2.1.4"
androidx-core = "androidx.core:core-ktx:1.13.1"
androidx-exifinterface = "androidx.exifinterface:exifinterface:1.3.7"
Expand Down
28 changes: 28 additions & 0 deletions internal/test-compose-screenshot/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import coil3.androidLibrary

plugins {
id("com.android.library")
id("kotlin-android")
id("org.jetbrains.compose")
id("org.jetbrains.kotlin.plugin.compose")
alias(libs.plugins.screenshot)
}

androidLibrary(name = "coil3.test.composescreenshot") {
buildFeatures {
compose = true
}
experimentalProperties["android.experimental.enableScreenshotTest"] = true
}

dependencies {
api(projects.coilCore)

implementation(projects.coilComposeCore)
implementation(projects.coilTest)

testImplementation(projects.internal.testUtils)
testImplementation(libs.bundles.test.jvm)

screenshotTestImplementation(libs.androidx.compose.ui.tooling)
}
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package coil3.composescreenshot

import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.tooling.preview.Devices
import androidx.compose.ui.tooling.preview.Preview
import coil3.ImageLoader
import coil3.compose.AsyncImage
import coil3.compose.rememberAsyncImagePainter
import coil3.request.ImageRequest
import coil3.request.placeholder

class PreviewScreenshots {

@Preview(
device = Devices.PIXEL,
showBackground = true,
)
@Composable
fun asyncImage() {
val context = LocalContext.current
val imageLoader = remember { ImageLoader(context) }

AsyncImage(
// AsyncImagePainter's default preview behaviour displays the request's placeholder.
model = ImageRequest.Builder(context)
.data(Unit)
.placeholder(
object : ColorDrawable(Color.RED) {
override fun getIntrinsicWidth() = 100
override fun getIntrinsicHeight() = 100
},
)
.build(),
contentDescription = null,
imageLoader = imageLoader,
contentScale = ContentScale.None,
modifier = Modifier.fillMaxSize(),
)
}

@Preview(
device = Devices.PIXEL,
showBackground = true,
)
@Composable
fun rememberAsyncImagePainter() {
val context = LocalContext.current
val imageLoader = remember { ImageLoader(context) }

Image(
painter = rememberAsyncImagePainter(
// AsyncImagePainter's default preview behaviour displays the request's placeholder.
model = ImageRequest.Builder(context)
.data(Unit)
.placeholder(
object : ColorDrawable(Color.RED) {
override fun getIntrinsicWidth() = 100
override fun getIntrinsicHeight() = 100
},
)
.build(),
imageLoader = imageLoader,
),
contentDescription = null,
contentScale = ContentScale.None,
modifier = Modifier.fillMaxSize(),
)
}
}
3 changes: 2 additions & 1 deletion internal/test-paparazzi/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ import coil3.androidLibrary
plugins {
id("com.android.library")
id("kotlin-android")
id("app.cash.paparazzi")
id("org.jetbrains.compose")
id("org.jetbrains.kotlin.plugin.compose")
id("app.cash.paparazzi")
}

androidLibrary(name = "coil3.test.paparazzi")
Expand Down
2 changes: 1 addition & 1 deletion internal/test-roborazzi/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import coil3.androidLibrary
plugins {
id("com.android.library")
id("kotlin-android")
id("io.github.takahirom.roborazzi")
id("org.jetbrains.compose")
id("org.jetbrains.kotlin.plugin.compose")
id("io.github.takahirom.roborazzi")
}

androidLibrary(name = "coil3.test.roborazzi")
Expand Down
3 changes: 2 additions & 1 deletion settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,10 @@ include(
// Private modules
include(
"internal:benchmark",
"internal:test-utils",
"internal:test-compose-screenshot",
"internal:test-paparazzi",
"internal:test-roborazzi",
"internal:test-utils",
"samples:compose",
"samples:shared",
"samples:view",
Expand Down

0 comments on commit c247a60

Please sign in to comment.