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