Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ guava = "33.5.0-android"
ical4j = "3.2.19" # final version; update to 4.x will require much work
junit = "4.13.2"
kotlin = "2.3.10"
kotlinx-coroutines-test = "1.10.2"
mockk = "1.14.9"
roboelectric = "4.16.1"
slf4j = "2.0.17"
Expand All @@ -27,6 +28,7 @@ ezvcard = { module = "com.googlecode.ez-vcard:ez-vcard", version.ref = "ezvcard"
guava = { module = "com.google.guava:guava", version.ref = "guava" }
ical4j = { module = "org.mnode.ical4j:ical4j", version.ref = "ical4j" }
junit = { module = "junit:junit", version.ref = "junit" }
kotlin-coroutines-test = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-test", version.ref = "kotlinx-coroutines-test" }
kotlin-stdlib = { module = "org.jetbrains.kotlin:kotlin-stdlib", version.ref = "kotlin" }
mockk = { module = "io.mockk:mockk", version.ref = "mockk" }
mockk-android = { module = "io.mockk:mockk-android", version.ref = "mockk" }
Expand Down
2 changes: 2 additions & 0 deletions lib/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -129,12 +129,14 @@ dependencies {
// instrumented tests
androidTestImplementation(libs.androidx.test.rules)
androidTestImplementation(libs.androidx.test.runner)
androidTestImplementation(libs.kotlin.coroutines.test)
androidTestImplementation(libs.mockk.android)
androidTestImplementation(libs.androidx.test.rules)
androidTestImplementation(libs.androidx.test.runner)

// unit tests
testImplementation(libs.junit)
testImplementation(libs.kotlin.coroutines.test)
testImplementation(libs.mockk)
testImplementation(libs.roboelectric)
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import android.content.ContentProviderClient
import android.provider.ContactsContract
import android.util.Base64
import androidx.test.filters.MediumTest
import androidx.test.filters.SmallTest
import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.rule.GrantPermissionRule
import at.bitfire.synctools.storage.LocalStorageException
Expand All @@ -24,6 +23,7 @@ import ezvcard.property.Address
import ezvcard.property.Birthday
import ezvcard.property.Email
import ezvcard.util.PartialDate
import kotlinx.coroutines.test.runTest
import org.junit.Assert.assertArrayEquals
import org.junit.Assert.assertEquals
import org.junit.Assert.assertNotNull
Expand Down Expand Up @@ -69,7 +69,6 @@ class AndroidContactTest {


@Test
@SmallTest
fun testAddAndReadContact() {
val samplePhoto = Base64.decode("/9j/4AAQSkZJRgABAQEASABIAAD//gATQ3JlYXRlZCB3aXRoIEdJTVD/2wBDAAMCAgMCAgMDAwMEAwMEBQgFBQQEBQoHBwYIDAoMDAsKCwsNDhIQDQ4RDgsLEBYQERMUFRUVDA8XGBYUGBIUFRT/2wBDAQMEBAUEBQkFBQkUDQsNFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBT/wgARCAAFAAUDAREAAhEBAxEB/8QAFAABAAAAAAAAAAAAAAAAAAAACP/EABQBAQAAAAAAAAAAAAAAAAAAAAD/2gAMAwEAAhADEAAAAVSf/8QAFBABAAAAAAAAAAAAAAAAAAAAAP/aAAgBAQABBQJ//8QAFBEBAAAAAAAAAAAAAAAAAAAAAP/aAAgBAwEBPwF//8QAFBEBAAAAAAAAAAAAAAAAAAAAAP/aAAgBAgEBPwF//8QAFBABAAAAAAAAAAAAAAAAAAAAAP/aAAgBAQAGPwJ//8QAFBABAAAAAAAAAAAAAAAAAAAAAP/aAAgBAQABPyF//9oADAMBAAIAAwAAABCf/8QAFBEBAAAAAAAAAAAAAAAAAAAAAP/aAAgBAwEBPxB//8QAFBEBAAAAAAAAAAAAAAAAAAAAAP/aAAgBAgEBPxB//8QAFBABAAAAAAAAAAAAAAAAAAAAAP/aAAgBAQABPxB//9k=", Base64.DEFAULT)

Expand Down Expand Up @@ -109,8 +108,7 @@ class AndroidContactTest {
}

@Test
@SmallTest
fun testInvalidPREF() {
fun testInvalidPREF() = runTest {
val vCard = "BEGIN:VCARD\r\n" +
"VERSION:4.0\r\n" +
"FN:Test\r\n" +
Expand All @@ -134,7 +132,7 @@ class AndroidContactTest {
}

@Test
fun testBirthdayWithOffset() {
fun testBirthdayWithOffset() = runTest {
val vCard = "BEGIN:VCARD\r\n" +
"VERSION:3.0\n\n" +
"N:Doe;John;;;\n\n" +
Expand Down
16 changes: 14 additions & 2 deletions lib/src/main/kotlin/at/bitfire/vcard4android/Contact.kt
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ data class Contact(
* @throws IOException on I/O errors when reading the stream
* @throws ezvcard.io.CannotParseException when the vCard can't be parsed
*/
fun fromReader(reader: Reader, jCard: Boolean, downloader: Downloader?): List<Contact> {
suspend fun fromReader(reader: Reader, jCard: Boolean, downloader: Downloader?): List<Contact> {
// create new reader and add custom scribes
val vCards =
if (jCard)
Expand Down Expand Up @@ -199,8 +199,20 @@ data class Contact(
.toString()


/**
* Downloads data (like photos) from a specified URL.
*/
interface Downloader {
fun download(url: String, accepts: String): ByteArray?

/**
* Downloads data from the specified URL.
*
* @param url The URL to download data from.
* @param accepts The accept header value for the download request.
* @return The downloaded data as a byte array, or null if the download failed.
*/
suspend fun download(url: String, accepts: String): ByteArray?

}

}
6 changes: 3 additions & 3 deletions lib/src/main/kotlin/at/bitfire/vcard4android/ContactReader.kt
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ class ContactReader internal constructor(val vCard: VCard, val downloader: Conta
*/
const val MAX_BINARY_DATA_SIZE = 25*1024

fun fromVCard(vCard: VCard, downloader: Contact.Downloader? = null) =
suspend fun fromVCard(vCard: VCard, downloader: Contact.Downloader? = null) =
ContactReader(vCard, downloader).toContact()

/**
Expand Down Expand Up @@ -154,7 +154,7 @@ class ContactReader internal constructor(val vCard: VCard, val downloader: Conta
/**
* Converts the vCard to a [Contact].
*/
private fun toContact(): Contact {
private suspend fun toContact(): Contact {
val c = Contact()

// process standard properties; after processing, only unknown properties will remain
Expand Down Expand Up @@ -369,7 +369,7 @@ class ContactReader internal constructor(val vCard: VCard, val downloader: Conta
return null
}

fun getPhotoBytes(photo: Photo): ByteArray? {
suspend fun getPhotoBytes(photo: Photo): ByteArray? {
if (photo.data != null)
return photo.data

Expand Down
Loading