|
1 | 1 | package io.sentry.uitest.android |
2 | 2 |
|
| 3 | +import android.graphics.Bitmap |
3 | 4 | import androidx.lifecycle.Lifecycle |
4 | 5 | import androidx.test.core.app.launchActivity |
5 | 6 | import io.sentry.SentryOptions |
| 7 | +import io.sentry.SentryReplayOptions |
| 8 | +import io.sentry.TypeCheckHint |
| 9 | +import java.util.concurrent.CountDownLatch |
| 10 | +import java.util.concurrent.TimeUnit |
6 | 11 | import java.util.concurrent.atomic.AtomicBoolean |
| 12 | +import java.util.concurrent.atomic.AtomicReference |
7 | 13 | import kotlin.test.Test |
| 14 | +import kotlin.test.assertEquals |
| 15 | +import kotlin.test.assertTrue |
8 | 16 | import leakcanary.LeakAssertions |
9 | 17 | import leakcanary.LeakCanary |
10 | 18 | import org.awaitility.kotlin.await |
@@ -81,4 +89,52 @@ class ReplayTest : BaseUiTest() { |
81 | 89 |
|
82 | 90 | LeakAssertions.assertNoLeaks() |
83 | 91 | } |
| 92 | + |
| 93 | + @Test |
| 94 | + fun composeCoilImageIsMasked() { |
| 95 | + val frame = AtomicReference<Bitmap>() |
| 96 | + val frameReceived = CountDownLatch(1) |
| 97 | + |
| 98 | + val activityScenario = launchActivity<ComposeActivity>() |
| 99 | + activityScenario.moveToState(Lifecycle.State.RESUMED) |
| 100 | + // an image that hasn't loaded yet is empty, and would look masked |
| 101 | + assertTrue(ComposeActivity.imageLoaded.await(10, TimeUnit.SECONDS), "Coil image didn't load") |
| 102 | + |
| 103 | + initSentry { |
| 104 | + it.sessionReplay.sessionSampleRate = 1.0 |
| 105 | + it.sessionReplay.frameObserver = SentryReplayOptions.ReplayFrameObserver { hint, _, _ -> |
| 106 | + val bitmap = hint.getAs(TypeCheckHint.REPLAY_FRAME_BITMAP, Bitmap::class.java) |
| 107 | + if (bitmap != null && frame.compareAndSet(null, bitmap)) { |
| 108 | + frameReceived.countDown() |
| 109 | + } else { |
| 110 | + bitmap?.recycle() |
| 111 | + } |
| 112 | + } |
| 113 | + } |
| 114 | + assertTrue(frameReceived.await(10, TimeUnit.SECONDS), "Expected a replay frame") |
| 115 | + |
| 116 | + var rootWidth = 0 |
| 117 | + var rootHeight = 0 |
| 118 | + activityScenario.onActivity { |
| 119 | + rootWidth = it.window.decorView.width |
| 120 | + rootHeight = it.window.decorView.height |
| 121 | + } |
| 122 | + val bitmap = frame.get() |
| 123 | + val scaleX = bitmap.width / rootWidth.toFloat() |
| 124 | + val scaleY = bitmap.height / rootHeight.toFloat() |
| 125 | + val bounds = ComposeActivity.imageBounds |
| 126 | + // the inner half of the image, away from the edges of the scaled mask |
| 127 | + val colors = mutableSetOf<Int>() |
| 128 | + for (i in 1..9) { |
| 129 | + for (j in 1..9) { |
| 130 | + val x = (bounds.left + bounds.width * (0.25f + i * 0.05f)) * scaleX |
| 131 | + val y = (bounds.top + bounds.height * (0.25f + j * 0.05f)) * scaleY |
| 132 | + colors += bitmap.getPixel(x.toInt(), y.toInt()) |
| 133 | + } |
| 134 | + } |
| 135 | + bitmap.recycle() |
| 136 | + activityScenario.moveToState(Lifecycle.State.DESTROYED) |
| 137 | + |
| 138 | + assertEquals(1, colors.size, "Coil image isn't masked in the replay frame") |
| 139 | + } |
84 | 140 | } |
0 commit comments