Skip to content

Commit

Permalink
Use context receiver
Browse files Browse the repository at this point in the history
  • Loading branch information
takahirom committed Aug 10, 2023
1 parent 20de04c commit 5823423
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 10 deletions.
3 changes: 2 additions & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
root = true

[*]
indent_size = 2
indent_size = 2
indent_style = space
10 changes: 9 additions & 1 deletion app-compose-desktop/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import org.jetbrains.compose.desktop.application.dsl.TargetFormat
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
kotlin("jvm")
Expand All @@ -18,7 +19,7 @@ dependencies {
// (in a separate module for demo project and in testMain).
// With compose.desktop.common you will also lose @Preview functionality
implementation(compose.desktop.currentOs)
testImplementation(project(":roborazzi-desktop"))
testImplementation(project(":roborazzi-compose-desktop"))

testImplementation(kotlin("test"))
}
Expand All @@ -34,3 +35,10 @@ compose.desktop {
}
}
}

tasks.withType<KotlinCompile>().configureEach {
kotlinOptions {
incremental = false
freeCompilerArgs += "-Xcontext-receivers"
}
}
5 changes: 4 additions & 1 deletion app-compose-desktop/src/main/kotlin/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ fun App() {
onClick = {
text = "Hello, Desktop!"
}) {
Text(text)
Text(
style = MaterialTheme.typography.h2,
text = text
)
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions app-compose-desktop/src/test/kotlin/MainTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ class MainTest {
val roborazziOptions = RoborazziOptions(
compareOptions = RoborazziOptions.CompareOptions(changeThreshold = 0F)
)
onNode(isRoot())
.captureToImage().captureRoboImage(roborazziOptions = roborazziOptions)
onNode(isRoot()).captureRoboImage(roborazziOptions = roborazziOptions)

onNodeWithTag("button").performClick()

onNode(isRoot()).captureToImage().captureRoboImage(roborazziOptions = roborazziOptions)
onNode(isRoot()).captureRoboImage(roborazziOptions = roborazziOptions)
}
}
}
}
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
id 'org.jetbrains.kotlin.multiplatform'
id 'org.jetbrains.compose'
Expand Down Expand Up @@ -47,6 +49,9 @@ java {
}
}

sourceSets {
main.java.srcDir 'src/generated/kotlin'
tasks.withType(KotlinCompile).configureEach {
kotlinOptions {
incremental = false
freeCompilerArgs += "-Xcontext-receivers"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,55 @@ package io.github.takahirom.roborazzi

import androidx.compose.ui.graphics.ImageBitmap
import androidx.compose.ui.graphics.toAwtImage
import androidx.compose.ui.test.DesktopComposeUiTest
import androidx.compose.ui.test.ExperimentalTestApi
import androidx.compose.ui.test.SemanticsNodeInteraction
import com.github.takahirom.roborazzi.*
import java.awt.image.BufferedImage
import java.io.File

context(DesktopComposeUiTest)
@OptIn(ExperimentalTestApi::class)
fun SemanticsNodeInteraction.captureRoboImage(
filePath: String = DefaultFileNameGenerator.generateFilePath("png"),
roborazziOptions: RoborazziOptions = provideRoborazziContext().options,
) {
if (!roborazziEnabled()) {
return
}
captureRoboImage(
file = File(filePath),
roborazziOptions = roborazziOptions
)
}

context(DesktopComposeUiTest)
@OptIn(ExperimentalTestApi::class)
fun SemanticsNodeInteraction.captureRoboImage(
file: File,
roborazziOptions: RoborazziOptions
) {
if (!roborazziEnabled()) {
return
}
val awtImage = this.captureToImage().toAwtImage()
val canvas = AwtRoboCanvas(
width = awtImage.width,
height = awtImage.height,
filled = true,
bufferedImageType = BufferedImage.TYPE_INT_ARGB
)
canvas.apply {
drawImage(awtImage)
}
processOutputImageAndReportWithDefaults(
canvas = canvas,
goldenFile = file,
roborazziOptions = roborazziOptions
)
canvas.release()
}

fun ImageBitmap.captureRoboImage(
filePath: String = DefaultFileNameGenerator.generateFilePath("png"),
roborazziOptions: RoborazziOptions = provideRoborazziContext().options,
Expand Down
2 changes: 1 addition & 1 deletion settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ include ':app-without-compose'
include ':roborazzi'
include ':app-compose-desktop'
include ':roborazzi-junit-rule'
include ':roborazzi-desktop'
include ':roborazzi-compose-desktop'
include ':roborazzi-compose'
include ':roborazzi-painter'

Expand Down

0 comments on commit 5823423

Please sign in to comment.