From 2f972ac591f973aacc911441e4dd3677ba5d8a36 Mon Sep 17 00:00:00 2001 From: YusukeMoriJapan Date: Wed, 11 Dec 2024 23:51:49 +0900 Subject: [PATCH 1/3] add localInspectionMode property to RoborazziComposeOptions --- .../roborazzi/RoborazziComposeOptions.kt | 23 ++++++++++++++++++- .../takahirom/roborazzi/sample/ManualTest.kt | 4 ++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/roborazzi-compose/src/main/java/com/github/takahirom/roborazzi/RoborazziComposeOptions.kt b/roborazzi-compose/src/main/java/com/github/takahirom/roborazzi/RoborazziComposeOptions.kt index 51b6daa5..aa6e8cfc 100644 --- a/roborazzi-compose/src/main/java/com/github/takahirom/roborazzi/RoborazziComposeOptions.kt +++ b/roborazzi-compose/src/main/java/com/github/takahirom/roborazzi/RoborazziComposeOptions.kt @@ -8,7 +8,9 @@ import androidx.compose.foundation.layout.height import androidx.compose.foundation.layout.size import androidx.compose.foundation.layout.width import androidx.compose.runtime.Composable +import androidx.compose.runtime.CompositionLocalProvider import androidx.compose.ui.Modifier +import androidx.compose.ui.platform.LocalInspectionMode import androidx.compose.ui.unit.dp import androidx.test.core.app.ActivityScenario import org.robolectric.RuntimeEnvironment.setFontScale @@ -239,4 +241,23 @@ data class RoborazziComposeFontScaleOption(private val fontScale: Float) : override fun configure() { setFontScale(fontScale) } -} \ No newline at end of file +} + +@ExperimentalRoborazziApi +fun RoborazziComposeOptions.Builder.localInspectionMode( + localInspectionMode: Boolean +): RoborazziComposeOptions.Builder = + addOption(RoborazziComposeLocalInspectionModeOption(localInspectionMode)) + + +@ExperimentalRoborazziApi +data class RoborazziComposeLocalInspectionModeOption(private val localInspectionMode: Boolean) : + RoborazziComposeComposableOption { + override fun configureWithComposable( + content: @Composable () -> Unit + ): @Composable () -> Unit = { + CompositionLocalProvider(LocalInspectionMode provides localInspectionMode) { + content() + } + } +} diff --git a/sample-android/src/test/java/com/github/takahirom/roborazzi/sample/ManualTest.kt b/sample-android/src/test/java/com/github/takahirom/roborazzi/sample/ManualTest.kt index 83c2092a..97610f09 100644 --- a/sample-android/src/test/java/com/github/takahirom/roborazzi/sample/ManualTest.kt +++ b/sample-android/src/test/java/com/github/takahirom/roborazzi/sample/ManualTest.kt @@ -40,6 +40,7 @@ import com.github.takahirom.roborazzi.captureRoboGif import com.github.takahirom.roborazzi.captureRoboImage import com.github.takahirom.roborazzi.captureRoboLastImage import com.github.takahirom.roborazzi.fontScale +import com.github.takahirom.roborazzi.localInspectionMode import com.github.takahirom.roborazzi.roboOutputName import com.github.takahirom.roborazzi.roborazziSystemPropertyOutputDirectory import com.github.takahirom.roborazzi.withComposeTestTag @@ -177,6 +178,9 @@ class ManualTest { roborazziComposeOptions = RoborazziComposeOptions { // We have several options to configure the test environment. fontScale(2f) + /* The default value is false, but we can set it to true, + if you want to use the logic for Preview in composable functions. */ + localInspectionMode(false) // We can also configure the activity scenario and the composable content. addOption( object : RoborazziComposeComposableOption, From 4f6007ad6296d48abf4c264b7d6ccc097176dc7c Mon Sep 17 00:00:00 2001 From: YusukeMoriJapan Date: Thu, 12 Dec 2024 13:37:21 +0900 Subject: [PATCH 2/3] change comment --- .../github/takahirom/roborazzi/sample/ManualTest.kt | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/sample-android/src/test/java/com/github/takahirom/roborazzi/sample/ManualTest.kt b/sample-android/src/test/java/com/github/takahirom/roborazzi/sample/ManualTest.kt index 97610f09..5faf349e 100644 --- a/sample-android/src/test/java/com/github/takahirom/roborazzi/sample/ManualTest.kt +++ b/sample-android/src/test/java/com/github/takahirom/roborazzi/sample/ManualTest.kt @@ -178,9 +178,16 @@ class ManualTest { roborazziComposeOptions = RoborazziComposeOptions { // We have several options to configure the test environment. fontScale(2f) - /* The default value is false, but we can set it to true, - if you want to use the logic for Preview in composable functions. */ - localInspectionMode(false) + + /* + We don't specify `localInspectionMode` by default. + The default value for `localInspectionMode` in Compose is `false`. + This is to maintain higher fidelity in tests. + If you encounter issues integrating the library, you can set `localInspectionMode` to `true`. + + localInspectionMode(true) + */ + // We can also configure the activity scenario and the composable content. addOption( object : RoborazziComposeComposableOption, From 95ae57cbe570616c6506a067d437d4731f5f43c6 Mon Sep 17 00:00:00 2001 From: takahirom Date: Thu, 12 Dec 2024 19:53:36 +0900 Subject: [PATCH 3/3] Separate compose lambda test --- .../roborazzi/sample/ComposeLambdaTest.kt | 78 +++++++++++++++++++ .../takahirom/roborazzi/sample/ManualTest.kt | 69 ---------------- 2 files changed, 78 insertions(+), 69 deletions(-) create mode 100644 sample-android/src/test/java/com/github/takahirom/roborazzi/sample/ComposeLambdaTest.kt diff --git a/sample-android/src/test/java/com/github/takahirom/roborazzi/sample/ComposeLambdaTest.kt b/sample-android/src/test/java/com/github/takahirom/roborazzi/sample/ComposeLambdaTest.kt new file mode 100644 index 00000000..4c145ffa --- /dev/null +++ b/sample-android/src/test/java/com/github/takahirom/roborazzi/sample/ComposeLambdaTest.kt @@ -0,0 +1,78 @@ +package com.github.takahirom.roborazzi.sample + +import android.app.Activity +import android.graphics.Color +import androidx.compose.foundation.background +import androidx.compose.foundation.layout.Box +import androidx.compose.foundation.layout.padding +import androidx.compose.material3.Text +import androidx.compose.runtime.Composable +import androidx.compose.ui.Modifier +import androidx.compose.ui.unit.dp +import androidx.test.core.app.ActivityScenario +import com.github.takahirom.roborazzi.ExperimentalRoborazziApi +import com.github.takahirom.roborazzi.RoborazziComposeActivityScenarioOption +import com.github.takahirom.roborazzi.RoborazziComposeComposableOption +import com.github.takahirom.roborazzi.RoborazziComposeOptions +import com.github.takahirom.roborazzi.captureRoboImage +import com.github.takahirom.roborazzi.fontScale +import com.github.takahirom.roborazzi.roborazziSystemPropertyOutputDirectory +import org.junit.Test + +class ComposeLambdaTest { + @OptIn(ExperimentalRoborazziApi::class) + @Test + fun captureComposeLambdaImage() { + captureRoboImage("${roborazziSystemPropertyOutputDirectory()}/manual_compose.png") { + Text("Hello Compose!") + } + } + + @OptIn(ExperimentalRoborazziApi::class) + @Test + fun captureComposeLambdaImageWithRoborazziComposeOptions() { + captureRoboImage( + "${roborazziSystemPropertyOutputDirectory()}/manual_compose_with_compose_options.png", + roborazziComposeOptions = RoborazziComposeOptions { + // We have several options to configure the test environment. + fontScale(2f) + + /* + We don't specify `localInspectionMode` by default. + The default value for `localInspectionMode` in Compose is `false`. + This is to maintain higher fidelity in tests. + If you encounter issues integrating the library, you can set `localInspectionMode` to `true`. + + localInspectionMode(true) + */ + + // We can also configure the activity scenario and the composable content. + addOption( + object : RoborazziComposeComposableOption, + RoborazziComposeActivityScenarioOption { + override fun configureWithActivityScenario(scenario: ActivityScenario) { + scenario.onActivity { + it.window.decorView.setBackgroundColor(Color.BLUE) + } + } + + override fun configureWithComposable(content: @Composable () -> Unit): @Composable () -> Unit { + return { + Box( + Modifier + .padding(10.dp) + .background(color = androidx.compose.ui.graphics.Color.Red) + .padding(10.dp) + ) { + content() + } + } + } + } + ) + }, + ) { + Text("Hello Compose!") + } + } +} \ No newline at end of file diff --git a/sample-android/src/test/java/com/github/takahirom/roborazzi/sample/ManualTest.kt b/sample-android/src/test/java/com/github/takahirom/roborazzi/sample/ManualTest.kt index 5faf349e..31cf8c4b 100644 --- a/sample-android/src/test/java/com/github/takahirom/roborazzi/sample/ManualTest.kt +++ b/sample-android/src/test/java/com/github/takahirom/roborazzi/sample/ManualTest.kt @@ -1,24 +1,15 @@ package com.github.takahirom.roborazzi.sample -import android.app.Activity import android.graphics.Bitmap import android.graphics.Color import android.view.View import android.widget.TextView -import androidx.compose.foundation.background -import androidx.compose.foundation.layout.Box -import androidx.compose.foundation.layout.padding -import androidx.compose.material3.Text -import androidx.compose.runtime.Composable -import androidx.compose.ui.Modifier import androidx.compose.ui.test.junit4.createAndroidComposeRule import androidx.compose.ui.test.onNodeWithTag import androidx.compose.ui.test.onParent import androidx.compose.ui.test.performClick -import androidx.compose.ui.unit.dp import androidx.core.graphics.applyCanvas import androidx.core.graphics.createBitmap -import androidx.test.core.app.ActivityScenario import androidx.test.espresso.Espresso.onView import androidx.test.espresso.Espresso.pressBack import androidx.test.espresso.action.ViewActions.click @@ -31,16 +22,11 @@ import com.github.takahirom.roborazzi.Dump import com.github.takahirom.roborazzi.ExperimentalRoborazziApi import com.github.takahirom.roborazzi.RoboComponent import com.github.takahirom.roborazzi.RobolectricDeviceQualifiers -import com.github.takahirom.roborazzi.RoborazziComposeActivityScenarioOption -import com.github.takahirom.roborazzi.RoborazziComposeComposableOption -import com.github.takahirom.roborazzi.RoborazziComposeOptions import com.github.takahirom.roborazzi.RoborazziOptions import com.github.takahirom.roborazzi.captureRoboAllImage import com.github.takahirom.roborazzi.captureRoboGif import com.github.takahirom.roborazzi.captureRoboImage import com.github.takahirom.roborazzi.captureRoboLastImage -import com.github.takahirom.roborazzi.fontScale -import com.github.takahirom.roborazzi.localInspectionMode import com.github.takahirom.roborazzi.roboOutputName import com.github.takahirom.roborazzi.roborazziSystemPropertyOutputDirectory import com.github.takahirom.roborazzi.withComposeTestTag @@ -162,61 +148,6 @@ class ManualTest { }.captureRoboImage("${roborazziSystemPropertyOutputDirectory()}/manual_view_without_window.png") } - @OptIn(ExperimentalRoborazziApi::class) - @Test - fun captureComposeLambdaImage() { - captureRoboImage("${roborazziSystemPropertyOutputDirectory()}/manual_compose.png") { - Text("Hello Compose!") - } - } - - @OptIn(ExperimentalRoborazziApi::class) - @Test - fun captureComposeLambdaImageWithRoborazziComposeOptions() { - captureRoboImage( - "${roborazziSystemPropertyOutputDirectory()}/manual_compose_with_compose_options.png", - roborazziComposeOptions = RoborazziComposeOptions { - // We have several options to configure the test environment. - fontScale(2f) - - /* - We don't specify `localInspectionMode` by default. - The default value for `localInspectionMode` in Compose is `false`. - This is to maintain higher fidelity in tests. - If you encounter issues integrating the library, you can set `localInspectionMode` to `true`. - - localInspectionMode(true) - */ - - // We can also configure the activity scenario and the composable content. - addOption( - object : RoborazziComposeComposableOption, - RoborazziComposeActivityScenarioOption { - override fun configureWithActivityScenario(scenario: ActivityScenario) { - scenario.onActivity { - it.window.decorView.setBackgroundColor(Color.BLUE) - } - } - - override fun configureWithComposable(content: @Composable () -> Unit): @Composable () -> Unit { - return { - Box(Modifier - .padding(10.dp) - .background(color = androidx.compose.ui.graphics.Color.Red) - .padding(10.dp) - ) { - content() - } - } - } - } - ) - }, - ) { - Text("Hello Compose!") - } - } - @OptIn(ExperimentalRoborazziApi::class) @Test fun captureBitmapImage() {