@@ -2,6 +2,7 @@ package io.sentry.android.core
22
33import android.os.Bundle
44import androidx.test.ext.junit.runners.AndroidJUnit4
5+ import com.google.common.truth.Truth.assertThat
56import io.sentry.Breadcrumb
67import io.sentry.Sentry
78import io.sentry.SentryLevel
@@ -15,6 +16,7 @@ import kotlin.test.Test
1516import kotlin.test.assertEquals
1617import kotlin.test.assertTrue
1718import org.junit.runner.RunWith
19+ import org.robolectric.shadows.ShadowLog
1820
1921@RunWith(AndroidJUnit4 ::class )
2022class SentryLogcatAdapterTest {
@@ -26,16 +28,22 @@ class SentryLogcatAdapterTest {
2628 val breadcrumbs = mutableListOf<Breadcrumb >()
2729 val logs = mutableListOf<SentryLogEvent >()
2830
29- fun initSut (options : Sentry .OptionsConfiguration <SentryAndroidOptions >? = null) {
30- val metadata =
31- Bundle ().apply { putString(ManifestMetadataReader .DSN , " https://key@sentry.io/123" ) }
31+ fun initSut (
32+ enableLogcatLogs : Boolean? = true,
33+ metadata : Bundle = Bundle (),
34+ options : Sentry .OptionsConfiguration <SentryAndroidOptions >? = null,
35+ ) {
36+ metadata.putString(ManifestMetadataReader .DSN , " https://key@sentry.io/123" )
3237 val mockContext = ContextUtilsTestHelper .mockMetaData(metaData = metadata)
3338 initForTest(mockContext) {
3439 it.beforeBreadcrumb = SentryOptions .BeforeBreadcrumbCallback { breadcrumb, _ ->
3540 breadcrumbs.add(breadcrumb)
3641 breadcrumb
3742 }
3843 it.logs.isEnabled = true
44+ if (enableLogcatLogs != null ) {
45+ it.isEnableLogcatLogs = enableLogcatLogs
46+ }
3947 it.logs.beforeSend =
4048 SentryOptions .Logs .BeforeSendLogCallback { logEvent ->
4149 logs.add(logEvent)
@@ -55,6 +63,37 @@ class SentryLogcatAdapterTest {
5563 Sentry .close()
5664 fixture.breadcrumbs.clear()
5765 fixture.logs.clear()
66+ ShadowLog .clear()
67+ }
68+
69+ @Test
70+ fun `Logcat logs are disabled by default while breadcrumbs and Android Log remain enabled` () {
71+ fixture.initSut(enableLogcatLogs = null )
72+
73+ SentryLogcatAdapter .d(tag, commonMsg)
74+
75+ assertThat(fixture.logs).isEmpty()
76+ assertThat(fixture.breadcrumbs).hasSize(1 )
77+ assertThat(ShadowLog .getLogs().any { it.tag == tag && it.msg == commonMsg }).isTrue()
78+ }
79+
80+ @Test
81+ fun `Logcat logs can be enabled through Android options` () {
82+ fixture.initSut(enableLogcatLogs = true )
83+
84+ SentryLogcatAdapter .d(tag, commonMsg)
85+
86+ assertThat(fixture.logs).hasSize(1 )
87+ }
88+
89+ @Test
90+ fun `Logcat logs can be enabled through manifest metadata` () {
91+ val metadata = Bundle ().apply { putBoolean(ManifestMetadataReader .ENABLE_LOGCAT_LOGS , true ) }
92+ fixture.initSut(enableLogcatLogs = null , metadata = metadata)
93+
94+ SentryLogcatAdapter .d(tag, commonMsg)
95+
96+ assertThat(fixture.logs).hasSize(1 )
5897 }
5998
6099 @Test
0 commit comments