// outside the country.
assertEquals("030123456",
phoneUtil.formatNumberForMobileDialing(DE_NUMBER, RegionCode.DE, false));
assertEquals("+4930123456",
phoneUtil.formatNumberForMobileDialing(DE_NUMBER, RegionCode.CH, false));
PhoneNumber deNumberWithExtn = new PhoneNumber().mergeFrom(DE_NUMBER).setExtension("1234");
assertEquals("030123456",
phoneUtil.formatNumberForMobileDialing(deNumberWithExtn, RegionCode.DE, false));
assertEquals("+4930123456",
phoneUtil.formatNumberForMobileDialing(deNumberWithExtn, RegionCode.CH, false));
// US toll free numbers are marked as noInternationalDialling in the test metadata for testing
// purposes. For such numbers, we expect nothing to be returned when the region code is not the
// same one.
assertEquals("800 253 0000",
phoneUtil.formatNumberForMobileDialing(US_TOLLFREE, RegionCode.US,
true /* keep formatting */));
assertEquals("", phoneUtil.formatNumberForMobileDialing(US_TOLLFREE, RegionCode.CN, true));
assertEquals("+1 650 253 0000",
phoneUtil.formatNumberForMobileDialing(US_NUMBER, RegionCode.US, true));
PhoneNumber usNumberWithExtn = new PhoneNumber().mergeFrom(US_NUMBER).setExtension("1234");
assertEquals("+1 650 253 0000",
phoneUtil.formatNumberForMobileDialing(usNumberWithExtn, RegionCode.US, true));
assertEquals("8002530000",
phoneUtil.formatNumberForMobileDialing(US_TOLLFREE, RegionCode.US,
false /* remove formatting */));
assertEquals("", phoneUtil.formatNumberForMobileDialing(US_TOLLFREE, RegionCode.CN, false));
assertEquals("+16502530000",
phoneUtil.formatNumberForMobileDialing(US_NUMBER, RegionCode.US, false));
assertEquals("+16502530000",
phoneUtil.formatNumberForMobileDialing(usNumberWithExtn, RegionCode.US, false));
// An invalid US number, which is one digit too long.
assertEquals("+165025300001",
phoneUtil.formatNumberForMobileDialing(US_LONG_NUMBER, RegionCode.US, false));
assertEquals("+1 65025300001",
phoneUtil.formatNumberForMobileDialing(US_LONG_NUMBER, RegionCode.US, true));
// Star numbers. In real life they appear in Israel, but we have them in JP in our test
// metadata.
assertEquals("*2345",
phoneUtil.formatNumberForMobileDialing(JP_STAR_NUMBER, RegionCode.JP, false));
assertEquals("*2345",
phoneUtil.formatNumberForMobileDialing(JP_STAR_NUMBER, RegionCode.JP, true));
assertEquals("+80012345678",
phoneUtil.formatNumberForMobileDialing(INTERNATIONAL_TOLL_FREE, RegionCode.JP, false));
assertEquals("+800 1234 5678",
phoneUtil.formatNumberForMobileDialing(INTERNATIONAL_TOLL_FREE, RegionCode.JP, true));
// UAE numbers beginning with 600 (classified as UAN) need to be dialled without +971 locally.
assertEquals("+971600123456",
phoneUtil.formatNumberForMobileDialing(AE_UAN, RegionCode.JP, false));
assertEquals("600123456",
phoneUtil.formatNumberForMobileDialing(AE_UAN, RegionCode.AE, false));
assertEquals("+523312345678",
phoneUtil.formatNumberForMobileDialing(MX_NUMBER1, RegionCode.MX, false));
assertEquals("+523312345678",
phoneUtil.formatNumberForMobileDialing(MX_NUMBER1, RegionCode.US, false));
// Non-geographical numbers should always be dialed in international format.
assertEquals("+80012345678",
phoneUtil.formatNumberForMobileDialing(INTERNATIONAL_TOLL_FREE, RegionCode.US, false));
assertEquals("+80012345678",
phoneUtil.formatNumberForMobileDialing(INTERNATIONAL_TOLL_FREE, RegionCode.UN001, false));
// Test that a short number is formatted correctly for mobile dialing within the region,
// and is not diallable from outside the region.
PhoneNumber deShortNumber = new PhoneNumber().setCountryCode(49).setNationalNumber(123L);
assertEquals("123", phoneUtil.formatNumberForMobileDialing(deShortNumber, RegionCode.DE,
false));
assertEquals("", phoneUtil.formatNumberForMobileDialing(deShortNumber, RegionCode.IT, false));
// Test the special logic for Hungary, where the national prefix must be added before dialing
// from a mobile phone for regular length numbers, but not for short numbers.
PhoneNumber huRegularNumber = new PhoneNumber().setCountryCode(36)
.setNationalNumber(301234567L);
assertEquals("06301234567", phoneUtil.formatNumberForMobileDialing(huRegularNumber,
RegionCode.HU, false));
assertEquals("+36301234567", phoneUtil.formatNumberForMobileDialing(huRegularNumber,
RegionCode.JP, false));
PhoneNumber huShortNumber = new PhoneNumber().setCountryCode(36).setNationalNumber(104L);
assertEquals("104", phoneUtil.formatNumberForMobileDialing(huShortNumber, RegionCode.HU,
false));
assertEquals("", phoneUtil.formatNumberForMobileDialing(huShortNumber, RegionCode.JP, false));
// Test the special logic for NANPA countries, for which regular length phone numbers are always
// output in international format, but short numbers are in national format.
assertEquals("+16502530000", phoneUtil.formatNumberForMobileDialing(US_NUMBER,
RegionCode.US, false));
assertEquals("+16502530000", phoneUtil.formatNumberForMobileDialing(US_NUMBER,
RegionCode.CA, false));
assertEquals("+16502530000", phoneUtil.formatNumberForMobileDialing(US_NUMBER,
RegionCode.BR, false));
PhoneNumber usShortNumber = new PhoneNumber().setCountryCode(1).setNationalNumber(911L);
assertEquals("911", phoneUtil.formatNumberForMobileDialing(usShortNumber, RegionCode.US,
false));
assertEquals("", phoneUtil.formatNumberForMobileDialing(usShortNumber, RegionCode.CA, false));
assertEquals("", phoneUtil.formatNumberForMobileDialing(usShortNumber, RegionCode.BR, false));
// Test that the Australian emergency number 000 is formatted correctly.
PhoneNumber auNumber = new PhoneNumber().setCountryCode(61).setNationalNumber(0L)
.setItalianLeadingZero(true).setNumberOfLeadingZeros(2);
assertEquals("000", phoneUtil.formatNumberForMobileDialing(auNumber, RegionCode.AU, false));
assertEquals("", phoneUtil.formatNumberForMobileDialing(auNumber, RegionCode.NZ, false));
}