Skip to content

Commit

Permalink
Merge branch 'takahirom/refactor-test/2024-12-12' into takahirom/add-…
Browse files Browse the repository at this point in the history
…alertdialog-test/2024-11-15
  • Loading branch information
takahirom authored Dec 12, 2024
2 parents f93fa2a + 95ae57c commit 434cc8e
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -239,4 +241,23 @@ data class RoborazziComposeFontScaleOption(private val fontScale: Float) :
override fun configure() {
setFontScale(fontScale)
}
}
}

@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()
}
}
}
Original file line number Diff line number Diff line change
@@ -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<out Activity>) {
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!")
}
}
}
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -31,15 +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.roboOutputName
import com.github.takahirom.roborazzi.roborazziSystemPropertyOutputDirectory
import com.github.takahirom.roborazzi.withComposeTestTag
Expand Down Expand Up @@ -161,51 +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 can also configure the activity scenario and the composable content.
addOption(
object : RoborazziComposeComposableOption,
RoborazziComposeActivityScenarioOption {
override fun configureWithActivityScenario(scenario: ActivityScenario<out Activity>) {
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() {
Expand Down

0 comments on commit 434cc8e

Please sign in to comment.