1 /* 2 * Copyright (C) 2009 Google Inc. 3 * 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 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 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. 15 */ 16 17 package com.android.i18n.phonenumbers; 18 19 import com.android.i18n.phonenumbers.PhoneNumberUtil.PhoneNumberFormat; 20 import com.android.i18n.phonenumbers.Phonemetadata.NumberFormat; 21 import com.android.i18n.phonenumbers.Phonemetadata.PhoneMetadata; 22 import com.android.i18n.phonenumbers.Phonemetadata.PhoneNumberDesc; 23 import com.android.i18n.phonenumbers.Phonenumber.PhoneNumber; 24 import com.android.i18n.phonenumbers.Phonenumber.PhoneNumber.CountryCodeSource; 25 26 import junit.framework.TestCase; 27 28 import java.util.ArrayList; 29 import java.util.List; 30 31 /** 32 * Unit tests for PhoneNumberUtil.java 33 * 34 * Note that these tests use the metadata contained in the files with TEST_META_DATA_FILE_PREFIX, 35 * not the normal metadata files, so should not be used for regression test purposes - these tests 36 * are illustrative only and test functionality. 37 * 38 * @author Shaopeng Jia 39 * @author Lara Rennie 40 */ 41 public class PhoneNumberUtilTest extends TestCase { 42 private PhoneNumberUtil phoneUtil; 43 // This is used by BuildMetadataProtoFromXml. 44 static final String TEST_META_DATA_FILE_PREFIX = 45 "/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting"; 46 47 // Set up some test numbers to re-use. 48 private static final PhoneNumber ALPHA_NUMERIC_NUMBER = 49 new PhoneNumber().setCountryCode(1).setNationalNumber(80074935247L); 50 private static final PhoneNumber AR_MOBILE = 51 new PhoneNumber().setCountryCode(54).setNationalNumber(91187654321L); 52 private static final PhoneNumber AR_NUMBER = 53 new PhoneNumber().setCountryCode(54).setNationalNumber(1187654321); 54 private static final PhoneNumber AU_NUMBER = 55 new PhoneNumber().setCountryCode(61).setNationalNumber(236618300L); 56 private static final PhoneNumber BS_MOBILE = 57 new PhoneNumber().setCountryCode(1).setNationalNumber(2423570000L); 58 private static final PhoneNumber BS_NUMBER = 59 new PhoneNumber().setCountryCode(1).setNationalNumber(2423651234L); 60 // Note that this is the same as the example number for DE in the metadata. 61 private static final PhoneNumber DE_NUMBER = 62 new PhoneNumber().setCountryCode(49).setNationalNumber(30123456L); 63 private static final PhoneNumber DE_SHORT_NUMBER = 64 new PhoneNumber().setCountryCode(49).setNationalNumber(1234L); 65 private static final PhoneNumber GB_MOBILE = 66 new PhoneNumber().setCountryCode(44).setNationalNumber(7912345678L); 67 private static final PhoneNumber GB_NUMBER = 68 new PhoneNumber().setCountryCode(44).setNationalNumber(2070313000L); 69 private static final PhoneNumber IT_MOBILE = 70 new PhoneNumber().setCountryCode(39).setNationalNumber(345678901L); 71 private static final PhoneNumber IT_NUMBER = 72 new PhoneNumber().setCountryCode(39).setNationalNumber(236618300L). 73 setItalianLeadingZero(true); 74 // Numbers to test the formatting rules from Mexico. 75 private static final PhoneNumber MX_MOBILE1 = 76 new PhoneNumber().setCountryCode(52).setNationalNumber(12345678900L); 77 private static final PhoneNumber MX_MOBILE2 = 78 new PhoneNumber().setCountryCode(52).setNationalNumber(15512345678L); 79 private static final PhoneNumber MX_NUMBER1 = 80 new PhoneNumber().setCountryCode(52).setNationalNumber(3312345678L); 81 private static final PhoneNumber MX_NUMBER2 = 82 new PhoneNumber().setCountryCode(52).setNationalNumber(8211234567L); 83 private static final PhoneNumber NZ_NUMBER = 84 new PhoneNumber().setCountryCode(64).setNationalNumber(33316005L); 85 private static final PhoneNumber SG_NUMBER = 86 new PhoneNumber().setCountryCode(65).setNationalNumber(65218000L); 87 // A too-long and hence invalid US number. 88 private static final PhoneNumber US_LONG_NUMBER = 89 new PhoneNumber().setCountryCode(1).setNationalNumber(65025300001L); 90 private static final PhoneNumber US_NUMBER = 91 new PhoneNumber().setCountryCode(1).setNationalNumber(6502530000L); 92 private static final PhoneNumber US_PREMIUM = 93 new PhoneNumber().setCountryCode(1).setNationalNumber(9002530000L); 94 // Too short, but still possible US numbers. 95 private static final PhoneNumber US_LOCAL_NUMBER = 96 new PhoneNumber().setCountryCode(1).setNationalNumber(2530000L); 97 private static final PhoneNumber US_SHORT_BY_ONE_NUMBER = 98 new PhoneNumber().setCountryCode(1).setNationalNumber(650253000L); 99 private static final PhoneNumber US_TOLLFREE = 100 new PhoneNumber().setCountryCode(1).setNationalNumber(8002530000L); 101 private static final PhoneNumber US_SPOOF = 102 new PhoneNumber().setCountryCode(1).setNationalNumber(0L); 103 private static final PhoneNumber US_SPOOF_WITH_RAW_INPUT = 104 new PhoneNumber().setCountryCode(1).setNationalNumber(0L) 105 .setRawInput("000-000-0000"); 106 107 // Class containing string constants of region codes for easier testing. 108 private static class RegionCode { 109 static final String AD = "AD"; 110 static final String AO = "AO"; 111 static final String AR = "AR"; 112 static final String AU = "AU"; 113 static final String BS = "BS"; 114 static final String CS = "CS"; 115 static final String DE = "DE"; 116 static final String GB = "GB"; 117 static final String IT = "IT"; 118 static final String KR = "KR"; 119 static final String MX = "MX"; 120 static final String NZ = "NZ"; 121 static final String PL = "PL"; 122 static final String RE = "RE"; 123 static final String SG = "SG"; 124 static final String US = "US"; 125 static final String YT = "YT"; 126 // Official code for the unknown region. 127 static final String ZZ = "ZZ"; 128 } 129 130 public PhoneNumberUtilTest() { 131 phoneUtil = initializePhoneUtilForTesting(); 132 } 133 134 static PhoneNumberUtil initializePhoneUtilForTesting() { 135 PhoneNumberUtil.resetInstance(); 136 return PhoneNumberUtil.getInstance( 137 TEST_META_DATA_FILE_PREFIX, 138 CountryCodeToRegionCodeMapForTesting.getCountryCodeToRegionCodeMap()); 139 } 140 141 public void testGetSupportedRegions() { 142 assertTrue(phoneUtil.getSupportedRegions().size() > 0); 143 } 144 145 public void testGetInstanceLoadUSMetadata() { 146 PhoneMetadata metadata = phoneUtil.getMetadataForRegion(RegionCode.US); 147 assertEquals("US", metadata.getId()); 148 assertEquals(1, metadata.getCountryCode()); 149 assertEquals("011", metadata.getInternationalPrefix()); 150 assertTrue(metadata.hasNationalPrefix()); 151 assertEquals(2, metadata.numberFormatSize()); 152 assertEquals("(\\d{3})(\\d{3})(\\d{4})", 153 metadata.getNumberFormat(1).getPattern()); 154 assertEquals("$1 $2 $3", metadata.getNumberFormat(1).getFormat()); 155 assertEquals("[13-689]\\d{9}|2[0-35-9]\\d{8}", 156 metadata.getGeneralDesc().getNationalNumberPattern()); 157 assertEquals("\\d{7}(?:\\d{3})?", metadata.getGeneralDesc().getPossibleNumberPattern()); 158 assertTrue(metadata.getGeneralDesc().exactlySameAs(metadata.getFixedLine())); 159 assertEquals("\\d{10}", metadata.getTollFree().getPossibleNumberPattern()); 160 assertEquals("900\\d{7}", metadata.getPremiumRate().getNationalNumberPattern()); 161 // No shared-cost data is available, so it should be initialised to "NA". 162 assertEquals("NA", metadata.getSharedCost().getNationalNumberPattern()); 163 assertEquals("NA", metadata.getSharedCost().getPossibleNumberPattern()); 164 } 165 166 public void testGetInstanceLoadDEMetadata() { 167 PhoneMetadata metadata = phoneUtil.getMetadataForRegion(RegionCode.DE); 168 assertEquals("DE", metadata.getId()); 169 assertEquals(49, metadata.getCountryCode()); 170 assertEquals("00", metadata.getInternationalPrefix()); 171 assertEquals("0", metadata.getNationalPrefix()); 172 assertEquals(6, metadata.numberFormatSize()); 173 assertEquals(1, metadata.getNumberFormat(5).leadingDigitsPatternSize()); 174 assertEquals("900", metadata.getNumberFormat(5).getLeadingDigitsPattern(0)); 175 assertEquals("(\\d{3})(\\d{3,4})(\\d{4})", 176 metadata.getNumberFormat(5).getPattern()); 177 assertEquals("$1 $2 $3", metadata.getNumberFormat(5).getFormat()); 178 assertEquals("(?:[24-6]\\d{2}|3[03-9]\\d|[789](?:[1-9]\\d|0[2-9]))\\d{1,8}", 179 metadata.getFixedLine().getNationalNumberPattern()); 180 assertEquals("\\d{2,14}", metadata.getFixedLine().getPossibleNumberPattern()); 181 assertEquals("30123456", metadata.getFixedLine().getExampleNumber()); 182 assertEquals("\\d{10}", metadata.getTollFree().getPossibleNumberPattern()); 183 assertEquals("900([135]\\d{6}|9\\d{7})", metadata.getPremiumRate().getNationalNumberPattern()); 184 } 185 186 public void testGetInstanceLoadARMetadata() { 187 PhoneMetadata metadata = phoneUtil.getMetadataForRegion(RegionCode.AR); 188 assertEquals("AR", metadata.getId()); 189 assertEquals(54, metadata.getCountryCode()); 190 assertEquals("00", metadata.getInternationalPrefix()); 191 assertEquals("0", metadata.getNationalPrefix()); 192 assertEquals("0(?:(11|343|3715)15)?", metadata.getNationalPrefixForParsing()); 193 assertEquals("9$1", metadata.getNationalPrefixTransformRule()); 194 assertEquals("$2 15 $3-$4", metadata.getNumberFormat(2).getFormat()); 195 assertEquals("(9)(\\d{4})(\\d{2})(\\d{4})", 196 metadata.getNumberFormat(3).getPattern()); 197 assertEquals("(9)(\\d{4})(\\d{2})(\\d{4})", 198 metadata.getIntlNumberFormat(3).getPattern()); 199 assertEquals("$1 $2 $3 $4", metadata.getIntlNumberFormat(3).getFormat()); 200 } 201 202 public void testIsLeadingZeroPossible() { 203 assertTrue(phoneUtil.isLeadingZeroPossible(39)); // Italy 204 assertFalse(phoneUtil.isLeadingZeroPossible(1)); // USA 205 assertFalse(phoneUtil.isLeadingZeroPossible(800)); // Not in metadata file, just default to 206 // false. 207 } 208 209 public void testGetLengthOfGeographicalAreaCode() { 210 // Google MTV, which has area code "650". 211 assertEquals(3, phoneUtil.getLengthOfGeographicalAreaCode(US_NUMBER)); 212 213 // A North America toll-free number, which has no area code. 214 assertEquals(0, phoneUtil.getLengthOfGeographicalAreaCode(US_TOLLFREE)); 215 216 // Google London, which has area code "20". 217 assertEquals(2, phoneUtil.getLengthOfGeographicalAreaCode(GB_NUMBER)); 218 219 // A UK mobile phone, which has no area code. 220 assertEquals(0, phoneUtil.getLengthOfGeographicalAreaCode(GB_MOBILE)); 221 222 // Google Buenos Aires, which has area code "11". 223 assertEquals(2, phoneUtil.getLengthOfGeographicalAreaCode(AR_NUMBER)); 224 225 // Google Sydney, which has area code "2". 226 assertEquals(1, phoneUtil.getLengthOfGeographicalAreaCode(AU_NUMBER)); 227 228 // Google Singapore. Singapore has no area code and no national prefix. 229 assertEquals(0, phoneUtil.getLengthOfGeographicalAreaCode(SG_NUMBER)); 230 231 // An invalid US number (1 digit shorter), which has no area code. 232 assertEquals(0, phoneUtil.getLengthOfGeographicalAreaCode(US_SHORT_BY_ONE_NUMBER)); 233 } 234 235 public void testGetLengthOfNationalDestinationCode() { 236 // Google MTV, which has national destination code (NDC) "650". 237 assertEquals(3, phoneUtil.getLengthOfNationalDestinationCode(US_NUMBER)); 238 239 // A North America toll-free number, which has NDC "800". 240 assertEquals(3, phoneUtil.getLengthOfNationalDestinationCode(US_TOLLFREE)); 241 242 // Google London, which has NDC "20". 243 assertEquals(2, phoneUtil.getLengthOfNationalDestinationCode(GB_NUMBER)); 244 245 // A UK mobile phone, which has NDC "7912". 246 assertEquals(4, phoneUtil.getLengthOfNationalDestinationCode(GB_MOBILE)); 247 248 // Google Buenos Aires, which has NDC "11". 249 assertEquals(2, phoneUtil.getLengthOfNationalDestinationCode(AR_NUMBER)); 250 251 // An Argentinian mobile which has NDC "911". 252 assertEquals(3, phoneUtil.getLengthOfNationalDestinationCode(AR_MOBILE)); 253 254 // Google Sydney, which has NDC "2". 255 assertEquals(1, phoneUtil.getLengthOfNationalDestinationCode(AU_NUMBER)); 256 257 // Google Singapore, which has NDC "6521". 258 assertEquals(4, phoneUtil.getLengthOfNationalDestinationCode(SG_NUMBER)); 259 260 // An invalid US number (1 digit shorter), which has no NDC. 261 assertEquals(0, phoneUtil.getLengthOfNationalDestinationCode(US_SHORT_BY_ONE_NUMBER)); 262 263 // A number containing an invalid country calling code, which shouldn't have any NDC. 264 PhoneNumber number = new PhoneNumber().setCountryCode(123).setNationalNumber(6502530000L); 265 assertEquals(0, phoneUtil.getLengthOfNationalDestinationCode(number)); 266 } 267 268 public void testGetNationalSignificantNumber() { 269 assertEquals("6502530000", phoneUtil.getNationalSignificantNumber(US_NUMBER)); 270 271 // An Italian mobile number. 272 assertEquals("345678901", phoneUtil.getNationalSignificantNumber(IT_MOBILE)); 273 274 // An Italian fixed line number. 275 assertEquals("0236618300", phoneUtil.getNationalSignificantNumber(IT_NUMBER)); 276 } 277 278 public void testGetExampleNumber() { 279 assertEquals(DE_NUMBER, phoneUtil.getExampleNumber(RegionCode.DE)); 280 281 assertEquals(DE_NUMBER, 282 phoneUtil.getExampleNumberForType(RegionCode.DE, 283 PhoneNumberUtil.PhoneNumberType.FIXED_LINE)); 284 assertEquals(null, 285 phoneUtil.getExampleNumberForType(RegionCode.DE, 286 PhoneNumberUtil.PhoneNumberType.MOBILE)); 287 // For the US, the example number is placed under general description, and hence should be used 288 // for both fixed line and mobile, so neither of these should return null. 289 assertNotNull(phoneUtil.getExampleNumberForType(RegionCode.US, 290 PhoneNumberUtil.PhoneNumberType.FIXED_LINE)); 291 assertNotNull(phoneUtil.getExampleNumberForType(RegionCode.US, 292 PhoneNumberUtil.PhoneNumberType.MOBILE)); 293 // CS is an invalid region, so we have no data for it. 294 assertNull(phoneUtil.getExampleNumberForType(RegionCode.CS, 295 PhoneNumberUtil.PhoneNumberType.MOBILE)); 296 } 297 298 public void testConvertAlphaCharactersInNumber() { 299 String input = "1800-ABC-DEF"; 300 // Alpha chars are converted to digits; everything else is left untouched. 301 String expectedOutput = "1800-222-333"; 302 assertEquals(expectedOutput, PhoneNumberUtil.convertAlphaCharactersInNumber(input)); 303 } 304 305 public void testNormaliseRemovePunctuation() { 306 String inputNumber = "034-56&+#234"; 307 String expectedOutput = "03456234"; 308 assertEquals("Conversion did not correctly remove punctuation", 309 expectedOutput, 310 PhoneNumberUtil.normalize(inputNumber)); 311 } 312 313 public void testNormaliseReplaceAlphaCharacters() { 314 String inputNumber = "034-I-am-HUNGRY"; 315 String expectedOutput = "034426486479"; 316 assertEquals("Conversion did not correctly replace alpha characters", 317 expectedOutput, 318 PhoneNumberUtil.normalize(inputNumber)); 319 } 320 321 public void testNormaliseOtherDigits() { 322 String inputNumber = "\uFF125\u0665"; 323 String expectedOutput = "255"; 324 assertEquals("Conversion did not correctly replace non-latin digits", 325 expectedOutput, 326 PhoneNumberUtil.normalize(inputNumber)); 327 // Eastern-Arabic digits. 328 inputNumber = "\u06F52\u06F0"; 329 expectedOutput = "520"; 330 assertEquals("Conversion did not correctly replace non-latin digits", 331 expectedOutput, 332 PhoneNumberUtil.normalize(inputNumber)); 333 } 334 335 public void testNormaliseStripAlphaCharacters() { 336 String inputNumber = "034-56&+a#234"; 337 String expectedOutput = "03456234"; 338 assertEquals("Conversion did not correctly remove alpha character", 339 expectedOutput, 340 PhoneNumberUtil.normalizeDigitsOnly(inputNumber)); 341 } 342 343 public void testFormatUSNumber() { 344 assertEquals("650 253 0000", phoneUtil.format(US_NUMBER, PhoneNumberFormat.NATIONAL)); 345 assertEquals("+1 650 253 0000", phoneUtil.format(US_NUMBER, PhoneNumberFormat.INTERNATIONAL)); 346 347 assertEquals("800 253 0000", phoneUtil.format(US_TOLLFREE, PhoneNumberFormat.NATIONAL)); 348 assertEquals("+1 800 253 0000", phoneUtil.format(US_TOLLFREE, PhoneNumberFormat.INTERNATIONAL)); 349 350 assertEquals("900 253 0000", phoneUtil.format(US_PREMIUM, PhoneNumberFormat.NATIONAL)); 351 assertEquals("+1 900 253 0000", phoneUtil.format(US_PREMIUM, PhoneNumberFormat.INTERNATIONAL)); 352 assertEquals("+1-900-253-0000", phoneUtil.format(US_PREMIUM, PhoneNumberFormat.RFC3966)); 353 // Numbers with all zeros in the national number part will be formatted by using the raw_input 354 // if that is available no matter which format is specified. 355 assertEquals("000-000-0000", 356 phoneUtil.format(US_SPOOF_WITH_RAW_INPUT, PhoneNumberFormat.NATIONAL)); 357 assertEquals("0", phoneUtil.format(US_SPOOF, PhoneNumberFormat.NATIONAL)); 358 } 359 360 public void testFormatBSNumber() { 361 assertEquals("242 365 1234", phoneUtil.format(BS_NUMBER, PhoneNumberFormat.NATIONAL)); 362 assertEquals("+1 242 365 1234", phoneUtil.format(BS_NUMBER, PhoneNumberFormat.INTERNATIONAL)); 363 } 364 365 public void testFormatGBNumber() { 366 assertEquals("(020) 7031 3000", phoneUtil.format(GB_NUMBER, PhoneNumberFormat.NATIONAL)); 367 assertEquals("+44 20 7031 3000", phoneUtil.format(GB_NUMBER, PhoneNumberFormat.INTERNATIONAL)); 368 369 assertEquals("(07912) 345 678", phoneUtil.format(GB_MOBILE, PhoneNumberFormat.NATIONAL)); 370 assertEquals("+44 7912 345 678", phoneUtil.format(GB_MOBILE, PhoneNumberFormat.INTERNATIONAL)); 371 } 372 373 public void testFormatDENumber() { 374 PhoneNumber deNumber = new PhoneNumber(); 375 deNumber.setCountryCode(49).setNationalNumber(301234L); 376 assertEquals("030/1234", phoneUtil.format(deNumber, PhoneNumberFormat.NATIONAL)); 377 assertEquals("+49 30/1234", phoneUtil.format(deNumber, PhoneNumberFormat.INTERNATIONAL)); 378 assertEquals("+49-30-1234", phoneUtil.format(deNumber, PhoneNumberFormat.RFC3966)); 379 380 deNumber.clear(); 381 deNumber.setCountryCode(49).setNationalNumber(291123L); 382 assertEquals("0291 123", phoneUtil.format(deNumber, PhoneNumberFormat.NATIONAL)); 383 assertEquals("+49 291 123", phoneUtil.format(deNumber, PhoneNumberFormat.INTERNATIONAL)); 384 385 deNumber.clear(); 386 deNumber.setCountryCode(49).setNationalNumber(29112345678L); 387 assertEquals("0291 12345678", phoneUtil.format(deNumber, PhoneNumberFormat.NATIONAL)); 388 assertEquals("+49 291 12345678", phoneUtil.format(deNumber, PhoneNumberFormat.INTERNATIONAL)); 389 390 deNumber.clear(); 391 deNumber.setCountryCode(49).setNationalNumber(912312345L); 392 assertEquals("09123 12345", phoneUtil.format(deNumber, PhoneNumberFormat.NATIONAL)); 393 assertEquals("+49 9123 12345", phoneUtil.format(deNumber, PhoneNumberFormat.INTERNATIONAL)); 394 deNumber.clear(); 395 deNumber.setCountryCode(49).setNationalNumber(80212345L); 396 assertEquals("08021 2345", phoneUtil.format(deNumber, PhoneNumberFormat.NATIONAL)); 397 assertEquals("+49 8021 2345", phoneUtil.format(deNumber, PhoneNumberFormat.INTERNATIONAL)); 398 // Note this number is correctly formatted without national prefix. Most of the numbers that 399 // are treated as invalid numbers by the library are short numbers, and they are usually not 400 // dialed with national prefix. 401 assertEquals("1234", phoneUtil.format(DE_SHORT_NUMBER, PhoneNumberFormat.NATIONAL)); 402 assertEquals("+49 1234", phoneUtil.format(DE_SHORT_NUMBER, PhoneNumberFormat.INTERNATIONAL)); 403 404 deNumber.clear(); 405 deNumber.setCountryCode(49).setNationalNumber(41341234); 406 assertEquals("04134 1234", phoneUtil.format(deNumber, PhoneNumberFormat.NATIONAL)); 407 } 408 409 public void testFormatITNumber() { 410 assertEquals("02 3661 8300", phoneUtil.format(IT_NUMBER, PhoneNumberFormat.NATIONAL)); 411 assertEquals("+39 02 3661 8300", phoneUtil.format(IT_NUMBER, PhoneNumberFormat.INTERNATIONAL)); 412 assertEquals("+390236618300", phoneUtil.format(IT_NUMBER, PhoneNumberFormat.E164)); 413 414 assertEquals("345 678 901", phoneUtil.format(IT_MOBILE, PhoneNumberFormat.NATIONAL)); 415 assertEquals("+39 345 678 901", phoneUtil.format(IT_MOBILE, PhoneNumberFormat.INTERNATIONAL)); 416 assertEquals("+39345678901", phoneUtil.format(IT_MOBILE, PhoneNumberFormat.E164)); 417 } 418 419 public void testFormatAUNumber() { 420 assertEquals("02 3661 8300", phoneUtil.format(AU_NUMBER, PhoneNumberFormat.NATIONAL)); 421 assertEquals("+61 2 3661 8300", phoneUtil.format(AU_NUMBER, PhoneNumberFormat.INTERNATIONAL)); 422 assertEquals("+61236618300", phoneUtil.format(AU_NUMBER, PhoneNumberFormat.E164)); 423 424 PhoneNumber auNumber = new PhoneNumber().setCountryCode(61).setNationalNumber(1800123456L); 425 assertEquals("1800 123 456", phoneUtil.format(auNumber, PhoneNumberFormat.NATIONAL)); 426 assertEquals("+61 1800 123 456", phoneUtil.format(auNumber, PhoneNumberFormat.INTERNATIONAL)); 427 assertEquals("+611800123456", phoneUtil.format(auNumber, PhoneNumberFormat.E164)); 428 } 429 430 public void testFormatARNumber() { 431 assertEquals("011 8765-4321", phoneUtil.format(AR_NUMBER, PhoneNumberFormat.NATIONAL)); 432 assertEquals("+54 11 8765-4321", phoneUtil.format(AR_NUMBER, PhoneNumberFormat.INTERNATIONAL)); 433 assertEquals("+541187654321", phoneUtil.format(AR_NUMBER, PhoneNumberFormat.E164)); 434 435 assertEquals("011 15 8765-4321", phoneUtil.format(AR_MOBILE, PhoneNumberFormat.NATIONAL)); 436 assertEquals("+54 9 11 8765 4321", phoneUtil.format(AR_MOBILE, 437 PhoneNumberFormat.INTERNATIONAL)); 438 assertEquals("+5491187654321", phoneUtil.format(AR_MOBILE, PhoneNumberFormat.E164)); 439 } 440 441 public void testFormatMXNumber() { 442 assertEquals("045 234 567 8900", phoneUtil.format(MX_MOBILE1, PhoneNumberFormat.NATIONAL)); 443 assertEquals("+52 1 234 567 8900", phoneUtil.format( 444 MX_MOBILE1, PhoneNumberFormat.INTERNATIONAL)); 445 assertEquals("+5212345678900", phoneUtil.format(MX_MOBILE1, PhoneNumberFormat.E164)); 446 447 assertEquals("045 55 1234 5678", phoneUtil.format(MX_MOBILE2, PhoneNumberFormat.NATIONAL)); 448 assertEquals("+52 1 55 1234 5678", phoneUtil.format( 449 MX_MOBILE2, PhoneNumberFormat.INTERNATIONAL)); 450 assertEquals("+5215512345678", phoneUtil.format(MX_MOBILE2, PhoneNumberFormat.E164)); 451 452 assertEquals("01 33 1234 5678", phoneUtil.format(MX_NUMBER1, PhoneNumberFormat.NATIONAL)); 453 assertEquals("+52 33 1234 5678", phoneUtil.format(MX_NUMBER1, PhoneNumberFormat.INTERNATIONAL)); 454 assertEquals("+523312345678", phoneUtil.format(MX_NUMBER1, PhoneNumberFormat.E164)); 455 456 assertEquals("01 821 123 4567", phoneUtil.format(MX_NUMBER2, PhoneNumberFormat.NATIONAL)); 457 assertEquals("+52 821 123 4567", phoneUtil.format(MX_NUMBER2, PhoneNumberFormat.INTERNATIONAL)); 458 assertEquals("+528211234567", phoneUtil.format(MX_NUMBER2, PhoneNumberFormat.E164)); 459 } 460 461 public void testFormatOutOfCountryCallingNumber() { 462 assertEquals("00 1 900 253 0000", 463 phoneUtil.formatOutOfCountryCallingNumber(US_PREMIUM, RegionCode.DE)); 464 465 assertEquals("1 650 253 0000", 466 phoneUtil.formatOutOfCountryCallingNumber(US_NUMBER, RegionCode.BS)); 467 468 assertEquals("0~0 1 650 253 0000", 469 phoneUtil.formatOutOfCountryCallingNumber(US_NUMBER, RegionCode.PL)); 470 471 assertEquals("011 44 7912 345 678", 472 phoneUtil.formatOutOfCountryCallingNumber(GB_MOBILE, RegionCode.US)); 473 474 assertEquals("00 49 1234", 475 phoneUtil.formatOutOfCountryCallingNumber(DE_SHORT_NUMBER, RegionCode.GB)); 476 // Note this number is correctly formatted without national prefix. Most of the numbers that 477 // are treated as invalid numbers by the library are short numbers, and they are usually not 478 // dialed with national prefix. 479 assertEquals("1234", phoneUtil.formatOutOfCountryCallingNumber(DE_SHORT_NUMBER, RegionCode.DE)); 480 481 assertEquals("011 39 02 3661 8300", 482 phoneUtil.formatOutOfCountryCallingNumber(IT_NUMBER, RegionCode.US)); 483 assertEquals("02 3661 8300", 484 phoneUtil.formatOutOfCountryCallingNumber(IT_NUMBER, RegionCode.IT)); 485 assertEquals("+39 02 3661 8300", 486 phoneUtil.formatOutOfCountryCallingNumber(IT_NUMBER, RegionCode.SG)); 487 488 assertEquals("6521 8000", 489 phoneUtil.formatOutOfCountryCallingNumber(SG_NUMBER, RegionCode.SG)); 490 491 assertEquals("011 54 9 11 8765 4321", 492 phoneUtil.formatOutOfCountryCallingNumber(AR_MOBILE, RegionCode.US)); 493 494 PhoneNumber arNumberWithExtn = new PhoneNumber().mergeFrom(AR_MOBILE).setExtension("1234"); 495 assertEquals("011 54 9 11 8765 4321 ext. 1234", 496 phoneUtil.formatOutOfCountryCallingNumber(arNumberWithExtn, RegionCode.US)); 497 assertEquals("0011 54 9 11 8765 4321 ext. 1234", 498 phoneUtil.formatOutOfCountryCallingNumber(arNumberWithExtn, RegionCode.AU)); 499 assertEquals("011 15 8765-4321 ext. 1234", 500 phoneUtil.formatOutOfCountryCallingNumber(arNumberWithExtn, RegionCode.AR)); 501 } 502 503 public void testFormatOutOfCountryWithPreferredIntlPrefix() { 504 // This should use 0011, since that is the preferred international prefix (both 0011 and 0012 505 // are accepted as possible international prefixes in our test metadta.) 506 assertEquals("0011 39 02 3661 8300", 507 phoneUtil.formatOutOfCountryCallingNumber(IT_NUMBER, RegionCode.AU)); 508 } 509 510 public void testFormatOutOfCountryKeepingAlphaChars() { 511 PhoneNumber alphaNumericNumber = new PhoneNumber(); 512 alphaNumericNumber.setCountryCode(1).setNationalNumber(8007493524L) 513 .setRawInput("1800 six-flag"); 514 assertEquals("0011 1 800 SIX-FLAG", 515 phoneUtil.formatOutOfCountryKeepingAlphaChars(alphaNumericNumber, RegionCode.AU)); 516 517 alphaNumericNumber.setRawInput("1-800-SIX-flag"); 518 assertEquals("0011 1 800-SIX-FLAG", 519 phoneUtil.formatOutOfCountryKeepingAlphaChars(alphaNumericNumber, RegionCode.AU)); 520 521 alphaNumericNumber.setRawInput("Call us from UK: 00 1 800 SIX-flag"); 522 assertEquals("0011 1 800 SIX-FLAG", 523 phoneUtil.formatOutOfCountryKeepingAlphaChars(alphaNumericNumber, RegionCode.AU)); 524 525 alphaNumericNumber.setRawInput("800 SIX-flag"); 526 assertEquals("0011 1 800 SIX-FLAG", 527 phoneUtil.formatOutOfCountryKeepingAlphaChars(alphaNumericNumber, RegionCode.AU)); 528 529 // Formatting from within the NANPA region. 530 assertEquals("1 800 SIX-FLAG", 531 phoneUtil.formatOutOfCountryKeepingAlphaChars(alphaNumericNumber, RegionCode.US)); 532 533 assertEquals("1 800 SIX-FLAG", 534 phoneUtil.formatOutOfCountryKeepingAlphaChars(alphaNumericNumber, RegionCode.BS)); 535 536 // Testing that if the raw input doesn't exist, it is formatted using 537 // formatOutOfCountryCallingNumber. 538 alphaNumericNumber.clearRawInput(); 539 assertEquals("00 1 800 749 3524", 540 phoneUtil.formatOutOfCountryKeepingAlphaChars(alphaNumericNumber, RegionCode.DE)); 541 542 // Testing AU alpha number formatted from Australia. 543 alphaNumericNumber.setCountryCode(61).setNationalNumber(827493524L) 544 .setRawInput("+61 82749-FLAG"); 545 // This number should have the national prefix fixed. 546 assertEquals("082749-FLAG", 547 phoneUtil.formatOutOfCountryKeepingAlphaChars(alphaNumericNumber, RegionCode.AU)); 548 549 alphaNumericNumber.setRawInput("082749-FLAG"); 550 assertEquals("082749-FLAG", 551 phoneUtil.formatOutOfCountryKeepingAlphaChars(alphaNumericNumber, RegionCode.AU)); 552 553 alphaNumericNumber.setNationalNumber(18007493524L).setRawInput("1-800-SIX-flag"); 554 // This number should not have the national prefix prefixed, in accordance with the override for 555 // this specific formatting rule. 556 assertEquals("1-800-SIX-FLAG", 557 phoneUtil.formatOutOfCountryKeepingAlphaChars(alphaNumericNumber, RegionCode.AU)); 558 559 // The metadata should not be permanently changed, since we copied it before modifying patterns. 560 // Here we check this. 561 alphaNumericNumber.setNationalNumber(1800749352L); 562 assertEquals("1800 749 352", 563 phoneUtil.formatOutOfCountryCallingNumber(alphaNumericNumber, RegionCode.AU)); 564 565 // Testing a region with multiple international prefixes. 566 assertEquals("+61 1-800-SIX-FLAG", 567 phoneUtil.formatOutOfCountryKeepingAlphaChars(alphaNumericNumber, RegionCode.SG)); 568 569 // Testing the case with an invalid country calling code. 570 alphaNumericNumber.setCountryCode(0).setNationalNumber(18007493524L) 571 .setRawInput("1-800-SIX-flag"); 572 // Uses the raw input only. 573 assertEquals("1-800-SIX-flag", 574 phoneUtil.formatOutOfCountryKeepingAlphaChars(alphaNumericNumber, RegionCode.DE)); 575 576 // Testing the case of an invalid alpha number. 577 alphaNumericNumber.setCountryCode(1).setNationalNumber(80749L).setRawInput("180-SIX"); 578 // No country-code stripping can be done. 579 assertEquals("00 1 180-SIX", 580 phoneUtil.formatOutOfCountryKeepingAlphaChars(alphaNumericNumber, RegionCode.DE)); 581 } 582 583 public void testFormatWithCarrierCode() { 584 // We only support this for AR in our test metadata, and only for mobile numbers starting with 585 // certain values. 586 PhoneNumber arMobile = new PhoneNumber().setCountryCode(54).setNationalNumber(92234654321L); 587 assertEquals("02234 65-4321", phoneUtil.format(arMobile, PhoneNumberFormat.NATIONAL)); 588 // Here we force 14 as the carrier code. 589 assertEquals("02234 14 65-4321", 590 phoneUtil.formatNationalNumberWithCarrierCode(arMobile, "14")); 591 // Here we force the number to be shown with no carrier code. 592 assertEquals("02234 65-4321", 593 phoneUtil.formatNationalNumberWithCarrierCode(arMobile, "")); 594 // Here the international rule is used, so no carrier code should be present. 595 assertEquals("+5492234654321", phoneUtil.format(arMobile, PhoneNumberFormat.E164)); 596 // We don't support this for the US so there should be no change. 597 assertEquals("650 253 0000", phoneUtil.formatNationalNumberWithCarrierCode(US_NUMBER, "15")); 598 } 599 600 public void testFormatWithPreferredCarrierCode() { 601 // We only support this for AR in our test metadata. 602 PhoneNumber arNumber = new PhoneNumber(); 603 arNumber.setCountryCode(54).setNationalNumber(91234125678L); 604 // Test formatting with no preferred carrier code stored in the number itself. 605 assertEquals("01234 15 12-5678", 606 phoneUtil.formatNationalNumberWithPreferredCarrierCode(arNumber, "15")); 607 assertEquals("01234 12-5678", 608 phoneUtil.formatNationalNumberWithPreferredCarrierCode(arNumber, "")); 609 // Test formatting with preferred carrier code present. 610 arNumber.setPreferredDomesticCarrierCode("19"); 611 assertEquals("01234 12-5678", phoneUtil.format(arNumber, PhoneNumberFormat.NATIONAL)); 612 assertEquals("01234 19 12-5678", 613 phoneUtil.formatNationalNumberWithPreferredCarrierCode(arNumber, "15")); 614 assertEquals("01234 19 12-5678", 615 phoneUtil.formatNationalNumberWithPreferredCarrierCode(arNumber, "")); 616 // When the preferred_domestic_carrier_code is present (even when it contains an empty string), 617 // use it instead of the default carrier code passed in. 618 arNumber.setPreferredDomesticCarrierCode(""); 619 assertEquals("01234 12-5678", 620 phoneUtil.formatNationalNumberWithPreferredCarrierCode(arNumber, "15")); 621 // We don't support this for the US so there should be no change. 622 PhoneNumber usNumber = new PhoneNumber(); 623 usNumber.setCountryCode(1).setNationalNumber(4241231234L).setPreferredDomesticCarrierCode("99"); 624 assertEquals("424 123 1234", phoneUtil.format(usNumber, PhoneNumberFormat.NATIONAL)); 625 assertEquals("424 123 1234", 626 phoneUtil.formatNationalNumberWithPreferredCarrierCode(usNumber, "15")); 627 } 628 629 public void testFormatByPattern() { 630 NumberFormat newNumFormat = new NumberFormat(); 631 newNumFormat.setPattern("(\\d{3})(\\d{3})(\\d{4})"); 632 newNumFormat.setFormat("($1) $2-$3"); 633 List<NumberFormat> newNumberFormats = new ArrayList<NumberFormat>(); 634 newNumberFormats.add(newNumFormat); 635 636 assertEquals("(650) 253-0000", phoneUtil.formatByPattern(US_NUMBER, PhoneNumberFormat.NATIONAL, 637 newNumberFormats)); 638 assertEquals("+1 (650) 253-0000", phoneUtil.formatByPattern(US_NUMBER, 639 PhoneNumberFormat.INTERNATIONAL, 640 newNumberFormats)); 641 642 // $NP is set to '1' for the US. Here we check that for other NANPA countries the US rules are 643 // followed. 644 newNumFormat.setNationalPrefixFormattingRule("$NP ($FG)"); 645 newNumFormat.setFormat("$1 $2-$3"); 646 assertEquals("1 (242) 365-1234", 647 phoneUtil.formatByPattern(BS_NUMBER, PhoneNumberFormat.NATIONAL, 648 newNumberFormats)); 649 assertEquals("+1 242 365-1234", 650 phoneUtil.formatByPattern(BS_NUMBER, PhoneNumberFormat.INTERNATIONAL, 651 newNumberFormats)); 652 653 newNumFormat.setPattern("(\\d{2})(\\d{5})(\\d{3})"); 654 newNumFormat.setFormat("$1-$2 $3"); 655 newNumberFormats.set(0, newNumFormat); 656 657 assertEquals("02-36618 300", 658 phoneUtil.formatByPattern(IT_NUMBER, PhoneNumberFormat.NATIONAL, 659 newNumberFormats)); 660 assertEquals("+39 02-36618 300", 661 phoneUtil.formatByPattern(IT_NUMBER, PhoneNumberFormat.INTERNATIONAL, 662 newNumberFormats)); 663 664 newNumFormat.setNationalPrefixFormattingRule("$NP$FG"); 665 newNumFormat.setPattern("(\\d{2})(\\d{4})(\\d{4})"); 666 newNumFormat.setFormat("$1 $2 $3"); 667 newNumberFormats.set(0, newNumFormat); 668 assertEquals("020 7031 3000", 669 phoneUtil.formatByPattern(GB_NUMBER, PhoneNumberFormat.NATIONAL, 670 newNumberFormats)); 671 672 newNumFormat.setNationalPrefixFormattingRule("($NP$FG)"); 673 assertEquals("(020) 7031 3000", 674 phoneUtil.formatByPattern(GB_NUMBER, PhoneNumberFormat.NATIONAL, 675 newNumberFormats)); 676 677 newNumFormat.setNationalPrefixFormattingRule(""); 678 assertEquals("20 7031 3000", 679 phoneUtil.formatByPattern(GB_NUMBER, PhoneNumberFormat.NATIONAL, 680 newNumberFormats)); 681 682 assertEquals("+44 20 7031 3000", 683 phoneUtil.formatByPattern(GB_NUMBER, PhoneNumberFormat.INTERNATIONAL, 684 newNumberFormats)); 685 } 686 687 public void testFormatE164Number() { 688 assertEquals("+16502530000", phoneUtil.format(US_NUMBER, PhoneNumberFormat.E164)); 689 assertEquals("+4930123456", phoneUtil.format(DE_NUMBER, PhoneNumberFormat.E164)); 690 } 691 692 public void testFormatNumberWithExtension() { 693 PhoneNumber nzNumber = new PhoneNumber().mergeFrom(NZ_NUMBER).setExtension("1234"); 694 // Uses default extension prefix: 695 assertEquals("03-331 6005 ext. 1234", phoneUtil.format(nzNumber, PhoneNumberFormat.NATIONAL)); 696 // Uses RFC 3966 syntax. 697 assertEquals("+64-3-331-6005;ext=1234", phoneUtil.format(nzNumber, PhoneNumberFormat.RFC3966)); 698 // Extension prefix overridden in the territory information for the US: 699 PhoneNumber usNumberWithExtension = new PhoneNumber().mergeFrom(US_NUMBER).setExtension("4567"); 700 assertEquals("650 253 0000 extn. 4567", phoneUtil.format(usNumberWithExtension, 701 PhoneNumberFormat.NATIONAL)); 702 } 703 704 public void testFormatUsingOriginalNumberFormat() throws Exception { 705 PhoneNumber number1 = phoneUtil.parseAndKeepRawInput("+442087654321", RegionCode.GB); 706 assertEquals("+44 20 8765 4321", phoneUtil.formatInOriginalFormat(number1, RegionCode.GB)); 707 708 PhoneNumber number2 = phoneUtil.parseAndKeepRawInput("02087654321", RegionCode.GB); 709 assertEquals("(020) 8765 4321", phoneUtil.formatInOriginalFormat(number2, RegionCode.GB)); 710 711 PhoneNumber number3 = phoneUtil.parseAndKeepRawInput("011442087654321", RegionCode.US); 712 assertEquals("011 44 20 8765 4321", phoneUtil.formatInOriginalFormat(number3, RegionCode.US)); 713 714 PhoneNumber number4 = phoneUtil.parseAndKeepRawInput("442087654321", RegionCode.GB); 715 assertEquals("44 20 8765 4321", phoneUtil.formatInOriginalFormat(number4, RegionCode.GB)); 716 717 PhoneNumber number5 = phoneUtil.parse("+442087654321", RegionCode.GB); 718 assertEquals("(020) 8765 4321", phoneUtil.formatInOriginalFormat(number5, RegionCode.GB)); 719 720 // This number is valid, but we don't have a formatting pattern for it. Fall back to the raw 721 // input. 722 PhoneNumber number8 = phoneUtil.parseAndKeepRawInput("02-4567-8900", RegionCode.KR); 723 assertEquals("02-4567-8900", phoneUtil.formatInOriginalFormat(number8, RegionCode.KR)); 724 } 725 726 public void testIsPremiumRate() { 727 assertEquals(PhoneNumberUtil.PhoneNumberType.PREMIUM_RATE, phoneUtil.getNumberType(US_PREMIUM)); 728 729 PhoneNumber premiumRateNumber = new PhoneNumber(); 730 premiumRateNumber.setCountryCode(39).setNationalNumber(892123L); 731 assertEquals(PhoneNumberUtil.PhoneNumberType.PREMIUM_RATE, 732 phoneUtil.getNumberType(premiumRateNumber)); 733 734 premiumRateNumber.clear(); 735 premiumRateNumber.setCountryCode(44).setNationalNumber(9187654321L); 736 assertEquals(PhoneNumberUtil.PhoneNumberType.PREMIUM_RATE, 737 phoneUtil.getNumberType(premiumRateNumber)); 738 739 premiumRateNumber.clear(); 740 premiumRateNumber.setCountryCode(49).setNationalNumber(9001654321L); 741 assertEquals(PhoneNumberUtil.PhoneNumberType.PREMIUM_RATE, 742 phoneUtil.getNumberType(premiumRateNumber)); 743 744 premiumRateNumber.clear(); 745 premiumRateNumber.setCountryCode(49).setNationalNumber(90091234567L); 746 assertEquals(PhoneNumberUtil.PhoneNumberType.PREMIUM_RATE, 747 phoneUtil.getNumberType(premiumRateNumber)); 748 } 749 750 public void testIsTollFree() { 751 PhoneNumber tollFreeNumber = new PhoneNumber(); 752 753 tollFreeNumber.setCountryCode(1).setNationalNumber(8881234567L); 754 assertEquals(PhoneNumberUtil.PhoneNumberType.TOLL_FREE, 755 phoneUtil.getNumberType(tollFreeNumber)); 756 757 tollFreeNumber.clear(); 758 tollFreeNumber.setCountryCode(39).setNationalNumber(803123L); 759 assertEquals(PhoneNumberUtil.PhoneNumberType.TOLL_FREE, 760 phoneUtil.getNumberType(tollFreeNumber)); 761 762 tollFreeNumber.clear(); 763 tollFreeNumber.setCountryCode(44).setNationalNumber(8012345678L); 764 assertEquals(PhoneNumberUtil.PhoneNumberType.TOLL_FREE, 765 phoneUtil.getNumberType(tollFreeNumber)); 766 767 tollFreeNumber.clear(); 768 tollFreeNumber.setCountryCode(49).setNationalNumber(8001234567L); 769 assertEquals(PhoneNumberUtil.PhoneNumberType.TOLL_FREE, 770 phoneUtil.getNumberType(tollFreeNumber)); 771 } 772 773 public void testIsMobile() { 774 assertEquals(PhoneNumberUtil.PhoneNumberType.MOBILE, phoneUtil.getNumberType(BS_MOBILE)); 775 assertEquals(PhoneNumberUtil.PhoneNumberType.MOBILE, phoneUtil.getNumberType(GB_MOBILE)); 776 assertEquals(PhoneNumberUtil.PhoneNumberType.MOBILE, phoneUtil.getNumberType(IT_MOBILE)); 777 assertEquals(PhoneNumberUtil.PhoneNumberType.MOBILE, phoneUtil.getNumberType(AR_MOBILE)); 778 779 PhoneNumber mobileNumber = new PhoneNumber(); 780 mobileNumber.setCountryCode(49).setNationalNumber(15123456789L); 781 assertEquals(PhoneNumberUtil.PhoneNumberType.MOBILE, phoneUtil.getNumberType(mobileNumber)); 782 } 783 784 public void testIsFixedLine() { 785 assertEquals(PhoneNumberUtil.PhoneNumberType.FIXED_LINE, phoneUtil.getNumberType(BS_NUMBER)); 786 assertEquals(PhoneNumberUtil.PhoneNumberType.FIXED_LINE, phoneUtil.getNumberType(IT_NUMBER)); 787 assertEquals(PhoneNumberUtil.PhoneNumberType.FIXED_LINE, phoneUtil.getNumberType(GB_NUMBER)); 788 assertEquals(PhoneNumberUtil.PhoneNumberType.FIXED_LINE, phoneUtil.getNumberType(DE_NUMBER)); 789 } 790 791 public void testIsFixedLineAndMobile() { 792 assertEquals(PhoneNumberUtil.PhoneNumberType.FIXED_LINE_OR_MOBILE, 793 phoneUtil.getNumberType(US_NUMBER)); 794 795 PhoneNumber fixedLineAndMobileNumber = new PhoneNumber(). 796 setCountryCode(54).setNationalNumber(1987654321L); 797 assertEquals(PhoneNumberUtil.PhoneNumberType.FIXED_LINE_OR_MOBILE, 798 phoneUtil.getNumberType(fixedLineAndMobileNumber)); 799 } 800 801 public void testIsSharedCost() { 802 PhoneNumber gbNumber = new PhoneNumber(); 803 gbNumber.setCountryCode(44).setNationalNumber(8431231234L); 804 assertEquals(PhoneNumberUtil.PhoneNumberType.SHARED_COST, phoneUtil.getNumberType(gbNumber)); 805 } 806 807 public void testIsVoip() { 808 PhoneNumber gbNumber = new PhoneNumber(); 809 gbNumber.setCountryCode(44).setNationalNumber(5631231234L); 810 assertEquals(PhoneNumberUtil.PhoneNumberType.VOIP, phoneUtil.getNumberType(gbNumber)); 811 } 812 813 public void testIsPersonalNumber() { 814 PhoneNumber gbNumber = new PhoneNumber(); 815 gbNumber.setCountryCode(44).setNationalNumber(7031231234L); 816 assertEquals(PhoneNumberUtil.PhoneNumberType.PERSONAL_NUMBER, 817 phoneUtil.getNumberType(gbNumber)); 818 } 819 820 public void testIsUnknown() { 821 // Invalid numbers should be of type UNKNOWN. 822 assertEquals(PhoneNumberUtil.PhoneNumberType.UNKNOWN, phoneUtil.getNumberType(US_LOCAL_NUMBER)); 823 } 824 825 public void testIsValidNumber() { 826 assertTrue(phoneUtil.isValidNumber(US_NUMBER)); 827 assertTrue(phoneUtil.isValidNumber(IT_NUMBER)); 828 assertTrue(phoneUtil.isValidNumber(GB_MOBILE)); 829 830 PhoneNumber nzNumber = new PhoneNumber().setCountryCode(64).setNationalNumber(21387835L); 831 assertTrue(phoneUtil.isValidNumber(nzNumber)); 832 } 833 834 public void testIsValidForRegion() { 835 // This number is valid for the Bahamas, but is not a valid US number. 836 assertTrue(phoneUtil.isValidNumber(BS_NUMBER)); 837 assertTrue(phoneUtil.isValidNumberForRegion(BS_NUMBER, RegionCode.BS)); 838 assertFalse(phoneUtil.isValidNumberForRegion(BS_NUMBER, RegionCode.US)); 839 PhoneNumber bsInvalidNumber = 840 new PhoneNumber().setCountryCode(1).setNationalNumber(2421232345L); 841 // This number is no longer valid. 842 assertFalse(phoneUtil.isValidNumber(bsInvalidNumber)); 843 844 // La Mayotte and Reunion use 'leadingDigits' to differentiate them. 845 PhoneNumber reNumber = new PhoneNumber(); 846 reNumber.setCountryCode(262).setNationalNumber(262123456L); 847 assertTrue(phoneUtil.isValidNumber(reNumber)); 848 assertTrue(phoneUtil.isValidNumberForRegion(reNumber, RegionCode.RE)); 849 assertFalse(phoneUtil.isValidNumberForRegion(reNumber, RegionCode.YT)); 850 // Now change the number to be a number for La Mayotte. 851 reNumber.setNationalNumber(269601234L); 852 assertTrue(phoneUtil.isValidNumberForRegion(reNumber, RegionCode.YT)); 853 assertFalse(phoneUtil.isValidNumberForRegion(reNumber, RegionCode.RE)); 854 // This number is no longer valid for La Reunion. 855 reNumber.setNationalNumber(269123456L); 856 assertFalse(phoneUtil.isValidNumberForRegion(reNumber, RegionCode.YT)); 857 assertFalse(phoneUtil.isValidNumberForRegion(reNumber, RegionCode.RE)); 858 assertFalse(phoneUtil.isValidNumber(reNumber)); 859 // However, it should be recognised as from La Mayotte, since it is valid for this region. 860 assertEquals(RegionCode.YT, phoneUtil.getRegionCodeForNumber(reNumber)); 861 // This number is valid in both places. 862 reNumber.setNationalNumber(800123456L); 863 assertTrue(phoneUtil.isValidNumberForRegion(reNumber, RegionCode.YT)); 864 assertTrue(phoneUtil.isValidNumberForRegion(reNumber, RegionCode.RE)); 865 } 866 867 public void testIsNotValidNumber() { 868 assertFalse(phoneUtil.isValidNumber(US_LOCAL_NUMBER)); 869 870 PhoneNumber invalidNumber = new PhoneNumber(); 871 invalidNumber.setCountryCode(39).setNationalNumber(23661830000L).setItalianLeadingZero(true); 872 assertFalse(phoneUtil.isValidNumber(invalidNumber)); 873 874 invalidNumber.clear(); 875 invalidNumber.setCountryCode(44).setNationalNumber(791234567L); 876 assertFalse(phoneUtil.isValidNumber(invalidNumber)); 877 878 invalidNumber.clear(); 879 invalidNumber.setCountryCode(49).setNationalNumber(1234L); 880 assertFalse(phoneUtil.isValidNumber(invalidNumber)); 881 882 invalidNumber.clear(); 883 invalidNumber.setCountryCode(64).setNationalNumber(3316005L); 884 assertFalse(phoneUtil.isValidNumber(invalidNumber)); 885 } 886 887 public void testGetRegionCodeForCountryCode() { 888 assertEquals(RegionCode.US, phoneUtil.getRegionCodeForCountryCode(1)); 889 assertEquals(RegionCode.GB, phoneUtil.getRegionCodeForCountryCode(44)); 890 assertEquals(RegionCode.DE, phoneUtil.getRegionCodeForCountryCode(49)); 891 } 892 893 public void testGetRegionCodeForNumber() { 894 assertEquals(RegionCode.BS, phoneUtil.getRegionCodeForNumber(BS_NUMBER)); 895 assertEquals(RegionCode.US, phoneUtil.getRegionCodeForNumber(US_NUMBER)); 896 assertEquals(RegionCode.GB, phoneUtil.getRegionCodeForNumber(GB_MOBILE)); 897 } 898 899 public void testGetCountryCodeForRegion() { 900 assertEquals(1, phoneUtil.getCountryCodeForRegion(RegionCode.US)); 901 assertEquals(64, phoneUtil.getCountryCodeForRegion(RegionCode.NZ)); 902 assertEquals(0, phoneUtil.getCountryCodeForRegion(null)); 903 assertEquals(0, phoneUtil.getCountryCodeForRegion(RegionCode.ZZ)); 904 // CS is already deprecated so the library doesn't support it. 905 assertEquals(0, phoneUtil.getCountryCodeForRegion(RegionCode.CS)); 906 } 907 908 public void testGetNationalDiallingPrefixForRegion() { 909 assertEquals("1", phoneUtil.getNddPrefixForRegion(RegionCode.US, false)); 910 // Test non-main country to see it gets the national dialling prefix for the main country with 911 // that country calling code. 912 assertEquals("1", phoneUtil.getNddPrefixForRegion(RegionCode.BS, false)); 913 assertEquals("0", phoneUtil.getNddPrefixForRegion(RegionCode.NZ, false)); 914 // Test case with non digit in the national prefix. 915 assertEquals("0~0", phoneUtil.getNddPrefixForRegion(RegionCode.AO, false)); 916 assertEquals("00", phoneUtil.getNddPrefixForRegion(RegionCode.AO, true)); 917 // Test cases with invalid regions. 918 assertEquals(null, phoneUtil.getNddPrefixForRegion(null, false)); 919 assertEquals(null, phoneUtil.getNddPrefixForRegion(RegionCode.ZZ, false)); 920 // CS is already deprecated so the library doesn't support it. 921 assertEquals(null, phoneUtil.getNddPrefixForRegion(RegionCode.CS, false)); 922 } 923 924 public void testIsNANPACountry() { 925 assertTrue(phoneUtil.isNANPACountry(RegionCode.US)); 926 assertTrue(phoneUtil.isNANPACountry(RegionCode.BS)); 927 assertFalse(phoneUtil.isNANPACountry(RegionCode.DE)); 928 assertFalse(phoneUtil.isNANPACountry(RegionCode.ZZ)); 929 assertFalse(phoneUtil.isNANPACountry(null)); 930 } 931 932 public void testIsPossibleNumber() { 933 assertTrue(phoneUtil.isPossibleNumber(US_NUMBER)); 934 assertTrue(phoneUtil.isPossibleNumber(US_LOCAL_NUMBER)); 935 assertTrue(phoneUtil.isPossibleNumber(GB_NUMBER)); 936 937 assertTrue(phoneUtil.isPossibleNumber("+1 650 253 0000", RegionCode.US)); 938 assertTrue(phoneUtil.isPossibleNumber("+1 650 GOO OGLE", RegionCode.US)); 939 assertTrue(phoneUtil.isPossibleNumber("(650) 253-0000", RegionCode.US)); 940 assertTrue(phoneUtil.isPossibleNumber("253-0000", RegionCode.US)); 941 assertTrue(phoneUtil.isPossibleNumber("+1 650 253 0000", RegionCode.GB)); 942 assertTrue(phoneUtil.isPossibleNumber("+44 20 7031 3000", RegionCode.GB)); 943 assertTrue(phoneUtil.isPossibleNumber("(020) 7031 3000", RegionCode.GB)); 944 assertTrue(phoneUtil.isPossibleNumber("7031 3000", RegionCode.GB)); 945 assertTrue(phoneUtil.isPossibleNumber("3331 6005", RegionCode.NZ)); 946 } 947 948 public void testIsPossibleNumberWithReason() { 949 // National numbers for country calling code +1 that are within 7 to 10 digits are possible. 950 assertEquals(PhoneNumberUtil.ValidationResult.IS_POSSIBLE, 951 phoneUtil.isPossibleNumberWithReason(US_NUMBER)); 952 953 assertEquals(PhoneNumberUtil.ValidationResult.IS_POSSIBLE, 954 phoneUtil.isPossibleNumberWithReason(US_LOCAL_NUMBER)); 955 956 assertEquals(PhoneNumberUtil.ValidationResult.TOO_LONG, 957 phoneUtil.isPossibleNumberWithReason(US_LONG_NUMBER)); 958 959 PhoneNumber number = new PhoneNumber(); 960 number.setCountryCode(0).setNationalNumber(2530000L); 961 assertEquals(PhoneNumberUtil.ValidationResult.INVALID_COUNTRY_CODE, 962 phoneUtil.isPossibleNumberWithReason(number)); 963 964 number.clear(); 965 number.setCountryCode(1).setNationalNumber(253000L); 966 assertEquals(PhoneNumberUtil.ValidationResult.TOO_SHORT, 967 phoneUtil.isPossibleNumberWithReason(number)); 968 969 number.clear(); 970 number.setCountryCode(65).setNationalNumber(1234567890L); 971 assertEquals(PhoneNumberUtil.ValidationResult.IS_POSSIBLE, 972 phoneUtil.isPossibleNumberWithReason(number)); 973 974 // Try with number that we don't have metadata for. 975 PhoneNumber adNumber = new PhoneNumber(); 976 adNumber.setCountryCode(376).setNationalNumber(12345L); 977 assertEquals(PhoneNumberUtil.ValidationResult.IS_POSSIBLE, 978 phoneUtil.isPossibleNumberWithReason(adNumber)); 979 adNumber.setCountryCode(376).setNationalNumber(13L); 980 assertEquals(PhoneNumberUtil.ValidationResult.TOO_SHORT, 981 phoneUtil.isPossibleNumberWithReason(adNumber)); 982 adNumber.setCountryCode(376).setNationalNumber(1234567890123456L); 983 assertEquals(PhoneNumberUtil.ValidationResult.TOO_LONG, 984 phoneUtil.isPossibleNumberWithReason(adNumber)); 985 } 986 987 public void testIsNotPossibleNumber() { 988 assertFalse(phoneUtil.isPossibleNumber(US_LONG_NUMBER)); 989 990 PhoneNumber number = new PhoneNumber(); 991 number.setCountryCode(1).setNationalNumber(253000L); 992 assertFalse(phoneUtil.isPossibleNumber(number)); 993 994 number.clear(); 995 number.setCountryCode(44).setNationalNumber(300L); 996 assertFalse(phoneUtil.isPossibleNumber(number)); 997 998 assertFalse(phoneUtil.isPossibleNumber("+1 650 253 00000", RegionCode.US)); 999 assertFalse(phoneUtil.isPossibleNumber("(650) 253-00000", RegionCode.US)); 1000 assertFalse(phoneUtil.isPossibleNumber("I want a Pizza", RegionCode.US)); 1001 assertFalse(phoneUtil.isPossibleNumber("253-000", RegionCode.US)); 1002 assertFalse(phoneUtil.isPossibleNumber("1 3000", RegionCode.GB)); 1003 assertFalse(phoneUtil.isPossibleNumber("+44 300", RegionCode.GB)); 1004 } 1005 1006 public void testTruncateTooLongNumber() { 1007 // US number 650-253-0000, but entered with one additional digit at the end. 1008 assertTrue(phoneUtil.truncateTooLongNumber(US_LONG_NUMBER)); 1009 assertEquals(US_NUMBER, US_LONG_NUMBER); 1010 1011 // GB number 080 1234 5678, but entered with 4 extra digits at the end. 1012 PhoneNumber tooLongNumber = new PhoneNumber(); 1013 tooLongNumber.setCountryCode(44).setNationalNumber(80123456780123L); 1014 PhoneNumber validNumber = new PhoneNumber(); 1015 validNumber.setCountryCode(44).setNationalNumber(8012345678L); 1016 assertTrue(phoneUtil.truncateTooLongNumber(tooLongNumber)); 1017 assertEquals(validNumber, tooLongNumber); 1018 1019 // IT number 022 3456 7890, but entered with 3 extra digits at the end. 1020 tooLongNumber.clear(); 1021 tooLongNumber.setCountryCode(39).setNationalNumber(2234567890123L).setItalianLeadingZero(true); 1022 validNumber.clear(); 1023 validNumber.setCountryCode(39).setNationalNumber(2234567890L).setItalianLeadingZero(true); 1024 assertTrue(phoneUtil.truncateTooLongNumber(tooLongNumber)); 1025 assertEquals(validNumber, tooLongNumber); 1026 1027 // Tests what happens when a valid number is passed in. 1028 PhoneNumber validNumberCopy = new PhoneNumber().mergeFrom(validNumber); 1029 assertTrue(phoneUtil.truncateTooLongNumber(validNumber)); 1030 // Tests the number is not modified. 1031 assertEquals(validNumberCopy, validNumber); 1032 1033 // Tests what happens when a number with invalid prefix is passed in. 1034 PhoneNumber numberWithInvalidPrefix = new PhoneNumber(); 1035 // The test metadata says US numbers cannot have prefix 240. 1036 numberWithInvalidPrefix.setCountryCode(1).setNationalNumber(2401234567L); 1037 PhoneNumber invalidNumberCopy = new PhoneNumber().mergeFrom(numberWithInvalidPrefix); 1038 assertFalse(phoneUtil.truncateTooLongNumber(numberWithInvalidPrefix)); 1039 // Tests the number is not modified. 1040 assertEquals(invalidNumberCopy, numberWithInvalidPrefix); 1041 1042 // Tests what happens when a too short number is passed in. 1043 PhoneNumber tooShortNumber = new PhoneNumber().setCountryCode(1).setNationalNumber(1234L); 1044 PhoneNumber tooShortNumberCopy = new PhoneNumber().mergeFrom(tooShortNumber); 1045 assertFalse(phoneUtil.truncateTooLongNumber(tooShortNumber)); 1046 // Tests the number is not modified. 1047 assertEquals(tooShortNumberCopy, tooShortNumber); 1048 } 1049 1050 public void testIsViablePhoneNumber() { 1051 // Only one or two digits before strange non-possible punctuation. 1052 assertFalse(PhoneNumberUtil.isViablePhoneNumber("12. March")); 1053 assertFalse(PhoneNumberUtil.isViablePhoneNumber("1+1+1")); 1054 assertFalse(PhoneNumberUtil.isViablePhoneNumber("80+0")); 1055 assertFalse(PhoneNumberUtil.isViablePhoneNumber("00")); 1056 // Three digits is viable. 1057 assertTrue(PhoneNumberUtil.isViablePhoneNumber("111")); 1058 // Alpha numbers. 1059 assertTrue(PhoneNumberUtil.isViablePhoneNumber("0800-4-pizza")); 1060 assertTrue(PhoneNumberUtil.isViablePhoneNumber("0800-4-PIZZA")); 1061 } 1062 1063 public void testIsViablePhoneNumberNonAscii() { 1064 // Only one or two digits before possible punctuation followed by more digits. 1065 assertTrue(PhoneNumberUtil.isViablePhoneNumber("1\u300034")); 1066 assertFalse(PhoneNumberUtil.isViablePhoneNumber("1\u30003+4")); 1067 // Unicode variants of possible starting character and other allowed punctuation/digits. 1068 assertTrue(PhoneNumberUtil.isViablePhoneNumber("\uFF081\uFF09\u30003456789")); 1069 // Testing a leading + is okay. 1070 assertTrue(PhoneNumberUtil.isViablePhoneNumber("+1\uFF09\u30003456789")); 1071 } 1072 1073 public void testExtractPossibleNumber() { 1074 // Removes preceding funky punctuation and letters but leaves the rest untouched. 1075 assertEquals("0800-345-600", PhoneNumberUtil.extractPossibleNumber("Tel:0800-345-600")); 1076 assertEquals("0800 FOR PIZZA", PhoneNumberUtil.extractPossibleNumber("Tel:0800 FOR PIZZA")); 1077 // Should not remove plus sign 1078 assertEquals("+800-345-600", PhoneNumberUtil.extractPossibleNumber("Tel:+800-345-600")); 1079 // Should recognise wide digits as possible start values. 1080 assertEquals("\uFF10\uFF12\uFF13", 1081 PhoneNumberUtil.extractPossibleNumber("\uFF10\uFF12\uFF13")); 1082 // Dashes are not possible start values and should be removed. 1083 assertEquals("\uFF11\uFF12\uFF13", 1084 PhoneNumberUtil.extractPossibleNumber("Num-\uFF11\uFF12\uFF13")); 1085 // If not possible number present, return empty string. 1086 assertEquals("", PhoneNumberUtil.extractPossibleNumber("Num-....")); 1087 // Leading brackets are stripped - these are not used when parsing. 1088 assertEquals("650) 253-0000", PhoneNumberUtil.extractPossibleNumber("(650) 253-0000")); 1089 1090 // Trailing non-alpha-numeric characters should be removed. 1091 assertEquals("650) 253-0000", PhoneNumberUtil.extractPossibleNumber("(650) 253-0000..- ..")); 1092 assertEquals("650) 253-0000", PhoneNumberUtil.extractPossibleNumber("(650) 253-0000.")); 1093 // This case has a trailing RTL char. 1094 assertEquals("650) 253-0000", PhoneNumberUtil.extractPossibleNumber("(650) 253-0000\u200F")); 1095 } 1096 1097 public void testMaybeStripNationalPrefix() { 1098 PhoneMetadata metadata = new PhoneMetadata(); 1099 metadata.setNationalPrefixForParsing("34"); 1100 metadata.setGeneralDesc(new PhoneNumberDesc().setNationalNumberPattern("\\d{4,8}")); 1101 StringBuilder numberToStrip = new StringBuilder("34356778"); 1102 String strippedNumber = "356778"; 1103 phoneUtil.maybeStripNationalPrefixAndCarrierCode(numberToStrip, metadata); 1104 assertEquals("Should have had national prefix stripped.", 1105 strippedNumber, numberToStrip.toString()); 1106 // Retry stripping - now the number should not start with the national prefix, so no more 1107 // stripping should occur. 1108 phoneUtil.maybeStripNationalPrefixAndCarrierCode(numberToStrip, metadata); 1109 assertEquals("Should have had no change - no national prefix present.", 1110 strippedNumber, numberToStrip.toString()); 1111 // Some countries have no national prefix. Repeat test with none specified. 1112 metadata.setNationalPrefixForParsing(""); 1113 phoneUtil.maybeStripNationalPrefixAndCarrierCode(numberToStrip, metadata); 1114 assertEquals("Should not strip anything with empty national prefix.", 1115 strippedNumber, numberToStrip.toString()); 1116 // If the resultant number doesn't match the national rule, it shouldn't be stripped. 1117 metadata.setNationalPrefixForParsing("3"); 1118 numberToStrip = new StringBuilder("3123"); 1119 strippedNumber = "3123"; 1120 phoneUtil.maybeStripNationalPrefixAndCarrierCode(numberToStrip, metadata); 1121 assertEquals("Should have had no change - after stripping, it wouldn't have matched " + 1122 "the national rule.", 1123 strippedNumber, numberToStrip.toString()); 1124 // Test extracting carrier selection code. 1125 metadata.setNationalPrefixForParsing("0(81)?"); 1126 numberToStrip = new StringBuilder("08122123456"); 1127 strippedNumber = "22123456"; 1128 assertEquals("81", phoneUtil.maybeStripNationalPrefixAndCarrierCode(numberToStrip, metadata)); 1129 assertEquals("Should have had national prefix and carrier code stripped.", 1130 strippedNumber, numberToStrip.toString()); 1131 // If there was a transform rule, check it was applied. 1132 metadata.setNationalPrefixTransformRule("5$15"); 1133 // Note that a capturing group is present here. 1134 metadata.setNationalPrefixForParsing("0(\\d{2})"); 1135 numberToStrip = new StringBuilder("031123"); 1136 String transformedNumber = "5315123"; 1137 phoneUtil.maybeStripNationalPrefixAndCarrierCode(numberToStrip, metadata); 1138 assertEquals("Should transform the 031 to a 5315.", 1139 transformedNumber, numberToStrip.toString()); 1140 } 1141 1142 public void testMaybeStripInternationalPrefix() { 1143 String internationalPrefix = "00[39]"; 1144 StringBuilder numberToStrip = new StringBuilder("0034567700-3898003"); 1145 // Note the dash is removed as part of the normalization. 1146 StringBuilder strippedNumber = new StringBuilder("45677003898003"); 1147 assertEquals(CountryCodeSource.FROM_NUMBER_WITH_IDD, 1148 phoneUtil.maybeStripInternationalPrefixAndNormalize(numberToStrip, 1149 internationalPrefix)); 1150 assertEquals("The number supplied was not stripped of its international prefix.", 1151 strippedNumber.toString(), numberToStrip.toString()); 1152 // Now the number no longer starts with an IDD prefix, so it should now report 1153 // FROM_DEFAULT_COUNTRY. 1154 assertEquals(CountryCodeSource.FROM_DEFAULT_COUNTRY, 1155 phoneUtil.maybeStripInternationalPrefixAndNormalize(numberToStrip, 1156 internationalPrefix)); 1157 1158 numberToStrip = new StringBuilder("00945677003898003"); 1159 assertEquals(CountryCodeSource.FROM_NUMBER_WITH_IDD, 1160 phoneUtil.maybeStripInternationalPrefixAndNormalize(numberToStrip, 1161 internationalPrefix)); 1162 assertEquals("The number supplied was not stripped of its international prefix.", 1163 strippedNumber.toString(), numberToStrip.toString()); 1164 // Test it works when the international prefix is broken up by spaces. 1165 numberToStrip = new StringBuilder("00 9 45677003898003"); 1166 assertEquals(CountryCodeSource.FROM_NUMBER_WITH_IDD, 1167 phoneUtil.maybeStripInternationalPrefixAndNormalize(numberToStrip, 1168 internationalPrefix)); 1169 assertEquals("The number supplied was not stripped of its international prefix.", 1170 strippedNumber.toString(), numberToStrip.toString()); 1171 // Now the number no longer starts with an IDD prefix, so it should now report 1172 // FROM_DEFAULT_COUNTRY. 1173 assertEquals(CountryCodeSource.FROM_DEFAULT_COUNTRY, 1174 phoneUtil.maybeStripInternationalPrefixAndNormalize(numberToStrip, 1175 internationalPrefix)); 1176 1177 // Test the + symbol is also recognised and stripped. 1178 numberToStrip = new StringBuilder("+45677003898003"); 1179 strippedNumber = new StringBuilder("45677003898003"); 1180 assertEquals(CountryCodeSource.FROM_NUMBER_WITH_PLUS_SIGN, 1181 phoneUtil.maybeStripInternationalPrefixAndNormalize(numberToStrip, 1182 internationalPrefix)); 1183 assertEquals("The number supplied was not stripped of the plus symbol.", 1184 strippedNumber.toString(), numberToStrip.toString()); 1185 1186 // If the number afterwards is a zero, we should not strip this - no country calling code begins 1187 // with 0. 1188 numberToStrip = new StringBuilder("0090112-3123"); 1189 strippedNumber = new StringBuilder("00901123123"); 1190 assertEquals(CountryCodeSource.FROM_DEFAULT_COUNTRY, 1191 phoneUtil.maybeStripInternationalPrefixAndNormalize(numberToStrip, 1192 internationalPrefix)); 1193 assertEquals("The number supplied had a 0 after the match so shouldn't be stripped.", 1194 strippedNumber.toString(), numberToStrip.toString()); 1195 // Here the 0 is separated by a space from the IDD. 1196 numberToStrip = new StringBuilder("009 0-112-3123"); 1197 assertEquals(CountryCodeSource.FROM_DEFAULT_COUNTRY, 1198 phoneUtil.maybeStripInternationalPrefixAndNormalize(numberToStrip, 1199 internationalPrefix)); 1200 } 1201 1202 public void testMaybeExtractCountryCode() { 1203 PhoneNumber number = new PhoneNumber(); 1204 PhoneMetadata metadata = phoneUtil.getMetadataForRegion(RegionCode.US); 1205 // Note that for the US, the IDD is 011. 1206 try { 1207 String phoneNumber = "011112-3456789"; 1208 String strippedNumber = "123456789"; 1209 int countryCallingCode = 1; 1210 StringBuilder numberToFill = new StringBuilder(); 1211 assertEquals("Did not extract country calling code " + countryCallingCode + " correctly.", 1212 countryCallingCode, 1213 phoneUtil.maybeExtractCountryCode(phoneNumber, metadata, numberToFill, true, 1214 number)); 1215 assertEquals("Did not figure out CountryCodeSource correctly", 1216 CountryCodeSource.FROM_NUMBER_WITH_IDD, number.getCountryCodeSource()); 1217 // Should strip and normalize national significant number. 1218 assertEquals("Did not strip off the country calling code correctly.", 1219 strippedNumber, 1220 numberToFill.toString()); 1221 } catch (NumberParseException e) { 1222 fail("Should not have thrown an exception: " + e.toString()); 1223 } 1224 number.clear(); 1225 try { 1226 String phoneNumber = "+6423456789"; 1227 int countryCallingCode = 64; 1228 StringBuilder numberToFill = new StringBuilder(); 1229 assertEquals("Did not extract country calling code " + countryCallingCode + " correctly.", 1230 countryCallingCode, 1231 phoneUtil.maybeExtractCountryCode(phoneNumber, metadata, numberToFill, true, 1232 number)); 1233 assertEquals("Did not figure out CountryCodeSource correctly", 1234 CountryCodeSource.FROM_NUMBER_WITH_PLUS_SIGN, number.getCountryCodeSource()); 1235 } catch (NumberParseException e) { 1236 fail("Should not have thrown an exception: " + e.toString()); 1237 } 1238 number.clear(); 1239 try { 1240 String phoneNumber = "2345-6789"; 1241 StringBuilder numberToFill = new StringBuilder(); 1242 assertEquals( 1243 "Should not have extracted a country calling code - no international prefix present.", 1244 0, 1245 phoneUtil.maybeExtractCountryCode(phoneNumber, metadata, numberToFill, true, number)); 1246 assertEquals("Did not figure out CountryCodeSource correctly", 1247 CountryCodeSource.FROM_DEFAULT_COUNTRY, number.getCountryCodeSource()); 1248 } catch (NumberParseException e) { 1249 fail("Should not have thrown an exception: " + e.toString()); 1250 } 1251 number.clear(); 1252 try { 1253 String phoneNumber = "0119991123456789"; 1254 StringBuilder numberToFill = new StringBuilder(); 1255 phoneUtil.maybeExtractCountryCode(phoneNumber, metadata, numberToFill, true, number); 1256 fail("Should have thrown an exception, no valid country calling code present."); 1257 } catch (NumberParseException e) { 1258 // Expected. 1259 assertEquals("Wrong error type stored in exception.", 1260 NumberParseException.ErrorType.INVALID_COUNTRY_CODE, 1261 e.getErrorType()); 1262 } 1263 number.clear(); 1264 try { 1265 String phoneNumber = "(1 610) 619 4466"; 1266 int countryCallingCode = 1; 1267 StringBuilder numberToFill = new StringBuilder(); 1268 assertEquals("Should have extracted the country calling code of the region passed in", 1269 countryCallingCode, 1270 phoneUtil.maybeExtractCountryCode(phoneNumber, metadata, numberToFill, true, 1271 number)); 1272 assertEquals("Did not figure out CountryCodeSource correctly", 1273 CountryCodeSource.FROM_NUMBER_WITHOUT_PLUS_SIGN, 1274 number.getCountryCodeSource()); 1275 } catch (NumberParseException e) { 1276 fail("Should not have thrown an exception: " + e.toString()); 1277 } 1278 number.clear(); 1279 try { 1280 String phoneNumber = "(1 610) 619 4466"; 1281 int countryCallingCode = 1; 1282 StringBuilder numberToFill = new StringBuilder(); 1283 assertEquals("Should have extracted the country calling code of the region passed in", 1284 countryCallingCode, 1285 phoneUtil.maybeExtractCountryCode(phoneNumber, metadata, numberToFill, false, 1286 number)); 1287 assertFalse("Should not contain CountryCodeSource.", number.hasCountryCodeSource()); 1288 } catch (NumberParseException e) { 1289 fail("Should not have thrown an exception: " + e.toString()); 1290 } 1291 number.clear(); 1292 try { 1293 String phoneNumber = "(1 610) 619 446"; 1294 StringBuilder numberToFill = new StringBuilder(); 1295 assertEquals("Should not have extracted a country calling code - invalid number after " + 1296 "extraction of uncertain country calling code.", 1297 0, 1298 phoneUtil.maybeExtractCountryCode(phoneNumber, metadata, numberToFill, false, 1299 number)); 1300 assertFalse("Should not contain CountryCodeSource.", number.hasCountryCodeSource()); 1301 } catch (NumberParseException e) { 1302 fail("Should not have thrown an exception: " + e.toString()); 1303 } 1304 number.clear(); 1305 try { 1306 String phoneNumber = "(1 610) 619"; 1307 StringBuilder numberToFill = new StringBuilder(); 1308 assertEquals("Should not have extracted a country calling code - too short number both " + 1309 "before and after extraction of uncertain country calling code.", 1310 0, 1311 phoneUtil.maybeExtractCountryCode(phoneNumber, metadata, numberToFill, true, 1312 number)); 1313 assertEquals("Did not figure out CountryCodeSource correctly", 1314 CountryCodeSource.FROM_DEFAULT_COUNTRY, number.getCountryCodeSource()); 1315 } catch (NumberParseException e) { 1316 fail("Should not have thrown an exception: " + e.toString()); 1317 } 1318 } 1319 1320 public void testParseNationalNumber() throws Exception { 1321 // National prefix attached. 1322 assertEquals(NZ_NUMBER, phoneUtil.parse("033316005", RegionCode.NZ)); 1323 assertEquals(NZ_NUMBER, phoneUtil.parse("33316005", RegionCode.NZ)); 1324 // National prefix attached and some formatting present. 1325 assertEquals(NZ_NUMBER, phoneUtil.parse("03-331 6005", RegionCode.NZ)); 1326 assertEquals(NZ_NUMBER, phoneUtil.parse("03 331 6005", RegionCode.NZ)); 1327 1328 // Testing international prefixes. 1329 // Should strip country calling code. 1330 assertEquals(NZ_NUMBER, phoneUtil.parse("0064 3 331 6005", RegionCode.NZ)); 1331 // Try again, but this time we have an international number with Region Code US. It should 1332 // recognise the country calling code and parse accordingly. 1333 assertEquals(NZ_NUMBER, phoneUtil.parse("01164 3 331 6005", RegionCode.US)); 1334 assertEquals(NZ_NUMBER, phoneUtil.parse("+64 3 331 6005", RegionCode.US)); 1335 1336 PhoneNumber nzNumber = new PhoneNumber(); 1337 nzNumber.setCountryCode(64).setNationalNumber(64123456L); 1338 assertEquals(nzNumber, phoneUtil.parse("64(0)64123456", RegionCode.NZ)); 1339 // Check that using a "/" is fine in a phone number. 1340 assertEquals(DE_NUMBER, phoneUtil.parse("301/23456", RegionCode.DE)); 1341 1342 PhoneNumber usNumber = new PhoneNumber(); 1343 // Check it doesn't use the '1' as a country calling code when parsing if the phone number was 1344 // already possible. 1345 usNumber.setCountryCode(1).setNationalNumber(1234567890L); 1346 assertEquals(usNumber, phoneUtil.parse("123-456-7890", RegionCode.US)); 1347 } 1348 1349 public void testParseNumberWithAlphaCharacters() throws Exception { 1350 // Test case with alpha characters. 1351 PhoneNumber tollfreeNumber = new PhoneNumber(); 1352 tollfreeNumber.setCountryCode(64).setNationalNumber(800332005L); 1353 assertEquals(tollfreeNumber, phoneUtil.parse("0800 DDA 005", RegionCode.NZ)); 1354 PhoneNumber premiumNumber = new PhoneNumber(); 1355 premiumNumber.setCountryCode(64).setNationalNumber(9003326005L); 1356 assertEquals(premiumNumber, phoneUtil.parse("0900 DDA 6005", RegionCode.NZ)); 1357 // Not enough alpha characters for them to be considered intentional, so they are stripped. 1358 assertEquals(premiumNumber, phoneUtil.parse("0900 332 6005a", RegionCode.NZ)); 1359 assertEquals(premiumNumber, phoneUtil.parse("0900 332 600a5", RegionCode.NZ)); 1360 assertEquals(premiumNumber, phoneUtil.parse("0900 332 600A5", RegionCode.NZ)); 1361 assertEquals(premiumNumber, phoneUtil.parse("0900 a332 600A5", RegionCode.NZ)); 1362 } 1363 1364 public void testParseWithInternationalPrefixes() throws Exception { 1365 assertEquals(US_NUMBER, phoneUtil.parse("+1 (650) 253-0000", RegionCode.NZ)); 1366 assertEquals(US_NUMBER, phoneUtil.parse("1-650-253-0000", RegionCode.US)); 1367 // Calling the US number from Singapore by using different service providers 1368 // 1st test: calling using SingTel IDD service (IDD is 001) 1369 assertEquals(US_NUMBER, phoneUtil.parse("0011-650-253-0000", RegionCode.SG)); 1370 // 2nd test: calling using StarHub IDD service (IDD is 008) 1371 assertEquals(US_NUMBER, phoneUtil.parse("0081-650-253-0000", RegionCode.SG)); 1372 // 3rd test: calling using SingTel V019 service (IDD is 019) 1373 assertEquals(US_NUMBER, phoneUtil.parse("0191-650-253-0000", RegionCode.SG)); 1374 // Calling the US number from Poland 1375 assertEquals(US_NUMBER, phoneUtil.parse("0~01-650-253-0000", RegionCode.PL)); 1376 // Using "++" at the start. 1377 assertEquals(US_NUMBER, phoneUtil.parse("++1 (650) 253-0000", RegionCode.PL)); 1378 } 1379 1380 public void testParseNonAscii() throws Exception { 1381 // Using a full-width plus sign. 1382 assertEquals(US_NUMBER, phoneUtil.parse("\uFF0B1 (650) 253-0000", RegionCode.SG)); 1383 // The whole number, including punctuation, is here represented in full-width form. 1384 assertEquals(US_NUMBER, phoneUtil.parse("\uFF0B\uFF11\u3000\uFF08\uFF16\uFF15\uFF10\uFF09" + 1385 "\u3000\uFF12\uFF15\uFF13\uFF0D\uFF10\uFF10\uFF10" + 1386 "\uFF10", 1387 RegionCode.SG)); 1388 // Using U+30FC dash instead. 1389 assertEquals(US_NUMBER, phoneUtil.parse("\uFF0B\uFF11\u3000\uFF08\uFF16\uFF15\uFF10\uFF09" + 1390 "\u3000\uFF12\uFF15\uFF13\u30FC\uFF10\uFF10\uFF10" + 1391 "\uFF10", 1392 RegionCode.SG)); 1393 1394 // Using a very strange decimal digit range (Mongolian digits). 1395 assertEquals(US_NUMBER, phoneUtil.parse("\u1811 \u1816\u1815\u1810 " + 1396 "\u1812\u1815\u1813 \u1810\u1810\u1810\u1810", 1397 RegionCode.US)); 1398 } 1399 1400 public void testParseWithLeadingZero() throws Exception { 1401 assertEquals(IT_NUMBER, phoneUtil.parse("+39 02-36618 300", RegionCode.NZ)); 1402 assertEquals(IT_NUMBER, phoneUtil.parse("02-36618 300", RegionCode.IT)); 1403 1404 assertEquals(IT_MOBILE, phoneUtil.parse("345 678 901", RegionCode.IT)); 1405 } 1406 1407 public void testParseNationalNumberArgentina() throws Exception { 1408 // Test parsing mobile numbers of Argentina. 1409 PhoneNumber arNumber = new PhoneNumber(); 1410 arNumber.setCountryCode(54).setNationalNumber(93435551212L); 1411 assertEquals(arNumber, phoneUtil.parse("+54 9 343 555 1212", RegionCode.AR)); 1412 assertEquals(arNumber, phoneUtil.parse("0343 15 555 1212", RegionCode.AR)); 1413 1414 arNumber.clear(); 1415 arNumber.setCountryCode(54).setNationalNumber(93715654320L); 1416 assertEquals(arNumber, phoneUtil.parse("+54 9 3715 65 4320", RegionCode.AR)); 1417 assertEquals(arNumber, phoneUtil.parse("03715 15 65 4320", RegionCode.AR)); 1418 assertEquals(AR_MOBILE, phoneUtil.parse("911 876 54321", RegionCode.AR)); 1419 1420 // Test parsing fixed-line numbers of Argentina. 1421 assertEquals(AR_NUMBER, phoneUtil.parse("+54 11 8765 4321", RegionCode.AR)); 1422 assertEquals(AR_NUMBER, phoneUtil.parse("011 8765 4321", RegionCode.AR)); 1423 1424 arNumber.clear(); 1425 arNumber.setCountryCode(54).setNationalNumber(3715654321L); 1426 assertEquals(arNumber, phoneUtil.parse("+54 3715 65 4321", RegionCode.AR)); 1427 assertEquals(arNumber, phoneUtil.parse("03715 65 4321", RegionCode.AR)); 1428 1429 arNumber.clear(); 1430 arNumber.setCountryCode(54).setNationalNumber(2312340000L); 1431 assertEquals(arNumber, phoneUtil.parse("+54 23 1234 0000", RegionCode.AR)); 1432 assertEquals(arNumber, phoneUtil.parse("023 1234 0000", RegionCode.AR)); 1433 } 1434 1435 public void testParseWithXInNumber() throws Exception { 1436 // Test that having an 'x' in the phone number at the start is ok and that it just gets removed. 1437 assertEquals(AR_NUMBER, phoneUtil.parse("01187654321", RegionCode.AR)); 1438 assertEquals(AR_NUMBER, phoneUtil.parse("(0) 1187654321", RegionCode.AR)); 1439 assertEquals(AR_NUMBER, phoneUtil.parse("0 1187654321", RegionCode.AR)); 1440 assertEquals(AR_NUMBER, phoneUtil.parse("(0xx) 1187654321", RegionCode.AR)); 1441 PhoneNumber arFromUs = new PhoneNumber(); 1442 arFromUs.setCountryCode(54).setNationalNumber(81429712L); 1443 // This test is intentionally constructed such that the number of digit after xx is larger than 1444 // 7, so that the number won't be mistakenly treated as an extension, as we allow extensions up 1445 // to 7 digits. This assumption is okay for now as all the countries where a carrier selection 1446 // code is written in the form of xx have a national significant number of length larger than 7. 1447 assertEquals(arFromUs, phoneUtil.parse("011xx5481429712", RegionCode.US)); 1448 } 1449 1450 public void testParseNumbersMexico() throws Exception { 1451 // Test parsing fixed-line numbers of Mexico. 1452 PhoneNumber mxNumber = new PhoneNumber(); 1453 mxNumber.setCountryCode(52).setNationalNumber(4499780001L); 1454 assertEquals(mxNumber, phoneUtil.parse("+52 (449)978-0001", RegionCode.MX)); 1455 assertEquals(mxNumber, phoneUtil.parse("01 (449)978-0001", RegionCode.MX)); 1456 assertEquals(mxNumber, phoneUtil.parse("(449)978-0001", RegionCode.MX)); 1457 1458 // Test parsing mobile numbers of Mexico. 1459 mxNumber.clear(); 1460 mxNumber.setCountryCode(52).setNationalNumber(13312345678L); 1461 assertEquals(mxNumber, phoneUtil.parse("+52 1 33 1234-5678", RegionCode.MX)); 1462 assertEquals(mxNumber, phoneUtil.parse("044 (33) 1234-5678", RegionCode.MX)); 1463 assertEquals(mxNumber, phoneUtil.parse("045 33 1234-5678", RegionCode.MX)); 1464 } 1465 1466 public void testFailedParseOnInvalidNumbers() { 1467 try { 1468 String sentencePhoneNumber = "This is not a phone number"; 1469 phoneUtil.parse(sentencePhoneNumber, RegionCode.NZ); 1470 fail("This should not parse without throwing an exception " + sentencePhoneNumber); 1471 } catch (NumberParseException e) { 1472 // Expected this exception. 1473 assertEquals("Wrong error type stored in exception.", 1474 NumberParseException.ErrorType.NOT_A_NUMBER, 1475 e.getErrorType()); 1476 } 1477 try { 1478 String tooLongPhoneNumber = "01495 72553301873 810104"; 1479 phoneUtil.parse(tooLongPhoneNumber, RegionCode.GB); 1480 fail("This should not parse without throwing an exception " + tooLongPhoneNumber); 1481 } catch (NumberParseException e) { 1482 // Expected this exception. 1483 assertEquals("Wrong error type stored in exception.", 1484 NumberParseException.ErrorType.TOO_LONG, 1485 e.getErrorType()); 1486 } 1487 try { 1488 String plusMinusPhoneNumber = "+---"; 1489 phoneUtil.parse(plusMinusPhoneNumber, RegionCode.DE); 1490 fail("This should not parse without throwing an exception " + plusMinusPhoneNumber); 1491 } catch (NumberParseException e) { 1492 // Expected this exception. 1493 assertEquals("Wrong error type stored in exception.", 1494 NumberParseException.ErrorType.NOT_A_NUMBER, 1495 e.getErrorType()); 1496 } 1497 try { 1498 String tooShortPhoneNumber = "+49 0"; 1499 phoneUtil.parse(tooShortPhoneNumber, RegionCode.DE); 1500 fail("This should not parse without throwing an exception " + tooShortPhoneNumber); 1501 } catch (NumberParseException e) { 1502 // Expected this exception. 1503 assertEquals("Wrong error type stored in exception.", 1504 NumberParseException.ErrorType.TOO_SHORT_NSN, 1505 e.getErrorType()); 1506 } 1507 try { 1508 String invalidCountryCode = "+210 3456 56789"; 1509 phoneUtil.parse(invalidCountryCode, RegionCode.NZ); 1510 fail("This is not a recognised region code: should fail: " + invalidCountryCode); 1511 } catch (NumberParseException e) { 1512 // Expected this exception. 1513 assertEquals("Wrong error type stored in exception.", 1514 NumberParseException.ErrorType.INVALID_COUNTRY_CODE, 1515 e.getErrorType()); 1516 } 1517 try { 1518 String someNumber = "123 456 7890"; 1519 phoneUtil.parse(someNumber, RegionCode.ZZ); 1520 fail("'Unknown' region code not allowed: should fail."); 1521 } catch (NumberParseException e) { 1522 // Expected this exception. 1523 assertEquals("Wrong error type stored in exception.", 1524 NumberParseException.ErrorType.INVALID_COUNTRY_CODE, 1525 e.getErrorType()); 1526 } 1527 try { 1528 String someNumber = "123 456 7890"; 1529 phoneUtil.parse(someNumber, RegionCode.CS); 1530 fail("Deprecated region code not allowed: should fail."); 1531 } catch (NumberParseException e) { 1532 // Expected this exception. 1533 assertEquals("Wrong error type stored in exception.", 1534 NumberParseException.ErrorType.INVALID_COUNTRY_CODE, 1535 e.getErrorType()); 1536 } 1537 try { 1538 String someNumber = "123 456 7890"; 1539 phoneUtil.parse(someNumber, null); 1540 fail("Null region code not allowed: should fail."); 1541 } catch (NumberParseException e) { 1542 // Expected this exception. 1543 assertEquals("Wrong error type stored in exception.", 1544 NumberParseException.ErrorType.INVALID_COUNTRY_CODE, 1545 e.getErrorType()); 1546 } 1547 try { 1548 String someNumber = "0044------"; 1549 phoneUtil.parse(someNumber, RegionCode.GB); 1550 fail("No number provided, only region code: should fail"); 1551 } catch (NumberParseException e) { 1552 // Expected this exception. 1553 assertEquals("Wrong error type stored in exception.", 1554 NumberParseException.ErrorType.TOO_SHORT_AFTER_IDD, 1555 e.getErrorType()); 1556 } 1557 try { 1558 String someNumber = "0044"; 1559 phoneUtil.parse(someNumber, RegionCode.GB); 1560 fail("No number provided, only region code: should fail"); 1561 } catch (NumberParseException e) { 1562 // Expected this exception. 1563 assertEquals("Wrong error type stored in exception.", 1564 NumberParseException.ErrorType.TOO_SHORT_AFTER_IDD, 1565 e.getErrorType()); 1566 } 1567 try { 1568 String someNumber = "011"; 1569 phoneUtil.parse(someNumber, RegionCode.US); 1570 fail("Only IDD provided - should fail."); 1571 } catch (NumberParseException e) { 1572 // Expected this exception. 1573 assertEquals("Wrong error type stored in exception.", 1574 NumberParseException.ErrorType.TOO_SHORT_AFTER_IDD, 1575 e.getErrorType()); 1576 } 1577 try { 1578 String someNumber = "0119"; 1579 phoneUtil.parse(someNumber, RegionCode.US); 1580 fail("Only IDD provided and then 9 - should fail."); 1581 } catch (NumberParseException e) { 1582 // Expected this exception. 1583 assertEquals("Wrong error type stored in exception.", 1584 NumberParseException.ErrorType.TOO_SHORT_AFTER_IDD, 1585 e.getErrorType()); 1586 } 1587 try { 1588 String emptyNumber = ""; 1589 // Invalid region. 1590 phoneUtil.parse(emptyNumber, RegionCode.ZZ); 1591 fail("Empty string - should fail."); 1592 } catch (NumberParseException e) { 1593 // Expected this exception. 1594 assertEquals("Wrong error type stored in exception.", 1595 NumberParseException.ErrorType.NOT_A_NUMBER, 1596 e.getErrorType()); 1597 } 1598 try { 1599 String nullNumber = null; 1600 // Invalid region. 1601 phoneUtil.parse(nullNumber, RegionCode.ZZ); 1602 fail("Null string - should fail."); 1603 } catch (NumberParseException e) { 1604 // Expected this exception. 1605 assertEquals("Wrong error type stored in exception.", 1606 NumberParseException.ErrorType.NOT_A_NUMBER, 1607 e.getErrorType()); 1608 } catch (NullPointerException e) { 1609 fail("Null string - but should not throw a null pointer exception."); 1610 } 1611 try { 1612 String nullNumber = null; 1613 phoneUtil.parse(nullNumber, RegionCode.US); 1614 fail("Null string - should fail."); 1615 } catch (NumberParseException e) { 1616 // Expected this exception. 1617 assertEquals("Wrong error type stored in exception.", 1618 NumberParseException.ErrorType.NOT_A_NUMBER, 1619 e.getErrorType()); 1620 } catch (NullPointerException e) { 1621 fail("Null string - but should not throw a null pointer exception."); 1622 } 1623 } 1624 1625 public void testParseNumbersWithPlusWithNoRegion() throws Exception { 1626 // RegionCode.ZZ is allowed only if the number starts with a '+' - then the country calling code 1627 // can be calculated. 1628 assertEquals(NZ_NUMBER, phoneUtil.parse("+64 3 331 6005", RegionCode.ZZ)); 1629 // Test with full-width plus. 1630 assertEquals(NZ_NUMBER, phoneUtil.parse("\uFF0B64 3 331 6005", RegionCode.ZZ)); 1631 // Test with normal plus but leading characters that need to be stripped. 1632 assertEquals(NZ_NUMBER, phoneUtil.parse("Tel: +64 3 331 6005", RegionCode.ZZ)); 1633 assertEquals(NZ_NUMBER, phoneUtil.parse("+64 3 331 6005", null)); 1634 1635 // It is important that we set the carrier code to an empty string, since we used 1636 // ParseAndKeepRawInput and no carrier code was found. 1637 PhoneNumber nzNumberWithRawInput = new PhoneNumber().mergeFrom(NZ_NUMBER). 1638 setRawInput("+64 3 331 6005"). 1639 setCountryCodeSource(CountryCodeSource.FROM_NUMBER_WITH_PLUS_SIGN). 1640 setPreferredDomesticCarrierCode(""); 1641 assertEquals(nzNumberWithRawInput, phoneUtil.parseAndKeepRawInput("+64 3 331 6005", 1642 RegionCode.ZZ)); 1643 // Null is also allowed for the region code in these cases. 1644 assertEquals(nzNumberWithRawInput, phoneUtil.parseAndKeepRawInput("+64 3 331 6005", null)); 1645 } 1646 1647 public void testParseExtensions() throws Exception { 1648 PhoneNumber nzNumber = new PhoneNumber(); 1649 nzNumber.setCountryCode(64).setNationalNumber(33316005L).setExtension("3456"); 1650 assertEquals(nzNumber, phoneUtil.parse("03 331 6005 ext 3456", RegionCode.NZ)); 1651 assertEquals(nzNumber, phoneUtil.parse("03-3316005x3456", RegionCode.NZ)); 1652 assertEquals(nzNumber, phoneUtil.parse("03-3316005 int.3456", RegionCode.NZ)); 1653 assertEquals(nzNumber, phoneUtil.parse("03 3316005 #3456", RegionCode.NZ)); 1654 // Test the following do not extract extensions: 1655 assertEquals(ALPHA_NUMERIC_NUMBER, phoneUtil.parse("1800 six-flags", RegionCode.US)); 1656 assertEquals(ALPHA_NUMERIC_NUMBER, phoneUtil.parse("1800 SIX FLAGS", RegionCode.US)); 1657 assertEquals(ALPHA_NUMERIC_NUMBER, phoneUtil.parse("0~0 1800 7493 5247", RegionCode.PL)); 1658 assertEquals(ALPHA_NUMERIC_NUMBER, phoneUtil.parse("(1800) 7493.5247", RegionCode.US)); 1659 // Check that the last instance of an extension token is matched. 1660 PhoneNumber extnNumber = new PhoneNumber().mergeFrom(ALPHA_NUMERIC_NUMBER).setExtension("1234"); 1661 assertEquals(extnNumber, phoneUtil.parse("0~0 1800 7493 5247 ~1234", RegionCode.PL)); 1662 // Verifying bug-fix where the last digit of a number was previously omitted if it was a 0 when 1663 // extracting the extension. Also verifying a few different cases of extensions. 1664 PhoneNumber ukNumber = new PhoneNumber(); 1665 ukNumber.setCountryCode(44).setNationalNumber(2034567890L).setExtension("456"); 1666 assertEquals(ukNumber, phoneUtil.parse("+44 2034567890x456", RegionCode.NZ)); 1667 assertEquals(ukNumber, phoneUtil.parse("+44 2034567890x456", RegionCode.GB)); 1668 assertEquals(ukNumber, phoneUtil.parse("+44 2034567890 x456", RegionCode.GB)); 1669 assertEquals(ukNumber, phoneUtil.parse("+44 2034567890 X456", RegionCode.GB)); 1670 assertEquals(ukNumber, phoneUtil.parse("+44 2034567890 X 456", RegionCode.GB)); 1671 assertEquals(ukNumber, phoneUtil.parse("+44 2034567890 X 456", RegionCode.GB)); 1672 assertEquals(ukNumber, phoneUtil.parse("+44 2034567890 x 456 ", RegionCode.GB)); 1673 assertEquals(ukNumber, phoneUtil.parse("+44 2034567890 X 456", RegionCode.GB)); 1674 assertEquals(ukNumber, phoneUtil.parse("+44-2034567890;ext=456", RegionCode.GB)); 1675 1676 PhoneNumber usWithExtension = new PhoneNumber(); 1677 usWithExtension.setCountryCode(1).setNationalNumber(8009013355L).setExtension("7246433"); 1678 assertEquals(usWithExtension, phoneUtil.parse("(800) 901-3355 x 7246433", RegionCode.US)); 1679 assertEquals(usWithExtension, phoneUtil.parse("(800) 901-3355 , ext 7246433", RegionCode.US)); 1680 assertEquals(usWithExtension, 1681 phoneUtil.parse("(800) 901-3355 ,extension 7246433", RegionCode.US)); 1682 assertEquals(usWithExtension, 1683 phoneUtil.parse("(800) 901-3355 ,extensi\u00F3n 7246433", RegionCode.US)); 1684 // Repeat with the small letter o with acute accent created by combining characters. 1685 assertEquals(usWithExtension, 1686 phoneUtil.parse("(800) 901-3355 ,extensio\u0301n 7246433", RegionCode.US)); 1687 assertEquals(usWithExtension, phoneUtil.parse("(800) 901-3355 , 7246433", RegionCode.US)); 1688 assertEquals(usWithExtension, phoneUtil.parse("(800) 901-3355 ext: 7246433", RegionCode.US)); 1689 1690 // Test that if a number has two extensions specified, we ignore the second. 1691 PhoneNumber usWithTwoExtensionsNumber = new PhoneNumber(); 1692 usWithTwoExtensionsNumber.setCountryCode(1).setNationalNumber(2121231234L).setExtension("508"); 1693 assertEquals(usWithTwoExtensionsNumber, phoneUtil.parse("(212)123-1234 x508/x1234", 1694 RegionCode.US)); 1695 assertEquals(usWithTwoExtensionsNumber, phoneUtil.parse("(212)123-1234 x508/ x1234", 1696 RegionCode.US)); 1697 assertEquals(usWithTwoExtensionsNumber, phoneUtil.parse("(212)123-1234 x508\\x1234", 1698 RegionCode.US)); 1699 1700 // Test parsing numbers in the form (645) 123-1234-910# works, where the last 3 digits before 1701 // the # are an extension. 1702 usWithExtension.clear(); 1703 usWithExtension.setCountryCode(1).setNationalNumber(6451231234L).setExtension("910"); 1704 assertEquals(usWithExtension, phoneUtil.parse("+1 (645) 123 1234-910#", RegionCode.US)); 1705 // Retry with the same number in a slightly different format. 1706 assertEquals(usWithExtension, phoneUtil.parse("+1 (645) 123 1234 ext. 910#", RegionCode.US)); 1707 } 1708 1709 public void testParseAndKeepRaw() throws Exception { 1710 PhoneNumber alphaNumericNumber = new PhoneNumber().mergeFrom(ALPHA_NUMERIC_NUMBER). 1711 setRawInput("800 six-flags"). 1712 setCountryCodeSource(CountryCodeSource.FROM_DEFAULT_COUNTRY). 1713 setPreferredDomesticCarrierCode(""); 1714 assertEquals(alphaNumericNumber, 1715 phoneUtil.parseAndKeepRawInput("800 six-flags", RegionCode.US)); 1716 1717 PhoneNumber shorterAlphaNumber = new PhoneNumber(). 1718 setCountryCode(1).setNationalNumber(8007493524L). 1719 setRawInput("1800 six-flag"). 1720 setCountryCodeSource(CountryCodeSource.FROM_NUMBER_WITHOUT_PLUS_SIGN). 1721 setPreferredDomesticCarrierCode(""); 1722 assertEquals(shorterAlphaNumber, 1723 phoneUtil.parseAndKeepRawInput("1800 six-flag", RegionCode.US)); 1724 1725 shorterAlphaNumber.setRawInput("+1800 six-flag"). 1726 setCountryCodeSource(CountryCodeSource.FROM_NUMBER_WITH_PLUS_SIGN); 1727 assertEquals(shorterAlphaNumber, 1728 phoneUtil.parseAndKeepRawInput("+1800 six-flag", RegionCode.NZ)); 1729 1730 shorterAlphaNumber.setRawInput("001800 six-flag"). 1731 setCountryCodeSource(CountryCodeSource.FROM_NUMBER_WITH_IDD); 1732 assertEquals(shorterAlphaNumber, 1733 phoneUtil.parseAndKeepRawInput("001800 six-flag", RegionCode.NZ)); 1734 1735 // Invalid region code supplied. 1736 try { 1737 phoneUtil.parseAndKeepRawInput("123 456 7890", RegionCode.CS); 1738 fail("Deprecated region code not allowed: should fail."); 1739 } catch (NumberParseException e) { 1740 // Expected this exception. 1741 assertEquals("Wrong error type stored in exception.", 1742 NumberParseException.ErrorType.INVALID_COUNTRY_CODE, 1743 e.getErrorType()); 1744 } 1745 1746 PhoneNumber koreanNumber = new PhoneNumber(); 1747 koreanNumber.setCountryCode(82).setNationalNumber(22123456).setRawInput("08122123456"). 1748 setCountryCodeSource(CountryCodeSource.FROM_DEFAULT_COUNTRY). 1749 setPreferredDomesticCarrierCode("81"); 1750 assertEquals(koreanNumber, phoneUtil.parseAndKeepRawInput("08122123456", RegionCode.KR)); 1751 } 1752 1753 public void testCountryWithNoNumberDesc() { 1754 // Andorra is a country where we don't have PhoneNumberDesc info in the metadata. 1755 PhoneNumber adNumber = new PhoneNumber(); 1756 adNumber.setCountryCode(376).setNationalNumber(12345L); 1757 assertEquals("+376 12345", phoneUtil.format(adNumber, PhoneNumberFormat.INTERNATIONAL)); 1758 assertEquals("+37612345", phoneUtil.format(adNumber, PhoneNumberFormat.E164)); 1759 assertEquals("12345", phoneUtil.format(adNumber, PhoneNumberFormat.NATIONAL)); 1760 assertEquals(PhoneNumberUtil.PhoneNumberType.UNKNOWN, phoneUtil.getNumberType(adNumber)); 1761 assertTrue(phoneUtil.isValidNumber(adNumber)); 1762 1763 // Test dialing a US number from within Andorra. 1764 assertEquals("00 1 650 253 0000", 1765 phoneUtil.formatOutOfCountryCallingNumber(US_NUMBER, RegionCode.AD)); 1766 } 1767 1768 public void testUnknownCountryCallingCodeForValidation() { 1769 PhoneNumber invalidNumber = new PhoneNumber(); 1770 invalidNumber.setCountryCode(0).setNationalNumber(1234L); 1771 assertFalse(phoneUtil.isValidNumber(invalidNumber)); 1772 } 1773 1774 public void testIsNumberMatchMatches() throws Exception { 1775 // Test simple matches where formatting is different, or leading zeroes, or country calling code 1776 // has been specified. 1777 assertEquals(PhoneNumberUtil.MatchType.EXACT_MATCH, 1778 phoneUtil.isNumberMatch("+64 3 331 6005", "+64 03 331 6005")); 1779 assertEquals(PhoneNumberUtil.MatchType.EXACT_MATCH, 1780 phoneUtil.isNumberMatch("+64 03 331-6005", "+64 03331 6005")); 1781 assertEquals(PhoneNumberUtil.MatchType.EXACT_MATCH, 1782 phoneUtil.isNumberMatch("+643 331-6005", "+64033316005")); 1783 assertEquals(PhoneNumberUtil.MatchType.EXACT_MATCH, 1784 phoneUtil.isNumberMatch("+643 331-6005", "+6433316005")); 1785 assertEquals(PhoneNumberUtil.MatchType.EXACT_MATCH, 1786 phoneUtil.isNumberMatch("+64 3 331-6005", "+6433316005")); 1787 // Test alpha numbers. 1788 assertEquals(PhoneNumberUtil.MatchType.EXACT_MATCH, 1789 phoneUtil.isNumberMatch("+1800 siX-Flags", "+1 800 7493 5247")); 1790 // Test numbers with extensions. 1791 assertEquals(PhoneNumberUtil.MatchType.EXACT_MATCH, 1792 phoneUtil.isNumberMatch("+64 3 331-6005 extn 1234", "+6433316005#1234")); 1793 // Test proto buffers. 1794 assertEquals(PhoneNumberUtil.MatchType.EXACT_MATCH, 1795 phoneUtil.isNumberMatch(NZ_NUMBER, "+6403 331 6005")); 1796 1797 PhoneNumber nzNumber = new PhoneNumber().mergeFrom(NZ_NUMBER).setExtension("3456"); 1798 assertEquals(PhoneNumberUtil.MatchType.EXACT_MATCH, 1799 phoneUtil.isNumberMatch(nzNumber, "+643 331 6005 ext 3456")); 1800 // Check empty extensions are ignored. 1801 nzNumber.setExtension(""); 1802 assertEquals(PhoneNumberUtil.MatchType.EXACT_MATCH, 1803 phoneUtil.isNumberMatch(nzNumber, "+6403 331 6005")); 1804 // Check variant with two proto buffers. 1805 assertEquals("Number " + nzNumber.toString() + " did not match " + NZ_NUMBER.toString(), 1806 PhoneNumberUtil.MatchType.EXACT_MATCH, 1807 phoneUtil.isNumberMatch(nzNumber, NZ_NUMBER)); 1808 1809 // Check raw_input, country_code_source and preferred_domestic_carrier_code are ignored. 1810 PhoneNumber brNumberOne = new PhoneNumber(); 1811 PhoneNumber brNumberTwo = new PhoneNumber(); 1812 brNumberOne.setCountryCode(55).setNationalNumber(3121286979L) 1813 .setCountryCodeSource(PhoneNumber.CountryCodeSource.FROM_NUMBER_WITH_PLUS_SIGN) 1814 .setPreferredDomesticCarrierCode("12").setRawInput("012 3121286979"); 1815 brNumberTwo.setCountryCode(55).setNationalNumber(3121286979L) 1816 .setCountryCodeSource(PhoneNumber.CountryCodeSource.FROM_DEFAULT_COUNTRY) 1817 .setPreferredDomesticCarrierCode("14").setRawInput("143121286979"); 1818 assertEquals(PhoneNumberUtil.MatchType.EXACT_MATCH, 1819 phoneUtil.isNumberMatch(brNumberOne, brNumberTwo)); 1820 } 1821 1822 public void testIsNumberMatchNonMatches() throws Exception { 1823 // Non-matches. 1824 assertEquals(PhoneNumberUtil.MatchType.NO_MATCH, 1825 phoneUtil.isNumberMatch("03 331 6005", "03 331 6006")); 1826 // Different country calling code, partial number match. 1827 assertEquals(PhoneNumberUtil.MatchType.NO_MATCH, 1828 phoneUtil.isNumberMatch("+64 3 331-6005", "+16433316005")); 1829 // Different country calling code, same number. 1830 assertEquals(PhoneNumberUtil.MatchType.NO_MATCH, 1831 phoneUtil.isNumberMatch("+64 3 331-6005", "+6133316005")); 1832 // Extension different, all else the same. 1833 assertEquals(PhoneNumberUtil.MatchType.NO_MATCH, 1834 phoneUtil.isNumberMatch("+64 3 331-6005 extn 1234", "0116433316005#1235")); 1835 // NSN matches, but extension is different - not the same number. 1836 assertEquals(PhoneNumberUtil.MatchType.NO_MATCH, 1837 phoneUtil.isNumberMatch("+64 3 331-6005 ext.1235", "3 331 6005#1234")); 1838 1839 // Invalid numbers that can't be parsed. 1840 assertEquals(PhoneNumberUtil.MatchType.NOT_A_NUMBER, 1841 phoneUtil.isNumberMatch("43", "3 331 6043")); 1842 assertEquals(PhoneNumberUtil.MatchType.NOT_A_NUMBER, 1843 phoneUtil.isNumberMatch("+43", "+64 3 331 6005")); 1844 assertEquals(PhoneNumberUtil.MatchType.NOT_A_NUMBER, 1845 phoneUtil.isNumberMatch("+43", "64 3 331 6005")); 1846 assertEquals(PhoneNumberUtil.MatchType.NOT_A_NUMBER, 1847 phoneUtil.isNumberMatch("Dog", "64 3 331 6005")); 1848 } 1849 1850 public void testIsNumberMatchNsnMatches() throws Exception { 1851 // NSN matches. 1852 assertEquals(PhoneNumberUtil.MatchType.NSN_MATCH, 1853 phoneUtil.isNumberMatch("+64 3 331-6005", "03 331 6005")); 1854 assertEquals(PhoneNumberUtil.MatchType.NSN_MATCH, 1855 phoneUtil.isNumberMatch(NZ_NUMBER, "03 331 6005")); 1856 // Here the second number possibly starts with the country calling code for New Zealand, 1857 // although we are unsure. 1858 PhoneNumber unchangedNzNumber = new PhoneNumber().mergeFrom(NZ_NUMBER); 1859 assertEquals(PhoneNumberUtil.MatchType.NSN_MATCH, 1860 phoneUtil.isNumberMatch(unchangedNzNumber, "(64-3) 331 6005")); 1861 // Check the phone number proto was not edited during the method call. 1862 assertEquals(NZ_NUMBER, unchangedNzNumber); 1863 1864 // Here, the 1 might be a national prefix, if we compare it to the US number, so the resultant 1865 // match is an NSN match. 1866 assertEquals(PhoneNumberUtil.MatchType.NSN_MATCH, 1867 phoneUtil.isNumberMatch(US_NUMBER, "1-650-253-0000")); 1868 assertEquals(PhoneNumberUtil.MatchType.NSN_MATCH, 1869 phoneUtil.isNumberMatch(US_NUMBER, "6502530000")); 1870 assertEquals(PhoneNumberUtil.MatchType.NSN_MATCH, 1871 phoneUtil.isNumberMatch("+1 650-253 0000", "1 650 253 0000")); 1872 assertEquals(PhoneNumberUtil.MatchType.NSN_MATCH, 1873 phoneUtil.isNumberMatch("1 650-253 0000", "1 650 253 0000")); 1874 assertEquals(PhoneNumberUtil.MatchType.NSN_MATCH, 1875 phoneUtil.isNumberMatch("1 650-253 0000", "+1 650 253 0000")); 1876 // For this case, the match will be a short NSN match, because we cannot assume that the 1 might 1877 // be a national prefix, so don't remove it when parsing. 1878 PhoneNumber randomNumber = new PhoneNumber(); 1879 randomNumber.setCountryCode(41).setNationalNumber(6502530000L); 1880 assertEquals(PhoneNumberUtil.MatchType.SHORT_NSN_MATCH, 1881 phoneUtil.isNumberMatch(randomNumber, "1-650-253-0000")); 1882 } 1883 1884 public void testIsNumberMatchShortNsnMatches() throws Exception { 1885 // Short NSN matches with the country not specified for either one or both numbers. 1886 assertEquals(PhoneNumberUtil.MatchType.SHORT_NSN_MATCH, 1887 phoneUtil.isNumberMatch("+64 3 331-6005", "331 6005")); 1888 // We did not know that the "0" was a national prefix since neither number has a country code, 1889 // so this is considered a SHORT_NSN_MATCH. 1890 assertEquals(PhoneNumberUtil.MatchType.SHORT_NSN_MATCH, 1891 phoneUtil.isNumberMatch("3 331-6005", "03 331 6005")); 1892 assertEquals(PhoneNumberUtil.MatchType.SHORT_NSN_MATCH, 1893 phoneUtil.isNumberMatch("3 331-6005", "331 6005")); 1894 assertEquals(PhoneNumberUtil.MatchType.SHORT_NSN_MATCH, 1895 phoneUtil.isNumberMatch("3 331-6005", "+64 331 6005")); 1896 // Short NSN match with the country specified. 1897 assertEquals(PhoneNumberUtil.MatchType.SHORT_NSN_MATCH, 1898 phoneUtil.isNumberMatch("03 331-6005", "331 6005")); 1899 assertEquals(PhoneNumberUtil.MatchType.SHORT_NSN_MATCH, 1900 phoneUtil.isNumberMatch("1 234 345 6789", "345 6789")); 1901 assertEquals(PhoneNumberUtil.MatchType.SHORT_NSN_MATCH, 1902 phoneUtil.isNumberMatch("+1 (234) 345 6789", "345 6789")); 1903 // NSN matches, country calling code omitted for one number, extension missing for one. 1904 assertEquals(PhoneNumberUtil.MatchType.SHORT_NSN_MATCH, 1905 phoneUtil.isNumberMatch("+64 3 331-6005", "3 331 6005#1234")); 1906 // One has Italian leading zero, one does not. 1907 PhoneNumber italianNumberOne = new PhoneNumber(); 1908 italianNumberOne.setCountryCode(39).setNationalNumber(1234L).setItalianLeadingZero(true); 1909 PhoneNumber italianNumberTwo = new PhoneNumber(); 1910 italianNumberTwo.setCountryCode(39).setNationalNumber(1234L); 1911 assertEquals(PhoneNumberUtil.MatchType.SHORT_NSN_MATCH, 1912 phoneUtil.isNumberMatch(italianNumberOne, italianNumberTwo)); 1913 // One has an extension, the other has an extension of "". 1914 italianNumberOne.setExtension("1234").clearItalianLeadingZero(); 1915 italianNumberTwo.setExtension(""); 1916 assertEquals(PhoneNumberUtil.MatchType.SHORT_NSN_MATCH, 1917 phoneUtil.isNumberMatch(italianNumberOne, italianNumberTwo)); 1918 } 1919 1920 public void testCanBeInternationallyDialled() throws Exception { 1921 // We have no-international-dialling rules for the US in our test metadata that say that 1922 // toll-free numbers cannot be dialled internationally. 1923 assertFalse(phoneUtil.canBeInternationallyDialled(US_TOLLFREE)); 1924 1925 // Normal US numbers can be internationally dialled. 1926 assertTrue(phoneUtil.canBeInternationallyDialled(US_NUMBER)); 1927 1928 // Invalid number. 1929 assertTrue(phoneUtil.canBeInternationallyDialled(US_LOCAL_NUMBER)); 1930 1931 // We have no data for NZ - should return true. 1932 assertTrue(phoneUtil.canBeInternationallyDialled(NZ_NUMBER)); 1933 } 1934 1935 public void testIsAlphaNumber() throws Exception { 1936 assertTrue(phoneUtil.isAlphaNumber("1800 six-flags")); 1937 assertTrue(phoneUtil.isAlphaNumber("1800 six-flags ext. 1234")); 1938 assertFalse(phoneUtil.isAlphaNumber("1800 123-1234")); 1939 assertFalse(phoneUtil.isAlphaNumber("1800 123-1234 extension: 1234")); 1940 } 1941 } 1942