Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / contacts / src / test / java / com / vayunmathur / contacts / IntentHelpersPass2Test.kt
IntentHelpersPass2Test.kt69 lines · 2.6 KiBRaw
| 1 | package com.vayunmathur.contacts |
| 2 | |
| 3 | import android.content.Intent |
| 4 | import android.net.Uri |
| 5 | import com.vayunmathur.contacts.intents.isVcfIntent |
| 6 | import com.vayunmathur.contacts.intents.readType |
| 7 | import com.vayunmathur.contacts.intents.uriPhoneNumber |
| 8 | import android.provider.ContactsContract |
| 9 | import android.provider.ContactsContract.CommonDataKinds.Phone as PhoneKind |
| 10 | import kotlin.test.Test |
| 11 | import kotlin.test.assertEquals |
| 12 | import kotlin.test.assertFalse |
| 13 | import kotlin.test.assertNull |
| 14 | import kotlin.test.assertTrue |
| 15 | import org.junit.runner.RunWith |
| 16 | import org.robolectric.RobolectricTestRunner |
| 17 | |
| 18 | /** Second-pass coverage for MainActivity's intent helpers. */ |
| 19 | @RunWith(RobolectricTestRunner::class) |
| 20 | class IntentHelpersPass2Test { |
| 21 | |
| 22 | @Test |
| 23 | fun readTypeHandlesIntNumericStringCustomAndMissing() { |
| 24 | val intIntent = Intent().putExtra("k", 3) |
| 25 | assertEquals(3 to null, readType(intIntent, "k")) |
| 26 | |
| 27 | val numericString = Intent().putExtra("k", "2") |
| 28 | assertEquals(2 to null, readType(numericString, "k")) |
| 29 | |
| 30 | val customLabel = Intent().putExtra("k", "Cabin") |
| 31 | assertEquals(PhoneKind.TYPE_CUSTOM to "Cabin", readType(customLabel, "k")) |
| 32 | |
| 33 | val styled = Intent().putExtra("k", android.text.SpannableString("4")) |
| 34 | assertEquals(4 to null, readType(styled, "k")) |
| 35 | |
| 36 | assertEquals(null to null, readType(Intent(), "k")) |
| 37 | assertEquals(null to null, readType(Intent().putExtra("other", 1), "k")) |
| 38 | } |
| 39 | |
| 40 | @Test |
| 41 | fun uriPhoneNumberExtractsTelAndLookupNumbers() { |
| 42 | val tel = Intent().apply { data = Uri.parse("tel:+14155550102") } |
| 43 | assertEquals("+14155550102", uriPhoneNumber(tel)) |
| 44 | |
| 45 | val lookup = Intent().apply { |
| 46 | data = Uri.parse("content://com.android.contacts/phone_lookup/+442071234567") |
| 47 | } |
| 48 | assertEquals("+442071234567", uriPhoneNumber(lookup)) |
| 49 | |
| 50 | val other = Intent().apply { data = Uri.parse("content://com.android.contacts/contacts/7") } |
| 51 | assertNull(uriPhoneNumber(other)) |
| 52 | assertNull(uriPhoneNumber(Intent())) |
| 53 | } |
| 54 | |
| 55 | @Test |
| 56 | fun isVcfIntentMatchesTypeAndExtension() { |
| 57 | assertTrue(isVcfIntent(Intent().setType("text/x-vcard"))) |
| 58 | assertTrue(isVcfIntent(Intent().setType("text/vcf"))) |
| 59 | assertTrue( |
| 60 | isVcfIntent(Intent(Intent.ACTION_VIEW, Uri.parse("content://downloads/CARD.VCF"))) |
| 61 | ) |
| 62 | assertFalse(isVcfIntent(Intent().setType("text/plain"))) |
| 63 | assertFalse(isVcfIntent(Intent(Intent.ACTION_VIEW, Uri.parse("https://wa.me/123")))) |
| 64 | assertFalse(isVcfIntent(Intent())) |
| 65 | assertTrue( |
| 66 | ContactsContract.AUTHORITY == "com.android.contacts" |
| 67 | ) |
| 68 | } |
| 69 | } |