Skip to content

Commit ae8b1a2

Browse files
authored
Extend tests to ensure default integrations are registered (#4453)
* Extend tests to ensure default integrations are registered * Only check for NDK if it's bundled with the app
1 parent 0ca0884 commit ae8b1a2

File tree

2 files changed

+75
-0
lines changed

2 files changed

+75
-0
lines changed

sentry-android-integration-tests/sentry-uitest-android/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ android {
6161
isMinifyEnabled = true
6262
signingConfig = signingConfigs.getByName("debug")
6363
proguardFiles(getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro")
64+
testProguardFiles("proguard-rules.pro")
6465
}
6566
getByName("release") {
6667
isMinifyEnabled = true

sentry-android-integration-tests/sentry-uitest-android/src/androidTest/java/io/sentry/uitest/android/SdkInitTests.kt

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ import androidx.test.core.app.launchActivity
55
import androidx.test.ext.junit.runners.AndroidJUnit4
66
import io.sentry.ProfilingTraceData
77
import io.sentry.Sentry
8+
import io.sentry.SentryIntegrationPackageStorage
89
import io.sentry.android.core.AndroidLogger
10+
import io.sentry.android.core.CurrentActivityHolder
11+
import io.sentry.android.core.NdkIntegration
912
import io.sentry.android.core.SentryAndroidOptions
1013
import io.sentry.assertEnvelopeTransaction
1114
import io.sentry.protocol.SentryTransaction
@@ -15,6 +18,7 @@ import org.junit.runner.RunWith
1518
import shark.AndroidReferenceMatchers
1619
import shark.IgnoredReferenceMatcher
1720
import shark.ReferencePattern
21+
import java.util.concurrent.CountDownLatch
1822
import kotlin.test.Test
1923
import kotlin.test.assertEquals
2024
import kotlin.test.assertTrue
@@ -215,4 +219,74 @@ class SdkInitTests : BaseUiTest() {
215219

216220
LeakAssertions.assertNoLeaks()
217221
}
222+
223+
@Test
224+
fun foregroundInitInstallsDefaultIntegrations() {
225+
val activityScenario = launchActivity<ComposeActivity>()
226+
activityScenario.moveToState(Lifecycle.State.RESUMED)
227+
activityScenario.onActivity { activity ->
228+
// Our SentryInitProvider does not run in this test
229+
// so we need to set the current activity manually
230+
CurrentActivityHolder.getInstance().setActivity(activity)
231+
initSentry(false) { options: SentryAndroidOptions ->
232+
options.tracesSampleRate = 1.0
233+
options.profilesSampleRate = 1.0
234+
}
235+
}
236+
activityScenario.moveToState(Lifecycle.State.DESTROYED)
237+
assertDefaultIntegrations()
238+
}
239+
240+
@Test
241+
fun backgroundInitInstallsDefaultIntegrations() {
242+
val initLatch = CountDownLatch(1)
243+
244+
val activityScenario = launchActivity<ComposeActivity>()
245+
activityScenario.moveToState(Lifecycle.State.RESUMED)
246+
activityScenario.onActivity { activity ->
247+
// Our SentryInitProvider does not run in this test
248+
// so we need to set the current activity manually
249+
CurrentActivityHolder.getInstance().setActivity(activity)
250+
Thread {
251+
initSentry(false) { options: SentryAndroidOptions ->
252+
options.tracesSampleRate = 1.0
253+
options.profilesSampleRate = 1.0
254+
}
255+
initLatch.countDown()
256+
}.start()
257+
}
258+
initLatch.await()
259+
260+
activityScenario.moveToState(Lifecycle.State.DESTROYED)
261+
262+
assertDefaultIntegrations()
263+
}
264+
265+
private fun assertDefaultIntegrations() {
266+
val integrations = mutableListOf(
267+
"UncaughtExceptionHandler",
268+
"ShutdownHook",
269+
"SendCachedEnvelope",
270+
"AppLifecycle",
271+
"EnvelopeFileObserver",
272+
"AnrV2",
273+
"ActivityLifecycle",
274+
"ActivityBreadcrumbs",
275+
"UserInteraction",
276+
"AppComponentsBreadcrumbs",
277+
"NetworkBreadcrumbs"
278+
)
279+
280+
// NdkIntegration is not always available, so we check for its presence
281+
try {
282+
Class.forName(NdkIntegration.SENTRY_NDK_CLASS_NAME)
283+
integrations.add("Ndk")
284+
} catch (_: ClassNotFoundException) {
285+
// ignored, in case the app is build without NDK support
286+
}
287+
288+
for (integration in integrations) {
289+
assertTrue(SentryIntegrationPackageStorage.getInstance().integrations.contains(integration), "Integration $integration was expected, but was not registered")
290+
}
291+
}
218292
}

0 commit comments

Comments
 (0)