|
| 1 | +package io.sentry.android.core |
| 2 | + |
| 3 | +import android.os.Bundle |
| 4 | +import androidx.test.ext.junit.runners.AndroidJUnit4 |
| 5 | +import io.sentry.Breadcrumb |
| 6 | +import io.sentry.Sentry |
| 7 | +import io.sentry.SentryLevel |
| 8 | +import io.sentry.SentryOptions |
| 9 | +import org.junit.runner.RunWith |
| 10 | +import java.lang.RuntimeException |
| 11 | +import kotlin.test.BeforeTest |
| 12 | +import kotlin.test.Test |
| 13 | +import kotlin.test.assertEquals |
| 14 | + |
| 15 | +@RunWith(AndroidJUnit4::class) |
| 16 | +class SentryLogcatAdapterTest { |
| 17 | + private val breadcrumbs = mutableListOf<Breadcrumb>() |
| 18 | + private val tag = "my-tag" |
| 19 | + private val commonMsg = "SentryLogcatAdapter" |
| 20 | + private val throwable = RuntimeException("Test Exception") |
| 21 | + |
| 22 | + class Fixture { |
| 23 | + |
| 24 | + fun initSut( |
| 25 | + options: Sentry.OptionsConfiguration<SentryAndroidOptions>? = null |
| 26 | + ) { |
| 27 | + val metadata = Bundle().apply { |
| 28 | + putString(ManifestMetadataReader.DSN, "https://key@sentry.io/123") |
| 29 | + } |
| 30 | + val mockContext = ContextUtilsTest.mockMetaData(metaData = metadata) |
| 31 | + when { |
| 32 | + options != null -> SentryAndroid.init(mockContext, options) |
| 33 | + else -> SentryAndroid.init(mockContext) |
| 34 | + } |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + private val fixture = Fixture() |
| 39 | + |
| 40 | + @BeforeTest |
| 41 | + fun `set up`() { |
| 42 | + Sentry.close() |
| 43 | + AppStartState.getInstance().resetInstance() |
| 44 | + breadcrumbs.clear() |
| 45 | + |
| 46 | + fixture.initSut { |
| 47 | + it.beforeBreadcrumb = SentryOptions.BeforeBreadcrumbCallback { breadcrumb, _ -> |
| 48 | + breadcrumbs.add(breadcrumb) |
| 49 | + breadcrumb |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + SentryLogcatAdapter.v(tag, "$commonMsg verbose") |
| 54 | + SentryLogcatAdapter.i(tag, "$commonMsg info") |
| 55 | + SentryLogcatAdapter.d(tag, "$commonMsg debug") |
| 56 | + SentryLogcatAdapter.w(tag, "$commonMsg warning") |
| 57 | + SentryLogcatAdapter.e(tag, "$commonMsg error") |
| 58 | + SentryLogcatAdapter.wtf(tag, "$commonMsg wtf") |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + fun `verbose log message has expected content`() { |
| 63 | + val breadcrumb = breadcrumbs.find { it.level == SentryLevel.DEBUG && it.message?.contains("verbose") ?: false } |
| 64 | + assertEquals("Logcat", breadcrumb?.category) |
| 65 | + assertEquals(tag, breadcrumb?.data?.get("tag")) |
| 66 | + assert(breadcrumb?.message?.contains("verbose") == true) |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + fun `info log message has expected content`() { |
| 71 | + val breadcrumb = breadcrumbs.find { it.level == SentryLevel.INFO && it.message?.contains("info") ?: false } |
| 72 | + assertEquals("Logcat", breadcrumb?.category) |
| 73 | + assertEquals(tag, breadcrumb?.data?.get("tag")) |
| 74 | + assert(breadcrumb?.message?.contains("info") == true) |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + fun `debug log message has expected content`() { |
| 79 | + val breadcrumb = breadcrumbs.find { it.level == SentryLevel.DEBUG && it.message?.contains("debug") ?: false } |
| 80 | + assertEquals("Logcat", breadcrumb?.category) |
| 81 | + assertEquals(tag, breadcrumb?.data?.get("tag")) |
| 82 | + assert(breadcrumb?.message?.contains("debug") == true) |
| 83 | + } |
| 84 | + |
| 85 | + @Test |
| 86 | + fun `warning log message has expected content`() { |
| 87 | + val breadcrumb = breadcrumbs.find { it.level == SentryLevel.WARNING && it.message?.contains("warning") ?: false } |
| 88 | + assertEquals("Logcat", breadcrumb?.category) |
| 89 | + assertEquals(tag, breadcrumb?.data?.get("tag")) |
| 90 | + assert(breadcrumb?.message?.contains("warning") == true) |
| 91 | + } |
| 92 | + |
| 93 | + @Test |
| 94 | + fun `error log message has expected content`() { |
| 95 | + val breadcrumb = breadcrumbs.find { it.level == SentryLevel.ERROR && it.message?.contains("error") ?: false } |
| 96 | + assertEquals("Logcat", breadcrumb?.category) |
| 97 | + assertEquals(tag, breadcrumb?.data?.get("tag")) |
| 98 | + assert(breadcrumb?.message?.contains("error") == true) |
| 99 | + } |
| 100 | + |
| 101 | + @Test |
| 102 | + fun `wtf log message has expected content`() { |
| 103 | + val breadcrumb = breadcrumbs.find { it.level == SentryLevel.ERROR && it.message?.contains("wtf") ?: false } |
| 104 | + assertEquals("Logcat", breadcrumb?.category) |
| 105 | + assertEquals(tag, breadcrumb?.data?.get("tag")) |
| 106 | + assert(breadcrumb?.message?.contains("wtf") == true) |
| 107 | + } |
| 108 | + |
| 109 | + @Test |
| 110 | + fun `e log throwable has expected content`() { |
| 111 | + SentryLogcatAdapter.e(tag, "$commonMsg error exception", throwable) |
| 112 | + |
| 113 | + val breadcrumb = breadcrumbs.find { it.message?.contains("exception") ?: false } |
| 114 | + assertEquals("Logcat", breadcrumb?.category) |
| 115 | + assertEquals(tag, breadcrumb?.getData("tag")) |
| 116 | + assertEquals(SentryLevel.ERROR, breadcrumb?.level) |
| 117 | + assertEquals(throwable.message, breadcrumb?.getData("throwable")) |
| 118 | + } |
| 119 | + |
| 120 | + @Test |
| 121 | + fun `v log throwable has expected content`() { |
| 122 | + SentryLogcatAdapter.v(tag, "$commonMsg verbose exception", throwable) |
| 123 | + |
| 124 | + val breadcrumb = breadcrumbs.find { it.message?.contains("exception") ?: false } |
| 125 | + assertEquals("Logcat", breadcrumb?.category) |
| 126 | + assertEquals(tag, breadcrumb?.getData("tag")) |
| 127 | + assertEquals(SentryLevel.DEBUG, breadcrumb?.level) |
| 128 | + assertEquals(throwable.message, breadcrumb?.getData("throwable")) |
| 129 | + } |
| 130 | + |
| 131 | + @Test |
| 132 | + fun `i log throwable has expected content`() { |
| 133 | + SentryLogcatAdapter.i(tag, "$commonMsg info exception", throwable) |
| 134 | + |
| 135 | + val breadcrumb = breadcrumbs.find { it.message?.contains("exception") ?: false } |
| 136 | + assertEquals("Logcat", breadcrumb?.category) |
| 137 | + assertEquals(tag, breadcrumb?.getData("tag")) |
| 138 | + assertEquals(SentryLevel.INFO, breadcrumb?.level) |
| 139 | + assertEquals(throwable.message, breadcrumb?.getData("throwable")) |
| 140 | + } |
| 141 | + |
| 142 | + @Test |
| 143 | + fun `d log throwable has expected content`() { |
| 144 | + SentryLogcatAdapter.d(tag, "$commonMsg debug exception", throwable) |
| 145 | + |
| 146 | + val breadcrumb = breadcrumbs.find { it.message?.contains("exception") ?: false } |
| 147 | + assertEquals("Logcat", breadcrumb?.category) |
| 148 | + assertEquals(tag, breadcrumb?.getData("tag")) |
| 149 | + assertEquals(SentryLevel.DEBUG, breadcrumb?.level) |
| 150 | + assertEquals(throwable.message, breadcrumb?.getData("throwable")) |
| 151 | + } |
| 152 | + |
| 153 | + @Test |
| 154 | + fun `w log throwable has expected content`() { |
| 155 | + SentryLogcatAdapter.w(tag, "$commonMsg warning exception", throwable) |
| 156 | + |
| 157 | + val breadcrumb = breadcrumbs.find { it.message?.contains("exception") ?: false } |
| 158 | + assertEquals("Logcat", breadcrumb?.category) |
| 159 | + assertEquals(tag, breadcrumb?.getData("tag")) |
| 160 | + assertEquals(SentryLevel.WARNING, breadcrumb?.level) |
| 161 | + assertEquals(throwable.message, breadcrumb?.getData("throwable")) |
| 162 | + } |
| 163 | + |
| 164 | + @Test |
| 165 | + fun `wtf log throwable has expected content`() { |
| 166 | + SentryLogcatAdapter.wtf(tag, "$commonMsg wtf exception", throwable) |
| 167 | + |
| 168 | + val breadcrumb = breadcrumbs.find { it.message?.contains("exception") ?: false } |
| 169 | + assertEquals("Logcat", breadcrumb?.category) |
| 170 | + assertEquals(tag, breadcrumb?.getData("tag")) |
| 171 | + assertEquals(SentryLevel.ERROR, breadcrumb?.level) |
| 172 | + assertEquals(throwable.message, breadcrumb?.getData("throwable")) |
| 173 | + } |
| 174 | + |
| 175 | + @Test |
| 176 | + fun `logs add correct number of breadcrumb`() { |
| 177 | + assertEquals( |
| 178 | + 6, |
| 179 | + breadcrumbs.filter { |
| 180 | + it.message?.contains("SentryLogcatAdapter") ?: false |
| 181 | + }.size |
| 182 | + ) |
| 183 | + } |
| 184 | +} |
0 commit comments