1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/dom/contacts/tests/test_contacts_basics.html Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,784 @@ 1.4 +<!DOCTYPE html> 1.5 +<html> 1.6 +<!-- 1.7 +https://bugzilla.mozilla.org/show_bug.cgi?id=674720 1.8 +--> 1.9 +<head> 1.10 + <title>Test for Bug 674720 WebContacts</title> 1.11 + <script type="text/javascript" src="/MochiKit/MochiKit.js"></script> 1.12 + <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script> 1.13 + <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" /> 1.14 +</head> 1.15 +<body> 1.16 + 1.17 +<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=674720">Mozilla Bug 674720</a> 1.18 +<p id="display"></p> 1.19 +<div id="content" style="display: none"> 1.20 + 1.21 +</div> 1.22 +<pre id="test"> 1.23 +<script type="text/javascript;version=1.8" src="http://mochi.test:8888/tests/dom/contacts/tests/shared.js"></script> 1.24 +<script class="testbody" type="text/javascript"> 1.25 +"use strict"; 1.26 + 1.27 +var initialRev; 1.28 + 1.29 +function checkRevision(revision, msg, then) { 1.30 + var revReq = mozContacts.getRevision(); 1.31 + revReq.onsuccess = function(e) { 1.32 + is(e.target.result, initialRev+revision, msg); 1.33 + then(); 1.34 + }; 1.35 + // The revision function isn't supported on Android so treat on failure as success 1.36 + if (isAndroid) { 1.37 + revReq.onerror = function(e) { 1.38 + then(); 1.39 + }; 1.40 + } else { 1.41 + revReq.onerror = onFailure; 1.42 + } 1.43 +} 1.44 + 1.45 +var req; 1.46 + 1.47 +var steps = [ 1.48 + function() { 1.49 + req = mozContacts.getRevision(); 1.50 + req.onsuccess = function(e) { 1.51 + initialRev = e.target.result; 1.52 + next(); 1.53 + }; 1.54 + 1.55 + // Android does not support the revision function. Treat errors as success. 1.56 + if (isAndroid) { 1.57 + req.onerror = function(e) { 1.58 + initialRev = 0; 1.59 + next(); 1.60 + }; 1.61 + } else { 1.62 + req.onerror = onFailure; 1.63 + } 1.64 + }, 1.65 + function () { 1.66 + ok(true, "Deleting database"); 1.67 + checkRevision(0, "Initial revision is 0", function() { 1.68 + req = mozContacts.clear(); 1.69 + req.onsuccess = function () { 1.70 + ok(true, "Deleted the database"); 1.71 + checkCount(0, "No contacts after clear", function() { 1.72 + checkRevision(1, "Revision was incremented on clear", next); 1.73 + }); 1.74 + }; 1.75 + req.onerror = onFailure; 1.76 + }); 1.77 + }, 1.78 + function () { 1.79 + ok(true, "Retrieving all contacts"); 1.80 + req = mozContacts.find(defaultOptions); 1.81 + req.onsuccess = function () { 1.82 + is(req.result.length, 0, "Empty database."); 1.83 + checkRevision(1, "Revision was not incremented on find", next); 1.84 + }; 1.85 + req.onerror = onFailure; 1.86 + }, 1.87 + function () { 1.88 + ok(true, "Adding empty contact"); 1.89 + createResult1 = new mozContact({}); 1.90 + req = navigator.mozContacts.save(createResult1); 1.91 + req.onsuccess = function () { 1.92 + ok(createResult1.id, "The contact now has an ID."); 1.93 + sample_id1 = createResult1.id; 1.94 + checkCount(1, "1 contact after adding empty contact", function() { 1.95 + checkRevision(2, "Revision was incremented on save", next); 1.96 + }); 1.97 + }; 1.98 + req.onerror = onFailure; 1.99 + }, 1.100 + function () { 1.101 + ok(true, "Retrieving all contacts"); 1.102 + req = mozContacts.find(defaultOptions); 1.103 + req.onsuccess = function () { 1.104 + is(req.result.length, 1, "One contact."); 1.105 + findResult1 = req.result[0]; 1.106 + next(); 1.107 + }; 1.108 + req.onerror = onFailure; 1.109 + }, 1.110 + function () { 1.111 + ok(true, "Deleting empty contact"); 1.112 + req = navigator.mozContacts.remove(findResult1); 1.113 + req.onsuccess = function () { 1.114 + var req2 = mozContacts.find(defaultOptions); 1.115 + req2.onsuccess = function () { 1.116 + is(req2.result.length, 0, "Empty Database."); 1.117 + clearTemps(); 1.118 + checkRevision(3, "Revision was incremented on remove", next); 1.119 + } 1.120 + req2.onerror = onFailure; 1.121 + } 1.122 + req.onerror = onFailure; 1.123 + }, 1.124 + function () { 1.125 + ok(true, "Adding a new contact1"); 1.126 + createResult1 = new mozContact(properties1); 1.127 + 1.128 + mozContacts.oncontactchange = function(event) { 1.129 + is(event.contactID, createResult1.id, "Same contactID"); 1.130 + is(event.reason, "create", "Same reason"); 1.131 + next(); 1.132 + } 1.133 + 1.134 + req = navigator.mozContacts.save(createResult1); 1.135 + req.onsuccess = function () { 1.136 + ok(createResult1.id, "The contact now has an ID."); 1.137 + sample_id1 = createResult1.id; 1.138 + checkContacts(createResult1, properties1); 1.139 + }; 1.140 + req.onerror = onFailure; 1.141 + }, 1.142 + function () { 1.143 + ok(true, "Retrieving by substring 1"); 1.144 + var options = {filterBy: ["givenName"], 1.145 + filterOp: "startsWith", 1.146 + filterValue: properties1.givenName[1].substring(0,3)}; 1.147 + req = mozContacts.find(options); 1.148 + req.onsuccess = function () { 1.149 + is(req.result.length, 1, "Found exactly 1 contact."); 1.150 + findResult1 = req.result[0]; 1.151 + ok(findResult1.id == sample_id1, "Same ID"); 1.152 + checkContacts(createResult1, properties1); 1.153 + // Some manual testing. Testint the testfunctions 1.154 + // tel: [{type: ["work"], value: "123456", carrier: "testCarrier"} , {type: ["home", "fax"], value: "+55 (31) 9876-3456"}], 1.155 + is(findResult1.tel[0].carrier, "testCarrier", "Same Carrier"); 1.156 + is(findResult1.tel[0].type, "work", "Same type"); 1.157 + is(findResult1.tel[0].value, "123456", "Same Value"); 1.158 + is(findResult1.tel[1].type[1], "fax", "Same type"); 1.159 + is(findResult1.tel[1].value, "+55 (31) 9876-3456", "Same Value"); 1.160 + 1.161 + is(findResult1.adr[0].countryName, "country 1", "Same country"); 1.162 + 1.163 + // email: [{type: ["work"], value: "x@y.com"}] 1.164 + is(findResult1.email[0].type, "work", "Same Type"); 1.165 + is(findResult1.email[0].value, "x@y.com", "Same Value"); 1.166 + next(); 1.167 + }; 1.168 + req.onerror = onFailure; 1.169 + }, 1.170 + function () { 1.171 + ok(true, "Searching for exact email"); 1.172 + var options = {filterBy: ["email"], 1.173 + filterOp: "equals", 1.174 + filterValue: properties1.email[0].value}; 1.175 + req = mozContacts.find(options); 1.176 + req.onsuccess = function () { 1.177 + is(req.result.length, 1, "Found exactly 1 contact."); 1.178 + findResult1 = req.result[0]; 1.179 + ok(findResult1.id == sample_id1, "Same ID"); 1.180 + checkContacts(findResult1, createResult1); 1.181 + next(); 1.182 + }; 1.183 + req.onerror = onFailure; 1.184 + }, 1.185 + function () { 1.186 + ok(true, "Retrieving by substring and update"); 1.187 + mozContacts.oncontactchange = function(event) { 1.188 + is(event.contactID, findResult1.id, "Same contactID"); 1.189 + is(event.reason, "update", "Same reason"); 1.190 + } 1.191 + var options = {filterBy: ["givenName"], 1.192 + filterOp: "startsWith", 1.193 + filterValue: properties1.givenName[0].substring(0,3)}; 1.194 + req = mozContacts.find(options); 1.195 + req.onsuccess = function () { 1.196 + is(req.result.length, 1, "Found exactly 1 contact."); 1.197 + findResult1 = req.result[0]; 1.198 + findResult1.jobTitle = ["new Job"]; 1.199 + ok(findResult1.id == sample_id1, "Same ID"); 1.200 + checkContacts(createResult1, properties1); 1.201 + next(); 1.202 + }; 1.203 + req.onerror = onFailure; 1.204 + }, 1.205 + function () { 1.206 + ok(true, "Adding a new contact"); 1.207 + mozContacts.oncontactchange = function(event) { 1.208 + is(event.contactID, createResult2.id, "Same contactID"); 1.209 + is(event.reason, "create", "Same reason"); 1.210 + } 1.211 + createResult2 = new mozContact({name: ["newName"]}); 1.212 + req = navigator.mozContacts.save(createResult2); 1.213 + req.onsuccess = function () { 1.214 + ok(createResult2.id, "The contact now has an ID."); 1.215 + next(); 1.216 + }; 1.217 + req.onerror = onFailure; 1.218 + }, 1.219 + function () { 1.220 + ok(true, "Retrieving by substring 2"); 1.221 + var options = {filterBy: ["givenName"], 1.222 + filterOp: "startsWith", 1.223 + filterValue: properties1.givenName[0].substring(0,3)}; 1.224 + req = mozContacts.find(options); 1.225 + req.onsuccess = function () { 1.226 + is(req.result.length, 1, "Found exactly 1 contact."); 1.227 + findResult1 = req.result[0]; 1.228 + checkContacts(createResult1, findResult1); 1.229 + next(); 1.230 + }; 1.231 + req.onerror = onFailure; 1.232 + }, 1.233 + function() { 1.234 + ok(true, "Retrieving by name equality 1"); 1.235 + var options = {filterBy: ["name"], 1.236 + filterOp: "equals", 1.237 + filterValue: properties1.name[0]}; 1.238 + req = mozContacts.find(options); 1.239 + req.onsuccess = function () { 1.240 + is(req.result.length, 1, "Found exactly 1 contact."); 1.241 + findResult1 = req.result[0]; 1.242 + checkContacts(createResult1, findResult1); 1.243 + next(); 1.244 + }; 1.245 + req.onerror = onFailure; 1.246 + }, 1.247 + function() { 1.248 + ok(true, "Retrieving by name equality 2"); 1.249 + var options = {filterBy: ["name"], 1.250 + filterOp: "equals", 1.251 + filterValue: properties1.name[1]}; 1.252 + req = mozContacts.find(options); 1.253 + req.onsuccess = function () { 1.254 + is(req.result.length, 1, "Found exactly 1 contact."); 1.255 + findResult1 = req.result[0]; 1.256 + checkContacts(createResult1, findResult1); 1.257 + next(); 1.258 + }; 1.259 + req.onerror = onFailure; 1.260 + }, 1.261 + function() { 1.262 + ok(true, "Retrieving by name substring 1"); 1.263 + var options = {filterBy: ["name"], 1.264 + filterOp: "startsWith", 1.265 + filterValue: properties1.name[0].substring(0,3).toLowerCase()}; 1.266 + req = mozContacts.find(options); 1.267 + req.onsuccess = function () { 1.268 + is(req.result.length, 1, "Found exactly 1 contact."); 1.269 + findResult1 = req.result[0]; 1.270 + checkContacts(createResult1, findResult1); 1.271 + next(); 1.272 + }; 1.273 + req.onerror = onFailure; 1.274 + }, 1.275 + function() { 1.276 + ok(true, "Retrieving by name substring 2"); 1.277 + var options = {filterBy: ["name"], 1.278 + filterOp: "startsWith", 1.279 + filterValue: properties1.name[1].substring(0,3).toLowerCase()}; 1.280 + req = mozContacts.find(options); 1.281 + req.onsuccess = function () { 1.282 + is(req.result.length, 1, "Found exactly 1 contact."); 1.283 + findResult1 = req.result[0]; 1.284 + checkContacts(createResult1, findResult1); 1.285 + next(); 1.286 + }; 1.287 + req.onerror = onFailure; 1.288 + }, 1.289 + function () { 1.290 + ok(true, "Remove contact1"); 1.291 + mozContacts.oncontactchange = function(event) { 1.292 + is(event.contactID, createResult1.id, "Same contactID"); 1.293 + is(event.reason, "remove", "Same reason"); 1.294 + } 1.295 + req = navigator.mozContacts.remove(createResult1); 1.296 + req.onsuccess = function () { 1.297 + next(); 1.298 + }; 1.299 + req.onerror = onFailure; 1.300 + }, 1.301 + function () { 1.302 + ok(true, "Retrieving by substring 3"); 1.303 + var options = {filterBy: ["givenName"], 1.304 + filterOp: "startsWith", 1.305 + filterValue: properties1.givenName[1].substring(0,3)}; 1.306 + req = mozContacts.find(options); 1.307 + req.onsuccess = function () { 1.308 + is(req.result.length, 0, "Found no contact."); 1.309 + next(); 1.310 + }; 1.311 + req.onerror = onFailure; 1.312 + }, 1.313 + function () { 1.314 + ok(true, "Remove contact2"); 1.315 + mozContacts.oncontactchange = function(event) { 1.316 + is(event.contactID, createResult2.id, "Same contactID"); 1.317 + is(event.reason, "remove", "Same reason"); 1.318 + } 1.319 + req = navigator.mozContacts.remove(createResult2); 1.320 + req.onsuccess = function () { 1.321 + next(); 1.322 + }; 1.323 + req.onerror = onFailure; 1.324 + }, 1.325 + function () { 1.326 + ok(true, "Retrieving by substring 4"); 1.327 + var options = {filterBy: ["givenName"], 1.328 + filterOp: "startsWith", 1.329 + filterValue: properties1.givenName[1].substring(0,3)}; 1.330 + req = mozContacts.find(options); 1.331 + req.onsuccess = function () { 1.332 + is(req.result.length, 0, "Found no contact."); 1.333 + next(); 1.334 + }; 1.335 + req.onerror = onFailure; 1.336 + }, 1.337 + function () { 1.338 + ok(true, "Deleting database"); 1.339 + mozContacts.oncontactchange = function(event) { 1.340 + is(event.contactID, "undefined", "Same contactID"); 1.341 + is(event.reason, "remove", "Same reason"); 1.342 + } 1.343 + req = mozContacts.clear(); 1.344 + req.onsuccess = function () { 1.345 + ok(true, "Deleted the database"); 1.346 + next(); 1.347 + }; 1.348 + req.onerror = onFailure; 1.349 + }, 1.350 + function () { 1.351 + ok(true, "Adding a new contact with properties1"); 1.352 + createResult1 = new mozContact(properties1); 1.353 + mozContacts.oncontactchange = null; 1.354 + req = navigator.mozContacts.save(createResult1); 1.355 + req.onsuccess = function () { 1.356 + ok(createResult1.id, "The contact now has an ID."); 1.357 + sample_id1 = createResult1.id; 1.358 + checkContacts(createResult1, properties1); 1.359 + next(); 1.360 + }; 1.361 + req.onerror = onFailure; 1.362 + }, 1.363 + function () { 1.364 + ok(true, "Retrieving by substring tel1"); 1.365 + var options = {filterBy: ["tel"], 1.366 + filterOp: "contains", 1.367 + filterValue: properties1.tel[1].value.substring(2,5)}; 1.368 + req = mozContacts.find(options); 1.369 + req.onsuccess = function () { 1.370 + is(req.result.length, 1, "Found exactly 1 contact."); 1.371 + findResult1 = req.result[0]; 1.372 + ok(findResult1.id == sample_id1, "Same ID"); 1.373 + checkContacts(createResult1, properties1); 1.374 + next(); 1.375 + }; 1.376 + req.onerror = onFailure; 1.377 + }, 1.378 + function () { 1.379 + ok(true, "Retrieving by tel exact"); 1.380 + var options = {filterBy: ["tel"], 1.381 + filterOp: "equals", 1.382 + filterValue: "+55 319 8 7 6 3456"}; 1.383 + req = mozContacts.find(options); 1.384 + req.onsuccess = function () { 1.385 + is(req.result.length, 1, "Found exactly 1 contact."); 1.386 + findResult1 = req.result[0]; 1.387 + ok(findResult1.id == sample_id1, "Same ID"); 1.388 + checkContacts(createResult1, properties1); 1.389 + next(); 1.390 + }; 1.391 + req.onerror = onFailure; 1.392 + }, 1.393 + function () { 1.394 + ok(true, "Retrieving by tel exact with substring"); 1.395 + var options = {filterBy: ["tel"], 1.396 + filterOp: "equals", 1.397 + filterValue: "3456"}; 1.398 + req = mozContacts.find(options); 1.399 + req.onsuccess = function () { 1.400 + is(req.result.length, 0, "Found no contacts."); 1.401 + next(); 1.402 + }; 1.403 + req.onerror = onFailure; 1.404 + }, 1.405 + function () { 1.406 + ok(true, "Retrieving by tel exact with substring"); 1.407 + var options = {filterBy: ["tel"], 1.408 + filterOp: "equals", 1.409 + filterValue: "+55 (31)"}; 1.410 + req = mozContacts.find(options); 1.411 + req.onsuccess = function () { 1.412 + is(req.result.length, 0, "Found no contacts."); 1.413 + next(); 1.414 + }; 1.415 + req.onerror = onFailure; 1.416 + }, 1.417 + function () { 1.418 + ok(true, "Retrieving by tel match national number"); 1.419 + var options = {filterBy: ["tel"], 1.420 + filterOp: "match", 1.421 + filterValue: "3198763456"}; 1.422 + req = mozContacts.find(options); 1.423 + req.onsuccess = function () { 1.424 + is(req.result.length, 1, "Found exactly 1 contact."); 1.425 + findResult1 = req.result[0]; 1.426 + ok(findResult1.id == sample_id1, "Same ID"); 1.427 + checkContacts(createResult1, properties1); 1.428 + next(); 1.429 + }; 1.430 + req.onerror = onFailure; 1.431 + }, 1.432 + function () { 1.433 + ok(true, "Retrieving by tel match national format"); 1.434 + var options = {filterBy: ["tel"], 1.435 + filterOp: "match", 1.436 + filterValue: "0451 491934"}; 1.437 + req = mozContacts.find(options); 1.438 + req.onsuccess = function () { 1.439 + is(req.result.length, 1, "Found exactly 1 contact."); 1.440 + findResult1 = req.result[0]; 1.441 + ok(findResult1.id == sample_id1, "Same ID"); 1.442 + checkContacts(createResult1, properties1); 1.443 + next(); 1.444 + }; 1.445 + req.onerror = onFailure; 1.446 + }, 1.447 + function () { 1.448 + ok(true, "Retrieving by tel match entered number"); 1.449 + var options = {filterBy: ["tel"], 1.450 + filterOp: "match", 1.451 + filterValue: "123456"}; 1.452 + req = mozContacts.find(options); 1.453 + req.onsuccess = function () { 1.454 + is(req.result.length, 1, "Found exactly 1 contact."); 1.455 + findResult1 = req.result[0]; 1.456 + ok(findResult1.id == sample_id1, "Same ID"); 1.457 + checkContacts(createResult1, properties1); 1.458 + next(); 1.459 + }; 1.460 + req.onerror = onFailure; 1.461 + }, 1.462 + function () { 1.463 + ok(true, "Retrieving by tel match international number"); 1.464 + var options = {filterBy: ["tel"], 1.465 + filterOp: "match", 1.466 + filterValue: "+55 31 98763456"}; 1.467 + req = mozContacts.find(options); 1.468 + req.onsuccess = function () { 1.469 + is(req.result.length, 1, "Found exactly 1 contact."); 1.470 + findResult1 = req.result[0]; 1.471 + ok(findResult1.id == sample_id1, "Same ID"); 1.472 + checkContacts(createResult1, properties1); 1.473 + next(); 1.474 + }; 1.475 + req.onerror = onFailure; 1.476 + }, 1.477 + function () { 1.478 + ok(true, "Retrieving by match with field other than tel"); 1.479 + var options = {filterBy: ["givenName"], 1.480 + filterOp: "match", 1.481 + filterValue: "my friends call me 555-4040"}; 1.482 + req = mozContacts.find(options); 1.483 + req.onsuccess = onUnwantedSuccess; 1.484 + req.onerror = function() { 1.485 + ok(true, "Failed"); 1.486 + next(); 1.487 + } 1.488 + }, 1.489 + function () { 1.490 + ok(true, "Retrieving by substring tel2"); 1.491 + var options = {filterBy: ["tel"], 1.492 + filterOp: "startsWith", 1.493 + filterValue: "9876"}; 1.494 + req = mozContacts.find(options); 1.495 + req.onsuccess = function () { 1.496 + is(req.result.length, 1, "Found exactly 1 contact."); 1.497 + findResult1 = req.result[0]; 1.498 + ok(findResult1.id == sample_id1, "Same ID"); 1.499 + checkContacts(createResult1, properties1); 1.500 + next(); 1.501 + }; 1.502 + req.onerror = onFailure; 1.503 + }, 1.504 + function () { 1.505 + ok(true, "Retrieving by substring tel3"); 1.506 + var options = {filterBy: ["tel"], 1.507 + filterOp: "startsWith", 1.508 + filterValue: "98763456"}; 1.509 + req = mozContacts.find(options); 1.510 + req.onsuccess = function () { 1.511 + is(req.result.length, 1, "Found exactly 1 contact."); 1.512 + findResult1 = req.result[0]; 1.513 + ok(findResult1.id == sample_id1, "Same ID"); 1.514 + checkContacts(createResult1, properties1); 1.515 + next(); 1.516 + }; 1.517 + req.onerror = onFailure; 1.518 + }, 1.519 + function () { 1.520 + ok(true, "Retrieving by substring 5"); 1.521 + var options = {filterBy: ["givenName"], 1.522 + filterOp: "startsWith", 1.523 + filterValue: properties1.givenName[0].substring(0,3)}; 1.524 + req = mozContacts.find(options); 1.525 + req.onsuccess = function () { 1.526 + is(req.result.length, 1, "Found exactly 1 contact."); 1.527 + findResult1 = req.result[0]; 1.528 + ok(findResult1.id == sample_id1, "Same ID"); 1.529 + checkContacts(createResult1, properties1); 1.530 + next(); 1.531 + }; 1.532 + req.onerror = onFailure; 1.533 + }, 1.534 + function () { 1.535 + ok(true, "Retrieving by substring 6"); 1.536 + var options = {filterBy: ["familyName", "givenName"], 1.537 + filterOp: "startsWith", 1.538 + filterValue: properties1.givenName[0].substring(0,3)}; 1.539 + req = mozContacts.find(options); 1.540 + req.onsuccess = function () { 1.541 + is(req.result.length, 1, "Found exactly 1 contact."); 1.542 + findResult1 = req.result[0]; 1.543 + ok(findResult1.id == sample_id1, "Same ID"); 1.544 + checkContacts(createResult1, properties1); 1.545 + next(); 1.546 + }; 1.547 + req.onerror = onFailure; 1.548 + }, 1.549 + function () { 1.550 + ok(true, "Retrieving by substring3, Testing multi entry"); 1.551 + var options = {filterBy: ["givenName", "familyName"], 1.552 + filterOp: "startsWith", 1.553 + filterValue: properties1.familyName[1].substring(0,3).toLowerCase()}; 1.554 + req = mozContacts.find(options); 1.555 + req.onsuccess = function () { 1.556 + is(req.result.length, 1, "Found exactly 1 contact."); 1.557 + findResult1 = req.result[0]; 1.558 + ok(findResult1.id == sample_id1, "Same ID"); 1.559 + checkContacts(createResult1, properties1); 1.560 + next(); 1.561 + }; 1.562 + req.onerror = onFailure; 1.563 + }, 1.564 + function () { 1.565 + ok(true, "Retrieving all contacts"); 1.566 + req = mozContacts.find(defaultOptions); 1.567 + req.onsuccess = function() { 1.568 + is(req.result.length, 1, "Found exactly 1 contact."); 1.569 + findResult1 = req.result[0]; 1.570 + ok(findResult1.id == sample_id1, "Same ID"); 1.571 + checkContacts(createResult1, findResult1); 1.572 + if (!isAndroid) { 1.573 + ok(findResult1.updated, "Has updated field"); 1.574 + ok(findResult1.published, "Has published field"); 1.575 + } 1.576 + next(); 1.577 + } 1.578 + req.onerror = onFailure; 1.579 + }, 1.580 + function () { 1.581 + ok(true, "Modifying contact1"); 1.582 + if (!findResult1) { 1.583 + SpecialPowers.executeSoon(next); 1.584 + } else { 1.585 + findResult1.impp = properties1.impp = [{value:"phil impp"}]; 1.586 + req = navigator.mozContacts.save(findResult1); 1.587 + req.onsuccess = function () { 1.588 + var req2 = mozContacts.find(defaultOptions); 1.589 + req2.onsuccess = function() { 1.590 + is(req2.result.length, 1, "Found exactly 1 contact."); 1.591 + findResult2 = req2.result[0]; 1.592 + ok(findResult2.id == sample_id1, "Same ID"); 1.593 + checkContacts(findResult2, properties1); 1.594 + is(findResult2.impp.length, 1, "Found exactly 1 IMS info."); 1.595 + next(); 1.596 + }; 1.597 + req2.onerror = onFailure; 1.598 + }; 1.599 + req.onerror = onFailure; 1.600 + } 1.601 + }, 1.602 + function() { 1.603 + // Android does not support published/updated fields. Skip this. 1.604 + if (isAndroid) { 1.605 + next(); 1.606 + return; 1.607 + } 1.608 + 1.609 + ok(true, "Saving old contact, should abort!"); 1.610 + req = mozContacts.save(createResult1); 1.611 + req.onsuccess = onUnwantedSuccess; 1.612 + req.onerror = function() { ok(true, "Successfully declined updating old contact!"); next(); }; 1.613 + }, 1.614 + function () { 1.615 + ok(true, "Retrieving a specific contact by ID"); 1.616 + var options = {filterBy: ["id"], 1.617 + filterOp: "equals", 1.618 + filterValue: sample_id1}; 1.619 + req = mozContacts.find(options); 1.620 + req.onsuccess = function () { 1.621 + is(req.result.length, 1, "Found exactly 1 contact."); 1.622 + findResult1 = req.result[0]; 1.623 + ok(findResult1.id == sample_id1, "Same ID"); 1.624 + checkContacts(findResult1, properties1); 1.625 + next(); 1.626 + }; 1.627 + req.onerror = onFailure; 1.628 + }, 1.629 + function () { 1.630 + ok(true, "Retrieving a specific contact by givenName"); 1.631 + var options = {filterBy: ["givenName"], 1.632 + filterOp: "equals", 1.633 + filterValue: properties1.givenName[0]}; 1.634 + req = mozContacts.find(options); 1.635 + req.onsuccess = function () { 1.636 + is(req.result.length, 1, "Found exactly 1 contact."); 1.637 + findResult1 = req.result[0]; 1.638 + ok(findResult1.id == sample_id1, "Same ID"); 1.639 + checkContacts(findResult1, properties1); 1.640 + next(); 1.641 + } 1.642 + req.onerror = onFailure; 1.643 + }, 1.644 + function () { 1.645 + ok(true, "Modifying contact2"); 1.646 + if (!findResult1) { 1.647 + SpecialPowers.executeSoon(next); 1.648 + } else { 1.649 + findResult1.impp = properties1.impp = [{value: "phil impp"}]; 1.650 + req = mozContacts.save(findResult1); 1.651 + req.onsuccess = function () { 1.652 + var req2 = mozContacts.find(defaultOptions); 1.653 + req2.onsuccess = function () { 1.654 + is(req2.result.length, 1, "Found exactly 1 contact."); 1.655 + findResult1 = req2.result[0]; 1.656 + ok(findResult1.id == sample_id1, "Same ID"); 1.657 + checkContacts(findResult1, properties1); 1.658 + is(findResult1.impp.length, 1, "Found exactly 1 IMS info."); 1.659 + next(); 1.660 + } 1.661 + req2.onerror = onFailure; 1.662 + }; 1.663 + req.onerror = onFailure; 1.664 + } 1.665 + }, 1.666 + function () { 1.667 + ok(true, "Searching contacts by query"); 1.668 + var options = {filterBy: ["givenName", "email"], 1.669 + filterOp: "startsWith", 1.670 + filterValue: properties1.givenName[0].substring(0,4)}; 1.671 + req = mozContacts.find(options); 1.672 + req.onsuccess = function () { 1.673 + is(req.result.length, 1, "Found exactly 1 contact."); 1.674 + findResult1 = req.result[0]; 1.675 + ok(findResult1.id == sample_id1, "Same ID"); 1.676 + checkContacts(findResult1, properties1); 1.677 + next(); 1.678 + }; 1.679 + req.onerror = onFailure; 1.680 + }, 1.681 + function () { 1.682 + ok(true, "Searching contacts by query"); 1.683 + var options = {filterBy: ["givenName", "email"], 1.684 + filterOp: "startsWith", 1.685 + filterValue: properties1.givenName[0]}; 1.686 + req = mozContacts.find(options); 1.687 + req.onsuccess = function () { 1.688 + is(req.result.length, 1, "Found exactly 1 contact."); 1.689 + findResult1 = req.result[0]; 1.690 + ok(findResult1.id == sample_id1, "Same ID"); 1.691 + checkContacts(findResult1, properties1); 1.692 + next(); 1.693 + }; 1.694 + req.onerror = onFailure; 1.695 + }, 1.696 + function () { 1.697 + ok(true, "Searching contacts with multiple indices"); 1.698 + var options = {filterBy: ["email", "givenName"], 1.699 + filterOp: "equals", 1.700 + filterValue: properties1.givenName[1]}; 1.701 + req = mozContacts.find(options); 1.702 + req.onsuccess = function () { 1.703 + is(req.result.length, 1, "Found exactly 1 contact."); 1.704 + findResult1 = req.result[0]; 1.705 + ok(findResult1.id == sample_id1, "Same ID"); 1.706 + checkContacts(findResult1, properties1); 1.707 + next(); 1.708 + }; 1.709 + req.onerror = onFailure; 1.710 + }, 1.711 + function () { 1.712 + ok(true, "Modifying contact3"); 1.713 + if (!findResult1) { 1.714 + SpecialPowers.executeSoon(next); 1.715 + } else { 1.716 + findResult1.email = [{value: properties1.nickname}]; 1.717 + findResult1.nickname = ["TEST"]; 1.718 + var newContact = new mozContact(findResult1); 1.719 + req = mozContacts.save(newContact); 1.720 + req.onsuccess = function () { 1.721 + var options = {filterBy: ["email", "givenName"], 1.722 + filterOp: "startsWith", 1.723 + filterValue: properties1.givenName[0]}; 1.724 + // One contact has it in nickname and the other in email 1.725 + var req2 = mozContacts.find(options); 1.726 + req2.onsuccess = function () { 1.727 + is(req2.result.length, 2, "Found exactly 2 contacts."); 1.728 + ok(req2.result[0].id != req2.result[1].id, "Different ID"); 1.729 + next(); 1.730 + } 1.731 + req2.onerror = onFailure; 1.732 + }; 1.733 + req.onerror = onFailure; 1.734 + } 1.735 + }, 1.736 + function () { 1.737 + ok(true, "Deleting contact" + findResult1); 1.738 + req = mozContacts.remove(findResult1); 1.739 + req.onsuccess = function () { 1.740 + var req2 = mozContacts.find(defaultOptions); 1.741 + req2.onsuccess = function () { 1.742 + is(req2.result.length, 1, "One contact left."); 1.743 + findResult1 = req2.result[0]; 1.744 + next(); 1.745 + } 1.746 + req2.onerror = onFailure; 1.747 + } 1.748 + req.onerror = onFailure; 1.749 + }, 1.750 + function () { 1.751 + ok(true, "Deleting database"); 1.752 + req = mozContacts.remove(findResult1); 1.753 + req.onsuccess = function () { 1.754 + clearTemps(); 1.755 + next(); 1.756 + }; 1.757 + req.onerror = onFailure; 1.758 + }, 1.759 + function() { 1.760 + ok(true, "Test JSON.stringify output for mozContact objects"); 1.761 + var json = JSON.parse(JSON.stringify(new mozContact(properties1))); 1.762 + checkContacts(json, properties1); 1.763 + next(); 1.764 + }, 1.765 + function() { 1.766 + ok(true, "Test slice"); 1.767 + var c = new mozContact(); 1.768 + c.email = [{ type: ["foo"], value: "bar@baz" }] 1.769 + var arr = c.email; 1.770 + is(arr[0].value, "bar@baz", "Should have the right value"); 1.771 + arr = arr.slice(); 1.772 + is(arr[0].value, "bar@baz", "Should have the right value after slicing"); 1.773 + next(); 1.774 + }, 1.775 + function () { 1.776 + ok(true, "all done!\n"); 1.777 + clearTemps(); 1.778 + 1.779 + SimpleTest.finish(); 1.780 + } 1.781 +]; 1.782 + 1.783 +start_tests(); 1.784 +</script> 1.785 +</pre> 1.786 +</body> 1.787 +</html>