Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/bitfireAT/vcard4android.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRicki Hirner <hirner@bitfire.at>2021-01-05 15:36:56 +0300
committerRicki Hirner <hirner@bitfire.at>2021-01-05 15:36:56 +0300
commite4b7025257d898445497b5e4f3f43efdbdc9e455 (patch)
tree226ec6b5af05f5380e7cd687354b01bd8ac0f1d5
parent368898fc0221e65a5cb091a6d4a24c98219ba157 (diff)
Nickname and Website URL: treat TYPE value as case-insensitive
-rw-r--r--src/androidTest/java/at/bitfire/vcard4android/AndroidContactTest.kt106
-rw-r--r--src/main/java/at/bitfire/vcard4android/AndroidContact.kt19
2 files changed, 96 insertions, 29 deletions
diff --git a/src/androidTest/java/at/bitfire/vcard4android/AndroidContactTest.kt b/src/androidTest/java/at/bitfire/vcard4android/AndroidContactTest.kt
index 72c5d09..66f13a5 100644
--- a/src/androidTest/java/at/bitfire/vcard4android/AndroidContactTest.kt
+++ b/src/androidTest/java/at/bitfire/vcard4android/AndroidContactTest.kt
@@ -204,16 +204,17 @@ class AndroidContactTest {
}
@Test
- @SmallTest
fun testEmailTypes() {
val vCard = "BEGIN:VCARD\r\n" +
"VERSION:4.0\r\n" +
"FN:Test\r\n" +
"EMAIL;TYPE=internet;TYPE=work:work@example.com\r\n" +
- "EMAIL;TYPE=home:home@example.com\r\n" +
+ "EMAIL;TYPE=HOME:home@example.com\r\n" +
"EMAIL;TYPE=internet,pref:other1@example.com\r\n" +
- "EMAIL;TYPE=x400,other:other2@example.com\r\n" +
+ "EMAIL;TYPE=x400,oTHer:other2@example.com\r\n" +
"EMAIL;TYPE=x-mobile:mobile@example.com\r\n" +
+ "group1.EMAIL;TYPE=x-with-label:withlabel@example.com\r\n" +
+ "group1.X-ABLABEL:With label and x-type\r\n" +
"END:VCARD\r\n"
val contacts = Contact.fromReader(StringReader(vCard), null)
@@ -223,25 +224,89 @@ class AndroidContactTest {
val dbContact2 = addressBook.findContactByID(dbContact.id!!)
try {
val contact2 = dbContact2.contact!!
- assertEquals("work@example.com", contact2.emails[0].property.value)
- assertArrayEquals(arrayOf(EmailType.WORK), contact2.emails[0].property.types.toTypedArray())
- assertNull(contact2.emails[0].property.pref)
-
- assertEquals("home@example.com", contact2.emails[1].property.value)
- assertArrayEquals(arrayOf(EmailType.HOME), contact2.emails[1].property.types.toTypedArray())
- assertNull(contact2.emails[1].property.pref)
+ assertEquals(6, contact2.emails.size)
+
+ contact2.emails[0].property.let { email ->
+ assertEquals("work@example.com", email.value)
+ assertArrayEquals(arrayOf(EmailType.WORK), email.types.toTypedArray())
+ assertNull(email.pref)
+ }
+
+ contact2.emails[1].property.let { email ->
+ assertEquals("home@example.com", email.value)
+ assertArrayEquals(arrayOf(EmailType.HOME), email.types.toTypedArray())
+ assertNull(email.pref)
+ }
+
+ contact2.emails[2].property.let { email ->
+ assertEquals("other1@example.com", email.value)
+ assertTrue(email.types.isEmpty())
+ assertNotEquals(0, email.pref)
+ }
+
+ contact2.emails[3].property.let { email ->
+ assertEquals("other2@example.com", email.value)
+ assertTrue(email.types.isEmpty())
+ assertNull(email.pref)
+ }
+
+ contact2.emails[4].property.let { email ->
+ assertEquals("mobile@example.com", email.value)
+ assertArrayEquals(arrayOf(Contact.EMAIL_TYPE_MOBILE), email.types.toTypedArray())
+ assertNull(email.pref)
+ }
+
+ contact2.emails[5].let { email ->
+ assertEquals("withlabel@example.com", email.property.value)
+ assertEquals(EmailType.get("x-with-label-and-x-type"), email.property.types.first())
+ assertEquals("With label and x-type", email.label)
+ }
+ } finally {
+ dbContact2.delete()
+ }
+ }
- assertEquals("other1@example.com", contact2.emails[2].property.value)
- assertTrue(contact2.emails[2].property.types.isEmpty())
- assertNotEquals(0, contact2.emails[2].property.pref)
+ @Test
+ fun testWebsiteTypes() {
+ val vCard = "BEGIN:VCARD\r\n" +
+ "VERSION:4.0\r\n" +
+ "FN:Test\r\n" +
+ "URL;TYPE=home:http://home.example\r\n" +
+ "URL;TYPE=WORK:http://WORK.example\r\n" +
+ "URL;TYPE=Custom:http://Custom.example\r\n" +
+ "group1.URL;TYPE=x-custom-with-label:http://Custom.example\r\n" +
+ "group1.X-ABLABEL:Custom (with label)\r\n" +
+ "END:VCARD\r\n"
+ val contacts = Contact.fromReader(StringReader(vCard), null)
- assertEquals("other2@example.com", contact2.emails[3].property.value)
- assertTrue(contact2.emails[3].property.types.isEmpty())
- assertNull(contact2.emails[3].property.pref)
+ val dbContact = AndroidContact(addressBook, contacts.first(), null, null)
+ dbContact.add()
- assertEquals("mobile@example.com", contact2.emails[4].property.value)
- assertArrayEquals(arrayOf(Contact.EMAIL_TYPE_MOBILE), contact2.emails[4].property.types.toTypedArray())
- assertNull(contact2.emails[4].property.pref)
+ val dbContact2 = addressBook.findContactByID(dbContact.id!!)
+ try {
+ val contact2 = dbContact2.contact!!
+ assertEquals(4, contact2.urls.size)
+
+ contact2.urls[0].property.let { url ->
+ assertEquals("http://home.example", url.value)
+ assertEquals("home", url.type)
+ }
+
+ contact2.urls[1].property.let { url ->
+ assertEquals("http://WORK.example", url.value)
+ assertEquals("work", url.type)
+ }
+
+ contact2.urls[2].property.let { url ->
+ assertEquals("http://Custom.example", url.value)
+ assertEquals("x-custom", url.type)
+ }
+
+ contact2.urls[3].let { url ->
+ assertEquals("http://Custom.example", url.property.value)
+ assertEquals("x-custom-with-label", url.property.type)
+ assertEquals("Custom (with label)", url.label)
+ }
} finally {
dbContact2.delete()
}
@@ -250,7 +315,7 @@ class AndroidContactTest {
@Test
fun testLabelToXName() {
- assertEquals("X-AUNTIES_HOME", AndroidContact.labelToXName("auntie's home"))
+ assertEquals("x-aunties-home", AndroidContact.labelToXName("auntie's home"))
}
@Test
@@ -262,6 +327,7 @@ class AndroidContactTest {
@Test
fun testXNameToLabel() {
+ assertEquals("Aunties Home", AndroidContact.xNameToLabel("X-AUNTIES-HOME"))
assertEquals("Aunties Home", AndroidContact.xNameToLabel("X-AUNTIES_HOME"))
}
diff --git a/src/main/java/at/bitfire/vcard4android/AndroidContact.kt b/src/main/java/at/bitfire/vcard4android/AndroidContact.kt
index a1a9aee..0356ef8 100644
--- a/src/main/java/at/bitfire/vcard4android/AndroidContact.kt
+++ b/src/main/java/at/bitfire/vcard4android/AndroidContact.kt
@@ -52,18 +52,19 @@ open class AndroidContact(
const val COLUMN_UID = RawContacts.SYNC1
const val COLUMN_ETAG = RawContacts.SYNC2
- fun labelToXName(label: String) = "X-" + label
- .replace(" ","_")
+ fun labelToXName(label: String) = "x-" + label
+ .replace(' ','-')
.replace(Regex("[^\\p{L}\\p{Nd}\\-_]"), "")
- .toUpperCase(Locale.getDefault())
+ .toLowerCase()
fun xNameToLabel(xname: String): String {
- // "X-MY_PROPERTY"
+ // "x-my_property"
var s = xname.toLowerCase(Locale.getDefault()) // 1. ensure lower case -> "x-my_property"
- if (s.startsWith("x-")) // 2. remove x- from beginning -> "my_property"
+ if (s.startsWith("x-")) // 2. remove x- from beginning -> "my_property"
s = s.substring(2)
- s = s.replace('_', ' ') // 3. replace "_" by " " -> "my property"
- return WordUtils.capitalize(s) // 4. capitalize -> "My Property"
+ s = s .replace('_', ' ') // 3. replace "_" and "-" by " " -> "my property"
+ .replace('-', ' ')
+ return WordUtils.capitalize(s) // 4. capitalize -> "My Property"
}
fun toURIScheme(s: String?) =
@@ -916,7 +917,7 @@ open class AndroidContact(
val typeCode: Int
var typeLabel: String? = null
- val type = nick.type
+ val type = nick.type?.toLowerCase()
typeCode = when (type) {
Contact.NICKNAME_TYPE_MAIDEN_NAME -> Nickname.TYPE_MAIDEN_NAME
Contact.NICKNAME_TYPE_SHORT_NAME -> Nickname.TYPE_SHORT_NAME
@@ -1019,7 +1020,7 @@ open class AndroidContact(
typeCode = Website.TYPE_CUSTOM
typeLabel = labeledUrl.label
} else {
- val type = url.type
+ val type = url.type?.toLowerCase()
typeCode = when (type) {
Contact.URL_TYPE_HOMEPAGE -> Website.TYPE_HOMEPAGE
Contact.URL_TYPE_BLOG -> Website.TYPE_BLOG