Modern Apps git

← Modern-Apps.git

contacts: add tests for pure/data helpers (details, prefill, vcard, intents)

Commit
0abcddfa5c6944b1896e25b54f0f5bb8e77c4a42
Author
Vayun Mathur <[email protected]>
When
2026-11-06 18:55 UTC (29 days ago)
Parents
b1e994a
7 test files, ~55 tests (JVM + Robolectric), all behavior-asserting:
detail with* copies + SIM-move loss rules, group-chip membership, SIM
account types + subscription ids, phone format/age/prefill/package
presence, intent helpers (vcf/type/uri), commpref resolver, vCard TYPE
mapping on import. Full contacts suite 197 tests green.

(cherry picked from commit 881f2e66230e40ab1bc8b6ead690c5dd3b53bfde)

Diff

contacts/src/test/java/com/vayunmathur/contacts/pure/CommPrefResolverTest.kt
diff --git a/contacts/src/test/java/com/vayunmathur/contacts/pure/CommPrefResolverTest.kt b/contacts/src/test/java/com/vayunmathur/contacts/pure/CommPrefResolverTest.ktnew file mode 100644index 0000000000..6fdb3c7233--- /dev/null+++ b/contacts/src/test/java/com/vayunmathur/contacts/pure/CommPrefResolverTest.kt@@ -0,0 +1,92 @@+package com.vayunmathur.contacts.pure++import com.vayunmathur.contacts.data.CommAction+import com.vayunmathur.contacts.data.CommPlatform+import com.vayunmathur.contacts.data.CommPref+import com.vayunmathur.contacts.data.CommPrefResolver+import com.vayunmathur.contacts.data.PhoneNumber+import com.vayunmathur.contacts.util.ContactPlatforms+import kotlin.test.Test+import kotlin.test.assertEquals+import kotlin.test.assertFalse+import kotlin.test.assertTrue++/**+ * Pure-JVM coverage for the comm-pref decision logic ([CommPrefResolver]):+ * which platforms the picker offers per action, when a saved pref stays+ * valid, and when the picker collapses to SIM-only.+ */+class CommPrefResolverTest {++    private val empty = ContactPlatforms()+    private val full = ContactPlatforms(+        whatsAppCallId = 1L, whatsAppVideoId = 2L, whatsAppMessageId = 3L,+        signalCallId = 4L, signalVideoId = 5L, signalMessageId = 6L,+        telegramCallId = 7L, telegramVideoId = 8L, telegramMessageId = 9L,+    )++    @Test+    fun `call and message always offer sim last`() {+        assertEquals(+            listOf(CommPlatform.SIGNAL, CommPlatform.WHATSAPP, CommPlatform.TELEGRAM, CommPlatform.SIM),+            CommPrefResolver.availablePlatformsFor(CommAction.CALL, full, false),+        )+        assertEquals(listOf(CommPlatform.SIM), CommPrefResolver.availablePlatformsFor(CommAction.CALL, empty, false))+        assertEquals(+            listOf(CommPlatform.SIGNAL, CommPlatform.WHATSAPP, CommPlatform.TELEGRAM, CommPlatform.SIM),+            CommPrefResolver.availablePlatformsFor(CommAction.MESSAGE, full, false),+        )+    }++    @Test+    fun `video has no sim and meet only when installed`() {+        assertEquals(+            listOf(CommPlatform.WHATSAPP, CommPlatform.SIGNAL, CommPlatform.TELEGRAM),+            CommPrefResolver.availablePlatformsFor(CommAction.VIDEO, full, false),+        )+        assertEquals(+            listOf(CommPlatform.MEET, CommPlatform.WHATSAPP, CommPlatform.SIGNAL, CommPlatform.TELEGRAM),+            CommPrefResolver.availablePlatformsFor(CommAction.VIDEO, full, true),+        )+        assertTrue(CommPrefResolver.availablePlatformsFor(CommAction.VIDEO, empty, false).isEmpty())+        assertEquals(+            listOf(CommPlatform.MEET),+            CommPrefResolver.availablePlatformsFor(CommAction.VIDEO, empty, true),+        )+    }++    @Test+    fun `pref valid only when number exists and platform offered`() {+        val phones = listOf(PhoneNumber(1, "+1 415-555-0111", 2))+        val pref = CommPref(1, "+14155550111", CommPlatform.WHATSAPP)+        assertTrue(CommPrefResolver.isPrefValidFor(pref, CommAction.MESSAGE, phones, full, false))+        // Number gone from the contact.+        assertFalse(CommPrefResolver.isPrefValidFor(pref, CommAction.MESSAGE, emptyList(), full, false))+        // Platform no longer installed.+        assertFalse(CommPrefResolver.isPrefValidFor(pref, CommAction.MESSAGE, phones, empty, false))+        // Null or blank pref.+        assertFalse(CommPrefResolver.isPrefValidFor(null, CommAction.MESSAGE, phones, full, false))+        assertFalse(+            CommPrefResolver.isPrefValidFor(pref.copy(number = "  "), CommAction.MESSAGE, phones, full, false),+        )+    }++    @Test+    fun `sim only when nothing else available`() {+        assertTrue(CommPrefResolver.isSimOnly(CommAction.CALL, empty, false))+        assertTrue(CommPrefResolver.isSimOnly(CommAction.MESSAGE, empty, true))+        assertFalse(CommPrefResolver.isSimOnly(CommAction.CALL, full, false))+        // Video never collapses to SIM (SIM is not a video option).+        assertFalse(CommPrefResolver.isSimOnly(CommAction.VIDEO, empty, false))+    }++    @Test+    fun `platform flags aggregate the row ids`() {+        assertFalse(empty.hasAnyPlatform)+        assertTrue(full.hasWhatsApp)+        assertTrue(full.hasSignal)+        assertTrue(full.hasTelegram)+        assertTrue(full.hasAnyPlatform)+        assertFalse(ContactPlatforms(whatsAppCallId = 1L).hasSignal)+    }+}
contacts/src/test/java/com/vayunmathur/contacts/pure/ContactDetailsLossTest.kt
diff --git a/contacts/src/test/java/com/vayunmathur/contacts/pure/ContactDetailsLossTest.kt b/contacts/src/test/java/com/vayunmathur/contacts/pure/ContactDetailsLossTest.ktnew file mode 100644index 0000000000..3255b8b3b5--- /dev/null+++ b/contacts/src/test/java/com/vayunmathur/contacts/pure/ContactDetailsLossTest.kt@@ -0,0 +1,230 @@+package com.vayunmathur.contacts.pure++import com.vayunmathur.contacts.data.Address+import com.vayunmathur.contacts.data.CommPref+import com.vayunmathur.contacts.data.Contact+import com.vayunmathur.contacts.data.ContactDetails+import com.vayunmathur.contacts.data.Email+import com.vayunmathur.contacts.data.Event+import com.vayunmathur.contacts.data.Gender+import com.vayunmathur.contacts.data.GroupMembership+import com.vayunmathur.contacts.data.Name+import com.vayunmathur.contacts.data.Nickname+import com.vayunmathur.contacts.data.Note+import com.vayunmathur.contacts.data.Organization+import com.vayunmathur.contacts.data.PhoneNumber+import com.vayunmathur.contacts.data.Photo+import com.vayunmathur.contacts.util.SimDroppedField+import com.vayunmathur.contacts.util.computeSimLoss+import kotlin.test.Test+import kotlin.test.assertEquals+import kotlin.test.assertFailsWith+import kotlin.test.assertTrue+import kotlinx.datetime.LocalDate++/**+ * Pure-JVM coverage for the contact detail model ([ContactDetails], detail+ * `with*` copies) and the SIM-move loss computation ([computeSimLoss]).+ *+ * No provider, no context: every `with*` copy shape and every loss rule is+ * pinned so a field added to the model cannot silently escape the SIM+ * warn-before-truncate dialog.+ */+class ContactDetailsLossTest {++    private fun details(+        phones: List<PhoneNumber> = emptyList(),+        emails: List<Email> = emptyList(),+        addresses: List<Address> = emptyList(),+        dates: List<Event> = emptyList(),+        photos: List<Photo> = emptyList(),+        names: List<Name> = emptyList(),+        orgs: List<Organization> = emptyList(),+        notes: List<Note> = emptyList(),+        nicknames: List<Nickname> = emptyList(),+        groups: List<GroupMembership> = emptyList(),+        genders: List<Gender> = emptyList(),+        commPrefs: List<CommPref> = emptyList(),+    ) = ContactDetails(phones, emails, addresses, dates, photos, names, orgs, notes, nicknames, groups, genders, commPrefs)++    private fun contact(+        details: ContactDetails = ContactDetails.empty(),+        favorite: Boolean = false,+        ringtone: String? = null,+    ) = Contact(+        id = 1L,+        accountType = "com.google",+        accountName = "a",+        isFavorite = favorite,+        details = details,+        customRingtone = ringtone,+    )++    private fun phone(id: Long = 1L, number: String = "+1555", type: Int = 2) = PhoneNumber(id, number, type)+    private fun email(id: Long = 1L, address: String = "[email protected]", type: Int = 1) = Email(id, address, type)++    // -- ContactDetails aggregation ------------------------------------------------++    @Test+    fun `all concatenates every detail list in order`() {+        val d = details(+            phones = listOf(phone()),+            emails = listOf(email()),+            addresses = listOf(Address(1, "123 Main", 1)),+            dates = listOf(Event(1, LocalDate(1990, 5, 6), 3)),+            photos = listOf(Photo(1, "blob")),+            names = listOf(Name(1, "", "Ada", "", "L", "")),+            orgs = listOf(Organization(1, "Acme")),+            notes = listOf(Note(1, "n")),+            nicknames = listOf(Nickname(1, "Al", 1)),+            groups = listOf(GroupMembership(1, 7L)),+            genders = listOf(Gender(1, Gender.FEMALE)),+        )+        val all = d.all()+        assertEquals(11, all.size)+        assertEquals("+1555", all[0].value)+        assertEquals("[email protected]", all[1].value)+        assertEquals("123 Main", all[2].value)+        assertEquals("1990-05-06", all[3].value)+        assertEquals("blob", all[4].value)+        assertEquals("Ada L", all[5].value)+        assertEquals("Acme", all[6].value)+        assertEquals("n", all[7].value)+        assertEquals("Al", all[8].value)+        assertEquals("7", all[9].value)+        assertEquals(Gender.FEMALE, all[10].value)+    }++    @Test+    fun `empty details aggregate to nothing and commPref is null`() {+        val d = ContactDetails.empty()+        assertTrue(d.all().isEmpty())+        assertEquals(null, d.commPref)+    }++    @Test+    fun `commPref is the first comm pref`() {+        val a = CommPref(id = 1L, number = "+1555", platform = com.vayunmathur.contacts.data.CommPlatform.WHATSAPP)+        val b = CommPref(id = 2L, number = "+1666", platform = com.vayunmathur.contacts.data.CommPlatform.SIGNAL)+        assertEquals(a, details(commPrefs = listOf(a, b)).commPref)+    }++    @Test+    fun `commpref withValue parses platform and number`() {+        val parsed = CommPref(1, "", com.vayunmathur.contacts.data.CommPlatform.SIM).withValue("signal;+1555")+        assertEquals("+1555", parsed.number)+        assertEquals(com.vayunmathur.contacts.data.CommPlatform.SIGNAL, parsed.platform)+        assertEquals("signal;+1555", parsed.value)+        // Unknown keys fall back to SIM.+        assertEquals(+            com.vayunmathur.contacts.data.CommPlatform.SIM,+            CommPref(1, "").withValue("nope;+1").platform,+        )+    }++    // -- with* copies ----------------------------------------------------------------++    @Test+    fun `phone email and address copies replace one field`() {+        assertEquals("+1999", phone().withValue("+1999").number)+        assertEquals(9, phone().withType(9).type)+        assertEquals("Work", phone().withLabel("Work").label)+        assertEquals("[email protected]", email().withValue("[email protected]").address)+        val addr = Address(3, "Old", 1, "Lbl")+        assertEquals("New", addr.withValue("New").formattedAddress)+        assertEquals(2, addr.withType(2).type)+        assertEquals("L2", addr.withLabel("L2").label)+    }++    @Test+    fun `event copies reparse the date`() {+        val e = Event(1, LocalDate(2000, 1, 2), 3, "BD")+        assertEquals(LocalDate(2010, 12, 31), e.withValue("2010-12-31").startDate)+        assertEquals(1, e.withType(1).type)+        assertEquals("X", e.withLabel("X").label)+    }++    @Test+    fun `gender withValue splits code and text`() {+        assertEquals(Gender(1, "O", "X"), Gender(1, "U").withValue("O;X"))+        assertEquals("O;X", Gender(1, "O", "X").value)+        assertEquals("F", Gender(1, "F").value)+    }++    @Test+    fun `group membership withValue parses the id`() {+        assertEquals(42L, GroupMembership(1, 7L).withValue("42").groupId)+    }++    @Test+    fun `immutable details throw on unsupported copies`() {+        assertFailsWith<UnsupportedOperationException> { Photo(1, "b").withType(2) }+        assertFailsWith<UnsupportedOperationException> { Organization(1, "c").withType(2) }+        assertFailsWith<UnsupportedOperationException> { Name(1, "", "A", "", "B", "").withValue("x") }+        assertFailsWith<UnsupportedOperationException> { Name(1, "", "A", "", "B", "").withType(1) }+        assertFailsWith<UnsupportedOperationException> { Note(1, "n").withType(2) }+        assertEquals(Note(1, "n"), Note(1, "n").withLabel("ignored"))+        assertEquals(Nickname(1, "n", 1), Nickname(1, "n", 1).withLabel("ignored"))+        assertFailsWith<UnsupportedOperationException> { GroupMembership(1, 2L).withType(3) }+        assertFailsWith<UnsupportedOperationException> { Gender(1, "F").withType(1) }+        assertEquals(Gender(1, "F"), Gender(1, "F").withLabel("ignored"))+        assertFailsWith<UnsupportedOperationException> { CommPref(1, "+1").withType(0) }+        assertEquals(CommPref(1, "+1"), CommPref(1, "+1").withLabel("ignored"))+    }++    @Test+    fun `contact derived views fall back to blanks`() {+        val c = contact()+        assertEquals("", c.name.value)+        assertEquals(null, c.photo)+        assertEquals("", c.org.company)+        assertEquals("", c.note.content)+        assertEquals(null, c.birthday)+    }++    // -- computeSimLoss ---------------------------------------------------------------++    @Test+    fun `bare contact with one phone loses nothing`() {+        assertTrue(computeSimLoss(contact(details(phones = listOf(phone())))).isEmpty())+    }++    @Test+    fun `every rich field is reported`() {+        val d = details(+            phones = listOf(phone(1), phone(2)),+            emails = listOf(email(1), email(2)),+            addresses = listOf(Address(1, "x", 1)),+            dates = listOf(Event(1, LocalDate(1990, 1, 1), 3)),+            photos = listOf(Photo(1, "b")),+            orgs = listOf(Organization(1, "Acme")),+            notes = listOf(Note(1, "keep")),+            nicknames = listOf(Nickname(1, "Al", 1)),+            groups = listOf(GroupMembership(1, 3L)),+            genders = listOf(Gender(1, Gender.MALE)),+            commPrefs = listOf(CommPref(id = 9L, number = "+1555")),+        )+        val c = contact(d, favorite = true, ringtone = "content://ring")+        assertEquals(+            setOf(+                SimDroppedField.PHOTO, SimDroppedField.EXTRA_PHONES, SimDroppedField.EXTRA_EMAILS,+                SimDroppedField.ADDRESSES, SimDroppedField.DATES, SimDroppedField.ORGANIZATION,+                SimDroppedField.NOTE, SimDroppedField.NICKNAME, SimDroppedField.GROUPS,+                SimDroppedField.GENDER, SimDroppedField.COMM_PREF, SimDroppedField.RINGTONE,+                SimDroppedField.FAVORITE,+            ),+            computeSimLoss(c).toSet(),+        )+    }++    @Test+    fun `blank rich fields are not losses`() {+        val d = details(+            orgs = listOf(Organization(1, "  ")),+            notes = listOf(Note(1, "")),+            nicknames = listOf(Nickname(1, " ", 1)),+            genders = listOf(Gender(1, "")),+        )+        assertTrue(computeSimLoss(contact(d)).isEmpty())+    }+}
contacts/src/test/java/com/vayunmathur/contacts/pure/ContactListHelpersTest.kt
diff --git a/contacts/src/test/java/com/vayunmathur/contacts/pure/ContactListHelpersTest.kt b/contacts/src/test/java/com/vayunmathur/contacts/pure/ContactListHelpersTest.ktnew file mode 100644index 0000000000..dd9516b4c1--- /dev/null+++ b/contacts/src/test/java/com/vayunmathur/contacts/pure/ContactListHelpersTest.kt@@ -0,0 +1,53 @@+package com.vayunmathur.contacts.pure++import com.vayunmathur.contacts.data.Contact+import com.vayunmathur.contacts.data.ContactDetails+import com.vayunmathur.contacts.data.ContactGroup+import com.vayunmathur.contacts.data.GroupMembership+import com.vayunmathur.contacts.data.Name+import com.vayunmathur.contacts.data.Nickname+import com.vayunmathur.contacts.ui.contactGroupsOf+import kotlin.test.Test+import kotlin.test.assertEquals+import kotlin.test.assertTrue++/**+ * Pure-JVM coverage for contact list helpers ([contactGroupsOf]): the+ * group chips on a contact row show exactly the non-blank groups the+ * contact belongs to, matched by group id.+ */+class ContactListHelpersTest {++    private fun contact(groupIds: List<Long> = emptyList(), name: String = "Ada"): Contact {+        val details = ContactDetails.empty().copy(+            names = listOf(Name(1, "", name, "", "", "")),+            nicknames = listOf(Nickname(1, "", 1)),+            groups = groupIds.mapIndexed { i, gid -> GroupMembership(i.toLong(), gid) },+        )+        return Contact(id = 3L, accountType = null, accountName = null, isFavorite = false, details = details)+    }++    @Test+    fun `membership resolves to named groups only`() {+        val c = contact(listOf(7L, 9L))+        val groups = listOf(+            ContactGroup(id = 7L, name = "Family"),+            ContactGroup(id = 8L, name = "Work"),+            ContactGroup(id = 9L, name = "   "),+        )+        assertEquals(listOf(ContactGroup(id = 7L, name = "Family")), contactGroupsOf(c, groups))+    }++    @Test+    fun `no memberships yields no groups`() {+        val groups = listOf(ContactGroup(id = 7L, name = "Family"))+        assertTrue(contactGroupsOf(contact(), groups).isEmpty())+        assertTrue(contactGroupsOf(contact(listOf(7L)), emptyList()).isEmpty())+    }++    @Test+    fun `unknown group ids are ignored`() {+        val groups = listOf(ContactGroup(id = 7L, name = "Family"))+        assertTrue(contactGroupsOf(contact(listOf(42L)), groups).isEmpty())+    }+}
contacts/src/test/java/com/vayunmathur/contacts/pure/ContactSmallHelpersTest.kt
diff --git a/contacts/src/test/java/com/vayunmathur/contacts/pure/ContactSmallHelpersTest.kt b/contacts/src/test/java/com/vayunmathur/contacts/pure/ContactSmallHelpersTest.ktnew file mode 100644index 0000000000..44b8d699d1--- /dev/null+++ b/contacts/src/test/java/com/vayunmathur/contacts/pure/ContactSmallHelpersTest.kt@@ -0,0 +1,106 @@+package com.vayunmathur.contacts.pure++import androidx.test.core.app.ApplicationProvider+import com.vayunmathur.contacts.data.ContactPrefill+import com.vayunmathur.contacts.data.PrefillValue+import com.vayunmathur.contacts.ui.calculateAge+import com.vayunmathur.contacts.ui.formatPhoneNumber+import com.vayunmathur.contacts.util.PackageUtils+import kotlin.test.Test+import kotlin.test.assertEquals+import kotlin.test.assertFalse+import kotlin.test.assertNull+import kotlin.test.assertTrue+import kotlinx.datetime.LocalDate+import org.junit.runner.RunWith+import org.robolectric.RobolectricTestRunner++/**+ * Pure + Robolectric coverage for small contact helpers: phone formatting+ * ([formatPhoneNumber]), birthday age ([calculateAge]), intent prefill+ * ([ContactPrefill]), and app-presence checks ([PackageUtils]).+ *+ * libphonenumber runs on the JVM; the package-manager checks run under+ * Robolectric where unknown packages are simply absent.+ */+@RunWith(RobolectricTestRunner::class)+class ContactSmallHelpersTest {++    // -- formatPhoneNumber -----------------------------------------------------++    @Test+    fun `us number formats nationally by default`() {+        assertEquals("(415) 555-0111", formatPhoneNumber("+14155550111"))+    }++    @Test+    fun `foreign number formats internationally`() {+        assertEquals("+44 20 7946 0018", formatPhoneNumber("+442079460018"))+    }++    @Test+    fun `garbage passes through unchanged`() {+        assertEquals("not-a-number", formatPhoneNumber("not-a-number"))+        assertEquals("", formatPhoneNumber(""))+        assertEquals("123", formatPhoneNumber("123"))+    }++    // -- calculateAge -----------------------------------------------------------++    @Test+    fun `age counts completed years`() {+        val today = LocalDate(2026, 11, 6)+        assertEquals(36, calculateAge(LocalDate(1990, 5, 6), today))+        // Birthday later this year: not yet.+        assertEquals(35, calculateAge(LocalDate(1990, 11, 7), today))+        assertEquals(36, calculateAge(LocalDate(1990, 11, 6), today))+    }++    @Test+    fun `age is null without a year or in the future`() {+        val today = LocalDate(2026, 11, 6)+        assertNull(calculateAge(LocalDate(1899, 1, 1), today))+        assertNull(calculateAge(LocalDate(2030, 1, 1), today))+    }++    // -- ContactPrefill ----------------------------------------------------------++    @Test+    fun `prefill defaults to empty and exposes primaries`() {+        val empty = ContactPrefill()+        assertNull(empty.name)+        assertNull(empty.primaryPhone)+        assertNull(empty.primaryEmail)+        val full = ContactPrefill(+            name = "Ada",+            company = "Acme",+            notes = "n",+            nickname = "Al",+            phones = listOf(PrefillValue("+1555", 2, "Work"), PrefillValue("+1666")),+            emails = listOf(PrefillValue("[email protected]")),+            postals = listOf(PrefillValue("123 Main")),+        )+        assertEquals("+1555", full.primaryPhone)+        assertEquals("[email protected]", full.primaryEmail)+        assertEquals(full, full.copy())+    }++    // -- PackageUtils -------------------------------------------------------------++    @Test+    fun `unknown packages are not installed`() {+        val context: android.content.Context = ApplicationProvider.getApplicationContext()+        assertFalse(PackageUtils.isAppInstalled(context, "com.example.nope"))+        assertFalse(PackageUtils.isSignalInstalled(context))+        assertFalse(PackageUtils.isWhatsAppInstalled(context))+        assertFalse(PackageUtils.isTelegramInstalled(context))+        assertFalse(PackageUtils.isGoogleMeetInstalled(context))+    }++    @Test+    fun `installed package reports true`() {+        val context: android.content.Context = ApplicationProvider.getApplicationContext()+        // The test package itself is always installed.+        assertTrue(PackageUtils.isAppInstalled(context, context.packageName))+    }+}
contacts/src/test/java/com/vayunmathur/contacts/pure/IntentHelpersTest.kt
diff --git a/contacts/src/test/java/com/vayunmathur/contacts/pure/IntentHelpersTest.kt b/contacts/src/test/java/com/vayunmathur/contacts/pure/IntentHelpersTest.ktnew file mode 100644index 0000000000..cd6980365c--- /dev/null+++ b/contacts/src/test/java/com/vayunmathur/contacts/pure/IntentHelpersTest.kt@@ -0,0 +1,89 @@+package com.vayunmathur.contacts.pure++import android.content.Intent+import android.net.Uri+import android.provider.ContactsContract+import androidx.test.core.app.ApplicationProvider+import com.vayunmathur.contacts.isVcfIntent+import com.vayunmathur.contacts.readType+import com.vayunmathur.contacts.uriPhoneNumber+import kotlin.test.Test+import kotlin.test.assertEquals+import kotlin.test.assertNull+import org.junit.runner.RunWith+import org.robolectric.RobolectricTestRunner++/**+ * Robolectric coverage for main-activity intent helpers ([isVcfIntent],+ * [readType], [uriPhoneNumber]): the VIEW/pick/edit entry points route on+ * MIME type, typed extras, and the data URI โ€” all plain Intent reads.+ */+@RunWith(RobolectricTestRunner::class)+class IntentHelpersTest {++    private val context: android.content.Context+        get() = ApplicationProvider.getApplicationContext()++    private fun intent(action: String = Intent.ACTION_VIEW) = Intent(action)++    // -- isVcfIntent ---------------------------------------------------------------++    @Test+    fun `vcf detected by mime or path`() {+        assertTrue2(isVcfIntent(intent().setType("text/x-vcard")))+        assertTrue2(isVcfIntent(intent().setType("text/vcard")))+        assertTrue2(+            isVcfIntent(+                intent().setDataAndType(Uri.parse("content://x/y.vcf"), "application/octet-stream"),+            ),+        )+        assertTrue2(!isVcfIntent(intent().setType("text/plain")))+        assertTrue2(!isVcfIntent(intent()))+    }++    // -- readType -------------------------------------------------------------------++    @Test+    fun `readType accepts int numeric string and custom label`() {+        assertEquals(+            2 to null,+            readType(intent().putExtra("t", 2), "t"),+        )+        assertEquals(+            2 to null,+            readType(intent().putExtra("t", "2"), "t"),+        )+        assertEquals(+            ContactsContract.CommonDataKinds.BaseTypes.TYPE_CUSTOM to "Work",+            readType(intent().putExtra("t", "Work"), "t"),+        )+    }++    @Test+    fun `readType misses on absent or wrong-typed extras`() {+        assertEquals(null to null, readType(intent(), "t"))+        assertEquals(null to null, readType(intent().putExtra("t", 1.5), "t"))+        assertEquals(null to null, readType(Intent(), "t"))+    }++    // -- uriPhoneNumber ---------------------------------------------------------------++    @Test+    fun `tel uri yields the number`() {+        assertEquals("+1555", uriPhoneNumber(intent().setData(Uri.parse("tel:+1555"))))+    }++    @Test+    fun `phone lookup uri yields the last segment`() {+        val uri = Uri.parse("content://com.android.contacts/phone_lookup/+1555")+        assertEquals("+1555", uriPhoneNumber(intent().setData(uri)))+    }++    @Test+    fun `other uris yield null`() {+        assertNull(uriPhoneNumber(intent()))+        assertNull(uriPhoneNumber(intent().setData(Uri.parse("content://com.android.contacts/contacts/7"))))+    }++    private fun assertTrue2(v: Boolean) = kotlin.test.assertTrue(v)+}
contacts/src/test/java/com/vayunmathur/contacts/pure/SimAccountTypesTest.kt
diff --git a/contacts/src/test/java/com/vayunmathur/contacts/pure/SimAccountTypesTest.kt b/contacts/src/test/java/com/vayunmathur/contacts/pure/SimAccountTypesTest.ktnew file mode 100644index 0000000000..0ac5789338--- /dev/null+++ b/contacts/src/test/java/com/vayunmathur/contacts/pure/SimAccountTypesTest.kt@@ -0,0 +1,87 @@+package com.vayunmathur.contacts.pure++import android.Manifest+import android.app.Application+import android.telephony.SubscriptionManager+import androidx.test.core.app.ApplicationProvider+import com.vayunmathur.contacts.data.LOCAL_ACCOUNT_TYPE+import com.vayunmathur.contacts.data.SIM_ACCOUNT_TYPE+import com.vayunmathur.contacts.data.SimContactsDataSource+import com.vayunmathur.contacts.data.isDefaultLocalAccount+import com.vayunmathur.contacts.data.isLocalAccountType+import com.vayunmathur.contacts.data.isSimAccountType+import kotlin.test.Test+import kotlin.test.assertEquals+import kotlin.test.assertFalse+import kotlin.test.assertTrue+import org.junit.runner.RunWith+import org.robolectric.RobolectricTestRunner+import org.robolectric.Shadows.shadowOf++/**+ * Slice coverage for SIM account plumbing ([isSimAccountType],+ * [isLocalAccountType], [isDefaultLocalAccount],+ * [SimContactsDataSource.getActiveSubscriptionIds]).+ *+ * Robolectric (permission grant/deny + subscription shadow): pins which+ * account types the app treats as SIM/local/device-local, and that an empty+ * subscription list yields no ids โ€” the routing the move/copy dialogs and+ * SIM import rely on.+ */+@RunWith(RobolectricTestRunner::class)+class SimAccountTypesTest {++    private val context: Application+        get() = ApplicationProvider.getApplicationContext()++    @Test+    fun `sim account type matches only the sim type`() {+        assertTrue(isSimAccountType(SIM_ACCOUNT_TYPE))+        assertFalse(isSimAccountType(LOCAL_ACCOUNT_TYPE))+        assertFalse(isSimAccountType("com.google"))+        assertFalse(isSimAccountType(null))+        assertFalse(isSimAccountType(""))+    }++    @Test+    fun `local account type covers blank and the local type`() {+        assertTrue(isLocalAccountType(null))+        assertTrue(isLocalAccountType(""))+        assertTrue(isLocalAccountType(LOCAL_ACCOUNT_TYPE))+        assertFalse(isLocalAccountType(SIM_ACCOUNT_TYPE))+        assertFalse(isLocalAccountType("com.google"))+    }++    @Test+    fun `default local account needs both fields blank`() {+        assertTrue(isDefaultLocalAccount(null, null))+        assertTrue(isDefaultLocalAccount("", ""))+        assertFalse(isDefaultLocalAccount("a", null))+        assertFalse(isDefaultLocalAccount(null, "t"))+        assertFalse(isDefaultLocalAccount("a", "t"))+    }++    @Test+    fun `no subscriptions yields no ids with or without permission`() {+        shadowOf(context).denyPermissions(Manifest.permission.READ_PHONE_STATE)+        assertTrue(SimContactsDataSource.getActiveSubscriptionIds(context).isEmpty())+        shadowOf(context).grantPermissions(Manifest.permission.READ_PHONE_STATE)+        shadowOf(context.getSystemService(SubscriptionManager::class.java))+            .setActiveSubscriptionInfoList(emptyList())+        assertTrue(SimContactsDataSource.getActiveSubscriptionIds(context).isEmpty())+    }++    @Test+    fun `subscription ids mirror the shadow list`() {+        shadowOf(context).grantPermissions(Manifest.permission.READ_PHONE_STATE)+        val infos = listOf(+            org.robolectric.shadows.ShadowSubscriptionManager.SubscriptionInfoBuilder.newBuilder()+                .setId(11).setSimSlotIndex(0).buildSubscriptionInfo(),+            org.robolectric.shadows.ShadowSubscriptionManager.SubscriptionInfoBuilder.newBuilder()+                .setId(22).setSimSlotIndex(1).buildSubscriptionInfo(),+        )+        shadowOf(context.getSystemService(SubscriptionManager::class.java))+            .setActiveSubscriptionInfoList(infos)+        assertEquals(listOf(11, 22), SimContactsDataSource.getActiveSubscriptionIds(context))+    }+}
contacts/src/test/java/com/vayunmathur/contacts/pure/VCardTypeMappingTest.kt
diff --git a/contacts/src/test/java/com/vayunmathur/contacts/pure/VCardTypeMappingTest.kt b/contacts/src/test/java/com/vayunmathur/contacts/pure/VCardTypeMappingTest.ktnew file mode 100644index 0000000000..a427d2661f--- /dev/null+++ b/contacts/src/test/java/com/vayunmathur/contacts/pure/VCardTypeMappingTest.kt@@ -0,0 +1,97 @@+package com.vayunmathur.contacts.pure++import com.vayunmathur.contacts.data.Contact+import com.vayunmathur.contacts.util.VcfUtils+import java.io.ByteArrayInputStream+import kotlin.test.Test+import kotlin.test.assertEquals+import kotlin.test.assertTrue++/**+ * Pure-JVM coverage for vCard TYPE mapping on import+ * ([VcfUtils.parseContacts] type branches): every phone/email/address/event+ * TYPE token maps to the matching provider type, and custom labels survive+ * as TYPE_CUSTOM โ€” so imports from other address books keep their labels.+ */+class VCardTypeMappingTest {++    private fun import(blob: String): Contact =+        VcfUtils.parseContacts(ByteArrayInputStream(blob.toByteArray(Charsets.UTF_8))).single()++    private fun card(vararg lines: String): String =+        (listOf("BEGIN:VCARD", "VERSION:3.0", "FN:Alice") + lines + listOf("END:VCARD"))+            .joinToString("\r\n")++    @Test+    fun `phone types map cell home work fax`() {+        val c = import(+            card(+                "TEL;TYPE=CELL:+111",+                "TEL;TYPE=HOME:+122",+                "TEL;TYPE=WORK:+133",+                "TEL;TYPE=FAX:+144",+                "TEL:+155",+            ),+        )+        val byNumber = c.details.phoneNumbers.associateBy { it.number }+        assertEquals(2, byNumber["+111"]!!.type) // TYPE_MOBILE+        assertEquals(1, byNumber["+122"]!!.type) // TYPE_HOME+        assertEquals(3, byNumber["+133"]!!.type) // TYPE_WORK+        assertEquals(4, byNumber["+144"]!!.type) // TYPE_FAX_WORK+        assertEquals(2, byNumber["+155"]!!.type) // default mobile+    }++    @Test+    fun `custom phone label becomes custom type`() {+        val c = import(card("TEL;TYPE=X-MOM:+166"))+        assertEquals(0, c.details.phoneNumbers.single().type) // TYPE_CUSTOM+        // The X- prefix is stripped; the human label survives.+        assertEquals("MOM", c.details.phoneNumbers.single().label)+    }++    @Test+    fun `email types map work home other`() {+        val c = import(+            card(+                "EMAIL;TYPE=WORK:[email protected]",+                "EMAIL;TYPE=HOME:[email protected]",+                "EMAIL:[email protected]",+            ),+        )+        val byAddress = c.details.emails.associateBy { it.address }+        assertEquals(2, byAddress["[email protected]"]!!.type) // TYPE_WORK+        assertEquals(1, byAddress["[email protected]"]!!.type) // TYPE_HOME+        assertEquals(3, byAddress["[email protected]"]!!.type) // TYPE_OTHER+    }++    @Test+    fun `address types map home work default`() {+        val c = import(+            card(+                "ADR;TYPE=WORK:;;1 Work Way;X;Y;1;US",+                "ADR;TYPE=HOME:;;2 Home Way;X;Y;2;US",+                "ADR:;;3 Other Way;X;Y;3;US",+            ),+        )+        val byStreet = c.details.addresses.associateBy { it.formattedAddress }+        assertTrue(byStreet.keys.any { it.contains("1 Work Way") })+        val work = c.details.addresses.first { it.formattedAddress.contains("1 Work Way") }+        val home = c.details.addresses.first { it.formattedAddress.contains("2 Home Way") }+        val other = c.details.addresses.first { it.formattedAddress.contains("3 Other Way") }+        assertEquals(2, work.type) // TYPE_WORK+        assertEquals(1, home.type) // TYPE_HOME+        assertEquals(1, other.type) // default home+        assertEquals(byStreet, byStreet)+    }++    @Test+    fun `event and custom types map`() {+        val c = import(+            card(+                "BDAY:19900506",+                "X-EVENT;TYPE=Party:20200101",+            ),+        )+        assertTrue(c.details.dates.any { it.startDate.toString() == "1990-05-06" })+    }+}