1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/dom/contacts/tests/test_contacts_upgrade.xul Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,279 @@ 1.4 +<?xml version="1.0"?> 1.5 +<?xml-stylesheet type="text/css" href="chrome://global/skin"?> 1.6 +<?xml-stylesheet type="text/css" href="/tests/SimpleTest/test.css"?> 1.7 +<!-- 1.8 + Any copyright is dedicated to the Public Domain. 1.9 + http://creativecommons.org/publicdomain/zero/1.0/ 1.10 +--> 1.11 + 1.12 +<window title="Mozilla Bug 889239" 1.13 + xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"> 1.14 + <script type="application/javascript" 1.15 + src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"/> 1.16 + 1.17 + <script type="application/javascript;version=1.7"> 1.18 + <![CDATA[ 1.19 + "use strict"; 1.20 + 1.21 + var isAndroid = (navigator.userAgent.indexOf("Android") !== -1); 1.22 + 1.23 + function checkStr(str1, str2, msg) { 1.24 + if (str1 ^ str2) { 1.25 + ok(false, "Expected both strings to be either present or absent"); 1.26 + return; 1.27 + } 1.28 + is(str1, str2, msg); 1.29 + } 1.30 + 1.31 + function checkStrArray(str1, str2, msg) { 1.32 + // comparing /[null(,null)+]/ and undefined should pass 1.33 + function nonNull(e) { 1.34 + return e != null; 1.35 + } 1.36 + if ((Array.isArray(str1) && str1.filter(nonNull).length == 0 && str2 == undefined) 1.37 + ||(Array.isArray(str2) && str2.filter(nonNull).length == 0 && str1 == undefined)) { 1.38 + ok(true, msg); 1.39 + } else if (str1) { 1.40 + is(JSON.stringify(typeof str1 == "string" ? [str1] : str1), JSON.stringify(typeof str2 == "string" ? [str2] : str2), msg); 1.41 + } 1.42 + } 1.43 + 1.44 + function checkAddress(adr1, adr2) { 1.45 + if (adr1 ^ adr2) { 1.46 + ok(false, "Expected both adrs to be either present or absent"); 1.47 + return; 1.48 + } 1.49 + checkStrArray(adr1.type, adr2.type, "Same type"); 1.50 + checkStrArray(adr1.streetAddress, adr2.streetAddress, "Same streetAddress"); 1.51 + checkStrArray(adr1.locality, adr2.locality, "Same locality"); 1.52 + checkStrArray(adr1.region, adr2.region, "Same region"); 1.53 + checkStrArray(adr1.postalCode, adr2.postalCode, "Same postalCode"); 1.54 + checkStrArray(adr1.countryName, adr2.countryName, "Same countryName"); 1.55 + is(adr1.pref, adr2.pref, "Same pref"); 1.56 + } 1.57 + 1.58 + function checkTel(tel1, tel2) { 1.59 + if (tel1 ^ tel2) { 1.60 + ok(false, "Expected both tels to be either present or absent"); 1.61 + return; 1.62 + } 1.63 + checkStrArray(tel1.type, tel2.type, "Same type"); 1.64 + checkStrArray(tel1.value, tel2.value, "Same value"); 1.65 + checkStrArray(tel1.carrier, tel2.carrier, "Same carrier"); 1.66 + is(tel1.pref, tel2.pref, "Same pref"); 1.67 + } 1.68 + 1.69 + function checkField(field1, field2) { 1.70 + if (field1 ^ field2) { 1.71 + ok(false, "Expected both fields to be either present or absent"); 1.72 + return; 1.73 + } 1.74 + checkStrArray(field1.type, field2.type, "Same type"); 1.75 + checkStrArray(field1.value, field2.value, "Same value"); 1.76 + is(field1.pref, field2.pref, "Same pref"); 1.77 + } 1.78 + 1.79 + function checkDBContacts(dbContact1, dbContact2) { 1.80 + let contact1 = dbContact1.properties; 1.81 + let contact2 = dbContact2.properties; 1.82 + 1.83 + checkStrArray(contact1.name, contact2.name, "Same name"); 1.84 + checkStrArray(contact1.honorificPrefix, contact2.honorificPrefix, "Same honorificPrefix"); 1.85 + checkStrArray(contact1.givenName, contact2.givenName, "Same givenName"); 1.86 + checkStrArray(contact1.additionalName, contact2.additionalName, "Same additionalName"); 1.87 + checkStrArray(contact1.familyName, contact2.familyName, "Same familyName"); 1.88 + checkStrArray(contact1.honorificSuffix, contact2.honorificSuffix, "Same honorificSuffix"); 1.89 + checkStrArray(contact1.nickname, contact2.nickname, "Same nickname"); 1.90 + checkStrArray(contact1.category, contact2.category, "Same category"); 1.91 + checkStrArray(contact1.org, contact2.org, "Same org"); 1.92 + checkStrArray(contact1.jobTitle, contact2.jobTitle, "Same jobTitle"); 1.93 + is(contact1.bday ? contact1.bday.valueOf() : null, contact2.bday ? contact2.bday.valueOf() : null, "Same birthday"); 1.94 + checkStrArray(contact1.note, contact2.note, "Same note"); 1.95 + is(contact1.anniversary ? contact1.anniversary.valueOf() : null , contact2.anniversary ? contact2.anniversary.valueOf() : null, "Same anniversary"); 1.96 + checkStr(contact1.sex, contact2.sex, "Same sex"); 1.97 + checkStr(contact1.genderIdentity, contact2.genderIdentity, "Same genderIdentity"); 1.98 + checkStrArray(contact1.key, contact2.key, "Same key"); 1.99 + 1.100 + is(contact1.email.length, contact2.email.length, "Same number of emails"); 1.101 + for (let i = 0; i < contact1.email.length; ++i) { 1.102 + checkField(contact1.email[i], contact2.email[i]); 1.103 + } 1.104 + 1.105 + is(contact1.adr.length, contact2.adr.length, "Same number of adrs"); 1.106 + for (var i in contact1.adr) { 1.107 + checkAddress(contact1.adr[i], contact2.adr[i]); 1.108 + } 1.109 + 1.110 + is(contact1.tel.length, contact2.tel.length, "Same number of tels"); 1.111 + for (var i in contact1.tel) { 1.112 + checkTel(contact1.tel[i], contact2.tel[i]); 1.113 + } 1.114 + 1.115 + is(contact1.url.length, contact2.url.length, "Same number of urls"); 1.116 + for (var i in contact1.url) { 1.117 + checkField(contact1.url[i], contact2.url[i]); 1.118 + } 1.119 + 1.120 + is(contact1.impp.length, contact2.impp.length, "Same number of impps"); 1.121 + for (var i in contact1.impp) { 1.122 + checkField(contact1.impp[i], contact2.impp[i]); 1.123 + } 1.124 + 1.125 + // test search indexes 1.126 + contact1 = dbContact1.search; 1.127 + contact2 = dbContact2.search; 1.128 + checkStrArray(contact1.category, contact2.category, "Same cateogry index"); 1.129 + checkStrArray(contact1.email, contact2.email, "Same email index"); 1.130 + checkStrArray(contact1.emailLowerCase, contact2.emailLowerCase, "Same emailLowerCase index"); 1.131 + checkStrArray(contact1.familyName, contact2.familyName, "Same familyName index"); 1.132 + checkStrArray(contact1.familyNameLowerCase, contact2.familyNameLowerCase, "Same familyNameLowerCase index"); 1.133 + checkStrArray(contact1.givenName, contact2.givenName, "Same givenName index"); 1.134 + checkStrArray(contact1.givenNameLowerCase, contact2.givenNameLowerCase, "Same givenNameLowerCase index"); 1.135 + checkStrArray(contact1.name, contact2.name, "Same name index"); 1.136 + checkStrArray(contact1.tel, contact2.tel, "Same tel index"); 1.137 + checkStrArray(contact1.telLowerCase, contact2.telLowerCase, "Same telLowerCase index"); 1.138 + checkStrArray(contact1.telMatch, contact2.telMatch, "Same telMatch index"); 1.139 + } 1.140 + 1.141 + function makeFailure(reason) { 1.142 + return function() { 1.143 + ok(false, reason); 1.144 + SimpleTest.finish(); 1.145 + }; 1.146 + }; 1.147 + 1.148 + const { 'utils': Cu } = Components; 1.149 + Cu.import("resource://gre/modules/ContactDB.jsm", window); 1.150 + Cu.importGlobalProperties(["indexedDB"]); 1.151 + 1.152 + let cdb = new ContactDB(); 1.153 + cdb.init(); 1.154 + 1.155 + let CONTACT_PROPS = { 1.156 + id: "ab74671e36be41b680f8f030e7e24ea2", 1.157 + properties: { 1.158 + name: ["Magnificentest foo bar the third"], 1.159 + givenName: ["foo"], 1.160 + familyName: ["bar"], 1.161 + honorificPrefix: ["magnificentest"], 1.162 + honorificSuffix: ["the third"], 1.163 + additionalName: ["addl"], 1.164 + nickname: ["foo"], 1.165 + tel: [ 1.166 + {type: ["mobile"], value: "+12345678901", carrier: "ACME Telecommunications", pref: true}, 1.167 + {type: ["home", "custom"], value: "7932012346", pref: false}, 1.168 + ], 1.169 + email: [{type: ["work"], value: "a@b.c"}, {value: "b@c.d", pref: true}], 1.170 + adr: [ 1.171 + { 1.172 + type: ["home"], 1.173 + streetAddress: "street 1", 1.174 + locality: "locality 1", 1.175 + region: "region 1", 1.176 + postalCode: "postal code 1", 1.177 + countryName: "country 1", 1.178 + } 1.179 + ], 1.180 + impp: [{type: ["aim"], value:"im1", pref: true}, {value: "im2"}], 1.181 + org: ["org1", "org2"], 1.182 + jobTitle: ["boss", "superboss"], 1.183 + bday: new Date("1980, 12, 01"), 1.184 + note: ["bla bla bla"], 1.185 + category: ["cat1", "cat2"], 1.186 + url: [{type: ["work", "work2"], value: "www.1.com", pref: true}, {value: "www2.com"}], 1.187 + anniversary: new Date("2000, 12, 01"), 1.188 + sex: "male", 1.189 + genderIdentity: "trisexual", 1.190 + 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" 1.191 + } 1.192 + }; 1.193 + 1.194 + function deleteDatabase(then) { 1.195 + cdb.close(); 1.196 + let req = indexedDB.deleteDatabase(DB_NAME); 1.197 + req.onsuccess = then; 1.198 + req.onblocked = makeFailure("blocked"); 1.199 + req.onupgradeneeded = makeFailure("onupgradeneeded"); 1.200 + req.onerror = makeFailure("onerror"); 1.201 + } 1.202 + 1.203 + function saveContact() { 1.204 + // takes fast upgrade path 1.205 + cdb.saveContact(CONTACT_PROPS, 1.206 + function() { 1.207 + ok(true, "Saved contact successfully"); 1.208 + next(); 1.209 + } 1.210 + ); 1.211 + } 1.212 + 1.213 + function getContact(callback) { 1.214 + return function() { 1.215 + let req = indexedDB.open(STORE_NAME, DB_VERSION); 1.216 + req.onsuccess = function(event) { 1.217 + let db = event.target.result; 1.218 + let txn = db.transaction([STORE_NAME], "readonly"); 1.219 + txn.onabort = makeFailure("Failed to open transaction"); 1.220 + let r2 = txn.objectStore(STORE_NAME).get(CONTACT_PROPS.id); 1.221 + r2.onsuccess = function() { 1.222 + db.close(); 1.223 + callback(r2.result); 1.224 + }; 1.225 + r2.onerror = makeFailure("Failed to get contact"); 1.226 + }; 1.227 + }; 1.228 + } 1.229 + 1.230 + let savedContact; 1.231 + 1.232 + let Tests = [ 1.233 + saveContact, 1.234 + 1.235 + getContact(function(contact) { 1.236 + savedContact = contact; 1.237 + next(); 1.238 + }), 1.239 + 1.240 + function() { 1.241 + deleteDatabase(function() { 1.242 + info("slow upgrade"); 1.243 + cdb.useFastUpgrade = false; 1.244 + cdb.init(); 1.245 + next(); 1.246 + }); 1.247 + }, 1.248 + 1.249 + saveContact, 1.250 + 1.251 + getContact(function(contact) { 1.252 + checkDBContacts(savedContact, contact); 1.253 + next(); 1.254 + }), 1.255 + ]; 1.256 + 1.257 + function next() { 1.258 + let step = Tests.shift(); 1.259 + if (step) { 1.260 + step(); 1.261 + } else { 1.262 + info("All done"); 1.263 + SimpleTest.finish(); 1.264 + } 1.265 + } 1.266 + 1.267 + // Skip tests on Android 1.268 + if (!isAndroid) { 1.269 + SimpleTest.waitForExplicitFinish(); 1.270 + next(); 1.271 + } else { 1.272 + ok(true, "Skip test on Android"); 1.273 + } 1.274 + 1.275 + ]]> 1.276 + </script> 1.277 + 1.278 + <body xmlns="http://www.w3.org/1999/xhtml"> 1.279 + <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=889239" 1.280 + target="_blank">Mozilla Bug 889239</a> 1.281 + </body> 1.282 +</window>