-
-
Notifications
You must be signed in to change notification settings - Fork 426
/
SentryAndroidTest.kt
215 lines (175 loc) · 7.03 KB
/
SentryAndroidTest.kt
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package io.sentry.android.core
import android.os.Bundle
import androidx.test.core.app.ApplicationProvider
import androidx.test.ext.junit.runners.AndroidJUnit4
import io.sentry.Hint
import io.sentry.ILogger
import io.sentry.Sentry
import io.sentry.SentryEnvelope
import io.sentry.SentryLevel
import io.sentry.SentryLevel.DEBUG
import io.sentry.SentryLevel.FATAL
import io.sentry.SentryOptions
import io.sentry.android.core.cache.AndroidEnvelopeCache
import io.sentry.android.fragment.FragmentLifecycleIntegration
import io.sentry.android.timber.SentryTimberIntegration
import io.sentry.cache.IEnvelopeCache
import io.sentry.transport.NoOpEnvelopeCache
import io.sentry.util.StringUtils
import org.junit.runner.RunWith
import org.mockito.kotlin.any
import org.mockito.kotlin.eq
import org.mockito.kotlin.mock
import org.mockito.kotlin.never
import org.mockito.kotlin.verify
import java.nio.file.Files
import kotlin.io.path.absolutePathString
import kotlin.test.BeforeTest
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertFalse
import kotlin.test.assertNotNull
import kotlin.test.assertNull
import kotlin.test.assertTrue
@RunWith(AndroidJUnit4::class)
class SentryAndroidTest {
class Fixture {
fun initSut(
autoInit: Boolean = false,
logger: ILogger? = null,
options: Sentry.OptionsConfiguration<SentryAndroidOptions>? = null
) {
val metadata = Bundle().apply {
putString(ManifestMetadataReader.DSN, "https://key@sentry.io/123")
putBoolean(ManifestMetadataReader.AUTO_INIT, autoInit)
}
val mockContext = ContextUtilsTest.mockMetaData(metaData = metadata)
when {
logger != null -> SentryAndroid.init(mockContext, logger)
options != null -> SentryAndroid.init(mockContext, options)
else -> SentryAndroid.init(mockContext)
}
}
}
private val fixture = Fixture()
@BeforeTest
fun `set up`() {
Sentry.close()
AppStartState.getInstance().resetInstance()
}
@Test
fun `when auto-init is disabled and user calls init manually, SDK initializes`() {
assertFalse(Sentry.isEnabled())
fixture.initSut()
assertTrue(Sentry.isEnabled())
}
@Test
fun `when auto-init is disabled and user calls init manually with a logger, SDK initializes`() {
assertFalse(Sentry.isEnabled())
fixture.initSut(logger = mock())
assertTrue(Sentry.isEnabled())
}
@Test
fun `when auto-init is disabled and user calls init manually with configuration handler, options should be set`() {
assertFalse(Sentry.isEnabled())
var refOptions: SentryAndroidOptions? = null
fixture.initSut {
it.anrTimeoutIntervalMillis = 3000
refOptions = it
}
assertEquals(3000, refOptions!!.anrTimeoutIntervalMillis)
assertTrue(Sentry.isEnabled())
}
@Test
fun `init won't throw exception`() {
val logger = mock<ILogger>()
fixture.initSut(autoInit = true, logger = logger)
verify(logger, never()).log(eq(SentryLevel.FATAL), any<String>(), any())
}
@Test
fun `set app start if provider is disabled`() {
fixture.initSut(autoInit = true)
// done by ActivityLifecycleIntegration so forcing it here
AppStartState.getInstance().setAppStartEnd()
AppStartState.getInstance().setColdStart(true)
assertNotNull(AppStartState.getInstance().appStartInterval)
}
@Test
fun `deduplicates fragment and timber integrations`() {
var refOptions: SentryAndroidOptions? = null
fixture.initSut(autoInit = true) {
it.addIntegration(
FragmentLifecycleIntegration(ApplicationProvider.getApplicationContext())
)
it.addIntegration(
SentryTimberIntegration(minEventLevel = FATAL, minBreadcrumbLevel = DEBUG)
)
refOptions = it
}
assertEquals(refOptions!!.integrations.filterIsInstance<SentryTimberIntegration>().size, 1)
val timberIntegration =
refOptions!!.integrations.find { it is SentryTimberIntegration } as SentryTimberIntegration
assertEquals(timberIntegration.minEventLevel, FATAL)
assertEquals(timberIntegration.minBreadcrumbLevel, DEBUG)
// fragment integration is not auto-installed in the test, since the context is not Application
// but we just verify here that the single integration is preserved
assertEquals(refOptions!!.integrations.filterIsInstance<FragmentLifecycleIntegration>().size, 1)
}
@Test
fun `AndroidEnvelopeCache is reset if the user disabled caching via cacheDirPath`() {
var refOptions: SentryAndroidOptions? = null
fixture.initSut {
it.cacheDirPath = null
refOptions = it
}
assertTrue { refOptions!!.envelopeDiskCache is NoOpEnvelopeCache }
}
@Test
fun `envelopeCache remains unchanged if the user set their own IEnvelopCache impl`() {
var refOptions: SentryAndroidOptions? = null
fixture.initSut {
it.cacheDirPath = null
it.setEnvelopeDiskCache(CustomEnvelopCache())
refOptions = it
}
assertTrue { refOptions!!.envelopeDiskCache is CustomEnvelopCache }
}
@Test
fun `When initializing Sentry manually and changing both cache dir and dsn, the corresponding options should reflect that change`() {
var options: SentryOptions? = null
val mockContext = ContextUtilsTest.createMockContext(true)
val cacheDirPath = Files.createTempDirectory("new_cache").absolutePathString()
SentryAndroid.init(mockContext) {
it.dsn = "https://key@sentry.io/123"
it.cacheDirPath = cacheDirPath
options = it
}
val dsnHash = StringUtils.calculateStringHash(options!!.dsn, options!!.logger)
val expectedCacheDir = "$cacheDirPath/$dsnHash"
assertEquals(expectedCacheDir, options!!.cacheDirPath)
assertEquals(expectedCacheDir, (options!!.envelopeDiskCache as AndroidEnvelopeCache).directory.absolutePath)
}
@Test
fun `init starts a session if auto session tracking is enabled`() {
fixture.initSut { options ->
options.isEnableAutoSessionTracking = true
}
Sentry.getCurrentHub().withScope { scope ->
assertNotNull(scope.session)
}
}
@Test
fun `init does not start a session by if auto session tracking is disabled`() {
fixture.initSut { options ->
options.isEnableAutoSessionTracking = false
}
Sentry.getCurrentHub().withScope { scope ->
assertNull(scope.session)
}
}
private class CustomEnvelopCache : IEnvelopeCache {
override fun iterator(): MutableIterator<SentryEnvelope> = TODO()
override fun store(envelope: SentryEnvelope, hint: Hint) = Unit
override fun discard(envelope: SentryEnvelope) = Unit
}
}