Skip to content

Commit 706c725

Browse files
committed
[WIP] Fix tests
1 parent 5f7ff56 commit 706c725

File tree

3 files changed

+25
-42
lines changed

3 files changed

+25
-42
lines changed

lib/src/androidTest/kotlin/at/bitfire/synctools/storage/calendar/AndroidCalendarTest.kt

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
package at.bitfire.synctools.storage.calendar
88

9-
import android.Manifest
109
import android.accounts.Account
1110
import android.content.ContentProviderClient
1211
import android.content.ContentValues
@@ -17,9 +16,9 @@ import android.provider.CalendarContract.Events
1716
import android.provider.CalendarContract.Reminders
1817
import androidx.core.content.contentValuesOf
1918
import androidx.test.platform.app.InstrumentationRegistry
20-
import androidx.test.rule.GrantPermissionRule
2119
import at.bitfire.ical4android.impl.TestCalendar
2220
import at.bitfire.ical4android.util.MiscUtils.closeCompat
21+
import at.bitfire.synctools.test.InitCalendarProviderRule
2322
import at.bitfire.synctools.test.assertContentValuesEqual
2423
import org.junit.After
2524
import org.junit.Assert.assertEquals
@@ -32,10 +31,7 @@ import org.junit.Test
3231
class AndroidCalendarTest {
3332

3433
@get:Rule
35-
val permissionRule = GrantPermissionRule.grant(
36-
Manifest.permission.READ_CALENDAR,
37-
Manifest.permission.WRITE_CALENDAR
38-
)
34+
val initCalendarProviderRule = InitCalendarProviderRule.initialize()
3935

4036
private val now = System.currentTimeMillis()
4137
private val testAccount = Account(javaClass.name, CalendarContract.ACCOUNT_TYPE_LOCAL)

lib/src/main/kotlin/at/bitfire/synctools/storage/calendar/AndroidCalendar.kt

Lines changed: 22 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ import java.util.LinkedList
3939
*/
4040
class AndroidCalendar(
4141
internal val provider: AndroidCalendarProvider,
42-
val values: ContentValues
42+
private val values: ContentValues
4343
) {
4444

4545
/** see [Calendars._ID] */
@@ -359,41 +359,36 @@ class AndroidCalendar(
359359
// query event to get first and last instance
360360
var first: Long? = null
361361
var last: Long? = null
362-
client.query(
363-
eventUri(eventId),
364-
arrayOf(Events.DTSTART, Events.LAST_DATE), null, null, null
365-
)?.use { cursor ->
366-
cursor.moveToNext()
367-
if (!cursor.isNull(0))
368-
first = cursor.getLong(0)
369-
if (!cursor.isNull(1))
370-
last = cursor.getLong(1)
362+
getEventRow(id, arrayOf(Events.DTSTART, Events.LAST_DATE))?.let { values ->
363+
first = values.getAsLong(Events.DTSTART)
364+
last = values.getAsLong(Events.LAST_DATE)
371365
}
372366
// if this event doesn't have a last occurrence, it's endless and always has instances
373367
if (first == null || last == null)
374368
return null
375369

376370
/* We can't use Long.MIN_VALUE and Long.MAX_VALUE because Android generates the instances
377-
on the fly and it doesn't accept those values. So we use the first/last actual occurence
378-
of the event (calculated by Android). */
371+
on the fly and it doesn't accept those values. So we use the first/last actual occurrence
372+
of the event (as calculated by Android). */
379373
val instancesUri = CalendarContract.Instances.CONTENT_URI.asSyncAdapter(account)
380374
.buildUpon()
381375
.appendPath(first.toString()) // begin timestamp
382376
.appendPath(last.toString()) // end timestamp
383377
.build()
384378

379+
var numInstances: Int? = null
385380
try {
386381
client.query(
387382
instancesUri, null,
388383
"${CalendarContract.Instances.EVENT_ID}=?", arrayOf(eventId.toString()),
389384
null
390385
)?.use { cursor ->
391-
return cursor.count
386+
numInstances = cursor.count
392387
}
393388
} catch (e: RemoteException) {
394389
throw LocalStorageException("Couldn't query number of instances for event $eventId", e)
395390
}
396-
return null
391+
return numInstances
397392
}
398393

399394
/**
@@ -406,30 +401,22 @@ class AndroidCalendar(
406401
*/
407402
fun numInstances(eventId: Long): Int? {
408403
// num instances of the main event
409-
var numInstances = numDirectInstances(eventId) ?: return null
404+
val numDirectInstances = numDirectInstances(eventId) ?: return null
410405

411406
// add the number of instances of every main event's exception
412-
try {
413-
client.query(
414-
Events.CONTENT_URI,
415-
arrayOf(Events._ID),
416-
"${Events.ORIGINAL_ID}=?", // get exception events of the main event
417-
arrayOf(eventId.toString()), null
418-
)?.use { exceptionsEventCursor ->
419-
while (exceptionsEventCursor.moveToNext()) {
420-
val exceptionEventId = exceptionsEventCursor.getLong(0)
421-
val exceptionInstances = numDirectInstances(exceptionEventId)
422-
423-
if (exceptionInstances == null)
424-
return null // number of instances of exception can't be determined; so the total number of instances is also unclear
425-
426-
numInstances += exceptionInstances
427-
}
428-
}
429-
} catch (e: RemoteException) {
430-
throw LocalStorageException("Couldn't query number of exception instances for event $eventId", e)
407+
var numExInstances = 0
408+
iterateEventRows(
409+
arrayOf(Events._ID),
410+
"${Events.ORIGINAL_ID}=?", // get exception events of the main event
411+
arrayOf(eventId.toString())
412+
) { values ->
413+
val exceptionEventId = values.getAsLong(Events._ID)
414+
val exceptionInstances = numDirectInstances(exceptionEventId)
415+
416+
if (exceptionInstances != null)
417+
numExInstances += exceptionInstances
431418
}
432-
return numInstances
419+
return numDirectInstances - numExInstances
433420
}
434421

435422

lib/src/main/kotlin/at/bitfire/synctools/storage/calendar/AndroidEvent2.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import at.bitfire.synctools.storage.calendar.AndroidEvent2.Companion.CATEGORIES_
2929
*/
3030
class AndroidEvent2(
3131
val calendar: AndroidCalendar,
32-
val values: Entity
32+
private val values: Entity
3333
) {
3434

3535
private val mainValues

0 commit comments

Comments
 (0)