diff --git a/app/src/main/java/org/thoughtcrime/securesms/recipients/ui/findby/FindByState.kt b/app/src/main/java/org/thoughtcrime/securesms/recipients/ui/findby/FindByState.kt index f39e2ab3a7..115c310c5a 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/recipients/ui/findby/FindByState.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/recipients/ui/findby/FindByState.kt @@ -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 + } + } } } diff --git a/app/src/test/java/org/thoughtcrime/securesms/recipients/ui/findby/FindByViewModelTest.kt b/app/src/test/java/org/thoughtcrime/securesms/recipients/ui/findby/FindByViewModelTest.kt index 13bbabc4cd..a373045b28 100644 --- a/app/src/test/java/org/thoughtcrime/securesms/recipients/ui/findby/FindByViewModelTest.kt +++ b/app/src/test/java/org/thoughtcrime/securesms/recipients/ui/findby/FindByViewModelTest.kt @@ -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(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)