@@ -6,6 +6,7 @@ package at.bitfire.davdroid
6
6
7
7
import android.Manifest
8
8
import android.accounts.Account
9
+ import android.content.ContentProviderClient
9
10
import android.content.ContentUris
10
11
import android.content.ContentValues
11
12
import android.os.Build
@@ -18,10 +19,9 @@ import at.bitfire.ical4android.AndroidCalendar
18
19
import at.bitfire.ical4android.Event
19
20
import net.fortuna.ical4j.model.property.DtStart
20
21
import net.fortuna.ical4j.model.property.RRule
22
+ import org.junit.Assert.assertNotNull
23
+ import org.junit.rules.ExternalResource
21
24
import org.junit.rules.RuleChain
22
- import org.junit.rules.TestRule
23
- import org.junit.runner.Description
24
- import org.junit.runners.model.Statement
25
25
import java.util.logging.Logger
26
26
27
27
/* *
@@ -36,63 +36,69 @@ import java.util.logging.Logger
36
36
*
37
37
* If you run tests manually, just make sure to ignore the first run after the calendar
38
38
* provider has been accessed the first time.
39
+ *
40
+ * See [at.bitfire.davdroid.resource.LocalCalendarTest] for how to use this rule.
39
41
*/
40
- class InitCalendarProviderRule private constructor(): TestRule {
41
-
42
- private val logger = Logger .getLogger(InitCalendarProviderRule ::javaClass.name)
42
+ class InitCalendarProviderRule private constructor(): ExternalResource() {
43
43
44
44
companion object {
45
- fun getInstance () = RuleChain
45
+
46
+ private var isInitialized = false
47
+ private val logger = Logger .getLogger(InitCalendarProviderRule ::javaClass.name)
48
+
49
+ fun getInstance (): RuleChain = RuleChain
46
50
.outerRule(GrantPermissionRule .grant(Manifest .permission.READ_CALENDAR , Manifest .permission.WRITE_CALENDAR ))
47
51
.around(InitCalendarProviderRule ())
48
- }
49
52
50
- override fun apply (base : Statement , description : Description ): Statement {
51
- logger.info(" Initializing calendar provider before running ${description.displayName} " )
52
- return InitCalendarProviderStatement (base)
53
53
}
54
54
55
-
56
- class InitCalendarProviderStatement (val base : Statement ): Statement() {
57
-
58
- private val logger = Logger .getLogger(InitCalendarProviderRule ::javaClass.name)
59
-
60
- override fun evaluate () {
55
+ override fun before () {
56
+ if (! isInitialized) {
57
+ logger.info(" Initializing calendar provider" )
61
58
if (Build .VERSION .SDK_INT < 31 )
62
59
logger.warning(" Calendar provider initialization may or may not work. See InitCalendarProviderRule" )
63
- initCalendarProvider()
64
60
65
- base.evaluate()
61
+ val context = InstrumentationRegistry .getInstrumentation().targetContext
62
+ val client = context.contentResolver.acquireContentProviderClient(CalendarContract .AUTHORITY )
63
+ assertNotNull(" Couldn't acquire calendar provider" , client)
64
+
65
+ client!! .use {
66
+ initCalendarProvider(client)
67
+ isInitialized = true
68
+ }
66
69
}
70
+ }
67
71
68
- private fun initCalendarProvider () {
69
- val account = Account (" LocalCalendarTest" , CalendarContract .ACCOUNT_TYPE_LOCAL )
70
- val context = InstrumentationRegistry .getInstrumentation().targetContext
71
- val provider = context.contentResolver.acquireContentProviderClient(CalendarContract .AUTHORITY )!!
72
- val uri = AndroidCalendar .create(account, provider, ContentValues ())
73
- val calendar = AndroidCalendar .findByID(account, provider, LocalCalendar .Factory , ContentUris .parseId(uri))
74
- try {
75
- // single event init
76
- val normalEvent = Event ().apply {
77
- dtStart = DtStart (" 20220120T010203Z" )
78
- summary = " Event with 1 instance"
79
- }
80
- val normalLocalEvent = LocalEvent (calendar, normalEvent, null , null , null , 0 )
81
- normalLocalEvent.add()
82
- LocalEvent .numInstances(provider, account, normalLocalEvent.id!! )
72
+ private fun initCalendarProvider (provider : ContentProviderClient ) {
73
+ val account = Account (" LocalCalendarTest" , CalendarContract .ACCOUNT_TYPE_LOCAL )
74
+ val uri = AndroidCalendar .create(account, provider, ContentValues ())
75
+ val calendar = AndroidCalendar .findByID(
76
+ account,
77
+ provider,
78
+ LocalCalendar .Factory ,
79
+ ContentUris .parseId(uri)
80
+ )
81
+ try {
82
+ // single event init
83
+ val normalEvent = Event ().apply {
84
+ dtStart = DtStart (" 20220120T010203Z" )
85
+ summary = " Event with 1 instance"
86
+ }
87
+ val normalLocalEvent = LocalEvent (calendar, normalEvent, null , null , null , 0 )
88
+ normalLocalEvent.add()
89
+ LocalEvent .numInstances(provider, account, normalLocalEvent.id!! )
83
90
84
- // recurring event init
85
- val recurringEvent = Event ().apply {
86
- dtStart = DtStart (" 20220120T010203Z" )
87
- summary = " Event over 22 years"
88
- rRules.add(RRule (" FREQ=YEARLY;UNTIL=20740119T010203Z" )) // year needs to be >2074 (not supported by Android <11 Calendar Storage)
89
- }
90
- val localRecurringEvent = LocalEvent (calendar, recurringEvent, null , null , null , 0 )
91
- localRecurringEvent.add()
92
- LocalEvent .numInstances(provider, account, localRecurringEvent.id!! )
93
- } finally {
94
- calendar.delete()
91
+ // recurring event init
92
+ val recurringEvent = Event ().apply {
93
+ dtStart = DtStart (" 20220120T010203Z" )
94
+ summary = " Event over 22 years"
95
+ rRules.add(RRule (" FREQ=YEARLY;UNTIL=20740119T010203Z" )) // year needs to be >2074 (not supported by Android <11 Calendar Storage)
95
96
}
97
+ val localRecurringEvent = LocalEvent (calendar, recurringEvent, null , null , null , 0 )
98
+ localRecurringEvent.add()
99
+ LocalEvent .numInstances(provider, account, localRecurringEvent.id!! )
100
+ } finally {
101
+ calendar.delete()
96
102
}
97
103
}
98
104
0 commit comments