Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[andr][replay] Handle hybrid AndroidViews inside Compose #126

Merged
merged 2 commits into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
package io.bitdrift.gradletestapp

import android.view.ViewGroup
import android.widget.Button
import android.widget.TextView
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.Box
Expand Down Expand Up @@ -324,9 +325,9 @@ class ComposeReplayTest {
Column {
AndroidView(::TextView) {
it.layoutParams = ViewGroup.LayoutParams(200, 80)
it.text = "Baguette Avec Fromage"
it.text = "hi"
}
AndroidView(::TextView) {
AndroidView(::Button) {
it.layoutParams = ViewGroup.LayoutParams(200, 80)
it.text = "short"
}
Expand All @@ -337,12 +338,10 @@ class ComposeReplayTest {
val capture = verifyReplayScreen(viewCount = 10)
// Column
assertThat(capture).contains(ReplayRect(ReplayType.View, 0, 88, 200, 160))
// AndroidView Top
// TODO(murki): The ReplayRect should be a Label
assertThat(capture).contains(ReplayRect(ReplayType.View, 0, 88, 200, 80))
// AndroidView Bottom
// TODO(murki): The ReplayRect should be a Label
assertThat(capture).contains(ReplayRect(ReplayType.View, 0, 168, 200, 80))
// AndroidView w/TextView Top (width reflects the text length
assertThat(capture).contains(ReplayRect(ReplayType.Label, 3, 88, 33, 37))
// AndroidView w/Button Bottom
assertThat(capture).contains(ReplayRect(ReplayType.Button, 0, 168, 200, 80))
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ internal sealed class ScannableView {
/** The children of this view. */
abstract val children: Sequence<ScannableView>

class AndroidView(val view: View, private val skipReplayComposeViews: Boolean) : ScannableView() {
class AndroidView(val view: View, skipReplayComposeViews: Boolean) : ScannableView() {
override val displayName: String get() = view::class.java.simpleName
override val children: Sequence<ScannableView> =
view.scannableChildren(skipReplayComposeViews)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ package io.bitdrift.capture.replay.internal.compose

import android.view.View
import androidx.compose.ui.ExperimentalComposeUiApi
import androidx.compose.ui.InternalComposeUiApi
import androidx.compose.ui.geometry.Rect
import androidx.compose.ui.platform.AndroidComposeView
import androidx.compose.ui.semantics.Role
Expand All @@ -25,6 +26,7 @@ import io.bitdrift.capture.replay.ReplayType
import io.bitdrift.capture.replay.SessionReplayController
import io.bitdrift.capture.replay.internal.ReplayRect
import io.bitdrift.capture.replay.internal.ScannableView
import io.bitdrift.capture.replay.internal.compose.ComposeTreeParser.unclippedGlobalBounds

internal object ComposeTreeParser {
internal val View.mightBeComposeView: Boolean
Expand All @@ -45,8 +47,8 @@ internal object ComposeTreeParser {
return rootNode.toScannableView()
}

@OptIn(ExperimentalComposeUiApi::class)
private fun SemanticsNode.toScannableView(): ScannableView.ComposeView {
@OptIn(ExperimentalComposeUiApi::class, InternalComposeUiApi::class)
private fun SemanticsNode.toScannableView(): ScannableView {
val notAttachedOrPlaced = !this.layoutNode.isPlaced || !this.layoutNode.isAttached
val isVisible = !this.isTransparent && !unmergedConfig.contains(SemanticsProperties.InvisibleToUser)
val type = if (notAttachedOrPlaced) {
Expand All @@ -56,6 +58,16 @@ internal object ComposeTreeParser {
} else {
this.unmergedConfig.toReplayType()
}

// Handle hybrid interop AndroidViews inside Compose elements
val interopAndroidView = this.layoutNode.getInteropView()
if (type == ReplayType.View && interopAndroidView != null) {
return ScannableView.AndroidView(
view = interopAndroidView,
skipReplayComposeViews = false,
)
}

return ScannableView.ComposeView(
replayRect = ReplayRect(
type = type,
Expand Down
Loading