Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit d7b73a35 authored by Ricki Hirner's avatar Ricki Hirner
Browse files

Tests

parent f20d9215
Loading
Loading
Loading
Loading
+26 −52
Original line number Diff line number Diff line
@@ -13,7 +13,6 @@ import androidx.test.filters.SmallTest
import at.bitfire.dav4jvm.DavResource
import at.bitfire.dav4jvm.property.ResourceType
import at.bitfire.davdroid.HttpClient
import at.bitfire.davdroid.model.ServiceDB.Collections
import okhttp3.HttpUrl
import okhttp3.mockwebserver.MockResponse
import okhttp3.mockwebserver.MockWebServer
@@ -22,7 +21,7 @@ import org.junit.Assert.*
import org.junit.Before
import org.junit.Test

class CollectionInfoTest {
class CollectionTest {

    private lateinit var httpClient: HttpClient
    private val server = MockWebServer()
@@ -40,7 +39,7 @@ class CollectionInfoTest {

    @Test
    @SmallTest
    fun testFromDavResource() {
    fun testFromDavResponseAddressBook() {
        // r/w address book
        server.enqueue(MockResponse()
                .setResponseCode(207)
@@ -55,17 +54,24 @@ class CollectionInfoTest {
                        "</response>" +
                        "</multistatus>"))

        var info: CollectionInfo? = null
        lateinit var info: Collection
        DavResource(httpClient.okHttpClient, server.url("/"))
                .propfind(0, ResourceType.NAME) { response, _ ->
            info = CollectionInfo(response)
            info = Collection.fromDavResponse(response) ?: throw IllegalArgumentException()
        }
        assertEquals(Collection.TYPE_ADDRESSBOOK, info.type)
        assertTrue(info.privWriteContent)
        assertTrue(info.privUnbind)
        assertNull(info.supportsVEVENT)
        assertNull(info.supportsVTODO)
        assertNull(info.supportsVJOURNAL)
        assertEquals("My Contacts", info.displayName)
        assertEquals("My Contacts Description", info.description)
    }
        assertEquals(CollectionInfo.Type.ADDRESS_BOOK, info?.type)
        assertTrue(info!!.privWriteContent)
        assertTrue(info!!.privUnbind)
        assertEquals("My Contacts", info?.displayName)
        assertEquals("My Contacts Description", info?.description)

    @Test
    @SmallTest
    fun testFromDavResponseCalendar() {
        // read-only calendar, no display name
        server.enqueue(MockResponse()
                .setResponseCode(207)
@@ -82,53 +88,21 @@ class CollectionInfoTest {
                        "</response>" +
                        "</multistatus>"))

        info = null
        lateinit var info: Collection
        DavResource(httpClient.okHttpClient, server.url("/"))
                .propfind(0, ResourceType.NAME) { response, _ ->
            info = CollectionInfo(response)
        }
        assertEquals(CollectionInfo.Type.CALENDAR, info?.type)
        assertFalse(info!!.privWriteContent)
        assertFalse(info!!.privUnbind)
        assertNull(info?.displayName)
        assertEquals("My Calendar", info?.description)
        assertEquals(0xFFFF0000.toInt(), info?.color)
        assertEquals("tzdata", info?.timeZone)
        assertTrue(info!!.supportsVEVENT)
        assertTrue(info!!.supportsVTODO)
                    info = Collection.fromDavResponse(response) ?: throw IllegalArgumentException()
                }

    @Test
    fun testFromDB() {
        val values = ContentValues()
        values.put(Collections.ID, 1)
        values.put(Collections.SERVICE_ID, 1)
        values.put(Collections.TYPE, CollectionInfo.Type.CALENDAR.name)
        values.put(Collections.URL, "http://example.com")
        values.put(Collections.PRIV_WRITE_CONTENT, 0)
        values.put(Collections.PRIV_UNBIND, 0)
        values.put(Collections.DISPLAY_NAME, "display name")
        values.put(Collections.DESCRIPTION, "description")
        values.put(Collections.COLOR, 0xFFFF0000)
        values.put(Collections.TIME_ZONE, "tzdata")
        values.put(Collections.SUPPORTS_VEVENT, 1)
        values.put(Collections.SUPPORTS_VTODO, 1)
        values.put(Collections.SYNC, 1)

        val info = CollectionInfo(values)
        assertEquals(CollectionInfo.Type.CALENDAR, info.type)
        assertEquals(1.toLong(), info.id)
        assertEquals(1.toLong(), info.serviceID)
        assertEquals(HttpUrl.parse("http://example.com/"), info.url)
        assertEquals(Collection.TYPE_CALENDAR, info.type)
        assertFalse(info.privWriteContent)
        assertFalse(info.privUnbind)
        assertEquals("display name", info.displayName)
        assertEquals("description", info.description)
        assertNull(info.displayName)
        assertEquals("My Calendar", info.description)
        assertEquals(0xFFFF0000.toInt(), info.color)
        assertEquals("tzdata", info.timeZone)
        assertTrue(info.supportsVEVENT)
        assertTrue(info.supportsVTODO)
        assertTrue(info.selected)
        assertEquals("tzdata", info.timezone)
        assertTrue(info.supportsVEVENT!!)
        assertTrue(info.supportsVTODO!!)
        assertTrue(info.supportsVJOURNAL!!)
    }

}
+0 −1
Original line number Diff line number Diff line
@@ -8,7 +8,6 @@

package at.bitfire.davdroid

import android.app.Application
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
+0 −1
Original line number Diff line number Diff line
@@ -10,7 +10,6 @@ package at.bitfire.davdroid

import android.accounts.Account
import android.annotation.TargetApi
import android.content.ContentProviderClient
import android.content.ContentResolver
import android.content.Context
import android.net.ConnectivityManager
+4 −0
Original line number Diff line number Diff line
@@ -94,6 +94,7 @@ data class Collection(
            var timezone: String? = null
            var supportsVEVENT: Boolean? = null
            var supportsVTODO: Boolean? = null
            var supportsVJOURNAL: Boolean? = null
            var source: String? = null
            when (type) {
                Collection.TYPE_ADDRESSBOOK -> {
@@ -107,9 +108,11 @@ data class Collection(
                    if (type == Collection.TYPE_CALENDAR) {
                        supportsVEVENT = true
                        supportsVTODO = true
                        supportsVJOURNAL = true
                        dav[SupportedCalendarComponentSet::class.java]?.let {
                            supportsVEVENT = it.supportsEvents
                            supportsVTODO = it.supportsTasks
                            supportsVJOURNAL = it.supportsJournal
                        }
                    } else { // Type.WEBCAL
                        dav[Source::class.java]?.let { source = it.hrefs.firstOrNull() }
@@ -129,6 +132,7 @@ data class Collection(
                    timezone = timezone,
                    supportsVEVENT = supportsVEVENT,
                    supportsVTODO = supportsVTODO,
                    supportsVJOURNAL = supportsVJOURNAL,
                    source = source
            )
        }
+3 −0
Original line number Diff line number Diff line
@@ -12,6 +12,9 @@ interface CollectionDao {
    @Query("SELECT * FROM collection WHERE serviceId=:serviceId")
    fun getByService(serviceId: Long): List<Collection>

    @Query("SELECT * FROM collection WHERE serviceId=:serviceId AND sync")
    fun getByServiceAndSync(serviceId: Long): List<Collection>

    @Query("SELECT * FROM collection WHERE serviceId=:serviceId AND type=:type")
    fun observeByServiceAndType(serviceId: Long, type: String): LiveData<List<Collection>>

Loading