dom/contacts/tests/test_contacts_upgrade.xul

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.

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

mercurial