-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathAndroidCalendarTest.kt
More file actions
133 lines (110 loc) · 4.15 KB
/
AndroidCalendarTest.kt
File metadata and controls
133 lines (110 loc) · 4.15 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
/*
* 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.Manifest
import android.accounts.Account
import android.content.ContentProviderClient
import android.content.ContentUris
import android.content.ContentValues
import android.provider.CalendarContract
import android.provider.CalendarContract.Calendars
import android.provider.CalendarContract.Colors
import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.rule.GrantPermissionRule
import at.bitfire.ical4android.impl.TestCalendar
import at.bitfire.ical4android.util.MiscUtils.asSyncAdapter
import at.bitfire.ical4android.util.MiscUtils.closeCompat
import net.fortuna.ical4j.model.property.DtEnd
import net.fortuna.ical4j.model.property.DtStart
import org.junit.AfterClass
import org.junit.Assert.assertEquals
import org.junit.Assert.assertNotNull
import org.junit.Assert.assertTrue
import org.junit.Before
import org.junit.BeforeClass
import org.junit.ClassRule
import org.junit.Test
class AndroidCalendarTest {
companion object {
@JvmField
@ClassRule
val permissionRule = GrantPermissionRule.grant(
Manifest.permission.READ_CALENDAR,
Manifest.permission.WRITE_CALENDAR
)
lateinit var provider: ContentProviderClient
@BeforeClass
@JvmStatic
fun connectProvider() {
provider = InstrumentationRegistry.getInstrumentation().targetContext.contentResolver.acquireContentProviderClient(CalendarContract.AUTHORITY)!!
}
@AfterClass
@JvmStatic
fun closeProvider() {
provider.closeCompat()
}
}
private val testAccount = Account("ical4android.AndroidCalendarTest", CalendarContract.ACCOUNT_TYPE_LOCAL)
@Before
fun prepare() {
// make sure there are no colors for testAccount
AndroidCalendar.removeColors(provider, testAccount)
assertEquals(0, countColors(testAccount))
}
@Test
fun testManageCalendars() {
// create calendar
val info = ContentValues()
info.put(Calendars.NAME, "TestCalendar")
info.put(Calendars.CALENDAR_DISPLAY_NAME, "ical4android Test Calendar")
info.put(Calendars.VISIBLE, 0)
info.put(Calendars.SYNC_EVENTS, 0)
val uri = AndroidCalendar.create(testAccount, provider, info)
assertNotNull(uri)
// query calendar
val calendar = AndroidCalendar.findByID(testAccount, provider, TestCalendar.Factory, ContentUris.parseId(uri))
assertNotNull(calendar)
// delete calendar
assertTrue(calendar.delete())
}
@Test
fun testInsertColors() {
AndroidCalendar.insertColors(provider, testAccount)
assertEquals(Css3Color.entries.size, countColors(testAccount))
}
@Test
fun testInsertColors_AlreadyThere() {
AndroidCalendar.insertColors(provider, testAccount)
AndroidCalendar.insertColors(provider, testAccount)
assertEquals(Css3Color.entries.size, countColors(testAccount))
}
@Test
fun testRemoveColors() {
AndroidCalendar.insertColors(provider, testAccount)
// insert an event with that color
val cal = TestCalendar.findOrCreate(testAccount, provider)
try {
// add event with color
AndroidEvent(cal, Event().apply {
dtStart = DtStart("20210314T204200Z")
dtEnd = DtEnd("20210314T204230Z")
color = Css3Color.limegreen
summary = "Test event with color"
}, "remove-colors").add()
AndroidCalendar.removeColors(provider, testAccount)
assertEquals(0, countColors(testAccount))
} finally {
cal.delete()
}
}
private fun countColors(account: Account): Int {
val uri = Colors.CONTENT_URI.asSyncAdapter(account)
provider.query(uri, null, null, null, null)!!.use { cursor ->
cursor.moveToNext()
return cursor.count
}
}
}