dom/phonenumberutils/tests/test_phonenumber.xul

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/dom/phonenumberutils/tests/test_phonenumber.xul	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,356 @@
     1.4 +<?xml version="1.0"?>
     1.5 +
     1.6 +<!-- Any copyright is dedicated to the Public Domain.
     1.7 +   - http://creativecommons.org/publicdomain/zero/1.0/ -->
     1.8 +
     1.9 +<?xml-stylesheet type="text/css" href="chrome://global/skin"?>
    1.10 +<?xml-stylesheet type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css"?>
    1.11 +
    1.12 +<window xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
    1.13 +        title="Mozilla Bug 781379">
    1.14 +  <script type="application/javascript" src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"/>
    1.15 +  <script type="application/javascript" src="head.js"/>
    1.16 +  <!-- test results are displayed in the html:body -->
    1.17 +  <body xmlns="http://www.w3.org/1999/xhtml">
    1.18 +  <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=809213"
    1.19 +     target="_blank">Mozilla Bug 809213</a>
    1.20 +  </body>
    1.21 +
    1.22 +<script type="application/javascript;version=1.8">
    1.23 +
    1.24 +"use strict";
    1.25 +
    1.26 +Components.utils.import("resource://gre/modules/PhoneNumber.jsm");
    1.27 +Components.utils.import("resource://gre/modules/PhoneNumberNormalizer.jsm");
    1.28 +
    1.29 +function IsPlain(dial, expected) {
    1.30 +  var result = PhoneNumber.IsPlain(dial);
    1.31 +  if (result != expected) {
    1.32 +    ok(false, dial + " is " + (result ? "" : "not ") + "plain, expects otherwise.");
    1.33 +  } else {
    1.34 +    ok(true, dial + " is " + (result ? "" : "not ") + "plain as expected.");
    1.35 +  }
    1.36 +}
    1.37 +
    1.38 +function Normalize(dial, expected) {
    1.39 +  var result = PhoneNumberNormalizer.Normalize(dial);
    1.40 +  if (result !== expected) {
    1.41 +    ok(false, "normalized " + dial + " to " + result + ", expected " + expected + " instead.");
    1.42 +  } else {
    1.43 +    ok(true, "normalized " + dial + " to " + result + " as expected.");
    1.44 +  }
    1.45 +}
    1.46 +
    1.47 +function Normalize(dial, expected) {
    1.48 +  var result = PhoneNumberNormalizer.Normalize(dial);
    1.49 +  if (result != expected) {
    1.50 +    ok(false, "Normalize error!\n");
    1.51 +    print("expected: " + expected);
    1.52 +    print("got: " + result);
    1.53 +  } else {
    1.54 +    ok(true, "Normalization OK");
    1.55 +  }
    1.56 +}
    1.57 +
    1.58 +function CantParse(dial, currentRegion) {
    1.59 +  var result = PhoneNumber.Parse(dial, currentRegion);
    1.60 +  if (result) {
    1.61 +  	ok(false, "Shouldn't parse!\n");
    1.62 +    print("expected: does not parse");
    1.63 +    print("got: " + dial + " " + currentRegion);
    1.64 +  }
    1.65 +}
    1.66 +
    1.67 +function Parse(dial, currentRegion) {
    1.68 +  dump("try: " + dial + ", " + currentRegion + "\n");
    1.69 +  var result = PhoneNumber.Parse(dial, currentRegion);
    1.70 +  if (!result) {
    1.71 +  	ok(false, "got: " + dial + " " + currentRegion);
    1.72 +  } else {
    1.73 +  	ok(true, "Parses!\n");
    1.74 +  }
    1.75 +  return result;
    1.76 +}
    1.77 +
    1.78 +function ParseWithIntl(dial, currentRegion) {
    1.79 +  var result = PhoneNumber.Parse(dial, currentRegion);
    1.80 +  if (!result) {
    1.81 +  	ok(false, "got: " + dial + " " + currentRegion);
    1.82 +  } else {
    1.83 +    ok(result.internationalNumber, "Has International!");
    1.84 +  	ok(true, "Parses!\n");
    1.85 +  }
    1.86 +  return result;
    1.87 +}
    1.88 +
    1.89 +function Test(dial, currentRegion, nationalNumber, region) {
    1.90 +  var result = Parse(dial, currentRegion);
    1.91 +  if (result.region != region || result.nationalNumber != nationalNumber) {
    1.92 +    ok(false, "expected: " + nationalNumber + " " + region + "got: " + result.nationalNumber + " " + result.region);
    1.93 +  } else {
    1.94 +  	ok(true, "Test ok!");
    1.95 +  }
    1.96 +  return result;
    1.97 +}
    1.98 +
    1.99 +function Format(dial, currentRegion, nationalNumber, region, nationalFormat, internationalFormat) {
   1.100 +  var result = Test(dial, currentRegion, nationalNumber, region);
   1.101 +  if (result.nationalFormat != nationalFormat ||
   1.102 +      result.internationalFormat != internationalFormat) {
   1.103 +    ok(false, "expected: " + nationalFormat + " " + internationalFormat + 
   1.104 +      "got: " + result.nationalFormat + " " + result.internationalFormat);
   1.105 +  }
   1.106 +}
   1.107 +
   1.108 +function TestProperties(dial, currentRegion) {
   1.109 +  var result = PhoneNumber.Parse(dial, currentRegion);
   1.110 +  if (result) {
   1.111 +    ok(true, "found it");
   1.112 +    ok(true, "InternationalFormat: " + result.internationalFormat);
   1.113 +    ok(true, "InternationalNumber: " + result.internationalNumber);
   1.114 +    ok(true, "NationalNumber: " + result.nationalNumber);
   1.115 +    ok(true, "NationalFormat: " + result.nationalFormat);
   1.116 +  } else {
   1.117 +    ok(true, "not found");
   1.118 +  }
   1.119 +}
   1.120 +
   1.121 +function TestPropertiesWithExpectedCountry(dial, currentRegion, expectedRegion) {
   1.122 +  var result = PhoneNumber.Parse(dial, currentRegion);
   1.123 +  if (result) {
   1.124 +    ok(true, "found it");
   1.125 +    ok(true, "InternationalFormat: " + result.internationalFormat);
   1.126 +    ok(true, "InternationalNumber: " + result.internationalNumber);
   1.127 +    ok(true, "NationalNumber: " + result.nationalNumber);
   1.128 +    ok(true, "NationalFormat: " + result.nationalFormat);
   1.129 +    ok(true, "CountryName: " + result.countryName);
   1.130 +    is(result.countryName, expectedRegion, "Same region");
   1.131 +  } else {
   1.132 +    ok(true, "not found");
   1.133 +  }
   1.134 +}
   1.135 +
   1.136 +function AllEqual(list, currentRegion) {
   1.137 +  var first = PhoneNumber.Parse(list.shift(), currentRegion);
   1.138 +  ok(!!first, "first parses");
   1.139 +  for (var index in list) {
   1.140 +    var other = PhoneNumber.Parse(list[index], currentRegion);
   1.141 +    ok(!!other, "other parses");
   1.142 +    ok(first.internationalNumber == other.internationalNumber, "first and other match");
   1.143 +  }
   1.144 +}
   1.145 +
   1.146 +TestProperties("+0988782456");
   1.147 +TestProperties("+33442020", "ES");
   1.148 +TestProperties("+43987614", "ES");
   1.149 +TestProperties("+0988782456");
   1.150 +TestProperties("+34556657");
   1.151 +TestProperties("+66554433");
   1.152 +TestProperties("+43442075");
   1.153 +TestProperties("+13442074");
   1.154 +
   1.155 +TestPropertiesWithExpectedCountry("+4333822222", "DE", "AT");
   1.156 +TestPropertiesWithExpectedCountry("+19491234567", "DE", "US");
   1.157 +
   1.158 +// Test whether could a string be a phone number.
   1.159 +IsPlain(null, false);
   1.160 +IsPlain("", false);
   1.161 +IsPlain("1", true);
   1.162 +IsPlain("*2", true); // Real number used in Venezuela
   1.163 +IsPlain("*8", true); // Real number used in Venezuela
   1.164 +IsPlain("12", true);
   1.165 +IsPlain("123", true);
   1.166 +IsPlain("1a2", false);
   1.167 +IsPlain("12a", false);
   1.168 +IsPlain("1234", true);
   1.169 +IsPlain("123a", false);
   1.170 +IsPlain("+", true);
   1.171 +IsPlain("+1", true);
   1.172 +IsPlain("+12", true);
   1.173 +IsPlain("+123", true);
   1.174 +IsPlain("()123", false);
   1.175 +IsPlain("(1)23", false);
   1.176 +IsPlain("(12)3", false);
   1.177 +IsPlain("(123)", false);
   1.178 +IsPlain("(123)4", false);
   1.179 +IsPlain("123;ext=", false);
   1.180 +IsPlain("123;ext=1", false);
   1.181 +IsPlain("123;ext=1234567", false);
   1.182 +IsPlain("123;ext=12345678", false);
   1.183 +IsPlain("123 ext:1", false);
   1.184 +IsPlain("123 ext:1#", false);
   1.185 +IsPlain("123-1#", false);
   1.186 +IsPlain("123 1#", false);
   1.187 +IsPlain("123 12345#", false);
   1.188 +IsPlain("123 +123456#", false);
   1.189 +IsPlain("#123#", true);
   1.190 +IsPlain("*#004#", true);
   1.191 +IsPlain("*30#", true);
   1.192 +IsPlain("*#06#", true);
   1.193 +IsPlain("123456789012345678901234567890123456789012345678901", false); // more than 50 characters
   1.194 +
   1.195 +// test normalization
   1.196 +Normalize(null, "");
   1.197 +Normalize("", "");
   1.198 +Normalize("1", "1");
   1.199 +Normalize("*2", "*2"); // Real number used in Venezuela
   1.200 +Normalize("*8", "*8"); // Real number used in Venezuela
   1.201 +Normalize("12", "12");
   1.202 +Normalize("123", "123");
   1.203 +Normalize("1a2", "122");
   1.204 +Normalize("12a", "122");
   1.205 +Normalize("1234", "1234");
   1.206 +Normalize("123a", "1232");
   1.207 +Normalize("+", "+");
   1.208 +Normalize("+1", "+1");
   1.209 +Normalize("+12", "+12");
   1.210 +Normalize("+123", "+123");
   1.211 +Normalize("()123", "123");
   1.212 +Normalize("(1)23", "123");
   1.213 +Normalize("(12)3", "123");
   1.214 +Normalize("(123)", "123");
   1.215 +Normalize("(123)4", "1234");
   1.216 +Normalize("123-1#", "1231#");
   1.217 +Normalize("123 1#", "1231#");
   1.218 +Normalize("123 12345#", "12312345#");
   1.219 +Normalize("123 +123456#", "123+123456#");
   1.220 +Normalize("#123#", "#123#");
   1.221 +Normalize("*#004#", "*#004#");
   1.222 +Normalize("*30#", "*30#");
   1.223 +Normalize("*#06#", "*#06#");
   1.224 +
   1.225 +// Test parsing national numbers.
   1.226 +Parse("033316005", "NZ");
   1.227 +Parse("03-331 6005", "NZ");
   1.228 +Parse("03 331 6005", "NZ");
   1.229 +
   1.230 +// Always test CA before US because CA has to load all meta-info for US.
   1.231 +ParseWithIntl("4031234567", "CA");
   1.232 +Parse("(416) 585-4319", "CA");
   1.233 +Parse("647-967-4357", "CA");
   1.234 +Parse("416-716-8768", "CA");
   1.235 +Parse("18002684646", "CA");
   1.236 +Parse("416-445-9119", "CA");
   1.237 +Parse("1-800-668-6866", "CA");
   1.238 +Parse("(416) 453-6486", "CA");
   1.239 +Parse("(647) 268-4778", "CA");
   1.240 +Parse("647-218-1313", "CA");
   1.241 +Parse("+1 647-209-4642", "CA");
   1.242 +Parse("416-559-0133", "CA");
   1.243 +Parse("+1 647-639-4118", "CA");
   1.244 +Parse("+12898803664", "CA");
   1.245 +Parse("780-901-4687", "CA");
   1.246 +Parse("+14167070550", "CA");
   1.247 +Parse("+1-647-522-6487", "CA");
   1.248 +Parse("(416) 877-0880", "CA");
   1.249 +
   1.250 +// Testing international prefixes.
   1.251 +// Should strip country code.
   1.252 +Parse("0064 3 331 6005", "NZ");
   1.253 +// Try again, but this time we have an international number with region rode US. It should
   1.254 +// recognize the country code and parse accordingly.
   1.255 +Parse("01164 3 331 6005", "US");
   1.256 +Parse("+64 3 331 6005", "US");
   1.257 +Parse("64(0)64123456", "NZ");
   1.258 +// Check that using a "/" is fine in a phone number.
   1.259 +Parse("123/45678", "DE");
   1.260 +Parse("123-456-7890", "US");
   1.261 +
   1.262 +// Test parsing international numbers.
   1.263 +Parse("+1 (650) 333-6000", "NZ");
   1.264 +Parse("1-650-333-6000", "US");
   1.265 +// Calling the US number from Singapore by using different service providers
   1.266 +// 1st test: calling using SingTel IDD service (IDD is 001)
   1.267 +Parse("0011-650-333-6000", "SG");
   1.268 +// 2nd test: calling using StarHub IDD service (IDD is 008)
   1.269 +Parse("0081-650-333-6000", "SG");
   1.270 +// 3rd test: calling using SingTel V019 service (IDD is 019)
   1.271 +Parse("0191-650-333-6000", "SG");
   1.272 +// Calling the US number from Poland
   1.273 +Parse("0~01-650-333-6000", "PL");
   1.274 +// Using "++" at the start.
   1.275 +Parse("++1 (650) 333-6000", "PL");
   1.276 +// Using a full-width plus sign.
   1.277 +Parse("\uFF0B1 (650) 333-6000", "SG");
   1.278 +// The whole number, including punctuation, is here represented in full-width form.
   1.279 +Parse("\uFF0B\uFF11\u3000\uFF08\uFF16\uFF15\uFF10\uFF09" +
   1.280 +      "\u3000\uFF13\uFF13\uFF13\uFF0D\uFF16\uFF10\uFF10\uFF10",
   1.281 +      "SG");
   1.282 +
   1.283 +// Test parsing with leading zeros.
   1.284 +Parse("+39 02-36618 300", "NZ");
   1.285 +Parse("02-36618 300", "IT");
   1.286 +Parse("312 345 678", "IT");
   1.287 +
   1.288 +// Test parsing numbers in Argentina.
   1.289 +Parse("+54 9 343 555 1212", "AR");
   1.290 +Parse("0343 15 555 1212", "AR");
   1.291 +Parse("+54 9 3715 65 4320", "AR");
   1.292 +Parse("03715 15 65 4320", "AR");
   1.293 +Parse("+54 11 3797 0000", "AR");
   1.294 +Parse("011 3797 0000", "AR");
   1.295 +Parse("+54 3715 65 4321", "AR");
   1.296 +Parse("03715 65 4321", "AR");
   1.297 +Parse("+54 23 1234 0000", "AR");
   1.298 +Parse("023 1234 0000", "AR");
   1.299 +
   1.300 +// Test numbers in Mexico
   1.301 +Parse("+52 (449)978-0001", "MX");
   1.302 +Parse("01 (449)978-0001", "MX");
   1.303 +Parse("(449)978-0001", "MX");
   1.304 +Parse("+52 1 33 1234-5678", "MX");
   1.305 +Parse("044 (33) 1234-5678", "MX");
   1.306 +Parse("045 33 1234-5678", "MX");
   1.307 +
   1.308 +// Test that lots of spaces are ok.
   1.309 +Parse("0 3   3 3 1   6 0 0 5", "NZ");
   1.310 +
   1.311 +// Test omitting the current region. This is only valid when the number starts
   1.312 +// with a '+'.
   1.313 +Parse("+64 3 331 6005");
   1.314 +Parse("+64 3 331 6005", null);
   1.315 +
   1.316 +// US numbers
   1.317 +Format("19497261234", "US", "9497261234", "US", "(949) 726-1234", "+1 949-726-1234");
   1.318 +
   1.319 +// Try a couple german numbers from the US with various access codes.
   1.320 +Format("49451491934", "US", "451491934", "DE", "0451 491934", "+49 451 491934");
   1.321 +Format("+49451491934", "US", "451491934", "DE", "0451 491934", "+49 451 491934");
   1.322 +Format("01149451491934", "US", "451491934", "DE", "0451 491934", "+49 451 491934");
   1.323 +
   1.324 +// Now try dialing the same number from within the German region.
   1.325 +Format("451491934", "DE", "451491934", "DE", "0451 491934", "+49 451 491934");
   1.326 +Format("0451491934", "DE", "451491934", "DE", "0451 491934", "+49 451 491934");
   1.327 +
   1.328 +// Numbers in italy keep the leading 0 in the city code when dialing internationally.
   1.329 +Format("0577-555-555", "IT", "0577555555", "IT", "05 7755 5555", "+39 05 7755 5555");
   1.330 +
   1.331 +// Telefonica tests
   1.332 +Format("612123123", "ES", "612123123", "ES", "612 12 31 23", "+34 612 12 31 23");
   1.333 +
   1.334 +// Chile mobile number from a landline
   1.335 +Format("0997654321", "CL", "997654321", "CL", "(99) 765 4321", "+56 99 765 4321");
   1.336 +
   1.337 +// Chile mobile number from another mobile number
   1.338 +Format("997654321", "CL", "997654321", "CL", "(99) 765 4321", "+56 99 765 4321");
   1.339 +
   1.340 +// Colombian international number without the leading "+"
   1.341 +Format("5712234567", "CO", "12234567", "CO", "(1) 2234567", "+57 1 2234567");
   1.342 +
   1.343 +// Dialing 911 in the US. This is not a national number.
   1.344 +CantParse("911", "US");
   1.345 +
   1.346 +// China mobile number with a 0 in it
   1.347 +Format("15955042864", "CN", "15955042864", "CN", "0159 5504 2864", "+86 159 5504 2864");
   1.348 +
   1.349 +// Test normalizing numbers. Only 0-9,#* are valid in a phone number.
   1.350 +Normalize("+ABC # * , 9 _ 1 _0", "+222#*,910");
   1.351 +Normalize("ABCDEFGHIJKLMNOPQRSTUVWXYZ", "22233344455566677778889999");
   1.352 +Normalize("abcdefghijklmnopqrstuvwxyz", "22233344455566677778889999");
   1.353 +
   1.354 +// 8 and 9 digit numbers with area code in Brazil with collect call prefix (90)
   1.355 +AllEqual(["01187654321","0411187654321","551187654321","90411187654321","+551187654321"],"BR");
   1.356 +AllEqual(["011987654321","04111987654321","5511987654321","904111987654321","+5511987654321"],"BR");
   1.357 +
   1.358 +</script>
   1.359 +</window>

mercurial