dom/phonenumberutils/tests/test_phonenumber.xul

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

     1 <?xml version="1.0"?>
     3 <!-- Any copyright is dedicated to the Public Domain.
     4    - http://creativecommons.org/publicdomain/zero/1.0/ -->
     6 <?xml-stylesheet type="text/css" href="chrome://global/skin"?>
     7 <?xml-stylesheet type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css"?>
     9 <window xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
    10         title="Mozilla Bug 781379">
    11   <script type="application/javascript" src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"/>
    12   <script type="application/javascript" src="head.js"/>
    13   <!-- test results are displayed in the html:body -->
    14   <body xmlns="http://www.w3.org/1999/xhtml">
    15   <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=809213"
    16      target="_blank">Mozilla Bug 809213</a>
    17   </body>
    19 <script type="application/javascript;version=1.8">
    21 "use strict";
    23 Components.utils.import("resource://gre/modules/PhoneNumber.jsm");
    24 Components.utils.import("resource://gre/modules/PhoneNumberNormalizer.jsm");
    26 function IsPlain(dial, expected) {
    27   var result = PhoneNumber.IsPlain(dial);
    28   if (result != expected) {
    29     ok(false, dial + " is " + (result ? "" : "not ") + "plain, expects otherwise.");
    30   } else {
    31     ok(true, dial + " is " + (result ? "" : "not ") + "plain as expected.");
    32   }
    33 }
    35 function Normalize(dial, expected) {
    36   var result = PhoneNumberNormalizer.Normalize(dial);
    37   if (result !== expected) {
    38     ok(false, "normalized " + dial + " to " + result + ", expected " + expected + " instead.");
    39   } else {
    40     ok(true, "normalized " + dial + " to " + result + " as expected.");
    41   }
    42 }
    44 function Normalize(dial, expected) {
    45   var result = PhoneNumberNormalizer.Normalize(dial);
    46   if (result != expected) {
    47     ok(false, "Normalize error!\n");
    48     print("expected: " + expected);
    49     print("got: " + result);
    50   } else {
    51     ok(true, "Normalization OK");
    52   }
    53 }
    55 function CantParse(dial, currentRegion) {
    56   var result = PhoneNumber.Parse(dial, currentRegion);
    57   if (result) {
    58   	ok(false, "Shouldn't parse!\n");
    59     print("expected: does not parse");
    60     print("got: " + dial + " " + currentRegion);
    61   }
    62 }
    64 function Parse(dial, currentRegion) {
    65   dump("try: " + dial + ", " + currentRegion + "\n");
    66   var result = PhoneNumber.Parse(dial, currentRegion);
    67   if (!result) {
    68   	ok(false, "got: " + dial + " " + currentRegion);
    69   } else {
    70   	ok(true, "Parses!\n");
    71   }
    72   return result;
    73 }
    75 function ParseWithIntl(dial, currentRegion) {
    76   var result = PhoneNumber.Parse(dial, currentRegion);
    77   if (!result) {
    78   	ok(false, "got: " + dial + " " + currentRegion);
    79   } else {
    80     ok(result.internationalNumber, "Has International!");
    81   	ok(true, "Parses!\n");
    82   }
    83   return result;
    84 }
    86 function Test(dial, currentRegion, nationalNumber, region) {
    87   var result = Parse(dial, currentRegion);
    88   if (result.region != region || result.nationalNumber != nationalNumber) {
    89     ok(false, "expected: " + nationalNumber + " " + region + "got: " + result.nationalNumber + " " + result.region);
    90   } else {
    91   	ok(true, "Test ok!");
    92   }
    93   return result;
    94 }
    96 function Format(dial, currentRegion, nationalNumber, region, nationalFormat, internationalFormat) {
    97   var result = Test(dial, currentRegion, nationalNumber, region);
    98   if (result.nationalFormat != nationalFormat ||
    99       result.internationalFormat != internationalFormat) {
   100     ok(false, "expected: " + nationalFormat + " " + internationalFormat + 
   101       "got: " + result.nationalFormat + " " + result.internationalFormat);
   102   }
   103 }
   105 function TestProperties(dial, currentRegion) {
   106   var result = PhoneNumber.Parse(dial, currentRegion);
   107   if (result) {
   108     ok(true, "found it");
   109     ok(true, "InternationalFormat: " + result.internationalFormat);
   110     ok(true, "InternationalNumber: " + result.internationalNumber);
   111     ok(true, "NationalNumber: " + result.nationalNumber);
   112     ok(true, "NationalFormat: " + result.nationalFormat);
   113   } else {
   114     ok(true, "not found");
   115   }
   116 }
   118 function TestPropertiesWithExpectedCountry(dial, currentRegion, expectedRegion) {
   119   var result = PhoneNumber.Parse(dial, currentRegion);
   120   if (result) {
   121     ok(true, "found it");
   122     ok(true, "InternationalFormat: " + result.internationalFormat);
   123     ok(true, "InternationalNumber: " + result.internationalNumber);
   124     ok(true, "NationalNumber: " + result.nationalNumber);
   125     ok(true, "NationalFormat: " + result.nationalFormat);
   126     ok(true, "CountryName: " + result.countryName);
   127     is(result.countryName, expectedRegion, "Same region");
   128   } else {
   129     ok(true, "not found");
   130   }
   131 }
   133 function AllEqual(list, currentRegion) {
   134   var first = PhoneNumber.Parse(list.shift(), currentRegion);
   135   ok(!!first, "first parses");
   136   for (var index in list) {
   137     var other = PhoneNumber.Parse(list[index], currentRegion);
   138     ok(!!other, "other parses");
   139     ok(first.internationalNumber == other.internationalNumber, "first and other match");
   140   }
   141 }
   143 TestProperties("+0988782456");
   144 TestProperties("+33442020", "ES");
   145 TestProperties("+43987614", "ES");
   146 TestProperties("+0988782456");
   147 TestProperties("+34556657");
   148 TestProperties("+66554433");
   149 TestProperties("+43442075");
   150 TestProperties("+13442074");
   152 TestPropertiesWithExpectedCountry("+4333822222", "DE", "AT");
   153 TestPropertiesWithExpectedCountry("+19491234567", "DE", "US");
   155 // Test whether could a string be a phone number.
   156 IsPlain(null, false);
   157 IsPlain("", false);
   158 IsPlain("1", true);
   159 IsPlain("*2", true); // Real number used in Venezuela
   160 IsPlain("*8", true); // Real number used in Venezuela
   161 IsPlain("12", true);
   162 IsPlain("123", true);
   163 IsPlain("1a2", false);
   164 IsPlain("12a", false);
   165 IsPlain("1234", true);
   166 IsPlain("123a", false);
   167 IsPlain("+", true);
   168 IsPlain("+1", true);
   169 IsPlain("+12", true);
   170 IsPlain("+123", true);
   171 IsPlain("()123", false);
   172 IsPlain("(1)23", false);
   173 IsPlain("(12)3", false);
   174 IsPlain("(123)", false);
   175 IsPlain("(123)4", false);
   176 IsPlain("123;ext=", false);
   177 IsPlain("123;ext=1", false);
   178 IsPlain("123;ext=1234567", false);
   179 IsPlain("123;ext=12345678", false);
   180 IsPlain("123 ext:1", false);
   181 IsPlain("123 ext:1#", false);
   182 IsPlain("123-1#", false);
   183 IsPlain("123 1#", false);
   184 IsPlain("123 12345#", false);
   185 IsPlain("123 +123456#", false);
   186 IsPlain("#123#", true);
   187 IsPlain("*#004#", true);
   188 IsPlain("*30#", true);
   189 IsPlain("*#06#", true);
   190 IsPlain("123456789012345678901234567890123456789012345678901", false); // more than 50 characters
   192 // test normalization
   193 Normalize(null, "");
   194 Normalize("", "");
   195 Normalize("1", "1");
   196 Normalize("*2", "*2"); // Real number used in Venezuela
   197 Normalize("*8", "*8"); // Real number used in Venezuela
   198 Normalize("12", "12");
   199 Normalize("123", "123");
   200 Normalize("1a2", "122");
   201 Normalize("12a", "122");
   202 Normalize("1234", "1234");
   203 Normalize("123a", "1232");
   204 Normalize("+", "+");
   205 Normalize("+1", "+1");
   206 Normalize("+12", "+12");
   207 Normalize("+123", "+123");
   208 Normalize("()123", "123");
   209 Normalize("(1)23", "123");
   210 Normalize("(12)3", "123");
   211 Normalize("(123)", "123");
   212 Normalize("(123)4", "1234");
   213 Normalize("123-1#", "1231#");
   214 Normalize("123 1#", "1231#");
   215 Normalize("123 12345#", "12312345#");
   216 Normalize("123 +123456#", "123+123456#");
   217 Normalize("#123#", "#123#");
   218 Normalize("*#004#", "*#004#");
   219 Normalize("*30#", "*30#");
   220 Normalize("*#06#", "*#06#");
   222 // Test parsing national numbers.
   223 Parse("033316005", "NZ");
   224 Parse("03-331 6005", "NZ");
   225 Parse("03 331 6005", "NZ");
   227 // Always test CA before US because CA has to load all meta-info for US.
   228 ParseWithIntl("4031234567", "CA");
   229 Parse("(416) 585-4319", "CA");
   230 Parse("647-967-4357", "CA");
   231 Parse("416-716-8768", "CA");
   232 Parse("18002684646", "CA");
   233 Parse("416-445-9119", "CA");
   234 Parse("1-800-668-6866", "CA");
   235 Parse("(416) 453-6486", "CA");
   236 Parse("(647) 268-4778", "CA");
   237 Parse("647-218-1313", "CA");
   238 Parse("+1 647-209-4642", "CA");
   239 Parse("416-559-0133", "CA");
   240 Parse("+1 647-639-4118", "CA");
   241 Parse("+12898803664", "CA");
   242 Parse("780-901-4687", "CA");
   243 Parse("+14167070550", "CA");
   244 Parse("+1-647-522-6487", "CA");
   245 Parse("(416) 877-0880", "CA");
   247 // Testing international prefixes.
   248 // Should strip country code.
   249 Parse("0064 3 331 6005", "NZ");
   250 // Try again, but this time we have an international number with region rode US. It should
   251 // recognize the country code and parse accordingly.
   252 Parse("01164 3 331 6005", "US");
   253 Parse("+64 3 331 6005", "US");
   254 Parse("64(0)64123456", "NZ");
   255 // Check that using a "/" is fine in a phone number.
   256 Parse("123/45678", "DE");
   257 Parse("123-456-7890", "US");
   259 // Test parsing international numbers.
   260 Parse("+1 (650) 333-6000", "NZ");
   261 Parse("1-650-333-6000", "US");
   262 // Calling the US number from Singapore by using different service providers
   263 // 1st test: calling using SingTel IDD service (IDD is 001)
   264 Parse("0011-650-333-6000", "SG");
   265 // 2nd test: calling using StarHub IDD service (IDD is 008)
   266 Parse("0081-650-333-6000", "SG");
   267 // 3rd test: calling using SingTel V019 service (IDD is 019)
   268 Parse("0191-650-333-6000", "SG");
   269 // Calling the US number from Poland
   270 Parse("0~01-650-333-6000", "PL");
   271 // Using "++" at the start.
   272 Parse("++1 (650) 333-6000", "PL");
   273 // Using a full-width plus sign.
   274 Parse("\uFF0B1 (650) 333-6000", "SG");
   275 // The whole number, including punctuation, is here represented in full-width form.
   276 Parse("\uFF0B\uFF11\u3000\uFF08\uFF16\uFF15\uFF10\uFF09" +
   277       "\u3000\uFF13\uFF13\uFF13\uFF0D\uFF16\uFF10\uFF10\uFF10",
   278       "SG");
   280 // Test parsing with leading zeros.
   281 Parse("+39 02-36618 300", "NZ");
   282 Parse("02-36618 300", "IT");
   283 Parse("312 345 678", "IT");
   285 // Test parsing numbers in Argentina.
   286 Parse("+54 9 343 555 1212", "AR");
   287 Parse("0343 15 555 1212", "AR");
   288 Parse("+54 9 3715 65 4320", "AR");
   289 Parse("03715 15 65 4320", "AR");
   290 Parse("+54 11 3797 0000", "AR");
   291 Parse("011 3797 0000", "AR");
   292 Parse("+54 3715 65 4321", "AR");
   293 Parse("03715 65 4321", "AR");
   294 Parse("+54 23 1234 0000", "AR");
   295 Parse("023 1234 0000", "AR");
   297 // Test numbers in Mexico
   298 Parse("+52 (449)978-0001", "MX");
   299 Parse("01 (449)978-0001", "MX");
   300 Parse("(449)978-0001", "MX");
   301 Parse("+52 1 33 1234-5678", "MX");
   302 Parse("044 (33) 1234-5678", "MX");
   303 Parse("045 33 1234-5678", "MX");
   305 // Test that lots of spaces are ok.
   306 Parse("0 3   3 3 1   6 0 0 5", "NZ");
   308 // Test omitting the current region. This is only valid when the number starts
   309 // with a '+'.
   310 Parse("+64 3 331 6005");
   311 Parse("+64 3 331 6005", null);
   313 // US numbers
   314 Format("19497261234", "US", "9497261234", "US", "(949) 726-1234", "+1 949-726-1234");
   316 // Try a couple german numbers from the US with various access codes.
   317 Format("49451491934", "US", "451491934", "DE", "0451 491934", "+49 451 491934");
   318 Format("+49451491934", "US", "451491934", "DE", "0451 491934", "+49 451 491934");
   319 Format("01149451491934", "US", "451491934", "DE", "0451 491934", "+49 451 491934");
   321 // Now try dialing the same number from within the German region.
   322 Format("451491934", "DE", "451491934", "DE", "0451 491934", "+49 451 491934");
   323 Format("0451491934", "DE", "451491934", "DE", "0451 491934", "+49 451 491934");
   325 // Numbers in italy keep the leading 0 in the city code when dialing internationally.
   326 Format("0577-555-555", "IT", "0577555555", "IT", "05 7755 5555", "+39 05 7755 5555");
   328 // Telefonica tests
   329 Format("612123123", "ES", "612123123", "ES", "612 12 31 23", "+34 612 12 31 23");
   331 // Chile mobile number from a landline
   332 Format("0997654321", "CL", "997654321", "CL", "(99) 765 4321", "+56 99 765 4321");
   334 // Chile mobile number from another mobile number
   335 Format("997654321", "CL", "997654321", "CL", "(99) 765 4321", "+56 99 765 4321");
   337 // Colombian international number without the leading "+"
   338 Format("5712234567", "CO", "12234567", "CO", "(1) 2234567", "+57 1 2234567");
   340 // Dialing 911 in the US. This is not a national number.
   341 CantParse("911", "US");
   343 // China mobile number with a 0 in it
   344 Format("15955042864", "CN", "15955042864", "CN", "0159 5504 2864", "+86 159 5504 2864");
   346 // Test normalizing numbers. Only 0-9,#* are valid in a phone number.
   347 Normalize("+ABC # * , 9 _ 1 _0", "+222#*,910");
   348 Normalize("ABCDEFGHIJKLMNOPQRSTUVWXYZ", "22233344455566677778889999");
   349 Normalize("abcdefghijklmnopqrstuvwxyz", "22233344455566677778889999");
   351 // 8 and 9 digit numbers with area code in Brazil with collect call prefix (90)
   352 AllEqual(["01187654321","0411187654321","551187654321","90411187654321","+551187654321"],"BR");
   353 AllEqual(["011987654321","04111987654321","5511987654321","904111987654321","+5511987654321"],"BR");
   355 </script>
   356 </window>

mercurial