Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -341,9 +341,6 @@
<intent-filter android:priority="2147483647">
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
<intent-filter android:priority="2147483647">
<action android:name="android.provider.Telephony.MMS_DOWNLOADED" />
</intent-filter>
</receiver>
<receiver android:name=".receiver.MmsWapPushDeliverReceiver"
android:exported="true"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package com.android.messaging.data.contact.formatter

import com.android.messaging.data.phone.formatter.PhoneNumberFormatter
import io.mockk.every
import io.mockk.mockk
import io.mockk.verify
import org.junit.Assert.assertEquals
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner

@RunWith(RobolectricTestRunner::class)
internal class ContactDestinationFormatterImplTest {

private val phoneNumberFormatter = mockk<PhoneNumberFormatter>()
private val formatter = ContactDestinationFormatterImpl(
phoneNumberFormatter = phoneNumberFormatter,
)

@Test
fun canonicalize_whenValueIsBlank_returnsTrimmedBlank() {
assertEquals("", formatter.canonicalize(" "))
verify(exactly = 0) {
phoneNumberFormatter.getCanonicalForEnteredNumber(any())
}
}

@Test
fun canonicalize_whenValueIsEmail_trimsAndLowercasesEmail() {
assertEquals("alice@example.com", formatter.canonicalize(" Alice@Example.COM "))
verify(exactly = 0) {
phoneNumberFormatter.getCanonicalForEnteredNumber(any())
}
}

@Test
fun canonicalize_whenValueIsPhone_trimsCanonicalizesAndStripsSeparators() {
every {
phoneNumberFormatter.getCanonicalForEnteredNumber("+1 (555) 123-4567")
} returns "+1 (555) 123-4567"

assertEquals("+15551234567", formatter.canonicalize(" +1 (555) 123-4567 "))
}

@Test
fun canonicalize_withCountryCandidates_passesCandidatesToPhoneNumberFormatter() {
val countryCandidates = listOf("US", "CA")
every {
phoneNumberFormatter.getCanonicalForEnteredNumber("555.123/4567", countryCandidates)
} returns "555.123/4567"

assertEquals(
"5551234567",
formatter.canonicalize("555.123/4567", countryCandidates),
)
}

@Test
fun formatPhoneForDisplay_delegatesToPhoneNumberFormatter() {
every { phoneNumberFormatter.formatForDisplay("+15551234") } returns "+1 555-1234"

assertEquals("+1 555-1234", formatter.formatPhoneForDisplay("+15551234"))
}

@Test
fun countryCandidates_delegatesToPhoneNumberFormatter() {
val countryCandidates = listOf("US", "CA")
every { phoneNumberFormatter.countryCandidates() } returns countryCandidates

assertEquals(countryCandidates, formatter.countryCandidates())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import android.provider.ContactsContract
import com.android.messaging.data.contact.formatter.ContactDestinationFormatterImpl
import com.android.messaging.data.contact.model.Contact
import com.android.messaging.data.contact.model.ContactDestination
import com.android.messaging.data.phone.formatter.PhoneNumberFormatterImpl
import com.android.messaging.sms.MmsSmsUtils
import com.android.messaging.util.PhoneUtils
import io.mockk.every
Expand Down Expand Up @@ -496,7 +497,9 @@ internal class ContactsRepositoryImplTest {

private fun createRepository(): ContactsRepositoryImpl {
return ContactsRepositoryImpl(
formatter = ContactDestinationFormatterImpl(),
formatter = ContactDestinationFormatterImpl(
PhoneNumberFormatterImpl(phoneUtilsInstance),
),
contentResolver = contentResolver,
ioDispatcher = UnconfinedTestDispatcher(),
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package com.android.messaging.data.conversationlist.store

import com.android.messaging.data.secondaryuser.SecondaryUserNotifier
import com.android.messaging.datamodel.DataModel
import com.android.messaging.datamodel.SyncManager
import com.android.messaging.receiver.SmsReceiver
import io.mockk.every
import io.mockk.just
import io.mockk.mockk
import io.mockk.mockkStatic
import io.mockk.runs
import io.mockk.unmockkAll
import io.mockk.verify
import org.junit.After
Expand All @@ -19,17 +17,16 @@ internal class ConversationListStatusStoreTest {

private val dataModel = mockk<DataModel>(relaxed = true)
private val syncManager = mockk<SyncManager>()
private val secondaryUserNotifier = mockk<SecondaryUserNotifier>(relaxed = true)

private val store = ConversationListStatusStoreImpl()
private val store = ConversationListStatusStoreImpl(secondaryUserNotifier)

@Before
fun setUp() {
mockkStatic(DataModel::class)
mockkStatic(SmsReceiver::class)

every { DataModel.get() } returns dataModel
every { dataModel.syncManager } returns syncManager
every { SmsReceiver.cancelSecondaryUserNotification() } just runs
}

@After
Expand All @@ -49,7 +46,7 @@ internal class ConversationListStatusStoreTest {
store.setNewestConversationVisible(isVisible = true)

verify { dataModel.isConversationListScrolledToNewestConversation = true }
verify { SmsReceiver.cancelSecondaryUserNotification() }
verify { secondaryUserNotifier.cancel() }
}

@Test
Expand All @@ -60,7 +57,7 @@ internal class ConversationListStatusStoreTest {
dataModel.isConversationListScrolledToNewestConversation = false
}
verify(exactly = 0) {
SmsReceiver.cancelSecondaryUserNotification()
secondaryUserNotifier.cancel()
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package com.android.messaging.data.phone.formatter

import com.android.messaging.util.PhoneUtils
import io.mockk.every
import io.mockk.mockk
import io.mockk.verify
import org.junit.Assert.assertEquals
import org.junit.Test

internal class PhoneNumberFormatterImplTest {

private val phoneUtils = mockk<PhoneUtils>()
private val formatter = PhoneNumberFormatterImpl(phoneUtils = phoneUtils)

@Test
fun formatForDisplay_delegatesToPhoneUtils() {
every { phoneUtils.formatForDisplay(PHONE_NUMBER) } returns FORMATTED_PHONE_NUMBER

assertEquals(FORMATTED_PHONE_NUMBER, formatter.formatForDisplay(PHONE_NUMBER))
}

@Test
fun formatForDisplayUsingSimCountry_whenPhoneUtilsReturnsNull_returnsEmptyString() {
every { phoneUtils.formatForDisplayUsingSimCountry(PHONE_NUMBER) } returns null

assertEquals("", formatter.formatForDisplayUsingSimCountry(PHONE_NUMBER))
}

@Test
fun formatNormalizedUsingSimCountry_whenPhoneUtilsReturnsNull_returnsEmptyString() {
every { phoneUtils.formatNormalizedDestinationUsingSimCountry(PHONE_NUMBER) } returns null

assertEquals("", formatter.formatNormalizedUsingSimCountry(PHONE_NUMBER))
}

@Test
fun getCanonicalForEnteredNumber_delegatesToPhoneUtils() {
every { phoneUtils.getCanonicalForEnteredPhoneNumber(PHONE_NUMBER) } returns
CANONICAL_NUMBER

assertEquals(CANONICAL_NUMBER, formatter.getCanonicalForEnteredNumber(PHONE_NUMBER))
}

@Test
fun getCanonicalForEnteredNumber_withCountryCandidates_delegatesToPhoneUtils() {
val countryCandidates = listOf("US", "CA")
every {
phoneUtils.getCanonicalForEnteredPhoneNumber(PHONE_NUMBER, countryCandidates)
} returns CANONICAL_NUMBER

assertEquals(
CANONICAL_NUMBER,
formatter.getCanonicalForEnteredNumber(PHONE_NUMBER, countryCandidates),
)
}

@Test
fun countryCandidates_warmsUpPhoneUtilsBeforeReadingCandidates() {
val countryCandidates = listOf("US", "CA")
every { phoneUtils.warmUp() } returns Unit
every { phoneUtils.countryCandidatesForEnteredPhoneNumber } returns countryCandidates

assertEquals(countryCandidates, formatter.countryCandidates())
verify {
phoneUtils.warmUp()
phoneUtils.countryCandidatesForEnteredPhoneNumber
}
}

private companion object {
private const val PHONE_NUMBER = "+15551234"
private const val FORMATTED_PHONE_NUMBER = "+1 555-1234"
private const val CANONICAL_NUMBER = "+15551234"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package com.android.messaging.data.secondaryuser

import com.android.messaging.data.phone.formatter.PhoneNumberFormatter
import com.android.messaging.data.secondaryuser.model.SecondaryUserMessageInfo
import io.mockk.every
import io.mockk.mockk
import io.mockk.verify
import org.junit.Assert.assertEquals
import org.junit.Assert.assertNull
import org.junit.Test

internal class SecondaryUserMessageResolverTest {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test coverage should be improved, currently tests are skipping lots of cases


private val contactNameLookup = mockk<SmsContactNameLookup>()
private val phoneNumberFormatter = mockk<PhoneNumberFormatter>()

private val resolver = SecondaryUserMessageResolverImpl(
contactNameLookup = contactNameLookup,
phoneNumberFormatter = phoneNumberFormatter,
)

@Test
fun resolve_contactFound_usesContactName() {
every { contactNameLookup.lookup("+15551234") } returns "Alice"

val info = resolver.resolve(address = "+15551234", body = "Hello there")
assertEquals(
SecondaryUserMessageInfo(sender = "Alice", body = "Hello there"),
info,
)
verify(exactly = 0) {
phoneNumberFormatter.formatForDisplay(any())
}
}

@Test
fun resolve_noMatchingContact_fallsBackToFormattedNumber() {
every { contactNameLookup.lookup("+15551234") } returns null
every { phoneNumberFormatter.formatForDisplay("+15551234") } returns "+1 555-1234"

val info = resolver.resolve(address = "+15551234", body = "Hello there")
assertEquals(
SecondaryUserMessageInfo(sender = "+1 555-1234", body = "Hello there"),
info,
)
}

@Test
fun resolve_blankContactName_fallsBackToFormattedNumber() {
every { contactNameLookup.lookup("+15551234") } returns ""
every { phoneNumberFormatter.formatForDisplay("+15551234") } returns "+1 555-1234"

val info = resolver.resolve(address = "+15551234", body = "Hello there")
assertEquals(
SecondaryUserMessageInfo(sender = "+1 555-1234", body = "Hello there"),
info,
)
}

@Test
fun resolve_shortAddress_usesFormattedAddress() {
every { contactNameLookup.lookup("123") } returns null
every { phoneNumberFormatter.formatForDisplay("123") } returns "123"

val info = resolver.resolve(address = "123", body = "Hello there")
assertEquals(
SecondaryUserMessageInfo(sender = "123", body = "Hello there"),
info,
)
}

@Test
fun resolve_nullBody_returnsNull() {
assertNull(resolver.resolve(address = "+15551234", body = null))
verifyNoSenderResolution()
}

@Test
fun resolve_emptyBody_returnsNull() {
assertNull(resolver.resolve(address = "+15551234", body = ""))
verifyNoSenderResolution()
}

@Test
fun resolve_nullAddress_returnsNull() {
assertNull(resolver.resolve(address = null, body = "Hello there"))
verifyNoSenderResolution()
}

@Test
fun resolve_emptyAddress_returnsNull() {
assertNull(resolver.resolve(address = "", body = "Hello there"))
verifyNoSenderResolution()
}

private fun verifyNoSenderResolution() {
verify(exactly = 0) {
contactNameLookup.lookup(any())
phoneNumberFormatter.formatForDisplay(any())
}
}
}
Loading
Loading