1 /* 2 * Copyright (C) 2009 The Libphonenumber Authors 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 /** 20 * Unit tests for AsYouTypeFormatter.java 21 * 22 * Note that these tests use the test metadata, not the normal metadata file, so should not be used 23 * for regression test purposes - these tests are illustrative only and test functionality. 24 * 25 * @author Shaopeng Jia 26 */ 27 public class AsYouTypeFormatterTest extends TestMetadataTestCase { 28 29 public void testInvalidRegion() { 30 AsYouTypeFormatter formatter = phoneUtil.getAsYouTypeFormatter(RegionCode.ZZ); 31 assertEquals("+", formatter.inputDigit('+')); 32 assertEquals("+4", formatter.inputDigit('4')); 33 assertEquals("+48 ", formatter.inputDigit('8')); 34 assertEquals("+48 8", formatter.inputDigit('8')); 35 assertEquals("+48 88", formatter.inputDigit('8')); 36 assertEquals("+48 88 1", formatter.inputDigit('1')); 37 assertEquals("+48 88 12", formatter.inputDigit('2')); 38 assertEquals("+48 88 123", formatter.inputDigit('3')); 39 assertEquals("+48 88 123 1", formatter.inputDigit('1')); 40 assertEquals("+48 88 123 12", formatter.inputDigit('2')); 41 42 formatter.clear(); 43 assertEquals("6", formatter.inputDigit('6')); 44 assertEquals("65", formatter.inputDigit('5')); 45 assertEquals("650", formatter.inputDigit('0')); 46 assertEquals("6502", formatter.inputDigit('2')); 47 assertEquals("65025", formatter.inputDigit('5')); 48 assertEquals("650253", formatter.inputDigit('3')); 49 } 50 51 public void testInvalidPlusSign() { 52 AsYouTypeFormatter formatter = phoneUtil.getAsYouTypeFormatter(RegionCode.ZZ); 53 assertEquals("+", formatter.inputDigit('+')); 54 assertEquals("+4", formatter.inputDigit('4')); 55 assertEquals("+48 ", formatter.inputDigit('8')); 56 assertEquals("+48 8", formatter.inputDigit('8')); 57 assertEquals("+48 88", formatter.inputDigit('8')); 58 assertEquals("+48 88 1", formatter.inputDigit('1')); 59 assertEquals("+48 88 12", formatter.inputDigit('2')); 60 assertEquals("+48 88 123", formatter.inputDigit('3')); 61 assertEquals("+48 88 123 1", formatter.inputDigit('1')); 62 // A plus sign can only appear at the beginning of the number; otherwise, no formatting is 63 // applied. 64 assertEquals("+48881231+", formatter.inputDigit('+')); 65 assertEquals("+48881231+2", formatter.inputDigit('2')); 66 } 67 68 public void testTooLongNumberMatchingMultipleLeadingDigits() { 69 // See http://code.google.com/p/libphonenumber/issues/detail?id=36 70 // The bug occurred last time for countries which have two formatting rules with exactly the 71 // same leading digits pattern but differ in length. 72 AsYouTypeFormatter formatter = phoneUtil.getAsYouTypeFormatter(RegionCode.ZZ); 73 assertEquals("+", formatter.inputDigit('+')); 74 assertEquals("+8", formatter.inputDigit('8')); 75 assertEquals("+81 ", formatter.inputDigit('1')); 76 assertEquals("+81 9", formatter.inputDigit('9')); 77 assertEquals("+81 90", formatter.inputDigit('0')); 78 assertEquals("+81 90 1", formatter.inputDigit('1')); 79 assertEquals("+81 90 12", formatter.inputDigit('2')); 80 assertEquals("+81 90 123", formatter.inputDigit('3')); 81 assertEquals("+81 90 1234", formatter.inputDigit('4')); 82 assertEquals("+81 90 1234 5", formatter.inputDigit('5')); 83 assertEquals("+81 90 1234 56", formatter.inputDigit('6')); 84 assertEquals("+81 90 1234 567", formatter.inputDigit('7')); 85 assertEquals("+81 90 1234 5678", formatter.inputDigit('8')); 86 assertEquals("+81 90 12 345 6789", formatter.inputDigit('9')); 87 assertEquals("+81901234567890", formatter.inputDigit('0')); 88 assertEquals("+819012345678901", formatter.inputDigit('1')); 89 } 90 91 public void testCountryWithSpaceInNationalPrefixFormattingRule() { 92 AsYouTypeFormatter formatter = phoneUtil.getAsYouTypeFormatter(RegionCode.BY); 93 assertEquals("8", formatter.inputDigit('8')); 94 assertEquals("88", formatter.inputDigit('8')); 95 assertEquals("881", formatter.inputDigit('1')); 96 assertEquals("8 819", formatter.inputDigit('9')); 97 assertEquals("8 8190", formatter.inputDigit('0')); 98 // The formatting rule for 5 digit numbers states that no space should be present after the 99 // national prefix. 100 assertEquals("881 901", formatter.inputDigit('1')); 101 assertEquals("8 819 012", formatter.inputDigit('2')); 102 // Too long, no formatting rule applies. 103 assertEquals("88190123", formatter.inputDigit('3')); 104 } 105 106 public void testCountryWithSpaceInNationalPrefixFormattingRuleAndLongNdd() { 107 AsYouTypeFormatter formatter = phoneUtil.getAsYouTypeFormatter(RegionCode.BY); 108 assertEquals("9", formatter.inputDigit('9')); 109 assertEquals("99", formatter.inputDigit('9')); 110 assertEquals("999", formatter.inputDigit('9')); 111 assertEquals("9999", formatter.inputDigit('9')); 112 assertEquals("99999 ", formatter.inputDigit('9')); 113 assertEquals("99999 1", formatter.inputDigit('1')); 114 assertEquals("99999 12", formatter.inputDigit('2')); 115 assertEquals("99999 123", formatter.inputDigit('3')); 116 assertEquals("99999 1234", formatter.inputDigit('4')); 117 assertEquals("99999 12 345", formatter.inputDigit('5')); 118 } 119 120 public void testAYTFUS() { 121 AsYouTypeFormatter formatter = phoneUtil.getAsYouTypeFormatter(RegionCode.US); 122 assertEquals("6", formatter.inputDigit('6')); 123 assertEquals("65", formatter.inputDigit('5')); 124 assertEquals("650", formatter.inputDigit('0')); 125 assertEquals("650 2", formatter.inputDigit('2')); 126 assertEquals("650 25", formatter.inputDigit('5')); 127 assertEquals("650 253", formatter.inputDigit('3')); 128 // Note this is how a US local number (without area code) should be formatted. 129 assertEquals("650 2532", formatter.inputDigit('2')); 130 assertEquals("650 253 22", formatter.inputDigit('2')); 131 assertEquals("650 253 222", formatter.inputDigit('2')); 132 assertEquals("650 253 2222", formatter.inputDigit('2')); 133 134 formatter.clear(); 135 assertEquals("1", formatter.inputDigit('1')); 136 assertEquals("16", formatter.inputDigit('6')); 137 assertEquals("1 65", formatter.inputDigit('5')); 138 assertEquals("1 650", formatter.inputDigit('0')); 139 assertEquals("1 650 2", formatter.inputDigit('2')); 140 assertEquals("1 650 25", formatter.inputDigit('5')); 141 assertEquals("1 650 253", formatter.inputDigit('3')); 142 assertEquals("1 650 253 2", formatter.inputDigit('2')); 143 assertEquals("1 650 253 22", formatter.inputDigit('2')); 144 assertEquals("1 650 253 222", formatter.inputDigit('2')); 145 assertEquals("1 650 253 2222", formatter.inputDigit('2')); 146 147 formatter.clear(); 148 assertEquals("0", formatter.inputDigit('0')); 149 assertEquals("01", formatter.inputDigit('1')); 150 assertEquals("011 ", formatter.inputDigit('1')); 151 assertEquals("011 4", formatter.inputDigit('4')); 152 assertEquals("011 44 ", formatter.inputDigit('4')); 153 assertEquals("011 44 6", formatter.inputDigit('6')); 154 assertEquals("011 44 61", formatter.inputDigit('1')); 155 assertEquals("011 44 6 12", formatter.inputDigit('2')); 156 assertEquals("011 44 6 123", formatter.inputDigit('3')); 157 assertEquals("011 44 6 123 1", formatter.inputDigit('1')); 158 assertEquals("011 44 6 123 12", formatter.inputDigit('2')); 159 assertEquals("011 44 6 123 123", formatter.inputDigit('3')); 160 assertEquals("011 44 6 123 123 1", formatter.inputDigit('1')); 161 assertEquals("011 44 6 123 123 12", formatter.inputDigit('2')); 162 assertEquals("011 44 6 123 123 123", formatter.inputDigit('3')); 163 164 formatter.clear(); 165 assertEquals("0", formatter.inputDigit('0')); 166 assertEquals("01", formatter.inputDigit('1')); 167 assertEquals("011 ", formatter.inputDigit('1')); 168 assertEquals("011 5", formatter.inputDigit('5')); 169 assertEquals("011 54 ", formatter.inputDigit('4')); 170 assertEquals("011 54 9", formatter.inputDigit('9')); 171 assertEquals("011 54 91", formatter.inputDigit('1')); 172 assertEquals("011 54 9 11", formatter.inputDigit('1')); 173 assertEquals("011 54 9 11 2", formatter.inputDigit('2')); 174 assertEquals("011 54 9 11 23", formatter.inputDigit('3')); 175 assertEquals("011 54 9 11 231", formatter.inputDigit('1')); 176 assertEquals("011 54 9 11 2312", formatter.inputDigit('2')); 177 assertEquals("011 54 9 11 2312 1", formatter.inputDigit('1')); 178 assertEquals("011 54 9 11 2312 12", formatter.inputDigit('2')); 179 assertEquals("011 54 9 11 2312 123", formatter.inputDigit('3')); 180 assertEquals("011 54 9 11 2312 1234", formatter.inputDigit('4')); 181 182 formatter.clear(); 183 assertEquals("0", formatter.inputDigit('0')); 184 assertEquals("01", formatter.inputDigit('1')); 185 assertEquals("011 ", formatter.inputDigit('1')); 186 assertEquals("011 2", formatter.inputDigit('2')); 187 assertEquals("011 24", formatter.inputDigit('4')); 188 assertEquals("011 244 ", formatter.inputDigit('4')); 189 assertEquals("011 244 2", formatter.inputDigit('2')); 190 assertEquals("011 244 28", formatter.inputDigit('8')); 191 assertEquals("011 244 280", formatter.inputDigit('0')); 192 assertEquals("011 244 280 0", formatter.inputDigit('0')); 193 assertEquals("011 244 280 00", formatter.inputDigit('0')); 194 assertEquals("011 244 280 000", formatter.inputDigit('0')); 195 assertEquals("011 244 280 000 0", formatter.inputDigit('0')); 196 assertEquals("011 244 280 000 00", formatter.inputDigit('0')); 197 assertEquals("011 244 280 000 000", formatter.inputDigit('0')); 198 199 formatter.clear(); 200 assertEquals("+", formatter.inputDigit('+')); 201 assertEquals("+4", formatter.inputDigit('4')); 202 assertEquals("+48 ", formatter.inputDigit('8')); 203 assertEquals("+48 8", formatter.inputDigit('8')); 204 assertEquals("+48 88", formatter.inputDigit('8')); 205 assertEquals("+48 88 1", formatter.inputDigit('1')); 206 assertEquals("+48 88 12", formatter.inputDigit('2')); 207 assertEquals("+48 88 123", formatter.inputDigit('3')); 208 assertEquals("+48 88 123 1", formatter.inputDigit('1')); 209 assertEquals("+48 88 123 12", formatter.inputDigit('2')); 210 assertEquals("+48 88 123 12 1", formatter.inputDigit('1')); 211 assertEquals("+48 88 123 12 12", formatter.inputDigit('2')); 212 } 213 214 public void testAYTFUSFullWidthCharacters() { 215 AsYouTypeFormatter formatter = phoneUtil.getAsYouTypeFormatter(RegionCode.US); 216 assertEquals("\uFF16", formatter.inputDigit('\uFF16')); 217 assertEquals("\uFF16\uFF15", formatter.inputDigit('\uFF15')); 218 assertEquals("650", formatter.inputDigit('\uFF10')); 219 assertEquals("650 2", formatter.inputDigit('\uFF12')); 220 assertEquals("650 25", formatter.inputDigit('\uFF15')); 221 assertEquals("650 253", formatter.inputDigit('\uFF13')); 222 assertEquals("650 2532", formatter.inputDigit('\uFF12')); 223 assertEquals("650 253 22", formatter.inputDigit('\uFF12')); 224 assertEquals("650 253 222", formatter.inputDigit('\uFF12')); 225 assertEquals("650 253 2222", formatter.inputDigit('\uFF12')); 226 } 227 228 public void testAYTFUSMobileShortCode() { 229 AsYouTypeFormatter formatter = phoneUtil.getAsYouTypeFormatter(RegionCode.US); 230 assertEquals("*", formatter.inputDigit('*')); 231 assertEquals("*1", formatter.inputDigit('1')); 232 assertEquals("*12", formatter.inputDigit('2')); 233 assertEquals("*121", formatter.inputDigit('1')); 234 assertEquals("*121#", formatter.inputDigit('#')); 235 } 236 237 public void testAYTFUSVanityNumber() { 238 AsYouTypeFormatter formatter = phoneUtil.getAsYouTypeFormatter(RegionCode.US); 239 assertEquals("8", formatter.inputDigit('8')); 240 assertEquals("80", formatter.inputDigit('0')); 241 assertEquals("800", formatter.inputDigit('0')); 242 assertEquals("800 ", formatter.inputDigit(' ')); 243 assertEquals("800 M", formatter.inputDigit('M')); 244 assertEquals("800 MY", formatter.inputDigit('Y')); 245 assertEquals("800 MY ", formatter.inputDigit(' ')); 246 assertEquals("800 MY A", formatter.inputDigit('A')); 247 assertEquals("800 MY AP", formatter.inputDigit('P')); 248 assertEquals("800 MY APP", formatter.inputDigit('P')); 249 assertEquals("800 MY APPL", formatter.inputDigit('L')); 250 assertEquals("800 MY APPLE", formatter.inputDigit('E')); 251 } 252 253 public void testAYTFAndRememberPositionUS() { 254 AsYouTypeFormatter formatter = phoneUtil.getAsYouTypeFormatter(RegionCode.US); 255 assertEquals("1", formatter.inputDigitAndRememberPosition('1')); 256 assertEquals(1, formatter.getRememberedPosition()); 257 assertEquals("16", formatter.inputDigit('6')); 258 assertEquals("1 65", formatter.inputDigit('5')); 259 assertEquals(1, formatter.getRememberedPosition()); 260 assertEquals("1 650", formatter.inputDigitAndRememberPosition('0')); 261 assertEquals(5, formatter.getRememberedPosition()); 262 assertEquals("1 650 2", formatter.inputDigit('2')); 263 assertEquals("1 650 25", formatter.inputDigit('5')); 264 // Note the remembered position for digit "0" changes from 4 to 5, because a space is now 265 // inserted in the front. 266 assertEquals(5, formatter.getRememberedPosition()); 267 assertEquals("1 650 253", formatter.inputDigit('3')); 268 assertEquals("1 650 253 2", formatter.inputDigit('2')); 269 assertEquals("1 650 253 22", formatter.inputDigit('2')); 270 assertEquals(5, formatter.getRememberedPosition()); 271 assertEquals("1 650 253 222", formatter.inputDigitAndRememberPosition('2')); 272 assertEquals(13, formatter.getRememberedPosition()); 273 assertEquals("1 650 253 2222", formatter.inputDigit('2')); 274 assertEquals(13, formatter.getRememberedPosition()); 275 assertEquals("165025322222", formatter.inputDigit('2')); 276 assertEquals(10, formatter.getRememberedPosition()); 277 assertEquals("1650253222222", formatter.inputDigit('2')); 278 assertEquals(10, formatter.getRememberedPosition()); 279 280 formatter.clear(); 281 assertEquals("1", formatter.inputDigit('1')); 282 assertEquals("16", formatter.inputDigitAndRememberPosition('6')); 283 assertEquals(2, formatter.getRememberedPosition()); 284 assertEquals("1 65", formatter.inputDigit('5')); 285 assertEquals("1 650", formatter.inputDigit('0')); 286 assertEquals(3, formatter.getRememberedPosition()); 287 assertEquals("1 650 2", formatter.inputDigit('2')); 288 assertEquals("1 650 25", formatter.inputDigit('5')); 289 assertEquals(3, formatter.getRememberedPosition()); 290 assertEquals("1 650 253", formatter.inputDigit('3')); 291 assertEquals("1 650 253 2", formatter.inputDigit('2')); 292 assertEquals("1 650 253 22", formatter.inputDigit('2')); 293 assertEquals(3, formatter.getRememberedPosition()); 294 assertEquals("1 650 253 222", formatter.inputDigit('2')); 295 assertEquals("1 650 253 2222", formatter.inputDigit('2')); 296 assertEquals("165025322222", formatter.inputDigit('2')); 297 assertEquals(2, formatter.getRememberedPosition()); 298 assertEquals("1650253222222", formatter.inputDigit('2')); 299 assertEquals(2, formatter.getRememberedPosition()); 300 301 formatter.clear(); 302 assertEquals("6", formatter.inputDigit('6')); 303 assertEquals("65", formatter.inputDigit('5')); 304 assertEquals("650", formatter.inputDigit('0')); 305 assertEquals("650 2", formatter.inputDigit('2')); 306 assertEquals("650 25", formatter.inputDigit('5')); 307 assertEquals("650 253", formatter.inputDigit('3')); 308 assertEquals("650 2532", formatter.inputDigitAndRememberPosition('2')); 309 assertEquals(8, formatter.getRememberedPosition()); 310 assertEquals("650 253 22", formatter.inputDigit('2')); 311 assertEquals(9, formatter.getRememberedPosition()); 312 assertEquals("650 253 222", formatter.inputDigit('2')); 313 // No more formatting when semicolon is entered. 314 assertEquals("650253222;", formatter.inputDigit(';')); 315 assertEquals(7, formatter.getRememberedPosition()); 316 assertEquals("650253222;2", formatter.inputDigit('2')); 317 318 formatter.clear(); 319 assertEquals("6", formatter.inputDigit('6')); 320 assertEquals("65", formatter.inputDigit('5')); 321 assertEquals("650", formatter.inputDigit('0')); 322 // No more formatting when users choose to do their own formatting. 323 assertEquals("650-", formatter.inputDigit('-')); 324 assertEquals("650-2", formatter.inputDigitAndRememberPosition('2')); 325 assertEquals(5, formatter.getRememberedPosition()); 326 assertEquals("650-25", formatter.inputDigit('5')); 327 assertEquals(5, formatter.getRememberedPosition()); 328 assertEquals("650-253", formatter.inputDigit('3')); 329 assertEquals(5, formatter.getRememberedPosition()); 330 assertEquals("650-253-", formatter.inputDigit('-')); 331 assertEquals("650-253-2", formatter.inputDigit('2')); 332 assertEquals("650-253-22", formatter.inputDigit('2')); 333 assertEquals("650-253-222", formatter.inputDigit('2')); 334 assertEquals("650-253-2222", formatter.inputDigit('2')); 335 336 formatter.clear(); 337 assertEquals("0", formatter.inputDigit('0')); 338 assertEquals("01", formatter.inputDigit('1')); 339 assertEquals("011 ", formatter.inputDigit('1')); 340 assertEquals("011 4", formatter.inputDigitAndRememberPosition('4')); 341 assertEquals("011 48 ", formatter.inputDigit('8')); 342 assertEquals(5, formatter.getRememberedPosition()); 343 assertEquals("011 48 8", formatter.inputDigit('8')); 344 assertEquals(5, formatter.getRememberedPosition()); 345 assertEquals("011 48 88", formatter.inputDigit('8')); 346 assertEquals("011 48 88 1", formatter.inputDigit('1')); 347 assertEquals("011 48 88 12", formatter.inputDigit('2')); 348 assertEquals(5, formatter.getRememberedPosition()); 349 assertEquals("011 48 88 123", formatter.inputDigit('3')); 350 assertEquals("011 48 88 123 1", formatter.inputDigit('1')); 351 assertEquals("011 48 88 123 12", formatter.inputDigit('2')); 352 assertEquals("011 48 88 123 12 1", formatter.inputDigit('1')); 353 assertEquals("011 48 88 123 12 12", formatter.inputDigit('2')); 354 355 formatter.clear(); 356 assertEquals("+", formatter.inputDigit('+')); 357 assertEquals("+1", formatter.inputDigit('1')); 358 assertEquals("+1 6", formatter.inputDigitAndRememberPosition('6')); 359 assertEquals("+1 65", formatter.inputDigit('5')); 360 assertEquals("+1 650", formatter.inputDigit('0')); 361 assertEquals(4, formatter.getRememberedPosition()); 362 assertEquals("+1 650 2", formatter.inputDigit('2')); 363 assertEquals(4, formatter.getRememberedPosition()); 364 assertEquals("+1 650 25", formatter.inputDigit('5')); 365 assertEquals("+1 650 253", formatter.inputDigitAndRememberPosition('3')); 366 assertEquals("+1 650 253 2", formatter.inputDigit('2')); 367 assertEquals("+1 650 253 22", formatter.inputDigit('2')); 368 assertEquals("+1 650 253 222", formatter.inputDigit('2')); 369 assertEquals(10, formatter.getRememberedPosition()); 370 371 formatter.clear(); 372 assertEquals("+", formatter.inputDigit('+')); 373 assertEquals("+1", formatter.inputDigit('1')); 374 assertEquals("+1 6", formatter.inputDigitAndRememberPosition('6')); 375 assertEquals("+1 65", formatter.inputDigit('5')); 376 assertEquals("+1 650", formatter.inputDigit('0')); 377 assertEquals(4, formatter.getRememberedPosition()); 378 assertEquals("+1 650 2", formatter.inputDigit('2')); 379 assertEquals(4, formatter.getRememberedPosition()); 380 assertEquals("+1 650 25", formatter.inputDigit('5')); 381 assertEquals("+1 650 253", formatter.inputDigit('3')); 382 assertEquals("+1 650 253 2", formatter.inputDigit('2')); 383 assertEquals("+1 650 253 22", formatter.inputDigit('2')); 384 assertEquals("+1 650 253 222", formatter.inputDigit('2')); 385 assertEquals("+1650253222;", formatter.inputDigit(';')); 386 assertEquals(3, formatter.getRememberedPosition()); 387 } 388 389 public void testAYTFGBFixedLine() { 390 AsYouTypeFormatter formatter = phoneUtil.getAsYouTypeFormatter(RegionCode.GB); 391 assertEquals("0", formatter.inputDigit('0')); 392 assertEquals("02", formatter.inputDigit('2')); 393 assertEquals("020", formatter.inputDigit('0')); 394 assertEquals("020 7", formatter.inputDigitAndRememberPosition('7')); 395 assertEquals(5, formatter.getRememberedPosition()); 396 assertEquals("020 70", formatter.inputDigit('0')); 397 assertEquals("020 703", formatter.inputDigit('3')); 398 assertEquals(5, formatter.getRememberedPosition()); 399 assertEquals("020 7031", formatter.inputDigit('1')); 400 assertEquals("020 7031 3", formatter.inputDigit('3')); 401 assertEquals("020 7031 30", formatter.inputDigit('0')); 402 assertEquals("020 7031 300", formatter.inputDigit('0')); 403 assertEquals("020 7031 3000", formatter.inputDigit('0')); 404 } 405 406 public void testAYTFGBTollFree() { 407 AsYouTypeFormatter formatter = phoneUtil.getAsYouTypeFormatter(RegionCode.GB); 408 assertEquals("0", formatter.inputDigit('0')); 409 assertEquals("08", formatter.inputDigit('8')); 410 assertEquals("080", formatter.inputDigit('0')); 411 assertEquals("080 7", formatter.inputDigit('7')); 412 assertEquals("080 70", formatter.inputDigit('0')); 413 assertEquals("080 703", formatter.inputDigit('3')); 414 assertEquals("080 7031", formatter.inputDigit('1')); 415 assertEquals("080 7031 3", formatter.inputDigit('3')); 416 assertEquals("080 7031 30", formatter.inputDigit('0')); 417 assertEquals("080 7031 300", formatter.inputDigit('0')); 418 assertEquals("080 7031 3000", formatter.inputDigit('0')); 419 } 420 421 public void testAYTFGBPremiumRate() { 422 AsYouTypeFormatter formatter = phoneUtil.getAsYouTypeFormatter(RegionCode.GB); 423 assertEquals("0", formatter.inputDigit('0')); 424 assertEquals("09", formatter.inputDigit('9')); 425 assertEquals("090", formatter.inputDigit('0')); 426 assertEquals("090 7", formatter.inputDigit('7')); 427 assertEquals("090 70", formatter.inputDigit('0')); 428 assertEquals("090 703", formatter.inputDigit('3')); 429 assertEquals("090 7031", formatter.inputDigit('1')); 430 assertEquals("090 7031 3", formatter.inputDigit('3')); 431 assertEquals("090 7031 30", formatter.inputDigit('0')); 432 assertEquals("090 7031 300", formatter.inputDigit('0')); 433 assertEquals("090 7031 3000", formatter.inputDigit('0')); 434 } 435 436 public void testAYTFNZMobile() { 437 AsYouTypeFormatter formatter = phoneUtil.getAsYouTypeFormatter(RegionCode.NZ); 438 assertEquals("0", formatter.inputDigit('0')); 439 assertEquals("02", formatter.inputDigit('2')); 440 assertEquals("021", formatter.inputDigit('1')); 441 assertEquals("02-11", formatter.inputDigit('1')); 442 assertEquals("02-112", formatter.inputDigit('2')); 443 // Note the unittest is using fake metadata which might produce non-ideal results. 444 assertEquals("02-112 3", formatter.inputDigit('3')); 445 assertEquals("02-112 34", formatter.inputDigit('4')); 446 assertEquals("02-112 345", formatter.inputDigit('5')); 447 assertEquals("02-112 3456", formatter.inputDigit('6')); 448 } 449 450 public void testAYTFDE() { 451 AsYouTypeFormatter formatter = phoneUtil.getAsYouTypeFormatter(RegionCode.DE); 452 assertEquals("0", formatter.inputDigit('0')); 453 assertEquals("03", formatter.inputDigit('3')); 454 assertEquals("030", formatter.inputDigit('0')); 455 assertEquals("030/1", formatter.inputDigit('1')); 456 assertEquals("030/12", formatter.inputDigit('2')); 457 assertEquals("030/123", formatter.inputDigit('3')); 458 assertEquals("030/1234", formatter.inputDigit('4')); 459 460 // 04134 1234 461 formatter.clear(); 462 assertEquals("0", formatter.inputDigit('0')); 463 assertEquals("04", formatter.inputDigit('4')); 464 assertEquals("041", formatter.inputDigit('1')); 465 assertEquals("041 3", formatter.inputDigit('3')); 466 assertEquals("041 34", formatter.inputDigit('4')); 467 assertEquals("04134 1", formatter.inputDigit('1')); 468 assertEquals("04134 12", formatter.inputDigit('2')); 469 assertEquals("04134 123", formatter.inputDigit('3')); 470 assertEquals("04134 1234", formatter.inputDigit('4')); 471 472 // 08021 2345 473 formatter.clear(); 474 assertEquals("0", formatter.inputDigit('0')); 475 assertEquals("08", formatter.inputDigit('8')); 476 assertEquals("080", formatter.inputDigit('0')); 477 assertEquals("080 2", formatter.inputDigit('2')); 478 assertEquals("080 21", formatter.inputDigit('1')); 479 assertEquals("08021 2", formatter.inputDigit('2')); 480 assertEquals("08021 23", formatter.inputDigit('3')); 481 assertEquals("08021 234", formatter.inputDigit('4')); 482 assertEquals("08021 2345", formatter.inputDigit('5')); 483 484 // 00 1 650 253 2250 485 formatter.clear(); 486 assertEquals("0", formatter.inputDigit('0')); 487 assertEquals("00", formatter.inputDigit('0')); 488 assertEquals("00 1 ", formatter.inputDigit('1')); 489 assertEquals("00 1 6", formatter.inputDigit('6')); 490 assertEquals("00 1 65", formatter.inputDigit('5')); 491 assertEquals("00 1 650", formatter.inputDigit('0')); 492 assertEquals("00 1 650 2", formatter.inputDigit('2')); 493 assertEquals("00 1 650 25", formatter.inputDigit('5')); 494 assertEquals("00 1 650 253", formatter.inputDigit('3')); 495 assertEquals("00 1 650 253 2", formatter.inputDigit('2')); 496 assertEquals("00 1 650 253 22", formatter.inputDigit('2')); 497 assertEquals("00 1 650 253 222", formatter.inputDigit('2')); 498 assertEquals("00 1 650 253 2222", formatter.inputDigit('2')); 499 } 500 501 public void testAYTFAR() { 502 AsYouTypeFormatter formatter = phoneUtil.getAsYouTypeFormatter(RegionCode.AR); 503 assertEquals("0", formatter.inputDigit('0')); 504 assertEquals("01", formatter.inputDigit('1')); 505 assertEquals("011", formatter.inputDigit('1')); 506 assertEquals("011 7", formatter.inputDigit('7')); 507 assertEquals("011 70", formatter.inputDigit('0')); 508 assertEquals("011 703", formatter.inputDigit('3')); 509 assertEquals("011 7031", formatter.inputDigit('1')); 510 assertEquals("011 7031-3", formatter.inputDigit('3')); 511 assertEquals("011 7031-30", formatter.inputDigit('0')); 512 assertEquals("011 7031-300", formatter.inputDigit('0')); 513 assertEquals("011 7031-3000", formatter.inputDigit('0')); 514 } 515 516 public void testAYTFARMobile() { 517 AsYouTypeFormatter formatter = phoneUtil.getAsYouTypeFormatter(RegionCode.AR); 518 assertEquals("+", formatter.inputDigit('+')); 519 assertEquals("+5", formatter.inputDigit('5')); 520 assertEquals("+54 ", formatter.inputDigit('4')); 521 assertEquals("+54 9", formatter.inputDigit('9')); 522 assertEquals("+54 91", formatter.inputDigit('1')); 523 assertEquals("+54 9 11", formatter.inputDigit('1')); 524 assertEquals("+54 9 11 2", formatter.inputDigit('2')); 525 assertEquals("+54 9 11 23", formatter.inputDigit('3')); 526 assertEquals("+54 9 11 231", formatter.inputDigit('1')); 527 assertEquals("+54 9 11 2312", formatter.inputDigit('2')); 528 assertEquals("+54 9 11 2312 1", formatter.inputDigit('1')); 529 assertEquals("+54 9 11 2312 12", formatter.inputDigit('2')); 530 assertEquals("+54 9 11 2312 123", formatter.inputDigit('3')); 531 assertEquals("+54 9 11 2312 1234", formatter.inputDigit('4')); 532 } 533 534 public void testAYTFKR() { 535 // +82 51 234 5678 536 AsYouTypeFormatter formatter = phoneUtil.getAsYouTypeFormatter(RegionCode.KR); 537 assertEquals("+", formatter.inputDigit('+')); 538 assertEquals("+8", formatter.inputDigit('8')); 539 assertEquals("+82 ", formatter.inputDigit('2')); 540 assertEquals("+82 5", formatter.inputDigit('5')); 541 assertEquals("+82 51", formatter.inputDigit('1')); 542 assertEquals("+82 51-2", formatter.inputDigit('2')); 543 assertEquals("+82 51-23", formatter.inputDigit('3')); 544 assertEquals("+82 51-234", formatter.inputDigit('4')); 545 assertEquals("+82 51-234-5", formatter.inputDigit('5')); 546 assertEquals("+82 51-234-56", formatter.inputDigit('6')); 547 assertEquals("+82 51-234-567", formatter.inputDigit('7')); 548 assertEquals("+82 51-234-5678", formatter.inputDigit('8')); 549 550 // +82 2 531 5678 551 formatter.clear(); 552 assertEquals("+", formatter.inputDigit('+')); 553 assertEquals("+8", formatter.inputDigit('8')); 554 assertEquals("+82 ", formatter.inputDigit('2')); 555 assertEquals("+82 2", formatter.inputDigit('2')); 556 assertEquals("+82 25", formatter.inputDigit('5')); 557 assertEquals("+82 2-53", formatter.inputDigit('3')); 558 assertEquals("+82 2-531", formatter.inputDigit('1')); 559 assertEquals("+82 2-531-5", formatter.inputDigit('5')); 560 assertEquals("+82 2-531-56", formatter.inputDigit('6')); 561 assertEquals("+82 2-531-567", formatter.inputDigit('7')); 562 assertEquals("+82 2-531-5678", formatter.inputDigit('8')); 563 564 // +82 2 3665 5678 565 formatter.clear(); 566 assertEquals("+", formatter.inputDigit('+')); 567 assertEquals("+8", formatter.inputDigit('8')); 568 assertEquals("+82 ", formatter.inputDigit('2')); 569 assertEquals("+82 2", formatter.inputDigit('2')); 570 assertEquals("+82 23", formatter.inputDigit('3')); 571 assertEquals("+82 2-36", formatter.inputDigit('6')); 572 assertEquals("+82 2-366", formatter.inputDigit('6')); 573 assertEquals("+82 2-3665", formatter.inputDigit('5')); 574 assertEquals("+82 2-3665-5", formatter.inputDigit('5')); 575 assertEquals("+82 2-3665-56", formatter.inputDigit('6')); 576 assertEquals("+82 2-3665-567", formatter.inputDigit('7')); 577 assertEquals("+82 2-3665-5678", formatter.inputDigit('8')); 578 579 // 02-114 580 formatter.clear(); 581 assertEquals("0", formatter.inputDigit('0')); 582 assertEquals("02", formatter.inputDigit('2')); 583 assertEquals("021", formatter.inputDigit('1')); 584 assertEquals("02-11", formatter.inputDigit('1')); 585 assertEquals("02-114", formatter.inputDigit('4')); 586 587 // 02-1300 588 formatter.clear(); 589 assertEquals("0", formatter.inputDigit('0')); 590 assertEquals("02", formatter.inputDigit('2')); 591 assertEquals("021", formatter.inputDigit('1')); 592 assertEquals("02-13", formatter.inputDigit('3')); 593 assertEquals("02-130", formatter.inputDigit('0')); 594 assertEquals("02-1300", formatter.inputDigit('0')); 595 596 // 011-456-7890 597 formatter.clear(); 598 assertEquals("0", formatter.inputDigit('0')); 599 assertEquals("01", formatter.inputDigit('1')); 600 assertEquals("011", formatter.inputDigit('1')); 601 assertEquals("011-4", formatter.inputDigit('4')); 602 assertEquals("011-45", formatter.inputDigit('5')); 603 assertEquals("011-456", formatter.inputDigit('6')); 604 assertEquals("011-456-7", formatter.inputDigit('7')); 605 assertEquals("011-456-78", formatter.inputDigit('8')); 606 assertEquals("011-456-789", formatter.inputDigit('9')); 607 assertEquals("011-456-7890", formatter.inputDigit('0')); 608 609 // 011-9876-7890 610 formatter.clear(); 611 assertEquals("0", formatter.inputDigit('0')); 612 assertEquals("01", formatter.inputDigit('1')); 613 assertEquals("011", formatter.inputDigit('1')); 614 assertEquals("011-9", formatter.inputDigit('9')); 615 assertEquals("011-98", formatter.inputDigit('8')); 616 assertEquals("011-987", formatter.inputDigit('7')); 617 assertEquals("011-9876", formatter.inputDigit('6')); 618 assertEquals("011-9876-7", formatter.inputDigit('7')); 619 assertEquals("011-9876-78", formatter.inputDigit('8')); 620 assertEquals("011-9876-789", formatter.inputDigit('9')); 621 assertEquals("011-9876-7890", formatter.inputDigit('0')); 622 } 623 624 public void testAYTF_MX() { 625 AsYouTypeFormatter formatter = phoneUtil.getAsYouTypeFormatter(RegionCode.MX); 626 627 // +52 800 123 4567 628 assertEquals("+", formatter.inputDigit('+')); 629 assertEquals("+5", formatter.inputDigit('5')); 630 assertEquals("+52 ", formatter.inputDigit('2')); 631 assertEquals("+52 8", formatter.inputDigit('8')); 632 assertEquals("+52 80", formatter.inputDigit('0')); 633 assertEquals("+52 800", formatter.inputDigit('0')); 634 assertEquals("+52 800 1", formatter.inputDigit('1')); 635 assertEquals("+52 800 12", formatter.inputDigit('2')); 636 assertEquals("+52 800 123", formatter.inputDigit('3')); 637 assertEquals("+52 800 123 4", formatter.inputDigit('4')); 638 assertEquals("+52 800 123 45", formatter.inputDigit('5')); 639 assertEquals("+52 800 123 456", formatter.inputDigit('6')); 640 assertEquals("+52 800 123 4567", formatter.inputDigit('7')); 641 642 // +52 55 1234 5678 643 formatter.clear(); 644 assertEquals("+", formatter.inputDigit('+')); 645 assertEquals("+5", formatter.inputDigit('5')); 646 assertEquals("+52 ", formatter.inputDigit('2')); 647 assertEquals("+52 5", formatter.inputDigit('5')); 648 assertEquals("+52 55", formatter.inputDigit('5')); 649 assertEquals("+52 55 1", formatter.inputDigit('1')); 650 assertEquals("+52 55 12", formatter.inputDigit('2')); 651 assertEquals("+52 55 123", formatter.inputDigit('3')); 652 assertEquals("+52 55 1234", formatter.inputDigit('4')); 653 assertEquals("+52 55 1234 5", formatter.inputDigit('5')); 654 assertEquals("+52 55 1234 56", formatter.inputDigit('6')); 655 assertEquals("+52 55 1234 567", formatter.inputDigit('7')); 656 assertEquals("+52 55 1234 5678", formatter.inputDigit('8')); 657 658 // +52 212 345 6789 659 formatter.clear(); 660 assertEquals("+", formatter.inputDigit('+')); 661 assertEquals("+5", formatter.inputDigit('5')); 662 assertEquals("+52 ", formatter.inputDigit('2')); 663 assertEquals("+52 2", formatter.inputDigit('2')); 664 assertEquals("+52 21", formatter.inputDigit('1')); 665 assertEquals("+52 212", formatter.inputDigit('2')); 666 assertEquals("+52 212 3", formatter.inputDigit('3')); 667 assertEquals("+52 212 34", formatter.inputDigit('4')); 668 assertEquals("+52 212 345", formatter.inputDigit('5')); 669 assertEquals("+52 212 345 6", formatter.inputDigit('6')); 670 assertEquals("+52 212 345 67", formatter.inputDigit('7')); 671 assertEquals("+52 212 345 678", formatter.inputDigit('8')); 672 assertEquals("+52 212 345 6789", formatter.inputDigit('9')); 673 674 // +52 1 55 1234 5678 675 formatter.clear(); 676 assertEquals("+", formatter.inputDigit('+')); 677 assertEquals("+5", formatter.inputDigit('5')); 678 assertEquals("+52 ", formatter.inputDigit('2')); 679 assertEquals("+52 1", formatter.inputDigit('1')); 680 assertEquals("+52 15", formatter.inputDigit('5')); 681 assertEquals("+52 1 55", formatter.inputDigit('5')); 682 assertEquals("+52 1 55 1", formatter.inputDigit('1')); 683 assertEquals("+52 1 55 12", formatter.inputDigit('2')); 684 assertEquals("+52 1 55 123", formatter.inputDigit('3')); 685 assertEquals("+52 1 55 1234", formatter.inputDigit('4')); 686 assertEquals("+52 1 55 1234 5", formatter.inputDigit('5')); 687 assertEquals("+52 1 55 1234 56", formatter.inputDigit('6')); 688 assertEquals("+52 1 55 1234 567", formatter.inputDigit('7')); 689 assertEquals("+52 1 55 1234 5678", formatter.inputDigit('8')); 690 691 // +52 1 541 234 5678 692 formatter.clear(); 693 assertEquals("+", formatter.inputDigit('+')); 694 assertEquals("+5", formatter.inputDigit('5')); 695 assertEquals("+52 ", formatter.inputDigit('2')); 696 assertEquals("+52 1", formatter.inputDigit('1')); 697 assertEquals("+52 15", formatter.inputDigit('5')); 698 assertEquals("+52 1 54", formatter.inputDigit('4')); 699 assertEquals("+52 1 541", formatter.inputDigit('1')); 700 assertEquals("+52 1 541 2", formatter.inputDigit('2')); 701 assertEquals("+52 1 541 23", formatter.inputDigit('3')); 702 assertEquals("+52 1 541 234", formatter.inputDigit('4')); 703 assertEquals("+52 1 541 234 5", formatter.inputDigit('5')); 704 assertEquals("+52 1 541 234 56", formatter.inputDigit('6')); 705 assertEquals("+52 1 541 234 567", formatter.inputDigit('7')); 706 assertEquals("+52 1 541 234 5678", formatter.inputDigit('8')); 707 } 708 709 public void testAYTF_International_Toll_Free() { 710 AsYouTypeFormatter formatter = phoneUtil.getAsYouTypeFormatter(RegionCode.US); 711 // +800 1234 5678 712 assertEquals("+", formatter.inputDigit('+')); 713 assertEquals("+8", formatter.inputDigit('8')); 714 assertEquals("+80", formatter.inputDigit('0')); 715 assertEquals("+800 ", formatter.inputDigit('0')); 716 assertEquals("+800 1", formatter.inputDigit('1')); 717 assertEquals("+800 12", formatter.inputDigit('2')); 718 assertEquals("+800 123", formatter.inputDigit('3')); 719 assertEquals("+800 1234", formatter.inputDigit('4')); 720 assertEquals("+800 1234 5", formatter.inputDigit('5')); 721 assertEquals("+800 1234 56", formatter.inputDigit('6')); 722 assertEquals("+800 1234 567", formatter.inputDigit('7')); 723 assertEquals("+800 1234 5678", formatter.inputDigit('8')); 724 assertEquals("+800123456789", formatter.inputDigit('9')); 725 } 726 727 public void testAYTFMultipleLeadingDigitPatterns() { 728 // +81 50 2345 6789 729 AsYouTypeFormatter formatter = phoneUtil.getAsYouTypeFormatter(RegionCode.JP); 730 assertEquals("+", formatter.inputDigit('+')); 731 assertEquals("+8", formatter.inputDigit('8')); 732 assertEquals("+81 ", formatter.inputDigit('1')); 733 assertEquals("+81 5", formatter.inputDigit('5')); 734 assertEquals("+81 50", formatter.inputDigit('0')); 735 assertEquals("+81 50 2", formatter.inputDigit('2')); 736 assertEquals("+81 50 23", formatter.inputDigit('3')); 737 assertEquals("+81 50 234", formatter.inputDigit('4')); 738 assertEquals("+81 50 2345", formatter.inputDigit('5')); 739 assertEquals("+81 50 2345 6", formatter.inputDigit('6')); 740 assertEquals("+81 50 2345 67", formatter.inputDigit('7')); 741 assertEquals("+81 50 2345 678", formatter.inputDigit('8')); 742 assertEquals("+81 50 2345 6789", formatter.inputDigit('9')); 743 744 // +81 222 12 5678 745 formatter.clear(); 746 assertEquals("+", formatter.inputDigit('+')); 747 assertEquals("+8", formatter.inputDigit('8')); 748 assertEquals("+81 ", formatter.inputDigit('1')); 749 assertEquals("+81 2", formatter.inputDigit('2')); 750 assertEquals("+81 22", formatter.inputDigit('2')); 751 assertEquals("+81 22 2", formatter.inputDigit('2')); 752 assertEquals("+81 22 21", formatter.inputDigit('1')); 753 assertEquals("+81 2221 2", formatter.inputDigit('2')); 754 assertEquals("+81 222 12 5", formatter.inputDigit('5')); 755 assertEquals("+81 222 12 56", formatter.inputDigit('6')); 756 assertEquals("+81 222 12 567", formatter.inputDigit('7')); 757 assertEquals("+81 222 12 5678", formatter.inputDigit('8')); 758 759 // 011113 760 formatter.clear(); 761 assertEquals("0", formatter.inputDigit('0')); 762 assertEquals("01", formatter.inputDigit('1')); 763 assertEquals("011", formatter.inputDigit('1')); 764 assertEquals("011 1", formatter.inputDigit('1')); 765 assertEquals("011 11", formatter.inputDigit('1')); 766 assertEquals("011113", formatter.inputDigit('3')); 767 768 // +81 3332 2 5678 769 formatter.clear(); 770 assertEquals("+", formatter.inputDigit('+')); 771 assertEquals("+8", formatter.inputDigit('8')); 772 assertEquals("+81 ", formatter.inputDigit('1')); 773 assertEquals("+81 3", formatter.inputDigit('3')); 774 assertEquals("+81 33", formatter.inputDigit('3')); 775 assertEquals("+81 33 3", formatter.inputDigit('3')); 776 assertEquals("+81 3332", formatter.inputDigit('2')); 777 assertEquals("+81 3332 2", formatter.inputDigit('2')); 778 assertEquals("+81 3332 2 5", formatter.inputDigit('5')); 779 assertEquals("+81 3332 2 56", formatter.inputDigit('6')); 780 assertEquals("+81 3332 2 567", formatter.inputDigit('7')); 781 assertEquals("+81 3332 2 5678", formatter.inputDigit('8')); 782 } 783 784 public void testAYTFLongIDD_AU() { 785 AsYouTypeFormatter formatter = phoneUtil.getAsYouTypeFormatter(RegionCode.AU); 786 // 0011 1 650 253 2250 787 assertEquals("0", formatter.inputDigit('0')); 788 assertEquals("00", formatter.inputDigit('0')); 789 assertEquals("001", formatter.inputDigit('1')); 790 assertEquals("0011", formatter.inputDigit('1')); 791 assertEquals("0011 1 ", formatter.inputDigit('1')); 792 assertEquals("0011 1 6", formatter.inputDigit('6')); 793 assertEquals("0011 1 65", formatter.inputDigit('5')); 794 assertEquals("0011 1 650", formatter.inputDigit('0')); 795 assertEquals("0011 1 650 2", formatter.inputDigit('2')); 796 assertEquals("0011 1 650 25", formatter.inputDigit('5')); 797 assertEquals("0011 1 650 253", formatter.inputDigit('3')); 798 assertEquals("0011 1 650 253 2", formatter.inputDigit('2')); 799 assertEquals("0011 1 650 253 22", formatter.inputDigit('2')); 800 assertEquals("0011 1 650 253 222", formatter.inputDigit('2')); 801 assertEquals("0011 1 650 253 2222", formatter.inputDigit('2')); 802 803 // 0011 81 3332 2 5678 804 formatter.clear(); 805 assertEquals("0", formatter.inputDigit('0')); 806 assertEquals("00", formatter.inputDigit('0')); 807 assertEquals("001", formatter.inputDigit('1')); 808 assertEquals("0011", formatter.inputDigit('1')); 809 assertEquals("00118", formatter.inputDigit('8')); 810 assertEquals("0011 81 ", formatter.inputDigit('1')); 811 assertEquals("0011 81 3", formatter.inputDigit('3')); 812 assertEquals("0011 81 33", formatter.inputDigit('3')); 813 assertEquals("0011 81 33 3", formatter.inputDigit('3')); 814 assertEquals("0011 81 3332", formatter.inputDigit('2')); 815 assertEquals("0011 81 3332 2", formatter.inputDigit('2')); 816 assertEquals("0011 81 3332 2 5", formatter.inputDigit('5')); 817 assertEquals("0011 81 3332 2 56", formatter.inputDigit('6')); 818 assertEquals("0011 81 3332 2 567", formatter.inputDigit('7')); 819 assertEquals("0011 81 3332 2 5678", formatter.inputDigit('8')); 820 821 // 0011 244 250 253 222 822 formatter.clear(); 823 assertEquals("0", formatter.inputDigit('0')); 824 assertEquals("00", formatter.inputDigit('0')); 825 assertEquals("001", formatter.inputDigit('1')); 826 assertEquals("0011", formatter.inputDigit('1')); 827 assertEquals("00112", formatter.inputDigit('2')); 828 assertEquals("001124", formatter.inputDigit('4')); 829 assertEquals("0011 244 ", formatter.inputDigit('4')); 830 assertEquals("0011 244 2", formatter.inputDigit('2')); 831 assertEquals("0011 244 25", formatter.inputDigit('5')); 832 assertEquals("0011 244 250", formatter.inputDigit('0')); 833 assertEquals("0011 244 250 2", formatter.inputDigit('2')); 834 assertEquals("0011 244 250 25", formatter.inputDigit('5')); 835 assertEquals("0011 244 250 253", formatter.inputDigit('3')); 836 assertEquals("0011 244 250 253 2", formatter.inputDigit('2')); 837 assertEquals("0011 244 250 253 22", formatter.inputDigit('2')); 838 assertEquals("0011 244 250 253 222", formatter.inputDigit('2')); 839 } 840 841 public void testAYTFLongIDD_KR() { 842 AsYouTypeFormatter formatter = phoneUtil.getAsYouTypeFormatter(RegionCode.KR); 843 // 00300 1 650 253 2222 844 assertEquals("0", formatter.inputDigit('0')); 845 assertEquals("00", formatter.inputDigit('0')); 846 assertEquals("003", formatter.inputDigit('3')); 847 assertEquals("0030", formatter.inputDigit('0')); 848 assertEquals("00300", formatter.inputDigit('0')); 849 assertEquals("00300 1 ", formatter.inputDigit('1')); 850 assertEquals("00300 1 6", formatter.inputDigit('6')); 851 assertEquals("00300 1 65", formatter.inputDigit('5')); 852 assertEquals("00300 1 650", formatter.inputDigit('0')); 853 assertEquals("00300 1 650 2", formatter.inputDigit('2')); 854 assertEquals("00300 1 650 25", formatter.inputDigit('5')); 855 assertEquals("00300 1 650 253", formatter.inputDigit('3')); 856 assertEquals("00300 1 650 253 2", formatter.inputDigit('2')); 857 assertEquals("00300 1 650 253 22", formatter.inputDigit('2')); 858 assertEquals("00300 1 650 253 222", formatter.inputDigit('2')); 859 assertEquals("00300 1 650 253 2222", formatter.inputDigit('2')); 860 } 861 862 public void testAYTFLongNDD_KR() { 863 AsYouTypeFormatter formatter = phoneUtil.getAsYouTypeFormatter(RegionCode.KR); 864 // 08811-9876-7890 865 assertEquals("0", formatter.inputDigit('0')); 866 assertEquals("08", formatter.inputDigit('8')); 867 assertEquals("088", formatter.inputDigit('8')); 868 assertEquals("0881", formatter.inputDigit('1')); 869 assertEquals("08811", formatter.inputDigit('1')); 870 assertEquals("08811-9", formatter.inputDigit('9')); 871 assertEquals("08811-98", formatter.inputDigit('8')); 872 assertEquals("08811-987", formatter.inputDigit('7')); 873 assertEquals("08811-9876", formatter.inputDigit('6')); 874 assertEquals("08811-9876-7", formatter.inputDigit('7')); 875 assertEquals("08811-9876-78", formatter.inputDigit('8')); 876 assertEquals("08811-9876-789", formatter.inputDigit('9')); 877 assertEquals("08811-9876-7890", formatter.inputDigit('0')); 878 879 // 08500 11-9876-7890 880 formatter.clear(); 881 assertEquals("0", formatter.inputDigit('0')); 882 assertEquals("08", formatter.inputDigit('8')); 883 assertEquals("085", formatter.inputDigit('5')); 884 assertEquals("0850", formatter.inputDigit('0')); 885 assertEquals("08500 ", formatter.inputDigit('0')); 886 assertEquals("08500 1", formatter.inputDigit('1')); 887 assertEquals("08500 11", formatter.inputDigit('1')); 888 assertEquals("08500 11-9", formatter.inputDigit('9')); 889 assertEquals("08500 11-98", formatter.inputDigit('8')); 890 assertEquals("08500 11-987", formatter.inputDigit('7')); 891 assertEquals("08500 11-9876", formatter.inputDigit('6')); 892 assertEquals("08500 11-9876-7", formatter.inputDigit('7')); 893 assertEquals("08500 11-9876-78", formatter.inputDigit('8')); 894 assertEquals("08500 11-9876-789", formatter.inputDigit('9')); 895 assertEquals("08500 11-9876-7890", formatter.inputDigit('0')); 896 } 897 898 public void testAYTFLongNDD_SG() { 899 AsYouTypeFormatter formatter = phoneUtil.getAsYouTypeFormatter(RegionCode.SG); 900 // 777777 9876 7890 901 assertEquals("7", formatter.inputDigit('7')); 902 assertEquals("77", formatter.inputDigit('7')); 903 assertEquals("777", formatter.inputDigit('7')); 904 assertEquals("7777", formatter.inputDigit('7')); 905 assertEquals("77777", formatter.inputDigit('7')); 906 assertEquals("777777 ", formatter.inputDigit('7')); 907 assertEquals("777777 9", formatter.inputDigit('9')); 908 assertEquals("777777 98", formatter.inputDigit('8')); 909 assertEquals("777777 987", formatter.inputDigit('7')); 910 assertEquals("777777 9876", formatter.inputDigit('6')); 911 assertEquals("777777 9876 7", formatter.inputDigit('7')); 912 assertEquals("777777 9876 78", formatter.inputDigit('8')); 913 assertEquals("777777 9876 789", formatter.inputDigit('9')); 914 assertEquals("777777 9876 7890", formatter.inputDigit('0')); 915 } 916 917 public void testAYTFShortNumberFormattingFix_AU() { 918 // For Australia, the national prefix is not optional when formatting. 919 AsYouTypeFormatter formatter = phoneUtil.getAsYouTypeFormatter(RegionCode.AU); 920 921 // 1234567890 - For leading digit 1, the national prefix formatting rule has first group only. 922 assertEquals("1", formatter.inputDigit('1')); 923 assertEquals("12", formatter.inputDigit('2')); 924 assertEquals("123", formatter.inputDigit('3')); 925 assertEquals("1234", formatter.inputDigit('4')); 926 assertEquals("1234 5", formatter.inputDigit('5')); 927 assertEquals("1234 56", formatter.inputDigit('6')); 928 assertEquals("1234 567", formatter.inputDigit('7')); 929 assertEquals("1234 567 8", formatter.inputDigit('8')); 930 assertEquals("1234 567 89", formatter.inputDigit('9')); 931 assertEquals("1234 567 890", formatter.inputDigit('0')); 932 933 // +61 1234 567 890 - Test the same number, but with the country code. 934 formatter.clear(); 935 assertEquals("+", formatter.inputDigit('+')); 936 assertEquals("+6", formatter.inputDigit('6')); 937 assertEquals("+61 ", formatter.inputDigit('1')); 938 assertEquals("+61 1", formatter.inputDigit('1')); 939 assertEquals("+61 12", formatter.inputDigit('2')); 940 assertEquals("+61 123", formatter.inputDigit('3')); 941 assertEquals("+61 1234", formatter.inputDigit('4')); 942 assertEquals("+61 1234 5", formatter.inputDigit('5')); 943 assertEquals("+61 1234 56", formatter.inputDigit('6')); 944 assertEquals("+61 1234 567", formatter.inputDigit('7')); 945 assertEquals("+61 1234 567 8", formatter.inputDigit('8')); 946 assertEquals("+61 1234 567 89", formatter.inputDigit('9')); 947 assertEquals("+61 1234 567 890", formatter.inputDigit('0')); 948 949 // 212345678 - For leading digit 2, the national prefix formatting rule puts the national prefix 950 // before the first group. 951 formatter.clear(); 952 assertEquals("0", formatter.inputDigit('0')); 953 assertEquals("02", formatter.inputDigit('2')); 954 assertEquals("021", formatter.inputDigit('1')); 955 assertEquals("02 12", formatter.inputDigit('2')); 956 assertEquals("02 123", formatter.inputDigit('3')); 957 assertEquals("02 1234", formatter.inputDigit('4')); 958 assertEquals("02 1234 5", formatter.inputDigit('5')); 959 assertEquals("02 1234 56", formatter.inputDigit('6')); 960 assertEquals("02 1234 567", formatter.inputDigit('7')); 961 assertEquals("02 1234 5678", formatter.inputDigit('8')); 962 963 // 212345678 - Test the same number, but without the leading 0. 964 formatter.clear(); 965 assertEquals("2", formatter.inputDigit('2')); 966 assertEquals("21", formatter.inputDigit('1')); 967 assertEquals("212", formatter.inputDigit('2')); 968 assertEquals("2123", formatter.inputDigit('3')); 969 assertEquals("21234", formatter.inputDigit('4')); 970 assertEquals("212345", formatter.inputDigit('5')); 971 assertEquals("2123456", formatter.inputDigit('6')); 972 assertEquals("21234567", formatter.inputDigit('7')); 973 assertEquals("212345678", formatter.inputDigit('8')); 974 975 // +61 2 1234 5678 - Test the same number, but with the country code. 976 formatter.clear(); 977 assertEquals("+", formatter.inputDigit('+')); 978 assertEquals("+6", formatter.inputDigit('6')); 979 assertEquals("+61 ", formatter.inputDigit('1')); 980 assertEquals("+61 2", formatter.inputDigit('2')); 981 assertEquals("+61 21", formatter.inputDigit('1')); 982 assertEquals("+61 2 12", formatter.inputDigit('2')); 983 assertEquals("+61 2 123", formatter.inputDigit('3')); 984 assertEquals("+61 2 1234", formatter.inputDigit('4')); 985 assertEquals("+61 2 1234 5", formatter.inputDigit('5')); 986 assertEquals("+61 2 1234 56", formatter.inputDigit('6')); 987 assertEquals("+61 2 1234 567", formatter.inputDigit('7')); 988 assertEquals("+61 2 1234 5678", formatter.inputDigit('8')); 989 } 990 991 public void testAYTFShortNumberFormattingFix_KR() { 992 // For Korea, the national prefix is not optional when formatting, and the national prefix 993 // formatting rule doesn't consist of only the first group. 994 AsYouTypeFormatter formatter = phoneUtil.getAsYouTypeFormatter(RegionCode.KR); 995 996 // 111 997 assertEquals("1", formatter.inputDigit('1')); 998 assertEquals("11", formatter.inputDigit('1')); 999 assertEquals("111", formatter.inputDigit('1')); 1000 1001 // 114 1002 formatter.clear(); 1003 assertEquals("1", formatter.inputDigit('1')); 1004 assertEquals("11", formatter.inputDigit('1')); 1005 assertEquals("114", formatter.inputDigit('4')); 1006 1007 // 13121234 - Test a mobile number without the national prefix. Even though it is not an 1008 // emergency number, it should be formatted as a block. 1009 formatter.clear(); 1010 assertEquals("1", formatter.inputDigit('1')); 1011 assertEquals("13", formatter.inputDigit('3')); 1012 assertEquals("131", formatter.inputDigit('1')); 1013 assertEquals("1312", formatter.inputDigit('2')); 1014 assertEquals("13121", formatter.inputDigit('1')); 1015 assertEquals("131212", formatter.inputDigit('2')); 1016 assertEquals("1312123", formatter.inputDigit('3')); 1017 assertEquals("13121234", formatter.inputDigit('4')); 1018 1019 // +82 131-2-1234 - Test the same number, but with the country code. 1020 formatter.clear(); 1021 assertEquals("+", formatter.inputDigit('+')); 1022 assertEquals("+8", formatter.inputDigit('8')); 1023 assertEquals("+82 ", formatter.inputDigit('2')); 1024 assertEquals("+82 1", formatter.inputDigit('1')); 1025 assertEquals("+82 13", formatter.inputDigit('3')); 1026 assertEquals("+82 131", formatter.inputDigit('1')); 1027 assertEquals("+82 131-2", formatter.inputDigit('2')); 1028 assertEquals("+82 131-2-1", formatter.inputDigit('1')); 1029 assertEquals("+82 131-2-12", formatter.inputDigit('2')); 1030 assertEquals("+82 131-2-123", formatter.inputDigit('3')); 1031 assertEquals("+82 131-2-1234", formatter.inputDigit('4')); 1032 } 1033 1034 public void testAYTFShortNumberFormattingFix_MX() { 1035 // For Mexico, the national prefix is optional when formatting. 1036 AsYouTypeFormatter formatter = phoneUtil.getAsYouTypeFormatter(RegionCode.MX); 1037 1038 // 911 1039 assertEquals("9", formatter.inputDigit('9')); 1040 assertEquals("91", formatter.inputDigit('1')); 1041 assertEquals("911", formatter.inputDigit('1')); 1042 1043 // 800 123 4567 - Test a toll-free number, which should have a formatting rule applied to it 1044 // even though it doesn't begin with the national prefix. 1045 formatter.clear(); 1046 assertEquals("8", formatter.inputDigit('8')); 1047 assertEquals("80", formatter.inputDigit('0')); 1048 assertEquals("800", formatter.inputDigit('0')); 1049 assertEquals("800 1", formatter.inputDigit('1')); 1050 assertEquals("800 12", formatter.inputDigit('2')); 1051 assertEquals("800 123", formatter.inputDigit('3')); 1052 assertEquals("800 123 4", formatter.inputDigit('4')); 1053 assertEquals("800 123 45", formatter.inputDigit('5')); 1054 assertEquals("800 123 456", formatter.inputDigit('6')); 1055 assertEquals("800 123 4567", formatter.inputDigit('7')); 1056 1057 // +52 800 123 4567 - Test the same number, but with the country code. 1058 formatter.clear(); 1059 assertEquals("+", formatter.inputDigit('+')); 1060 assertEquals("+5", formatter.inputDigit('5')); 1061 assertEquals("+52 ", formatter.inputDigit('2')); 1062 assertEquals("+52 8", formatter.inputDigit('8')); 1063 assertEquals("+52 80", formatter.inputDigit('0')); 1064 assertEquals("+52 800", formatter.inputDigit('0')); 1065 assertEquals("+52 800 1", formatter.inputDigit('1')); 1066 assertEquals("+52 800 12", formatter.inputDigit('2')); 1067 assertEquals("+52 800 123", formatter.inputDigit('3')); 1068 assertEquals("+52 800 123 4", formatter.inputDigit('4')); 1069 assertEquals("+52 800 123 45", formatter.inputDigit('5')); 1070 assertEquals("+52 800 123 456", formatter.inputDigit('6')); 1071 assertEquals("+52 800 123 4567", formatter.inputDigit('7')); 1072 } 1073 1074 public void testAYTFNoNationalPrefix() { 1075 AsYouTypeFormatter formatter = phoneUtil.getAsYouTypeFormatter(RegionCode.IT); 1076 1077 assertEquals("3", formatter.inputDigit('3')); 1078 assertEquals("33", formatter.inputDigit('3')); 1079 assertEquals("333", formatter.inputDigit('3')); 1080 assertEquals("333 3", formatter.inputDigit('3')); 1081 assertEquals("333 33", formatter.inputDigit('3')); 1082 assertEquals("333 333", formatter.inputDigit('3')); 1083 } 1084 1085 public void testAYTFShortNumberFormattingFix_US() { 1086 // For the US, an initial 1 is treated specially. 1087 AsYouTypeFormatter formatter = phoneUtil.getAsYouTypeFormatter(RegionCode.US); 1088 1089 // 101 - Test that the initial 1 is not treated as a national prefix. 1090 assertEquals("1", formatter.inputDigit('1')); 1091 assertEquals("10", formatter.inputDigit('0')); 1092 assertEquals("101", formatter.inputDigit('1')); 1093 1094 // 112 - Test that the initial 1 is not treated as a national prefix. 1095 formatter.clear(); 1096 assertEquals("1", formatter.inputDigit('1')); 1097 assertEquals("11", formatter.inputDigit('1')); 1098 assertEquals("112", formatter.inputDigit('2')); 1099 1100 // 122 - Test that the initial 1 is treated as a national prefix. 1101 formatter.clear(); 1102 assertEquals("1", formatter.inputDigit('1')); 1103 assertEquals("12", formatter.inputDigit('2')); 1104 assertEquals("1 22", formatter.inputDigit('2')); 1105 } 1106 } 1107