Skip to content

Commit

Permalink
Merge pull request #237 from takahirom/takahirom/add-compose-dialog-t…
Browse files Browse the repository at this point in the history
…est/2024-01-03

Use View draw method to capture for Compose Material3
  • Loading branch information
takahirom authored Jan 4, 2024
2 parents c7295ac + 517f207 commit 53c63d4
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 9 deletions.
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ roborazzi-for-replacing-by-include-build = "1.0.0"
androidx-activity = "1.7.2"
androidx-appcompat = "1.4.1"
androidx-compose-material = "1.4.2"
androidx-compose-material3 = "1.0.1"
androidx-compose-material3 = "1.1.2"
androidx-compose-foundation = "1.4.0"
androidx-compose-runtime = "1.4.0"
androidx-compose-ui = "1.4.0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ private fun View.generateBitmap(
debugLog {
e.stackTraceToString()
}
// Material3 ModalBottomSheetWindow doesn't have mWindow field.
nullableWindow = null
}
}
if (nullableWindow != null) {
Expand All @@ -123,7 +125,8 @@ private fun View.generateBitmap(
}
} else {
println(
"View.captureToImage Could not find window for view. Falling back to View#draw instead of PixelCopy"
"View.captureToImage Could not find window for view. Falling back to View#draw instead of PixelCopy" +
"(If you are using Material3 dialogs, this is expected).",
)
generateBitmapFromDraw(destBitmap, bitmapFuture)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,35 @@ import androidx.appcompat.app.AlertDialog
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.material.AlertDialog
import androidx.compose.material.Button
import androidx.compose.material.Icon
import androidx.compose.material.Text
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Add
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.ExtendedFloatingActionButton
import androidx.compose.material3.ModalBottomSheet
import androidx.compose.material3.Scaffold
import androidx.compose.material3.rememberModalBottomSheetState
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.test.hasText
import androidx.compose.ui.test.junit4.createComposeRule
import androidx.compose.ui.test.performClick
import androidx.test.core.app.ActivityScenario
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.github.takahirom.roborazzi.*
import com.google.android.material.bottomsheet.BottomSheetDialogFragment
import kotlinx.coroutines.launch
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
Expand All @@ -38,7 +57,7 @@ class WindowCaptureTest {
fun composeDialog() {
composeTestRule.setContent {
Column(
modifier = androidx.compose.ui.Modifier
modifier = Modifier
.background(Color.Cyan)
.fillMaxSize()
) {
Expand All @@ -60,11 +79,69 @@ class WindowCaptureTest {
captureScreenRoboImage()
}


@OptIn(ExperimentalMaterial3Api::class)
@Test
fun composeBottomSheet() {
composeTestRule.setContent {
Column(
modifier = Modifier
.background(Color.Cyan)
.fillMaxSize()
) {
val sheetState = rememberModalBottomSheetState()
val scope = rememberCoroutineScope()
var showBottomSheet by remember { mutableStateOf(false) }
Scaffold(
floatingActionButton = {
ExtendedFloatingActionButton(
text = { Text("Show bottom sheet") },
icon = { Icon(Icons.Filled.Add, contentDescription = "") },
onClick = {
showBottomSheet = true
}
)
}
) { contentPadding ->
// Screen content
Text(text = "Under the dialog")

if (showBottomSheet) {
ModalBottomSheet(
onDismissRequest = {
showBottomSheet = false
},
sheetState = sheetState
) {
// Sheet content
Button(onClick = {
scope.launch { sheetState.hide() }.invokeOnCompletion {
if (!sheetState.isVisible) {
showBottomSheet = false
}
}
}) {
Text(modifier = Modifier.padding(contentPadding), text = "Hide bottom sheet")
}
}
}
}
}
}

composeTestRule
.onNode(hasText("Show bottom sheet"), true)
.performClick()
composeTestRule.waitForIdle()

captureScreenRoboImage()
}

@Test
fun androidDialog() {
composeTestRule.setContent {
Column(
modifier = androidx.compose.ui.Modifier
modifier = Modifier
.background(Color.Cyan)
.fillMaxSize()
) {
Expand All @@ -88,7 +165,7 @@ class WindowCaptureTest {
fun noDialog() {
composeTestRule.setContent {
Column(
modifier = androidx.compose.ui.Modifier
modifier = Modifier
.background(Color.Cyan)
.fillMaxSize()
) {
Expand All @@ -103,7 +180,7 @@ class WindowCaptureTest {
fun dump() {
composeTestRule.setContent {
Column(
modifier = androidx.compose.ui.Modifier
modifier = Modifier
.background(Color.Cyan)
.fillMaxSize()
) {
Expand Down Expand Up @@ -131,9 +208,11 @@ class WindowCaptureTest {
}
}

captureScreenRoboImage(roborazziOptions = RoborazziOptions(
captureType = RoborazziOptions.CaptureType.Dump(),
))
captureScreenRoboImage(
roborazziOptions = RoborazziOptions(
captureType = RoborazziOptions.CaptureType.Dump(),
)
)
}
}

Expand All @@ -155,6 +234,7 @@ class FragmentActivityWindowCaptureTest {
})
}
}

@Test
fun bottomSheetDialog() {
ROBORAZZI_DEBUG = true
Expand Down

0 comments on commit 53c63d4

Please sign in to comment.