mirror of
https://github.com/signalapp/Signal-Android.git
synced 2026-09-28 07:51:42 +01:00
Fix default country code for numberless accounts.
This commit is contained in:
committed by
Michelle Tang
parent
c8daf21fb4
commit
fee574daab
@@ -11,6 +11,8 @@ import org.signal.core.util.orNull
|
||||
import org.thoughtcrime.securesms.recipients.Recipient
|
||||
import org.thoughtcrime.securesms.registration.ui.countrycode.Country
|
||||
import org.thoughtcrime.securesms.registration.ui.countrycode.CountryUtils
|
||||
import java.util.Locale
|
||||
import org.signal.registration.screens.countrycode.CountryUtils as RegistrationCountryUtils
|
||||
|
||||
/**
|
||||
* State for driving find by number/username screen.
|
||||
@@ -25,19 +27,26 @@ data class FindByState(
|
||||
val query: String = ""
|
||||
) {
|
||||
companion object {
|
||||
private const val DEFAULT_REGION_CODE = "US"
|
||||
|
||||
fun startingState(self: Recipient, mode: FindByMode): FindByState {
|
||||
val countryCode: Int = try {
|
||||
PhoneNumberUtil.getInstance()
|
||||
.parse(self.e164.orNull(), null)
|
||||
.countryCode
|
||||
} catch (e: NumberParseException) {
|
||||
-1
|
||||
}
|
||||
val regionCode: String = selfRegionCode(self)
|
||||
?: RegistrationCountryUtils.localeToRegionCode(Locale.getDefault())
|
||||
?: DEFAULT_REGION_CODE
|
||||
|
||||
val state = FindByState(mode = mode)
|
||||
return state.copy(
|
||||
selectedCountry = state.supportedCountries.firstOrNull { it.countryCode == countryCode } ?: state.supportedCountries.first()
|
||||
selectedCountry = state.supportedCountries.firstOrNull { it.regionCode == regionCode } ?: state.supportedCountries.first()
|
||||
)
|
||||
}
|
||||
|
||||
private fun selfRegionCode(self: Recipient): String? {
|
||||
return try {
|
||||
PhoneNumberUtil.getInstance()
|
||||
.getRegionCodeForNumber(PhoneNumberUtil.getInstance().parse(self.e164.orNull(), null))
|
||||
} catch (e: NumberParseException) {
|
||||
null
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+55
@@ -28,11 +28,13 @@ import org.thoughtcrime.securesms.recipients.RecipientId
|
||||
import org.thoughtcrime.securesms.recipients.RecipientRepository
|
||||
import org.thoughtcrime.securesms.registration.ui.countrycode.Country
|
||||
import org.thoughtcrime.securesms.registration.ui.countrycode.CountryUtils
|
||||
import java.util.Locale
|
||||
import java.util.Optional
|
||||
|
||||
class FindByViewModelTest {
|
||||
|
||||
private val liveRecipientCache = mockk<LiveRecipientCache>(relaxed = true)
|
||||
private val defaultLocale = Locale.getDefault()
|
||||
private lateinit var viewModel: FindByViewModel
|
||||
|
||||
@Before
|
||||
@@ -46,9 +48,62 @@ class FindByViewModelTest {
|
||||
|
||||
@After
|
||||
fun tearDown() {
|
||||
Locale.setDefault(defaultLocale)
|
||||
unmockkAll()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `Given a self e164, when I start the screen, then I expect the country of that number`() {
|
||||
every { Recipient.self() } returns mockk {
|
||||
every { e164 } returns Optional.of("+31612345678")
|
||||
}
|
||||
|
||||
viewModel = FindByViewModel(FindByMode.PHONE_NUMBER)
|
||||
val result = viewModel.state.value.selectedCountry
|
||||
|
||||
assertEquals("NL", result.regionCode)
|
||||
assertEquals(31, result.countryCode)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `Given a shared calling code, when I start the screen, then I expect the primary region`() {
|
||||
every { Recipient.self() } returns mockk {
|
||||
every { e164 } returns Optional.of("+442071838750")
|
||||
}
|
||||
|
||||
viewModel = FindByViewModel(FindByMode.PHONE_NUMBER)
|
||||
val result = viewModel.state.value.selectedCountry
|
||||
|
||||
assertEquals("GB", result.regionCode)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `Given no self e164, when I start the screen, then I expect the country of the default locale`() {
|
||||
Locale.setDefault(Locale("nl", "NL"))
|
||||
every { Recipient.self() } returns mockk {
|
||||
every { e164 } returns Optional.empty()
|
||||
}
|
||||
|
||||
viewModel = FindByViewModel(FindByMode.PHONE_NUMBER)
|
||||
val result = viewModel.state.value.selectedCountry
|
||||
|
||||
assertEquals("NL", result.regionCode)
|
||||
assertEquals(31, result.countryCode)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `Given no self e164 and an unknown locale, when I start the screen, then I expect US`() {
|
||||
Locale.setDefault(Locale("xx", "ZZ"))
|
||||
every { Recipient.self() } returns mockk {
|
||||
every { e164 } returns Optional.empty()
|
||||
}
|
||||
|
||||
viewModel = FindByViewModel(FindByMode.PHONE_NUMBER)
|
||||
val result = viewModel.state.value.selectedCountry
|
||||
|
||||
assertEquals("US", result.regionCode)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `Given phone number mode, when I change user entry, then I expect digits only`() {
|
||||
viewModel = FindByViewModel(FindByMode.PHONE_NUMBER)
|
||||
|
||||
Reference in New Issue
Block a user