-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathDmfsTaskTest.kt
More file actions
204 lines (174 loc) · 7.08 KB
/
DmfsTaskTest.kt
File metadata and controls
204 lines (174 loc) · 7.08 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
/*
* This file is part of bitfireAT/synctools which is released under GPLv3.
* Copyright © All Contributors. See the LICENSE and AUTHOR files in the root directory for details.
* SPDX-License-Identifier: GPL-3.0-or-later
*/
package at.bitfire.ical4android
import android.accounts.Account
import android.content.ContentUris
import android.net.Uri
import androidx.core.content.contentValuesOf
import at.bitfire.ical4android.impl.TestTaskList
import at.bitfire.synctools.storage.LocalStorageException
import at.bitfire.synctools.storage.tasks.DmfsTaskList
import net.fortuna.ical4j.model.TimeZoneRegistryFactory
import net.fortuna.ical4j.model.component.VAlarm
import net.fortuna.ical4j.model.parameter.RelType
import net.fortuna.ical4j.model.property.DtStart
import net.fortuna.ical4j.model.property.Due
import net.fortuna.ical4j.model.property.Duration
import net.fortuna.ical4j.model.property.Organizer
import net.fortuna.ical4j.model.property.RelatedTo
import net.fortuna.ical4j.model.property.XProperty
import org.dmfs.tasks.contract.TaskContract
import org.dmfs.tasks.contract.TaskContract.Tasks
import org.junit.After
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertNotNull
import org.junit.Before
import org.junit.Test
import java.time.LocalDate
import java.time.ZonedDateTime
class DmfsTaskTest(
providerName: TaskProvider.ProviderName
): DmfsStyleProvidersTaskTest(providerName) {
private val tzRegistry = TimeZoneRegistryFactory.getInstance().createRegistry()!!
private val tzVienna = tzRegistry.getTimeZone("Europe/Vienna")!!
private val testAccount = Account(javaClass.name, TaskContract.LOCAL_ACCOUNT_TYPE)
private lateinit var taskListUri: Uri
private var taskList: DmfsTaskList? = null
@Before
override fun prepare() {
super.prepare()
taskList = TestTaskList.create(testAccount, provider)
assertNotNull("Couldn't find/create test task list", taskList)
taskListUri = ContentUris.withAppendedId(provider.taskListsUri(), taskList!!.id)
}
@After
override fun shutdown() {
taskList?.delete()
super.shutdown()
}
// tests
@Test
fun testConstructor_ContentValues() {
val dmfsTask = DmfsTask(
taskList!!, contentValuesOf(
Tasks._ID to 123,
Tasks._SYNC_ID to "some-ical.ics",
DmfsTask.COLUMN_ETAG to "some-etag",
DmfsTask.COLUMN_FLAGS to 45
)
)
assertEquals(123L, dmfsTask.id)
assertEquals("some-ical.ics", dmfsTask.syncId)
assertEquals("some-etag", dmfsTask.eTag)
assertEquals(45, dmfsTask.flags)
}
@Test
fun testAddTask() {
// build and write event to calendar provider
val task = Task()
task.uid = "sample1@testAddEvent"
task.summary = "Sample event"
task.description = "Sample event with date/time"
task.location = "Sample location"
task.dtStart = DtStart(ZonedDateTime.of(
2015, 5, 1,
12, 0, 0, 0,
tzVienna.toZoneId()
))
task.due = Due(ZonedDateTime.of(
2015, 5, 1,
14, 0, 0, 0,
tzVienna.toZoneId()
))
task.organizer = Organizer("mailto:organizer@example.com")
assertFalse(task.isAllDay())
// extended properties
task.categories.addAll(arrayOf("Cat1", "Cat2"))
task.comment = "A comment"
task.relatedTo.add(
RelatedTo("most-fields2@example.com")
.add(RelType.SIBLING)
)
task.unknownProperties += XProperty("X-UNKNOWN-PROP", "Unknown Value")
// add to task list
val uri = DmfsTask(taskList!!, task, "9468a4cf-0d5b-4379-a704-12f1f84100ba", null, 0).add()
assertNotNull("Couldn't add task", uri)
// read and parse event from calendar provider
val testTask = taskList!!.getTask(ContentUris.parseId(uri))
try {
assertNotNull("Inserted task is not here", testTask)
val task2 = testTask.task
assertNotNull("Inserted task is empty", task2)
// compare with original event
assertEquals(task.summary, task2!!.summary)
assertEquals(task.description, task2.description)
assertEquals(task.location, task2.location)
assertEquals(task.dtStart, task2.dtStart)
assertEquals(task.categories, task2.categories)
assertEquals(task.comment, task2.comment)
assertEquals(task.relatedTo, task2.relatedTo)
assertEquals(task.unknownProperties, task2.unknownProperties)
} finally {
testTask.delete()
}
}
@Test(expected = LocalStorageException::class)
fun testAddTaskWithInvalidDue() {
val task = Task()
task.uid = "invalidDUE@ical4android.tests"
task.summary = "Task with invalid DUE"
task.dtStart = DtStart(LocalDate.of(2015, 1, 2))
task.due = Due(LocalDate.of(2015, 1, 1))
DmfsTask(taskList!!, task, "9468a4cf-0d5b-4379-a704-12f1f84100ba", null, 0).add()
}
@Test
fun testAddTaskWithManyAlarms() {
val task = Task()
task.uid = "TaskWithManyAlarms"
task.summary = "Task with many alarms"
task.dtStart = DtStart(LocalDate.of(2015, 1, 2))
for (i in 1..1050)
task.alarms += VAlarm(java.time.Duration.ofMinutes(i.toLong()))
val uri = DmfsTask(taskList!!, task, "9468a4cf-0d5b-4379-a704-12f1f84100ba", null, 0).add()
val task2 = taskList!!.getTask(ContentUris.parseId(uri))
assertEquals(1050, task2.task?.alarms?.size)
}
@Test
fun testUpdateTask() {
// add test event without reminder
val task = Task()
task.uid = "sample1@testAddEvent"
task.summary = "Sample event"
task.description = "Sample event with date/time"
task.location = "Sample location"
task.dtStart = DtStart(ZonedDateTime.of(
2015, 5, 1,
12, 0, 0, 0,
tzVienna.toZoneId()
))
assertFalse(task.isAllDay())
val uri = DmfsTask(taskList!!, task, "9468a4cf-0d5b-4379-a704-12f1f84100ba", null, 0).add()
assertNotNull(uri)
val testTask = taskList!!.getTask(ContentUris.parseId(uri))
try {
// update test event in calendar
val task2 = testTask.task!!
task2.summary = "Updated event" // change value
task.location = null // remove value
task2.duration = Duration(java.time.Duration.ofMinutes(10)) // add value
testTask.update(task2)
// read again and verify result
val updatedTask = taskList!!.getTask(ContentUris.parseId(uri)).task!!
assertEquals(task2.summary, updatedTask.summary)
assertEquals(task2.location, updatedTask.location)
assertEquals(task2.dtStart, updatedTask.dtStart)
assertEquals(task2.duration!!.value, updatedTask.duration!!.value)
} finally {
testTask.delete()
}
}
}