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.
1 <!DOCTYPE html>
2 <html>
3 <!--
4 https://bugzilla.mozilla.org/show_bug.cgi?id=674720
5 -->
6 <head>
7 <title>Test for Bug 674720 WebContacts</title>
8 <script type="text/javascript" src="/MochiKit/MochiKit.js"></script>
9 <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
10 <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
11 </head>
12 <body>
14 <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=674720">Mozilla Bug 674720</a>
15 <p id="display"></p>
16 <div id="content" style="display: none">
18 </div>
19 <pre id="test">
20 <script type="text/javascript;version=1.8" src="http://mochi.test:8888/tests/dom/contacts/tests/shared.js"></script>
21 <script class="testbody" type="text/javascript">
22 "use strict";
24 var req;
26 var steps = [
27 function () {
28 ok(true, "Adding a new contact");
29 createResult1 = new mozContact(properties1);
30 req = mozContacts.save(createResult1)
31 req.onsuccess = function () {
32 ok(createResult1.id, "The contact now has an ID.");
33 sample_id1 = createResult1.id;
34 next();
35 }
36 req.onerror = onFailure;
37 },
38 function () {
39 ok(true, "Adding a new contact2");
40 createResult2 = new mozContact(properties2);
41 req = mozContacts.save(createResult2);
42 req.onsuccess = function () {
43 ok(createResult2.id, "The contact now has an ID.");
44 sample_id2 = createResult2.id;
45 next();
46 };
47 req.onerror = onFailure;
48 },
49 function () {
50 ok(true, "Retrieving all contacts");
51 req = mozContacts.find({sortBy: "familyName"});
52 req.onsuccess = function () {
53 is(req.result.length, 2, "Found exactly 2 contact.");
54 checkContacts(req.result[1], properties1);
55 next();
56 }
57 req.onerror = onFailure;
58 },
59 function () {
60 console.log("Searching contacts by query1");
61 var options = {filterBy: ["givenName", "email"],
62 filterOp: "startsWith",
63 filterValue: properties1.givenName[0].substring(0, 4)}
64 req = mozContacts.find(options)
65 req.onsuccess = function () {
66 is(req.result.length, 1, "Found exactly 1 contact.");
67 findResult1 = req.result[0];
68 ok(findResult1.id == sample_id1, "Same ID");
69 checkContacts(findResult1, createResult1);
70 next();
71 }
72 req.onerror = onFailure;
73 },
74 function () {
75 ok(true, "Searching contacts by query2");
76 var options = {filterBy: ["givenName", "email"],
77 filterOp: "startsWith",
78 filterValue: properties2.givenName[0].substring(0, 4)};
79 req = mozContacts.find(options);
80 req.onsuccess = function () {
81 is(req.result.length, 1, "Found exactly 1 contact.");
82 findResult1 = req.result[0];
83 is(findResult1.adr.length, 2, "Adr length 2");
84 checkContacts(findResult1, createResult2);
85 next();
86 }
87 req.onerror = onFailure;
88 },
89 function () {
90 ok(true, "Searching contacts by tel");
91 var options = {filterBy: ["tel"],
92 filterOp: "contains",
93 filterValue: properties2.tel[0].value.substring(3, 7)};
94 req = mozContacts.find(options);
95 req.onsuccess = function () {
96 is(req.result.length, 1, "Found exactly 1 contact.");
97 findResult1 = req.result[0];
98 ok(findResult1.id == sample_id2, "Same ID");
99 checkContacts(findResult1, createResult2);
100 next();
101 }
102 req.onerror = onFailure;
103 },
104 function () {
105 ok(true, "Searching contacts by email");
106 var options = {filterBy: ["email"],
107 filterOp: "startsWith",
108 filterValue: properties2.email[0].value.substring(0, 4)};
109 req = mozContacts.find(options);
110 req.onsuccess = function () {
111 is(req.result.length, 1, "Found exactly 1 contact.");
112 findResult1 = req.result[0];
113 ok(findResult1.id == sample_id2, "Same ID");
114 checkContacts(findResult1, createResult2);
115 next();
116 }
117 req.onerror = onFailure;
118 },
119 function () {
120 ok(true, "Deleting database");
121 req = mozContacts.clear();
122 req.onsuccess = function () {
123 next();
124 }
125 req.onerror = onFailure;
126 },
127 function () {
128 ok(true, "Adding 20 contacts");
129 for (var i=0; i<19; i++) {
130 createResult1 = new mozContact(properties1);
131 req = mozContacts.save(createResult1);
132 req.onsuccess = function () {
133 ok(createResult1.id, "The contact now has an ID.");
134 };
135 req.onerror = onFailure;
136 };
137 createResult1 = new mozContact(properties1);
138 req = mozContacts.save(createResult1);
139 req.onsuccess = function () {
140 ok(createResult1.id, "The contact now has an ID.");
141 checkStrArray(createResult1.name, properties1.name, "Same Name");
142 checkCount(20, "20 contacts in DB", next);
143 };
144 req.onerror = onFailure;
145 },
146 function () {
147 ok(true, "Retrieving all contacts");
148 req = mozContacts.find(defaultOptions);
149 req.onsuccess = function () {
150 is(req.result.length, 20, "20 Entries.");
151 next();
152 }
153 req.onerror = onFailure;
154 },
155 function () {
156 ok(true, "Retrieving all contacts with limit 10");
157 var options = { filterLimit: 10 };
158 req = mozContacts.find(options);
159 req.onsuccess = function () {
160 is(req.result.length, 10, "10 Entries.");
161 next();
162 }
163 req.onerror = onFailure;
164 },
165 function () {
166 ok(true, "Retrieving all contacts with limit 10 and sorted");
167 var options = { filterLimit: 10,
168 sortBy: 'FamilyName',
169 sortOrder: 'descending' };
170 req = mozContacts.find(options);
171 req.onsuccess = function () {
172 is(req.result.length, 10, "10 Entries.");
173 next();
174 }
175 req.onerror = onFailure;
176 },
177 function () {
178 ok(true, "Retrieving all contacts2");
179 var options = {filterBy: ["givenName"],
180 filterOp: "startsWith",
181 filterValue: properties1.givenName[0].substring(0, 4)};
182 req = mozContacts.find(options);
183 req.onsuccess = function () {
184 is(req.result.length, 20, "20 Entries.");
185 checkContacts(createResult1, req.result[19]);
186 next();
187 }
188 req.onerror = onFailure;
189 },
190 function () {
191 ok(true, "Retrieving all contacts3");
192 var options = {filterBy: ["givenName", "tel", "email"],
193 filterOp: "startsWith",
194 filterValue: properties1.givenName[0].substring(0, 4)};
195 req = mozContacts.find(options);
196 req.onsuccess = function () {
197 is(req.result.length, 20, "20 Entries.");
198 checkContacts(createResult1, req.result[10]);
199 next();
200 }
201 req.onerror = onFailure;
202 },
203 function () {
204 ok(true, "Deleting database");
205 req = mozContacts.clear();
206 req.onsuccess = function () {
207 next();
208 }
209 req.onerror = onFailure;
210 },
211 function () {
212 ok(true, "Testing clone contact");
213 createResult1 = new mozContact(properties1);
214 req = mozContacts.save(createResult1);
215 req.onsuccess = function () {
216 ok(createResult1.id, "The contact now has an ID.");
217 checkStrArray(createResult1.name, properties1.name, "Same Name");
218 next();
219 }
220 req.onerror = onFailure;
221 },
222 function() {
223 ok(true, "Testing clone contact2");
224 var cloned = new mozContact(createResult1);
225 ok(cloned.id != createResult1.id, "Cloned contact has new ID");
226 cloned.email = [{value: "new email!"}];
227 cloned.givenName = ["Tom"];
228 req = mozContacts.save(cloned);
229 req.onsuccess = function () {
230 ok(cloned.id, "The contact now has an ID.");
231 is(cloned.email[0].value, "new email!", "Same Email");
232 isnot(createResult1.email[0].value, cloned.email[0].value, "Clone has different email");
233 is(cloned.givenName, "Tom", "New Name");
234 next();
235 }
236 req.onerror = onFailure;
237 },
238 function () {
239 ok(true, "Retrieving all contacts");
240 var options = {filterBy: ["givenName"],
241 filterOp: "startsWith",
242 filterValue: properties2.givenName[0].substring(0, 4)};
243 req = mozContacts.find(defaultOptions);
244 req.onsuccess = function () {
245 is(req.result.length, 2, "2 Entries.");
246 next();
247 }
248 req.onerror = onFailure;
249 },
250 function () {
251 ok(true, "Search with redundant fields should only return 1 contact");
252 createResult1 = new mozContact({name: ["XXX"],
253 givenName: ["XXX"],
254 email: [{value: "XXX"}],
255 tel: [{value: "XXX"}]
256 });
257 req = mozContacts.save(createResult1);
258 req.onsuccess = function() {
259 var options = {filterBy: ["givenName", "familyName"],
260 filterOp: "equals",
261 filterValue: "XXX"};
262 var req2 = mozContacts.find(options);
263 req2.onsuccess = function() {
264 is(req2.result.length, 1, "1 Entry");
265 next();
266 }
267 req2.onerror = onFailure;
268 }
269 req.onerror = onFailure;
270 },
271 function () {
272 ok(true, "Deleting database");
273 req = mozContacts.clear()
274 req.onsuccess = function () {
275 ok(true, "Deleted the database");
276 next();
277 }
278 req.onerror = onFailure;
279 },
280 function () {
281 ok(true, "Test sorting");
282 createResult1 = new mozContact(c3);
283 req = navigator.mozContacts.save(createResult1);
284 req.onsuccess = function () {
285 ok(createResult1.id, "The contact now has an ID.");
286 checkContacts(c3, createResult1);
287 next();
288 };
289 req.onerror = onFailure;
290 },
291 function () {
292 ok(true, "Test sorting");
293 createResult1 = new mozContact(c2);
294 req = navigator.mozContacts.save(createResult1);
295 req.onsuccess = function () {
296 ok(createResult1.id, "The contact now has an ID.");
297 checkContacts(c2, createResult1);
298 next();
299 };
300 req.onerror = onFailure;
301 },
302 function () {
303 ok(true, "Test sorting");
304 createResult1 = new mozContact(c4);
305 req = navigator.mozContacts.save(createResult1);
306 req.onsuccess = function () {
307 ok(createResult1.id, "The contact now has an ID.");
308 checkContacts(c4, createResult1);
309 next();
310 };
311 req.onerror = onFailure;
312 },
313 function () {
314 ok(true, "Test sorting");
315 createResult1 = new mozContact(c1);
316 req = navigator.mozContacts.save(createResult1);
317 req.onsuccess = function () {
318 ok(createResult1.id, "The contact now has an ID.");
319 checkContacts(c1, createResult1);
320 next();
321 };
322 req.onerror = onFailure;
323 },
324 function () {
325 ok(true, "Test sorting");
326 var options = {sortBy: "familyName",
327 sortOrder: "ascending"};
328 req = navigator.mozContacts.find(options);
329 req.onsuccess = function () {
330 is(req.result.length, 4, "4 results");
331 checkContacts(req.result[0], c1);
332 checkContacts(req.result[1], c2);
333 checkContacts(req.result[2], c3);
334 checkContacts(req.result[3], c4);
335 next();
336 };
337 req.onerror = onFailure;
338 },
339 function () {
340 ok(true, "Test sorting");
341 var options = {sortBy: "familyName",
342 sortOrder: "descending"};
343 req = navigator.mozContacts.find(options);
344 req.onsuccess = function () {
345 is(req.result.length, 4, "4 results");
346 checkContacts(req.result[0], c4);
347 checkContacts(req.result[1], c3);
348 checkContacts(req.result[2], c2);
349 checkContacts(req.result[3], c1);
350 next();
351 };
352 req.onerror = onFailure;
353 },
354 function () {
355 ok(true, "Test sorting");
356 createResult1 = new mozContact(c5);
357 req = navigator.mozContacts.save(createResult1);
358 req.onsuccess = function () {
359 ok(createResult1.id, "The contact now has an ID.");
360 checkContacts(c5, createResult1);
361 next();
362 };
363 req.onerror = onFailure;
364 },
365 function () {
366 ok(true, "Test sorting with empty string");
367 var options = {sortBy: "familyName",
368 sortOrder: "ascending"};
369 req = navigator.mozContacts.find(options);
370 req.onsuccess = function () {
371 is(req.result.length, 5, "5 results");
372 checkContacts(req.result[0], c5);
373 checkContacts(req.result[1], c1);
374 checkContacts(req.result[2], c2);
375 checkContacts(req.result[3], c3);
376 checkContacts(req.result[4], c4);
377 next();
378 };
379 req.onerror = onFailure;
380 },
381 function () {
382 ok(true, "Don't allow to add custom fields");
383 createResult1 = new mozContact({givenName: ["customTest"], yyy: "XXX"});
384 req = mozContacts.save(createResult1);
385 req.onsuccess = function() {
386 var options = {filterBy: ["givenName"],
387 filterOp: "equals",
388 filterValue: "customTest"};
389 var req2 = mozContacts.find(options);
390 req2.onsuccess = function() {
391 is(req2.result.length, 1, "1 Entry");
392 checkStrArray(req2.result[0].givenName, ["customTest"], "same name");
393 ok(req2.result.yyy === undefined, "custom property undefined");
394 next();
395 }
396 req2.onerror = onFailure;
397 }
398 req.onerror = onFailure;
399 },
400 function () {
401 ok(true, "Deleting database");
402 req = mozContacts.clear()
403 req.onsuccess = function () {
404 ok(true, "Deleted the database");
405 next();
406 }
407 req.onerror = onFailure;
408 },
409 function () {
410 ok(true, "Test sorting");
411 createResult1 = new mozContact(c7);
412 req = navigator.mozContacts.save(createResult1);
413 req.onsuccess = function () {
414 ok(createResult1.id, "The contact now has an ID.");
415 checkContacts(c7, createResult1);
416 next();
417 };
418 req.onerror = onFailure;
419 },
420 function () {
421 ok(true, "Test sorting");
422 createResult1 = new mozContact(c6);
423 req = navigator.mozContacts.save(createResult1);
424 req.onsuccess = function () {
425 ok(createResult1.id, "The contact now has an ID.");
426 checkContacts(c6, createResult1);
427 next();
428 };
429 req.onerror = onFailure;
430 },
431 function () {
432 ok(true, "Test sorting");
433 createResult1 = new mozContact(c8);
434 req = navigator.mozContacts.save(createResult1);
435 req.onsuccess = function () {
436 ok(createResult1.id, "The contact now has an ID.");
437 checkContacts(c8, createResult1);
438 next();
439 };
440 req.onerror = onFailure;
441 },
442 function () {
443 // Android does not support published/updated fields. Skip this.
444 if (isAndroid) {
445 next();
446 return;
447 }
449 ok(true, "Test sorting with published");
450 var options = {sortBy: "familyName",
451 sortOrder: "descending"};
452 req = navigator.mozContacts.find(options);
453 req.onsuccess = function () {
454 is(req.result.length, 3, "3 results");
455 ok(req.result[0].published < req.result[1].published, "Right sorting order");
456 ok(req.result[1].published < req.result[2].published, "Right sorting order");
457 next();
458 };
459 req.onerror = onFailure;
460 },
461 function () {
462 ok(true, "Deleting database");
463 req = mozContacts.clear();
464 req.onsuccess = function () {
465 ok(true, "Deleted the database");
466 next();
467 }
468 req.onerror = onFailure;
469 },
470 function () {
471 ok(true, "Adding a new contact with properties2");
472 createResult2 = new mozContact(properties2);
473 req = mozContacts.save(createResult2);
474 req.onsuccess = function () {
475 ok(createResult2.id, "The contact now has an ID.");
476 sample_id2 = createResult2.id;
477 next();
478 };
479 req.onerror = onFailure;
480 },
481 function () {
482 ok(true, "Test category search with startsWith");
483 var options = {filterBy: ["category"],
484 filterOp: "startsWith",
485 filterValue: properties2.category[0]};
486 req = mozContacts.find(options);
487 req.onsuccess = function () {
488 is(req.result.length, 1, "1 Entry.");
489 checkContacts(req.result[0], createResult2);
490 next();
491 }
492 req.onerror = onFailure;
493 },
494 function () {
495 ok(true, "Test category search with equals");
496 var options = {filterBy: ["category"],
497 filterOp: "equals",
498 filterValue: properties2.category[0]};
499 req = mozContacts.find(options);
500 req.onsuccess = function () {
501 is(req.result.length, 1, "1 Entry.");
502 checkContacts(req.result[0], createResult2);
503 next();
504 }
505 req.onerror = onFailure;
506 },
507 function () {
508 ok(true, "Deleting database");
509 req = mozContacts.clear()
510 req.onsuccess = function () {
511 ok(true, "Deleted the database");
512 next();
513 }
514 req.onerror = onFailure;
515 },
516 function () {
517 ok(true, "Adding contact for category search");
518 createResult1 = new mozContact({name: ["5"], givenName: ["5"]});
519 req = navigator.mozContacts.save(createResult1);
520 req.onsuccess = function () {
521 ok(createResult1.id, "The contact now has an ID.");
522 sample_id1 = createResult1.id;
523 next();
524 };
525 req.onerror = onFailure;
526 },
527 function () {
528 ok(true, "Test category search with equals");
529 var options = {filterBy: ["givenName"],
530 filterOp: "startsWith",
531 filterValue: "5"};
532 req = mozContacts.find(options);
533 req.onsuccess = function () {
534 is(req.result.length, 1, "1 Entry.");
535 checkContacts(req.result[0], createResult1);
536 next();
537 }
538 req.onerror = onFailure;
539 },
540 function () {
541 ok(true, "Deleting database");
542 req = mozContacts.clear()
543 req.onsuccess = function () {
544 ok(true, "Deleted the database");
545 next();
546 }
547 req.onerror = onFailure;
548 },
549 function () {
550 ok(true, "Adding contact with invalid data");
551 var obj = {
552 honorificPrefix: [],
553 honorificSuffix: [{foo: "bar"}],
554 sex: 17,
555 genderIdentity: 18,
556 email: [{type: ["foo"], value: "bar"}]
557 };
558 obj.honorificPrefix.__defineGetter__('0',(function() {
559 var c = 0;
560 return function() {
561 if (c == 0) {
562 c++;
563 return "string";
564 } else {
565 return {foo:"bar"};
566 }
567 }
568 })());
569 createResult1 = new mozContact(obj);
570 createResult1.email.push({aeiou: "abcde"});
571 req = mozContacts.save(createResult1);
572 req.onsuccess = function () {
573 checkContacts(createResult1, {
574 honorificPrefix: ["string"],
575 honorificSuffix: ["[object Object]"],
576 sex: "17",
577 genderIdentity: "18",
578 email: [{type: ["foo"], value: "bar"}, {}]
579 });
580 next();
581 };
582 },
583 function () {
584 ok(true, "Adding contact with no number but carrier");
585 createResult1 = new mozContact({ tel: [{type: ["home"], carrier: "myCarrier"} ] });
586 req = navigator.mozContacts.save(createResult1);
587 req.onsuccess = function () {
588 ok(createResult1.id, "The contact now has an ID.");
589 next();
590 };
591 req.onerror = onFailure;
592 },
593 function () {
594 ok(true, "Adding contact with email but no value");
595 createResult1 = new mozContact({ email: [{type: ["home"]}] });
596 req = navigator.mozContacts.save(createResult1);
597 req.onsuccess = function () {
598 ok(createResult1.id, "The contact now has an ID.");
599 next();
600 };
601 req.onerror = onFailure;
602 },
603 function () {
604 ok(true, "Testing numbersOnly search 1");
605 createResult1 = new mozContact({ name: ["aaaaaaaaa"], givenName: ["aaaaaaaaa"], tel: [{ value: "1234567890"}]});
606 req = navigator.mozContacts.save(createResult1);
607 req.onsuccess = function () {
608 ok(createResult1.id, "The contact now has an ID.");
609 next();
610 };
611 req.onerror = onFailure;
612 },
613 function () {
614 ok(true, "Test numbersOnly search 2");
615 var options = {filterBy: ["givenName", "tel"],
616 filterOp: "contains",
617 filterValue: "a"};
618 req = mozContacts.find(options);
619 req.onsuccess = function () {
620 ok(req.result.length == 1, "1 Entry.");
621 checkContacts(req.result[0], createResult1);
622 next();
623 }
624 req.onerror = onFailure;
625 },
626 function () {
627 ok(true, "Test numbersOnly search 3");
628 var options = {filterBy: ["givenName", "tel"],
629 filterOp: "contains",
630 filterValue: "b"};
631 req = mozContacts.find(options);
632 req.onsuccess = function () {
633 ok(req.result.length == 0, "0 Entries.");
634 next();
635 }
636 req.onerror = onFailure;
637 },
638 function () {
639 ok(true, "Test numbersOnly search 4");
640 var options = {filterBy: ["givenName", "tel"],
641 filterOp: "contains",
642 filterValue: "1a"};
643 req = mozContacts.find(options);
644 req.onsuccess = function () {
645 ok(req.result.length == 0, "0 Entries.");
646 next();
647 }
648 req.onerror = onFailure;
649 },
650 function () {
651 ok(true, "Test numbersOnly search 5");
652 var options = {filterBy: ["givenName", "tel"],
653 filterOp: "contains",
654 filterValue: "1(23)"};
655 req = mozContacts.find(options);
656 req.onsuccess = function () {
657 ok(req.result.length == 1, "1 Entry.");
658 next();
659 }
660 req.onerror = onFailure;
661 },
662 function () {
663 ok(true, "Test numbersOnly search 6");
664 var options = {filterBy: ["givenName", "tel"],
665 filterOp: "contains",
666 filterValue: "1(23)a"};
667 req = mozContacts.find(options);
668 req.onsuccess = function () {
669 ok(req.result.length == 0, "0 Entries.");
670 next();
671 }
672 req.onerror = onFailure;
673 },
674 function () {
675 ok(true, "Deleting database");
676 req = mozContacts.clear()
677 req.onsuccess = function () {
678 ok(true, "Deleted the database");
679 next();
680 }
681 req.onerror = onFailure;
682 },
683 function() {
684 ok(true, "Test that after setting array properties to scalar values the property os not a non-array")
685 const FIELDS = ["email","url","adr","tel","impp"];
686 createResult1 = new mozContact();
687 for (var prop of FIELDS) {
688 try {
689 createResult1[prop] = {type: ["foo"]};
690 } catch (e) {}
691 ok(createResult1[prop] === null ||
692 Array.isArray(createResult1[prop]), prop + " is array");
693 }
694 next();
695 },
696 function() {
697 ok(true, "Undefined properties of fields should be treated correctly");
698 var c = new mozContact({
699 adr: [{streetAddress: undefined}],
700 email: [{value: undefined}],
701 url: [{value: undefined}],
702 impp: [{value: undefined}],
703 tel: [{value: undefined}],
704 });
705 ise(c.adr[0].streetAddress, undefined, "adr.streetAddress is undefined");
706 ise(c.adr[0].locality, undefined, "adr.locality is undefined");
707 ise(c.adr[0].pref, undefined, "adr.pref is undefined");
708 ise(c.email[0].value, undefined, "email.value is undefined");
709 ise(c.url[0].value, undefined, "url.value is undefined");
710 ise(c.impp[0].value, undefined, "impp.value is undefined");
711 ise(c.tel[0].value, undefined, "tel.value is undefined");
712 next();
713 },
714 function() {
715 ok(true, "Setting array properties to an empty array should work");
716 var c = new mozContact();
717 function testArrayProp(prop) {
718 is(c[prop], null, "property is initially null");
719 c[prop] = [];
720 ok(Array.isArray(c[prop]), "property is an array after setting");
721 is(c[prop].length, 0, "property has length 0 after setting");
722 }
723 testArrayProp("email");
724 testArrayProp("adr");
725 testArrayProp("tel");
726 testArrayProp("impp");
727 testArrayProp("url");
728 next();
729 },
730 function() {
731 ok(true, "Passing a mozContact with invalid data to save() should throw");
732 var c = new mozContact({
733 photo: [],
734 tel: []
735 });
736 c.photo.push({});
737 SimpleTest.doesThrow(()=>navigator.mozContacts.save(c), "Invalid data in Blob array");
738 c.tel.push(123);
739 SimpleTest.doesThrow(()=>navigator.mozContacts.save(c), "Invalid data in dictionary array");
740 next();
741 },
742 function() {
743 ok(true, "Inline changes to array properties should be seen by save");
744 var c = new mozContact({
745 name: [],
746 familyName: [],
747 givenName: [],
748 phoneticFamilyName: [],
749 phoneticGivenName: [],
750 nickname: [],
751 tel: [],
752 adr: [],
753 email: []
754 });
755 for (var prop of Object.getOwnPropertyNames(properties1)) {
756 if (!Array.isArray(properties1[prop])) {
757 continue;
758 }
759 for (var i = 0; i < properties1[prop].length; ++i) {
760 c[prop].push(properties1[prop][i]);
761 }
762 }
763 req = navigator.mozContacts.save(c);
764 req.onsuccess = function() {
765 req = navigator.mozContacts.find(defaultOptions);
766 req.onsuccess = function() {
767 ise(req.result.length, 1, "Got 1 contact");
768 checkContacts(req.result[0], properties1);
769 next();
770 };
771 req.onerror = onFailure;
772 };
773 req.onerror = onFailure;
774 },
775 clearDatabase,
776 function() {
777 ok(true, "mozContact.init deprecation message");
778 var c = new mozContact();
779 SimpleTest.monitorConsole(next, [
780 { errorMessage: "mozContact.init is DEPRECATED. Use the mozContact constructor instead. " +
781 "See https://developer.mozilla.org/docs/WebAPI/Contacts for details." }
782 ], /* forbidUnexpectedMsgs */ true);
783 c.init({name: ["Bar"]});
784 c.init({name: ["Bar"]});
785 SimpleTest.endMonitorConsole();
786 },
787 function() {
788 ok(true, "mozContact.init works as expected");
789 var c = new mozContact({name: ["Foo"]});
790 c.init({name: ["Bar"]});
791 ise(c.name[0], "Bar", "Same name");
792 next();
793 },
794 function() {
795 ok(true, "mozContact.init without parameters");
796 var c = new mozContact({name: ["Foo"]});
797 c.init();
798 next();
799 },
800 function() {
801 ok(true, "mozContact.init resets properties");
802 var c = new mozContact({jobTitle: ["Software Engineer"]});
803 c.init({nickname: ["Jobless Johnny"]});
804 ise(c.nickname[0], "Jobless Johnny", "Same nickname");
805 ok(!c.jobTitle, "jobTitle is not set");
806 next();
807 },
808 function() {
809 ok(true, "mozContacts.remove with an ID works");
810 var c = new mozContact({name: ["Ephemeral Jimmy"]});
811 req = navigator.mozContacts.save(c);
812 req.onsuccess = function() {
813 req = navigator.mozContacts.remove(c.id);
814 req.onsuccess = function() {
815 req = navigator.mozContacts.find({
816 filterBy: ["id"],
817 filterOp: "equals",
818 filterValue: c.id
819 });
820 req.onsuccess = function() {
821 ise(req.result.length, 0, "Successfully removed contact by ID");
822 next();
823 };
824 req.onerror = onFailure;
825 };
826 req.onerror = onFailure;
827 };
828 req.onerror = onFailure;
829 },
830 function () {
831 ok(true, "Adding a new contact");
832 createResult1 = new mozContact(properties3);
833 req = mozContacts.save(createResult1)
834 req.onsuccess = function () {
835 ok(createResult1.id, "The contact now has an ID.");
836 sample_id1 = createResult1.id;
837 next();
838 }
839 req.onerror = onFailure;
840 },
841 function () {
842 ok(true, "Adding a new contact2");
843 createResult2 = new mozContact(properties4);
844 req = mozContacts.save(createResult2);
845 req.onsuccess = function () {
846 ok(createResult2.id, "The contact now has an ID.");
847 sample_id2 = createResult2.id;
848 next();
849 };
850 req.onerror = onFailure;
851 },
852 function () {
853 ok(true, "Retrieving all contacts");
854 req = mozContacts.find({sortBy: "phoneticFamilyName"});
855 req.onsuccess = function () {
856 is(req.result.length, 2, "Found exactly 2 contact.");
857 checkContacts(req.result[1], properties3);
858 next();
859 }
860 req.onerror = onFailure;
861 },
862 function () {
863 ok(true, "Searching contacts by query1");
864 var options = {filterBy: ["phoneticGivenName", "email"],
865 filterOp: "startsWith",
866 filterValue: properties3.phoneticGivenName[0].substring(0, 3)}
867 req = mozContacts.find(options)
868 req.onsuccess = function () {
869 is(req.result.length, 1, "Found exactly 1 contact.");
870 findResult1 = req.result[0];
871 ok(findResult1.id == sample_id1, "Same ID");
872 checkContacts(findResult1, createResult1);
873 next();
874 }
875 req.onerror = onFailure;
876 },
877 function () {
878 ok(true, "Searching contacts by query2");
879 var options = {filterBy: ["phoneticGivenName", "email"],
880 filterOp: "startsWith",
881 filterValue: properties4.phoneticGivenName[0].substring(0, 3)};
882 req = mozContacts.find(options);
883 req.onsuccess = function () {
884 is(req.result.length, 1, "Found exactly 1 contact.");
885 findResult1 = req.result[0];
886 is(findResult1.adr.length, 2, "Adr length 2");
887 checkContacts(findResult1, createResult2);
888 next();
889 }
890 req.onerror = onFailure;
891 },
892 clearDatabase,
893 function () {
894 ok(true, "Adding 20 contacts");
895 for (var i=0; i<19; i++) {
896 createResult1 = new mozContact(properties3);
897 req = mozContacts.save(createResult1);
898 req.onsuccess = function () {
899 ok(createResult1.id, "The contact now has an ID.");
900 };
901 req.onerror = onFailure;
902 };
903 createResult1 = new mozContact(properties3);
904 req = mozContacts.save(createResult1);
905 req.onsuccess = function () {
906 ok(createResult1.id, "The contact now has an ID.");
907 checkStrArray(createResult1.name, properties3.name, "Same Name");
908 checkCount(20, "20 contacts in DB", next);
909 };
910 req.onerror = onFailure;
911 },
912 function () {
913 ok(true, "Retrieving all contacts");
914 req = mozContacts.find(defaultOptions);
915 req.onsuccess = function () {
916 is(req.result.length, 20, "20 Entries.");
917 next();
918 }
919 req.onerror = onFailure;
920 },
921 function () {
922 ok(true, "Retrieving all contacts2");
923 var options = {filterBy: ["phoneticGivenName"],
924 filterOp: "startsWith",
925 filterValue: properties3.phoneticGivenName[0].substring(0, 3)};
926 req = mozContacts.find(options);
927 req.onsuccess = function () {
928 is(req.result.length, 20, "20 Entries.");
929 checkContacts(createResult1, req.result[19]);
930 next();
931 }
932 req.onerror = onFailure;
933 },
934 function () {
935 ok(true, "Retrieving all contacts3");
936 var options = {filterBy: ["phoneticGivenName", "tel", "email"],
937 filterOp: "startsWith",
938 filterValue: properties3.phoneticGivenName[0].substring(0, 3)};
939 req = mozContacts.find(options);
940 req.onsuccess = function () {
941 is(req.result.length, 20, "20 Entries.");
942 checkContacts(createResult1, req.result[10]);
943 next();
944 }
945 req.onerror = onFailure;
946 },
947 clearDatabase,
948 function () {
949 ok(true, "Testing clone contact");
950 createResult1 = new mozContact(properties3);
951 req = mozContacts.save(createResult1);
952 req.onsuccess = function () {
953 ok(createResult1.id, "The contact now has an ID.");
954 checkStrArray(createResult1.phoneticFamilyName, properties3.phoneticFamilyName, "Same phoneticFamilyName");
955 checkStrArray(createResult1.phoneticGivenName, properties3.phoneticGivenName, "Same phoneticGivenName");
956 next();
957 }
958 req.onerror = onFailure;
959 },
960 function () {
961 ok(true, "Retrieving all contacts");
962 req = mozContacts.find({sortBy: "phoneticGivenName"});
963 req.onsuccess = function () {
964 is(req.result.length, 1, "1 Entries.");
965 next();
966 }
967 req.onerror = onFailure;
968 },
969 clearDatabase,
970 function () {
971 ok(true, "Test sorting");
972 createResult1 = new mozContact(c11);
973 req = navigator.mozContacts.save(createResult1);
974 req.onsuccess = function () {
975 ok(createResult1.id, "The contact now has an ID.");
976 checkContacts(c11, createResult1);
977 next();
978 };
979 req.onerror = onFailure;
980 },
981 function () {
982 ok(true, "Test sorting");
983 createResult1 = new mozContact(c10);
984 req = navigator.mozContacts.save(createResult1);
985 req.onsuccess = function () {
986 ok(createResult1.id, "The contact now has an ID.");
987 checkContacts(c10, createResult1);
988 next();
989 };
990 req.onerror = onFailure;
991 },
992 function () {
993 ok(true, "Test sorting");
994 createResult1 = new mozContact(c12);
995 req = navigator.mozContacts.save(createResult1);
996 req.onsuccess = function () {
997 ok(createResult1.id, "The contact now has an ID.");
998 checkContacts(c12, createResult1);
999 next();
1000 };
1001 req.onerror = onFailure;
1002 },
1003 function () {
1004 ok(true, "Test sorting");
1005 createResult1 = new mozContact(c9);
1006 req = navigator.mozContacts.save(createResult1);
1007 req.onsuccess = function () {
1008 ok(createResult1.id, "The contact now has an ID.");
1009 checkContacts(c9, createResult1);
1010 next();
1011 };
1012 req.onerror = onFailure;
1013 },
1014 function () {
1015 ok(true, "Test sorting");
1016 var options = {sortBy: "phoneticFamilyName",
1017 sortOrder: "ascending"};
1018 req = navigator.mozContacts.find(options);
1019 req.onsuccess = function () {
1020 is(req.result.length, 4, "4 results");
1021 checkContacts(req.result[0], c9);
1022 checkContacts(req.result[1], c10);
1023 checkContacts(req.result[2], c11);
1024 checkContacts(req.result[3], c12);
1025 next();
1026 };
1027 req.onerror = onFailure;
1028 },
1029 function () {
1030 ok(true, "Test sorting");
1031 var options = {sortBy: "phoneticFamilyName",
1032 sortOrder: "descending"};
1033 req = navigator.mozContacts.find(options);
1034 req.onsuccess = function () {
1035 is(req.result.length, 4, "4 results");
1036 checkContacts(req.result[0], c12);
1037 checkContacts(req.result[1], c11);
1038 checkContacts(req.result[2], c10);
1039 checkContacts(req.result[3], c9);
1040 next();
1041 };
1042 req.onerror = onFailure;
1043 },
1044 function () {
1045 ok(true, "Test sorting");
1046 createResult1 = new mozContact(c13);
1047 req = navigator.mozContacts.save(createResult1);
1048 req.onsuccess = function () {
1049 ok(createResult1.id, "The contact now has an ID.");
1050 checkContacts(c13, createResult1);
1051 next();
1052 };
1053 req.onerror = onFailure;
1054 },
1055 function () {
1056 ok(true, "Test sorting with empty string");
1057 var options = {sortBy: "phoneticFamilyName",
1058 sortOrder: "ascending"};
1059 req = navigator.mozContacts.find(options);
1060 req.onsuccess = function () {
1061 is(req.result.length, 5, "5 results");
1062 checkContacts(req.result[0], c13);
1063 checkContacts(req.result[1], c9);
1064 checkContacts(req.result[2], c10);
1065 checkContacts(req.result[3], c11);
1066 checkContacts(req.result[4], c12);
1067 next();
1068 };
1069 req.onerror = onFailure;
1070 },
1071 clearDatabase,
1072 function () {
1073 ok(true, "Test sorting");
1074 createResult1 = new mozContact(c15);
1075 req = navigator.mozContacts.save(createResult1);
1076 req.onsuccess = function () {
1077 ok(createResult1.id, "The contact now has an ID.");
1078 checkContacts(c15, createResult1);
1079 next();
1080 };
1081 req.onerror = onFailure;
1082 },
1083 function () {
1084 ok(true, "Test sorting");
1085 createResult1 = new mozContact(c14);
1086 req = navigator.mozContacts.save(createResult1);
1087 req.onsuccess = function () {
1088 ok(createResult1.id, "The contact now has an ID.");
1089 checkContacts(c14, createResult1);
1090 next();
1091 };
1092 req.onerror = onFailure;
1093 },
1094 function () {
1095 ok(true, "Test sorting");
1096 createResult1 = new mozContact(c16);
1097 req = navigator.mozContacts.save(createResult1);
1098 req.onsuccess = function () {
1099 ok(createResult1.id, "The contact now has an ID.");
1100 checkContacts(c16, createResult1);
1101 next();
1102 };
1103 req.onerror = onFailure;
1104 },
1105 function () {
1106 // Android does not support published/updated fields. Skip this.
1107 if (isAndroid) {
1108 next();
1109 return;
1110 }
1112 ok(true, "Test sorting with published");
1113 var options = {sortBy: "phoneticFamilyName",
1114 sortOrder: "descending"};
1115 req = navigator.mozContacts.find(options);
1116 req.onsuccess = function () {
1117 is(req.result.length, 3, "3 results");
1118 ok(req.result[0].published < req.result[1].published, "Right sorting order");
1119 ok(req.result[1].published < req.result[2].published, "Right sorting order");
1120 next();
1121 };
1122 req.onerror = onFailure;
1123 },
1124 clearDatabase,
1125 function () {
1126 ok(true, "all done!\n");
1127 SimpleTest.finish();
1128 }
1129 ];
1131 function next() {
1132 ok(true, "Begin!");
1133 if (index >= steps.length) {
1134 ok(false, "Shouldn't get here!");
1135 return;
1136 }
1137 try {
1138 var i = index++;
1139 steps[i]();
1140 } catch(ex) {
1141 ok(false, "Caught exception", ex);
1142 }
1143 }
1145 start_tests();
1146 </script>
1147 </pre>
1148 </body>
1149 </html>