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] Add support for ignoring Compose elements via Modifiers #129

Merged
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 @@ -45,6 +45,7 @@ import com.google.common.truth.Truth.assertThat
import io.bitdrift.capture.replay.ReplayCaptureMetrics
import io.bitdrift.capture.replay.ReplayPreviewClient
import io.bitdrift.capture.replay.ReplayType
import io.bitdrift.capture.replay.compose.CaptureModifier.captureIgnore
import io.bitdrift.capture.replay.internal.FilteredCapture
import io.bitdrift.capture.replay.internal.ReplayRect
import org.junit.Before
Expand Down Expand Up @@ -315,7 +316,49 @@ class ComposeReplayTest {

val capture = verifyReplayScreen(viewCount = 13)
assertThat(capture).contains(ReplayRect(ReplayType.Button, 0, 109, 351, 126))
assertThat(capture).contains(ReplayRect(ReplayType.Label, 28, 144, 295, 57))
assertThat(capture).contains(ReplayRect(ReplayType.Button, 0, 277, 224, 126))
assertThat(capture).contains(ReplayRect(ReplayType.Label, 45, 312, 135, 57))
}

@Test
fun textButtonIgnoreOneButtonOnly() {
composeRule.setContentWithExplicitRoot {
Column(Modifier.wrapContentWidth()) {
TextButton(onClick = {}) {
Text("Button Text")
}
TextButton(onClick = {}, modifier = Modifier.captureIgnore(ignoreSubTree = false)) {
Text("short")
}
}
}

val capture = verifyReplayScreen(viewCount = 13)
assertThat(capture).contains(ReplayRect(ReplayType.Button, 0, 109, 351, 126))
assertThat(capture).contains(ReplayRect(ReplayType.Label, 28, 144, 295, 57))
assertThat(capture).doesNotContain(ReplayRect(ReplayType.Button, 0, 277, 224, 126))
assertThat(capture).contains(ReplayRect(ReplayType.Label, 45, 312, 135, 57))
}

@Test
fun textButtonIgnoreOneFullTextButton() {
composeRule.setContentWithExplicitRoot {
Column(Modifier.wrapContentWidth()) {
TextButton(onClick = {}) {
Text("Button Text")
}
TextButton(onClick = {}, modifier = Modifier.captureIgnore(ignoreSubTree = true)) {
Text("short")
}
}
}

val capture = verifyReplayScreen(viewCount = 13)
assertThat(capture).contains(ReplayRect(ReplayType.Button, 0, 109, 351, 126))
assertThat(capture).contains(ReplayRect(ReplayType.Label, 28, 144, 295, 57))
assertThat(capture).doesNotContain(ReplayRect(ReplayType.Button, 0, 277, 224, 126))
assertThat(capture).doesNotContain(ReplayRect(ReplayType.Label, 45, 312, 135, 57))
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.viewinterop.AndroidView
import androidx.core.text.HtmlCompat
import io.bitdrift.capture.Capture
import io.bitdrift.capture.replay.compose.CaptureModifier.captureIgnore
import timber.log.Timber

@Composable
Expand Down Expand Up @@ -62,7 +62,7 @@ fun SecondScreen() {
)
ElevatedButton(
onClick = { Timber.w("Warning logged from Compose Screen") },
modifier = centerWithPaddingModifier.padding(top = normalSpacing)
modifier = centerWithPaddingModifier.padding(top = normalSpacing).captureIgnore(ignoreSubTree = false)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

example use

) {
Text("Log-a-log (Warning)")
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// capture-sdk - bitdrift's client SDK
// Copyright Bitdrift, Inc. All rights reserved.
//
// Use of this source code is governed by a source available license that can be found in the
// LICENSE file or at:
// https://polyformproject.org/wp-content/uploads/2020/06/PolyForm-Shield-1.0.0.txt

package io.bitdrift.capture.replay.compose

import androidx.compose.ui.Modifier
import androidx.compose.ui.semantics.SemanticsPropertyKey
import androidx.compose.ui.semantics.semantics

/**
* Compose Modifiers exposed by the Capture SDK.
*/
object CaptureModifier {

internal val CaptureIgnore = SemanticsPropertyKey<Boolean>(
name = "CaptureIgnoreModifier",
mergePolicy = { parentValue, _ ->
parentValue
},
)

/**
* Semantic Modifier that can be used to ignore elements of the Compose tree from being captured by Session Replay.
*/
@JvmStatic
fun Modifier.captureIgnore(ignoreSubTree: Boolean = false): Modifier {
return semantics(
properties = {
this[CaptureIgnore] = ignoreSubTree
},
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ import androidx.compose.ui.state.ToggleableState
import androidx.compose.ui.unit.toSize
import io.bitdrift.capture.replay.ReplayType
import io.bitdrift.capture.replay.SessionReplayController
import io.bitdrift.capture.replay.compose.CaptureModifier
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 @@ -50,9 +50,18 @@ internal object ComposeTreeParser {
@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 captureIgnoreSubTree = this.unmergedConfig.getOrNull(CaptureModifier.CaptureIgnore)
val isVisible = !this.isTransparent && !this.unmergedConfig.contains(SemanticsProperties.InvisibleToUser)
val type = if (notAttachedOrPlaced) {
return ScannableView.IgnoredComposeView
} else if (captureIgnoreSubTree != null) {
if (captureIgnoreSubTree) {
// short-circuit the entire sub-tree
return ScannableView.IgnoredComposeView
} else {
// just ignore this one element
ReplayType.Ignore
}
} else if (!isVisible) {
ReplayType.TransparentView
} else {
Expand Down
Loading