1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
/*
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*
*/
/* jshint jasmine: true */
/* global WinJS */
exports.defineAutoTests = function() {
var isWindowsPhone8 = cordova.platformId == 'windowsphone';
var isWindows = (cordova.platformId === "windows") || (cordova.platformId === "windows8");
var isWindowsPhone81 = isWindows && WinJS.Utilities.isPhone;
// Error callback spies should not be called
var errorCallbacks = {};
errorCallbacks[ContactError.UNKNOWN_ERROR] = jasmine.createSpy('unknownErrorCallback');
errorCallbacks[ContactError.INVALID_ARGUMENT_ERROR] = jasmine.createSpy('invalidArgumentErrorCallback');
errorCallbacks[ContactError.TIMEOUT_ERROR] = jasmine.createSpy('timeoutErrorCallback');
errorCallbacks[ContactError.PENDING_OPERATION_ERROR] = jasmine.createSpy('pendingOperationErrorCallback');
errorCallbacks[ContactError.IO_ERROR] = jasmine.createSpy('ioErrorCallback');
errorCallbacks[ContactError.NOT_SUPPORTED_ERROR] = jasmine.createSpy('notSupportedErrorCallback');
errorCallbacks[ContactError.OPERATION_CANCELLED_ERROR] = jasmine.createSpy('operationCancelledErrorCallback');
errorCallbacks[ContactError.PERMISSION_DENIED_ERROR] = jasmine.createSpy('permissionDeniedErrorCallback');
var isIOSPermissionBlocked = false;
var fail = function(done) {
expect(true).toBe(false);
done();
};
var MEDIUM_TIMEOUT = 30000;
var HIGH_TIMEOUT = 120000;
var removeContact = function(done, contactObj) {
if (!contactObj) {
done();
return;
}
contactObj.remove(function() {
done();
}, function(contactError) {
if (contactError) {
if (errorCallbacks[contactError.code]) {
errorCallbacks[contactError.code]();
} else {
fail(done);
}
}
for (var error in errorCallbacks) {
expect(errorCallbacks[error]).not.toHaveBeenCalled();
}
done();
});
};
function removeContactsByFields(fields, filter, done) {
var obj = new ContactFindOptions();
obj.filter = filter;
obj.multiple = true;
navigator.contacts.find(fields, function(contacts) {
var removes = [];
contacts.forEach(function(contact) {
removes.push(contact);
});
if (removes.length === 0) {
done();
return;
}
var nextToRemove;
if (removes.length > 0) {
nextToRemove = removes.shift();
}
function removeNext(item) {
if (typeof item === 'undefined') {
done();
return;
}
if (removes.length > 0) {
nextToRemove = removes.shift();
} else {
nextToRemove = undefined;
}
item.remove(function removeSucceeded() {
removeNext(nextToRemove);
}, function removeFailed() {
removeNext(nextToRemove);
});
}
removeNext(nextToRemove);
}, done, obj);
}
describe("Contacts (navigator.contacts)", function() {
this.contactObj = null;
it("contacts.spec.1 should exist", function() {
expect(navigator.contacts).toBeDefined();
});
it("contacts.spec.2 should contain a find function", function() {
expect(navigator.contacts.find).toBeDefined();
expect(typeof navigator.contacts.find).toBe('function');
});
describe("find method", function() {
it("contacts.spec.3 success callback should be called with an array", function(done) {
// Find method is not supported on Windows platform
if (isWindows && !isWindowsPhone81) {
pending();
return;
}
var win = function(result) {
expect(result).toBeDefined();
expect(result instanceof Array).toBe(true);
done();
},
obj = new ContactFindOptions();
obj.filter = "";
obj.multiple = true;
function failed(err) {
if (err.code == ContactError.PERMISSION_DENIED_ERROR) {
isIOSPermissionBlocked = true;
done();
}
}
navigator.contacts.find(["displayName", "name", "phoneNumbers", "emails"], win, failed, obj);
}, HIGH_TIMEOUT); // give permission buster or a user a chance to accept the permission alert
it("contacts.spec.4 success callback should be called with an array, even if partial ContactFindOptions specified", function(done) {
// Find method is not supported on Windows platform
if ((isWindows && !isWindowsPhone81) || isIOSPermissionBlocked) {
pending();
return;
}
var win = function(result) {
expect(result).toBeDefined();
expect(result instanceof Array).toBe(true);
done();
};
navigator.contacts.find(["displayName", "name", "phoneNumbers", "emails"], win, fail.bind(null, done), {
multiple: true
});
});
it("contacts.spec.5 should throw an exception if success callback is empty", function() {
var obj = new ContactFindOptions();
obj.filter = "";
obj.multiple = true;
expect(function() {
navigator.contacts.find(["displayName", "name", "emails", "phoneNumbers"], null, function (err) {
expect(err).toBeUndefined();
}, obj);
}).toThrow();
});
it("contacts.spec.6 error callback should be called when no fields are specified", function(done) {
var win = fail,
// we don't want this to be called
error = function(result) {
expect(result).toBeDefined();
expect(result.code).toBe(ContactError.INVALID_ARGUMENT_ERROR);
done();
},
obj = new ContactFindOptions();
obj.filter = "";
obj.multiple = true;
navigator.contacts.find([], win, error, obj);
});
describe("with newly-created contact", function() {
afterEach(function (done) {
removeContact(done, this.contactObj);
});
it("contacts.spec.7 should be able to find a contact by name", function(done) {
// Find method is not supported on Windows Store apps.
// also this test will be skipped for Windows Phone 8.1 because function "save" not supported on WP8.1
if (isWindows || isWindowsPhone8 || isIOSPermissionBlocked) {
pending();
}
var specContext = this;
specContext.contactObj = new Contact();
specContext.contactObj.name = new ContactName();
specContext.contactObj.name.familyName = "Delete";
var foundName = function(result) {
var bFound = false;
try {
for (var i = 0; i < result.length; i++) {
if (result[i].name.familyName == "Delete") {
bFound = true;
break;
}
}
} catch (e) {
return false;
}
return bFound;
};
var test = function(savedContact) {
// update so contact will get removed
specContext.contactObj = savedContact;
// ----
// Find asserts
// ---
var findWin = function(object) {
expect(object instanceof Array).toBe(true);
expect(object.length >= 1).toBe(true);
expect(foundName(object)).toBe(true);
done();
},
findFail = fail,
obj = new ContactFindOptions();
obj.filter = "Delete";
obj.multiple = true;
navigator.contacts.find(["displayName", "name", "phoneNumbers", "emails"], findWin, findFail.bind(null, done), obj);
};
specContext.contactObj.save(test, fail.bind(null, done));
});
it("contacts.spec.7.1 should contain displayName if specified in desiredFields", function(done) {
if (isWindows || isWindowsPhone8 || isIOSPermissionBlocked) {
pending();
}
var testDisplayName = "testContact";
var specContext = this;
specContext.contactObj = new Contact();
specContext.contactObj.displayName = testDisplayName;
var win = function(contactResult) {
expect(contactResult.length > 0).toBe(true);
var namesDisplayed = contactResult.every(function(contact, index) {
return contact.displayName !== null;
});
expect(namesDisplayed).toBe(true);
done();
};
var onSuccessSave = function(savedContact) {
specContext.contactObj = savedContact;
var options = new ContactFindOptions();
options.filter = testDisplayName;
options.multiple = true;
options.desiredFields = [navigator.contacts.fieldType.displayName];
navigator.contacts.find(["displayName", "nickname"], win, fail.bind(null, done), options);
};
specContext.contactObj.save(onSuccessSave, fail.bind(null, done));
});
it("contacts.spec.7.2 should find contact despite id isn't string ", function(done) {
if (isWindows || isWindowsPhone8 || isIOSPermissionBlocked) {
pending();
}
var testDisplayName = "testContact";
var specContext = this;
specContext.contactObj = new Contact();
specContext.contactObj.displayName = testDisplayName;
var win = function(contactResult) {
expect(contactResult.length > 0).toBe(true);
done();
};
var onSuccessSave = function(savedContact) {
specContext.contactObj = savedContact;
var options = new ContactFindOptions();
options.filter = savedContact.id;
options.multiple = true;
navigator.contacts.find(["id"], win, fail.bind(null, done), options);
};
specContext.contactObj.save(onSuccessSave, fail.bind(null, done));
});
it("contacts.spec.7.3 should contain custom label in type", function(done) {
if (isWindows || isWindowsPhone8 || isIOSPermissionBlocked) {
pending();
}
var testDisplayName = "testContact";
var customLabel = "myType";
var testContactDetail = new ContactField(customLabel, "a", true);
var contactFields = ["phoneNumbers", "emails", "urls", "ims"];
var specContext = this;
specContext.contactObj = new Contact();
specContext.contactObj.nickname = testDisplayName;
specContext.contactObj.displayName = testDisplayName;
contactFields.forEach(function(contactField) {
specContext.contactObj[contactField] = [];
specContext.contactObj[contactField][0] = testContactDetail;
});
specContext.contactObj.addresses = [];
specContext.contactObj.addresses[0] = new ContactAddress(true, customLabel, "a", "b", "c", "d", "e", "f");
var checkTypes = function(contact) {
var allFieldsWithCustomLabel = contactFields.concat(["addresses"]);
return allFieldsWithCustomLabel.every(function(contactField) {
return contact[contactField] && contact[contactField][0].type === customLabel;
});
};
var win = function(contactResult) {
expect(contactResult.length > 0).toBe(true);
var typesCustomized = contactResult.every(function(contact) {
return checkTypes(contact);
});
expect(typesCustomized).toBe(true);
done();
};
var onSuccessSave = function(savedContact) {
expect(checkTypes(savedContact)).toBe(true);
specContext.contactObj = savedContact;
var options = new ContactFindOptions();
options.filter = testDisplayName;
options.multiple = true;
navigator.contacts.find(["displayName", "nickname"], win, fail.bind(null, done), options);
};
specContext.contactObj.save(onSuccessSave, fail.bind(null, done));
});
it('spec 7.4 contact detail type should equal default label', function(done) {
if (isWindows || isWindowsPhone8 || isIOSPermissionBlocked) {
pending();
}
var specContext = this;
specContext.contactObj = navigator.contacts.create({
"displayName": "test name",
"ims": [{
"type": "SKYPE",
"value": "000"
}]
});
specContext.contactObj.save(onSuccessSave, fail.bind(null, done));
function onSuccessSave(savedContact) {
specContext.contactObj = savedContact;
var imsType = savedContact.ims[0].type;
var expectedType = (cordova.platformId == 'android') ? "Skype" : "skype";
expect(imsType).toBe(expectedType);
done();
}
});
});
});
describe('create method', function() {
it("contacts.spec.8 should exist", function() {
expect(navigator.contacts.create).toBeDefined();
expect(typeof navigator.contacts.create).toBe('function');
});
it("contacts.spec.9 should return a Contact object", function() {
var bDay = new Date(1976, 7, 4);
var obj = navigator.contacts.create({
"displayName": "test name",
"gender": "male",
"note": "my note",
"name": {
"formatted": "Mr. Test Name"
},
"emails": [{
"value": "here@there.com"
}, {
"value": "there@here.com"
}],
"birthday": bDay
});
expect(obj).toBeDefined();
expect(obj.displayName).toBe('test name');
expect(obj.note).toBe('my note');
expect(obj.name.formatted).toBe('Mr. Test Name');
expect(obj.emails.length).toBe(2);
expect(obj.emails[0].value).toBe('here@there.com');
expect(obj.emails[1].value).toBe('there@here.com');
expect(obj.nickname).toBe(null);
expect(obj.birthday).toBe(bDay);
});
});
describe("Contact object", function() {
it("contacts.spec.10 should be able to create instance", function() {
var contact = new Contact("a", "b", new ContactName("a", "b", "c", "d", "e", "f"), "c", [], [], [], [], [], "f", "i", [], [], []);
expect(contact).toBeDefined();
expect(contact.id).toBe("a");
expect(contact.displayName).toBe("b");
expect(contact.name.formatted).toBe("a");
expect(contact.nickname).toBe("c");
expect(contact.phoneNumbers).toBeDefined();
expect(contact.emails).toBeDefined();
expect(contact.addresses).toBeDefined();
expect(contact.ims).toBeDefined();
expect(contact.organizations).toBeDefined();
expect(contact.birthday).toBe("f");
expect(contact.note).toBe("i");
expect(contact.photos).toBeDefined();
expect(contact.categories).toBeDefined();
expect(contact.urls).toBeDefined();
});
it("contacts.spec.11 should be able to define a ContactName object", function() {
var contactName = new ContactName("Dr. First Last Jr.", "Last", "First", "Middle", "Dr.", "Jr.");
expect(contactName).toBeDefined();
expect(contactName.formatted).toBe("Dr. First Last Jr.");
expect(contactName.familyName).toBe("Last");
expect(contactName.givenName).toBe("First");
expect(contactName.middleName).toBe("Middle");
expect(contactName.honorificPrefix).toBe("Dr.");
expect(contactName.honorificSuffix).toBe("Jr.");
});
it("contacts.spec.12 should be able to define a ContactField object", function() {
var contactField = new ContactField("home", "8005551212", true);
expect(contactField).toBeDefined();
expect(contactField.type).toBe("home");
expect(contactField.value).toBe("8005551212");
expect(contactField.pref).toBe(true);
});
it("contacts.spec.13 ContactField object should coerce type and value properties to strings", function() {
var contactField = new ContactField(12345678, 12345678, true);
expect(contactField.type).toBe("12345678");
expect(contactField.value).toBe("12345678");
});
it("contacts.spec.14 should be able to define a ContactAddress object", function() {
var contactAddress = new ContactAddress(true, "home", "a", "b", "c", "d", "e", "f");
expect(contactAddress).toBeDefined();
expect(contactAddress.pref).toBe(true);
expect(contactAddress.type).toBe("home");
expect(contactAddress.formatted).toBe("a");
expect(contactAddress.streetAddress).toBe("b");
expect(contactAddress.locality).toBe("c");
expect(contactAddress.region).toBe("d");
expect(contactAddress.postalCode).toBe("e");
expect(contactAddress.country).toBe("f");
});
it("contacts.spec.15 should be able to define a ContactOrganization object", function() {
var contactOrg = new ContactOrganization(true, "home", "a", "b", "c", "d", "e", "f", "g");
expect(contactOrg).toBeDefined();
expect(contactOrg.pref).toBe(true);
expect(contactOrg.type).toBe("home");
expect(contactOrg.name).toBe("a");
expect(contactOrg.department).toBe("b");
expect(contactOrg.title).toBe("c");
});
it("contacts.spec.16 should be able to define a ContactFindOptions object", function() {
var contactFindOptions = new ContactFindOptions("a", true, "b");
expect(contactFindOptions).toBeDefined();
expect(contactFindOptions.filter).toBe("a");
expect(contactFindOptions.multiple).toBe(true);
});
it("contacts.spec.17 should contain a clone function", function() {
var contact = new Contact();
expect(contact.clone).toBeDefined();
expect(typeof contact.clone).toBe('function');
});
it("contacts.spec.18 clone function should make deep copy of Contact Object", function() {
var contact = new Contact();
contact.id = 1;
contact.displayName = "Test Name";
contact.nickname = "Testy";
contact.gender = "male";
contact.note = "note to be cloned";
contact.name = new ContactName("Mr. Test Name");
var clonedContact = contact.clone();
expect(contact.id).toBe(1);
expect(clonedContact.id).toBe(null);
expect(clonedContact.displayName).toBe(contact.displayName);
expect(clonedContact.nickname).toBe(contact.nickname);
expect(clonedContact.gender).toBe(contact.gender);
expect(clonedContact.note).toBe(contact.note);
expect(clonedContact.name.formatted).toBe(contact.name.formatted);
expect(clonedContact.connected).toBe(contact.connected);
});
it("contacts.spec.19 should contain a save function", function() {
var contact = new Contact();
expect(contact.save).toBeDefined();
expect(typeof contact.save).toBe('function');
});
it("contacts.spec.20 should contain a remove function", function() {
var contact = new Contact();
expect(contact.remove).toBeDefined();
expect(typeof contact.remove).toBe('function');
});
});
describe('save method', function() {
afterEach(function (done) {
removeContact(done, this.contactObj);
});
it("contacts.spec.21 should be able to save a contact", function(done) {
// Save method is not supported on Windows platform
if (isWindows || isWindowsPhone8 || isIOSPermissionBlocked) {
pending();
}
var specContext = this;
var bDay = new Date(1976, 6, 4);
var obj = {
"gender": "male",
"note": "my note",
"name": {
"familyName": "Delete",
"givenName": "Test"
},
"emails": [{
"value": "here@there.com"
}, {
"value": "there@here.com"
}],
"birthday": bDay
};
var saveSuccess = function(obj) {
specContext.contactObj = obj;
expect(obj).toBeDefined();
expect(obj.note).toBe('my note');
expect(obj.name.familyName).toBe('Delete');
expect(obj.name.givenName).toBe('Test');
expect(obj.emails.length).toBe(2);
expect(obj.emails[0].value).toBe('here@there.com');
expect(obj.emails[1].value).toBe('there@here.com');
expect(obj.birthday.toDateString()).toBe(bDay.toDateString());
expect(obj.addresses).toBe(null);
done();
};
var saveFail = fail.bind(null, done);
navigator.contacts
.create(obj)
.save(saveSuccess, saveFail);
});
it("contacts.spec.22 update a contact", function(done) {
// Save method is not supported on Windows platform
if (isWindows || isWindowsPhone8 || isIOSPermissionBlocked) {
pending();
}
var specContext = this;
var aDay = new Date(1976, 6, 4);
var bDay;
var noteText = "an UPDATED note";
var savedContact;
var contact = {
"gender": "male",
"note": "my note",
"name": {
"familyName": "Delete",
"givenName": "Test"
},
"emails": [{
"value": "here@there.com"
}, {
"value": "there@here.com"
}],
"birthday": aDay
};
var saveFail = fail.bind(null, done);
function updateSuccess(obj) {
specContext.contactObj = obj;
expect(obj).toBeDefined();
expect(obj.id).toBe(savedContact.id);
expect(obj.note).toBe(noteText);
expect(obj.birthday.toDateString()).toBe(bDay.toDateString());
expect(obj.emails.length).toBe(1);
expect(obj.emails[0].value).toBe('here@there.com');
done();
}
var saveSuccess = function(newContact) {
specContext.contactObj = newContact;
savedContact = newContact;
newContact.emails[1].value = "";
bDay = new Date(1975, 5, 4);
newContact.birthday = bDay;
newContact.note = noteText;
newContact.save(updateSuccess, saveFail);
};
navigator.contacts
.create(contact)
.save(saveSuccess, saveFail);
}, HIGH_TIMEOUT);
});
describe('Contact.remove method', function() {
afterEach(function (done) {
removeContact(done, this.contactObj);
});
it("contacts.spec.23 calling remove on a contact that has an id of null should return ContactError.UNKNOWN_ERROR", function(done) {
var expectedFail = function(result) {
expect(result.code).toBe(ContactError.UNKNOWN_ERROR);
done();
};
var rmContact = new Contact();
rmContact.remove(fail.bind(null, done), expectedFail);
});
it("contacts.spec.24 calling remove on a contact that does not exist should return ContactError.UNKNOWN_ERROR", function(done) {
// remove method is not supported on Windows platform
if (isWindows || isWindowsPhone8 || isIOSPermissionBlocked) {
pending();
}
var rmWin = fail.bind(null, done);
var rmFail = function(result) {
expect(result.code).toBe(ContactError.UNKNOWN_ERROR);
done();
};
// this is a bit risky as some devices may have contact ids that large
var contact = new Contact("this string is supposed to be a unique identifier that will never show up on a device");
contact.remove(rmWin, rmFail);
}, MEDIUM_TIMEOUT);
});
describe("Round trip Contact tests (creating + save + delete + find)", function() {
var saveAndFindBy = function (contact, fields, filter, callback, specContext) {
removeContactsByFields(["note"], "DeleteMe", function() {
contact.save(function(c_obj) {
specContext.contactObj = c_obj;
var findWin = function(cs) {
expect(cs.length).toBe(1);
specContext.contactObj = cs[0];
callback(cs[0]);
};
var findFail = fail;
var obj = new ContactFindOptions();
obj.filter = filter;
obj.multiple = true;
navigator.contacts.find(fields, findWin, findFail, obj);
}, fail);
});
};
afterEach(function (done) {
removeContact(done, this.contactObj);
});
it("contacts.spec.25 Creating, saving, finding a contact should work", function(done) {
// Save method is not supported on Windows platform
if (isWindows || isWindowsPhone8 || isIOSPermissionBlocked) {
pending();
}
var contactName = "DeleteMe";
var contact = new Contact();
contact.name = new ContactName();
contact.name.familyName = contactName;
contact.note = "DeleteMe";
saveAndFindBy(contact, ["displayName", "name"], contactName, function() {
done();
}, this);
}, MEDIUM_TIMEOUT);
it("contacts.spec.26 Creating, saving, finding a contact should work, removing it should work", function(done) {
// Save method is not supported on Windows platform
var specContext = this;
if (isWindows || isWindowsPhone8 || isIOSPermissionBlocked) {
pending();
}
var contactName = "DeleteMe";
var contact = new Contact();
contact.name = new ContactName();
contact.name.familyName = contactName;
contact.note = "DeleteMe";
saveAndFindBy(contact, ["displayName", "name"], contactName, function(savedContact) {
savedContact.remove(function() {
specContext.contactObj = null;
done();
}, function(e) {
throw ("Newly created contact's remove function invoked error callback. Test failed: " + JSON.stringify(e));
});
}, this);
}, MEDIUM_TIMEOUT);
it("contacts.spec.27 Should not be able to delete the same contact twice", function(done) {
// Save method is not supported on Windows platform
if (isWindows || isWindowsPhone8 || isIOSPermissionBlocked) {
pending();
}
var specContext = this;
var contactName = "DeleteMe2";
var contact = new Contact();
contact.name = new ContactName();
contact.name.familyName = contactName;
contact.note = "DeleteMe2";
saveAndFindBy(contact, ["displayName", "name"], contactName, function(savedContact) {
savedContact.remove(function() {
specContext.contactObj = null;
var findWin = function(seas) {
expect(seas.length).toBe(0);
savedContact.remove(function(e) {
throw ("Success callback called after non-existent Contact object called remove(). Test failed: " + JSON.stringify(e));
}, function(e) {
expect(e.code).toBe(ContactError.UNKNOWN_ERROR);
done();
});
};
var obj = new ContactFindOptions();
obj.filter = contactName;
obj.multiple = true;
navigator.contacts.find(["displayName", "name", "phoneNumbers", "emails"], findWin, fail, obj);
}, fail);
}, this);
}, MEDIUM_TIMEOUT);
it("contacts.spec.28 should find a contact with unicode name", function (done) {
// Save method is not supported on Windows platform
if (isWindows || isWindowsPhone8) {
pending();
}
var contactName = "\u2602";
var contact = new Contact();
contact.note = "DeleteMe";
contact.name = new ContactName();
contact.name.familyName = contactName;
saveAndFindBy(contact, ["displayName", "name"], contactName, function() {
done();
}, this);
}, MEDIUM_TIMEOUT);
it("contacts.spec.29 should find a contact without a name", function (done) {
// Save method is not supported on Windows platform
if (isWindows || isWindowsPhone8) {
pending();
}
var contact = new Contact();
var phoneNumbers = [1];
phoneNumbers[0] = new ContactField('work', '555-555-1234', true);
contact.phoneNumbers = phoneNumbers;
saveAndFindBy(contact, ["phoneNumbers"], "555-555-1234", function() {
done();
}, this);
}, MEDIUM_TIMEOUT);
it("contacts.spec.31 Find should return a contact with correct birthday field type", function(done) {
// Save method is not supported on Windows platform
if (isWindows || isWindowsPhone8 || isIOSPermissionBlocked) {
pending();
}
var contactName = "DeleteMe";
var bDay = new Date(1976, 7, 4);
var contact = new Contact();
contact.name = new ContactName();
contact.name.familyName = contactName;
contact.note = "DeleteMe";
contact.birthday = bDay;
saveAndFindBy(contact, ["displayName", "name"], contactName, function(found) {
expect(found.birthday).toEqual(jasmine.any(Date));
expect(found.birthday).toEqual(bDay);
done();
}, this);
}, MEDIUM_TIMEOUT);
it("contacts.spec.32 Find should return a contact with correct IM field", function(done) {
// Save method is not supported on Windows platform
if (isWindows || isWindowsPhone8 || isIOSPermissionBlocked) {
pending();
}
var contactName = "DeleteMe";
var ims = [{
type: "Skype",
value: "imValue"
}];
var contact = new Contact();
contact.name = new ContactName();
contact.name.familyName = contactName;
contact.note = "DeleteMe";
contact.ims = ims;
saveAndFindBy(contact, ["displayName", "name"], contactName, function(found) {
expect(found.ims).toEqual(jasmine.any(Array));
expect(found.ims[0]).toBeDefined();
if (found.ims[0]) {
expect(found.ims[0].type).toEqual(cordova.platformId == 'android' ? ims[0].type : ims[0].type.toLowerCase());
expect(found.ims[0].value).toEqual(ims[0].value);
}
done();
}, this);
}, MEDIUM_TIMEOUT);
});
describe('ContactError interface', function() {
it("contacts.spec.30 ContactError constants should be defined", function() {
expect(ContactError.UNKNOWN_ERROR).toBe(0);
expect(ContactError.INVALID_ARGUMENT_ERROR).toBe(1);
expect(ContactError.TIMEOUT_ERROR).toBe(2);
expect(ContactError.PENDING_OPERATION_ERROR).toBe(3);
expect(ContactError.IO_ERROR).toBe(4);
expect(ContactError.NOT_SUPPORTED_ERROR).toBe(5);
expect(ContactError.OPERATION_CANCELLED_ERROR).toBe(6);
expect(ContactError.PERMISSION_DENIED_ERROR).toBe(20);
});
});
});
};
/******************************************************************************/
/******************************************************************************/
/******************************************************************************/
exports.defineManualTests = function(contentEl, createActionButton) {
function getContacts(filter) {
var results = document.getElementById('contact_results');
var obj = new ContactFindOptions();
if (filter) {
obj.filter = filter;
}
obj.multiple = true;
navigator.contacts.find(["displayName", "name", "phoneNumbers", "emails", "urls", "note"], function(contacts) {
var s = "";
if (contacts.length === 0) {
s = "No contacts found";
} else {
s = "Number of contacts: " + contacts.length + "<br><table width='100%'><tr><th>Name</th><td>Phone</td><td>Email</td></tr>";
for (var i = 0; i < contacts.length; i++) {
var contact = contacts[i];
var contactNameTag = contact.name ? "<tr><td>" + contact.name.formatted + "</td><td>" : "<tr><td>(No Name)</td><td>";
s = s + contactNameTag;
if (contact.phoneNumbers && contact.phoneNumbers.length > 0) {
s = s + contact.phoneNumbers[0].value;
}
s = s + "</td><td>";
if (contact.emails && contact.emails.length > 0) {
s = s + contact.emails[0].value;
}
s = s + "</td></tr>";
}
s = s + "</table>";
}
results.innerHTML = s;
}, function(e) {
if (e.code === ContactError.NOT_SUPPORTED_ERROR) {
results.innerHTML = "Searching for contacts is not supported.";
} else {
results.innerHTML = "Search failed: error " + e.code;
}
}, obj);
}
function filterContacts() {
var filter = document.getElementById('searchstring');
getContacts(filter.value);
}
function pickContact() {
var results = document.getElementById('contact_results');
navigator.contacts.pickContact(
function (contact) {
results.innerHTML = contact ?
"Picked contact: <pre>" + JSON.stringify(contact, null, 4) + "</pre>" :
"No contacts found";
},
function (e) {
results.innerHTML = (e && e.code === ContactError.NOT_SUPPORTED_ERROR) ?
"Searching for contacts is not supported." :
(e && e.code === ContactError.OPERATION_CANCELLED_ERROR) ?
"Pick cancelled" :
"Pick failed: error " + (e && e.code);
}
);
}
function addContact(displayName, name, phoneNumber, birthday) {
try {
var results = document.getElementById('contact_results');
var contact = navigator.contacts.create({ "displayName": displayName, "name": name, "birthday": birthday, "note": "DeleteMe" });
var phoneNumbers = [1];
phoneNumbers[0] = new ContactField('work', phoneNumber, true);
contact.phoneNumbers = phoneNumbers;
contact.save(function() {
results.innerHTML = (displayName || "Nameless contact") + " saved.";
}, function(e) {
if (e.code === ContactError.NOT_SUPPORTED_ERROR) {
results.innerHTML = "Saving contacts not supported.";
} else {
results.innerHTML = "Contact save failed: error " + e.code;
}
});
} catch (e) {
console.error(e.message);
}
}
function addDooneyEvans() {
var displayName = "Dooney Evans";
var contactName = {
formatted: "Dooney Evans",
familyName: "Evans",
givenName: "Dooney",
middleName: ""
};
var phoneNumber = '512-555-1234';
var birthday = new Date(1985, 0, 23);
addContact(displayName, contactName, phoneNumber, birthday);
}
function addNamelessContact() {
addContact();
}
function addUnicodeContact() {
var displayName = "Н€йромонах \nФеофаЊ";
var contactName = {
formatted: "Н€йромонах \nФеофаЊ",
familyName: "\nФеофаЊ",
givenName: "Н€йромонах",
middleName: ""
};
addContact(displayName, contactName);
}
function renameDooneyEvans() {
var results = document.getElementById('contact_results');
var obj = new ContactFindOptions();
obj.filter = 'Dooney Evans';
obj.multiple = false;
navigator.contacts.find(['displayName', 'name'], function(contacts) {
if (contacts.length === 0) {
results.innerHTML = 'No contacts to update.';
return;
}
var contact = contacts[0];
contact.displayName = "Urist McContact";
var name = new ContactName();
name.givenName = "Urist";
name.familyName = "McContact";
contact.name = name;
contact.save(function(updated) {
results.innerHTML = 'Contact updated.';
},function(e) {
results.innerHTML = 'Update failed: error ' + e.code;
});
}, function(e) {
if (e.code === ContactError.NOT_SUPPORTED_ERROR) {
results.innerHTML = 'Searching for contacts is not supported.';
} else {
results.innerHTML = 'Search failed: error ' + e.code;
}
}, obj);
}
function removeTestContacts() {
var results = document.getElementById('contact_results');
results.innerHTML = "";
var obj = new ContactFindOptions();
obj.filter = 'DeleteMe';
obj.multiple = true;
navigator.contacts.find(['note'], function(contacts) {
var removes = [];
contacts.forEach(function(contact) {
removes.push(contact);
});
if (removes.length === 0) {
results.innerHTML = "No contacts to remove";
return;
}
var nextToRemove;
if (removes.length > 0) {
nextToRemove = removes.shift();
}
function removeNext(item) {
if (typeof item === 'undefined') {
return;
}
if (removes.length > 0) {
nextToRemove = removes.shift();
} else {
nextToRemove = undefined;
}
item.remove(function removeSucceeded() {
results.innerHTML += "Removed a contact with ID " + item.id + "<br/>";
removeNext(nextToRemove);
}, function removeFailed() {
results.innerHTML += "Failed to remove a contact with ID " + item.id + "<br/>";
removeNext(nextToRemove);
});
}
removeNext(nextToRemove);
}, function(e) {
if (e.code === ContactError.NOT_SUPPORTED_ERROR) {
results.innerHTML = 'Searching for contacts is not supported.';
} else {
results.innerHTML = 'Search failed: error ' + e.code;
}
}, obj);
}
/******************************************************************************/
contentEl.innerHTML = '<div id="info">' +
'<b>Results:</b><br>' +
'<div id="contact_results"></div>' +
'</div>' +
'<div id="get_contacts"></div>' +
'<p>Expected result: Status box will show number of contacts and list them. May be empty on a fresh device until you click Add.</p>' +
'<div id="filter_contacts">Search: <input type="text" id="searchstring"></div>' +
'<p>Expected result: Will return only contacts which contain specified string</p>' +
'<div id="pick_contact"></div>' +
'<p>Expected result: Device\'s address book will be shown. After picking a contact status box will show Contact object, passed to success callback</p>' +
'<div id="add_contact"></div>' +
'<p>Expected result: Will add a new contact. Log will say "Contact saved." or "Saving contacts not supported." if not supported on current platform. Verify by running Get phone contacts again</p>' +
'<div id="update_contact"></div>' +
'<p>Expected result: Will rename "Dooney Evans" to "Urist McContact".</p>' +
'<div id="remove_contacts"></div>' +
'<p>Expected result: Will remove all contacts created by these tests. Log will output success or failure and ID of the deleted contacts.</p>';
createActionButton("Get phone's contacts", function() {
getContacts();
}, 'get_contacts');
createActionButton("Filter contacts", function() {
filterContacts();
}, 'filter_contacts');
createActionButton("Pick contact", function() {
pickContact();
}, 'pick_contact');
createActionButton("Add a new contact 'Dooney Evans'", function() {
addDooneyEvans();
}, 'add_contact');
createActionButton("Add new nameless contact", function() {
addNamelessContact();
}, 'add_contact');
createActionButton("Add new unicode contact", function() {
addUnicodeContact();
}, 'add_contact');
createActionButton("Rename 'Dooney Evans'", function() {
renameDooneyEvans();
}, 'update_contact');
createActionButton("Delete all test contacts", function() {
removeTestContacts();
}, 'remove_contacts');
};