2 * Copyright (C) 2009 The Libphonenumber Authors
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package com.google.i18n.phonenumbers;
19 import com.google.i18n.phonenumbers.PhoneNumberUtil.PhoneNumberFormat;
20 import com.google.i18n.phonenumbers.Phonemetadata.NumberFormat;
21 import com.google.i18n.phonenumbers.Phonemetadata.PhoneMetadata;
22 import com.google.i18n.phonenumbers.Phonemetadata.PhoneNumberDesc;
23 import com.google.i18n.phonenumbers.Phonenumber.PhoneNumber;
24 import com.google.i18n.phonenumbers.Phonenumber.PhoneNumber.CountryCodeSource;
26 import java.util.ArrayList;
27 import java.util.List;
30 * Unit tests for PhoneNumberUtil.java
32 * Note that these tests use the test metadata, not the normal metadata file, so should not be used
33 * for regression test purposes - these tests are illustrative only and test functionality.
35 * @author Shaopeng Jia
38 public class PhoneNumberUtilTest extends TestMetadataTestCase {
39 // Set up some test numbers to re-use.
40 private static final PhoneNumber ALPHA_NUMERIC_NUMBER =
41 new PhoneNumber().setCountryCode(1).setNationalNumber(80074935247L);
42 private static final PhoneNumber AR_MOBILE =
43 new PhoneNumber().setCountryCode(54).setNationalNumber(91187654321L);
44 private static final PhoneNumber AR_NUMBER =
45 new PhoneNumber().setCountryCode(54).setNationalNumber(1187654321);
46 private static final PhoneNumber AU_NUMBER =
47 new PhoneNumber().setCountryCode(61).setNationalNumber(236618300L);
48 private static final PhoneNumber BS_MOBILE =
49 new PhoneNumber().setCountryCode(1).setNationalNumber(2423570000L);
50 private static final PhoneNumber BS_NUMBER =
51 new PhoneNumber().setCountryCode(1).setNationalNumber(2423651234L);
52 // Note that this is the same as the example number for DE in the metadata.
53 private static final PhoneNumber DE_NUMBER =
54 new PhoneNumber().setCountryCode(49).setNationalNumber(30123456L);
55 private static final PhoneNumber DE_SHORT_NUMBER =
56 new PhoneNumber().setCountryCode(49).setNationalNumber(1234L);
57 private static final PhoneNumber GB_MOBILE =
58 new PhoneNumber().setCountryCode(44).setNationalNumber(7912345678L);
59 private static final PhoneNumber GB_NUMBER =
60 new PhoneNumber().setCountryCode(44).setNationalNumber(2070313000L);
61 private static final PhoneNumber IT_MOBILE =
62 new PhoneNumber().setCountryCode(39).setNationalNumber(345678901L);
63 private static final PhoneNumber IT_NUMBER =
64 new PhoneNumber().setCountryCode(39).setNationalNumber(236618300L).
65 setItalianLeadingZero(true);
66 private static final PhoneNumber JP_STAR_NUMBER =
67 new PhoneNumber().setCountryCode(81).setNationalNumber(2345);
68 // Numbers to test the formatting rules from Mexico.
69 private static final PhoneNumber MX_MOBILE1 =
70 new PhoneNumber().setCountryCode(52).setNationalNumber(12345678900L);
71 private static final PhoneNumber MX_MOBILE2 =
72 new PhoneNumber().setCountryCode(52).setNationalNumber(15512345678L);
73 private static final PhoneNumber MX_NUMBER1 =
74 new PhoneNumber().setCountryCode(52).setNationalNumber(3312345678L);
75 private static final PhoneNumber MX_NUMBER2 =
76 new PhoneNumber().setCountryCode(52).setNationalNumber(8211234567L);
77 private static final PhoneNumber NZ_NUMBER =
78 new PhoneNumber().setCountryCode(64).setNationalNumber(33316005L);
79 private static final PhoneNumber SG_NUMBER =
80 new PhoneNumber().setCountryCode(65).setNationalNumber(65218000L);
81 // A too-long and hence invalid US number.
82 private static final PhoneNumber US_LONG_NUMBER =
83 new PhoneNumber().setCountryCode(1).setNationalNumber(65025300001L);
84 private static final PhoneNumber US_NUMBER =
85 new PhoneNumber().setCountryCode(1).setNationalNumber(6502530000L);
86 private static final PhoneNumber US_PREMIUM =
87 new PhoneNumber().setCountryCode(1).setNationalNumber(9002530000L);
88 // Too short, but still possible US numbers.
89 private static final PhoneNumber US_LOCAL_NUMBER =
90 new PhoneNumber().setCountryCode(1).setNationalNumber(2530000L);
91 private static final PhoneNumber US_SHORT_BY_ONE_NUMBER =
92 new PhoneNumber().setCountryCode(1).setNationalNumber(650253000L);
93 private static final PhoneNumber US_TOLLFREE =
94 new PhoneNumber().setCountryCode(1).setNationalNumber(8002530000L);
95 private static final PhoneNumber US_SPOOF =
96 new PhoneNumber().setCountryCode(1).setNationalNumber(0L);
97 private static final PhoneNumber US_SPOOF_WITH_RAW_INPUT =
98 new PhoneNumber().setCountryCode(1).setNationalNumber(0L)
99 .setRawInput("000-000-0000");
100 private static final PhoneNumber INTERNATIONAL_TOLL_FREE =
101 new PhoneNumber().setCountryCode(800).setNationalNumber(12345678L);
102 // We set this to be the same length as numbers for the other non-geographical country prefix that
103 // we have in our test metadata. However, this is not considered valid because they differ in
104 // their country calling code.
105 private static final PhoneNumber INTERNATIONAL_TOLL_FREE_TOO_LONG =
106 new PhoneNumber().setCountryCode(800).setNationalNumber(123456789L);
107 private static final PhoneNumber UNIVERSAL_PREMIUM_RATE =
108 new PhoneNumber().setCountryCode(979).setNationalNumber(123456789L);
110 public void testGetSupportedRegions() {
111 assertTrue(phoneUtil.getSupportedRegions().size() > 0);
114 public void testGetInstanceLoadUSMetadata() {
115 PhoneMetadata metadata = phoneUtil.getMetadataForRegion(RegionCode.US);
116 assertEquals("US", metadata.getId());
117 assertEquals(1, metadata.getCountryCode());
118 assertEquals("011", metadata.getInternationalPrefix());
119 assertTrue(metadata.hasNationalPrefix());
120 assertEquals(2, metadata.numberFormatSize());
121 assertEquals("(\\d{3})(\\d{3})(\\d{4})",
122 metadata.getNumberFormat(1).getPattern());
123 assertEquals("$1 $2 $3", metadata.getNumberFormat(1).getFormat());
124 assertEquals("[13-689]\\d{9}|2[0-35-9]\\d{8}",
125 metadata.getGeneralDesc().getNationalNumberPattern());
126 assertEquals("\\d{7}(?:\\d{3})?", metadata.getGeneralDesc().getPossibleNumberPattern());
127 assertTrue(metadata.getGeneralDesc().exactlySameAs(metadata.getFixedLine()));
128 assertEquals("\\d{10}", metadata.getTollFree().getPossibleNumberPattern());
129 assertEquals("900\\d{7}", metadata.getPremiumRate().getNationalNumberPattern());
130 // No shared-cost data is available, so it should be initialised to "NA".
131 assertEquals("NA", metadata.getSharedCost().getNationalNumberPattern());
132 assertEquals("NA", metadata.getSharedCost().getPossibleNumberPattern());
135 public void testGetInstanceLoadDEMetadata() {
136 PhoneMetadata metadata = phoneUtil.getMetadataForRegion(RegionCode.DE);
137 assertEquals("DE", metadata.getId());
138 assertEquals(49, metadata.getCountryCode());
139 assertEquals("00", metadata.getInternationalPrefix());
140 assertEquals("0", metadata.getNationalPrefix());
141 assertEquals(6, metadata.numberFormatSize());
142 assertEquals(1, metadata.getNumberFormat(5).leadingDigitsPatternSize());
143 assertEquals("900", metadata.getNumberFormat(5).getLeadingDigitsPattern(0));
144 assertEquals("(\\d{3})(\\d{3,4})(\\d{4})",
145 metadata.getNumberFormat(5).getPattern());
146 assertEquals("$1 $2 $3", metadata.getNumberFormat(5).getFormat());
147 assertEquals("(?:[24-6]\\d{2}|3[03-9]\\d|[789](?:[1-9]\\d|0[2-9]))\\d{1,8}",
148 metadata.getFixedLine().getNationalNumberPattern());
149 assertEquals("\\d{2,14}", metadata.getFixedLine().getPossibleNumberPattern());
150 assertEquals("30123456", metadata.getFixedLine().getExampleNumber());
151 assertEquals("\\d{10}", metadata.getTollFree().getPossibleNumberPattern());
152 assertEquals("900([135]\\d{6}|9\\d{7})", metadata.getPremiumRate().getNationalNumberPattern());
155 public void testGetInstanceLoadARMetadata() {
156 PhoneMetadata metadata = phoneUtil.getMetadataForRegion(RegionCode.AR);
157 assertEquals("AR", metadata.getId());
158 assertEquals(54, metadata.getCountryCode());
159 assertEquals("00", metadata.getInternationalPrefix());
160 assertEquals("0", metadata.getNationalPrefix());
161 assertEquals("0(?:(11|343|3715)15)?", metadata.getNationalPrefixForParsing());
162 assertEquals("9$1", metadata.getNationalPrefixTransformRule());
163 assertEquals("$2 15 $3-$4", metadata.getNumberFormat(2).getFormat());
164 assertEquals("(9)(\\d{4})(\\d{2})(\\d{4})",
165 metadata.getNumberFormat(3).getPattern());
166 assertEquals("(9)(\\d{4})(\\d{2})(\\d{4})",
167 metadata.getIntlNumberFormat(3).getPattern());
168 assertEquals("$1 $2 $3 $4", metadata.getIntlNumberFormat(3).getFormat());
171 public void testGetInstanceLoadInternationalTollFreeMetadata() {
172 PhoneMetadata metadata = phoneUtil.getMetadataForNonGeographicalRegion(800);
173 assertEquals("001", metadata.getId());
174 assertEquals(800, metadata.getCountryCode());
175 assertEquals("$1 $2", metadata.getNumberFormat(0).getFormat());
176 assertEquals("(\\d{4})(\\d{4})", metadata.getNumberFormat(0).getPattern());
177 assertEquals("12345678", metadata.getGeneralDesc().getExampleNumber());
178 assertEquals("12345678", metadata.getTollFree().getExampleNumber());
181 public void testIsLeadingZeroPossible() {
182 assertTrue(phoneUtil.isLeadingZeroPossible(39)); // Italy
183 assertFalse(phoneUtil.isLeadingZeroPossible(1)); // USA
184 assertFalse(phoneUtil.isLeadingZeroPossible(800)); // International toll free numbers
185 assertFalse(phoneUtil.isLeadingZeroPossible(888)); // Not in metadata file, just default to
189 public void testGetLengthOfGeographicalAreaCode() {
190 // Google MTV, which has area code "650".
191 assertEquals(3, phoneUtil.getLengthOfGeographicalAreaCode(US_NUMBER));
193 // A North America toll-free number, which has no area code.
194 assertEquals(0, phoneUtil.getLengthOfGeographicalAreaCode(US_TOLLFREE));
196 // Google London, which has area code "20".
197 assertEquals(2, phoneUtil.getLengthOfGeographicalAreaCode(GB_NUMBER));
199 // A UK mobile phone, which has no area code.
200 assertEquals(0, phoneUtil.getLengthOfGeographicalAreaCode(GB_MOBILE));
202 // Google Buenos Aires, which has area code "11".
203 assertEquals(2, phoneUtil.getLengthOfGeographicalAreaCode(AR_NUMBER));
205 // Google Sydney, which has area code "2".
206 assertEquals(1, phoneUtil.getLengthOfGeographicalAreaCode(AU_NUMBER));
208 // Italian numbers - there is no national prefix, but it still has an area code.
209 assertEquals(2, phoneUtil.getLengthOfGeographicalAreaCode(IT_NUMBER));
211 // Google Singapore. Singapore has no area code and no national prefix.
212 assertEquals(0, phoneUtil.getLengthOfGeographicalAreaCode(SG_NUMBER));
214 // An invalid US number (1 digit shorter), which has no area code.
215 assertEquals(0, phoneUtil.getLengthOfGeographicalAreaCode(US_SHORT_BY_ONE_NUMBER));
217 // An international toll free number, which has no area code.
218 assertEquals(0, phoneUtil.getLengthOfGeographicalAreaCode(INTERNATIONAL_TOLL_FREE));
221 public void testGetLengthOfNationalDestinationCode() {
222 // Google MTV, which has national destination code (NDC) "650".
223 assertEquals(3, phoneUtil.getLengthOfNationalDestinationCode(US_NUMBER));
225 // A North America toll-free number, which has NDC "800".
226 assertEquals(3, phoneUtil.getLengthOfNationalDestinationCode(US_TOLLFREE));
228 // Google London, which has NDC "20".
229 assertEquals(2, phoneUtil.getLengthOfNationalDestinationCode(GB_NUMBER));
231 // A UK mobile phone, which has NDC "7912".
232 assertEquals(4, phoneUtil.getLengthOfNationalDestinationCode(GB_MOBILE));
234 // Google Buenos Aires, which has NDC "11".
235 assertEquals(2, phoneUtil.getLengthOfNationalDestinationCode(AR_NUMBER));
237 // An Argentinian mobile which has NDC "911".
238 assertEquals(3, phoneUtil.getLengthOfNationalDestinationCode(AR_MOBILE));
240 // Google Sydney, which has NDC "2".
241 assertEquals(1, phoneUtil.getLengthOfNationalDestinationCode(AU_NUMBER));
243 // Google Singapore, which has NDC "6521".
244 assertEquals(4, phoneUtil.getLengthOfNationalDestinationCode(SG_NUMBER));
246 // An invalid US number (1 digit shorter), which has no NDC.
247 assertEquals(0, phoneUtil.getLengthOfNationalDestinationCode(US_SHORT_BY_ONE_NUMBER));
249 // A number containing an invalid country calling code, which shouldn't have any NDC.
250 PhoneNumber number = new PhoneNumber().setCountryCode(123).setNationalNumber(6502530000L);
251 assertEquals(0, phoneUtil.getLengthOfNationalDestinationCode(number));
253 // An international toll free number, which has NDC "1234".
254 assertEquals(4, phoneUtil.getLengthOfNationalDestinationCode(INTERNATIONAL_TOLL_FREE));
257 public void testGetNationalSignificantNumber() {
258 assertEquals("6502530000", phoneUtil.getNationalSignificantNumber(US_NUMBER));
260 // An Italian mobile number.
261 assertEquals("345678901", phoneUtil.getNationalSignificantNumber(IT_MOBILE));
263 // An Italian fixed line number.
264 assertEquals("0236618300", phoneUtil.getNationalSignificantNumber(IT_NUMBER));
266 assertEquals("12345678", phoneUtil.getNationalSignificantNumber(INTERNATIONAL_TOLL_FREE));
269 public void testGetExampleNumber() {
270 assertEquals(DE_NUMBER, phoneUtil.getExampleNumber(RegionCode.DE));
272 assertEquals(DE_NUMBER,
273 phoneUtil.getExampleNumberForType(RegionCode.DE,
274 PhoneNumberUtil.PhoneNumberType.FIXED_LINE));
276 phoneUtil.getExampleNumberForType(RegionCode.DE,
277 PhoneNumberUtil.PhoneNumberType.MOBILE));
278 // For the US, the example number is placed under general description, and hence should be used
279 // for both fixed line and mobile, so neither of these should return null.
280 assertNotNull(phoneUtil.getExampleNumberForType(RegionCode.US,
281 PhoneNumberUtil.PhoneNumberType.FIXED_LINE));
282 assertNotNull(phoneUtil.getExampleNumberForType(RegionCode.US,
283 PhoneNumberUtil.PhoneNumberType.MOBILE));
284 // CS is an invalid region, so we have no data for it.
285 assertNull(phoneUtil.getExampleNumberForType(RegionCode.CS,
286 PhoneNumberUtil.PhoneNumberType.MOBILE));
287 // RegionCode 001 is reserved for supporting non-geographical country calling code. We don't
288 // support getting an example number for it with this method.
289 assertEquals(null, phoneUtil.getExampleNumber(RegionCode.UN001));
292 public void testGetExampleNumberForNonGeoEntity() {
293 assertEquals(INTERNATIONAL_TOLL_FREE, phoneUtil.getExampleNumberForNonGeoEntity(800));
294 assertEquals(UNIVERSAL_PREMIUM_RATE, phoneUtil.getExampleNumberForNonGeoEntity(979));
297 public void testConvertAlphaCharactersInNumber() {
298 String input = "1800-ABC-DEF";
299 // Alpha chars are converted to digits; everything else is left untouched.
300 String expectedOutput = "1800-222-333";
301 assertEquals(expectedOutput, PhoneNumberUtil.convertAlphaCharactersInNumber(input));
304 public void testNormaliseRemovePunctuation() {
305 String inputNumber = "034-56&+#2\u00AD34";
306 String expectedOutput = "03456234";
307 assertEquals("Conversion did not correctly remove punctuation",
309 PhoneNumberUtil.normalize(inputNumber));
312 public void testNormaliseReplaceAlphaCharacters() {
313 String inputNumber = "034-I-am-HUNGRY";
314 String expectedOutput = "034426486479";
315 assertEquals("Conversion did not correctly replace alpha characters",
317 PhoneNumberUtil.normalize(inputNumber));
320 public void testNormaliseOtherDigits() {
321 String inputNumber = "\uFF125\u0665";
322 String expectedOutput = "255";
323 assertEquals("Conversion did not correctly replace non-latin digits",
325 PhoneNumberUtil.normalize(inputNumber));
326 // Eastern-Arabic digits.
327 inputNumber = "\u06F52\u06F0";
328 expectedOutput = "520";
329 assertEquals("Conversion did not correctly replace non-latin digits",
331 PhoneNumberUtil.normalize(inputNumber));
334 public void testNormaliseStripAlphaCharacters() {
335 String inputNumber = "034-56&+a#234";
336 String expectedOutput = "03456234";
337 assertEquals("Conversion did not correctly remove alpha character",
339 PhoneNumberUtil.normalizeDigitsOnly(inputNumber));
342 public void testFormatUSNumber() {
343 assertEquals("650 253 0000", phoneUtil.format(US_NUMBER, PhoneNumberFormat.NATIONAL));
344 assertEquals("+1 650 253 0000", phoneUtil.format(US_NUMBER, PhoneNumberFormat.INTERNATIONAL));
346 assertEquals("800 253 0000", phoneUtil.format(US_TOLLFREE, PhoneNumberFormat.NATIONAL));
347 assertEquals("+1 800 253 0000", phoneUtil.format(US_TOLLFREE, PhoneNumberFormat.INTERNATIONAL));
349 assertEquals("900 253 0000", phoneUtil.format(US_PREMIUM, PhoneNumberFormat.NATIONAL));
350 assertEquals("+1 900 253 0000", phoneUtil.format(US_PREMIUM, PhoneNumberFormat.INTERNATIONAL));
351 assertEquals("tel:+1-900-253-0000", phoneUtil.format(US_PREMIUM, PhoneNumberFormat.RFC3966));
352 // Numbers with all zeros in the national number part will be formatted by using the raw_input
353 // if that is available no matter which format is specified.
354 assertEquals("000-000-0000",
355 phoneUtil.format(US_SPOOF_WITH_RAW_INPUT, PhoneNumberFormat.NATIONAL));
356 assertEquals("0", phoneUtil.format(US_SPOOF, PhoneNumberFormat.NATIONAL));
359 public void testFormatBSNumber() {
360 assertEquals("242 365 1234", phoneUtil.format(BS_NUMBER, PhoneNumberFormat.NATIONAL));
361 assertEquals("+1 242 365 1234", phoneUtil.format(BS_NUMBER, PhoneNumberFormat.INTERNATIONAL));
364 public void testFormatGBNumber() {
365 assertEquals("(020) 7031 3000", phoneUtil.format(GB_NUMBER, PhoneNumberFormat.NATIONAL));
366 assertEquals("+44 20 7031 3000", phoneUtil.format(GB_NUMBER, PhoneNumberFormat.INTERNATIONAL));
368 assertEquals("(07912) 345 678", phoneUtil.format(GB_MOBILE, PhoneNumberFormat.NATIONAL));
369 assertEquals("+44 7912 345 678", phoneUtil.format(GB_MOBILE, PhoneNumberFormat.INTERNATIONAL));
372 public void testFormatDENumber() {
373 PhoneNumber deNumber = new PhoneNumber();
374 deNumber.setCountryCode(49).setNationalNumber(301234L);
375 assertEquals("030/1234", phoneUtil.format(deNumber, PhoneNumberFormat.NATIONAL));
376 assertEquals("+49 30/1234", phoneUtil.format(deNumber, PhoneNumberFormat.INTERNATIONAL));
377 assertEquals("tel:+49-30-1234", phoneUtil.format(deNumber, PhoneNumberFormat.RFC3966));
380 deNumber.setCountryCode(49).setNationalNumber(291123L);
381 assertEquals("0291 123", phoneUtil.format(deNumber, PhoneNumberFormat.NATIONAL));
382 assertEquals("+49 291 123", phoneUtil.format(deNumber, PhoneNumberFormat.INTERNATIONAL));
385 deNumber.setCountryCode(49).setNationalNumber(29112345678L);
386 assertEquals("0291 12345678", phoneUtil.format(deNumber, PhoneNumberFormat.NATIONAL));
387 assertEquals("+49 291 12345678", phoneUtil.format(deNumber, PhoneNumberFormat.INTERNATIONAL));
390 deNumber.setCountryCode(49).setNationalNumber(912312345L);
391 assertEquals("09123 12345", phoneUtil.format(deNumber, PhoneNumberFormat.NATIONAL));
392 assertEquals("+49 9123 12345", phoneUtil.format(deNumber, PhoneNumberFormat.INTERNATIONAL));
394 deNumber.setCountryCode(49).setNationalNumber(80212345L);
395 assertEquals("08021 2345", phoneUtil.format(deNumber, PhoneNumberFormat.NATIONAL));
396 assertEquals("+49 8021 2345", phoneUtil.format(deNumber, PhoneNumberFormat.INTERNATIONAL));
397 // Note this number is correctly formatted without national prefix. Most of the numbers that
398 // are treated as invalid numbers by the library are short numbers, and they are usually not
399 // dialed with national prefix.
400 assertEquals("1234", phoneUtil.format(DE_SHORT_NUMBER, PhoneNumberFormat.NATIONAL));
401 assertEquals("+49 1234", phoneUtil.format(DE_SHORT_NUMBER, PhoneNumberFormat.INTERNATIONAL));
404 deNumber.setCountryCode(49).setNationalNumber(41341234);
405 assertEquals("04134 1234", phoneUtil.format(deNumber, PhoneNumberFormat.NATIONAL));
408 public void testFormatITNumber() {
409 assertEquals("02 3661 8300", phoneUtil.format(IT_NUMBER, PhoneNumberFormat.NATIONAL));
410 assertEquals("+39 02 3661 8300", phoneUtil.format(IT_NUMBER, PhoneNumberFormat.INTERNATIONAL));
411 assertEquals("+390236618300", phoneUtil.format(IT_NUMBER, PhoneNumberFormat.E164));
413 assertEquals("345 678 901", phoneUtil.format(IT_MOBILE, PhoneNumberFormat.NATIONAL));
414 assertEquals("+39 345 678 901", phoneUtil.format(IT_MOBILE, PhoneNumberFormat.INTERNATIONAL));
415 assertEquals("+39345678901", phoneUtil.format(IT_MOBILE, PhoneNumberFormat.E164));
418 public void testFormatAUNumber() {
419 assertEquals("02 3661 8300", phoneUtil.format(AU_NUMBER, PhoneNumberFormat.NATIONAL));
420 assertEquals("+61 2 3661 8300", phoneUtil.format(AU_NUMBER, PhoneNumberFormat.INTERNATIONAL));
421 assertEquals("+61236618300", phoneUtil.format(AU_NUMBER, PhoneNumberFormat.E164));
423 PhoneNumber auNumber = new PhoneNumber().setCountryCode(61).setNationalNumber(1800123456L);
424 assertEquals("1800 123 456", phoneUtil.format(auNumber, PhoneNumberFormat.NATIONAL));
425 assertEquals("+61 1800 123 456", phoneUtil.format(auNumber, PhoneNumberFormat.INTERNATIONAL));
426 assertEquals("+611800123456", phoneUtil.format(auNumber, PhoneNumberFormat.E164));
429 public void testFormatARNumber() {
430 assertEquals("011 8765-4321", phoneUtil.format(AR_NUMBER, PhoneNumberFormat.NATIONAL));
431 assertEquals("+54 11 8765-4321", phoneUtil.format(AR_NUMBER, PhoneNumberFormat.INTERNATIONAL));
432 assertEquals("+541187654321", phoneUtil.format(AR_NUMBER, PhoneNumberFormat.E164));
434 assertEquals("011 15 8765-4321", phoneUtil.format(AR_MOBILE, PhoneNumberFormat.NATIONAL));
435 assertEquals("+54 9 11 8765 4321", phoneUtil.format(AR_MOBILE,
436 PhoneNumberFormat.INTERNATIONAL));
437 assertEquals("+5491187654321", phoneUtil.format(AR_MOBILE, PhoneNumberFormat.E164));
440 public void testFormatMXNumber() {
441 assertEquals("045 234 567 8900", phoneUtil.format(MX_MOBILE1, PhoneNumberFormat.NATIONAL));
442 assertEquals("+52 1 234 567 8900", phoneUtil.format(
443 MX_MOBILE1, PhoneNumberFormat.INTERNATIONAL));
444 assertEquals("+5212345678900", phoneUtil.format(MX_MOBILE1, PhoneNumberFormat.E164));
446 assertEquals("045 55 1234 5678", phoneUtil.format(MX_MOBILE2, PhoneNumberFormat.NATIONAL));
447 assertEquals("+52 1 55 1234 5678", phoneUtil.format(
448 MX_MOBILE2, PhoneNumberFormat.INTERNATIONAL));
449 assertEquals("+5215512345678", phoneUtil.format(MX_MOBILE2, PhoneNumberFormat.E164));
451 assertEquals("01 33 1234 5678", phoneUtil.format(MX_NUMBER1, PhoneNumberFormat.NATIONAL));
452 assertEquals("+52 33 1234 5678", phoneUtil.format(MX_NUMBER1, PhoneNumberFormat.INTERNATIONAL));
453 assertEquals("+523312345678", phoneUtil.format(MX_NUMBER1, PhoneNumberFormat.E164));
455 assertEquals("01 821 123 4567", phoneUtil.format(MX_NUMBER2, PhoneNumberFormat.NATIONAL));
456 assertEquals("+52 821 123 4567", phoneUtil.format(MX_NUMBER2, PhoneNumberFormat.INTERNATIONAL));
457 assertEquals("+528211234567", phoneUtil.format(MX_NUMBER2, PhoneNumberFormat.E164));
460 public void testFormatOutOfCountryCallingNumber() {
461 assertEquals("00 1 900 253 0000",
462 phoneUtil.formatOutOfCountryCallingNumber(US_PREMIUM, RegionCode.DE));
464 assertEquals("1 650 253 0000",
465 phoneUtil.formatOutOfCountryCallingNumber(US_NUMBER, RegionCode.BS));
467 assertEquals("00 1 650 253 0000",
468 phoneUtil.formatOutOfCountryCallingNumber(US_NUMBER, RegionCode.PL));
470 assertEquals("011 44 7912 345 678",
471 phoneUtil.formatOutOfCountryCallingNumber(GB_MOBILE, RegionCode.US));
473 assertEquals("00 49 1234",
474 phoneUtil.formatOutOfCountryCallingNumber(DE_SHORT_NUMBER, RegionCode.GB));
475 // Note this number is correctly formatted without national prefix. Most of the numbers that
476 // are treated as invalid numbers by the library are short numbers, and they are usually not
477 // dialed with national prefix.
478 assertEquals("1234", phoneUtil.formatOutOfCountryCallingNumber(DE_SHORT_NUMBER, RegionCode.DE));
480 assertEquals("011 39 02 3661 8300",
481 phoneUtil.formatOutOfCountryCallingNumber(IT_NUMBER, RegionCode.US));
482 assertEquals("02 3661 8300",
483 phoneUtil.formatOutOfCountryCallingNumber(IT_NUMBER, RegionCode.IT));
484 assertEquals("+39 02 3661 8300",
485 phoneUtil.formatOutOfCountryCallingNumber(IT_NUMBER, RegionCode.SG));
487 assertEquals("6521 8000",
488 phoneUtil.formatOutOfCountryCallingNumber(SG_NUMBER, RegionCode.SG));
490 assertEquals("011 54 9 11 8765 4321",
491 phoneUtil.formatOutOfCountryCallingNumber(AR_MOBILE, RegionCode.US));
492 assertEquals("011 800 1234 5678",
493 phoneUtil.formatOutOfCountryCallingNumber(INTERNATIONAL_TOLL_FREE, RegionCode.US));
495 PhoneNumber arNumberWithExtn = new PhoneNumber().mergeFrom(AR_MOBILE).setExtension("1234");
496 assertEquals("011 54 9 11 8765 4321 ext. 1234",
497 phoneUtil.formatOutOfCountryCallingNumber(arNumberWithExtn, RegionCode.US));
498 assertEquals("0011 54 9 11 8765 4321 ext. 1234",
499 phoneUtil.formatOutOfCountryCallingNumber(arNumberWithExtn, RegionCode.AU));
500 assertEquals("011 15 8765-4321 ext. 1234",
501 phoneUtil.formatOutOfCountryCallingNumber(arNumberWithExtn, RegionCode.AR));
504 public void testFormatOutOfCountryWithInvalidRegion() {
505 // AQ/Antarctica isn't a valid region code for phone number formatting,
506 // so this falls back to intl formatting.
507 assertEquals("+1 650 253 0000",
508 phoneUtil.formatOutOfCountryCallingNumber(US_NUMBER, RegionCode.AQ));
509 // For region code 001, the out-of-country format always turns into the international format.
510 assertEquals("+1 650 253 0000",
511 phoneUtil.formatOutOfCountryCallingNumber(US_NUMBER, RegionCode.UN001));
514 public void testFormatOutOfCountryWithPreferredIntlPrefix() {
515 // This should use 0011, since that is the preferred international prefix (both 0011 and 0012
516 // are accepted as possible international prefixes in our test metadta.)
517 assertEquals("0011 39 02 3661 8300",
518 phoneUtil.formatOutOfCountryCallingNumber(IT_NUMBER, RegionCode.AU));
521 public void testFormatOutOfCountryKeepingAlphaChars() {
522 PhoneNumber alphaNumericNumber = new PhoneNumber();
523 alphaNumericNumber.setCountryCode(1).setNationalNumber(8007493524L)
524 .setRawInput("1800 six-flag");
525 assertEquals("0011 1 800 SIX-FLAG",
526 phoneUtil.formatOutOfCountryKeepingAlphaChars(alphaNumericNumber, RegionCode.AU));
528 alphaNumericNumber.setRawInput("1-800-SIX-flag");
529 assertEquals("0011 1 800-SIX-FLAG",
530 phoneUtil.formatOutOfCountryKeepingAlphaChars(alphaNumericNumber, RegionCode.AU));
532 alphaNumericNumber.setRawInput("Call us from UK: 00 1 800 SIX-flag");
533 assertEquals("0011 1 800 SIX-FLAG",
534 phoneUtil.formatOutOfCountryKeepingAlphaChars(alphaNumericNumber, RegionCode.AU));
536 alphaNumericNumber.setRawInput("800 SIX-flag");
537 assertEquals("0011 1 800 SIX-FLAG",
538 phoneUtil.formatOutOfCountryKeepingAlphaChars(alphaNumericNumber, RegionCode.AU));
540 // Formatting from within the NANPA region.
541 assertEquals("1 800 SIX-FLAG",
542 phoneUtil.formatOutOfCountryKeepingAlphaChars(alphaNumericNumber, RegionCode.US));
544 assertEquals("1 800 SIX-FLAG",
545 phoneUtil.formatOutOfCountryKeepingAlphaChars(alphaNumericNumber, RegionCode.BS));
547 // Testing that if the raw input doesn't exist, it is formatted using
548 // formatOutOfCountryCallingNumber.
549 alphaNumericNumber.clearRawInput();
550 assertEquals("00 1 800 749 3524",
551 phoneUtil.formatOutOfCountryKeepingAlphaChars(alphaNumericNumber, RegionCode.DE));
553 // Testing AU alpha number formatted from Australia.
554 alphaNumericNumber.setCountryCode(61).setNationalNumber(827493524L)
555 .setRawInput("+61 82749-FLAG");
556 // This number should have the national prefix fixed.
557 assertEquals("082749-FLAG",
558 phoneUtil.formatOutOfCountryKeepingAlphaChars(alphaNumericNumber, RegionCode.AU));
560 alphaNumericNumber.setRawInput("082749-FLAG");
561 assertEquals("082749-FLAG",
562 phoneUtil.formatOutOfCountryKeepingAlphaChars(alphaNumericNumber, RegionCode.AU));
564 alphaNumericNumber.setNationalNumber(18007493524L).setRawInput("1-800-SIX-flag");
565 // This number should not have the national prefix prefixed, in accordance with the override for
566 // this specific formatting rule.
567 assertEquals("1-800-SIX-FLAG",
568 phoneUtil.formatOutOfCountryKeepingAlphaChars(alphaNumericNumber, RegionCode.AU));
570 // The metadata should not be permanently changed, since we copied it before modifying patterns.
571 // Here we check this.
572 alphaNumericNumber.setNationalNumber(1800749352L);
573 assertEquals("1800 749 352",
574 phoneUtil.formatOutOfCountryCallingNumber(alphaNumericNumber, RegionCode.AU));
576 // Testing a region with multiple international prefixes.
577 assertEquals("+61 1-800-SIX-FLAG",
578 phoneUtil.formatOutOfCountryKeepingAlphaChars(alphaNumericNumber, RegionCode.SG));
579 // Testing the case of calling from a non-supported region.
580 assertEquals("+61 1-800-SIX-FLAG",
581 phoneUtil.formatOutOfCountryKeepingAlphaChars(alphaNumericNumber, RegionCode.AQ));
583 // Testing the case with an invalid country calling code.
584 alphaNumericNumber.setCountryCode(0).setNationalNumber(18007493524L)
585 .setRawInput("1-800-SIX-flag");
586 // Uses the raw input only.
587 assertEquals("1-800-SIX-flag",
588 phoneUtil.formatOutOfCountryKeepingAlphaChars(alphaNumericNumber, RegionCode.DE));
590 // Testing the case of an invalid alpha number.
591 alphaNumericNumber.setCountryCode(1).setNationalNumber(80749L).setRawInput("180-SIX");
592 // No country-code stripping can be done.
593 assertEquals("00 1 180-SIX",
594 phoneUtil.formatOutOfCountryKeepingAlphaChars(alphaNumericNumber, RegionCode.DE));
596 // Testing the case of calling from a non-supported region.
597 alphaNumericNumber.setCountryCode(1).setNationalNumber(80749L).setRawInput("180-SIX");
598 // No country-code stripping can be done since the number is invalid.
599 assertEquals("+1 180-SIX",
600 phoneUtil.formatOutOfCountryKeepingAlphaChars(alphaNumericNumber, RegionCode.AQ));
603 public void testFormatWithCarrierCode() {
604 // We only support this for AR in our test metadata, and only for mobile numbers starting with
606 PhoneNumber arMobile = new PhoneNumber().setCountryCode(54).setNationalNumber(92234654321L);
607 assertEquals("02234 65-4321", phoneUtil.format(arMobile, PhoneNumberFormat.NATIONAL));
608 // Here we force 14 as the carrier code.
609 assertEquals("02234 14 65-4321",
610 phoneUtil.formatNationalNumberWithCarrierCode(arMobile, "14"));
611 // Here we force the number to be shown with no carrier code.
612 assertEquals("02234 65-4321",
613 phoneUtil.formatNationalNumberWithCarrierCode(arMobile, ""));
614 // Here the international rule is used, so no carrier code should be present.
615 assertEquals("+5492234654321", phoneUtil.format(arMobile, PhoneNumberFormat.E164));
616 // We don't support this for the US so there should be no change.
617 assertEquals("650 253 0000", phoneUtil.formatNationalNumberWithCarrierCode(US_NUMBER, "15"));
620 public void testFormatWithPreferredCarrierCode() {
621 // We only support this for AR in our test metadata.
622 PhoneNumber arNumber = new PhoneNumber();
623 arNumber.setCountryCode(54).setNationalNumber(91234125678L);
624 // Test formatting with no preferred carrier code stored in the number itself.
625 assertEquals("01234 15 12-5678",
626 phoneUtil.formatNationalNumberWithPreferredCarrierCode(arNumber, "15"));
627 assertEquals("01234 12-5678",
628 phoneUtil.formatNationalNumberWithPreferredCarrierCode(arNumber, ""));
629 // Test formatting with preferred carrier code present.
630 arNumber.setPreferredDomesticCarrierCode("19");
631 assertEquals("01234 12-5678", phoneUtil.format(arNumber, PhoneNumberFormat.NATIONAL));
632 assertEquals("01234 19 12-5678",
633 phoneUtil.formatNationalNumberWithPreferredCarrierCode(arNumber, "15"));
634 assertEquals("01234 19 12-5678",
635 phoneUtil.formatNationalNumberWithPreferredCarrierCode(arNumber, ""));
636 // When the preferred_domestic_carrier_code is present (even when it contains an empty string),
637 // use it instead of the default carrier code passed in.
638 arNumber.setPreferredDomesticCarrierCode("");
639 assertEquals("01234 12-5678",
640 phoneUtil.formatNationalNumberWithPreferredCarrierCode(arNumber, "15"));
641 // We don't support this for the US so there should be no change.
642 PhoneNumber usNumber = new PhoneNumber();
643 usNumber.setCountryCode(1).setNationalNumber(4241231234L).setPreferredDomesticCarrierCode("99");
644 assertEquals("424 123 1234", phoneUtil.format(usNumber, PhoneNumberFormat.NATIONAL));
645 assertEquals("424 123 1234",
646 phoneUtil.formatNationalNumberWithPreferredCarrierCode(usNumber, "15"));
649 public void testFormatNumberForMobileDialing() {
650 // US toll free numbers are marked as noInternationalDialling in the test metadata for testing
652 assertEquals("800 253 0000",
653 phoneUtil.formatNumberForMobileDialing(US_TOLLFREE, RegionCode.US,
654 true /* keep formatting */));
655 assertEquals("", phoneUtil.formatNumberForMobileDialing(US_TOLLFREE, RegionCode.CN, true));
656 assertEquals("+1 650 253 0000",
657 phoneUtil.formatNumberForMobileDialing(US_NUMBER, RegionCode.US, true));
658 PhoneNumber usNumberWithExtn = new PhoneNumber().mergeFrom(US_NUMBER).setExtension("1234");
659 assertEquals("+1 650 253 0000",
660 phoneUtil.formatNumberForMobileDialing(usNumberWithExtn, RegionCode.US, true));
662 assertEquals("8002530000",
663 phoneUtil.formatNumberForMobileDialing(US_TOLLFREE, RegionCode.US,
664 false /* remove formatting */));
665 assertEquals("", phoneUtil.formatNumberForMobileDialing(US_TOLLFREE, RegionCode.CN, false));
666 assertEquals("+16502530000",
667 phoneUtil.formatNumberForMobileDialing(US_NUMBER, RegionCode.US, false));
668 assertEquals("+16502530000",
669 phoneUtil.formatNumberForMobileDialing(usNumberWithExtn, RegionCode.US, false));
671 // An invalid US number, which is one digit too long.
672 assertEquals("+165025300001",
673 phoneUtil.formatNumberForMobileDialing(US_LONG_NUMBER, RegionCode.US, false));
674 assertEquals("+1 65025300001",
675 phoneUtil.formatNumberForMobileDialing(US_LONG_NUMBER, RegionCode.US, true));
677 // Star numbers. In real life they appear in Israel, but we have them in JP in our test
679 assertEquals("*2345",
680 phoneUtil.formatNumberForMobileDialing(JP_STAR_NUMBER, RegionCode.JP, false));
681 assertEquals("*2345",
682 phoneUtil.formatNumberForMobileDialing(JP_STAR_NUMBER, RegionCode.JP, true));
684 assertEquals("+80012345678",
685 phoneUtil.formatNumberForMobileDialing(INTERNATIONAL_TOLL_FREE, RegionCode.JP, false));
686 assertEquals("+800 1234 5678",
687 phoneUtil.formatNumberForMobileDialing(INTERNATIONAL_TOLL_FREE, RegionCode.JP, true));
690 public void testFormatByPattern() {
691 NumberFormat newNumFormat = new NumberFormat();
692 newNumFormat.setPattern("(\\d{3})(\\d{3})(\\d{4})");
693 newNumFormat.setFormat("($1) $2-$3");
694 List<NumberFormat> newNumberFormats = new ArrayList<NumberFormat>();
695 newNumberFormats.add(newNumFormat);
697 assertEquals("(650) 253-0000", phoneUtil.formatByPattern(US_NUMBER, PhoneNumberFormat.NATIONAL,
699 assertEquals("+1 (650) 253-0000", phoneUtil.formatByPattern(US_NUMBER,
700 PhoneNumberFormat.INTERNATIONAL,
702 assertEquals("tel:+1-650-253-0000", phoneUtil.formatByPattern(US_NUMBER,
703 PhoneNumberFormat.RFC3966,
706 // $NP is set to '1' for the US. Here we check that for other NANPA countries the US rules are
708 newNumFormat.setNationalPrefixFormattingRule("$NP ($FG)");
709 newNumFormat.setFormat("$1 $2-$3");
710 assertEquals("1 (242) 365-1234",
711 phoneUtil.formatByPattern(BS_NUMBER, PhoneNumberFormat.NATIONAL,
713 assertEquals("+1 242 365-1234",
714 phoneUtil.formatByPattern(BS_NUMBER, PhoneNumberFormat.INTERNATIONAL,
717 newNumFormat.setPattern("(\\d{2})(\\d{5})(\\d{3})");
718 newNumFormat.setFormat("$1-$2 $3");
719 newNumberFormats.set(0, newNumFormat);
721 assertEquals("02-36618 300",
722 phoneUtil.formatByPattern(IT_NUMBER, PhoneNumberFormat.NATIONAL,
724 assertEquals("+39 02-36618 300",
725 phoneUtil.formatByPattern(IT_NUMBER, PhoneNumberFormat.INTERNATIONAL,
728 newNumFormat.setNationalPrefixFormattingRule("$NP$FG");
729 newNumFormat.setPattern("(\\d{2})(\\d{4})(\\d{4})");
730 newNumFormat.setFormat("$1 $2 $3");
731 newNumberFormats.set(0, newNumFormat);
732 assertEquals("020 7031 3000",
733 phoneUtil.formatByPattern(GB_NUMBER, PhoneNumberFormat.NATIONAL,
736 newNumFormat.setNationalPrefixFormattingRule("($NP$FG)");
737 assertEquals("(020) 7031 3000",
738 phoneUtil.formatByPattern(GB_NUMBER, PhoneNumberFormat.NATIONAL,
741 newNumFormat.setNationalPrefixFormattingRule("");
742 assertEquals("20 7031 3000",
743 phoneUtil.formatByPattern(GB_NUMBER, PhoneNumberFormat.NATIONAL,
746 assertEquals("+44 20 7031 3000",
747 phoneUtil.formatByPattern(GB_NUMBER, PhoneNumberFormat.INTERNATIONAL,
751 public void testFormatE164Number() {
752 assertEquals("+16502530000", phoneUtil.format(US_NUMBER, PhoneNumberFormat.E164));
753 assertEquals("+4930123456", phoneUtil.format(DE_NUMBER, PhoneNumberFormat.E164));
754 assertEquals("+80012345678", phoneUtil.format(INTERNATIONAL_TOLL_FREE, PhoneNumberFormat.E164));
757 public void testFormatNumberWithExtension() {
758 PhoneNumber nzNumber = new PhoneNumber().mergeFrom(NZ_NUMBER).setExtension("1234");
759 // Uses default extension prefix:
760 assertEquals("03-331 6005 ext. 1234", phoneUtil.format(nzNumber, PhoneNumberFormat.NATIONAL));
761 // Uses RFC 3966 syntax.
762 assertEquals("tel:+64-3-331-6005;ext=1234",
763 phoneUtil.format(nzNumber, PhoneNumberFormat.RFC3966));
764 // Extension prefix overridden in the territory information for the US:
765 PhoneNumber usNumberWithExtension = new PhoneNumber().mergeFrom(US_NUMBER).setExtension("4567");
766 assertEquals("650 253 0000 extn. 4567", phoneUtil.format(usNumberWithExtension,
767 PhoneNumberFormat.NATIONAL));
770 public void testFormatInOriginalFormat() throws Exception {
771 PhoneNumber number1 = phoneUtil.parseAndKeepRawInput("+442087654321", RegionCode.GB);
772 assertEquals("+44 20 8765 4321", phoneUtil.formatInOriginalFormat(number1, RegionCode.GB));
774 PhoneNumber number2 = phoneUtil.parseAndKeepRawInput("02087654321", RegionCode.GB);
775 assertEquals("(020) 8765 4321", phoneUtil.formatInOriginalFormat(number2, RegionCode.GB));
777 PhoneNumber number3 = phoneUtil.parseAndKeepRawInput("011442087654321", RegionCode.US);
778 assertEquals("011 44 20 8765 4321", phoneUtil.formatInOriginalFormat(number3, RegionCode.US));
780 PhoneNumber number4 = phoneUtil.parseAndKeepRawInput("442087654321", RegionCode.GB);
781 assertEquals("44 20 8765 4321", phoneUtil.formatInOriginalFormat(number4, RegionCode.GB));
783 PhoneNumber number5 = phoneUtil.parse("+442087654321", RegionCode.GB);
784 assertEquals("(020) 8765 4321", phoneUtil.formatInOriginalFormat(number5, RegionCode.GB));
786 // Invalid numbers that we have a formatting pattern for should be formatted properly. Note area
787 // codes starting with 7 are intentionally excluded in the test metadata for testing purposes.
788 PhoneNumber number6 = phoneUtil.parseAndKeepRawInput("7345678901", RegionCode.US);
789 assertEquals("734 567 8901", phoneUtil.formatInOriginalFormat(number6, RegionCode.US));
791 // US is not a leading zero country, and the presence of the leading zero leads us to format the
792 // number using raw_input.
793 PhoneNumber number7 = phoneUtil.parseAndKeepRawInput("0734567 8901", RegionCode.US);
794 assertEquals("0734567 8901", phoneUtil.formatInOriginalFormat(number7, RegionCode.US));
796 // This number is valid, but we don't have a formatting pattern for it. Fall back to the raw
798 PhoneNumber number8 = phoneUtil.parseAndKeepRawInput("02-4567-8900", RegionCode.KR);
799 assertEquals("02-4567-8900", phoneUtil.formatInOriginalFormat(number8, RegionCode.KR));
801 PhoneNumber number9 = phoneUtil.parseAndKeepRawInput("01180012345678", RegionCode.US);
802 assertEquals("011 800 1234 5678", phoneUtil.formatInOriginalFormat(number9, RegionCode.US));
804 PhoneNumber number10 = phoneUtil.parseAndKeepRawInput("+80012345678", RegionCode.KR);
805 assertEquals("+800 1234 5678", phoneUtil.formatInOriginalFormat(number10, RegionCode.KR));
807 // US local numbers are formatted correctly, as we have formatting patterns for them.
808 PhoneNumber localNumberUS = phoneUtil.parseAndKeepRawInput("2530000", RegionCode.US);
809 assertEquals("253 0000", phoneUtil.formatInOriginalFormat(localNumberUS, RegionCode.US));
811 PhoneNumber numberWithNationalPrefixUS =
812 phoneUtil.parseAndKeepRawInput("18003456789", RegionCode.US);
813 assertEquals("1 800 345 6789",
814 phoneUtil.formatInOriginalFormat(numberWithNationalPrefixUS, RegionCode.US));
816 PhoneNumber numberWithoutNationalPrefixGB =
817 phoneUtil.parseAndKeepRawInput("2087654321", RegionCode.GB);
818 assertEquals("20 8765 4321",
819 phoneUtil.formatInOriginalFormat(numberWithoutNationalPrefixGB, RegionCode.GB));
820 // Make sure no metadata is modified as a result of the previous function call.
821 assertEquals("(020) 8765 4321", phoneUtil.formatInOriginalFormat(number5, RegionCode.GB));
823 PhoneNumber numberWithNationalPrefixMX =
824 phoneUtil.parseAndKeepRawInput("013312345678", RegionCode.MX);
825 assertEquals("01 33 1234 5678",
826 phoneUtil.formatInOriginalFormat(numberWithNationalPrefixMX, RegionCode.MX));
828 PhoneNumber numberWithoutNationalPrefixMX =
829 phoneUtil.parseAndKeepRawInput("3312345678", RegionCode.MX);
830 assertEquals("33 1234 5678",
831 phoneUtil.formatInOriginalFormat(numberWithoutNationalPrefixMX, RegionCode.MX));
833 PhoneNumber italianFixedLineNumber =
834 phoneUtil.parseAndKeepRawInput("0212345678", RegionCode.IT);
835 assertEquals("02 1234 5678",
836 phoneUtil.formatInOriginalFormat(italianFixedLineNumber, RegionCode.IT));
838 PhoneNumber numberWithNationalPrefixJP =
839 phoneUtil.parseAndKeepRawInput("00777012", RegionCode.JP);
840 assertEquals("0077-7012",
841 phoneUtil.formatInOriginalFormat(numberWithNationalPrefixJP, RegionCode.JP));
843 PhoneNumber numberWithoutNationalPrefixJP =
844 phoneUtil.parseAndKeepRawInput("0777012", RegionCode.JP);
845 assertEquals("0777012",
846 phoneUtil.formatInOriginalFormat(numberWithoutNationalPrefixJP, RegionCode.JP));
848 PhoneNumber numberWithCarrierCodeBR =
849 phoneUtil.parseAndKeepRawInput("012 3121286979", RegionCode.BR);
850 assertEquals("012 3121286979",
851 phoneUtil.formatInOriginalFormat(numberWithCarrierCodeBR, RegionCode.BR));
853 // The default national prefix used in this case is 045. When a number with national prefix 044
854 // is entered, we return the raw input as we don't want to change the number entered.
855 PhoneNumber numberWithNationalPrefixMX1 =
856 phoneUtil.parseAndKeepRawInput("044(33)1234-5678", RegionCode.MX);
857 assertEquals("044(33)1234-5678",
858 phoneUtil.formatInOriginalFormat(numberWithNationalPrefixMX1, RegionCode.MX));
860 PhoneNumber numberWithNationalPrefixMX2 =
861 phoneUtil.parseAndKeepRawInput("045(33)1234-5678", RegionCode.MX);
862 assertEquals("045 33 1234 5678",
863 phoneUtil.formatInOriginalFormat(numberWithNationalPrefixMX2, RegionCode.MX));
865 // The default international prefix used in this case is 0011. When a number with international
866 // prefix 0012 is entered, we return the raw input as we don't want to change the number
868 PhoneNumber outOfCountryNumberFromAU1 =
869 phoneUtil.parseAndKeepRawInput("0012 16502530000", RegionCode.AU);
870 assertEquals("0012 16502530000",
871 phoneUtil.formatInOriginalFormat(outOfCountryNumberFromAU1, RegionCode.AU));
873 PhoneNumber outOfCountryNumberFromAU2 =
874 phoneUtil.parseAndKeepRawInput("0011 16502530000", RegionCode.AU);
875 assertEquals("0011 1 650 253 0000",
876 phoneUtil.formatInOriginalFormat(outOfCountryNumberFromAU2, RegionCode.AU));
878 // Test the star sign is not removed from or added to the original input by this method.
879 PhoneNumber starNumber = phoneUtil.parseAndKeepRawInput("*1234", RegionCode.JP);
880 assertEquals("*1234", phoneUtil.formatInOriginalFormat(starNumber, RegionCode.JP));
881 PhoneNumber numberWithoutStar = phoneUtil.parseAndKeepRawInput("1234", RegionCode.JP);
882 assertEquals("1234", phoneUtil.formatInOriginalFormat(numberWithoutStar, RegionCode.JP));
885 public void testIsPremiumRate() {
886 assertEquals(PhoneNumberUtil.PhoneNumberType.PREMIUM_RATE, phoneUtil.getNumberType(US_PREMIUM));
888 PhoneNumber premiumRateNumber = new PhoneNumber();
889 premiumRateNumber.setCountryCode(39).setNationalNumber(892123L);
890 assertEquals(PhoneNumberUtil.PhoneNumberType.PREMIUM_RATE,
891 phoneUtil.getNumberType(premiumRateNumber));
893 premiumRateNumber.clear();
894 premiumRateNumber.setCountryCode(44).setNationalNumber(9187654321L);
895 assertEquals(PhoneNumberUtil.PhoneNumberType.PREMIUM_RATE,
896 phoneUtil.getNumberType(premiumRateNumber));
898 premiumRateNumber.clear();
899 premiumRateNumber.setCountryCode(49).setNationalNumber(9001654321L);
900 assertEquals(PhoneNumberUtil.PhoneNumberType.PREMIUM_RATE,
901 phoneUtil.getNumberType(premiumRateNumber));
903 premiumRateNumber.clear();
904 premiumRateNumber.setCountryCode(49).setNationalNumber(90091234567L);
905 assertEquals(PhoneNumberUtil.PhoneNumberType.PREMIUM_RATE,
906 phoneUtil.getNumberType(premiumRateNumber));
908 assertEquals(PhoneNumberUtil.PhoneNumberType.PREMIUM_RATE,
909 phoneUtil.getNumberType(UNIVERSAL_PREMIUM_RATE));
912 public void testIsTollFree() {
913 PhoneNumber tollFreeNumber = new PhoneNumber();
915 tollFreeNumber.setCountryCode(1).setNationalNumber(8881234567L);
916 assertEquals(PhoneNumberUtil.PhoneNumberType.TOLL_FREE,
917 phoneUtil.getNumberType(tollFreeNumber));
919 tollFreeNumber.clear();
920 tollFreeNumber.setCountryCode(39).setNationalNumber(803123L);
921 assertEquals(PhoneNumberUtil.PhoneNumberType.TOLL_FREE,
922 phoneUtil.getNumberType(tollFreeNumber));
924 tollFreeNumber.clear();
925 tollFreeNumber.setCountryCode(44).setNationalNumber(8012345678L);
926 assertEquals(PhoneNumberUtil.PhoneNumberType.TOLL_FREE,
927 phoneUtil.getNumberType(tollFreeNumber));
929 tollFreeNumber.clear();
930 tollFreeNumber.setCountryCode(49).setNationalNumber(8001234567L);
931 assertEquals(PhoneNumberUtil.PhoneNumberType.TOLL_FREE,
932 phoneUtil.getNumberType(tollFreeNumber));
934 assertEquals(PhoneNumberUtil.PhoneNumberType.TOLL_FREE,
935 phoneUtil.getNumberType(INTERNATIONAL_TOLL_FREE));
938 public void testIsMobile() {
939 assertEquals(PhoneNumberUtil.PhoneNumberType.MOBILE, phoneUtil.getNumberType(BS_MOBILE));
940 assertEquals(PhoneNumberUtil.PhoneNumberType.MOBILE, phoneUtil.getNumberType(GB_MOBILE));
941 assertEquals(PhoneNumberUtil.PhoneNumberType.MOBILE, phoneUtil.getNumberType(IT_MOBILE));
942 assertEquals(PhoneNumberUtil.PhoneNumberType.MOBILE, phoneUtil.getNumberType(AR_MOBILE));
944 PhoneNumber mobileNumber = new PhoneNumber();
945 mobileNumber.setCountryCode(49).setNationalNumber(15123456789L);
946 assertEquals(PhoneNumberUtil.PhoneNumberType.MOBILE, phoneUtil.getNumberType(mobileNumber));
949 public void testIsFixedLine() {
950 assertEquals(PhoneNumberUtil.PhoneNumberType.FIXED_LINE, phoneUtil.getNumberType(BS_NUMBER));
951 assertEquals(PhoneNumberUtil.PhoneNumberType.FIXED_LINE, phoneUtil.getNumberType(IT_NUMBER));
952 assertEquals(PhoneNumberUtil.PhoneNumberType.FIXED_LINE, phoneUtil.getNumberType(GB_NUMBER));
953 assertEquals(PhoneNumberUtil.PhoneNumberType.FIXED_LINE, phoneUtil.getNumberType(DE_NUMBER));
956 public void testIsFixedLineAndMobile() {
957 assertEquals(PhoneNumberUtil.PhoneNumberType.FIXED_LINE_OR_MOBILE,
958 phoneUtil.getNumberType(US_NUMBER));
960 PhoneNumber fixedLineAndMobileNumber = new PhoneNumber().
961 setCountryCode(54).setNationalNumber(1987654321L);
962 assertEquals(PhoneNumberUtil.PhoneNumberType.FIXED_LINE_OR_MOBILE,
963 phoneUtil.getNumberType(fixedLineAndMobileNumber));
966 public void testIsSharedCost() {
967 PhoneNumber gbNumber = new PhoneNumber();
968 gbNumber.setCountryCode(44).setNationalNumber(8431231234L);
969 assertEquals(PhoneNumberUtil.PhoneNumberType.SHARED_COST, phoneUtil.getNumberType(gbNumber));
972 public void testIsVoip() {
973 PhoneNumber gbNumber = new PhoneNumber();
974 gbNumber.setCountryCode(44).setNationalNumber(5631231234L);
975 assertEquals(PhoneNumberUtil.PhoneNumberType.VOIP, phoneUtil.getNumberType(gbNumber));
978 public void testIsPersonalNumber() {
979 PhoneNumber gbNumber = new PhoneNumber();
980 gbNumber.setCountryCode(44).setNationalNumber(7031231234L);
981 assertEquals(PhoneNumberUtil.PhoneNumberType.PERSONAL_NUMBER,
982 phoneUtil.getNumberType(gbNumber));
985 public void testIsUnknown() {
986 // Invalid numbers should be of type UNKNOWN.
987 assertEquals(PhoneNumberUtil.PhoneNumberType.UNKNOWN, phoneUtil.getNumberType(US_LOCAL_NUMBER));
990 public void testIsValidNumber() {
991 assertTrue(phoneUtil.isValidNumber(US_NUMBER));
992 assertTrue(phoneUtil.isValidNumber(IT_NUMBER));
993 assertTrue(phoneUtil.isValidNumber(GB_MOBILE));
994 assertTrue(phoneUtil.isValidNumber(INTERNATIONAL_TOLL_FREE));
995 assertTrue(phoneUtil.isValidNumber(UNIVERSAL_PREMIUM_RATE));
997 PhoneNumber nzNumber = new PhoneNumber().setCountryCode(64).setNationalNumber(21387835L);
998 assertTrue(phoneUtil.isValidNumber(nzNumber));
1001 public void testIsValidForRegion() {
1002 // This number is valid for the Bahamas, but is not a valid US number.
1003 assertTrue(phoneUtil.isValidNumber(BS_NUMBER));
1004 assertTrue(phoneUtil.isValidNumberForRegion(BS_NUMBER, RegionCode.BS));
1005 assertFalse(phoneUtil.isValidNumberForRegion(BS_NUMBER, RegionCode.US));
1006 PhoneNumber bsInvalidNumber =
1007 new PhoneNumber().setCountryCode(1).setNationalNumber(2421232345L);
1008 // This number is no longer valid.
1009 assertFalse(phoneUtil.isValidNumber(bsInvalidNumber));
1011 // La Mayotte and Reunion use 'leadingDigits' to differentiate them.
1012 PhoneNumber reNumber = new PhoneNumber();
1013 reNumber.setCountryCode(262).setNationalNumber(262123456L);
1014 assertTrue(phoneUtil.isValidNumber(reNumber));
1015 assertTrue(phoneUtil.isValidNumberForRegion(reNumber, RegionCode.RE));
1016 assertFalse(phoneUtil.isValidNumberForRegion(reNumber, RegionCode.YT));
1017 // Now change the number to be a number for La Mayotte.
1018 reNumber.setNationalNumber(269601234L);
1019 assertTrue(phoneUtil.isValidNumberForRegion(reNumber, RegionCode.YT));
1020 assertFalse(phoneUtil.isValidNumberForRegion(reNumber, RegionCode.RE));
1021 // This number is no longer valid for La Reunion.
1022 reNumber.setNationalNumber(269123456L);
1023 assertFalse(phoneUtil.isValidNumberForRegion(reNumber, RegionCode.YT));
1024 assertFalse(phoneUtil.isValidNumberForRegion(reNumber, RegionCode.RE));
1025 assertFalse(phoneUtil.isValidNumber(reNumber));
1026 // However, it should be recognised as from La Mayotte, since it is valid for this region.
1027 assertEquals(RegionCode.YT, phoneUtil.getRegionCodeForNumber(reNumber));
1028 // This number is valid in both places.
1029 reNumber.setNationalNumber(800123456L);
1030 assertTrue(phoneUtil.isValidNumberForRegion(reNumber, RegionCode.YT));
1031 assertTrue(phoneUtil.isValidNumberForRegion(reNumber, RegionCode.RE));
1032 assertTrue(phoneUtil.isValidNumberForRegion(INTERNATIONAL_TOLL_FREE, RegionCode.UN001));
1033 assertFalse(phoneUtil.isValidNumberForRegion(INTERNATIONAL_TOLL_FREE, RegionCode.US));
1034 assertFalse(phoneUtil.isValidNumberForRegion(INTERNATIONAL_TOLL_FREE, RegionCode.ZZ));
1036 PhoneNumber invalidNumber = new PhoneNumber();
1037 // Invalid country calling codes.
1038 invalidNumber.setCountryCode(3923).setNationalNumber(2366L);
1039 assertFalse(phoneUtil.isValidNumberForRegion(invalidNumber, RegionCode.ZZ));
1040 assertFalse(phoneUtil.isValidNumberForRegion(invalidNumber, RegionCode.UN001));
1041 invalidNumber.setCountryCode(0);
1042 assertFalse(phoneUtil.isValidNumberForRegion(invalidNumber, RegionCode.UN001));
1043 assertFalse(phoneUtil.isValidNumberForRegion(invalidNumber, RegionCode.ZZ));
1046 public void testIsNotValidNumber() {
1047 assertFalse(phoneUtil.isValidNumber(US_LOCAL_NUMBER));
1049 PhoneNumber invalidNumber = new PhoneNumber();
1050 invalidNumber.setCountryCode(39).setNationalNumber(23661830000L).setItalianLeadingZero(true);
1051 assertFalse(phoneUtil.isValidNumber(invalidNumber));
1053 invalidNumber.clear();
1054 invalidNumber.setCountryCode(44).setNationalNumber(791234567L);
1055 assertFalse(phoneUtil.isValidNumber(invalidNumber));
1057 invalidNumber.clear();
1058 invalidNumber.setCountryCode(49).setNationalNumber(1234L);
1059 assertFalse(phoneUtil.isValidNumber(invalidNumber));
1061 invalidNumber.clear();
1062 invalidNumber.setCountryCode(64).setNationalNumber(3316005L);
1063 assertFalse(phoneUtil.isValidNumber(invalidNumber));
1065 invalidNumber.clear();
1066 // Invalid country calling codes.
1067 invalidNumber.setCountryCode(3923).setNationalNumber(2366L);
1068 assertFalse(phoneUtil.isValidNumber(invalidNumber));
1069 invalidNumber.setCountryCode(0);
1070 assertFalse(phoneUtil.isValidNumber(invalidNumber));
1072 assertFalse(phoneUtil.isValidNumber(INTERNATIONAL_TOLL_FREE_TOO_LONG));
1075 public void testGetRegionCodeForCountryCode() {
1076 assertEquals(RegionCode.US, phoneUtil.getRegionCodeForCountryCode(1));
1077 assertEquals(RegionCode.GB, phoneUtil.getRegionCodeForCountryCode(44));
1078 assertEquals(RegionCode.DE, phoneUtil.getRegionCodeForCountryCode(49));
1079 assertEquals(RegionCode.UN001, phoneUtil.getRegionCodeForCountryCode(800));
1080 assertEquals(RegionCode.UN001, phoneUtil.getRegionCodeForCountryCode(979));
1083 public void testGetRegionCodeForNumber() {
1084 assertEquals(RegionCode.BS, phoneUtil.getRegionCodeForNumber(BS_NUMBER));
1085 assertEquals(RegionCode.US, phoneUtil.getRegionCodeForNumber(US_NUMBER));
1086 assertEquals(RegionCode.GB, phoneUtil.getRegionCodeForNumber(GB_MOBILE));
1087 assertEquals(RegionCode.UN001, phoneUtil.getRegionCodeForNumber(INTERNATIONAL_TOLL_FREE));
1088 assertEquals(RegionCode.UN001, phoneUtil.getRegionCodeForNumber(UNIVERSAL_PREMIUM_RATE));
1091 public void testGetCountryCodeForRegion() {
1092 assertEquals(1, phoneUtil.getCountryCodeForRegion(RegionCode.US));
1093 assertEquals(64, phoneUtil.getCountryCodeForRegion(RegionCode.NZ));
1094 assertEquals(0, phoneUtil.getCountryCodeForRegion(null));
1095 assertEquals(0, phoneUtil.getCountryCodeForRegion(RegionCode.ZZ));
1096 assertEquals(0, phoneUtil.getCountryCodeForRegion(RegionCode.UN001));
1097 // CS is already deprecated so the library doesn't support it.
1098 assertEquals(0, phoneUtil.getCountryCodeForRegion(RegionCode.CS));
1101 public void testGetNationalDiallingPrefixForRegion() {
1102 assertEquals("1", phoneUtil.getNddPrefixForRegion(RegionCode.US, false));
1103 // Test non-main country to see it gets the national dialling prefix for the main country with
1104 // that country calling code.
1105 assertEquals("1", phoneUtil.getNddPrefixForRegion(RegionCode.BS, false));
1106 assertEquals("0", phoneUtil.getNddPrefixForRegion(RegionCode.NZ, false));
1107 // Test case with non digit in the national prefix.
1108 assertEquals("0~0", phoneUtil.getNddPrefixForRegion(RegionCode.AO, false));
1109 assertEquals("00", phoneUtil.getNddPrefixForRegion(RegionCode.AO, true));
1110 // Test cases with invalid regions.
1111 assertEquals(null, phoneUtil.getNddPrefixForRegion(null, false));
1112 assertEquals(null, phoneUtil.getNddPrefixForRegion(RegionCode.ZZ, false));
1113 assertEquals(null, phoneUtil.getNddPrefixForRegion(RegionCode.UN001, false));
1114 // CS is already deprecated so the library doesn't support it.
1115 assertEquals(null, phoneUtil.getNddPrefixForRegion(RegionCode.CS, false));
1118 public void testIsNANPACountry() {
1119 assertTrue(phoneUtil.isNANPACountry(RegionCode.US));
1120 assertTrue(phoneUtil.isNANPACountry(RegionCode.BS));
1121 assertFalse(phoneUtil.isNANPACountry(RegionCode.DE));
1122 assertFalse(phoneUtil.isNANPACountry(RegionCode.ZZ));
1123 assertFalse(phoneUtil.isNANPACountry(RegionCode.UN001));
1124 assertFalse(phoneUtil.isNANPACountry(null));
1127 public void testIsPossibleNumber() {
1128 assertTrue(phoneUtil.isPossibleNumber(US_NUMBER));
1129 assertTrue(phoneUtil.isPossibleNumber(US_LOCAL_NUMBER));
1130 assertTrue(phoneUtil.isPossibleNumber(GB_NUMBER));
1131 assertTrue(phoneUtil.isPossibleNumber(INTERNATIONAL_TOLL_FREE));
1133 assertTrue(phoneUtil.isPossibleNumber("+1 650 253 0000", RegionCode.US));
1134 assertTrue(phoneUtil.isPossibleNumber("+1 650 GOO OGLE", RegionCode.US));
1135 assertTrue(phoneUtil.isPossibleNumber("(650) 253-0000", RegionCode.US));
1136 assertTrue(phoneUtil.isPossibleNumber("253-0000", RegionCode.US));
1137 assertTrue(phoneUtil.isPossibleNumber("+1 650 253 0000", RegionCode.GB));
1138 assertTrue(phoneUtil.isPossibleNumber("+44 20 7031 3000", RegionCode.GB));
1139 assertTrue(phoneUtil.isPossibleNumber("(020) 7031 3000", RegionCode.GB));
1140 assertTrue(phoneUtil.isPossibleNumber("7031 3000", RegionCode.GB));
1141 assertTrue(phoneUtil.isPossibleNumber("3331 6005", RegionCode.NZ));
1142 assertTrue(phoneUtil.isPossibleNumber("+800 1234 5678", RegionCode.UN001));
1145 public void testIsPossibleNumberWithReason() {
1146 // National numbers for country calling code +1 that are within 7 to 10 digits are possible.
1147 assertEquals(PhoneNumberUtil.ValidationResult.IS_POSSIBLE,
1148 phoneUtil.isPossibleNumberWithReason(US_NUMBER));
1150 assertEquals(PhoneNumberUtil.ValidationResult.IS_POSSIBLE,
1151 phoneUtil.isPossibleNumberWithReason(US_LOCAL_NUMBER));
1153 assertEquals(PhoneNumberUtil.ValidationResult.TOO_LONG,
1154 phoneUtil.isPossibleNumberWithReason(US_LONG_NUMBER));
1156 PhoneNumber number = new PhoneNumber();
1157 number.setCountryCode(0).setNationalNumber(2530000L);
1158 assertEquals(PhoneNumberUtil.ValidationResult.INVALID_COUNTRY_CODE,
1159 phoneUtil.isPossibleNumberWithReason(number));
1162 number.setCountryCode(1).setNationalNumber(253000L);
1163 assertEquals(PhoneNumberUtil.ValidationResult.TOO_SHORT,
1164 phoneUtil.isPossibleNumberWithReason(number));
1167 number.setCountryCode(65).setNationalNumber(1234567890L);
1168 assertEquals(PhoneNumberUtil.ValidationResult.IS_POSSIBLE,
1169 phoneUtil.isPossibleNumberWithReason(number));
1171 assertEquals(PhoneNumberUtil.ValidationResult.TOO_LONG,
1172 phoneUtil.isPossibleNumberWithReason(INTERNATIONAL_TOLL_FREE_TOO_LONG));
1174 // Try with number that we don't have metadata for.
1175 PhoneNumber adNumber = new PhoneNumber();
1176 adNumber.setCountryCode(376).setNationalNumber(12345L);
1177 assertEquals(PhoneNumberUtil.ValidationResult.IS_POSSIBLE,
1178 phoneUtil.isPossibleNumberWithReason(adNumber));
1179 adNumber.setCountryCode(376).setNationalNumber(1L);
1180 assertEquals(PhoneNumberUtil.ValidationResult.TOO_SHORT,
1181 phoneUtil.isPossibleNumberWithReason(adNumber));
1182 adNumber.setCountryCode(376).setNationalNumber(12345678901234567L);
1183 assertEquals(PhoneNumberUtil.ValidationResult.TOO_LONG,
1184 phoneUtil.isPossibleNumberWithReason(adNumber));
1187 public void testIsNotPossibleNumber() {
1188 assertFalse(phoneUtil.isPossibleNumber(US_LONG_NUMBER));
1189 assertFalse(phoneUtil.isPossibleNumber(INTERNATIONAL_TOLL_FREE_TOO_LONG));
1191 PhoneNumber number = new PhoneNumber();
1192 number.setCountryCode(1).setNationalNumber(253000L);
1193 assertFalse(phoneUtil.isPossibleNumber(number));
1196 number.setCountryCode(44).setNationalNumber(300L);
1197 assertFalse(phoneUtil.isPossibleNumber(number));
1198 assertFalse(phoneUtil.isPossibleNumber("+1 650 253 00000", RegionCode.US));
1199 assertFalse(phoneUtil.isPossibleNumber("(650) 253-00000", RegionCode.US));
1200 assertFalse(phoneUtil.isPossibleNumber("I want a Pizza", RegionCode.US));
1201 assertFalse(phoneUtil.isPossibleNumber("253-000", RegionCode.US));
1202 assertFalse(phoneUtil.isPossibleNumber("1 3000", RegionCode.GB));
1203 assertFalse(phoneUtil.isPossibleNumber("+44 300", RegionCode.GB));
1204 assertFalse(phoneUtil.isPossibleNumber("+800 1234 5678 9", RegionCode.UN001));
1207 public void testTruncateTooLongNumber() {
1208 // GB number 080 1234 5678, but entered with 4 extra digits at the end.
1209 PhoneNumber tooLongNumber = new PhoneNumber();
1210 tooLongNumber.setCountryCode(44).setNationalNumber(80123456780123L);
1211 PhoneNumber validNumber = new PhoneNumber();
1212 validNumber.setCountryCode(44).setNationalNumber(8012345678L);
1213 assertTrue(phoneUtil.truncateTooLongNumber(tooLongNumber));
1214 assertEquals(validNumber, tooLongNumber);
1216 // IT number 022 3456 7890, but entered with 3 extra digits at the end.
1217 tooLongNumber.clear();
1218 tooLongNumber.setCountryCode(39).setNationalNumber(2234567890123L).setItalianLeadingZero(true);
1219 validNumber.clear();
1220 validNumber.setCountryCode(39).setNationalNumber(2234567890L).setItalianLeadingZero(true);
1221 assertTrue(phoneUtil.truncateTooLongNumber(tooLongNumber));
1222 assertEquals(validNumber, tooLongNumber);
1224 // US number 650-253-0000, but entered with one additional digit at the end.
1225 tooLongNumber.clear();
1226 tooLongNumber.mergeFrom(US_LONG_NUMBER);
1227 assertTrue(phoneUtil.truncateTooLongNumber(tooLongNumber));
1228 assertEquals(US_NUMBER, tooLongNumber);
1230 tooLongNumber.clear();
1231 tooLongNumber.mergeFrom(INTERNATIONAL_TOLL_FREE_TOO_LONG);
1232 assertTrue(phoneUtil.truncateTooLongNumber(tooLongNumber));
1233 assertEquals(INTERNATIONAL_TOLL_FREE, tooLongNumber);
1235 // Tests what happens when a valid number is passed in.
1236 PhoneNumber validNumberCopy = new PhoneNumber().mergeFrom(validNumber);
1237 assertTrue(phoneUtil.truncateTooLongNumber(validNumber));
1238 // Tests the number is not modified.
1239 assertEquals(validNumberCopy, validNumber);
1241 // Tests what happens when a number with invalid prefix is passed in.
1242 PhoneNumber numberWithInvalidPrefix = new PhoneNumber();
1243 // The test metadata says US numbers cannot have prefix 240.
1244 numberWithInvalidPrefix.setCountryCode(1).setNationalNumber(2401234567L);
1245 PhoneNumber invalidNumberCopy = new PhoneNumber().mergeFrom(numberWithInvalidPrefix);
1246 assertFalse(phoneUtil.truncateTooLongNumber(numberWithInvalidPrefix));
1247 // Tests the number is not modified.
1248 assertEquals(invalidNumberCopy, numberWithInvalidPrefix);
1250 // Tests what happens when a too short number is passed in.
1251 PhoneNumber tooShortNumber = new PhoneNumber().setCountryCode(1).setNationalNumber(1234L);
1252 PhoneNumber tooShortNumberCopy = new PhoneNumber().mergeFrom(tooShortNumber);
1253 assertFalse(phoneUtil.truncateTooLongNumber(tooShortNumber));
1254 // Tests the number is not modified.
1255 assertEquals(tooShortNumberCopy, tooShortNumber);
1258 public void testIsViablePhoneNumber() {
1259 assertFalse(PhoneNumberUtil.isViablePhoneNumber("1"));
1260 // Only one or two digits before strange non-possible punctuation.
1261 assertFalse(PhoneNumberUtil.isViablePhoneNumber("1+1+1"));
1262 assertFalse(PhoneNumberUtil.isViablePhoneNumber("80+0"));
1263 // Two digits is viable.
1264 assertTrue(PhoneNumberUtil.isViablePhoneNumber("00"));
1265 assertTrue(PhoneNumberUtil.isViablePhoneNumber("111"));
1267 assertTrue(PhoneNumberUtil.isViablePhoneNumber("0800-4-pizza"));
1268 assertTrue(PhoneNumberUtil.isViablePhoneNumber("0800-4-PIZZA"));
1269 // We need at least three digits before any alpha characters.
1270 assertFalse(PhoneNumberUtil.isViablePhoneNumber("08-PIZZA"));
1271 assertFalse(PhoneNumberUtil.isViablePhoneNumber("8-PIZZA"));
1272 assertFalse(PhoneNumberUtil.isViablePhoneNumber("12. March"));
1275 public void testIsViablePhoneNumberNonAscii() {
1276 // Only one or two digits before possible punctuation followed by more digits.
1277 assertTrue(PhoneNumberUtil.isViablePhoneNumber("1\u300034"));
1278 assertFalse(PhoneNumberUtil.isViablePhoneNumber("1\u30003+4"));
1279 // Unicode variants of possible starting character and other allowed punctuation/digits.
1280 assertTrue(PhoneNumberUtil.isViablePhoneNumber("\uFF081\uFF09\u30003456789"));
1281 // Testing a leading + is okay.
1282 assertTrue(PhoneNumberUtil.isViablePhoneNumber("+1\uFF09\u30003456789"));
1285 public void testExtractPossibleNumber() {
1286 // Removes preceding funky punctuation and letters but leaves the rest untouched.
1287 assertEquals("0800-345-600", PhoneNumberUtil.extractPossibleNumber("Tel:0800-345-600"));
1288 assertEquals("0800 FOR PIZZA", PhoneNumberUtil.extractPossibleNumber("Tel:0800 FOR PIZZA"));
1289 // Should not remove plus sign
1290 assertEquals("+800-345-600", PhoneNumberUtil.extractPossibleNumber("Tel:+800-345-600"));
1291 // Should recognise wide digits as possible start values.
1292 assertEquals("\uFF10\uFF12\uFF13",
1293 PhoneNumberUtil.extractPossibleNumber("\uFF10\uFF12\uFF13"));
1294 // Dashes are not possible start values and should be removed.
1295 assertEquals("\uFF11\uFF12\uFF13",
1296 PhoneNumberUtil.extractPossibleNumber("Num-\uFF11\uFF12\uFF13"));
1297 // If not possible number present, return empty string.
1298 assertEquals("", PhoneNumberUtil.extractPossibleNumber("Num-...."));
1299 // Leading brackets are stripped - these are not used when parsing.
1300 assertEquals("650) 253-0000", PhoneNumberUtil.extractPossibleNumber("(650) 253-0000"));
1302 // Trailing non-alpha-numeric characters should be removed.
1303 assertEquals("650) 253-0000", PhoneNumberUtil.extractPossibleNumber("(650) 253-0000..- .."));
1304 assertEquals("650) 253-0000", PhoneNumberUtil.extractPossibleNumber("(650) 253-0000."));
1305 // This case has a trailing RTL char.
1306 assertEquals("650) 253-0000", PhoneNumberUtil.extractPossibleNumber("(650) 253-0000\u200F"));
1309 public void testMaybeStripNationalPrefix() {
1310 PhoneMetadata metadata = new PhoneMetadata();
1311 metadata.setNationalPrefixForParsing("34");
1312 metadata.setGeneralDesc(new PhoneNumberDesc().setNationalNumberPattern("\\d{4,8}"));
1313 StringBuilder numberToStrip = new StringBuilder("34356778");
1314 String strippedNumber = "356778";
1315 assertTrue(phoneUtil.maybeStripNationalPrefixAndCarrierCode(numberToStrip, metadata, null));
1316 assertEquals("Should have had national prefix stripped.",
1317 strippedNumber, numberToStrip.toString());
1318 // Retry stripping - now the number should not start with the national prefix, so no more
1319 // stripping should occur.
1320 assertFalse(phoneUtil.maybeStripNationalPrefixAndCarrierCode(numberToStrip, metadata, null));
1321 assertEquals("Should have had no change - no national prefix present.",
1322 strippedNumber, numberToStrip.toString());
1323 // Some countries have no national prefix. Repeat test with none specified.
1324 metadata.setNationalPrefixForParsing("");
1325 assertFalse(phoneUtil.maybeStripNationalPrefixAndCarrierCode(numberToStrip, metadata, null));
1326 assertEquals("Should not strip anything with empty national prefix.",
1327 strippedNumber, numberToStrip.toString());
1328 // If the resultant number doesn't match the national rule, it shouldn't be stripped.
1329 metadata.setNationalPrefixForParsing("3");
1330 numberToStrip = new StringBuilder("3123");
1331 strippedNumber = "3123";
1332 assertFalse(phoneUtil.maybeStripNationalPrefixAndCarrierCode(numberToStrip, metadata, null));
1333 assertEquals("Should have had no change - after stripping, it wouldn't have matched " +
1334 "the national rule.",
1335 strippedNumber, numberToStrip.toString());
1336 // Test extracting carrier selection code.
1337 metadata.setNationalPrefixForParsing("0(81)?");
1338 numberToStrip = new StringBuilder("08122123456");
1339 strippedNumber = "22123456";
1340 StringBuilder carrierCode = new StringBuilder();
1341 assertTrue(phoneUtil.maybeStripNationalPrefixAndCarrierCode(
1342 numberToStrip, metadata, carrierCode));
1343 assertEquals("81", carrierCode.toString());
1344 assertEquals("Should have had national prefix and carrier code stripped.",
1345 strippedNumber, numberToStrip.toString());
1346 // If there was a transform rule, check it was applied.
1347 metadata.setNationalPrefixTransformRule("5$15");
1348 // Note that a capturing group is present here.
1349 metadata.setNationalPrefixForParsing("0(\\d{2})");
1350 numberToStrip = new StringBuilder("031123");
1351 String transformedNumber = "5315123";
1352 assertTrue(phoneUtil.maybeStripNationalPrefixAndCarrierCode(numberToStrip, metadata, null));
1353 assertEquals("Should transform the 031 to a 5315.",
1354 transformedNumber, numberToStrip.toString());
1357 public void testMaybeStripInternationalPrefix() {
1358 String internationalPrefix = "00[39]";
1359 StringBuilder numberToStrip = new StringBuilder("0034567700-3898003");
1360 // Note the dash is removed as part of the normalization.
1361 StringBuilder strippedNumber = new StringBuilder("45677003898003");
1362 assertEquals(CountryCodeSource.FROM_NUMBER_WITH_IDD,
1363 phoneUtil.maybeStripInternationalPrefixAndNormalize(numberToStrip,
1364 internationalPrefix));
1365 assertEquals("The number supplied was not stripped of its international prefix.",
1366 strippedNumber.toString(), numberToStrip.toString());
1367 // Now the number no longer starts with an IDD prefix, so it should now report
1368 // FROM_DEFAULT_COUNTRY.
1369 assertEquals(CountryCodeSource.FROM_DEFAULT_COUNTRY,
1370 phoneUtil.maybeStripInternationalPrefixAndNormalize(numberToStrip,
1371 internationalPrefix));
1373 numberToStrip = new StringBuilder("00945677003898003");
1374 assertEquals(CountryCodeSource.FROM_NUMBER_WITH_IDD,
1375 phoneUtil.maybeStripInternationalPrefixAndNormalize(numberToStrip,
1376 internationalPrefix));
1377 assertEquals("The number supplied was not stripped of its international prefix.",
1378 strippedNumber.toString(), numberToStrip.toString());
1379 // Test it works when the international prefix is broken up by spaces.
1380 numberToStrip = new StringBuilder("00 9 45677003898003");
1381 assertEquals(CountryCodeSource.FROM_NUMBER_WITH_IDD,
1382 phoneUtil.maybeStripInternationalPrefixAndNormalize(numberToStrip,
1383 internationalPrefix));
1384 assertEquals("The number supplied was not stripped of its international prefix.",
1385 strippedNumber.toString(), numberToStrip.toString());
1386 // Now the number no longer starts with an IDD prefix, so it should now report
1387 // FROM_DEFAULT_COUNTRY.
1388 assertEquals(CountryCodeSource.FROM_DEFAULT_COUNTRY,
1389 phoneUtil.maybeStripInternationalPrefixAndNormalize(numberToStrip,
1390 internationalPrefix));
1392 // Test the + symbol is also recognised and stripped.
1393 numberToStrip = new StringBuilder("+45677003898003");
1394 strippedNumber = new StringBuilder("45677003898003");
1395 assertEquals(CountryCodeSource.FROM_NUMBER_WITH_PLUS_SIGN,
1396 phoneUtil.maybeStripInternationalPrefixAndNormalize(numberToStrip,
1397 internationalPrefix));
1398 assertEquals("The number supplied was not stripped of the plus symbol.",
1399 strippedNumber.toString(), numberToStrip.toString());
1401 // If the number afterwards is a zero, we should not strip this - no country calling code begins
1403 numberToStrip = new StringBuilder("0090112-3123");
1404 strippedNumber = new StringBuilder("00901123123");
1405 assertEquals(CountryCodeSource.FROM_DEFAULT_COUNTRY,
1406 phoneUtil.maybeStripInternationalPrefixAndNormalize(numberToStrip,
1407 internationalPrefix));
1408 assertEquals("The number supplied had a 0 after the match so shouldn't be stripped.",
1409 strippedNumber.toString(), numberToStrip.toString());
1410 // Here the 0 is separated by a space from the IDD.
1411 numberToStrip = new StringBuilder("009 0-112-3123");
1412 assertEquals(CountryCodeSource.FROM_DEFAULT_COUNTRY,
1413 phoneUtil.maybeStripInternationalPrefixAndNormalize(numberToStrip,
1414 internationalPrefix));
1417 public void testMaybeExtractCountryCode() {
1418 PhoneNumber number = new PhoneNumber();
1419 PhoneMetadata metadata = phoneUtil.getMetadataForRegion(RegionCode.US);
1420 // Note that for the US, the IDD is 011.
1422 String phoneNumber = "011112-3456789";
1423 String strippedNumber = "123456789";
1424 int countryCallingCode = 1;
1425 StringBuilder numberToFill = new StringBuilder();
1426 assertEquals("Did not extract country calling code " + countryCallingCode + " correctly.",
1428 phoneUtil.maybeExtractCountryCode(phoneNumber, metadata, numberToFill, true,
1430 assertEquals("Did not figure out CountryCodeSource correctly",
1431 CountryCodeSource.FROM_NUMBER_WITH_IDD, number.getCountryCodeSource());
1432 // Should strip and normalize national significant number.
1433 assertEquals("Did not strip off the country calling code correctly.",
1435 numberToFill.toString());
1436 } catch (NumberParseException e) {
1437 fail("Should not have thrown an exception: " + e.toString());
1441 String phoneNumber = "+6423456789";
1442 int countryCallingCode = 64;
1443 StringBuilder numberToFill = new StringBuilder();
1444 assertEquals("Did not extract country calling code " + countryCallingCode + " correctly.",
1446 phoneUtil.maybeExtractCountryCode(phoneNumber, metadata, numberToFill, true,
1448 assertEquals("Did not figure out CountryCodeSource correctly",
1449 CountryCodeSource.FROM_NUMBER_WITH_PLUS_SIGN, number.getCountryCodeSource());
1450 } catch (NumberParseException e) {
1451 fail("Should not have thrown an exception: " + e.toString());
1455 String phoneNumber = "+80012345678";
1456 int countryCallingCode = 800;
1457 StringBuilder numberToFill = new StringBuilder();
1458 assertEquals("Did not extract country calling code " + countryCallingCode + " correctly.",
1460 phoneUtil.maybeExtractCountryCode(phoneNumber, metadata, numberToFill, true,
1462 assertEquals("Did not figure out CountryCodeSource correctly",
1463 CountryCodeSource.FROM_NUMBER_WITH_PLUS_SIGN, number.getCountryCodeSource());
1464 } catch (NumberParseException e) {
1465 fail("Should not have thrown an exception: " + e.toString());
1469 String phoneNumber = "2345-6789";
1470 StringBuilder numberToFill = new StringBuilder();
1472 "Should not have extracted a country calling code - no international prefix present.",
1474 phoneUtil.maybeExtractCountryCode(phoneNumber, metadata, numberToFill, true, number));
1475 assertEquals("Did not figure out CountryCodeSource correctly",
1476 CountryCodeSource.FROM_DEFAULT_COUNTRY, number.getCountryCodeSource());
1477 } catch (NumberParseException e) {
1478 fail("Should not have thrown an exception: " + e.toString());
1482 String phoneNumber = "0119991123456789";
1483 StringBuilder numberToFill = new StringBuilder();
1484 phoneUtil.maybeExtractCountryCode(phoneNumber, metadata, numberToFill, true, number);
1485 fail("Should have thrown an exception, no valid country calling code present.");
1486 } catch (NumberParseException e) {
1488 assertEquals("Wrong error type stored in exception.",
1489 NumberParseException.ErrorType.INVALID_COUNTRY_CODE,
1494 String phoneNumber = "(1 610) 619 4466";
1495 int countryCallingCode = 1;
1496 StringBuilder numberToFill = new StringBuilder();
1497 assertEquals("Should have extracted the country calling code of the region passed in",
1499 phoneUtil.maybeExtractCountryCode(phoneNumber, metadata, numberToFill, true,
1501 assertEquals("Did not figure out CountryCodeSource correctly",
1502 CountryCodeSource.FROM_NUMBER_WITHOUT_PLUS_SIGN,
1503 number.getCountryCodeSource());
1504 } catch (NumberParseException e) {
1505 fail("Should not have thrown an exception: " + e.toString());
1509 String phoneNumber = "(1 610) 619 4466";
1510 int countryCallingCode = 1;
1511 StringBuilder numberToFill = new StringBuilder();
1512 assertEquals("Should have extracted the country calling code of the region passed in",
1514 phoneUtil.maybeExtractCountryCode(phoneNumber, metadata, numberToFill, false,
1516 assertFalse("Should not contain CountryCodeSource.", number.hasCountryCodeSource());
1517 } catch (NumberParseException e) {
1518 fail("Should not have thrown an exception: " + e.toString());
1522 String phoneNumber = "(1 610) 619 446";
1523 StringBuilder numberToFill = new StringBuilder();
1524 assertEquals("Should not have extracted a country calling code - invalid number after " +
1525 "extraction of uncertain country calling code.",
1527 phoneUtil.maybeExtractCountryCode(phoneNumber, metadata, numberToFill, false,
1529 assertFalse("Should not contain CountryCodeSource.", number.hasCountryCodeSource());
1530 } catch (NumberParseException e) {
1531 fail("Should not have thrown an exception: " + e.toString());
1535 String phoneNumber = "(1 610) 619";
1536 StringBuilder numberToFill = new StringBuilder();
1537 assertEquals("Should not have extracted a country calling code - too short number both " +
1538 "before and after extraction of uncertain country calling code.",
1540 phoneUtil.maybeExtractCountryCode(phoneNumber, metadata, numberToFill, true,
1542 assertEquals("Did not figure out CountryCodeSource correctly",
1543 CountryCodeSource.FROM_DEFAULT_COUNTRY, number.getCountryCodeSource());
1544 } catch (NumberParseException e) {
1545 fail("Should not have thrown an exception: " + e.toString());
1549 public void testParseNationalNumber() throws Exception {
1550 // National prefix attached.
1551 assertEquals(NZ_NUMBER, phoneUtil.parse("033316005", RegionCode.NZ));
1552 assertEquals(NZ_NUMBER, phoneUtil.parse("33316005", RegionCode.NZ));
1553 // National prefix attached and some formatting present.
1554 assertEquals(NZ_NUMBER, phoneUtil.parse("03-331 6005", RegionCode.NZ));
1555 assertEquals(NZ_NUMBER, phoneUtil.parse("03 331 6005", RegionCode.NZ));
1556 // Test parsing RFC3966 format with a phone context.
1557 assertEquals(NZ_NUMBER, phoneUtil.parse("tel:03-331-6005;phone-context=+64", RegionCode.NZ));
1558 assertEquals(NZ_NUMBER, phoneUtil.parse("tel:331-6005;phone-context=+64-3", RegionCode.NZ));
1559 assertEquals(NZ_NUMBER, phoneUtil.parse("tel:331-6005;phone-context=+64-3", RegionCode.US));
1560 // Test parsing RFC3966 format with optional user-defined parameters. The parameters will appear
1561 // after the context if present.
1562 assertEquals(NZ_NUMBER, phoneUtil.parse("tel:03-331-6005;phone-context=+64;a=%A1",
1564 // Test parsing RFC3966 with an ISDN subaddress.
1565 assertEquals(NZ_NUMBER, phoneUtil.parse("tel:03-331-6005;isub=12345;phone-context=+64",
1567 assertEquals(NZ_NUMBER, phoneUtil.parse("tel:+64-3-331-6005;isub=12345", RegionCode.NZ));
1568 // Testing international prefixes.
1569 // Should strip country calling code.
1570 assertEquals(NZ_NUMBER, phoneUtil.parse("0064 3 331 6005", RegionCode.NZ));
1571 // Try again, but this time we have an international number with Region Code US. It should
1572 // recognise the country calling code and parse accordingly.
1573 assertEquals(NZ_NUMBER, phoneUtil.parse("01164 3 331 6005", RegionCode.US));
1574 assertEquals(NZ_NUMBER, phoneUtil.parse("+64 3 331 6005", RegionCode.US));
1575 // We should ignore the leading plus here, since it is not followed by a valid country code but
1576 // instead is followed by the IDD for the US.
1577 assertEquals(NZ_NUMBER, phoneUtil.parse("+01164 3 331 6005", RegionCode.US));
1578 assertEquals(NZ_NUMBER, phoneUtil.parse("+0064 3 331 6005", RegionCode.NZ));
1579 assertEquals(NZ_NUMBER, phoneUtil.parse("+ 00 64 3 331 6005", RegionCode.NZ));
1581 assertEquals(US_LOCAL_NUMBER,
1582 phoneUtil.parse("tel:253-0000;phone-context=www.google.com", RegionCode.US));
1583 assertEquals(US_LOCAL_NUMBER,
1584 phoneUtil.parse("tel:253-0000;isub=12345;phone-context=www.google.com", RegionCode.US));
1585 // This is invalid because no "+" sign is present as part of phone-context. The phone context
1586 // is simply ignored in this case just as if it contains a domain.
1587 assertEquals(US_LOCAL_NUMBER,
1588 phoneUtil.parse("tel:2530000;isub=12345;phone-context=1-650", RegionCode.US));
1589 assertEquals(US_LOCAL_NUMBER,
1590 phoneUtil.parse("tel:2530000;isub=12345;phone-context=1234.com", RegionCode.US));
1592 PhoneNumber nzNumber = new PhoneNumber();
1593 nzNumber.setCountryCode(64).setNationalNumber(64123456L);
1594 assertEquals(nzNumber, phoneUtil.parse("64(0)64123456", RegionCode.NZ));
1595 // Check that using a "/" is fine in a phone number.
1596 assertEquals(DE_NUMBER, phoneUtil.parse("301/23456", RegionCode.DE));
1598 PhoneNumber usNumber = new PhoneNumber();
1599 // Check it doesn't use the '1' as a country calling code when parsing if the phone number was
1600 // already possible.
1601 usNumber.setCountryCode(1).setNationalNumber(1234567890L);
1602 assertEquals(usNumber, phoneUtil.parse("123-456-7890", RegionCode.US));
1604 // Test star numbers. Although this is not strictly valid, we would like to make sure we can
1605 // parse the output we produce when formatting the number.
1606 assertEquals(JP_STAR_NUMBER, phoneUtil.parse("+81 *2345", RegionCode.JP));
1608 PhoneNumber shortNumber = new PhoneNumber();
1609 shortNumber.setCountryCode(64).setNationalNumber(12L);
1610 assertEquals(shortNumber, phoneUtil.parse("12", RegionCode.NZ));
1613 public void testParseNumberWithAlphaCharacters() throws Exception {
1614 // Test case with alpha characters.
1615 PhoneNumber tollfreeNumber = new PhoneNumber();
1616 tollfreeNumber.setCountryCode(64).setNationalNumber(800332005L);
1617 assertEquals(tollfreeNumber, phoneUtil.parse("0800 DDA 005", RegionCode.NZ));
1618 PhoneNumber premiumNumber = new PhoneNumber();
1619 premiumNumber.setCountryCode(64).setNationalNumber(9003326005L);
1620 assertEquals(premiumNumber, phoneUtil.parse("0900 DDA 6005", RegionCode.NZ));
1621 // Not enough alpha characters for them to be considered intentional, so they are stripped.
1622 assertEquals(premiumNumber, phoneUtil.parse("0900 332 6005a", RegionCode.NZ));
1623 assertEquals(premiumNumber, phoneUtil.parse("0900 332 600a5", RegionCode.NZ));
1624 assertEquals(premiumNumber, phoneUtil.parse("0900 332 600A5", RegionCode.NZ));
1625 assertEquals(premiumNumber, phoneUtil.parse("0900 a332 600A5", RegionCode.NZ));
1628 public void testParseMaliciousInput() throws Exception {
1629 // Lots of leading + signs before the possible number.
1630 StringBuilder maliciousNumber = new StringBuilder(6000);
1631 for (int i = 0; i < 6000; i++) {
1632 maliciousNumber.append('+');
1634 maliciousNumber.append("12222-33-244 extensioB 343+");
1636 phoneUtil.parse(maliciousNumber.toString(), RegionCode.US);
1637 fail("This should not parse without throwing an exception " + maliciousNumber);
1638 } catch (NumberParseException e) {
1639 // Expected this exception.
1640 assertEquals("Wrong error type stored in exception.",
1641 NumberParseException.ErrorType.TOO_LONG,
1644 StringBuilder maliciousNumberWithAlmostExt = new StringBuilder(6000);
1645 for (int i = 0; i < 350; i++) {
1646 maliciousNumberWithAlmostExt.append("200");
1648 maliciousNumberWithAlmostExt.append(" extensiOB 345");
1650 phoneUtil.parse(maliciousNumberWithAlmostExt.toString(), RegionCode.US);
1651 fail("This should not parse without throwing an exception " + maliciousNumberWithAlmostExt);
1652 } catch (NumberParseException e) {
1653 // Expected this exception.
1654 assertEquals("Wrong error type stored in exception.",
1655 NumberParseException.ErrorType.TOO_LONG,
1660 public void testParseWithInternationalPrefixes() throws Exception {
1661 assertEquals(US_NUMBER, phoneUtil.parse("+1 (650) 253-0000", RegionCode.NZ));
1662 assertEquals(INTERNATIONAL_TOLL_FREE, phoneUtil.parse("011 800 1234 5678", RegionCode.US));
1663 assertEquals(US_NUMBER, phoneUtil.parse("1-650-253-0000", RegionCode.US));
1664 // Calling the US number from Singapore by using different service providers
1665 // 1st test: calling using SingTel IDD service (IDD is 001)
1666 assertEquals(US_NUMBER, phoneUtil.parse("0011-650-253-0000", RegionCode.SG));
1667 // 2nd test: calling using StarHub IDD service (IDD is 008)
1668 assertEquals(US_NUMBER, phoneUtil.parse("0081-650-253-0000", RegionCode.SG));
1669 // 3rd test: calling using SingTel V019 service (IDD is 019)
1670 assertEquals(US_NUMBER, phoneUtil.parse("0191-650-253-0000", RegionCode.SG));
1671 // Calling the US number from Poland
1672 assertEquals(US_NUMBER, phoneUtil.parse("0~01-650-253-0000", RegionCode.PL));
1673 // Using "++" at the start.
1674 assertEquals(US_NUMBER, phoneUtil.parse("++1 (650) 253-0000", RegionCode.PL));
1677 public void testParseNonAscii() throws Exception {
1678 // Using a full-width plus sign.
1679 assertEquals(US_NUMBER, phoneUtil.parse("\uFF0B1 (650) 253-0000", RegionCode.SG));
1680 // Using a soft hyphen U+00AD.
1681 assertEquals(US_NUMBER, phoneUtil.parse("1 (650) 253\u00AD-0000", RegionCode.US));
1682 // The whole number, including punctuation, is here represented in full-width form.
1683 assertEquals(US_NUMBER, phoneUtil.parse("\uFF0B\uFF11\u3000\uFF08\uFF16\uFF15\uFF10\uFF09" +
1684 "\u3000\uFF12\uFF15\uFF13\uFF0D\uFF10\uFF10\uFF10" +
1687 // Using U+30FC dash instead.
1688 assertEquals(US_NUMBER, phoneUtil.parse("\uFF0B\uFF11\u3000\uFF08\uFF16\uFF15\uFF10\uFF09" +
1689 "\u3000\uFF12\uFF15\uFF13\u30FC\uFF10\uFF10\uFF10" +
1693 // Using a very strange decimal digit range (Mongolian digits).
1694 assertEquals(US_NUMBER, phoneUtil.parse("\u1811 \u1816\u1815\u1810 " +
1695 "\u1812\u1815\u1813 \u1810\u1810\u1810\u1810",
1699 public void testParseWithLeadingZero() throws Exception {
1700 assertEquals(IT_NUMBER, phoneUtil.parse("+39 02-36618 300", RegionCode.NZ));
1701 assertEquals(IT_NUMBER, phoneUtil.parse("02-36618 300", RegionCode.IT));
1703 assertEquals(IT_MOBILE, phoneUtil.parse("345 678 901", RegionCode.IT));
1706 public void testParseNationalNumberArgentina() throws Exception {
1707 // Test parsing mobile numbers of Argentina.
1708 PhoneNumber arNumber = new PhoneNumber();
1709 arNumber.setCountryCode(54).setNationalNumber(93435551212L);
1710 assertEquals(arNumber, phoneUtil.parse("+54 9 343 555 1212", RegionCode.AR));
1711 assertEquals(arNumber, phoneUtil.parse("0343 15 555 1212", RegionCode.AR));
1714 arNumber.setCountryCode(54).setNationalNumber(93715654320L);
1715 assertEquals(arNumber, phoneUtil.parse("+54 9 3715 65 4320", RegionCode.AR));
1716 assertEquals(arNumber, phoneUtil.parse("03715 15 65 4320", RegionCode.AR));
1717 assertEquals(AR_MOBILE, phoneUtil.parse("911 876 54321", RegionCode.AR));
1719 // Test parsing fixed-line numbers of Argentina.
1720 assertEquals(AR_NUMBER, phoneUtil.parse("+54 11 8765 4321", RegionCode.AR));
1721 assertEquals(AR_NUMBER, phoneUtil.parse("011 8765 4321", RegionCode.AR));
1724 arNumber.setCountryCode(54).setNationalNumber(3715654321L);
1725 assertEquals(arNumber, phoneUtil.parse("+54 3715 65 4321", RegionCode.AR));
1726 assertEquals(arNumber, phoneUtil.parse("03715 65 4321", RegionCode.AR));
1729 arNumber.setCountryCode(54).setNationalNumber(2312340000L);
1730 assertEquals(arNumber, phoneUtil.parse("+54 23 1234 0000", RegionCode.AR));
1731 assertEquals(arNumber, phoneUtil.parse("023 1234 0000", RegionCode.AR));
1734 public void testParseWithXInNumber() throws Exception {
1735 // Test that having an 'x' in the phone number at the start is ok and that it just gets removed.
1736 assertEquals(AR_NUMBER, phoneUtil.parse("01187654321", RegionCode.AR));
1737 assertEquals(AR_NUMBER, phoneUtil.parse("(0) 1187654321", RegionCode.AR));
1738 assertEquals(AR_NUMBER, phoneUtil.parse("0 1187654321", RegionCode.AR));
1739 assertEquals(AR_NUMBER, phoneUtil.parse("(0xx) 1187654321", RegionCode.AR));
1740 PhoneNumber arFromUs = new PhoneNumber();
1741 arFromUs.setCountryCode(54).setNationalNumber(81429712L);
1742 // This test is intentionally constructed such that the number of digit after xx is larger than
1743 // 7, so that the number won't be mistakenly treated as an extension, as we allow extensions up
1744 // to 7 digits. This assumption is okay for now as all the countries where a carrier selection
1745 // code is written in the form of xx have a national significant number of length larger than 7.
1746 assertEquals(arFromUs, phoneUtil.parse("011xx5481429712", RegionCode.US));
1749 public void testParseNumbersMexico() throws Exception {
1750 // Test parsing fixed-line numbers of Mexico.
1751 PhoneNumber mxNumber = new PhoneNumber();
1752 mxNumber.setCountryCode(52).setNationalNumber(4499780001L);
1753 assertEquals(mxNumber, phoneUtil.parse("+52 (449)978-0001", RegionCode.MX));
1754 assertEquals(mxNumber, phoneUtil.parse("01 (449)978-0001", RegionCode.MX));
1755 assertEquals(mxNumber, phoneUtil.parse("(449)978-0001", RegionCode.MX));
1757 // Test parsing mobile numbers of Mexico.
1759 mxNumber.setCountryCode(52).setNationalNumber(13312345678L);
1760 assertEquals(mxNumber, phoneUtil.parse("+52 1 33 1234-5678", RegionCode.MX));
1761 assertEquals(mxNumber, phoneUtil.parse("044 (33) 1234-5678", RegionCode.MX));
1762 assertEquals(mxNumber, phoneUtil.parse("045 33 1234-5678", RegionCode.MX));
1765 public void testFailedParseOnInvalidNumbers() {
1767 String sentencePhoneNumber = "This is not a phone number";
1768 phoneUtil.parse(sentencePhoneNumber, RegionCode.NZ);
1769 fail("This should not parse without throwing an exception " + sentencePhoneNumber);
1770 } catch (NumberParseException e) {
1771 // Expected this exception.
1772 assertEquals("Wrong error type stored in exception.",
1773 NumberParseException.ErrorType.NOT_A_NUMBER,
1777 String sentencePhoneNumber = "1 Still not a number";
1778 phoneUtil.parse(sentencePhoneNumber, RegionCode.NZ);
1779 fail("This should not parse without throwing an exception " + sentencePhoneNumber);
1780 } catch (NumberParseException e) {
1781 // Expected this exception.
1782 assertEquals("Wrong error type stored in exception.",
1783 NumberParseException.ErrorType.NOT_A_NUMBER,
1787 String sentencePhoneNumber = "1 MICROSOFT";
1788 phoneUtil.parse(sentencePhoneNumber, RegionCode.NZ);
1789 fail("This should not parse without throwing an exception " + sentencePhoneNumber);
1790 } catch (NumberParseException e) {
1791 // Expected this exception.
1792 assertEquals("Wrong error type stored in exception.",
1793 NumberParseException.ErrorType.NOT_A_NUMBER,
1797 String sentencePhoneNumber = "12 MICROSOFT";
1798 phoneUtil.parse(sentencePhoneNumber, RegionCode.NZ);
1799 fail("This should not parse without throwing an exception " + sentencePhoneNumber);
1800 } catch (NumberParseException e) {
1801 // Expected this exception.
1802 assertEquals("Wrong error type stored in exception.",
1803 NumberParseException.ErrorType.NOT_A_NUMBER,
1807 String tooLongPhoneNumber = "01495 72553301873 810104";
1808 phoneUtil.parse(tooLongPhoneNumber, RegionCode.GB);
1809 fail("This should not parse without throwing an exception " + tooLongPhoneNumber);
1810 } catch (NumberParseException e) {
1811 // Expected this exception.
1812 assertEquals("Wrong error type stored in exception.",
1813 NumberParseException.ErrorType.TOO_LONG,
1817 String plusMinusPhoneNumber = "+---";
1818 phoneUtil.parse(plusMinusPhoneNumber, RegionCode.DE);
1819 fail("This should not parse without throwing an exception " + plusMinusPhoneNumber);
1820 } catch (NumberParseException e) {
1821 // Expected this exception.
1822 assertEquals("Wrong error type stored in exception.",
1823 NumberParseException.ErrorType.NOT_A_NUMBER,
1827 String plusStar = "+***";
1828 phoneUtil.parse(plusStar, RegionCode.DE);
1829 fail("This should not parse without throwing an exception " + plusStar);
1830 } catch (NumberParseException e) {
1831 // Expected this exception.
1832 assertEquals("Wrong error type stored in exception.",
1833 NumberParseException.ErrorType.NOT_A_NUMBER,
1837 String plusStarPhoneNumber = "+*******91";
1838 phoneUtil.parse(plusStarPhoneNumber, RegionCode.DE);
1839 fail("This should not parse without throwing an exception " + plusStarPhoneNumber);
1840 } catch (NumberParseException e) {
1841 // Expected this exception.
1842 assertEquals("Wrong error type stored in exception.",
1843 NumberParseException.ErrorType.NOT_A_NUMBER,
1847 String tooShortPhoneNumber = "+49 0";
1848 phoneUtil.parse(tooShortPhoneNumber, RegionCode.DE);
1849 fail("This should not parse without throwing an exception " + tooShortPhoneNumber);
1850 } catch (NumberParseException e) {
1851 // Expected this exception.
1852 assertEquals("Wrong error type stored in exception.",
1853 NumberParseException.ErrorType.TOO_SHORT_NSN,
1857 String invalidCountryCode = "+210 3456 56789";
1858 phoneUtil.parse(invalidCountryCode, RegionCode.NZ);
1859 fail("This is not a recognised region code: should fail: " + invalidCountryCode);
1860 } catch (NumberParseException e) {
1861 // Expected this exception.
1862 assertEquals("Wrong error type stored in exception.",
1863 NumberParseException.ErrorType.INVALID_COUNTRY_CODE,
1867 String plusAndIddAndInvalidCountryCode = "+ 00 210 3 331 6005";
1868 phoneUtil.parse(plusAndIddAndInvalidCountryCode, RegionCode.NZ);
1869 fail("This should not parse without throwing an exception.");
1870 } catch (NumberParseException e) {
1871 // Expected this exception. 00 is a correct IDD, but 210 is not a valid country code.
1872 assertEquals("Wrong error type stored in exception.",
1873 NumberParseException.ErrorType.INVALID_COUNTRY_CODE,
1877 String someNumber = "123 456 7890";
1878 phoneUtil.parse(someNumber, RegionCode.ZZ);
1879 fail("'Unknown' region code not allowed: should fail.");
1880 } catch (NumberParseException e) {
1881 // Expected this exception.
1882 assertEquals("Wrong error type stored in exception.",
1883 NumberParseException.ErrorType.INVALID_COUNTRY_CODE,
1887 String someNumber = "123 456 7890";
1888 phoneUtil.parse(someNumber, RegionCode.CS);
1889 fail("Deprecated region code not allowed: should fail.");
1890 } catch (NumberParseException e) {
1891 // Expected this exception.
1892 assertEquals("Wrong error type stored in exception.",
1893 NumberParseException.ErrorType.INVALID_COUNTRY_CODE,
1897 String someNumber = "123 456 7890";
1898 phoneUtil.parse(someNumber, null);
1899 fail("Null region code not allowed: should fail.");
1900 } catch (NumberParseException e) {
1901 // Expected this exception.
1902 assertEquals("Wrong error type stored in exception.",
1903 NumberParseException.ErrorType.INVALID_COUNTRY_CODE,
1907 String someNumber = "0044------";
1908 phoneUtil.parse(someNumber, RegionCode.GB);
1909 fail("No number provided, only region code: should fail");
1910 } catch (NumberParseException e) {
1911 // Expected this exception.
1912 assertEquals("Wrong error type stored in exception.",
1913 NumberParseException.ErrorType.TOO_SHORT_AFTER_IDD,
1917 String someNumber = "0044";
1918 phoneUtil.parse(someNumber, RegionCode.GB);
1919 fail("No number provided, only region code: should fail");
1920 } catch (NumberParseException e) {
1921 // Expected this exception.
1922 assertEquals("Wrong error type stored in exception.",
1923 NumberParseException.ErrorType.TOO_SHORT_AFTER_IDD,
1927 String someNumber = "011";
1928 phoneUtil.parse(someNumber, RegionCode.US);
1929 fail("Only IDD provided - should fail.");
1930 } catch (NumberParseException e) {
1931 // Expected this exception.
1932 assertEquals("Wrong error type stored in exception.",
1933 NumberParseException.ErrorType.TOO_SHORT_AFTER_IDD,
1937 String someNumber = "0119";
1938 phoneUtil.parse(someNumber, RegionCode.US);
1939 fail("Only IDD provided and then 9 - should fail.");
1940 } catch (NumberParseException e) {
1941 // Expected this exception.
1942 assertEquals("Wrong error type stored in exception.",
1943 NumberParseException.ErrorType.TOO_SHORT_AFTER_IDD,
1947 String emptyNumber = "";
1949 phoneUtil.parse(emptyNumber, RegionCode.ZZ);
1950 fail("Empty string - should fail.");
1951 } catch (NumberParseException e) {
1952 // Expected this exception.
1953 assertEquals("Wrong error type stored in exception.",
1954 NumberParseException.ErrorType.NOT_A_NUMBER,
1958 String nullNumber = null;
1960 phoneUtil.parse(nullNumber, RegionCode.ZZ);
1961 fail("Null string - should fail.");
1962 } catch (NumberParseException e) {
1963 // Expected this exception.
1964 assertEquals("Wrong error type stored in exception.",
1965 NumberParseException.ErrorType.NOT_A_NUMBER,
1967 } catch (NullPointerException e) {
1968 fail("Null string - but should not throw a null pointer exception.");
1971 String nullNumber = null;
1972 phoneUtil.parse(nullNumber, RegionCode.US);
1973 fail("Null string - should fail.");
1974 } catch (NumberParseException e) {
1975 // Expected this exception.
1976 assertEquals("Wrong error type stored in exception.",
1977 NumberParseException.ErrorType.NOT_A_NUMBER,
1979 } catch (NullPointerException e) {
1980 fail("Null string - but should not throw a null pointer exception.");
1983 String domainRfcPhoneContext = "tel:555-1234;phone-context=www.google.com";
1984 phoneUtil.parse(domainRfcPhoneContext, RegionCode.ZZ);
1985 fail("'Unknown' region code not allowed: should fail.");
1986 } catch (NumberParseException e) {
1987 // Expected this exception.
1988 assertEquals("Wrong error type stored in exception.",
1989 NumberParseException.ErrorType.INVALID_COUNTRY_CODE,
1993 // This is invalid because no "+" sign is present as part of phone-context. This should not
1994 // succeed in being parsed.
1995 String invalidRfcPhoneContext = "tel:555-1234;phone-context=1-331";
1996 phoneUtil.parse(invalidRfcPhoneContext, RegionCode.ZZ);
1997 fail("'Unknown' region code not allowed: should fail.");
1998 } catch (NumberParseException e) {
1999 // Expected this exception.
2000 assertEquals("Wrong error type stored in exception.",
2001 NumberParseException.ErrorType.INVALID_COUNTRY_CODE,
2006 public void testParseNumbersWithPlusWithNoRegion() throws Exception {
2007 // RegionCode.ZZ is allowed only if the number starts with a '+' - then the country calling code
2008 // can be calculated.
2009 assertEquals(NZ_NUMBER, phoneUtil.parse("+64 3 331 6005", RegionCode.ZZ));
2010 // Test with full-width plus.
2011 assertEquals(NZ_NUMBER, phoneUtil.parse("\uFF0B64 3 331 6005", RegionCode.ZZ));
2012 // Test with normal plus but leading characters that need to be stripped.
2013 assertEquals(NZ_NUMBER, phoneUtil.parse("Tel: +64 3 331 6005", RegionCode.ZZ));
2014 assertEquals(NZ_NUMBER, phoneUtil.parse("+64 3 331 6005", null));
2015 assertEquals(INTERNATIONAL_TOLL_FREE, phoneUtil.parse("+800 1234 5678", null));
2016 assertEquals(UNIVERSAL_PREMIUM_RATE, phoneUtil.parse("+979 123 456 789", null));
2018 // Test parsing RFC3966 format with a phone context.
2019 assertEquals(NZ_NUMBER, phoneUtil.parse("tel:03-331-6005;phone-context=+64", RegionCode.ZZ));
2020 assertEquals(NZ_NUMBER, phoneUtil.parse(" tel:03-331-6005;phone-context=+64", RegionCode.ZZ));
2021 assertEquals(NZ_NUMBER, phoneUtil.parse("tel:03-331-6005;isub=12345;phone-context=+64",
2024 // It is important that we set the carrier code to an empty string, since we used
2025 // ParseAndKeepRawInput and no carrier code was found.
2026 PhoneNumber nzNumberWithRawInput = new PhoneNumber().mergeFrom(NZ_NUMBER).
2027 setRawInput("+64 3 331 6005").
2028 setCountryCodeSource(CountryCodeSource.FROM_NUMBER_WITH_PLUS_SIGN).
2029 setPreferredDomesticCarrierCode("");
2030 assertEquals(nzNumberWithRawInput, phoneUtil.parseAndKeepRawInput("+64 3 331 6005",
2032 // Null is also allowed for the region code in these cases.
2033 assertEquals(nzNumberWithRawInput, phoneUtil.parseAndKeepRawInput("+64 3 331 6005", null));
2036 public void testParseExtensions() throws Exception {
2037 PhoneNumber nzNumber = new PhoneNumber();
2038 nzNumber.setCountryCode(64).setNationalNumber(33316005L).setExtension("3456");
2039 assertEquals(nzNumber, phoneUtil.parse("03 331 6005 ext 3456", RegionCode.NZ));
2040 assertEquals(nzNumber, phoneUtil.parse("03-3316005x3456", RegionCode.NZ));
2041 assertEquals(nzNumber, phoneUtil.parse("03-3316005 int.3456", RegionCode.NZ));
2042 assertEquals(nzNumber, phoneUtil.parse("03 3316005 #3456", RegionCode.NZ));
2043 // Test the following do not extract extensions:
2044 assertEquals(ALPHA_NUMERIC_NUMBER, phoneUtil.parse("1800 six-flags", RegionCode.US));
2045 assertEquals(ALPHA_NUMERIC_NUMBER, phoneUtil.parse("1800 SIX FLAGS", RegionCode.US));
2046 assertEquals(ALPHA_NUMERIC_NUMBER, phoneUtil.parse("0~0 1800 7493 5247", RegionCode.PL));
2047 assertEquals(ALPHA_NUMERIC_NUMBER, phoneUtil.parse("(1800) 7493.5247", RegionCode.US));
2048 // Check that the last instance of an extension token is matched.
2049 PhoneNumber extnNumber = new PhoneNumber().mergeFrom(ALPHA_NUMERIC_NUMBER).setExtension("1234");
2050 assertEquals(extnNumber, phoneUtil.parse("0~0 1800 7493 5247 ~1234", RegionCode.PL));
2051 // Verifying bug-fix where the last digit of a number was previously omitted if it was a 0 when
2052 // extracting the extension. Also verifying a few different cases of extensions.
2053 PhoneNumber ukNumber = new PhoneNumber();
2054 ukNumber.setCountryCode(44).setNationalNumber(2034567890L).setExtension("456");
2055 assertEquals(ukNumber, phoneUtil.parse("+44 2034567890x456", RegionCode.NZ));
2056 assertEquals(ukNumber, phoneUtil.parse("+44 2034567890x456", RegionCode.GB));
2057 assertEquals(ukNumber, phoneUtil.parse("+44 2034567890 x456", RegionCode.GB));
2058 assertEquals(ukNumber, phoneUtil.parse("+44 2034567890 X456", RegionCode.GB));
2059 assertEquals(ukNumber, phoneUtil.parse("+44 2034567890 X 456", RegionCode.GB));
2060 assertEquals(ukNumber, phoneUtil.parse("+44 2034567890 X 456", RegionCode.GB));
2061 assertEquals(ukNumber, phoneUtil.parse("+44 2034567890 x 456 ", RegionCode.GB));
2062 assertEquals(ukNumber, phoneUtil.parse("+44 2034567890 X 456", RegionCode.GB));
2063 assertEquals(ukNumber, phoneUtil.parse("+44-2034567890;ext=456", RegionCode.GB));
2064 assertEquals(ukNumber, phoneUtil.parse("tel:2034567890;ext=456;phone-context=+44",
2066 // Full-width extension, "extn" only.
2067 assertEquals(ukNumber, phoneUtil.parse("+442034567890\uFF45\uFF58\uFF54\uFF4E456",
2070 assertEquals(ukNumber, phoneUtil.parse("+442034567890\uFF58\uFF54\uFF4E456",
2073 assertEquals(ukNumber, phoneUtil.parse("+442034567890\uFF58\uFF54456",
2076 PhoneNumber usWithExtension = new PhoneNumber();
2077 usWithExtension.setCountryCode(1).setNationalNumber(8009013355L).setExtension("7246433");
2078 assertEquals(usWithExtension, phoneUtil.parse("(800) 901-3355 x 7246433", RegionCode.US));
2079 assertEquals(usWithExtension, phoneUtil.parse("(800) 901-3355 , ext 7246433", RegionCode.US));
2080 assertEquals(usWithExtension,
2081 phoneUtil.parse("(800) 901-3355 ,extension 7246433", RegionCode.US));
2082 assertEquals(usWithExtension,
2083 phoneUtil.parse("(800) 901-3355 ,extensi\u00F3n 7246433", RegionCode.US));
2084 // Repeat with the small letter o with acute accent created by combining characters.
2085 assertEquals(usWithExtension,
2086 phoneUtil.parse("(800) 901-3355 ,extensio\u0301n 7246433", RegionCode.US));
2087 assertEquals(usWithExtension, phoneUtil.parse("(800) 901-3355 , 7246433", RegionCode.US));
2088 assertEquals(usWithExtension, phoneUtil.parse("(800) 901-3355 ext: 7246433", RegionCode.US));
2090 // Test that if a number has two extensions specified, we ignore the second.
2091 PhoneNumber usWithTwoExtensionsNumber = new PhoneNumber();
2092 usWithTwoExtensionsNumber.setCountryCode(1).setNationalNumber(2121231234L).setExtension("508");
2093 assertEquals(usWithTwoExtensionsNumber, phoneUtil.parse("(212)123-1234 x508/x1234",
2095 assertEquals(usWithTwoExtensionsNumber, phoneUtil.parse("(212)123-1234 x508/ x1234",
2097 assertEquals(usWithTwoExtensionsNumber, phoneUtil.parse("(212)123-1234 x508\\x1234",
2100 // Test parsing numbers in the form (645) 123-1234-910# works, where the last 3 digits before
2101 // the # are an extension.
2102 usWithExtension.clear();
2103 usWithExtension.setCountryCode(1).setNationalNumber(6451231234L).setExtension("910");
2104 assertEquals(usWithExtension, phoneUtil.parse("+1 (645) 123 1234-910#", RegionCode.US));
2105 // Retry with the same number in a slightly different format.
2106 assertEquals(usWithExtension, phoneUtil.parse("+1 (645) 123 1234 ext. 910#", RegionCode.US));
2109 public void testParseAndKeepRaw() throws Exception {
2110 PhoneNumber alphaNumericNumber = new PhoneNumber().mergeFrom(ALPHA_NUMERIC_NUMBER).
2111 setRawInput("800 six-flags").
2112 setCountryCodeSource(CountryCodeSource.FROM_DEFAULT_COUNTRY).
2113 setPreferredDomesticCarrierCode("");
2114 assertEquals(alphaNumericNumber,
2115 phoneUtil.parseAndKeepRawInput("800 six-flags", RegionCode.US));
2117 PhoneNumber shorterAlphaNumber = new PhoneNumber().
2118 setCountryCode(1).setNationalNumber(8007493524L).
2119 setRawInput("1800 six-flag").
2120 setCountryCodeSource(CountryCodeSource.FROM_NUMBER_WITHOUT_PLUS_SIGN).
2121 setPreferredDomesticCarrierCode("");
2122 assertEquals(shorterAlphaNumber,
2123 phoneUtil.parseAndKeepRawInput("1800 six-flag", RegionCode.US));
2125 shorterAlphaNumber.setRawInput("+1800 six-flag").
2126 setCountryCodeSource(CountryCodeSource.FROM_NUMBER_WITH_PLUS_SIGN);
2127 assertEquals(shorterAlphaNumber,
2128 phoneUtil.parseAndKeepRawInput("+1800 six-flag", RegionCode.NZ));
2130 shorterAlphaNumber.setRawInput("001800 six-flag").
2131 setCountryCodeSource(CountryCodeSource.FROM_NUMBER_WITH_IDD);
2132 assertEquals(shorterAlphaNumber,
2133 phoneUtil.parseAndKeepRawInput("001800 six-flag", RegionCode.NZ));
2135 // Invalid region code supplied.
2137 phoneUtil.parseAndKeepRawInput("123 456 7890", RegionCode.CS);
2138 fail("Deprecated region code not allowed: should fail.");
2139 } catch (NumberParseException e) {
2140 // Expected this exception.
2141 assertEquals("Wrong error type stored in exception.",
2142 NumberParseException.ErrorType.INVALID_COUNTRY_CODE,
2146 PhoneNumber koreanNumber = new PhoneNumber();
2147 koreanNumber.setCountryCode(82).setNationalNumber(22123456).setRawInput("08122123456").
2148 setCountryCodeSource(CountryCodeSource.FROM_DEFAULT_COUNTRY).
2149 setPreferredDomesticCarrierCode("81");
2150 assertEquals(koreanNumber, phoneUtil.parseAndKeepRawInput("08122123456", RegionCode.KR));
2153 public void testCountryWithNoNumberDesc() {
2154 // Andorra is a country where we don't have PhoneNumberDesc info in the metadata.
2155 PhoneNumber adNumber = new PhoneNumber();
2156 adNumber.setCountryCode(376).setNationalNumber(12345L);
2157 assertEquals("+376 12345", phoneUtil.format(adNumber, PhoneNumberFormat.INTERNATIONAL));
2158 assertEquals("+37612345", phoneUtil.format(adNumber, PhoneNumberFormat.E164));
2159 assertEquals("12345", phoneUtil.format(adNumber, PhoneNumberFormat.NATIONAL));
2160 assertEquals(PhoneNumberUtil.PhoneNumberType.UNKNOWN, phoneUtil.getNumberType(adNumber));
2161 assertTrue(phoneUtil.isValidNumber(adNumber));
2163 // Test dialing a US number from within Andorra.
2164 assertEquals("00 1 650 253 0000",
2165 phoneUtil.formatOutOfCountryCallingNumber(US_NUMBER, RegionCode.AD));
2168 public void testUnknownCountryCallingCodeForValidation() {
2169 PhoneNumber invalidNumber = new PhoneNumber();
2170 invalidNumber.setCountryCode(0).setNationalNumber(1234L);
2171 assertFalse(phoneUtil.isValidNumber(invalidNumber));
2174 public void testIsNumberMatchMatches() throws Exception {
2175 // Test simple matches where formatting is different, or leading zeroes, or country calling code
2176 // has been specified.
2177 assertEquals(PhoneNumberUtil.MatchType.EXACT_MATCH,
2178 phoneUtil.isNumberMatch("+64 3 331 6005", "+64 03 331 6005"));
2179 assertEquals(PhoneNumberUtil.MatchType.EXACT_MATCH,
2180 phoneUtil.isNumberMatch("+800 1234 5678", "+80012345678"));
2181 assertEquals(PhoneNumberUtil.MatchType.EXACT_MATCH,
2182 phoneUtil.isNumberMatch("+64 03 331-6005", "+64 03331 6005"));
2183 assertEquals(PhoneNumberUtil.MatchType.EXACT_MATCH,
2184 phoneUtil.isNumberMatch("+643 331-6005", "+64033316005"));
2185 assertEquals(PhoneNumberUtil.MatchType.EXACT_MATCH,
2186 phoneUtil.isNumberMatch("+643 331-6005", "+6433316005"));
2187 assertEquals(PhoneNumberUtil.MatchType.EXACT_MATCH,
2188 phoneUtil.isNumberMatch("+64 3 331-6005", "+6433316005"));
2189 assertEquals(PhoneNumberUtil.MatchType.EXACT_MATCH,
2190 phoneUtil.isNumberMatch("+64 3 331-6005", "tel:+64-3-331-6005;isub=123"));
2191 // Test alpha numbers.
2192 assertEquals(PhoneNumberUtil.MatchType.EXACT_MATCH,
2193 phoneUtil.isNumberMatch("+1800 siX-Flags", "+1 800 7493 5247"));
2194 // Test numbers with extensions.
2195 assertEquals(PhoneNumberUtil.MatchType.EXACT_MATCH,
2196 phoneUtil.isNumberMatch("+64 3 331-6005 extn 1234", "+6433316005#1234"));
2197 // Test proto buffers.
2198 assertEquals(PhoneNumberUtil.MatchType.EXACT_MATCH,
2199 phoneUtil.isNumberMatch(NZ_NUMBER, "+6403 331 6005"));
2201 PhoneNumber nzNumber = new PhoneNumber().mergeFrom(NZ_NUMBER).setExtension("3456");
2202 assertEquals(PhoneNumberUtil.MatchType.EXACT_MATCH,
2203 phoneUtil.isNumberMatch(nzNumber, "+643 331 6005 ext 3456"));
2204 // Check empty extensions are ignored.
2205 nzNumber.setExtension("");
2206 assertEquals(PhoneNumberUtil.MatchType.EXACT_MATCH,
2207 phoneUtil.isNumberMatch(nzNumber, "+6403 331 6005"));
2208 // Check variant with two proto buffers.
2209 assertEquals("Number " + nzNumber.toString() + " did not match " + NZ_NUMBER.toString(),
2210 PhoneNumberUtil.MatchType.EXACT_MATCH,
2211 phoneUtil.isNumberMatch(nzNumber, NZ_NUMBER));
2213 // Check raw_input, country_code_source and preferred_domestic_carrier_code are ignored.
2214 PhoneNumber brNumberOne = new PhoneNumber();
2215 PhoneNumber brNumberTwo = new PhoneNumber();
2216 brNumberOne.setCountryCode(55).setNationalNumber(3121286979L)
2217 .setCountryCodeSource(PhoneNumber.CountryCodeSource.FROM_NUMBER_WITH_PLUS_SIGN)
2218 .setPreferredDomesticCarrierCode("12").setRawInput("012 3121286979");
2219 brNumberTwo.setCountryCode(55).setNationalNumber(3121286979L)
2220 .setCountryCodeSource(PhoneNumber.CountryCodeSource.FROM_DEFAULT_COUNTRY)
2221 .setPreferredDomesticCarrierCode("14").setRawInput("143121286979");
2222 assertEquals(PhoneNumberUtil.MatchType.EXACT_MATCH,
2223 phoneUtil.isNumberMatch(brNumberOne, brNumberTwo));
2226 public void testIsNumberMatchNonMatches() throws Exception {
2228 assertEquals(PhoneNumberUtil.MatchType.NO_MATCH,
2229 phoneUtil.isNumberMatch("03 331 6005", "03 331 6006"));
2230 assertEquals(PhoneNumberUtil.MatchType.NO_MATCH,
2231 phoneUtil.isNumberMatch("+800 1234 5678", "+1 800 1234 5678"));
2232 // Different country calling code, partial number match.
2233 assertEquals(PhoneNumberUtil.MatchType.NO_MATCH,
2234 phoneUtil.isNumberMatch("+64 3 331-6005", "+16433316005"));
2235 // Different country calling code, same number.
2236 assertEquals(PhoneNumberUtil.MatchType.NO_MATCH,
2237 phoneUtil.isNumberMatch("+64 3 331-6005", "+6133316005"));
2238 // Extension different, all else the same.
2239 assertEquals(PhoneNumberUtil.MatchType.NO_MATCH,
2240 phoneUtil.isNumberMatch("+64 3 331-6005 extn 1234", "0116433316005#1235"));
2241 assertEquals(PhoneNumberUtil.MatchType.NO_MATCH,
2242 phoneUtil.isNumberMatch(
2243 "+64 3 331-6005 extn 1234", "tel:+64-3-331-6005;ext=1235"));
2244 // NSN matches, but extension is different - not the same number.
2245 assertEquals(PhoneNumberUtil.MatchType.NO_MATCH,
2246 phoneUtil.isNumberMatch("+64 3 331-6005 ext.1235", "3 331 6005#1234"));
2248 // Invalid numbers that can't be parsed.
2249 assertEquals(PhoneNumberUtil.MatchType.NOT_A_NUMBER,
2250 phoneUtil.isNumberMatch("4", "3 331 6043"));
2251 assertEquals(PhoneNumberUtil.MatchType.NOT_A_NUMBER,
2252 phoneUtil.isNumberMatch("+43", "+64 3 331 6005"));
2253 assertEquals(PhoneNumberUtil.MatchType.NOT_A_NUMBER,
2254 phoneUtil.isNumberMatch("+43", "64 3 331 6005"));
2255 assertEquals(PhoneNumberUtil.MatchType.NOT_A_NUMBER,
2256 phoneUtil.isNumberMatch("Dog", "64 3 331 6005"));
2259 public void testIsNumberMatchNsnMatches() throws Exception {
2261 assertEquals(PhoneNumberUtil.MatchType.NSN_MATCH,
2262 phoneUtil.isNumberMatch("+64 3 331-6005", "03 331 6005"));
2263 assertEquals(PhoneNumberUtil.MatchType.NSN_MATCH,
2264 phoneUtil.isNumberMatch(
2265 "+64 3 331-6005", "tel:03-331-6005;isub=1234;phone-context=abc.nz"));
2266 assertEquals(PhoneNumberUtil.MatchType.NSN_MATCH,
2267 phoneUtil.isNumberMatch(NZ_NUMBER, "03 331 6005"));
2268 // Here the second number possibly starts with the country calling code for New Zealand,
2269 // although we are unsure.
2270 PhoneNumber unchangedNzNumber = new PhoneNumber().mergeFrom(NZ_NUMBER);
2271 assertEquals(PhoneNumberUtil.MatchType.NSN_MATCH,
2272 phoneUtil.isNumberMatch(unchangedNzNumber, "(64-3) 331 6005"));
2273 // Check the phone number proto was not edited during the method call.
2274 assertEquals(NZ_NUMBER, unchangedNzNumber);
2276 // Here, the 1 might be a national prefix, if we compare it to the US number, so the resultant
2277 // match is an NSN match.
2278 assertEquals(PhoneNumberUtil.MatchType.NSN_MATCH,
2279 phoneUtil.isNumberMatch(US_NUMBER, "1-650-253-0000"));
2280 assertEquals(PhoneNumberUtil.MatchType.NSN_MATCH,
2281 phoneUtil.isNumberMatch(US_NUMBER, "6502530000"));
2282 assertEquals(PhoneNumberUtil.MatchType.NSN_MATCH,
2283 phoneUtil.isNumberMatch("+1 650-253 0000", "1 650 253 0000"));
2284 assertEquals(PhoneNumberUtil.MatchType.NSN_MATCH,
2285 phoneUtil.isNumberMatch("1 650-253 0000", "1 650 253 0000"));
2286 assertEquals(PhoneNumberUtil.MatchType.NSN_MATCH,
2287 phoneUtil.isNumberMatch("1 650-253 0000", "+1 650 253 0000"));
2288 // For this case, the match will be a short NSN match, because we cannot assume that the 1 might
2289 // be a national prefix, so don't remove it when parsing.
2290 PhoneNumber randomNumber = new PhoneNumber();
2291 randomNumber.setCountryCode(41).setNationalNumber(6502530000L);
2292 assertEquals(PhoneNumberUtil.MatchType.SHORT_NSN_MATCH,
2293 phoneUtil.isNumberMatch(randomNumber, "1-650-253-0000"));
2296 public void testIsNumberMatchShortNsnMatches() throws Exception {
2297 // Short NSN matches with the country not specified for either one or both numbers.
2298 assertEquals(PhoneNumberUtil.MatchType.SHORT_NSN_MATCH,
2299 phoneUtil.isNumberMatch("+64 3 331-6005", "331 6005"));
2300 assertEquals(PhoneNumberUtil.MatchType.SHORT_NSN_MATCH,
2301 phoneUtil.isNumberMatch("+64 3 331-6005", "tel:331-6005;phone-context=abc.nz"));
2302 assertEquals(PhoneNumberUtil.MatchType.SHORT_NSN_MATCH,
2303 phoneUtil.isNumberMatch("+64 3 331-6005",
2304 "tel:331-6005;isub=1234;phone-context=abc.nz"));
2305 assertEquals(PhoneNumberUtil.MatchType.SHORT_NSN_MATCH,
2306 phoneUtil.isNumberMatch("+64 3 331-6005",
2307 "tel:331-6005;isub=1234;phone-context=abc.nz;a=%A1"));
2308 // We did not know that the "0" was a national prefix since neither number has a country code,
2309 // so this is considered a SHORT_NSN_MATCH.
2310 assertEquals(PhoneNumberUtil.MatchType.SHORT_NSN_MATCH,
2311 phoneUtil.isNumberMatch("3 331-6005", "03 331 6005"));
2312 assertEquals(PhoneNumberUtil.MatchType.SHORT_NSN_MATCH,
2313 phoneUtil.isNumberMatch("3 331-6005", "331 6005"));
2314 assertEquals(PhoneNumberUtil.MatchType.SHORT_NSN_MATCH,
2315 phoneUtil.isNumberMatch("3 331-6005", "tel:331-6005;phone-context=abc.nz"));
2316 assertEquals(PhoneNumberUtil.MatchType.SHORT_NSN_MATCH,
2317 phoneUtil.isNumberMatch("3 331-6005", "+64 331 6005"));
2318 // Short NSN match with the country specified.
2319 assertEquals(PhoneNumberUtil.MatchType.SHORT_NSN_MATCH,
2320 phoneUtil.isNumberMatch("03 331-6005", "331 6005"));
2321 assertEquals(PhoneNumberUtil.MatchType.SHORT_NSN_MATCH,
2322 phoneUtil.isNumberMatch("1 234 345 6789", "345 6789"));
2323 assertEquals(PhoneNumberUtil.MatchType.SHORT_NSN_MATCH,
2324 phoneUtil.isNumberMatch("+1 (234) 345 6789", "345 6789"));
2325 // NSN matches, country calling code omitted for one number, extension missing for one.
2326 assertEquals(PhoneNumberUtil.MatchType.SHORT_NSN_MATCH,
2327 phoneUtil.isNumberMatch("+64 3 331-6005", "3 331 6005#1234"));
2328 // One has Italian leading zero, one does not.
2329 PhoneNumber italianNumberOne = new PhoneNumber();
2330 italianNumberOne.setCountryCode(39).setNationalNumber(1234L).setItalianLeadingZero(true);
2331 PhoneNumber italianNumberTwo = new PhoneNumber();
2332 italianNumberTwo.setCountryCode(39).setNationalNumber(1234L);
2333 assertEquals(PhoneNumberUtil.MatchType.SHORT_NSN_MATCH,
2334 phoneUtil.isNumberMatch(italianNumberOne, italianNumberTwo));
2335 // One has an extension, the other has an extension of "".
2336 italianNumberOne.setExtension("1234").clearItalianLeadingZero();
2337 italianNumberTwo.setExtension("");
2338 assertEquals(PhoneNumberUtil.MatchType.SHORT_NSN_MATCH,
2339 phoneUtil.isNumberMatch(italianNumberOne, italianNumberTwo));
2342 public void testCanBeInternationallyDialled() throws Exception {
2343 // We have no-international-dialling rules for the US in our test metadata that say that
2344 // toll-free numbers cannot be dialled internationally.
2345 assertFalse(phoneUtil.canBeInternationallyDialled(US_TOLLFREE));
2347 // Normal US numbers can be internationally dialled.
2348 assertTrue(phoneUtil.canBeInternationallyDialled(US_NUMBER));
2351 assertTrue(phoneUtil.canBeInternationallyDialled(US_LOCAL_NUMBER));
2353 // We have no data for NZ - should return true.
2354 assertTrue(phoneUtil.canBeInternationallyDialled(NZ_NUMBER));
2355 assertTrue(phoneUtil.canBeInternationallyDialled(INTERNATIONAL_TOLL_FREE));
2358 public void testIsAlphaNumber() throws Exception {
2359 assertTrue(phoneUtil.isAlphaNumber("1800 six-flags"));
2360 assertTrue(phoneUtil.isAlphaNumber("1800 six-flags ext. 1234"));
2361 assertTrue(phoneUtil.isAlphaNumber("+800 six-flags"));
2362 assertTrue(phoneUtil.isAlphaNumber("180 six-flags"));
2363 assertFalse(phoneUtil.isAlphaNumber("1800 123-1234"));
2364 assertFalse(phoneUtil.isAlphaNumber("1 six-flags"));
2365 assertFalse(phoneUtil.isAlphaNumber("18 six-flags"));
2366 assertFalse(phoneUtil.isAlphaNumber("1800 123-1234 extension: 1234"));
2367 assertFalse(phoneUtil.isAlphaNumber("+800 1234-1234"));