-
-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathNotificationStorageInstrumentedTest.kt
More file actions
276 lines (231 loc) · 8.36 KB
/
Copy pathNotificationStorageInstrumentedTest.kt
File metadata and controls
276 lines (231 loc) · 8.36 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
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
// Copyright 2019-2023 Tauri Programme within The Commons Conservancy
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT
package app.tauri.notification
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.platform.app.InstrumentationRegistry
import com.fasterxml.jackson.databind.ObjectMapper
import org.junit.After
import org.junit.Assert.*
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
@RunWith(AndroidJUnit4::class)
class NotificationStorageInstrumentedTest {
private lateinit var storage: NotificationStorage
private lateinit var objectMapper: ObjectMapper
@Before
fun setup() {
val context = InstrumentationRegistry.getInstrumentation().targetContext
objectMapper = ObjectMapper()
storage = NotificationStorage(context, objectMapper)
// Clear any existing data
clearAllNotifications()
}
@After
fun cleanup() {
clearAllNotifications()
}
private fun clearAllNotifications() {
val ids = storage.getSavedNotificationIds()
for (id in ids) {
storage.deleteNotification(id)
}
}
@Test
fun testSaveAndRetrieveNotification() {
val notification = Notification()
notification.id = 100
notification.title = "Test Notification"
notification.body = "Test Body"
notification.schedule = NotificationSchedule.At().apply {
repeating = false
}
notification.sourceJson = """{"id":100,"title":"Test Notification","body":"Test Body"}"""
storage.appendNotifications(listOf(notification))
val ids = storage.getSavedNotificationIds()
assertTrue(ids.contains("100"))
val retrieved = storage.getSavedNotification("100")
assertNotNull(retrieved)
assertEquals(100, retrieved?.id)
assertEquals("Test Notification", retrieved?.title)
}
@Test
fun testSaveMultipleNotifications() {
val notifications = listOf(
Notification().apply {
id = 1
title = "First"
schedule = NotificationSchedule.At()
sourceJson = """{"id":1}"""
},
Notification().apply {
id = 2
title = "Second"
schedule = NotificationSchedule.Every().apply {
interval = NotificationInterval.Hour
count = 1
}
sourceJson = """{"id":2}"""
},
Notification().apply {
id = 3
title = "Third"
schedule = NotificationSchedule.Interval().apply {
interval = DateMatch()
}
sourceJson = """{"id":3}"""
}
)
storage.appendNotifications(notifications)
val ids = storage.getSavedNotificationIds()
assertEquals(3, ids.size)
assertTrue(ids.contains("1"))
assertTrue(ids.contains("2"))
assertTrue(ids.contains("3"))
}
@Test
fun testDeleteNotification() {
val notification = Notification()
notification.id = 200
notification.title = "To Delete"
notification.schedule = NotificationSchedule.At()
notification.sourceJson = """{"id":200}"""
storage.appendNotifications(listOf(notification))
var ids = storage.getSavedNotificationIds()
assertTrue(ids.contains("200"))
storage.deleteNotification("200")
ids = storage.getSavedNotificationIds()
assertFalse(ids.contains("200"))
}
@Test
fun testGetSavedNotifications() {
val notifications = listOf(
Notification().apply {
id = 10
title = "First"
body = "Body 1"
schedule = NotificationSchedule.At()
sourceJson = """{"id":10,"title":"First","body":"Body 1"}"""
},
Notification().apply {
id = 20
title = "Second"
body = "Body 2"
schedule = NotificationSchedule.At()
sourceJson = """{"id":20,"title":"Second","body":"Body 2"}"""
}
)
storage.appendNotifications(notifications)
val retrieved = storage.getSavedNotifications()
assertTrue(retrieved.size >= 2)
val ids = retrieved.map { it.id }
assertTrue(ids.contains(10))
assertTrue(ids.contains(20))
}
@Test
fun testSaveActionGroups() {
val action1 = NotificationAction()
action1.id = "reply"
action1.title = "Reply"
action1.input = true
val action2 = NotificationAction()
action2.id = "dismiss"
action2.title = "Dismiss"
action2.input = false
val actionType = ActionType()
actionType.id = "message-actions"
actionType.actions = listOf(action1, action2)
storage.writeActionGroup(listOf(actionType))
val retrieved = storage.getActionGroup("message-actions")
assertEquals(2, retrieved.size)
assertEquals("reply", retrieved[0]?.id)
assertEquals("Reply", retrieved[0]?.title)
assertEquals(true, retrieved[0]?.input)
assertEquals("dismiss", retrieved[1]?.id)
assertEquals("Dismiss", retrieved[1]?.title)
assertEquals(false, retrieved[1]?.input)
}
@Test
fun testOverwriteNotification() {
val notification1 = Notification()
notification1.id = 300
notification1.title = "Original"
notification1.schedule = NotificationSchedule.At()
notification1.sourceJson = """{"id":300,"title":"Original"}"""
storage.appendNotifications(listOf(notification1))
val notification2 = Notification()
notification2.id = 300
notification2.title = "Updated"
notification2.schedule = NotificationSchedule.At()
notification2.sourceJson = """{"id":300,"title":"Updated"}"""
storage.appendNotifications(listOf(notification2))
val retrieved = storage.getSavedNotification("300")
assertEquals("Updated", retrieved?.title)
}
@Test
fun testNotificationWithoutScheduleNotSaved() {
val notification = Notification()
notification.id = 400
notification.title = "No Schedule"
notification.schedule = null
notification.sourceJson = """{"id":400}"""
storage.appendNotifications(listOf(notification))
val ids = storage.getSavedNotificationIds()
assertFalse(ids.contains("400"))
}
@Test
fun testGetNonExistentNotification() {
val retrieved = storage.getSavedNotification("999999")
assertNull(retrieved)
}
@Test
fun testEmptyActionGroup() {
val retrieved = storage.getActionGroup("nonexistent-group")
assertEquals(0, retrieved.size)
}
@Test
fun testMultipleActionGroups() {
val group1Actions = listOf(
NotificationAction().apply {
id = "action1"
title = "Action 1"
input = false
}
)
val group1 = ActionType().apply {
id = "group1"
actions = group1Actions
}
val group2Actions = listOf(
NotificationAction().apply {
id = "action2"
title = "Action 2"
input = true
},
NotificationAction().apply {
id = "action3"
title = "Action 3"
input = false
}
)
val group2 = ActionType().apply {
id = "group2"
actions = group2Actions
}
storage.writeActionGroup(listOf(group1, group2))
val retrieved1 = storage.getActionGroup("group1")
assertEquals(1, retrieved1.size)
assertEquals("action1", retrieved1[0]?.id)
assertEquals("Action 1", retrieved1[0]?.title)
assertEquals(false, retrieved1[0]?.input)
val retrieved2 = storage.getActionGroup("group2")
assertEquals(2, retrieved2.size)
assertEquals("action2", retrieved2[0]?.id)
assertEquals("Action 2", retrieved2[0]?.title)
assertEquals(true, retrieved2[0]?.input)
assertEquals("action3", retrieved2[1]?.id)
assertEquals("Action 3", retrieved2[1]?.title)
assertEquals(false, retrieved2[1]?.input)
}
}