dom/contacts/tests/shared.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     1 "use strict";
     3 // Fix the environment to run Contacts tests
     4 if (SpecialPowers.isMainProcess()) {
     5   SpecialPowers.Cu.import("resource://gre/modules/ContactService.jsm");
     6   SpecialPowers.Cu.import("resource://gre/modules/PermissionPromptHelper.jsm");
     7 }
     9 SpecialPowers.addPermission("contacts-write", true, document);
    10 SpecialPowers.addPermission("contacts-read", true, document);
    11 SpecialPowers.addPermission("contacts-create", true, document);
    13 // Some helpful global vars
    14 var isAndroid = (navigator.userAgent.indexOf("Android") !== -1);
    16 var defaultOptions = {
    17   sortBy: "givenName",
    18 };
    20 var mozContacts = navigator.mozContacts;
    22 // To test sorting
    23 var c1 = {
    24   name: ["a a"],
    25   familyName: ["a"],
    26   givenName: ["a"],
    27 };
    29 var c2 = {
    30   name: ["b b"],
    31   familyName: ["b"],
    32   givenName: ["b"],
    33 };
    35 var c3 = {
    36   name: ["c c", "a a", "b b"],
    37   familyName: ["c","a","b"],
    38   givenName: ["c","a","b"],
    39 };
    41 var c4 = {
    42   name: ["c c", "a a", "c c"],
    43   familyName: ["c","a","c"],
    44   givenName: ["c","a","c"],
    45 };
    47 var c5 = {
    48   familyName: [],
    49   givenName: [],
    50 };
    52 var c6 = {
    53   name: ["e"],
    54   familyName: ["e","e","e"],
    55   givenName: ["e","e","e"],
    56 };
    58 var c7 = {
    59   name: ["e"],
    60   familyName: ["e","e","e"],
    61   givenName: ["e","e","e"],
    62 };
    64 var c8 = {
    65   name: ["e"],
    66   familyName: ["e","e","e"],
    67   givenName: ["e","e","e"],
    68 };
    70 var adr1 = {
    71   type: ["work"],
    72   streetAddress: "street 1",
    73   locality: "locality 1",
    74   region: "region 1",
    75   postalCode: "postal code 1",
    76   countryName: "country 1"
    77 };
    79 var adr2 = {
    80   type: ["home, fax"],
    81   streetAddress: "street2",
    82   locality: "locality2",
    83   region: "region2",
    84   postalCode: "postal code2",
    85   countryName: "country2"
    86 };
    88 var properties1 = {
    89   // please keep capital letters at the start of these names
    90   name: ["Test1 TestFamilyName", "Test2 Wagner"],
    91   familyName: ["TestFamilyName","Wagner"],
    92   givenName: ["Test1","Test2"],
    93   phoneticFamilyName: ["TestphoneticFamilyName1","TestphoneticFamilyName2"],
    94   phoneticGivenName: ["TestphoneticGivenName1","TestphoneticGivenName2"],
    95   nickname: ["nicktest"],
    96   tel: [{type: ["work"], value: "123456", carrier: "testCarrier"} , {type: ["home", "fax"], value: "+55 (31) 9876-3456"}, {type: ["home"], value: "+49 451 491934"}],
    97   adr: [adr1],
    98   email: [{type: ["work"], value: "x@y.com"}],
    99 };
   101 var properties2 = {
   102   name: ["dummyHonorificPrefix dummyGivenName dummyFamilyName dummyHonorificSuffix", "dummyHonorificPrefix2"],
   103   familyName: ["dummyFamilyName"],
   104   givenName: ["dummyGivenName"],
   105   phoneticFamilyName: ["dummyphoneticFamilyName"],
   106   phoneticGivenName: ["dummyphoneticGivenName"],
   107   honorificPrefix: ["dummyHonorificPrefix","dummyHonorificPrefix2"],
   108   honorificSuffix: ["dummyHonorificSuffix"],
   109   additionalName: ["dummyadditionalName"],
   110   nickname: ["dummyNickname"],
   111   tel: [{type: ["test"], value: "7932012345", carrier: "myCarrier", pref: 1},{type: ["home", "custom"], value: "7932012346", pref: 0}],
   112   email: [{type: ["test"], value: "a@b.c"}, {value: "b@c.d", pref: 1}],
   113   adr: [adr1, adr2],
   114   impp: [{type: ["aim"], value:"im1", pref: 1}, {value: "im2"}],
   115   org: ["org1", "org2"],
   116   jobTitle: ["boss", "superboss"],
   117   note: ["test note"],
   118   category: ["cat1", "cat2"],
   119   url: [{type: ["work", "work2"], value: "www.1.com", pref: 1}, {value:"www2.com"}],
   120   bday: new Date("1980, 12, 01"),
   121   anniversary: new Date("2000, 12, 01"),
   122   sex: "male",
   123   genderIdentity: "test",
   124   key: ["ERPJ394GJJWEVJ0349GJ09W3H4FG0WFW80VHW3408GH30WGH348G3H"]
   125 };
   127 // To test sorting(CJK)
   128 var c9 = {
   129   phoneticFamilyName: ["a"],
   130   phoneticGivenName: ["a"],
   131 };
   133 var c10 = {
   134   phoneticFamilyName: ["b"],
   135   phoneticGivenName: ["b"],
   136 };
   138 var c11 = {
   139   phoneticFamilyName: ["c","a","b"],
   140   phoneticGivenName: ["c","a","b"],
   141 };
   143 var c12 = {
   144   phoneticFamilyName: ["c","a","c"],
   145   phoneticGivenName: ["c","a","c"],
   146 };
   148 var c13 = {
   149   phoneticFamilyName: [],
   150   phoneticGivenName: [],
   151 };
   153 var c14 = {
   154   phoneticFamilyName: ["e","e","e"],
   155   phoneticGivenName: ["e","e","e"],
   156 };
   158 var c15 = {
   159   phoneticFamilyName: ["e","e","e"],
   160   phoneticGivenName: ["e","e","e"],
   161 };
   163 var c16 = {
   164   phoneticFamilyName: ["e","e","e"],
   165   phoneticGivenName: ["e","e","e"],
   166 };
   168 var properties3 = {
   169   // please keep capital letters at the start of these names
   170   name: ["Taro Yamada", "Ichiro Suzuki"],
   171   familyName: ["Yamada","Suzuki"],
   172   givenName: ["Taro","Ichiro"],
   173   phoneticFamilyName: ["TestPhoneticFamilyYamada","TestPhoneticFamilySuzuki"],
   174   phoneticGivenName: ["TestPhoneticGivenTaro","TestPhoneticGivenIchiro"],
   175   nickname: ["phoneticNicktest"],
   176   tel: [{type: ["work"], value: "123456", carrier: "testCarrier"} , {type: ["home", "fax"], value: "+55 (31) 9876-3456"}, {type: ["home"], value: "+49 451 491934"}],
   177   adr: [adr1],
   178   email: [{type: ["work"], value: "x@y.com"}],
   179 };
   181 var properties4 = {
   182   name: ["dummyHonorificPrefix dummyTaro dummyYamada dummyHonorificSuffix", "dummyHonorificPrefix2"],
   183   familyName: ["dummyYamada"],
   184   givenName: ["dummyTaro"],
   185   phoneticFamilyName: ["dummyTestPhoneticFamilyYamada"],
   186   phoneticGivenName: ["dummyTestPhoneticGivenTaro"],
   187   honorificPrefix: ["dummyPhoneticHonorificPrefix","dummyPhoneticHonorificPrefix2"],
   188   honorificSuffix: ["dummyPhoneticHonorificSuffix"],
   189   additionalName: ["dummyPhoneticAdditionalName"],
   190   nickname: ["dummyPhoneticNickname"],
   191   tel: [{type: ["test"], value: "7932012345", carrier: "myCarrier", pref: 1},{type: ["home", "custom"], value: "7932012346", pref: 0}],
   192   email: [{type: ["test"], value: "a@b.c"}, {value: "b@c.d", pref: 1}],
   193   adr: [adr1, adr2],
   194   impp: [{type: ["aim"], value:"im1", pref: 1}, {value: "im2"}],
   195   org: ["org1", "org2"],
   196   jobTitle: ["boss", "superboss"],
   197   note: ["test note"],
   198   category: ["cat1", "cat2"],
   199   url: [{type: ["work", "work2"], value: "www.1.com", pref: 1}, {value:"www2.com"}],
   200   bday: new Date("1980, 12, 01"),
   201   anniversary: new Date("2000, 12, 01"),
   202   sex: "male",
   203   genderIdentity: "test",
   204   key: ["ERPJ394GJJWEVJ0349GJ09W3H4FG0WFW80VHW3408GH30WGH348G3H"]
   205 };
   207 var sample_id1;
   208 var sample_id2;
   210 var createResult1;
   211 var createResult2;
   213 var findResult1;
   214 var findResult2;
   216 // DOMRequest helper functions
   217 function onUnwantedSuccess() {
   218   ok(false, "onUnwantedSuccess: shouldn't get here");
   219 }
   221 function onFailure() {
   222   ok(false, "in on Failure!");
   223   next();
   224 }
   226 // Validation helper functions
   227 function checkStr(str1, str2, msg) {
   228   if (str1 ^ str2) {
   229     ok(false, "Expected both strings to be either present or absent");
   230     return;
   231   }
   232   if (!str1 || str1 == "null") {
   233     str1 = null;
   234   }
   235   if (!str2 || str2 == "null") {
   236     str2 = null;
   237   }
   238   is(str1, str2, msg);
   239 }
   241 function checkStrArray(str1, str2, msg) {
   242   function normalize_falsy(v) {
   243     if (!v || v == "null" || v == "undefined") {
   244       return "";
   245     }
   246     return v;
   247   }
   248   function optArray(val) {
   249     return Array.isArray(val) ? val : [val];
   250   }
   251   str1 = optArray(str1).map(normalize_falsy).filter(v => v != "");
   252   str2 = optArray(str2).map(normalize_falsy).filter(v => v != "");
   253   ise(JSON.stringify(str1), JSON.stringify(str2), msg);
   254 }
   256 function checkPref(pref1, pref2) {
   257   // If on Android treat one preference as 0 and the other as undefined as matching
   258   if (isAndroid) {
   259     if ((!pref1 && pref2 == undefined) || (pref1 == undefined && !pref2)) {
   260       pref1 = false;
   261       pref2 = false;
   262     }
   263   }
   264   ise(!!pref1, !!pref2, "Same pref");
   265 }
   267 function checkAddress(adr1, adr2) {
   268   if (adr1 ^ adr2) {
   269     ok(false, "Expected both adrs to be either present or absent");
   270     return;
   271   }
   272   checkStrArray(adr1.type, adr2.type, "Same type");
   273   checkStr(adr1.streetAddress, adr2.streetAddress, "Same streetAddress");
   274   checkStr(adr1.locality, adr2.locality, "Same locality");
   275   checkStr(adr1.region, adr2.region, "Same region");
   276   checkStr(adr1.postalCode, adr2.postalCode, "Same postalCode");
   277   checkStr(adr1.countryName, adr2.countryName, "Same countryName");
   278   checkPref(adr1.pref, adr2.pref);
   279 }
   281 function checkField(field1, field2) {
   282   if (field1 ^ field2) {
   283     ok(false, "Expected both fields to be either present or absent");
   284     return;
   285   }
   286   checkStrArray(field1.type, field2.type, "Same type");
   287   checkStr(field1.value, field2.value, "Same value");
   288   checkPref(field1.pref, field2.pref);
   289 }
   291 function checkTel(tel1, tel2) {
   292   if (tel1 ^ tel2) {
   293     ok(false, "Expected both tels to be either present or absent");
   294     return;
   295   }
   296   checkField(tel1, tel2);
   297   checkStr(tel1.carrier, tel2.carrier, "Same carrier");
   298 }
   300 function checkCategory(category1, category2) {
   301   // Android adds contacts to the a default category. This should be removed from the
   302   // results before comparing them
   303   if (isAndroid) {
   304     category1 = removeAndroidDefaultCategory(category1);
   305     category2 = removeAndroidDefaultCategory(category2);
   306   }
   307   checkStrArray(category1, category2, "Same Category")
   308 }
   310 function removeAndroidDefaultCategory(category) {
   311   if (!category) {
   312     return category;
   313   }
   315   var result = [];
   317   for (var i of category) {
   318     // Some devices may return the full group name (prefixed with "System Group: ")
   319     if (i != "My Contacts" && i != "System Group: My Contacts") {
   320       result.push(i);
   321     }
   322   }
   324   return result;
   325 }
   327 function checkArrayField(array1, array2, func, msg) {
   328   if (!!array1 ^ !!array2) {
   329     ok(false, "Expected both arrays to be either present or absent: " + JSON.stringify(array1) + " vs. " + JSON.stringify(array2) + ". (" + msg + ")");
   330     return;
   331   }
   332   if (!array1 && !array2)  {
   333     ok(true, msg);
   334     return;
   335   }
   336   ise(array1.length, array2.length, "Same length");
   337   for (var i = 0; i < array1.length; ++i) {
   338     func(array1[i], array2[i], msg);
   339   }
   340 }
   342 function checkContacts(contact1, contact2) {
   343   if (!!contact1 ^ !!contact2) {
   344     ok(false, "Expected both contacts to be either present or absent");
   345     return;
   346   }
   347   checkStrArray(contact1.name, contact2.name, "Same name");
   348   checkStrArray(contact1.honorificPrefix, contact2.honorificPrefix, "Same honorificPrefix");
   349   checkStrArray(contact1.givenName, contact2.givenName, "Same givenName");
   350   checkStrArray(contact1.additionalName, contact2.additionalName, "Same additionalName");
   351   checkStrArray(contact1.familyName, contact2.familyName, "Same familyName");
   352   checkStrArray(contact1.phoneticFamilyName, contact2.phoneticFamilyName, "Same phoneticFamilyName");
   353   checkStrArray(contact1.phoneticGivenName, contact2.phoneticGivenName, "Same phoneticGivenName");
   354   checkStrArray(contact1.honorificSuffix, contact2.honorificSuffix, "Same honorificSuffix");
   355   checkStrArray(contact1.nickname, contact2.nickname, "Same nickname");
   356   checkCategory(contact1.category, contact2.category);
   357   checkStrArray(contact1.org, contact2.org, "Same org");
   358   checkStrArray(contact1.jobTitle, contact2.jobTitle, "Same jobTitle");
   359   is(contact1.bday ? contact1.bday.valueOf() : null, contact2.bday ? contact2.bday.valueOf() : null, "Same birthday");
   360   checkStrArray(contact1.note, contact2.note, "Same note");
   361   is(contact1.anniversary ? contact1.anniversary.valueOf() : null , contact2.anniversary ? contact2.anniversary.valueOf() : null, "Same anniversary");
   362   checkStr(contact1.sex, contact2.sex, "Same sex");
   363   checkStr(contact1.genderIdentity, contact2.genderIdentity, "Same genderIdentity");
   364   checkStrArray(contact1.key, contact2.key, "Same key");
   366   checkArrayField(contact1.adr, contact2.adr, checkAddress, "Same adr");
   367   checkArrayField(contact1.tel, contact2.tel, checkTel, "Same tel");
   368   checkArrayField(contact1.email, contact2.email, checkField, "Same email");
   369   checkArrayField(contact1.url, contact2.url, checkField, "Same url");
   370   checkArrayField(contact1.impp, contact2.impp, checkField, "Same impp");
   371 }
   373 function addContacts() {
   374   ok(true, "Adding 40 contacts");
   375   let req;
   376   for (let i = 0; i < 39; ++i) {
   377     properties1.familyName[0] = "Testname" + (i < 10 ? "0" + i : i);
   378     properties1.name = [properties1.givenName[0] + " " + properties1.familyName[0]];
   379     createResult1 = new mozContact(properties1);
   380     req = mozContacts.save(createResult1);
   381     req.onsuccess = function() {
   382       ok(createResult1.id, "The contact now has an ID.");
   383     };
   384     req.onerror = onFailure;
   385   };
   386   properties1.familyName[0] = "Testname39";
   387   properties1.name = [properties1.givenName[0] + " Testname39"];
   388   createResult1 = new mozContact(properties1);
   389   req = mozContacts.save(createResult1);
   390   req.onsuccess = function() {
   391     ok(createResult1.id, "The contact now has an ID.");
   392     checkStrArray(createResult1.name, properties1.name, "Same Name");
   393     next();
   394   };
   395   req.onerror = onFailure;
   396 }
   398 function getOne(msg) {
   399   return function() {
   400     ok(true, msg || "Retrieving one contact with getAll");
   401     let req = mozContacts.getAll({});
   403     let count = 0;
   404     req.onsuccess = function(event) {
   405       ok(true, "on success");
   406       if (req.result) {
   407         ok(true, "result is valid");
   408         count++;
   409         req.continue();
   410       } else {
   411         is(count, 1, "last contact - only one contact returned");
   412         next();
   413       }
   414     };
   415     req.onerror = onFailure;
   416   };
   417 }
   419 function getAll(msg) {
   420   return function() {
   421     ok(true, msg || "Retrieving 40 contacts with getAll");
   422     let req = mozContacts.getAll({
   423       sortBy: "familyName",
   424       sortOrder: "ascending"
   425     });
   426     let count = 0;
   427     let result;
   428     let props;
   429     req.onsuccess = function(event) {
   430       if (req.result) {
   431         ok(true, "result is valid");
   432         result = req.result;
   433         properties1.familyName[0] = "Testname" + (count < 10 ? "0" + count : count);
   434         is(result.familyName[0], properties1.familyName[0], "Same familyName");
   435         count++;
   436         req.continue();
   437       } else {
   438         is(count, 40, "last contact - 40 contacts returned");
   439         next();
   440       }
   441     };
   442     req.onerror = onFailure;
   443   };
   444 }
   446 function clearTemps() {
   447   sample_id1 = null;
   448   sample_id2 = null;
   449   createResult1 = null;
   450   createResult2 = null;
   451   findResult1 = null;
   452   findResult2 = null;
   453 }
   455 function clearDatabase() {
   456   ok(true, "Deleting database");
   457   let req = mozContacts.clear()
   458   req.onsuccess = function () {
   459     ok(true, "Deleted the database");
   460     next();
   461   }
   462   req.onerror = onFailure;
   463 }
   465 function checkCount(count, msg, then) {
   466   var request = mozContacts.getCount();
   467   request.onsuccess = function(e) {
   468     is(e.target.result, count, msg);
   469     then();
   470   };
   471   request.onerror = onFailure;
   472 }
   474 // Helper functions to run tests
   475 var index = 0;
   477 function next() {
   478   info("Step " + index);
   479   if (index >= steps.length) {
   480     ok(false, "Shouldn't get here!");
   481     return;
   482   }
   483   try {
   484     var i = index++;
   485     steps[i]();
   486   } catch(ex) {
   487     ok(false, "Caught exception", ex);
   488   }
   489 }
   491 SimpleTest.waitForExplicitFinish();
   493 function start_tests() {
   494   // Skip tests on Android < 4.0 due to test failures on tbpl (see bugs 897924 & 888891)
   495   let androidVersion = SpecialPowers.Cc['@mozilla.org/system-info;1']
   496                                     .getService(SpecialPowers.Ci.nsIPropertyBag2)
   497                                     .getProperty('version');
   498   if (!isAndroid || androidVersion >= 14) {
   499     next();
   500   } else {
   501     ok(true, "Skip tests on Android < 4.0 (bugs 897924 & 888891");
   502     SimpleTest.finish();
   503   }
   504 }

mercurial