Skip to content

Commit

Permalink
Merge pull request #606 from takahirom/takahirom/refactor-test/2024-1…
Browse files Browse the repository at this point in the history
…2-12

[Refactor] Separate compose lambda test
  • Loading branch information
takahirom authored Dec 12, 2024
2 parents 02590f2 + 11e4834 commit 8339a5f
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 69 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
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 androidx.test.ext.junit.runners.AndroidJUnit4
import com.github.takahirom.roborazzi.ExperimentalRoborazziApi
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.captureRoboImage
import com.github.takahirom.roborazzi.fontScale
import com.github.takahirom.roborazzi.roborazziSystemPropertyOutputDirectory
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.annotation.Config
import org.robolectric.annotation.GraphicsMode

@RunWith(AndroidJUnit4::class)
@GraphicsMode(GraphicsMode.Mode.NATIVE)
@Config(
sdk = [30],
qualifiers = RobolectricDeviceQualifiers.NexusOne
)
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,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
Expand Down Expand Up @@ -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<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 8339a5f

Please sign in to comment.