-
-
Notifications
You must be signed in to change notification settings - Fork 468
Expand file tree
/
Copy pathNdkScopeObserverTest.kt
More file actions
187 lines (147 loc) · 4.87 KB
/
NdkScopeObserverTest.kt
File metadata and controls
187 lines (147 loc) · 4.87 KB
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
package io.sentry.android.ndk
import io.sentry.Attachment
import io.sentry.Breadcrumb
import io.sentry.DateUtils
import io.sentry.JsonSerializer
import io.sentry.SentryLevel
import io.sentry.SentryOptions
import io.sentry.ndk.INativeScope
import io.sentry.protocol.User
import io.sentry.test.DeferredExecutorService
import io.sentry.test.ImmediateExecutorService
import kotlin.test.Test
import org.mockito.kotlin.any
import org.mockito.kotlin.anyOrNull
import org.mockito.kotlin.eq
import org.mockito.kotlin.mock
import org.mockito.kotlin.never
import org.mockito.kotlin.verify
class NdkScopeObserverTest {
private class Fixture {
val nativeScope = mock<INativeScope>()
val options =
SentryOptions().apply {
setSerializer(JsonSerializer(mock()))
executorService = ImmediateExecutorService()
}
fun getSut(): NdkScopeObserver = NdkScopeObserver(options, nativeScope)
}
private val fixture = Fixture()
@Test
fun `set tag forwards call to scope sync`() {
val sut = fixture.getSut()
sut.setTag("a", "b")
verify(fixture.nativeScope).setTag("a", "b")
}
@Test
fun `remove tag forwards call to scope sync`() {
val sut = fixture.getSut()
sut.removeTag("a")
verify(fixture.nativeScope).removeTag("a")
}
@Test
fun `set extra forwards call to scope sync`() {
val sut = fixture.getSut()
sut.setExtra("a", "b")
verify(fixture.nativeScope).setExtra("a", "b")
}
@Test
fun `remove extra forwards call to scope sync`() {
val sut = fixture.getSut()
sut.removeExtra("a")
verify(fixture.nativeScope).removeExtra("a")
}
@Test
fun `set user forwards call to scope sync`() {
val sut = fixture.getSut()
val user =
User().apply {
id = "id"
email = "email"
ipAddress = "ip"
username = "username"
}
sut.setUser(user)
verify(fixture.nativeScope)
.setUser(eq(user.id), eq(user.email), eq(user.ipAddress), eq(user.username))
}
@Test
fun `remove user forwards call to scope sync`() {
val sut = fixture.getSut()
sut.setUser(null)
verify(fixture.nativeScope).removeUser()
}
@Test
fun `set breadcrumb forwards call to scope sync`() {
val sut = fixture.getSut()
val breadcrumb =
Breadcrumb().apply {
level = SentryLevel.DEBUG
message = "message"
category = "category"
setData("a", "b")
type = "type"
}
val timestamp = DateUtils.getTimestamp(breadcrumb.timestamp)
val data = "{\"a\":\"b\"}"
sut.addBreadcrumb(breadcrumb)
verify(fixture.nativeScope)
.addBreadcrumb(
eq("debug"),
eq(breadcrumb.message),
eq(breadcrumb.category),
eq(breadcrumb.type),
eq(timestamp),
eq(data),
)
}
@Test
fun `scope sync utilizes executor service`() {
val executorService = DeferredExecutorService()
fixture.options.executorService = executorService
val sut = fixture.getSut()
sut.setTag("a", "b")
sut.removeTag("a")
sut.setExtra("a", "b")
sut.removeExtra("a")
sut.setUser(User())
sut.addBreadcrumb(Breadcrumb())
// as long as the executor service is not run, the scope sync is not called
verify(fixture.nativeScope, never()).setTag(any(), any())
verify(fixture.nativeScope, never()).removeTag(any())
verify(fixture.nativeScope, never()).setExtra(any(), any())
verify(fixture.nativeScope, never()).removeExtra(any())
verify(fixture.nativeScope, never()).setUser(any(), any(), any(), any())
verify(fixture.nativeScope, never()).addBreadcrumb(any(), any(), any(), any(), any(), any())
// when the executor service is run, the scope sync is called
executorService.runAll()
verify(fixture.nativeScope).setTag(any(), any())
verify(fixture.nativeScope).removeTag(any())
verify(fixture.nativeScope).setExtra(any(), any())
verify(fixture.nativeScope).removeExtra(any())
verify(fixture.nativeScope).setUser(anyOrNull(), anyOrNull(), anyOrNull(), anyOrNull())
verify(fixture.nativeScope)
.addBreadcrumb(anyOrNull(), anyOrNull(), anyOrNull(), anyOrNull(), anyOrNull(), anyOrNull())
}
@Test
fun `add file-path attachment syncs to native scope`() {
val sut = fixture.getSut()
val attachment = Attachment("/data/data/com.example/files/log.txt")
sut.addAttachment(attachment)
verify(fixture.nativeScope).addAttachment("/data/data/com.example/files/log.txt")
}
@Test
fun `add byte attachment syncs bytes to native scope`() {
val sut = fixture.getSut()
val bytes = byteArrayOf(1, 2, 3)
val attachment = Attachment(bytes, "data.bin")
sut.addAttachment(attachment)
verify(fixture.nativeScope).addAttachmentBytes(bytes, "data.bin")
}
@Test
fun `clear attachments forwards call to native scope`() {
val sut = fixture.getSut()
sut.clearAttachments()
verify(fixture.nativeScope).clearAttachments()
}
}