Repository navigation
Expand file tree
/
Copy pathReplaySnapshotTest.kt
More file actions
70 lines (61 loc) · 2.52 KB
/
Copy pathReplaySnapshotTest.kt
File metadata and controls
70 lines (61 loc) · 2.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package io.sentry.uitest.android
import android.graphics.Bitmap
import android.os.Environment
import androidx.lifecycle.Lifecycle
import androidx.test.core.app.launchActivity
import io.sentry.SentryReplayOptions
import io.sentry.TypeCheckHint
import java.io.File
import java.util.concurrent.CopyOnWriteArraySet
import java.util.concurrent.CountDownLatch
import java.util.concurrent.TimeUnit
import kotlin.test.Test
import kotlin.test.assertTrue
import org.junit.Before
class ReplaySnapshotTest : BaseUiTest() {
@Before
fun setup() {
// crash on swallowed Compose masking errors (e.g. broken obfuscated internals) so regressions
// fail this on-device test instead of silently under-masking (see SentryReplayDebug)
System.setProperty("io.sentry.replay.compose.fail-fast", "true")
}
@Test
fun captureComposeReplayFrameSnapshots() {
val snapshotsDir =
File(
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS),
// pulled and uploaded by scripts/test-ui-android.sh in CI
"sentry_replay_snapshots",
)
.apply {
deleteRecursively()
mkdirs()
}
val frameReceived = CountDownLatch(1)
val capturedScreens = CopyOnWriteArraySet<String>()
val activityScenario = launchActivity<ComposeActivity>()
activityScenario.moveToState(Lifecycle.State.RESUMED)
initSentry {
it.sessionReplay.sessionSampleRate = 1.0
it.sessionReplay.frameObserver =
SentryReplayOptions.ReplayFrameObserver { hint, frameTimestamp, screenName ->
val bitmap =
hint.getAs(TypeCheckHint.REPLAY_FRAME_BITMAP, Bitmap::class.java)
?: return@ReplayFrameObserver
val name = screenName ?: "unknown"
if (capturedScreens.add(name)) {
val file = File(snapshotsDir, "${name}_$frameTimestamp.png")
file.outputStream().use { out -> bitmap.compress(Bitmap.CompressFormat.PNG, 100, out) }
}
bitmap.recycle()
frameReceived.countDown()
}
}
assertTrue(frameReceived.await(10, TimeUnit.SECONDS), "Expected at least one replay frame")
assertTrue(capturedScreens.isNotEmpty(), "Expected at least one screen captured")
val files = snapshotsDir.listFiles()?.filter { it.extension == "png" } ?: emptyList()
assertTrue(files.isNotEmpty(), "Expected snapshot PNG files on disk")
assertTrue(files.all { it.length() > 0 }, "Snapshot files should not be empty")
activityScenario.moveToState(Lifecycle.State.DESTROYED)
}
}