Skip to content
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
@@ -1,4 +1,4 @@
name: Compose UI Matrix
name: Session Replay Dependency Matrix

on:
push:
Expand All @@ -11,10 +11,20 @@ concurrency:
cancel-in-progress: true

jobs:
compose-ui-matrix:
replay-dependency-matrix:
timeout-minutes: 30
runs-on: ubuntu-latest
name: Compose UI Matrix
name: ${{ matrix.name }} Matrix
strategy:
fail-fast: false
matrix:
include:
# Runs every version in gradle/compose-ui-versions.json
- name: Compose UI
task: composeUiTest
# Runs every version in gradle/coil3-versions.json
- name: Coil 3
task: coil3Test

steps:
- name: Checkout Repo
Expand All @@ -29,15 +39,15 @@ jobs:
- name: Setup Gradle
uses: gradle/actions/setup-gradle@8da46b539cd8fe6ab27ca75128919a18d757604c # v6.4.1

# Runs every version in gradle/compose-ui-versions.json; --continue reports each failing one.
# --continue reports each failing version.
- name: Run Session Replay unit tests
run: ./gradlew :sentry-android-replay:composeUiTest --continue
run: ./gradlew :sentry-android-replay:${{ matrix.task }} --continue

- name: Upload test results
if: always()
uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7
with:
name: test-results-compose-ui
name: test-results-${{ matrix.task }}
path: |
sentry-android-replay/build/reports/*
sentry-android-replay/build/test-results/**/*.xml
Expand All @@ -46,7 +56,7 @@ jobs:
uses: phoenix-actions/test-reporting@7317eea6e13c47348dd0bb318669485157c518d6 # pin@v16
if: always()
with:
name: JUnit Compose UI
name: JUnit ${{ matrix.name }}
path: |
sentry-android-replay/build/test-results/**/*.xml
reporter: java-junit
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

### Fixes

- Session Replay: Mask Compose images loaded with Coil 3.1+ `AsyncImage` when `maskAllImages` is enabled ([#6759](https://github.com/getsentry/sentry-java/pull/6759))
- Profiling: No longer crash on API 35+ devices whose framework can't load `ProfilingManager`. Perfetto profiling is turned off on those devices instead ([#6742](https://github.com/getsentry/sentry-java/pull/6742))

### Dependencies
Expand Down
5 changes: 5 additions & 0 deletions gradle/coil3-versions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[
"3.0.4",
"3.1.0",
"3.6.3"
]
2 changes: 1 addition & 1 deletion gradle/compose-ui-versions.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[
{ "version": "1.8.3", "pin": { "androidx.compose.material3": "1.3.2" } },
{ "version": "1.8.3", "pin": { "androidx.compose.material3": "1.3.2", "io.coil-kt.coil3": "3.3.0" } },
"1.13.0",
"1.14.0-alpha01"
]
1 change: 1 addition & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ async-profiler-jfr-converter = { module = "tools.profiler:jfr-converter", versio
caffeine = { module = "com.github.ben-manes.caffeine:caffeine" }
caffeine-jcache = { module = "com.github.ben-manes.caffeine:jcache", version = "3.3.1" }
coil-compose = { module = "io.coil-kt:coil-compose", version = "2.7.0" }
coil3-compose = { module = "io.coil-kt.coil3:coil-compose", version = "3.4.0" }
commons-compress = {module = "org.apache.commons:commons-compress", version = "1.28.0"}
context-propagation = { module = "io.micrometer:context-propagation", version = "1.1.0" }
errorprone-core = { module = "com.google.errorprone:error_prone_core", version = "2.52.0" }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ android {
namespace = "io.sentry.uitest.android"

defaultConfig {
minSdk = libs.versions.minSdk.get().toInt()
// Compose UI 1.10+ requires minSdk 23; the Sentry SDK still supports 21.
minSdk = 23
targetSdk = libs.versions.compileSdk.get().toInt()
versionCode = 1
versionName = "1.0.0"
Expand Down Expand Up @@ -109,6 +110,9 @@ dependencies {
implementation(libs.androidx.activity.compose)
implementation(libs.androidx.compose.foundation)
implementation(libs.androidx.compose.material3)
implementation(libs.coil3.compose)
// Compose UI 1.10+ lets R8 rename modifier elements, so the replay masking keep rules get tested
implementation(libs.androidx.compose.ui.replay)
implementation(libs.androidx.constraintlayout)
implementation(libs.androidx.core)
implementation(libs.androidx.recyclerview)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
package io.sentry.uitest.android

import android.graphics.Bitmap
import androidx.lifecycle.Lifecycle
import androidx.test.core.app.launchActivity
import io.sentry.SentryOptions
import io.sentry.SentryReplayOptions
import io.sentry.TypeCheckHint
import java.util.concurrent.CountDownLatch
import java.util.concurrent.TimeUnit
import java.util.concurrent.atomic.AtomicBoolean
import java.util.concurrent.atomic.AtomicReference
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertTrue
import leakcanary.LeakAssertions
import leakcanary.LeakCanary
import org.awaitility.kotlin.await
Expand Down Expand Up @@ -81,4 +89,52 @@ class ReplayTest : BaseUiTest() {

LeakAssertions.assertNoLeaks()
}

@Test
fun composeCoilImageIsMasked() {
val frame = AtomicReference<Bitmap>()
val frameReceived = CountDownLatch(1)

val activityScenario = launchActivity<ComposeActivity>()
activityScenario.moveToState(Lifecycle.State.RESUMED)
// an image that hasn't loaded yet is empty, and would look masked
assertTrue(ComposeActivity.imageLoaded.await(10, TimeUnit.SECONDS), "Coil image didn't load")

initSentry {
it.sessionReplay.sessionSampleRate = 1.0
it.sessionReplay.frameObserver = SentryReplayOptions.ReplayFrameObserver { hint, _, _ ->
val bitmap = hint.getAs(TypeCheckHint.REPLAY_FRAME_BITMAP, Bitmap::class.java)
if (bitmap != null && frame.compareAndSet(null, bitmap)) {
frameReceived.countDown()
} else {
bitmap?.recycle()
}
}
}
assertTrue(frameReceived.await(10, TimeUnit.SECONDS), "Expected a replay frame")

var rootWidth = 0
var rootHeight = 0
activityScenario.onActivity {
rootWidth = it.window.decorView.width
rootHeight = it.window.decorView.height
}
val bitmap = frame.get()
val scaleX = bitmap.width / rootWidth.toFloat()
val scaleY = bitmap.height / rootHeight.toFloat()
val bounds = ComposeActivity.imageBounds
// the inner half of the image, away from the edges of the scaled mask
val colors = mutableSetOf<Int>()
for (i in 1..9) {
for (j in 1..9) {
val x = (bounds.left + bounds.width * (0.25f + i * 0.05f)) * scaleX
val y = (bounds.top + bounds.height * (0.25f + j * 0.05f)) * scaleY
colors += bitmap.getPixel(x.toInt(), y.toInt())
}
}
bitmap.recycle()
activityScenario.moveToState(Lifecycle.State.DESTROYED)

assertEquals(1, colors.size, "Coil image isn't masked in the replay frame")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@
android:label="Sentry End2End Tests"
android:largeHeap="true"
android:icon="@android:mipmap/sym_def_app_icon"
android:usesCleartextTraffic="true"
tools:targetApi="m">
android:usesCleartextTraffic="true">
<!-- android:usesCleartextTraffic="true" is needed to send data to http://localhost... (the mock relay server) -->

<activity android:name=".EmptyActivity" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,38 @@ import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.geometry.Rect
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.layout.boundsInWindow
import androidx.compose.ui.layout.onGloballyPositioned
import androidx.compose.ui.platform.testTag
import androidx.compose.ui.unit.dp
import androidx.core.graphics.createBitmap
import androidx.core.graphics.set
import coil3.compose.AsyncImage
import java.util.concurrent.CountDownLatch

class ComposeActivity : AppCompatActivity() {
companion object {
private const val ITEM_COUNT: Int = 100

@Volatile var imageLoaded = CountDownLatch(1)
@Volatile var imageBounds = Rect.Zero
}

// two colors, so a replay frame shows whether the image is masked: a mask is one color
private val image =
createBitmap(2, 1).apply {
this[0, 0] = android.graphics.Color.BLACK
this[1, 0] = android.graphics.Color.WHITE
}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
imageLoaded = CountDownLatch(1)

setContent {
MaterialTheme {
Expand All @@ -42,6 +62,18 @@ class ComposeActivity : AppCompatActivity() {
.testTag("button_login"),
) {
Text("Login")
// at the top, away from the center that the UI tests tap
AsyncImage(
model = image,
contentDescription = null,
contentScale = ContentScale.FillBounds,
onSuccess = { imageLoaded.countDown() },
modifier =
Modifier.align(Alignment.TopCenter)
.padding(top = 48.dp)
.size(96.dp)
.onGloballyPositioned { imageBounds = it.boundsInWindow() },
)
}
}
items(ITEM_COUNT) {
Expand Down
9 changes: 7 additions & 2 deletions sentry-android-replay/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,13 @@ dependencies {
testImplementation(libs.androidx.compose.foundation.layout)
testImplementation(libs.androidx.compose.material3)
testImplementation(libs.coil.compose)
testImplementation(libs.coil3.compose)
}

// Session Replay calls Compose internals, whose mangled JVM names change between Compose versions.
// .github/workflows/compose-ui-matrix.yml runs each version's task. The oldest entry pins material3
// to a version that runs on that Compose UI, because the catalog's material3 needs a newer one.
// .github/workflows/replay-dependency-matrix.yml runs each version's task. The oldest entry pins
// material3 and Coil 3 to versions that run on that Compose UI, because the catalog's need a newer
// one.
dependencyMatrix {
matrix(
name = "composeUi",
Expand All @@ -118,6 +120,9 @@ dependencyMatrix {
),
versionsFile = "compose-ui-versions.json",
)
// Compose image masking detects Coil's painter modifier by reflection, and Coil 3.1 changed it.
// Coil 2 is a different artifact and package, so it's covered by the default unit tests instead.
matrix(name = "coil3", groups = listOf("io.coil-kt.coil3"), versionsFile = "coil3-versions.json")
}

tasks.withType<Detekt>().configureEach {
Expand Down
1 change: 0 additions & 1 deletion sentry-android-replay/detekt-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,6 @@
<ID>MaxLineLength:ComposeMaskingOptionsTest.kt$ComposeMaskingOptionsTest$"Flaky: Robolectric intermittently reports zero bounds for nodes, causing isVisible=false and making the assertion non-deterministic"</ID>
<ID>NestedBlockDepth:CaptureStrategy.kt$CaptureStrategy.Companion$@Suppress("NewApi") private fun buildReplay( options: SentryOptions, video: File, currentReplayId: SentryId, segmentTimestamp: Date, segmentId: Int, height: Int, width: Int, frameCount: Int, frameRate: Int, videoDuration: Long, replayType: ReplayType, screenAtStart: String?, breadcrumbs: List&lt;Breadcrumb>, events: Deque&lt;RRWebEvent>, traceIds: List&lt;String>, segmentNames: List&lt;String>, ): ReplaySegment</ID>
<ID>NestedBlockDepth:DefaultReplayBreadcrumbConverter.kt$DefaultReplayBreadcrumbConverter$private fun Breadcrumb.toRRWebSpanEvent(): RRWebSpanEvent</ID>
<ID>NestedBlockDepth:Nodes.kt$internal fun LayoutNode.findPainter(): Painter?</ID>
<ID>NestedBlockDepth:Nodes.kt$internal fun LayoutNode.findTextColor(): Color?</ID>
<ID>NestedBlockDepth:ReplayGestureConverter.kt$ReplayGestureConverter$fun convert( event: MotionEvent, recorderConfig: ScreenshotRecorderConfig, ): List&lt;RRWebIncrementalSnapshotEvent>?</ID>
<ID>NestedBlockDepth:SimpleVideoEncoder.kt$SimpleVideoEncoder$private fun drainCodec(endOfStream: Boolean)</ID>
Expand Down
2 changes: 2 additions & 0 deletions sentry-android-replay/proguard-rules.pro
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
-keepclasseswithmembernames class * {
androidx.compose.ui.graphics.painter.Painter painter;
}
# Elements without a painter member, e.g. Coil 3.1+ ContentPainterElement, are masked by name only
-keepnames class **PainterElement
# Rules to detect Text colors and if they have Modifier.fillMaxWidth to later mask them
-dontwarn androidx.compose.ui.graphics.ColorProducer
-dontwarn androidx.compose.foundation.layout.FillElement
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,19 +83,28 @@ internal class ComposeTextLayout(internal val layout: TextLayoutResult) : TextLa
*
* That's not going to cover all cases, but probably 90%.
*
* Returns null if the node has no such modifier, otherwise whether the image should be masked (see
* [isMaskable]). If the modifier has no readable `painter` member, e.g. Coil 3.1+ keeps the painter
* on its Modifier.Node instead, we can't tell what it draws, so we treat it as maskable. For Coil
* that's the same result as reading it: its painter is always the `AsyncImagePainter` wrapper,
* which [isMaskable] can't tell apart from a remote image.
*
* We also add special proguard rules to keep the `Painter` class names and their `painter` member.
*/
internal fun LayoutNode.findPainter(): Painter? {
internal fun LayoutNode.isMaskableImage(): Boolean? {
val modifierInfos = getModifierInfo()
for (index in modifierInfos.indices) {
val modifier = modifierInfos[index].modifier
if (modifier::class.java.name.contains("Painter")) {
return try {
modifier::class.java.getDeclaredField("painter").apply { isAccessible = true }.get(modifier)
as? Painter
} catch (e: Throwable) {
null
}
// a lookup by name instead of getDeclaredField, which throws on every frame for Coil 3.1+
val painter =
modifier::class
.java
.declaredFields
.firstOrNull { it.name == "painter" }
?.apply { isAccessible = true }
?.get(modifier) as? Painter
return painter?.isMaskable() ?: true
}
}
return null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,8 @@ import io.sentry.android.replay.SentryReplayModifiers
import io.sentry.android.replay.util.ComposeTextLayout
import io.sentry.android.replay.util.SentryReplayDebug
import io.sentry.android.replay.util.boundsInWindow
import io.sentry.android.replay.util.findPainter
import io.sentry.android.replay.util.findTextColor
import io.sentry.android.replay.util.isMaskable
import io.sentry.android.replay.util.isMaskableImage
import io.sentry.android.replay.util.toOpaque
import io.sentry.android.replay.util.toRect
import io.sentry.android.replay.viewhierarchy.ViewHierarchyNode.GenericViewHierarchyNode
Expand Down Expand Up @@ -228,8 +227,8 @@ internal object ComposeViewHierarchyNode {
)
}
else -> {
val painter = node.findPainter()
if (painter != null) {
val isMaskableImage = node.isMaskableImage()
if (isMaskableImage != null) {
val shouldMask = isVisible && semantics.shouldMask(isImage = true, options)

parent?.setImportantForCaptureToAncestors(true)
Expand All @@ -243,7 +242,7 @@ internal object ComposeViewHierarchyNode {
parent = parent,
isVisible = isVisible,
isImportantForContentCapture = true,
shouldMask = shouldMask && painter.isMaskable(),
shouldMask = shouldMask && isMaskableImage,
visibleRect = visibleRect.toRect(),
)
} else {
Expand Down
Loading
Loading