dom/contacts/tests/test_contacts_upgrade.xul

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

     1 <?xml version="1.0"?>
     2 <?xml-stylesheet type="text/css" href="chrome://global/skin"?>
     3 <?xml-stylesheet type="text/css" href="/tests/SimpleTest/test.css"?>
     4 <!--
     5   Any copyright is dedicated to the Public Domain.
     6   http://creativecommons.org/publicdomain/zero/1.0/
     7 -->
     9 <window title="Mozilla Bug 889239"
    10         xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
    11   <script type="application/javascript"
    12           src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"/>
    14   <script type="application/javascript;version=1.7">
    15   <![CDATA[
    16   "use strict";
    18   var isAndroid = (navigator.userAgent.indexOf("Android") !== -1);
    20   function checkStr(str1, str2, msg) {
    21     if (str1 ^ str2) {
    22       ok(false, "Expected both strings to be either present or absent");
    23       return;
    24     }
    25     is(str1, str2, msg);
    26   }
    28   function checkStrArray(str1, str2, msg) {
    29     // comparing /[null(,null)+]/ and undefined should pass
    30     function nonNull(e) {
    31       return e != null;
    32     }
    33     if ((Array.isArray(str1) && str1.filter(nonNull).length == 0 && str2 == undefined)
    34        ||(Array.isArray(str2) && str2.filter(nonNull).length == 0 && str1 == undefined)) {
    35       ok(true, msg);
    36     } else if (str1) {
    37       is(JSON.stringify(typeof str1 == "string" ? [str1] : str1), JSON.stringify(typeof str2 == "string" ? [str2] : str2), msg);
    38     }
    39   }
    41   function checkAddress(adr1, adr2) {
    42     if (adr1 ^ adr2) {
    43       ok(false, "Expected both adrs to be either present or absent");
    44       return;
    45     }
    46     checkStrArray(adr1.type, adr2.type, "Same type");
    47     checkStrArray(adr1.streetAddress, adr2.streetAddress, "Same streetAddress");
    48     checkStrArray(adr1.locality, adr2.locality, "Same locality");
    49     checkStrArray(adr1.region, adr2.region, "Same region");
    50     checkStrArray(adr1.postalCode, adr2.postalCode, "Same postalCode");
    51     checkStrArray(adr1.countryName, adr2.countryName, "Same countryName");
    52     is(adr1.pref, adr2.pref, "Same pref");
    53   }
    55   function checkTel(tel1, tel2) {
    56     if (tel1 ^ tel2) {
    57       ok(false, "Expected both tels to be either present or absent");
    58       return;
    59     }
    60     checkStrArray(tel1.type, tel2.type, "Same type");
    61     checkStrArray(tel1.value, tel2.value, "Same value");
    62     checkStrArray(tel1.carrier, tel2.carrier, "Same carrier");
    63     is(tel1.pref, tel2.pref, "Same pref");
    64   }
    66   function checkField(field1, field2) {
    67     if (field1 ^ field2) {
    68       ok(false, "Expected both fields to be either present or absent");
    69       return;
    70     }
    71     checkStrArray(field1.type, field2.type, "Same type");
    72     checkStrArray(field1.value, field2.value, "Same value");
    73     is(field1.pref, field2.pref, "Same pref");
    74   }
    76   function checkDBContacts(dbContact1, dbContact2) {
    77     let contact1 = dbContact1.properties;
    78     let contact2 = dbContact2.properties;
    80     checkStrArray(contact1.name, contact2.name, "Same name");
    81     checkStrArray(contact1.honorificPrefix, contact2.honorificPrefix, "Same honorificPrefix");
    82     checkStrArray(contact1.givenName, contact2.givenName, "Same givenName");
    83     checkStrArray(contact1.additionalName, contact2.additionalName, "Same additionalName");
    84     checkStrArray(contact1.familyName, contact2.familyName, "Same familyName");
    85     checkStrArray(contact1.honorificSuffix, contact2.honorificSuffix, "Same honorificSuffix");
    86     checkStrArray(contact1.nickname, contact2.nickname, "Same nickname");
    87     checkStrArray(contact1.category, contact2.category, "Same category");
    88     checkStrArray(contact1.org, contact2.org, "Same org");
    89     checkStrArray(contact1.jobTitle, contact2.jobTitle, "Same jobTitle");
    90     is(contact1.bday ? contact1.bday.valueOf() : null, contact2.bday ? contact2.bday.valueOf() : null, "Same birthday");
    91     checkStrArray(contact1.note, contact2.note, "Same note");
    92     is(contact1.anniversary ? contact1.anniversary.valueOf() : null , contact2.anniversary ? contact2.anniversary.valueOf() : null, "Same anniversary");
    93     checkStr(contact1.sex, contact2.sex, "Same sex");
    94     checkStr(contact1.genderIdentity, contact2.genderIdentity, "Same genderIdentity");
    95     checkStrArray(contact1.key, contact2.key, "Same key");
    97     is(contact1.email.length, contact2.email.length, "Same number of emails");
    98     for (let i = 0; i < contact1.email.length; ++i) {
    99       checkField(contact1.email[i], contact2.email[i]);
   100     }
   102     is(contact1.adr.length, contact2.adr.length, "Same number of adrs");
   103     for (var i in contact1.adr) {
   104       checkAddress(contact1.adr[i], contact2.adr[i]);
   105     }
   107     is(contact1.tel.length, contact2.tel.length, "Same number of tels");
   108     for (var i in contact1.tel) {
   109       checkTel(contact1.tel[i], contact2.tel[i]);
   110     }
   112     is(contact1.url.length, contact2.url.length, "Same number of urls");
   113     for (var i in contact1.url) {
   114       checkField(contact1.url[i], contact2.url[i]);
   115     }
   117     is(contact1.impp.length, contact2.impp.length, "Same number of impps");
   118     for (var i in contact1.impp) {
   119       checkField(contact1.impp[i], contact2.impp[i]);
   120     }
   122     // test search indexes
   123     contact1 = dbContact1.search;
   124     contact2 = dbContact2.search;
   125     checkStrArray(contact1.category, contact2.category, "Same cateogry index");
   126     checkStrArray(contact1.email, contact2.email, "Same email index");
   127     checkStrArray(contact1.emailLowerCase, contact2.emailLowerCase, "Same emailLowerCase index");
   128     checkStrArray(contact1.familyName, contact2.familyName, "Same familyName index");
   129     checkStrArray(contact1.familyNameLowerCase, contact2.familyNameLowerCase, "Same familyNameLowerCase index");
   130     checkStrArray(contact1.givenName, contact2.givenName, "Same givenName index");
   131     checkStrArray(contact1.givenNameLowerCase, contact2.givenNameLowerCase, "Same givenNameLowerCase index");
   132     checkStrArray(contact1.name, contact2.name, "Same name index");
   133     checkStrArray(contact1.tel, contact2.tel, "Same tel index");
   134     checkStrArray(contact1.telLowerCase, contact2.telLowerCase, "Same telLowerCase index");
   135     checkStrArray(contact1.telMatch, contact2.telMatch, "Same telMatch index");
   136   }
   138   function makeFailure(reason) {
   139     return function() {
   140       ok(false, reason);
   141       SimpleTest.finish();
   142     };
   143   };
   145   const { 'utils': Cu } = Components;
   146   Cu.import("resource://gre/modules/ContactDB.jsm", window);
   147   Cu.importGlobalProperties(["indexedDB"]);
   149   let cdb = new ContactDB();
   150   cdb.init();
   152   let CONTACT_PROPS = {
   153     id: "ab74671e36be41b680f8f030e7e24ea2",
   154     properties: {
   155       name: ["Magnificentest foo bar the third"],
   156       givenName: ["foo"],
   157       familyName: ["bar"],
   158       honorificPrefix: ["magnificentest"],
   159       honorificSuffix: ["the third"],
   160       additionalName: ["addl"],
   161       nickname: ["foo"],
   162       tel: [
   163         {type: ["mobile"], value: "+12345678901", carrier: "ACME Telecommunications", pref: true},
   164         {type: ["home", "custom"], value: "7932012346", pref: false},
   165       ],
   166       email: [{type: ["work"], value: "a@b.c"}, {value: "b@c.d", pref: true}],
   167       adr: [
   168         {
   169           type: ["home"],
   170           streetAddress: "street 1",
   171           locality: "locality 1",
   172           region: "region 1",
   173           postalCode: "postal code 1",
   174           countryName: "country 1",
   175         }
   176       ],
   177       impp: [{type: ["aim"], value:"im1", pref: true}, {value: "im2"}],
   178       org: ["org1", "org2"],
   179       jobTitle: ["boss", "superboss"],
   180       bday: new Date("1980, 12, 01"),
   181       note: ["bla bla bla"],
   182       category: ["cat1", "cat2"],
   183       url: [{type: ["work", "work2"], value: "www.1.com", pref: true}, {value: "www2.com"}],
   184       anniversary: new Date("2000, 12, 01"),
   185       sex: "male",
   186       genderIdentity: "trisexual",
   187       key: "iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAIAAACQkWg2AAAACXBIWXMAAC4jAAAuIwF4pT92AAACrElEQVQozwXBTW8bRRgA4Hfemf1er7/iJI4Tq7VFlEZN1VZIlapy4MQBTkXcuSH+G/APKnGAAyCVCqmtCHETp64db5zdtdf7NbMzw/OQH378HkCZpmmapqYMy8yrNnadS6026HC/Z7k+SCkEBwKEEKaUQtQAmlDqrucH23nH4BRkJVRcwmod5gcn6LehFgCaEIIalFZaEcLCq73w355RdvY7nfGQGVTlmRXfqMlrUaSUMUQkhCISJIggKj3/YBHt7PRbpy+cwbF7dN/0vEqTMoo3s0tmGAAAoJAgImMq3xZ5WTPbHj4Mho8Nf+QcPtZBLxEkqeQ2WmklkRCtNdNaI1KpVCnqOC3j5ZK++4vnm6xSWZpzwQtRV2mOiBoRpEKtNQAQggjQcCwqinRxJeKlWW93dlqEsa2QRZbF85nWBAAZY4YUgl9fRJWKVuWgmhwHhpD1+ZrfVjAN867rMCne//rq7OuXjWaLCVHnOWHgFDwMw+Tvi09PdhtJXoVC7bWDIi8Lg8qyMk3rYjLzvJh2O30hwK6TpiG7zWDcck9GR17D9wxDcH7/oNtElRa1aZuLDJN4S7/87tssLVg0/eZs/3h0D5R89vR0v+1AVT0YHX31ZDy9uv7IeJrryeyu2+nS50/PqOXM5qt8Nf/jv08UwTfN27vkchldLpPf/nx/nqSz5sbzhkTYzLRppzNYre/ycrMIZwqsHdf96fd/Xr354AYBr/jESWhgGb6zVSuGrrQS1j4Zk8nc2Hs7frFb3Phc6+fOKDGLKOJTHvlj2u85N4t6vbw7OM4YRVquboPdsPNZ9eb8pvfAOf2iN4dN3EzWadnoO5JY19Oo0TYtw1t8TBqBR9v7wbOXROLWtZ3PH937+ZfXrb6BUHEbXL+FCIfDw92e5zebg8GR54r/AaMVcBxE6hgPAAAAJXRFWHRkYXRlOmNyZWF0ZQAyMDEyLTA3LTIxVDEwOjUzOjE5LTA0OjAwYyXbYgAAACV0RVh0ZGF0ZTptb2RpZnkAMjAxMi0wNy0yMVQxMDo1MzoxOS0wNDowMBJ4Y94AAAARdEVYdGpwZWc6Y29sb3JzcGFjZQAyLHVVnwAAACB0RVh0anBlZzpzYW1wbGluZy1mYWN0b3IAMXgxLDF4MSwxeDHplfxwAAAAAElFTkSuQmCC"
   188     }
   189   };
   191   function deleteDatabase(then) {
   192     cdb.close();
   193     let req = indexedDB.deleteDatabase(DB_NAME);
   194     req.onsuccess = then;
   195     req.onblocked = makeFailure("blocked");
   196     req.onupgradeneeded = makeFailure("onupgradeneeded");
   197     req.onerror = makeFailure("onerror");
   198   }
   200   function saveContact() {
   201     // takes fast upgrade path
   202     cdb.saveContact(CONTACT_PROPS,
   203       function() {
   204         ok(true, "Saved contact successfully");
   205         next();
   206       }
   207     );
   208   }
   210   function getContact(callback) {
   211     return function() {
   212       let req = indexedDB.open(STORE_NAME, DB_VERSION);
   213       req.onsuccess = function(event) {
   214         let db = event.target.result;
   215         let txn = db.transaction([STORE_NAME], "readonly");
   216         txn.onabort = makeFailure("Failed to open transaction");
   217         let r2 = txn.objectStore(STORE_NAME).get(CONTACT_PROPS.id);
   218         r2.onsuccess = function() {
   219           db.close();
   220           callback(r2.result);
   221         };
   222         r2.onerror = makeFailure("Failed to get contact");
   223       };
   224     };
   225   }
   227   let savedContact;
   229   let Tests = [
   230     saveContact,
   232     getContact(function(contact) {
   233       savedContact = contact;
   234       next();
   235     }),
   237     function() {
   238       deleteDatabase(function() {
   239         info("slow upgrade");
   240         cdb.useFastUpgrade = false;
   241         cdb.init();
   242         next();
   243       });
   244     },
   246     saveContact,
   248     getContact(function(contact) {
   249       checkDBContacts(savedContact, contact);
   250       next();
   251     }),
   252   ];
   254   function next() {
   255     let step = Tests.shift();
   256     if (step) {
   257       step();
   258     } else {
   259       info("All done");
   260       SimpleTest.finish();
   261     }
   262   }
   264   // Skip tests on Android
   265   if (!isAndroid) {
   266     SimpleTest.waitForExplicitFinish();
   267     next();
   268   } else {
   269     ok(true, "Skip test on Android");
   270   }
   272   ]]>
   273   </script>
   275   <body xmlns="http://www.w3.org/1999/xhtml">
   276   <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=889239"
   277      target="_blank">Mozilla Bug 889239</a>
   278   </body>
   279 </window>

mercurial