1/* 2 * Copyright (C) 2006 The Android Open Source Project 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 17package com.android.internal.telephony; 18 19import android.test.AndroidTestCase; 20import android.test.suitebuilder.annotation.SmallTest; 21import android.text.SpannableStringBuilder; 22import android.telephony.PhoneNumberUtils; 23 24public class PhoneNumberUtilsTest extends AndroidTestCase { 25 26 @SmallTest 27 public void testExtractNetworkPortion() throws Exception { 28 assertEquals( 29 "+17005554141", 30 PhoneNumberUtils.extractNetworkPortion("+17005554141") 31 ); 32 33 assertEquals( 34 "+17005554141", 35 PhoneNumberUtils.extractNetworkPortion("+1 (700).555-4141") 36 ); 37 38 assertEquals( 39 "17005554141", 40 PhoneNumberUtils.extractNetworkPortion("1 (700).555-4141") 41 ); 42 43 // This may seem wrong, but it's probably ok 44 assertEquals( 45 "17005554141*#", 46 PhoneNumberUtils.extractNetworkPortion("1 (700).555-4141*#") 47 ); 48 49 assertEquals( 50 "170055541NN", 51 PhoneNumberUtils.extractNetworkPortion("1 (700).555-41NN") 52 ); 53 54 assertEquals( 55 "170055541NN", 56 PhoneNumberUtils.extractNetworkPortion("1 (700).555-41NN,1234") 57 ); 58 59 assertEquals( 60 "170055541NN", 61 PhoneNumberUtils.extractNetworkPortion("1 (700).555-41NN;1234") 62 ); 63 64 // An MMI string is unperterbed, even though it contains a 65 // (valid in this case) embedded + 66 assertEquals( 67 "**21**17005554141#", 68 PhoneNumberUtils.extractNetworkPortion("**21**+17005554141#") 69 //TODO this is the correct result, although the above 70 //result has been returned since change 31776 71 //"**21**+17005554141#" 72 ); 73 74 assertEquals("", PhoneNumberUtils.extractNetworkPortion("")); 75 76 assertEquals("", PhoneNumberUtils.extractNetworkPortion(",1234")); 77 78 byte [] b = new byte[20]; 79 b[0] = (byte) 0x81; b[1] = (byte) 0x71; b[2] = (byte) 0x00; b[3] = (byte) 0x55; 80 b[4] = (byte) 0x05; b[5] = (byte) 0x20; b[6] = (byte) 0xF0; 81 assertEquals("17005550020", 82 PhoneNumberUtils.calledPartyBCDToString(b, 0, 7)); 83 84 b[0] = (byte) 0x80; b[1] = (byte) 0x71; b[2] = (byte) 0x00; b[3] = (byte) 0x55; 85 b[4] = (byte) 0x05; b[5] = (byte) 0x20; b[6] = (byte) 0xF0; 86 assertEquals("17005550020", 87 PhoneNumberUtils.calledPartyBCDToString(b, 0, 7)); 88 89 b[0] = (byte) 0x90; b[1] = (byte) 0x71; b[2] = (byte) 0x00; b[3] = (byte) 0x55; 90 b[4] = (byte) 0x05; b[5] = (byte) 0x20; b[6] = (byte) 0xF0; 91 assertEquals("+17005550020", 92 PhoneNumberUtils.calledPartyBCDToString(b, 0, 7)); 93 94 b[0] = (byte) 0x91; b[1] = (byte) 0x71; b[2] = (byte) 0x00; b[3] = (byte) 0x55; 95 b[4] = (byte) 0x05; b[5] = (byte) 0x20; b[6] = (byte) 0xF0; 96 assertEquals("+17005550020", 97 PhoneNumberUtils.calledPartyBCDToString(b, 0, 7)); 98 99 byte[] bRet = PhoneNumberUtils.networkPortionToCalledPartyBCD("+17005550020"); 100 assertEquals(7, bRet.length); 101 for (int i = 0; i < 7; i++) { 102 assertEquals(b[i], bRet[i]); 103 } 104 105 bRet = PhoneNumberUtils.networkPortionToCalledPartyBCDWithLength("+17005550020"); 106 assertEquals(8, bRet.length); 107 assertEquals(bRet[0], 7); 108 for (int i = 1; i < 8; i++) { 109 assertEquals(b[i - 1], bRet[i]); 110 } 111 112 bRet = PhoneNumberUtils.networkPortionToCalledPartyBCD("7005550020"); 113 assertEquals("7005550020", 114 PhoneNumberUtils.calledPartyBCDToString(bRet, 0, bRet.length)); 115 116 b[0] = (byte) 0x81; b[1] = (byte) 0x71; b[2] = (byte) 0x00; b[3] = (byte) 0x55; 117 b[4] = (byte) 0x05; b[5] = (byte) 0x20; b[6] = (byte) 0xB0; 118 assertEquals("17005550020#", 119 PhoneNumberUtils.calledPartyBCDToString(b, 0, 7)); 120 121 b[0] = (byte) 0x91; b[1] = (byte) 0x71; b[2] = (byte) 0x00; b[3] = (byte) 0x55; 122 b[4] = (byte) 0x05; b[5] = (byte) 0x20; b[6] = (byte) 0xB0; 123 assertEquals("+17005550020#", 124 PhoneNumberUtils.calledPartyBCDToString(b, 0, 7)); 125 126 b[0] = (byte) 0x81; b[1] = (byte) 0x2A; b[2] = (byte) 0xB1; 127 assertEquals("*21#", 128 PhoneNumberUtils.calledPartyBCDToString(b, 0, 3)); 129 130 b[0] = (byte) 0x81; b[1] = (byte) 0x2B; b[2] = (byte) 0xB1; 131 assertEquals("#21#", 132 PhoneNumberUtils.calledPartyBCDToString(b, 0, 3)); 133 134 b[0] = (byte) 0x91; b[1] = (byte) 0x2A; b[2] = (byte) 0xB1; 135 assertEquals("*21#+", 136 PhoneNumberUtils.calledPartyBCDToString(b, 0, 3)); 137 138 b[0] = (byte) 0x81; b[1] = (byte) 0xAA; b[2] = (byte) 0x12; b[3] = (byte) 0xFB; 139 assertEquals("**21#", 140 PhoneNumberUtils.calledPartyBCDToString(b, 0, 4)); 141 142 b[0] = (byte) 0x91; b[1] = (byte) 0xAA; b[2] = (byte) 0x12; b[3] = (byte) 0xFB; 143 assertEquals("**21#+", 144 PhoneNumberUtils.calledPartyBCDToString(b, 0, 4)); 145 146 b[0] = (byte) 0x81; b[1] = (byte) 0x9A; b[2] = (byte) 0xA9; b[3] = (byte) 0x71; 147 b[4] = (byte) 0x00; b[5] = (byte) 0x55; b[6] = (byte) 0x05; b[7] = (byte) 0x20; 148 b[8] = (byte) 0xB0; 149 assertEquals("*99*17005550020#", 150 PhoneNumberUtils.calledPartyBCDToString(b, 0, 9)); 151 152 b[0] = (byte) 0x91; b[1] = (byte) 0x9A; b[2] = (byte) 0xA9; b[3] = (byte) 0x71; 153 b[4] = (byte) 0x00; b[5] = (byte) 0x55; b[6] = (byte) 0x05; b[7] = (byte) 0x20; 154 b[8] = (byte) 0xB0; 155 assertEquals("*99*+17005550020#", 156 PhoneNumberUtils.calledPartyBCDToString(b, 0, 9)); 157 158 b[0] = (byte) 0x81; b[1] = (byte) 0xAA; b[2] = (byte) 0x12; b[3] = (byte) 0x1A; 159 b[4] = (byte) 0x07; b[5] = (byte) 0x50; b[6] = (byte) 0x55; b[7] = (byte) 0x00; 160 b[8] = (byte) 0x02; b[9] = (byte) 0xFB; 161 assertEquals("**21*17005550020#", 162 PhoneNumberUtils.calledPartyBCDToString(b, 0, 10)); 163 164 b[0] = (byte) 0x91; b[1] = (byte) 0xAA; b[2] = (byte) 0x12; b[3] = (byte) 0x1A; 165 b[4] = (byte) 0x07; b[5] = (byte) 0x50; b[6] = (byte) 0x55; b[7] = (byte) 0x00; 166 b[8] = (byte) 0x02; b[9] = (byte) 0xFB; 167 assertEquals("**21*+17005550020#", 168 PhoneNumberUtils.calledPartyBCDToString(b, 0, 10)); 169 170 b[0] = (byte) 0x81; b[1] = (byte) 0x2A; b[2] = (byte) 0xA1; b[3] = (byte) 0x71; 171 b[4] = (byte) 0x00; b[5] = (byte) 0x55; b[6] = (byte) 0x05; b[7] = (byte) 0x20; 172 b[8] = (byte) 0xF0; 173 assertEquals("*21*17005550020", 174 PhoneNumberUtils.calledPartyBCDToString(b, 0, 9)); 175 176 b[0] = (byte) 0x91; b[1] = (byte) 0x2A; b[2] = (byte) 0xB1; b[3] = (byte) 0x71; 177 b[4] = (byte) 0x00; b[5] = (byte) 0x55; b[6] = (byte) 0x05; b[7] = (byte) 0x20; 178 b[8] = (byte) 0xF0; 179 assertEquals("*21#+17005550020", 180 PhoneNumberUtils.calledPartyBCDToString(b, 0, 9)); 181 182 assertNull(PhoneNumberUtils.extractNetworkPortion(null)); 183 assertNull(PhoneNumberUtils.extractPostDialPortion(null)); 184 assertTrue(PhoneNumberUtils.compare(null, null)); 185 assertFalse(PhoneNumberUtils.compare(null, "123")); 186 assertFalse(PhoneNumberUtils.compare("123", null)); 187 assertNull(PhoneNumberUtils.toCallerIDMinMatch(null)); 188 assertNull(PhoneNumberUtils.getStrippedReversed(null)); 189 assertNull(PhoneNumberUtils.stringFromStringAndTOA(null, 1)); 190 } 191 192 @SmallTest 193 public void testExtractNetworkPortionAlt() throws Exception { 194 assertEquals( 195 "+17005554141", 196 PhoneNumberUtils.extractNetworkPortionAlt("+17005554141") 197 ); 198 199 assertEquals( 200 "+17005554141", 201 PhoneNumberUtils.extractNetworkPortionAlt("+1 (700).555-4141") 202 ); 203 204 assertEquals( 205 "17005554141", 206 PhoneNumberUtils.extractNetworkPortionAlt("1 (700).555-4141") 207 ); 208 209 // This may seem wrong, but it's probably ok 210 assertEquals( 211 "17005554141*#", 212 PhoneNumberUtils.extractNetworkPortionAlt("1 (700).555-4141*#") 213 ); 214 215 assertEquals( 216 "170055541NN", 217 PhoneNumberUtils.extractNetworkPortionAlt("1 (700).555-41NN") 218 ); 219 220 assertEquals( 221 "170055541NN", 222 PhoneNumberUtils.extractNetworkPortionAlt("1 (700).555-41NN,1234") 223 ); 224 225 assertEquals( 226 "170055541NN", 227 PhoneNumberUtils.extractNetworkPortionAlt("1 (700).555-41NN;1234") 228 ); 229 230 // An MMI string is unperterbed, even though it contains a 231 // (valid in this case) embedded + 232 assertEquals( 233 "**21**+17005554141#", 234 PhoneNumberUtils.extractNetworkPortionAlt("**21**+17005554141#") 235 ); 236 237 assertEquals( 238 "*31#+447966164208", 239 PhoneNumberUtils.extractNetworkPortionAlt("*31#+447966164208") 240 ); 241 242 assertEquals( 243 "*31#+447966164208", 244 PhoneNumberUtils.extractNetworkPortionAlt("*31# (+44) 79 6616 4208") 245 ); 246 247 assertEquals("", PhoneNumberUtils.extractNetworkPortionAlt("")); 248 249 assertEquals("", PhoneNumberUtils.extractNetworkPortionAlt(",1234")); 250 251 assertNull(PhoneNumberUtils.extractNetworkPortionAlt(null)); 252 } 253 254 @SmallTest 255 public void testB() throws Exception { 256 assertEquals("", PhoneNumberUtils.extractPostDialPortion("+17005554141")); 257 assertEquals("", PhoneNumberUtils.extractPostDialPortion("+1 (700).555-4141")); 258 assertEquals("", PhoneNumberUtils.extractPostDialPortion("+1 (700).555-41NN")); 259 assertEquals(",1234", PhoneNumberUtils.extractPostDialPortion("+1 (700).555-41NN,1234")); 260 assertEquals(";1234", PhoneNumberUtils.extractPostDialPortion("+1 (700).555-41NN;1234")); 261 assertEquals(";1234,;N", 262 PhoneNumberUtils.extractPostDialPortion("+1 (700).555-41NN;1-2.34 ,;N")); 263 } 264 265 @SmallTest 266 public void testCompare() throws Exception { 267 // this is odd 268 assertFalse(PhoneNumberUtils.compare("", "")); 269 270 assertTrue(PhoneNumberUtils.compare("911", "911")); 271 assertFalse(PhoneNumberUtils.compare("911", "18005550911")); 272 assertTrue(PhoneNumberUtils.compare("5555", "5555")); 273 assertFalse(PhoneNumberUtils.compare("5555", "180055555555")); 274 275 assertTrue(PhoneNumberUtils.compare("+17005554141", "+17005554141")); 276 assertTrue(PhoneNumberUtils.compare("+17005554141", "+1 (700).555-4141")); 277 assertTrue(PhoneNumberUtils.compare("+17005554141", "+1 (700).555-4141,1234")); 278 assertTrue(PhoneNumberUtils.compare("+17005554141", "17005554141")); 279 assertTrue(PhoneNumberUtils.compare("+17005554141", "7005554141")); 280 assertTrue(PhoneNumberUtils.compare("+17005554141", "5554141")); 281 assertTrue(PhoneNumberUtils.compare("17005554141", "5554141")); 282 assertTrue(PhoneNumberUtils.compare("+17005554141", "01117005554141")); 283 assertTrue(PhoneNumberUtils.compare("+17005554141", "0017005554141")); 284 assertTrue(PhoneNumberUtils.compare("17005554141", "0017005554141")); 285 286 287 assertTrue(PhoneNumberUtils.compare("+17005554141", "**31#+17005554141")); 288 289 assertFalse(PhoneNumberUtils.compare("+1 999 7005554141", "+1 7005554141")); 290 assertTrue(PhoneNumberUtils.compare("011 1 7005554141", "7005554141")); 291 292 assertFalse(PhoneNumberUtils.compare("011 11 7005554141", "+17005554141")); 293 294 assertFalse(PhoneNumberUtils.compare("+17005554141", "7085882300")); 295 296 assertTrue(PhoneNumberUtils.compare("+44 207 792 3490", "0 207 792 3490")); 297 298 assertFalse(PhoneNumberUtils.compare("+44 207 792 3490", "00 207 792 3490")); 299 assertFalse(PhoneNumberUtils.compare("+44 207 792 3490", "011 207 792 3490")); 300 301 /***** FIXME's ******/ 302 // 303 // MMI header should be ignored 304 assertFalse(PhoneNumberUtils.compare("+17005554141", "**31#17005554141")); 305 306 // It's too bad this is false 307 // +44 (0) 207 792 3490 is not a dialable number 308 // but it is commonly how European phone numbers are written 309 assertFalse(PhoneNumberUtils.compare("+44 207 792 3490", "+44 (0) 207 792 3490")); 310 311 // The japanese international prefix, for example, messes us up 312 // But who uses a GSM phone in Japan? 313 assertFalse(PhoneNumberUtils.compare("+44 207 792 3490", "010 44 207 792 3490")); 314 315 // The Australian one messes us up too 316 assertFalse(PhoneNumberUtils.compare("+44 207 792 3490", "0011 44 207 792 3490")); 317 318 // The Russian trunk prefix messes us up, as does current 319 // Russian area codes (which bein with 0) 320 321 assertFalse(PhoneNumberUtils.compare("+7(095)9100766", "8(095)9100766")); 322 323 // 444 is not a valid country code, but 324 // matchIntlPrefixAndCC doesnt know this 325 assertTrue(PhoneNumberUtils.compare("+444 207 792 3490", "0 207 792 3490")); 326 327 // compare SMS short code 328 assertTrue(PhoneNumberUtils.compare("404-04", "40404")); 329 } 330 331 332 @SmallTest 333 public void testToCallerIDIndexable() throws Exception { 334 assertEquals("1414555", PhoneNumberUtils.toCallerIDMinMatch("17005554141")); 335 assertEquals("1414555", PhoneNumberUtils.toCallerIDMinMatch("1-700-555-4141")); 336 assertEquals("1414555", PhoneNumberUtils.toCallerIDMinMatch("1-700-555-4141,1234")); 337 assertEquals("1414555", PhoneNumberUtils.toCallerIDMinMatch("1-700-555-4141;1234")); 338 339 //this seems wrong, or at least useless 340 assertEquals("NN14555", PhoneNumberUtils.toCallerIDMinMatch("1-700-555-41NN")); 341 342 //<shrug> -- these are all not useful, but not terribly wrong 343 assertEquals("", PhoneNumberUtils.toCallerIDMinMatch("")); 344 assertEquals("0032", PhoneNumberUtils.toCallerIDMinMatch("2300")); 345 assertEquals("0032+", PhoneNumberUtils.toCallerIDMinMatch("+2300")); 346 assertEquals("#130#*", PhoneNumberUtils.toCallerIDMinMatch("*#031#")); 347 } 348 349 @SmallTest 350 public void testGetIndexable() throws Exception { 351 assertEquals("14145550071", PhoneNumberUtils.getStrippedReversed("1-700-555-4141")); 352 assertEquals("14145550071", PhoneNumberUtils.getStrippedReversed("1-700-555-4141,1234")); 353 assertEquals("14145550071", PhoneNumberUtils.getStrippedReversed("1-700-555-4141;1234")); 354 355 //this seems wrong, or at least useless 356 assertEquals("NN145550071", PhoneNumberUtils.getStrippedReversed("1-700-555-41NN")); 357 358 //<shrug> -- these are all not useful, but not terribly wrong 359 assertEquals("", PhoneNumberUtils.getStrippedReversed("")); 360 assertEquals("0032", PhoneNumberUtils.getStrippedReversed("2300")); 361 assertEquals("0032+", PhoneNumberUtils.getStrippedReversed("+2300")); 362 assertEquals("#130#*", PhoneNumberUtils.getStrippedReversed("*#031#")); 363 } 364 365 @SmallTest 366 public void testNanpFormatting() { 367 SpannableStringBuilder number = new SpannableStringBuilder(); 368 number.append("8005551212"); 369 PhoneNumberUtils.formatNanpNumber(number); 370 assertEquals("800-555-1212", number.toString()); 371 372 number.clear(); 373 number.append("800555121"); 374 PhoneNumberUtils.formatNanpNumber(number); 375 assertEquals("800-555-121", number.toString()); 376 377 number.clear(); 378 number.append("555-1212"); 379 PhoneNumberUtils.formatNanpNumber(number); 380 assertEquals("555-1212", number.toString()); 381 382 number.clear(); 383 number.append("800-55512"); 384 PhoneNumberUtils.formatNanpNumber(number); 385 assertEquals("800-555-12", number.toString()); 386 387 number.clear(); 388 number.append("46645"); 389 PhoneNumberUtils.formatNanpNumber(number); 390 assertEquals("46645", number.toString()); 391 } 392 393 @SmallTest 394 public void testConvertKeypadLettersToDigits() { 395 assertEquals("1-800-4664-411", 396 PhoneNumberUtils.convertKeypadLettersToDigits("1-800-GOOG-411")); 397 assertEquals("18004664411", 398 PhoneNumberUtils.convertKeypadLettersToDigits("1800GOOG411")); 399 assertEquals("1-800-466-4411", 400 PhoneNumberUtils.convertKeypadLettersToDigits("1-800-466-4411")); 401 assertEquals("18004664411", 402 PhoneNumberUtils.convertKeypadLettersToDigits("18004664411")); 403 assertEquals("222-333-444-555-666-7777-888-9999", 404 PhoneNumberUtils.convertKeypadLettersToDigits( 405 "ABC-DEF-GHI-JKL-MNO-PQRS-TUV-WXYZ")); 406 assertEquals("222-333-444-555-666-7777-888-9999", 407 PhoneNumberUtils.convertKeypadLettersToDigits( 408 "abc-def-ghi-jkl-mno-pqrs-tuv-wxyz")); 409 assertEquals("(800) 222-3334", 410 PhoneNumberUtils.convertKeypadLettersToDigits("(800) ABC-DEFG")); 411 } 412 413 // To run this test, the device has to be registered with network 414 public void testCheckAndProcessPlusCode() { 415 assertEquals("0118475797000", 416 PhoneNumberUtils.cdmaCheckAndProcessPlusCode("+8475797000")); 417 assertEquals("18475797000", 418 PhoneNumberUtils.cdmaCheckAndProcessPlusCode("+18475797000")); 419 assertEquals("0111234567", 420 PhoneNumberUtils.cdmaCheckAndProcessPlusCode("+1234567")); 421 assertEquals("01123456700000", 422 PhoneNumberUtils.cdmaCheckAndProcessPlusCode("+23456700000")); 423 assertEquals("01111875767800", 424 PhoneNumberUtils.cdmaCheckAndProcessPlusCode("+11875767800")); 425 assertEquals("8475797000,18475231753", 426 PhoneNumberUtils.cdmaCheckAndProcessPlusCode("8475797000,+18475231753")); 427 assertEquals("0118475797000,18475231753", 428 PhoneNumberUtils.cdmaCheckAndProcessPlusCode("+8475797000,+18475231753")); 429 assertEquals("8475797000;0118469312345", 430 PhoneNumberUtils.cdmaCheckAndProcessPlusCode("8475797000;+8469312345")); 431 assertEquals("8475797000,0111234567", 432 PhoneNumberUtils.cdmaCheckAndProcessPlusCode("8475797000,+1234567")); 433 assertEquals("847597000;01111875767000", 434 PhoneNumberUtils.cdmaCheckAndProcessPlusCode("847597000;+11875767000")); 435 assertEquals("8475797000,,0118469312345", 436 PhoneNumberUtils.cdmaCheckAndProcessPlusCode("8475797000,,+8469312345")); 437 assertEquals("8475797000;,0118469312345", 438 PhoneNumberUtils.cdmaCheckAndProcessPlusCode("8475797000;,+8469312345")); 439 assertEquals("8475797000,;18475231753", 440 PhoneNumberUtils.cdmaCheckAndProcessPlusCode("8475797000,;+18475231753")); 441 assertEquals("8475797000;,01111875767000", 442 PhoneNumberUtils.cdmaCheckAndProcessPlusCode("8475797000;,+11875767000")); 443 assertEquals("8475797000,;01111875767000", 444 PhoneNumberUtils.cdmaCheckAndProcessPlusCode("8475797000,;+11875767000")); 445 assertEquals("8475797000,,,01111875767000", 446 PhoneNumberUtils.cdmaCheckAndProcessPlusCode("8475797000,,,+11875767000")); 447 assertEquals("8475797000;,,01111875767000", 448 PhoneNumberUtils.cdmaCheckAndProcessPlusCode("8475797000;,,+11875767000")); 449 assertEquals("+;,8475797000", 450 PhoneNumberUtils.cdmaCheckAndProcessPlusCode("+;,8475797000")); 451 assertEquals("8475797000,", 452 PhoneNumberUtils.cdmaCheckAndProcessPlusCode("8475797000,")); 453 assertEquals("847+579-7000", 454 PhoneNumberUtils.cdmaCheckAndProcessPlusCode("847+579-7000")); 455 assertEquals(",8475797000", 456 PhoneNumberUtils.cdmaCheckAndProcessPlusCode(",8475797000")); 457 assertEquals(";;8475797000,,", 458 PhoneNumberUtils.cdmaCheckAndProcessPlusCode(";;8475797000,,")); 459 assertEquals("+this+is$weird;,+", 460 PhoneNumberUtils.cdmaCheckAndProcessPlusCode("+this+is$weird;,+")); 461 assertEquals("", 462 PhoneNumberUtils.cdmaCheckAndProcessPlusCode("")); 463 assertNull(PhoneNumberUtils.cdmaCheckAndProcessPlusCode(null)); 464 } 465 466 @SmallTest 467 public void testCheckAndProcessPlusCodeByNumberFormat() { 468 assertEquals("18475797000", 469 PhoneNumberUtils.cdmaCheckAndProcessPlusCodeByNumberFormat("+18475797000", 470 PhoneNumberUtils.FORMAT_NANP,PhoneNumberUtils.FORMAT_NANP)); 471 assertEquals("+18475797000", 472 PhoneNumberUtils.cdmaCheckAndProcessPlusCodeByNumberFormat("+18475797000", 473 PhoneNumberUtils.FORMAT_NANP,PhoneNumberUtils.FORMAT_JAPAN)); 474 assertEquals("+18475797000", 475 PhoneNumberUtils.cdmaCheckAndProcessPlusCodeByNumberFormat("+18475797000", 476 PhoneNumberUtils.FORMAT_NANP,PhoneNumberUtils.FORMAT_UNKNOWN)); 477 assertEquals("+18475797000", 478 PhoneNumberUtils.cdmaCheckAndProcessPlusCodeByNumberFormat("+18475797000", 479 PhoneNumberUtils.FORMAT_JAPAN,PhoneNumberUtils.FORMAT_JAPAN)); 480 assertEquals("+18475797000", 481 PhoneNumberUtils.cdmaCheckAndProcessPlusCodeByNumberFormat("+18475797000", 482 PhoneNumberUtils.FORMAT_UNKNOWN,PhoneNumberUtils.FORMAT_UNKNOWN)); 483 } 484 485 /** 486 * Basic checks for the VoiceMail number. 487 */ 488 @SmallTest 489 public void testWithNumberNotEqualToVoiceMail() throws Exception { 490 assertFalse(PhoneNumberUtils.isVoiceMailNumber("911")); 491 assertFalse(PhoneNumberUtils.isVoiceMailNumber("tel:911")); 492 assertFalse(PhoneNumberUtils.isVoiceMailNumber("+18001234567")); 493 assertFalse(PhoneNumberUtils.isVoiceMailNumber("")); 494 assertFalse(PhoneNumberUtils.isVoiceMailNumber(null)); 495 // This test fails on a device without a sim card 496 /*TelephonyManager mTelephonyManager = 497 (TelephonyManager)getContext().getSystemService(Context.TELEPHONY_SERVICE); 498 String mVoiceMailNumber = mTelephonyManager.getDefault().getVoiceMailNumber(); 499 assertTrue(PhoneNumberUtils.isVoiceMailNumber(mVoiceMailNumber)); 500 */ 501 } 502 503 @SmallTest 504 public void testFormatNumberToE164() { 505 // Note: ISO 3166-1 only allows upper case country codes. 506 assertEquals("+16502910000", PhoneNumberUtils.formatNumberToE164("650 2910000", "US")); 507 assertNull(PhoneNumberUtils.formatNumberToE164("1234567", "US")); 508 assertEquals("+18004664114", PhoneNumberUtils.formatNumberToE164("800-GOOG-114", "US")); 509 } 510 511 @SmallTest 512 public void testFormatNumber() { 513 assertEquals("(650) 291-0000", PhoneNumberUtils.formatNumber("650 2910000", "US")); 514 assertEquals("123-4567", PhoneNumberUtils.formatNumber("1234567", "US")); 515 assertEquals("(800) 466-4114", PhoneNumberUtils.formatNumber("800-GOOG-114", "US")); 516 } 517 518 @SmallTest 519 public void testFormatNumber_LeadingStarAndHash() { 520 // Numbers with a leading '*' or '#' should be left unchanged. 521 assertEquals("*650 2910000", PhoneNumberUtils.formatNumber("*650 2910000", "US")); 522 assertEquals("#650 2910000", PhoneNumberUtils.formatNumber("#650 2910000", "US")); 523 assertEquals("*#650 2910000", PhoneNumberUtils.formatNumber("*#650 2910000", "US")); 524 assertEquals("#*650 2910000", PhoneNumberUtils.formatNumber("#*650 2910000", "US")); 525 assertEquals("#650*2910000", PhoneNumberUtils.formatNumber("#650*2910000", "US")); 526 assertEquals("#650*2910000", PhoneNumberUtils.formatNumber("#650*2910000", "US")); 527 assertEquals("##650 2910000", PhoneNumberUtils.formatNumber("##650 2910000", "US")); 528 assertEquals("**650 2910000", PhoneNumberUtils.formatNumber("**650 2910000", "US")); 529 } 530 531 @SmallTest 532 public void testNormalizeNumber() { 533 assertEquals("6502910000", PhoneNumberUtils.normalizeNumber("650 2910000")); 534 assertEquals("1234567", PhoneNumberUtils.normalizeNumber("12,3#4*567")); 535 assertEquals("8004664114", PhoneNumberUtils.normalizeNumber("800-GOOG-114")); 536 assertEquals("+16502910000", PhoneNumberUtils.normalizeNumber("+1 650 2910000")); 537 } 538 539 @SmallTest 540 public void testFormatDailabeNumber() { 541 // Using the phoneNumberE164's country code 542 assertEquals("(650) 291-0000", 543 PhoneNumberUtils.formatNumber("6502910000", "+16502910000", "CN")); 544 // The phoneNumberE164 is null 545 assertEquals("(650) 291-0000", PhoneNumberUtils.formatNumber("6502910000", null, "US")); 546 // The given number has a country code. 547 assertEquals("+1 650-291-0000", PhoneNumberUtils.formatNumber("+16502910000", null, "CN")); 548 // The given number was formatted. 549 assertEquals("650-291-0000", PhoneNumberUtils.formatNumber("650-291-0000", null, "US")); 550 } 551 @SmallTest 552 public void testIsEmergencyNumber() { 553 // There are two parallel sets of tests here: one for the 554 // regular isEmergencyNumber() method, and the other for 555 // isPotentialEmergencyNumber(). 556 // 557 // (The difference is that isEmergencyNumber() will return true 558 // only if the specified number exactly matches an actual 559 // emergency number, but isPotentialEmergencyNumber() will 560 // return true if the specified number simply starts with the 561 // same digits as any actual emergency number.) 562 563 // Tests for isEmergencyNumber(): 564 assertTrue(PhoneNumberUtils.isEmergencyNumber("911", "US")); 565 assertTrue(PhoneNumberUtils.isEmergencyNumber("112", "US")); 566 // The next two numbers are not valid phone numbers in the US, 567 // so do not count as emergency numbers (but they *are* "potential" 568 // emergency numbers; see below.) 569 assertFalse(PhoneNumberUtils.isEmergencyNumber("91112345", "US")); 570 assertFalse(PhoneNumberUtils.isEmergencyNumber("11212345", "US")); 571 // A valid mobile phone number from Singapore shouldn't be classified as an emergency number 572 // in Singapore, as 911 is not an emergency number there. 573 assertFalse(PhoneNumberUtils.isEmergencyNumber("91121234", "SG")); 574 // A valid fixed-line phone number from Brazil shouldn't be classified as an emergency number 575 // in Brazil, as 112 is not an emergency number there. 576 assertFalse(PhoneNumberUtils.isEmergencyNumber("1121234567", "BR")); 577 // A valid local phone number from Brazil shouldn't be classified as an emergency number in 578 // Brazil. 579 assertFalse(PhoneNumberUtils.isEmergencyNumber("91112345", "BR")); 580 581 // Tests for isPotentialEmergencyNumber(): 582 // These first two are obviously emergency numbers: 583 assertTrue(PhoneNumberUtils.isPotentialEmergencyNumber("911", "US")); 584 assertTrue(PhoneNumberUtils.isPotentialEmergencyNumber("112", "US")); 585 // The next two numbers are not valid phone numbers in the US, but can be used to trick the 586 // system to dial 911 and 112, which are emergency numbers in the US. For the purpose of 587 // addressing that, they are also classified as "potential" emergency numbers in the US. 588 assertTrue(PhoneNumberUtils.isPotentialEmergencyNumber("91112345", "US")); 589 assertTrue(PhoneNumberUtils.isPotentialEmergencyNumber("11212345", "US")); 590 // A valid mobile phone number from Singapore shouldn't be classified as an emergency number 591 // in Singapore, as 911 is not an emergency number there. 592 assertFalse(PhoneNumberUtils.isPotentialEmergencyNumber("91121234", "SG")); 593 // A valid fixed-line phone number from Brazil shouldn't be classified as an emergency number 594 // in Brazil, as 112 is not an emergency number there. 595 assertFalse(PhoneNumberUtils.isPotentialEmergencyNumber("1121234567", "BR")); 596 // A valid local phone number from Brazil shouldn't be classified as an emergency number in 597 // Brazil. 598 assertFalse(PhoneNumberUtils.isPotentialEmergencyNumber("91112345", "BR")); 599 } 600} 601