DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Mercurial (d1ed7de67f5a)

VCS Links

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
<?xml version="1.0"?>

<!-- Any copyright is dedicated to the Public Domain.
   - http://creativecommons.org/publicdomain/zero/1.0/ -->

<?xml-stylesheet type="text/css" href="chrome://global/skin"?>
<?xml-stylesheet type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css"?>

<window xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
        title="Mozilla Bug 781379">
  <script type="application/javascript" src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"/>
  <script type="application/javascript" src="head.js"/>
  <!-- test results are displayed in the html:body -->
  <body xmlns="http://www.w3.org/1999/xhtml">
  <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=809213"
     target="_blank">Mozilla Bug 809213</a>
  </body>

<script type="application/javascript;version=1.8">

"use strict";

Components.utils.import("resource://gre/modules/PhoneNumber.jsm");
Components.utils.import("resource://gre/modules/PhoneNumberNormalizer.jsm");

function IsPlain(dial, expected) {
  var result = PhoneNumber.IsPlain(dial);
  if (result != expected) {
    ok(false, dial + " is " + (result ? "" : "not ") + "plain, expects otherwise.");
  } else {
    ok(true, dial + " is " + (result ? "" : "not ") + "plain as expected.");
  }
}

function Normalize(dial, expected) {
  var result = PhoneNumberNormalizer.Normalize(dial);
  if (result !== expected) {
    ok(false, "normalized " + dial + " to " + result + ", expected " + expected + " instead.");
  } else {
    ok(true, "normalized " + dial + " to " + result + " as expected.");
  }
}

function Normalize(dial, expected) {
  var result = PhoneNumberNormalizer.Normalize(dial);
  if (result != expected) {
    ok(false, "Normalize error!\n");
    print("expected: " + expected);
    print("got: " + result);
  } else {
    ok(true, "Normalization OK");
  }
}

function CantParse(dial, currentRegion) {
  var result = PhoneNumber.Parse(dial, currentRegion);
  if (result) {
  	ok(false, "Shouldn't parse!\n");
    print("expected: does not parse");
    print("got: " + dial + " " + currentRegion);
  }
}

function Parse(dial, currentRegion) {
  dump("try: " + dial + ", " + currentRegion + "\n");
  var result = PhoneNumber.Parse(dial, currentRegion);
  if (!result) {
  	ok(false, "got: " + dial + " " + currentRegion);
  } else {
  	ok(true, "Parses!\n");
  }
  return result;
}

function ParseWithIntl(dial, currentRegion) {
  var result = PhoneNumber.Parse(dial, currentRegion);
  if (!result) {
  	ok(false, "got: " + dial + " " + currentRegion);
  } else {
    ok(result.internationalNumber, "Has International!");
  	ok(true, "Parses!\n");
  }
  return result;
}

function Test(dial, currentRegion, nationalNumber, region) {
  var result = Parse(dial, currentRegion);
  if (result.region != region || result.nationalNumber != nationalNumber) {
    ok(false, "expected: " + nationalNumber + " " + region + "got: " + result.nationalNumber + " " + result.region);
  } else {
  	ok(true, "Test ok!");
  }
  return result;
}

function Format(dial, currentRegion, nationalNumber, region, nationalFormat, internationalFormat) {
  var result = Test(dial, currentRegion, nationalNumber, region);
  if (result.nationalFormat != nationalFormat ||
      result.internationalFormat != internationalFormat) {
    ok(false, "expected: " + nationalFormat + " " + internationalFormat + 
      "got: " + result.nationalFormat + " " + result.internationalFormat);
  }
}

function TestProperties(dial, currentRegion) {
  var result = PhoneNumber.Parse(dial, currentRegion);
  if (result) {
    ok(true, "found it");
    ok(true, "InternationalFormat: " + result.internationalFormat);
    ok(true, "InternationalNumber: " + result.internationalNumber);
    ok(true, "NationalNumber: " + result.nationalNumber);
    ok(true, "NationalFormat: " + result.nationalFormat);
  } else {
    ok(true, "not found");
  }
}

function TestPropertiesWithExpectedCountry(dial, currentRegion, expectedRegion) {
  var result = PhoneNumber.Parse(dial, currentRegion);
  if (result) {
    ok(true, "found it");
    ok(true, "InternationalFormat: " + result.internationalFormat);
    ok(true, "InternationalNumber: " + result.internationalNumber);
    ok(true, "NationalNumber: " + result.nationalNumber);
    ok(true, "NationalFormat: " + result.nationalFormat);
    ok(true, "CountryName: " + result.countryName);
    is(result.countryName, expectedRegion, "Same region");
  } else {
    ok(true, "not found");
  }
}

function AllEqual(list, currentRegion) {
  var first = PhoneNumber.Parse(list.shift(), currentRegion);
  ok(!!first, "first parses");
  for (var index in list) {
    var other = PhoneNumber.Parse(list[index], currentRegion);
    ok(!!other, "other parses");
    ok(first.internationalNumber == other.internationalNumber, "first and other match");
  }
}

TestProperties("+0988782456");
TestProperties("+33442020", "ES");
TestProperties("+43987614", "ES");
TestProperties("+0988782456");
TestProperties("+34556657");
TestProperties("+66554433");
TestProperties("+43442075");
TestProperties("+13442074");

TestPropertiesWithExpectedCountry("+4333822222", "DE", "AT");
TestPropertiesWithExpectedCountry("+19491234567", "DE", "US");

// Test whether could a string be a phone number.
IsPlain(null, false);
IsPlain("", false);
IsPlain("1", true);
IsPlain("*2", true); // Real number used in Venezuela
IsPlain("*8", true); // Real number used in Venezuela
IsPlain("12", true);
IsPlain("123", true);
IsPlain("1a2", false);
IsPlain("12a", false);
IsPlain("1234", true);
IsPlain("123a", false);
IsPlain("+", true);
IsPlain("+1", true);
IsPlain("+12", true);
IsPlain("+123", true);
IsPlain("()123", false);
IsPlain("(1)23", false);
IsPlain("(12)3", false);
IsPlain("(123)", false);
IsPlain("(123)4", false);
IsPlain("123;ext=", false);
IsPlain("123;ext=1", false);
IsPlain("123;ext=1234567", false);
IsPlain("123;ext=12345678", false);
IsPlain("123 ext:1", false);
IsPlain("123 ext:1#", false);
IsPlain("123-1#", false);
IsPlain("123 1#", false);
IsPlain("123 12345#", false);
IsPlain("123 +123456#", false);
IsPlain("#123#", true);
IsPlain("*#004#", true);
IsPlain("*30#", true);
IsPlain("*#06#", true);
IsPlain("123456789012345678901234567890123456789012345678901", false); // more than 50 characters

// test normalization
Normalize(null, "");
Normalize("", "");
Normalize("1", "1");
Normalize("*2", "*2"); // Real number used in Venezuela
Normalize("*8", "*8"); // Real number used in Venezuela
Normalize("12", "12");
Normalize("123", "123");
Normalize("1a2", "122");
Normalize("12a", "122");
Normalize("1234", "1234");
Normalize("123a", "1232");
Normalize("+", "+");
Normalize("+1", "+1");
Normalize("+12", "+12");
Normalize("+123", "+123");
Normalize("()123", "123");
Normalize("(1)23", "123");
Normalize("(12)3", "123");
Normalize("(123)", "123");
Normalize("(123)4", "1234");
Normalize("123-1#", "1231#");
Normalize("123 1#", "1231#");
Normalize("123 12345#", "12312345#");
Normalize("123 +123456#", "123+123456#");
Normalize("#123#", "#123#");
Normalize("*#004#", "*#004#");
Normalize("*30#", "*30#");
Normalize("*#06#", "*#06#");

// Test parsing national numbers.
Parse("033316005", "NZ");
Parse("03-331 6005", "NZ");
Parse("03 331 6005", "NZ");

// Always test CA before US because CA has to load all meta-info for US.
ParseWithIntl("4031234567", "CA");
Parse("(416) 585-4319", "CA");
Parse("647-967-4357", "CA");
Parse("416-716-8768", "CA");
Parse("18002684646", "CA");
Parse("416-445-9119", "CA");
Parse("1-800-668-6866", "CA");
Parse("(416) 453-6486", "CA");
Parse("(647) 268-4778", "CA");
Parse("647-218-1313", "CA");
Parse("+1 647-209-4642", "CA");
Parse("416-559-0133", "CA");
Parse("+1 647-639-4118", "CA");
Parse("+12898803664", "CA");
Parse("780-901-4687", "CA");
Parse("+14167070550", "CA");
Parse("+1-647-522-6487", "CA");
Parse("(416) 877-0880", "CA");

// Testing international prefixes.
// Should strip country code.
Parse("0064 3 331 6005", "NZ");
// Try again, but this time we have an international number with region rode US. It should
// recognize the country code and parse accordingly.
Parse("01164 3 331 6005", "US");
Parse("+64 3 331 6005", "US");
Parse("64(0)64123456", "NZ");
// Check that using a "/" is fine in a phone number.
Parse("123/45678", "DE");
Parse("123-456-7890", "US");

// Test parsing international numbers.
Parse("+1 (650) 333-6000", "NZ");
Parse("1-650-333-6000", "US");
// Calling the US number from Singapore by using different service providers
// 1st test: calling using SingTel IDD service (IDD is 001)
Parse("0011-650-333-6000", "SG");
// 2nd test: calling using StarHub IDD service (IDD is 008)
Parse("0081-650-333-6000", "SG");
// 3rd test: calling using SingTel V019 service (IDD is 019)
Parse("0191-650-333-6000", "SG");
// Calling the US number from Poland
Parse("0~01-650-333-6000", "PL");
// Using "++" at the start.
Parse("++1 (650) 333-6000", "PL");
// Using a full-width plus sign.
Parse("\uFF0B1 (650) 333-6000", "SG");
// The whole number, including punctuation, is here represented in full-width form.
Parse("\uFF0B\uFF11\u3000\uFF08\uFF16\uFF15\uFF10\uFF09" +
      "\u3000\uFF13\uFF13\uFF13\uFF0D\uFF16\uFF10\uFF10\uFF10",
      "SG");

// Test parsing with leading zeros.
Parse("+39 02-36618 300", "NZ");
Parse("02-36618 300", "IT");
Parse("312 345 678", "IT");

// Test parsing numbers in Argentina.
Parse("+54 9 343 555 1212", "AR");
Parse("0343 15 555 1212", "AR");
Parse("+54 9 3715 65 4320", "AR");
Parse("03715 15 65 4320", "AR");
Parse("+54 11 3797 0000", "AR");
Parse("011 3797 0000", "AR");
Parse("+54 3715 65 4321", "AR");
Parse("03715 65 4321", "AR");
Parse("+54 23 1234 0000", "AR");
Parse("023 1234 0000", "AR");

// Test numbers in Mexico
Parse("+52 (449)978-0001", "MX");
Parse("01 (449)978-0001", "MX");
Parse("(449)978-0001", "MX");
Parse("+52 1 33 1234-5678", "MX");
Parse("044 (33) 1234-5678", "MX");
Parse("045 33 1234-5678", "MX");

// Test that lots of spaces are ok.
Parse("0 3   3 3 1   6 0 0 5", "NZ");

// Test omitting the current region. This is only valid when the number starts
// with a '+'.
Parse("+64 3 331 6005");
Parse("+64 3 331 6005", null);

// US numbers
Format("19497261234", "US", "9497261234", "US", "(949) 726-1234", "+1 949-726-1234");

// Try a couple german numbers from the US with various access codes.
Format("49451491934", "US", "451491934", "DE", "0451 491934", "+49 451 491934");
Format("+49451491934", "US", "451491934", "DE", "0451 491934", "+49 451 491934");
Format("01149451491934", "US", "451491934", "DE", "0451 491934", "+49 451 491934");

// Now try dialing the same number from within the German region.
Format("451491934", "DE", "451491934", "DE", "0451 491934", "+49 451 491934");
Format("0451491934", "DE", "451491934", "DE", "0451 491934", "+49 451 491934");

// Numbers in italy keep the leading 0 in the city code when dialing internationally.
Format("0577-555-555", "IT", "0577555555", "IT", "05 7755 5555", "+39 05 7755 5555");

// Telefonica tests
Format("612123123", "ES", "612123123", "ES", "612 12 31 23", "+34 612 12 31 23");

// Chile mobile number from a landline
Format("0997654321", "CL", "997654321", "CL", "(99) 765 4321", "+56 99 765 4321");

// Chile mobile number from another mobile number
Format("997654321", "CL", "997654321", "CL", "(99) 765 4321", "+56 99 765 4321");

// Colombian international number without the leading "+"
Format("5712234567", "CO", "12234567", "CO", "(1) 2234567", "+57 1 2234567");

// Dialing 911 in the US. This is not a national number.
CantParse("911", "US");

// China mobile number with a 0 in it
Format("15955042864", "CN", "15955042864", "CN", "0159 5504 2864", "+86 159 5504 2864");

// Test normalizing numbers. Only 0-9,#* are valid in a phone number.
Normalize("+ABC # * , 9 _ 1 _0", "+222#*,910");
Normalize("ABCDEFGHIJKLMNOPQRSTUVWXYZ", "22233344455566677778889999");
Normalize("abcdefghijklmnopqrstuvwxyz", "22233344455566677778889999");

// 8 and 9 digit numbers with area code in Brazil with collect call prefix (90)
AllEqual(["01187654321","0411187654321","551187654321","90411187654321","+551187654321"],"BR");
AllEqual(["011987654321","04111987654321","5511987654321","904111987654321","+5511987654321"],"BR");

</script>
</window>