Commit 80b1cb55 authored by Ricki Hirner's avatar Ricki Hirner 🐑

Tests, minor refactoring

parent 59dd6638
......@@ -6,7 +6,7 @@
* http://www.gnu.org/licenses/gpl.html
*/
package at.bitfire.davdroid;
package at.bitfire.davdroid
import android.support.test.InstrumentationRegistry.getInstrumentation
import at.bitfire.cert4android.CustomCertManager
......@@ -33,9 +33,9 @@ import javax.net.ssl.X509TrustManager
class CustomTlsSocketFactoryTest {
lateinit var certMgr: CustomCertManager
lateinit var factory: CustomTlsSocketFactory
val server = MockWebServer()
private lateinit var certMgr: CustomCertManager
private lateinit var factory: CustomTlsSocketFactory
private val server = MockWebServer()
@Before
fun startServer() {
......
......@@ -8,11 +8,11 @@
package at.bitfire.davdroid.settings
import android.content.ServiceConnection
import android.support.test.InstrumentationRegistry
import android.support.test.InstrumentationRegistry.getTargetContext
import at.bitfire.davdroid.App
import junit.framework.Assert.*
import junit.framework.Assert.assertFalse
import junit.framework.Assert.assertTrue
import org.junit.After
import org.junit.Before
import org.junit.Test
......
......@@ -39,7 +39,12 @@ import org.dmfs.tasks.contract.TaskContract
import java.util.*
import java.util.logging.Level
class AccountSettings @Throws(InvalidAccountException::class) constructor(
/**
* Manages settings of an account.
*
* @throws InvalidAccountException on construction when the account doesn't exist (anymore)
*/
class AccountSettings(
val context: Context,
val settings: ISettings,
val account: Account
......@@ -47,41 +52,40 @@ class AccountSettings @Throws(InvalidAccountException::class) constructor(
companion object {
val CURRENT_VERSION = 8
val KEY_SETTINGS_VERSION = "version"
const val CURRENT_VERSION = 8
const val KEY_SETTINGS_VERSION = "version"
val KEY_USERNAME = "user_name"
val KEY_CERTIFICATE_ALIAS = "certificate_alias"
const val KEY_USERNAME = "user_name"
const val KEY_CERTIFICATE_ALIAS = "certificate_alias"
val KEY_WIFI_ONLY = "wifi_only" // sync on WiFi only (default: false)
val KEY_WIFI_ONLY_SSIDS = "wifi_only_ssids" // restrict sync to specific WiFi SSIDs
const val KEY_WIFI_ONLY = "wifi_only" // sync on WiFi only (default: false)
const val KEY_WIFI_ONLY_SSIDS = "wifi_only_ssids" // restrict sync to specific WiFi SSIDs
/** Time range limitation to the past [in days]
value = null default value (DEFAULT_TIME_RANGE_PAST_DAYS)
< 0 (-1) no limit
>= 0 entries more than n days in the past won't be synchronized
*/
val KEY_TIME_RANGE_PAST_DAYS = "time_range_past_days"
val DEFAULT_TIME_RANGE_PAST_DAYS = 90
const val KEY_TIME_RANGE_PAST_DAYS = "time_range_past_days"
const val DEFAULT_TIME_RANGE_PAST_DAYS = 90
/* Whether DAVdroid sets the local calendar color to the value from service DB at every sync
value = null (not existing) true (default)
"0" false */
val KEY_MANAGE_CALENDAR_COLORS = "manage_calendar_colors"
const val KEY_MANAGE_CALENDAR_COLORS = "manage_calendar_colors"
/* Whether DAVdroid populates and uses CalendarContract.Colors
value = null (not existing) false (default)
"1" true */
val KEY_EVENT_COLORS = "event_colors"
const val KEY_EVENT_COLORS = "event_colors"
/** Contact group method:
value = null (not existing) groups as separate VCards (default)
"CATEGORIES" groups are per-contact CATEGORIES
*/
val KEY_CONTACT_GROUP_METHOD = "contact_group_method"
const val KEY_CONTACT_GROUP_METHOD = "contact_group_method"
@JvmField
val SYNC_INTERVAL_MANUALLY = -1L
const val SYNC_INTERVAL_MANUALLY = -1L
fun initialUserData(credentials: Credentials): Bundle {
val bundle = Bundle(2)
......
......@@ -22,14 +22,12 @@ import java.util.*
*/
object DavUtils {
@JvmStatic
fun ARGBtoCalDAVColor(colorWithAlpha: Int): String {
val alpha = (colorWithAlpha shr 24) and 0xFF
val color = colorWithAlpha and 0xFFFFFF
return String.format("#%06X%02X", color, alpha)
}
@JvmStatic
fun lastSegmentOfUrl(url: String): String {
val httpUrl = HttpUrl.parse(url) ?: throw IllegalArgumentException("url not parsable")
......
......@@ -64,7 +64,7 @@ class HttpClient private constructor(
certManager?.close()
}
class Builder @JvmOverloads constructor(
class Builder(
val context: Context? = null,
val settings: ISettings? = null,
accountSettings: AccountSettings? = null,
......
......@@ -27,7 +27,7 @@ class MemoryCookieStore: CookieJar {
* This ensures that cookies can be overwritten. [RFC 6265 5.3 Storage Model]
* Not thread-safe!
*/
val storage = MultiKeyMap.multiKeyMap(HashedMap<MultiKey<out String>, Cookie>())!!
private val storage = MultiKeyMap.multiKeyMap(HashedMap<MultiKey<out String>, Cookie>())!!
override fun saveFromResponse(url: HttpUrl, cookies: List<Cookie>) {
synchronized(storage) {
......
......@@ -32,7 +32,6 @@ class PackageChangedReceiver: BroadcastReceiver() {
companion object {
@JvmStatic
fun updateTaskSync(context: Context) {
val tasksInstalled = LocalTaskList.tasksProviderAvailable(context)
Logger.log.info("Package (un)installed; OpenTasks provider now available = $tasksInstalled")
......
......@@ -18,7 +18,7 @@ import java.util.logging.LogRecord
object LogcatHandler: Handler() {
private val MAX_LINE_LENGTH = 3000
private const val MAX_LINE_LENGTH = 3000
init {
formatter = PlainTextFormatter.LOGCAT
......
......@@ -19,10 +19,10 @@ class PlainTextFormatter private constructor(
): Formatter() {
companion object {
@JvmField val LOGCAT = PlainTextFormatter(true)
@JvmField val DEFAULT = PlainTextFormatter(false)
val LOGCAT = PlainTextFormatter(true)
val DEFAULT = PlainTextFormatter(false)
val MAX_MESSAGE_LENGTH = 20000
const val MAX_MESSAGE_LENGTH = 20000
}
override fun format(r: LogRecord): String {
......
......@@ -14,7 +14,7 @@ import at.bitfire.dav4android.property.*
import at.bitfire.davdroid.model.ServiceDB.Collections
import java.io.Serializable
data class CollectionInfo @JvmOverloads constructor(
data class CollectionInfo(
val url: String,
var id: Long? = null,
......@@ -48,7 +48,6 @@ data class CollectionInfo @JvmOverloads constructor(
companion object {
@JvmField
val DAV_PROPERTIES = arrayOf(
ResourceType.NAME,
CurrentUserPrivilegeSet.NAME,
......
......@@ -10,10 +10,10 @@ package at.bitfire.davdroid.model
import java.io.Serializable
class Credentials @JvmOverloads constructor(
@JvmField val userName: String? = null,
@JvmField val password: String? = null,
@JvmField val certificateAlias: String? = null
class Credentials(
val userName: String? = null,
val password: String? = null,
val certificateAlias: String? = null
): Serializable {
enum class Type {
......@@ -34,8 +34,7 @@ class Credentials @JvmOverloads constructor(
}
}
override fun toString(): String {
return "Credentials(type=$type, userName=$userName, certificateAlias=$certificateAlias)"
}
override fun toString() =
"Credentials(type=$type, userName=$userName, certificateAlias=$certificateAlias)"
}
\ No newline at end of file
......@@ -21,52 +21,45 @@ import java.util.logging.Level
class ServiceDB {
/*object Settings {
@JvmField val _TABLE = "settings"
@JvmField val NAME = "setting"
@JvmField val VALUE = "value"
}*/
object Services {
@JvmField val _TABLE = "services"
@JvmField val ID = "_id"
@JvmField val ACCOUNT_NAME = "accountName"
@JvmField val SERVICE = "service"
@JvmField val PRINCIPAL = "principal"
const val _TABLE = "services"
const val ID = "_id"
const val ACCOUNT_NAME = "accountName"
const val SERVICE = "service"
const val PRINCIPAL = "principal"
// allowed values for SERVICE column
@JvmField val SERVICE_CALDAV = "caldav"
@JvmField val SERVICE_CARDDAV = "carddav"
const val SERVICE_CALDAV = "caldav"
const val SERVICE_CARDDAV = "carddav"
}
object HomeSets {
@JvmField val _TABLE = "homesets"
@JvmField val ID = "_id"
@JvmField val SERVICE_ID = "serviceID"
@JvmField val URL = "url"
const val _TABLE = "homesets"
const val ID = "_id"
const val SERVICE_ID = "serviceID"
const val URL = "url"
}
object Collections {
@JvmField val _TABLE = "collections"
@JvmField val ID = "_id"
@JvmField val TYPE = "type"
@JvmField val SERVICE_ID = "serviceID"
@JvmField val URL = "url"
@JvmField val READ_ONLY = "readOnly"
@JvmField val FORCE_READ_ONLY = "forceReadOnly"
@JvmField val DISPLAY_NAME = "displayName"
@JvmField val DESCRIPTION = "description"
@JvmField val COLOR = "color"
@JvmField val TIME_ZONE = "timezone"
@JvmField val SUPPORTS_VEVENT = "supportsVEVENT"
@JvmField val SUPPORTS_VTODO = "supportsVTODO"
@JvmField val SOURCE = "source"
@JvmField val SYNC = "sync"
const val _TABLE = "collections"
const val ID = "_id"
const val TYPE = "type"
const val SERVICE_ID = "serviceID"
const val URL = "url"
const val READ_ONLY = "readOnly"
const val FORCE_READ_ONLY = "forceReadOnly"
const val DISPLAY_NAME = "displayName"
const val DESCRIPTION = "description"
const val COLOR = "color"
const val TIME_ZONE = "timezone"
const val SUPPORTS_VEVENT = "supportsVEVENT"
const val SUPPORTS_VTODO = "supportsVTODO"
const val SOURCE = "source"
const val SYNC = "sync"
}
companion object {
@JvmStatic
fun onRenameAccount(db: SQLiteDatabase, oldName: String, newName: String) {
val values = ContentValues(1)
values.put(Services.ACCOUNT_NAME, newName)
......@@ -81,8 +74,8 @@ class ServiceDB {
): SQLiteOpenHelper(context, DATABASE_NAME, null, DATABASE_VERSION), AutoCloseable {
companion object {
val DATABASE_NAME = "services.db"
val DATABASE_VERSION = 4
const val DATABASE_NAME = "services.db"
const val DATABASE_VERSION = 4
}
override fun onConfigure(db: SQLiteDatabase) {
......@@ -227,4 +220,4 @@ class ServiceDB {
}
}
}
}
\ No newline at end of file
......@@ -39,6 +39,6 @@ data class SyncState(
enum class Type { CTAG, SYNC_TOKEN }
override fun toString() =
"${type.name}:${value}"
"${type.name}:$value"
}
\ No newline at end of file
......@@ -12,17 +12,10 @@ import android.provider.ContactsContract.RawContacts
object UnknownProperties {
@JvmField
val CONTENT_ITEM_TYPE = "x.davdroid/unknown-properties"
const val CONTENT_ITEM_TYPE = "x.davdroid/unknown-properties"
@JvmField
val MIMETYPE = RawContacts.Data.MIMETYPE
@JvmField
val RAW_CONTACT_ID = RawContacts.Data.RAW_CONTACT_ID
@JvmField
val UNKNOWN_PROPERTIES = RawContacts.Data.DATA1
const val MIMETYPE = RawContacts.Data.MIMETYPE
const val RAW_CONTACT_ID = RawContacts.Data.RAW_CONTACT_ID
const val UNKNOWN_PROPERTIES = RawContacts.Data.DATA1
}
......@@ -32,7 +32,6 @@ class LocalContact: AndroidContact, LocalAddress {
const val COLUMN_FLAGS = ContactsContract.RawContacts.SYNC4
const val COLUMN_HASHCODE = ContactsContract.RawContacts.SYNC3
}
private val cachedGroupMemberships = HashSet<Long>()
......
......@@ -20,8 +20,8 @@ class SharedPreferencesProvider(
): Provider {
companion object {
private val META_VERSION = "version"
private val CURRENT_VERSION = 0
private const val META_VERSION = "version"
private const val CURRENT_VERSION = 0
}
private val preferences = PreferenceManager.getDefaultSharedPreferences(context)
......
......@@ -6,29 +6,28 @@
* http://www.gnu.org/licenses/gpl.html
*/
package at.bitfire.davdroid;
package at.bitfire.davdroid
import org.junit.Test;
import org.junit.Assert.assertEquals
import org.junit.Test
import static org.junit.Assert.assertEquals;
class DavUtilsTest {
public class TestDavUtils {
private static final String exampleURL = "http://example.com/";
val exampleURL = "http://example.com/"
@Test
public void testARGBtoCalDAVColor() {
assertEquals("#00000000", DavUtils.ARGBtoCalDAVColor(0));
assertEquals("#123456FF", DavUtils.ARGBtoCalDAVColor(0xFF123456));
assertEquals("#000000FF", DavUtils.ARGBtoCalDAVColor(0xFF000000));
fun testARGBtoCalDAVColor() {
assertEquals("#00000000", DavUtils.ARGBtoCalDAVColor(0))
assertEquals("#123456FF", DavUtils.ARGBtoCalDAVColor(0xFF123456.toInt()))
assertEquals("#000000FF", DavUtils.ARGBtoCalDAVColor(0xFF000000.toInt()))
}
@Test
public void testLastSegmentOfUrl() {
assertEquals("/", DavUtils.lastSegmentOfUrl(exampleURL));
assertEquals("dir", DavUtils.lastSegmentOfUrl(exampleURL + "dir"));
assertEquals("dir", DavUtils.lastSegmentOfUrl(exampleURL + "dir/"));
assertEquals("file.html", DavUtils.lastSegmentOfUrl(exampleURL + "dir/file.html"));
fun testLastSegmentOfUrl() {
assertEquals("/", DavUtils.lastSegmentOfUrl(exampleURL))
assertEquals("dir", DavUtils.lastSegmentOfUrl(exampleURL + "dir"))
assertEquals("dir", DavUtils.lastSegmentOfUrl(exampleURL + "dir/"))
assertEquals("file.html", DavUtils.lastSegmentOfUrl(exampleURL + "dir/file.html"))
}
}
......@@ -6,76 +6,69 @@
* http://www.gnu.org/licenses/gpl.html
*/
package at.bitfire.davdroid;
package at.bitfire.davdroid
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import okhttp3.Request
import okhttp3.mockwebserver.MockResponse
import okhttp3.mockwebserver.MockWebServer
import org.junit.After
import org.junit.Assert.assertEquals
import org.junit.Assert.assertNull
import org.junit.Before
import org.junit.Test
import java.io.IOException;
import java.net.URISyntaxException;
class HttpClientTest {
import okhttp3.HttpUrl;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.mockwebserver.MockResponse;
import okhttp3.mockwebserver.MockWebServer;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
public class HttpClientTest {
MockWebServer server;
HttpClient httpClient;
lateinit var server: MockWebServer
lateinit var httpClient: HttpClient
@Before
public void setUp() throws IOException {
httpClient = new HttpClient.Builder().build();
fun setUp() {
httpClient = HttpClient.Builder().build()
server = new MockWebServer();
server.start(30000);
server = MockWebServer()
server.start(30000)
}
@After
public void tearDown() throws IOException {
server.shutdown();
httpClient.close();
fun tearDown() {
server.shutdown()
httpClient.close()
}
@Test
public void testCookies() throws IOException, InterruptedException, URISyntaxException {
HttpUrl url = server.url("/test");
fun testCookies() {
val url = server.url("/test")
// set cookie for root path (/) and /test path in first response
server.enqueue(new MockResponse()
server.enqueue(MockResponse()
.setResponseCode(200)
.addHeader("Set-Cookie", "cookie1=1; path=/")
.addHeader("Set-Cookie", "cookie2=2")
.setBody("Cookie set"));
httpClient.getOkHttpClient().newCall(new Request.Builder()
.setBody("Cookie set"))
httpClient.okHttpClient.newCall(Request.Builder()
.get().url(url)
.build()).execute();
assertNull(server.takeRequest().getHeader("Cookie"));
.build()).execute()
assertNull(server.takeRequest().getHeader("Cookie"))
// cookie should be sent with second request
// second response lets first cookie expire and overwrites second cookie
server.enqueue(new MockResponse()
server.enqueue(MockResponse()
.addHeader("Set-Cookie", "cookie1=1a; path=/; Max-Age=0")
.addHeader("Set-Cookie", "cookie2=2a")
.setResponseCode(200));
httpClient.getOkHttpClient().newCall(new Request.Builder()
.setResponseCode(200))
httpClient.okHttpClient.newCall(Request.Builder()
.get().url(url)
.build()).execute();
assertEquals("cookie2=2; cookie1=1", server.takeRequest().getHeader("Cookie"));
.build()).execute()
assertEquals("cookie2=2; cookie1=1", server.takeRequest().getHeader("Cookie"))
server.enqueue(new MockResponse()
.setResponseCode(200));
httpClient.getOkHttpClient().newCall(new Request.Builder()
server.enqueue(MockResponse()
.setResponseCode(200))
httpClient.okHttpClient.newCall(Request.Builder()
.get().url(url)
.build()).execute();
assertEquals("cookie2=2a", server.takeRequest().getHeader("Cookie"));
.build()).execute()
assertEquals("cookie2=2a", server.takeRequest().getHeader("Cookie"))
}
}
/*
* Copyright © Ricki Hirner (bitfire web engineering).
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the GNU Public License v3.0
* which accompanies this distribution, and is available at
* http://www.gnu.org/licenses/gpl.html
*/
package at.bitfire.davdroid.syncadapter;
import org.junit.Test;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import static org.junit.Assert.assertEquals;
public class SyncManagerTest {
@Test
public void testUnion() {
Set<Integer> A = new HashSet<>(Arrays.asList(new Integer[] { 1,2,3 }));
Set<Integer> B = new HashSet<>(Arrays.asList(new Integer[] { 1,4,5 }));
assertEquals(new HashSet<>(Arrays.asList(new Integer[] { 2,3,4,5 })), SyncManager.Companion.disjunct(A, B));
assertEquals(new HashSet<>(Arrays.asList(new Integer[] { 2,3,4,5 })), SyncManager.Companion.disjunct(B, A));
}
}
Subproject commit 568f0285be35a5f95b4c0d00f7b40c3900c9ce37
Subproject commit 1169e15017a900d7fb3f29e42ea18d45e3c5d33a
Subproject commit bc0ab23403c1cd72513bb2ee69bf1238d91b133e
Subproject commit 0b1a898fd2cc6d84ea8a7d66984c62aa5e240b10
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment