|
| 1 | +package io.sentry.uitest.android |
| 2 | + |
| 3 | +import androidx.lifecycle.Lifecycle |
| 4 | +import androidx.test.core.app.launchActivity |
| 5 | +import io.sentry.SentryOptions |
| 6 | +import leakcanary.LeakAssertions |
| 7 | +import leakcanary.LeakCanary |
| 8 | +import org.awaitility.kotlin.await |
| 9 | +import org.hamcrest.CoreMatchers.`is` |
| 10 | +import org.junit.Assume.assumeThat |
| 11 | +import org.junit.Before |
| 12 | +import shark.AndroidReferenceMatchers |
| 13 | +import shark.IgnoredReferenceMatcher |
| 14 | +import shark.ReferencePattern |
| 15 | +import java.util.concurrent.atomic.AtomicBoolean |
| 16 | +import kotlin.test.Test |
| 17 | + |
| 18 | +class ReplayTest : BaseUiTest() { |
| 19 | + |
| 20 | + @Before |
| 21 | + fun setup() { |
| 22 | + // we can't run on GH actions emulator, because they don't allow capturing screenshots properly |
| 23 | + @Suppress("KotlinConstantConditions") |
| 24 | + assumeThat( |
| 25 | + BuildConfig.ENVIRONMENT != "github", |
| 26 | + `is`(true) |
| 27 | + ) |
| 28 | + } |
| 29 | + |
| 30 | + @Test |
| 31 | + fun composeReplayDoesNotLeak() { |
| 32 | + val sent = AtomicBoolean(false) |
| 33 | + |
| 34 | + LeakCanary.config = LeakCanary.config.copy( |
| 35 | + referenceMatchers = AndroidReferenceMatchers.appDefaults + |
| 36 | + listOf( |
| 37 | + IgnoredReferenceMatcher( |
| 38 | + ReferencePattern.InstanceFieldPattern( |
| 39 | + "com.saucelabs.rdcinjector.testfairy.TestFairyEventQueue", |
| 40 | + "context" |
| 41 | + ) |
| 42 | + ), |
| 43 | + // Seems like a false-positive returned by LeakCanary when curtains is used in |
| 44 | + // the host application (LeakCanary uses it itself internally). We use kind of |
| 45 | + // the same approach which possibly clashes with LeakCanary's internal state. |
| 46 | + // Only the case when replay is enabled. |
| 47 | + // TODO: check if it's actually a leak on our side, or a false-positive and report to LeakCanary's github issue tracker |
| 48 | + IgnoredReferenceMatcher( |
| 49 | + ReferencePattern.InstanceFieldPattern( |
| 50 | + "curtains.internal.RootViewsSpy", |
| 51 | + "delegatingViewList" |
| 52 | + ) |
| 53 | + ) |
| 54 | + ) + ('a'..'z').map { char -> |
| 55 | + IgnoredReferenceMatcher( |
| 56 | + ReferencePattern.StaticFieldPattern( |
| 57 | + "com.testfairy.modules.capture.TouchListener", |
| 58 | + "$char" |
| 59 | + ) |
| 60 | + ) |
| 61 | + } |
| 62 | + ) |
| 63 | + |
| 64 | + val activityScenario = launchActivity<ComposeActivity>() |
| 65 | + activityScenario.moveToState(Lifecycle.State.RESUMED) |
| 66 | + |
| 67 | + initSentry { |
| 68 | + it.experimental.sessionReplay.sessionSampleRate = 1.0 |
| 69 | + |
| 70 | + it.beforeSendReplay = |
| 71 | + SentryOptions.BeforeSendReplayCallback { event, _ -> |
| 72 | + sent.set(true) |
| 73 | + event |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + // wait until first segment is being sent |
| 78 | + await.untilTrue(sent) |
| 79 | + |
| 80 | + activityScenario.moveToState(Lifecycle.State.DESTROYED) |
| 81 | + |
| 82 | + LeakAssertions.assertNoLeaks() |
| 83 | + } |
| 84 | +} |
0 commit comments