1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/dom/contacts/tests/test_contacts_basics2.html Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,1149 @@ 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 req; 1.28 + 1.29 +var steps = [ 1.30 + function () { 1.31 + ok(true, "Adding a new contact"); 1.32 + createResult1 = new mozContact(properties1); 1.33 + req = mozContacts.save(createResult1) 1.34 + req.onsuccess = function () { 1.35 + ok(createResult1.id, "The contact now has an ID."); 1.36 + sample_id1 = createResult1.id; 1.37 + next(); 1.38 + } 1.39 + req.onerror = onFailure; 1.40 + }, 1.41 + function () { 1.42 + ok(true, "Adding a new contact2"); 1.43 + createResult2 = new mozContact(properties2); 1.44 + req = mozContacts.save(createResult2); 1.45 + req.onsuccess = function () { 1.46 + ok(createResult2.id, "The contact now has an ID."); 1.47 + sample_id2 = createResult2.id; 1.48 + next(); 1.49 + }; 1.50 + req.onerror = onFailure; 1.51 + }, 1.52 + function () { 1.53 + ok(true, "Retrieving all contacts"); 1.54 + req = mozContacts.find({sortBy: "familyName"}); 1.55 + req.onsuccess = function () { 1.56 + is(req.result.length, 2, "Found exactly 2 contact."); 1.57 + checkContacts(req.result[1], properties1); 1.58 + next(); 1.59 + } 1.60 + req.onerror = onFailure; 1.61 + }, 1.62 + function () { 1.63 + console.log("Searching contacts by query1"); 1.64 + var options = {filterBy: ["givenName", "email"], 1.65 + filterOp: "startsWith", 1.66 + filterValue: properties1.givenName[0].substring(0, 4)} 1.67 + req = mozContacts.find(options) 1.68 + req.onsuccess = function () { 1.69 + is(req.result.length, 1, "Found exactly 1 contact."); 1.70 + findResult1 = req.result[0]; 1.71 + ok(findResult1.id == sample_id1, "Same ID"); 1.72 + checkContacts(findResult1, createResult1); 1.73 + next(); 1.74 + } 1.75 + req.onerror = onFailure; 1.76 + }, 1.77 + function () { 1.78 + ok(true, "Searching contacts by query2"); 1.79 + var options = {filterBy: ["givenName", "email"], 1.80 + filterOp: "startsWith", 1.81 + filterValue: properties2.givenName[0].substring(0, 4)}; 1.82 + req = mozContacts.find(options); 1.83 + req.onsuccess = function () { 1.84 + is(req.result.length, 1, "Found exactly 1 contact."); 1.85 + findResult1 = req.result[0]; 1.86 + is(findResult1.adr.length, 2, "Adr length 2"); 1.87 + checkContacts(findResult1, createResult2); 1.88 + next(); 1.89 + } 1.90 + req.onerror = onFailure; 1.91 + }, 1.92 + function () { 1.93 + ok(true, "Searching contacts by tel"); 1.94 + var options = {filterBy: ["tel"], 1.95 + filterOp: "contains", 1.96 + filterValue: properties2.tel[0].value.substring(3, 7)}; 1.97 + req = mozContacts.find(options); 1.98 + req.onsuccess = function () { 1.99 + is(req.result.length, 1, "Found exactly 1 contact."); 1.100 + findResult1 = req.result[0]; 1.101 + ok(findResult1.id == sample_id2, "Same ID"); 1.102 + checkContacts(findResult1, createResult2); 1.103 + next(); 1.104 + } 1.105 + req.onerror = onFailure; 1.106 + }, 1.107 + function () { 1.108 + ok(true, "Searching contacts by email"); 1.109 + var options = {filterBy: ["email"], 1.110 + filterOp: "startsWith", 1.111 + filterValue: properties2.email[0].value.substring(0, 4)}; 1.112 + req = mozContacts.find(options); 1.113 + req.onsuccess = function () { 1.114 + is(req.result.length, 1, "Found exactly 1 contact."); 1.115 + findResult1 = req.result[0]; 1.116 + ok(findResult1.id == sample_id2, "Same ID"); 1.117 + checkContacts(findResult1, createResult2); 1.118 + next(); 1.119 + } 1.120 + req.onerror = onFailure; 1.121 + }, 1.122 + function () { 1.123 + ok(true, "Deleting database"); 1.124 + req = mozContacts.clear(); 1.125 + req.onsuccess = function () { 1.126 + next(); 1.127 + } 1.128 + req.onerror = onFailure; 1.129 + }, 1.130 + function () { 1.131 + ok(true, "Adding 20 contacts"); 1.132 + for (var i=0; i<19; i++) { 1.133 + createResult1 = new mozContact(properties1); 1.134 + req = mozContacts.save(createResult1); 1.135 + req.onsuccess = function () { 1.136 + ok(createResult1.id, "The contact now has an ID."); 1.137 + }; 1.138 + req.onerror = onFailure; 1.139 + }; 1.140 + createResult1 = new mozContact(properties1); 1.141 + req = mozContacts.save(createResult1); 1.142 + req.onsuccess = function () { 1.143 + ok(createResult1.id, "The contact now has an ID."); 1.144 + checkStrArray(createResult1.name, properties1.name, "Same Name"); 1.145 + checkCount(20, "20 contacts in DB", next); 1.146 + }; 1.147 + req.onerror = onFailure; 1.148 + }, 1.149 + function () { 1.150 + ok(true, "Retrieving all contacts"); 1.151 + req = mozContacts.find(defaultOptions); 1.152 + req.onsuccess = function () { 1.153 + is(req.result.length, 20, "20 Entries."); 1.154 + next(); 1.155 + } 1.156 + req.onerror = onFailure; 1.157 + }, 1.158 + function () { 1.159 + ok(true, "Retrieving all contacts with limit 10"); 1.160 + var options = { filterLimit: 10 }; 1.161 + req = mozContacts.find(options); 1.162 + req.onsuccess = function () { 1.163 + is(req.result.length, 10, "10 Entries."); 1.164 + next(); 1.165 + } 1.166 + req.onerror = onFailure; 1.167 + }, 1.168 + function () { 1.169 + ok(true, "Retrieving all contacts with limit 10 and sorted"); 1.170 + var options = { filterLimit: 10, 1.171 + sortBy: 'FamilyName', 1.172 + sortOrder: 'descending' }; 1.173 + req = mozContacts.find(options); 1.174 + req.onsuccess = function () { 1.175 + is(req.result.length, 10, "10 Entries."); 1.176 + next(); 1.177 + } 1.178 + req.onerror = onFailure; 1.179 + }, 1.180 + function () { 1.181 + ok(true, "Retrieving all contacts2"); 1.182 + var options = {filterBy: ["givenName"], 1.183 + filterOp: "startsWith", 1.184 + filterValue: properties1.givenName[0].substring(0, 4)}; 1.185 + req = mozContacts.find(options); 1.186 + req.onsuccess = function () { 1.187 + is(req.result.length, 20, "20 Entries."); 1.188 + checkContacts(createResult1, req.result[19]); 1.189 + next(); 1.190 + } 1.191 + req.onerror = onFailure; 1.192 + }, 1.193 + function () { 1.194 + ok(true, "Retrieving all contacts3"); 1.195 + var options = {filterBy: ["givenName", "tel", "email"], 1.196 + filterOp: "startsWith", 1.197 + filterValue: properties1.givenName[0].substring(0, 4)}; 1.198 + req = mozContacts.find(options); 1.199 + req.onsuccess = function () { 1.200 + is(req.result.length, 20, "20 Entries."); 1.201 + checkContacts(createResult1, req.result[10]); 1.202 + next(); 1.203 + } 1.204 + req.onerror = onFailure; 1.205 + }, 1.206 + function () { 1.207 + ok(true, "Deleting database"); 1.208 + req = mozContacts.clear(); 1.209 + req.onsuccess = function () { 1.210 + next(); 1.211 + } 1.212 + req.onerror = onFailure; 1.213 + }, 1.214 + function () { 1.215 + ok(true, "Testing clone contact"); 1.216 + createResult1 = new mozContact(properties1); 1.217 + req = mozContacts.save(createResult1); 1.218 + req.onsuccess = function () { 1.219 + ok(createResult1.id, "The contact now has an ID."); 1.220 + checkStrArray(createResult1.name, properties1.name, "Same Name"); 1.221 + next(); 1.222 + } 1.223 + req.onerror = onFailure; 1.224 + }, 1.225 + function() { 1.226 + ok(true, "Testing clone contact2"); 1.227 + var cloned = new mozContact(createResult1); 1.228 + ok(cloned.id != createResult1.id, "Cloned contact has new ID"); 1.229 + cloned.email = [{value: "new email!"}]; 1.230 + cloned.givenName = ["Tom"]; 1.231 + req = mozContacts.save(cloned); 1.232 + req.onsuccess = function () { 1.233 + ok(cloned.id, "The contact now has an ID."); 1.234 + is(cloned.email[0].value, "new email!", "Same Email"); 1.235 + isnot(createResult1.email[0].value, cloned.email[0].value, "Clone has different email"); 1.236 + is(cloned.givenName, "Tom", "New Name"); 1.237 + next(); 1.238 + } 1.239 + req.onerror = onFailure; 1.240 + }, 1.241 + function () { 1.242 + ok(true, "Retrieving all contacts"); 1.243 + var options = {filterBy: ["givenName"], 1.244 + filterOp: "startsWith", 1.245 + filterValue: properties2.givenName[0].substring(0, 4)}; 1.246 + req = mozContacts.find(defaultOptions); 1.247 + req.onsuccess = function () { 1.248 + is(req.result.length, 2, "2 Entries."); 1.249 + next(); 1.250 + } 1.251 + req.onerror = onFailure; 1.252 + }, 1.253 + function () { 1.254 + ok(true, "Search with redundant fields should only return 1 contact"); 1.255 + createResult1 = new mozContact({name: ["XXX"], 1.256 + givenName: ["XXX"], 1.257 + email: [{value: "XXX"}], 1.258 + tel: [{value: "XXX"}] 1.259 + }); 1.260 + req = mozContacts.save(createResult1); 1.261 + req.onsuccess = function() { 1.262 + var options = {filterBy: ["givenName", "familyName"], 1.263 + filterOp: "equals", 1.264 + filterValue: "XXX"}; 1.265 + var req2 = mozContacts.find(options); 1.266 + req2.onsuccess = function() { 1.267 + is(req2.result.length, 1, "1 Entry"); 1.268 + next(); 1.269 + } 1.270 + req2.onerror = onFailure; 1.271 + } 1.272 + req.onerror = onFailure; 1.273 + }, 1.274 + function () { 1.275 + ok(true, "Deleting database"); 1.276 + req = mozContacts.clear() 1.277 + req.onsuccess = function () { 1.278 + ok(true, "Deleted the database"); 1.279 + next(); 1.280 + } 1.281 + req.onerror = onFailure; 1.282 + }, 1.283 + function () { 1.284 + ok(true, "Test sorting"); 1.285 + createResult1 = new mozContact(c3); 1.286 + req = navigator.mozContacts.save(createResult1); 1.287 + req.onsuccess = function () { 1.288 + ok(createResult1.id, "The contact now has an ID."); 1.289 + checkContacts(c3, createResult1); 1.290 + next(); 1.291 + }; 1.292 + req.onerror = onFailure; 1.293 + }, 1.294 + function () { 1.295 + ok(true, "Test sorting"); 1.296 + createResult1 = new mozContact(c2); 1.297 + req = navigator.mozContacts.save(createResult1); 1.298 + req.onsuccess = function () { 1.299 + ok(createResult1.id, "The contact now has an ID."); 1.300 + checkContacts(c2, createResult1); 1.301 + next(); 1.302 + }; 1.303 + req.onerror = onFailure; 1.304 + }, 1.305 + function () { 1.306 + ok(true, "Test sorting"); 1.307 + createResult1 = new mozContact(c4); 1.308 + req = navigator.mozContacts.save(createResult1); 1.309 + req.onsuccess = function () { 1.310 + ok(createResult1.id, "The contact now has an ID."); 1.311 + checkContacts(c4, createResult1); 1.312 + next(); 1.313 + }; 1.314 + req.onerror = onFailure; 1.315 + }, 1.316 + function () { 1.317 + ok(true, "Test sorting"); 1.318 + createResult1 = new mozContact(c1); 1.319 + req = navigator.mozContacts.save(createResult1); 1.320 + req.onsuccess = function () { 1.321 + ok(createResult1.id, "The contact now has an ID."); 1.322 + checkContacts(c1, createResult1); 1.323 + next(); 1.324 + }; 1.325 + req.onerror = onFailure; 1.326 + }, 1.327 + function () { 1.328 + ok(true, "Test sorting"); 1.329 + var options = {sortBy: "familyName", 1.330 + sortOrder: "ascending"}; 1.331 + req = navigator.mozContacts.find(options); 1.332 + req.onsuccess = function () { 1.333 + is(req.result.length, 4, "4 results"); 1.334 + checkContacts(req.result[0], c1); 1.335 + checkContacts(req.result[1], c2); 1.336 + checkContacts(req.result[2], c3); 1.337 + checkContacts(req.result[3], c4); 1.338 + next(); 1.339 + }; 1.340 + req.onerror = onFailure; 1.341 + }, 1.342 + function () { 1.343 + ok(true, "Test sorting"); 1.344 + var options = {sortBy: "familyName", 1.345 + sortOrder: "descending"}; 1.346 + req = navigator.mozContacts.find(options); 1.347 + req.onsuccess = function () { 1.348 + is(req.result.length, 4, "4 results"); 1.349 + checkContacts(req.result[0], c4); 1.350 + checkContacts(req.result[1], c3); 1.351 + checkContacts(req.result[2], c2); 1.352 + checkContacts(req.result[3], c1); 1.353 + next(); 1.354 + }; 1.355 + req.onerror = onFailure; 1.356 + }, 1.357 + function () { 1.358 + ok(true, "Test sorting"); 1.359 + createResult1 = new mozContact(c5); 1.360 + req = navigator.mozContacts.save(createResult1); 1.361 + req.onsuccess = function () { 1.362 + ok(createResult1.id, "The contact now has an ID."); 1.363 + checkContacts(c5, createResult1); 1.364 + next(); 1.365 + }; 1.366 + req.onerror = onFailure; 1.367 + }, 1.368 + function () { 1.369 + ok(true, "Test sorting with empty string"); 1.370 + var options = {sortBy: "familyName", 1.371 + sortOrder: "ascending"}; 1.372 + req = navigator.mozContacts.find(options); 1.373 + req.onsuccess = function () { 1.374 + is(req.result.length, 5, "5 results"); 1.375 + checkContacts(req.result[0], c5); 1.376 + checkContacts(req.result[1], c1); 1.377 + checkContacts(req.result[2], c2); 1.378 + checkContacts(req.result[3], c3); 1.379 + checkContacts(req.result[4], c4); 1.380 + next(); 1.381 + }; 1.382 + req.onerror = onFailure; 1.383 + }, 1.384 + function () { 1.385 + ok(true, "Don't allow to add custom fields"); 1.386 + createResult1 = new mozContact({givenName: ["customTest"], yyy: "XXX"}); 1.387 + req = mozContacts.save(createResult1); 1.388 + req.onsuccess = function() { 1.389 + var options = {filterBy: ["givenName"], 1.390 + filterOp: "equals", 1.391 + filterValue: "customTest"}; 1.392 + var req2 = mozContacts.find(options); 1.393 + req2.onsuccess = function() { 1.394 + is(req2.result.length, 1, "1 Entry"); 1.395 + checkStrArray(req2.result[0].givenName, ["customTest"], "same name"); 1.396 + ok(req2.result.yyy === undefined, "custom property undefined"); 1.397 + next(); 1.398 + } 1.399 + req2.onerror = onFailure; 1.400 + } 1.401 + req.onerror = onFailure; 1.402 + }, 1.403 + function () { 1.404 + ok(true, "Deleting database"); 1.405 + req = mozContacts.clear() 1.406 + req.onsuccess = function () { 1.407 + ok(true, "Deleted the database"); 1.408 + next(); 1.409 + } 1.410 + req.onerror = onFailure; 1.411 + }, 1.412 + function () { 1.413 + ok(true, "Test sorting"); 1.414 + createResult1 = new mozContact(c7); 1.415 + req = navigator.mozContacts.save(createResult1); 1.416 + req.onsuccess = function () { 1.417 + ok(createResult1.id, "The contact now has an ID."); 1.418 + checkContacts(c7, createResult1); 1.419 + next(); 1.420 + }; 1.421 + req.onerror = onFailure; 1.422 + }, 1.423 + function () { 1.424 + ok(true, "Test sorting"); 1.425 + createResult1 = new mozContact(c6); 1.426 + req = navigator.mozContacts.save(createResult1); 1.427 + req.onsuccess = function () { 1.428 + ok(createResult1.id, "The contact now has an ID."); 1.429 + checkContacts(c6, createResult1); 1.430 + next(); 1.431 + }; 1.432 + req.onerror = onFailure; 1.433 + }, 1.434 + function () { 1.435 + ok(true, "Test sorting"); 1.436 + createResult1 = new mozContact(c8); 1.437 + req = navigator.mozContacts.save(createResult1); 1.438 + req.onsuccess = function () { 1.439 + ok(createResult1.id, "The contact now has an ID."); 1.440 + checkContacts(c8, createResult1); 1.441 + next(); 1.442 + }; 1.443 + req.onerror = onFailure; 1.444 + }, 1.445 + function () { 1.446 + // Android does not support published/updated fields. Skip this. 1.447 + if (isAndroid) { 1.448 + next(); 1.449 + return; 1.450 + } 1.451 + 1.452 + ok(true, "Test sorting with published"); 1.453 + var options = {sortBy: "familyName", 1.454 + sortOrder: "descending"}; 1.455 + req = navigator.mozContacts.find(options); 1.456 + req.onsuccess = function () { 1.457 + is(req.result.length, 3, "3 results"); 1.458 + ok(req.result[0].published < req.result[1].published, "Right sorting order"); 1.459 + ok(req.result[1].published < req.result[2].published, "Right sorting order"); 1.460 + next(); 1.461 + }; 1.462 + req.onerror = onFailure; 1.463 + }, 1.464 + function () { 1.465 + ok(true, "Deleting database"); 1.466 + req = mozContacts.clear(); 1.467 + req.onsuccess = function () { 1.468 + ok(true, "Deleted the database"); 1.469 + next(); 1.470 + } 1.471 + req.onerror = onFailure; 1.472 + }, 1.473 + function () { 1.474 + ok(true, "Adding a new contact with properties2"); 1.475 + createResult2 = new mozContact(properties2); 1.476 + req = mozContacts.save(createResult2); 1.477 + req.onsuccess = function () { 1.478 + ok(createResult2.id, "The contact now has an ID."); 1.479 + sample_id2 = createResult2.id; 1.480 + next(); 1.481 + }; 1.482 + req.onerror = onFailure; 1.483 + }, 1.484 + function () { 1.485 + ok(true, "Test category search with startsWith"); 1.486 + var options = {filterBy: ["category"], 1.487 + filterOp: "startsWith", 1.488 + filterValue: properties2.category[0]}; 1.489 + req = mozContacts.find(options); 1.490 + req.onsuccess = function () { 1.491 + is(req.result.length, 1, "1 Entry."); 1.492 + checkContacts(req.result[0], createResult2); 1.493 + next(); 1.494 + } 1.495 + req.onerror = onFailure; 1.496 + }, 1.497 + function () { 1.498 + ok(true, "Test category search with equals"); 1.499 + var options = {filterBy: ["category"], 1.500 + filterOp: "equals", 1.501 + filterValue: properties2.category[0]}; 1.502 + req = mozContacts.find(options); 1.503 + req.onsuccess = function () { 1.504 + is(req.result.length, 1, "1 Entry."); 1.505 + checkContacts(req.result[0], createResult2); 1.506 + next(); 1.507 + } 1.508 + req.onerror = onFailure; 1.509 + }, 1.510 + function () { 1.511 + ok(true, "Deleting database"); 1.512 + req = mozContacts.clear() 1.513 + req.onsuccess = function () { 1.514 + ok(true, "Deleted the database"); 1.515 + next(); 1.516 + } 1.517 + req.onerror = onFailure; 1.518 + }, 1.519 + function () { 1.520 + ok(true, "Adding contact for category search"); 1.521 + createResult1 = new mozContact({name: ["5"], givenName: ["5"]}); 1.522 + req = navigator.mozContacts.save(createResult1); 1.523 + req.onsuccess = function () { 1.524 + ok(createResult1.id, "The contact now has an ID."); 1.525 + sample_id1 = createResult1.id; 1.526 + next(); 1.527 + }; 1.528 + req.onerror = onFailure; 1.529 + }, 1.530 + function () { 1.531 + ok(true, "Test category search with equals"); 1.532 + var options = {filterBy: ["givenName"], 1.533 + filterOp: "startsWith", 1.534 + filterValue: "5"}; 1.535 + req = mozContacts.find(options); 1.536 + req.onsuccess = function () { 1.537 + is(req.result.length, 1, "1 Entry."); 1.538 + checkContacts(req.result[0], createResult1); 1.539 + next(); 1.540 + } 1.541 + req.onerror = onFailure; 1.542 + }, 1.543 + function () { 1.544 + ok(true, "Deleting database"); 1.545 + req = mozContacts.clear() 1.546 + req.onsuccess = function () { 1.547 + ok(true, "Deleted the database"); 1.548 + next(); 1.549 + } 1.550 + req.onerror = onFailure; 1.551 + }, 1.552 + function () { 1.553 + ok(true, "Adding contact with invalid data"); 1.554 + var obj = { 1.555 + honorificPrefix: [], 1.556 + honorificSuffix: [{foo: "bar"}], 1.557 + sex: 17, 1.558 + genderIdentity: 18, 1.559 + email: [{type: ["foo"], value: "bar"}] 1.560 + }; 1.561 + obj.honorificPrefix.__defineGetter__('0',(function() { 1.562 + var c = 0; 1.563 + return function() { 1.564 + if (c == 0) { 1.565 + c++; 1.566 + return "string"; 1.567 + } else { 1.568 + return {foo:"bar"}; 1.569 + } 1.570 + } 1.571 + })()); 1.572 + createResult1 = new mozContact(obj); 1.573 + createResult1.email.push({aeiou: "abcde"}); 1.574 + req = mozContacts.save(createResult1); 1.575 + req.onsuccess = function () { 1.576 + checkContacts(createResult1, { 1.577 + honorificPrefix: ["string"], 1.578 + honorificSuffix: ["[object Object]"], 1.579 + sex: "17", 1.580 + genderIdentity: "18", 1.581 + email: [{type: ["foo"], value: "bar"}, {}] 1.582 + }); 1.583 + next(); 1.584 + }; 1.585 + }, 1.586 + function () { 1.587 + ok(true, "Adding contact with no number but carrier"); 1.588 + createResult1 = new mozContact({ tel: [{type: ["home"], carrier: "myCarrier"} ] }); 1.589 + req = navigator.mozContacts.save(createResult1); 1.590 + req.onsuccess = function () { 1.591 + ok(createResult1.id, "The contact now has an ID."); 1.592 + next(); 1.593 + }; 1.594 + req.onerror = onFailure; 1.595 + }, 1.596 + function () { 1.597 + ok(true, "Adding contact with email but no value"); 1.598 + createResult1 = new mozContact({ email: [{type: ["home"]}] }); 1.599 + req = navigator.mozContacts.save(createResult1); 1.600 + req.onsuccess = function () { 1.601 + ok(createResult1.id, "The contact now has an ID."); 1.602 + next(); 1.603 + }; 1.604 + req.onerror = onFailure; 1.605 + }, 1.606 + function () { 1.607 + ok(true, "Testing numbersOnly search 1"); 1.608 + createResult1 = new mozContact({ name: ["aaaaaaaaa"], givenName: ["aaaaaaaaa"], tel: [{ value: "1234567890"}]}); 1.609 + req = navigator.mozContacts.save(createResult1); 1.610 + req.onsuccess = function () { 1.611 + ok(createResult1.id, "The contact now has an ID."); 1.612 + next(); 1.613 + }; 1.614 + req.onerror = onFailure; 1.615 + }, 1.616 + function () { 1.617 + ok(true, "Test numbersOnly search 2"); 1.618 + var options = {filterBy: ["givenName", "tel"], 1.619 + filterOp: "contains", 1.620 + filterValue: "a"}; 1.621 + req = mozContacts.find(options); 1.622 + req.onsuccess = function () { 1.623 + ok(req.result.length == 1, "1 Entry."); 1.624 + checkContacts(req.result[0], createResult1); 1.625 + next(); 1.626 + } 1.627 + req.onerror = onFailure; 1.628 + }, 1.629 + function () { 1.630 + ok(true, "Test numbersOnly search 3"); 1.631 + var options = {filterBy: ["givenName", "tel"], 1.632 + filterOp: "contains", 1.633 + filterValue: "b"}; 1.634 + req = mozContacts.find(options); 1.635 + req.onsuccess = function () { 1.636 + ok(req.result.length == 0, "0 Entries."); 1.637 + next(); 1.638 + } 1.639 + req.onerror = onFailure; 1.640 + }, 1.641 + function () { 1.642 + ok(true, "Test numbersOnly search 4"); 1.643 + var options = {filterBy: ["givenName", "tel"], 1.644 + filterOp: "contains", 1.645 + filterValue: "1a"}; 1.646 + req = mozContacts.find(options); 1.647 + req.onsuccess = function () { 1.648 + ok(req.result.length == 0, "0 Entries."); 1.649 + next(); 1.650 + } 1.651 + req.onerror = onFailure; 1.652 + }, 1.653 + function () { 1.654 + ok(true, "Test numbersOnly search 5"); 1.655 + var options = {filterBy: ["givenName", "tel"], 1.656 + filterOp: "contains", 1.657 + filterValue: "1(23)"}; 1.658 + req = mozContacts.find(options); 1.659 + req.onsuccess = function () { 1.660 + ok(req.result.length == 1, "1 Entry."); 1.661 + next(); 1.662 + } 1.663 + req.onerror = onFailure; 1.664 + }, 1.665 + function () { 1.666 + ok(true, "Test numbersOnly search 6"); 1.667 + var options = {filterBy: ["givenName", "tel"], 1.668 + filterOp: "contains", 1.669 + filterValue: "1(23)a"}; 1.670 + req = mozContacts.find(options); 1.671 + req.onsuccess = function () { 1.672 + ok(req.result.length == 0, "0 Entries."); 1.673 + next(); 1.674 + } 1.675 + req.onerror = onFailure; 1.676 + }, 1.677 + function () { 1.678 + ok(true, "Deleting database"); 1.679 + req = mozContacts.clear() 1.680 + req.onsuccess = function () { 1.681 + ok(true, "Deleted the database"); 1.682 + next(); 1.683 + } 1.684 + req.onerror = onFailure; 1.685 + }, 1.686 + function() { 1.687 + ok(true, "Test that after setting array properties to scalar values the property os not a non-array") 1.688 + const FIELDS = ["email","url","adr","tel","impp"]; 1.689 + createResult1 = new mozContact(); 1.690 + for (var prop of FIELDS) { 1.691 + try { 1.692 + createResult1[prop] = {type: ["foo"]}; 1.693 + } catch (e) {} 1.694 + ok(createResult1[prop] === null || 1.695 + Array.isArray(createResult1[prop]), prop + " is array"); 1.696 + } 1.697 + next(); 1.698 + }, 1.699 + function() { 1.700 + ok(true, "Undefined properties of fields should be treated correctly"); 1.701 + var c = new mozContact({ 1.702 + adr: [{streetAddress: undefined}], 1.703 + email: [{value: undefined}], 1.704 + url: [{value: undefined}], 1.705 + impp: [{value: undefined}], 1.706 + tel: [{value: undefined}], 1.707 + }); 1.708 + ise(c.adr[0].streetAddress, undefined, "adr.streetAddress is undefined"); 1.709 + ise(c.adr[0].locality, undefined, "adr.locality is undefined"); 1.710 + ise(c.adr[0].pref, undefined, "adr.pref is undefined"); 1.711 + ise(c.email[0].value, undefined, "email.value is undefined"); 1.712 + ise(c.url[0].value, undefined, "url.value is undefined"); 1.713 + ise(c.impp[0].value, undefined, "impp.value is undefined"); 1.714 + ise(c.tel[0].value, undefined, "tel.value is undefined"); 1.715 + next(); 1.716 + }, 1.717 + function() { 1.718 + ok(true, "Setting array properties to an empty array should work"); 1.719 + var c = new mozContact(); 1.720 + function testArrayProp(prop) { 1.721 + is(c[prop], null, "property is initially null"); 1.722 + c[prop] = []; 1.723 + ok(Array.isArray(c[prop]), "property is an array after setting"); 1.724 + is(c[prop].length, 0, "property has length 0 after setting"); 1.725 + } 1.726 + testArrayProp("email"); 1.727 + testArrayProp("adr"); 1.728 + testArrayProp("tel"); 1.729 + testArrayProp("impp"); 1.730 + testArrayProp("url"); 1.731 + next(); 1.732 + }, 1.733 + function() { 1.734 + ok(true, "Passing a mozContact with invalid data to save() should throw"); 1.735 + var c = new mozContact({ 1.736 + photo: [], 1.737 + tel: [] 1.738 + }); 1.739 + c.photo.push({}); 1.740 + SimpleTest.doesThrow(()=>navigator.mozContacts.save(c), "Invalid data in Blob array"); 1.741 + c.tel.push(123); 1.742 + SimpleTest.doesThrow(()=>navigator.mozContacts.save(c), "Invalid data in dictionary array"); 1.743 + next(); 1.744 + }, 1.745 + function() { 1.746 + ok(true, "Inline changes to array properties should be seen by save"); 1.747 + var c = new mozContact({ 1.748 + name: [], 1.749 + familyName: [], 1.750 + givenName: [], 1.751 + phoneticFamilyName: [], 1.752 + phoneticGivenName: [], 1.753 + nickname: [], 1.754 + tel: [], 1.755 + adr: [], 1.756 + email: [] 1.757 + }); 1.758 + for (var prop of Object.getOwnPropertyNames(properties1)) { 1.759 + if (!Array.isArray(properties1[prop])) { 1.760 + continue; 1.761 + } 1.762 + for (var i = 0; i < properties1[prop].length; ++i) { 1.763 + c[prop].push(properties1[prop][i]); 1.764 + } 1.765 + } 1.766 + req = navigator.mozContacts.save(c); 1.767 + req.onsuccess = function() { 1.768 + req = navigator.mozContacts.find(defaultOptions); 1.769 + req.onsuccess = function() { 1.770 + ise(req.result.length, 1, "Got 1 contact"); 1.771 + checkContacts(req.result[0], properties1); 1.772 + next(); 1.773 + }; 1.774 + req.onerror = onFailure; 1.775 + }; 1.776 + req.onerror = onFailure; 1.777 + }, 1.778 + clearDatabase, 1.779 + function() { 1.780 + ok(true, "mozContact.init deprecation message"); 1.781 + var c = new mozContact(); 1.782 + SimpleTest.monitorConsole(next, [ 1.783 + { errorMessage: "mozContact.init is DEPRECATED. Use the mozContact constructor instead. " + 1.784 + "See https://developer.mozilla.org/docs/WebAPI/Contacts for details." } 1.785 + ], /* forbidUnexpectedMsgs */ true); 1.786 + c.init({name: ["Bar"]}); 1.787 + c.init({name: ["Bar"]}); 1.788 + SimpleTest.endMonitorConsole(); 1.789 + }, 1.790 + function() { 1.791 + ok(true, "mozContact.init works as expected"); 1.792 + var c = new mozContact({name: ["Foo"]}); 1.793 + c.init({name: ["Bar"]}); 1.794 + ise(c.name[0], "Bar", "Same name"); 1.795 + next(); 1.796 + }, 1.797 + function() { 1.798 + ok(true, "mozContact.init without parameters"); 1.799 + var c = new mozContact({name: ["Foo"]}); 1.800 + c.init(); 1.801 + next(); 1.802 + }, 1.803 + function() { 1.804 + ok(true, "mozContact.init resets properties"); 1.805 + var c = new mozContact({jobTitle: ["Software Engineer"]}); 1.806 + c.init({nickname: ["Jobless Johnny"]}); 1.807 + ise(c.nickname[0], "Jobless Johnny", "Same nickname"); 1.808 + ok(!c.jobTitle, "jobTitle is not set"); 1.809 + next(); 1.810 + }, 1.811 + function() { 1.812 + ok(true, "mozContacts.remove with an ID works"); 1.813 + var c = new mozContact({name: ["Ephemeral Jimmy"]}); 1.814 + req = navigator.mozContacts.save(c); 1.815 + req.onsuccess = function() { 1.816 + req = navigator.mozContacts.remove(c.id); 1.817 + req.onsuccess = function() { 1.818 + req = navigator.mozContacts.find({ 1.819 + filterBy: ["id"], 1.820 + filterOp: "equals", 1.821 + filterValue: c.id 1.822 + }); 1.823 + req.onsuccess = function() { 1.824 + ise(req.result.length, 0, "Successfully removed contact by ID"); 1.825 + next(); 1.826 + }; 1.827 + req.onerror = onFailure; 1.828 + }; 1.829 + req.onerror = onFailure; 1.830 + }; 1.831 + req.onerror = onFailure; 1.832 + }, 1.833 + function () { 1.834 + ok(true, "Adding a new contact"); 1.835 + createResult1 = new mozContact(properties3); 1.836 + req = mozContacts.save(createResult1) 1.837 + req.onsuccess = function () { 1.838 + ok(createResult1.id, "The contact now has an ID."); 1.839 + sample_id1 = createResult1.id; 1.840 + next(); 1.841 + } 1.842 + req.onerror = onFailure; 1.843 + }, 1.844 + function () { 1.845 + ok(true, "Adding a new contact2"); 1.846 + createResult2 = new mozContact(properties4); 1.847 + req = mozContacts.save(createResult2); 1.848 + req.onsuccess = function () { 1.849 + ok(createResult2.id, "The contact now has an ID."); 1.850 + sample_id2 = createResult2.id; 1.851 + next(); 1.852 + }; 1.853 + req.onerror = onFailure; 1.854 + }, 1.855 + function () { 1.856 + ok(true, "Retrieving all contacts"); 1.857 + req = mozContacts.find({sortBy: "phoneticFamilyName"}); 1.858 + req.onsuccess = function () { 1.859 + is(req.result.length, 2, "Found exactly 2 contact."); 1.860 + checkContacts(req.result[1], properties3); 1.861 + next(); 1.862 + } 1.863 + req.onerror = onFailure; 1.864 + }, 1.865 + function () { 1.866 + ok(true, "Searching contacts by query1"); 1.867 + var options = {filterBy: ["phoneticGivenName", "email"], 1.868 + filterOp: "startsWith", 1.869 + filterValue: properties3.phoneticGivenName[0].substring(0, 3)} 1.870 + req = mozContacts.find(options) 1.871 + req.onsuccess = function () { 1.872 + is(req.result.length, 1, "Found exactly 1 contact."); 1.873 + findResult1 = req.result[0]; 1.874 + ok(findResult1.id == sample_id1, "Same ID"); 1.875 + checkContacts(findResult1, createResult1); 1.876 + next(); 1.877 + } 1.878 + req.onerror = onFailure; 1.879 + }, 1.880 + function () { 1.881 + ok(true, "Searching contacts by query2"); 1.882 + var options = {filterBy: ["phoneticGivenName", "email"], 1.883 + filterOp: "startsWith", 1.884 + filterValue: properties4.phoneticGivenName[0].substring(0, 3)}; 1.885 + req = mozContacts.find(options); 1.886 + req.onsuccess = function () { 1.887 + is(req.result.length, 1, "Found exactly 1 contact."); 1.888 + findResult1 = req.result[0]; 1.889 + is(findResult1.adr.length, 2, "Adr length 2"); 1.890 + checkContacts(findResult1, createResult2); 1.891 + next(); 1.892 + } 1.893 + req.onerror = onFailure; 1.894 + }, 1.895 + clearDatabase, 1.896 + function () { 1.897 + ok(true, "Adding 20 contacts"); 1.898 + for (var i=0; i<19; i++) { 1.899 + createResult1 = new mozContact(properties3); 1.900 + req = mozContacts.save(createResult1); 1.901 + req.onsuccess = function () { 1.902 + ok(createResult1.id, "The contact now has an ID."); 1.903 + }; 1.904 + req.onerror = onFailure; 1.905 + }; 1.906 + createResult1 = new mozContact(properties3); 1.907 + req = mozContacts.save(createResult1); 1.908 + req.onsuccess = function () { 1.909 + ok(createResult1.id, "The contact now has an ID."); 1.910 + checkStrArray(createResult1.name, properties3.name, "Same Name"); 1.911 + checkCount(20, "20 contacts in DB", next); 1.912 + }; 1.913 + req.onerror = onFailure; 1.914 + }, 1.915 + function () { 1.916 + ok(true, "Retrieving all contacts"); 1.917 + req = mozContacts.find(defaultOptions); 1.918 + req.onsuccess = function () { 1.919 + is(req.result.length, 20, "20 Entries."); 1.920 + next(); 1.921 + } 1.922 + req.onerror = onFailure; 1.923 + }, 1.924 + function () { 1.925 + ok(true, "Retrieving all contacts2"); 1.926 + var options = {filterBy: ["phoneticGivenName"], 1.927 + filterOp: "startsWith", 1.928 + filterValue: properties3.phoneticGivenName[0].substring(0, 3)}; 1.929 + req = mozContacts.find(options); 1.930 + req.onsuccess = function () { 1.931 + is(req.result.length, 20, "20 Entries."); 1.932 + checkContacts(createResult1, req.result[19]); 1.933 + next(); 1.934 + } 1.935 + req.onerror = onFailure; 1.936 + }, 1.937 + function () { 1.938 + ok(true, "Retrieving all contacts3"); 1.939 + var options = {filterBy: ["phoneticGivenName", "tel", "email"], 1.940 + filterOp: "startsWith", 1.941 + filterValue: properties3.phoneticGivenName[0].substring(0, 3)}; 1.942 + req = mozContacts.find(options); 1.943 + req.onsuccess = function () { 1.944 + is(req.result.length, 20, "20 Entries."); 1.945 + checkContacts(createResult1, req.result[10]); 1.946 + next(); 1.947 + } 1.948 + req.onerror = onFailure; 1.949 + }, 1.950 + clearDatabase, 1.951 + function () { 1.952 + ok(true, "Testing clone contact"); 1.953 + createResult1 = new mozContact(properties3); 1.954 + req = mozContacts.save(createResult1); 1.955 + req.onsuccess = function () { 1.956 + ok(createResult1.id, "The contact now has an ID."); 1.957 + checkStrArray(createResult1.phoneticFamilyName, properties3.phoneticFamilyName, "Same phoneticFamilyName"); 1.958 + checkStrArray(createResult1.phoneticGivenName, properties3.phoneticGivenName, "Same phoneticGivenName"); 1.959 + next(); 1.960 + } 1.961 + req.onerror = onFailure; 1.962 + }, 1.963 + function () { 1.964 + ok(true, "Retrieving all contacts"); 1.965 + req = mozContacts.find({sortBy: "phoneticGivenName"}); 1.966 + req.onsuccess = function () { 1.967 + is(req.result.length, 1, "1 Entries."); 1.968 + next(); 1.969 + } 1.970 + req.onerror = onFailure; 1.971 + }, 1.972 + clearDatabase, 1.973 + function () { 1.974 + ok(true, "Test sorting"); 1.975 + createResult1 = new mozContact(c11); 1.976 + req = navigator.mozContacts.save(createResult1); 1.977 + req.onsuccess = function () { 1.978 + ok(createResult1.id, "The contact now has an ID."); 1.979 + checkContacts(c11, createResult1); 1.980 + next(); 1.981 + }; 1.982 + req.onerror = onFailure; 1.983 + }, 1.984 + function () { 1.985 + ok(true, "Test sorting"); 1.986 + createResult1 = new mozContact(c10); 1.987 + req = navigator.mozContacts.save(createResult1); 1.988 + req.onsuccess = function () { 1.989 + ok(createResult1.id, "The contact now has an ID."); 1.990 + checkContacts(c10, createResult1); 1.991 + next(); 1.992 + }; 1.993 + req.onerror = onFailure; 1.994 + }, 1.995 + function () { 1.996 + ok(true, "Test sorting"); 1.997 + createResult1 = new mozContact(c12); 1.998 + req = navigator.mozContacts.save(createResult1); 1.999 + req.onsuccess = function () { 1.1000 + ok(createResult1.id, "The contact now has an ID."); 1.1001 + checkContacts(c12, createResult1); 1.1002 + next(); 1.1003 + }; 1.1004 + req.onerror = onFailure; 1.1005 + }, 1.1006 + function () { 1.1007 + ok(true, "Test sorting"); 1.1008 + createResult1 = new mozContact(c9); 1.1009 + req = navigator.mozContacts.save(createResult1); 1.1010 + req.onsuccess = function () { 1.1011 + ok(createResult1.id, "The contact now has an ID."); 1.1012 + checkContacts(c9, createResult1); 1.1013 + next(); 1.1014 + }; 1.1015 + req.onerror = onFailure; 1.1016 + }, 1.1017 + function () { 1.1018 + ok(true, "Test sorting"); 1.1019 + var options = {sortBy: "phoneticFamilyName", 1.1020 + sortOrder: "ascending"}; 1.1021 + req = navigator.mozContacts.find(options); 1.1022 + req.onsuccess = function () { 1.1023 + is(req.result.length, 4, "4 results"); 1.1024 + checkContacts(req.result[0], c9); 1.1025 + checkContacts(req.result[1], c10); 1.1026 + checkContacts(req.result[2], c11); 1.1027 + checkContacts(req.result[3], c12); 1.1028 + next(); 1.1029 + }; 1.1030 + req.onerror = onFailure; 1.1031 + }, 1.1032 + function () { 1.1033 + ok(true, "Test sorting"); 1.1034 + var options = {sortBy: "phoneticFamilyName", 1.1035 + sortOrder: "descending"}; 1.1036 + req = navigator.mozContacts.find(options); 1.1037 + req.onsuccess = function () { 1.1038 + is(req.result.length, 4, "4 results"); 1.1039 + checkContacts(req.result[0], c12); 1.1040 + checkContacts(req.result[1], c11); 1.1041 + checkContacts(req.result[2], c10); 1.1042 + checkContacts(req.result[3], c9); 1.1043 + next(); 1.1044 + }; 1.1045 + req.onerror = onFailure; 1.1046 + }, 1.1047 + function () { 1.1048 + ok(true, "Test sorting"); 1.1049 + createResult1 = new mozContact(c13); 1.1050 + req = navigator.mozContacts.save(createResult1); 1.1051 + req.onsuccess = function () { 1.1052 + ok(createResult1.id, "The contact now has an ID."); 1.1053 + checkContacts(c13, createResult1); 1.1054 + next(); 1.1055 + }; 1.1056 + req.onerror = onFailure; 1.1057 + }, 1.1058 + function () { 1.1059 + ok(true, "Test sorting with empty string"); 1.1060 + var options = {sortBy: "phoneticFamilyName", 1.1061 + sortOrder: "ascending"}; 1.1062 + req = navigator.mozContacts.find(options); 1.1063 + req.onsuccess = function () { 1.1064 + is(req.result.length, 5, "5 results"); 1.1065 + checkContacts(req.result[0], c13); 1.1066 + checkContacts(req.result[1], c9); 1.1067 + checkContacts(req.result[2], c10); 1.1068 + checkContacts(req.result[3], c11); 1.1069 + checkContacts(req.result[4], c12); 1.1070 + next(); 1.1071 + }; 1.1072 + req.onerror = onFailure; 1.1073 + }, 1.1074 + clearDatabase, 1.1075 + function () { 1.1076 + ok(true, "Test sorting"); 1.1077 + createResult1 = new mozContact(c15); 1.1078 + req = navigator.mozContacts.save(createResult1); 1.1079 + req.onsuccess = function () { 1.1080 + ok(createResult1.id, "The contact now has an ID."); 1.1081 + checkContacts(c15, createResult1); 1.1082 + next(); 1.1083 + }; 1.1084 + req.onerror = onFailure; 1.1085 + }, 1.1086 + function () { 1.1087 + ok(true, "Test sorting"); 1.1088 + createResult1 = new mozContact(c14); 1.1089 + req = navigator.mozContacts.save(createResult1); 1.1090 + req.onsuccess = function () { 1.1091 + ok(createResult1.id, "The contact now has an ID."); 1.1092 + checkContacts(c14, createResult1); 1.1093 + next(); 1.1094 + }; 1.1095 + req.onerror = onFailure; 1.1096 + }, 1.1097 + function () { 1.1098 + ok(true, "Test sorting"); 1.1099 + createResult1 = new mozContact(c16); 1.1100 + req = navigator.mozContacts.save(createResult1); 1.1101 + req.onsuccess = function () { 1.1102 + ok(createResult1.id, "The contact now has an ID."); 1.1103 + checkContacts(c16, createResult1); 1.1104 + next(); 1.1105 + }; 1.1106 + req.onerror = onFailure; 1.1107 + }, 1.1108 + function () { 1.1109 + // Android does not support published/updated fields. Skip this. 1.1110 + if (isAndroid) { 1.1111 + next(); 1.1112 + return; 1.1113 + } 1.1114 + 1.1115 + ok(true, "Test sorting with published"); 1.1116 + var options = {sortBy: "phoneticFamilyName", 1.1117 + sortOrder: "descending"}; 1.1118 + req = navigator.mozContacts.find(options); 1.1119 + req.onsuccess = function () { 1.1120 + is(req.result.length, 3, "3 results"); 1.1121 + ok(req.result[0].published < req.result[1].published, "Right sorting order"); 1.1122 + ok(req.result[1].published < req.result[2].published, "Right sorting order"); 1.1123 + next(); 1.1124 + }; 1.1125 + req.onerror = onFailure; 1.1126 + }, 1.1127 + clearDatabase, 1.1128 + function () { 1.1129 + ok(true, "all done!\n"); 1.1130 + SimpleTest.finish(); 1.1131 + } 1.1132 +]; 1.1133 + 1.1134 +function next() { 1.1135 + ok(true, "Begin!"); 1.1136 + if (index >= steps.length) { 1.1137 + ok(false, "Shouldn't get here!"); 1.1138 + return; 1.1139 + } 1.1140 + try { 1.1141 + var i = index++; 1.1142 + steps[i](); 1.1143 + } catch(ex) { 1.1144 + ok(false, "Caught exception", ex); 1.1145 + } 1.1146 +} 1.1147 + 1.1148 +start_tests(); 1.1149 +</script> 1.1150 +</pre> 1.1151 +</body> 1.1152 +</html>