Fix pretty print phone numbers.

Fixes #9047
This commit is contained in:
Alan Evans 2019-09-25 09:49:26 -04:00 committed by GitHub
parent 14999800e2
commit 840c493265
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 56 additions and 1 deletions

View file

@ -106,7 +106,7 @@ public final class ContactUtil {
private static @NonNull String getPrettyPhoneNumber(@NonNull String phoneNumber, @NonNull Locale fallbackLocale) {
PhoneNumberUtil util = PhoneNumberUtil.getInstance();
try {
PhoneNumber parsed = util.parse(phoneNumber, fallbackLocale.getISO3Country());
PhoneNumber parsed = util.parse(phoneNumber, fallbackLocale.getCountry());
return util.format(parsed, PhoneNumberUtil.PhoneNumberFormat.INTERNATIONAL);
} catch (NumberParseException e) {
return phoneNumber;

View file

@ -0,0 +1,55 @@
package org.thoughtcrime.securesms.contactshare;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import java.util.Arrays;
import java.util.Collection;
import java.util.Locale;
import static org.junit.Assert.assertEquals;
@RunWith(Parameterized.class)
public final class ContactUtilTest_getPrettyPhoneNumber {
private final Locale locale;
private final String input;
private final String expected;
@Parameterized.Parameters
public static Collection<Object[]> data() {
return Arrays.asList(new Object[][]{
/* Already international */
{ Locale.US, "+15551234567", "+1 555-123-4567" },
{ Locale.US, "+44 7700900000", "+44 7700 900000" },
/* US */
{ Locale.US, "555-123-4567", "+1 555-123-4567" },
/* GB */
{ new Locale("en" ,"GB"), "07700900000", "+44 7700 900000" },
/* Hungary */
{ new Locale("hu" ,"HU"), "0655153211", "+36 55 153 211" },
/* Canaries is a region that does not have an ISO3 country code */
{ new Locale("es", "IC"), "+345551224116", "+34 5551224116" },
});
}
public ContactUtilTest_getPrettyPhoneNumber(Locale locale, String input, String expected) {
this.locale = locale;
this.input = input;
this.expected = expected;
}
@Test
public void prettyPhoneNumber() {
String phoneNumber = ContactUtil.getPrettyPhoneNumber(new Contact.Phone(input, Contact.Phone.Type.MOBILE, null), locale);
assertEquals(expected, phoneNumber);
}
}