5 <style type="text/css">
6 body { margin: auto; width: 100%; }
7 div { margin: 15; padding: 10; width: 100%; }
8 textarea { margin: 10; padding: 10; width: 95%; height: 15%; font-size: 12; resize: none; }
9 .button { margin: 10; padding: 10; width: 80%; height: 5%; font-size: 40; }
10 .title { margin: 20; padding: 20; font-size: 42; font-weight: bold; }
11 .subtitle { margin: 10; padding: 10; font-size: 32; }
12 .text { font-size: 25; }
13 .center { text-align: center; }
14 .save { background-color: #FFFACD; }
15 .update { background-color: #CAE1FF; }
16 .find { background-color: #FFF0F5; }
17 .remove { background-color: #F0FFF0; }
18 .event { background-color: #D1D1D1; }
25 // 1. Save - by saving two new contacts and check by onchangechange event.
26 // 2. Contact change event - by checking if all new added IDs are received.
27 // 3. Update - by updating first contact.
28 // 4. Find - by looking for the updated contact and compare with expected result.
29 // 5. Remove - by removing the second contact.
30 // 6. Clear - by clearing all contacts and check if all contacts are removed.
32 window.onload = function() {
33 var contacts = navigator.contacts || xwalk.experimental.contacts;
35 contacts.addEventListener('contactschange', function(data) {
36 var output = document.getElementById('event');
37 output.value += 'contacts changes:\n';
38 data.added && logEventListener('added: ', data.added, output);
39 data.removed && logEventListener('removed: ', data.removed, output);
40 data.modified && logEventListener('modified: ', data.modified, output);
41 output.value += '-----------\n';
42 output.scrollTop = output.scrollHeight;
46 function setHTML(id, str) { document.getElementById(id).innerHTML = str; }
47 function addHTML(id, str) { document.getElementById(id).innerHTML += str+'<br>'; }
48 function ifcontains(a, e) { return a.indexOf(e) != -1; }
49 function logContactSaved(contact) {
50 addHTML('save', 'Contact ' + contact.id + ' ' + contact.name.givenNames[0] + ' ' + contact.name.familyNames[0] + ' saved!');
53 function errorCallback(error) {
54 document.title = 'Fail';
59 var testNewContactFlag = false;
60 var expectedNewPhoneNumber = '+4455001';
61 function testNewContacts() {
62 // Test Case 2: check if oncontactchanges received all new contacts IDs.
63 if (addedIDs.length != 2) {
67 if (ifcontains(addedIDs, contact1.id) && ifcontains(addedIDs, contact2.id)) {
68 testNewContactFlag = true;
71 // Test Case 3: update first contact after both contacts are saved.
72 var output = 'Will update contact ' +
74 contact1.name.givenNames[0] + ' ' +
75 contact1.name.familyNames[0] + ': phone from ' +
76 contact1.phoneNumbers[0].value + ' to ' + expectedNewPhoneNumber;
77 addHTML('update', output);
79 contact1.phoneNumbers[0].value = expectedNewPhoneNumber;
80 contacts.save(contact1).then(updateCallback, errorCallback);
84 contacts.oncontactschange = function(data) {
86 addedIDs.push.apply(addedIDs, data.added); // Append added IDs to addedIDs
90 removedIDs.push.apply(removedIDs, data.removed); // Append removed IDs to removedIDs
95 // Create two contacts for testing
96 var givenName = 'John';
98 var contactName1 = new ContactName({
99 givenNames: [givenName],
103 var contactName2 = new ContactName({
104 givenNames: [givenName],
105 familyNames: ['Smith']
108 var mobilePhone1 = new ContactTelField({
111 value: '+01234567890'
114 var mobilePhone2 = new ContactTelField({
117 value: '+11234567890'
120 // Test Case 1: Save two new contacts
121 var contact1 = new Contact({
123 phoneNumbers: [mobilePhone1]
126 var contact2 = new Contact({
128 phoneNumbers: [mobilePhone2]
131 contacts.save(contact1).then(function(item) { logContactSaved(item); contact1.id = item.id; }, errorCallback);
132 contacts.save(contact2).then(function(item) { logContactSaved(item); contact2.id = item.id; }, errorCallback);
134 var testUpdateFirstFlag = false;
135 function updateCallback(item) {
136 var output = '<p>Contact ' +
138 item.name.givenNames[0] + ' ' +
139 item.name.familyNames[0] + ' updated:<br>phone now is: ' +
140 item.phoneNumbers[0].value;
141 addHTML('update', output);
142 testUpdateFirstFlag = (item.phoneNumbers[0].value == expectedNewPhoneNumber);
144 // Test Case 4: find first contact after it is updated.
145 var ContactFindOptions = function(init) {
146 this.value = init.value;
147 this.operator = init.operator;
148 this.fields = init.fields;
149 this.sortBy = init.sortBy;
150 this.sortOrder = init.sortOrder;
151 this.resultsLimit = init.resultsLimit;
153 var options = new ContactFindOptions({
154 value: contact1.name.familyNames[0],
155 operator: 'contains',
156 fields: ['familyName'],
157 sortBy: ['familyNames'],
158 sortOrder: 'ascending',
161 contacts.find(options).then(findCallback, errorCallback);
164 var testFindFirstFlag = false;
165 function findCallback(items) {
166 testFindFirstFlag = (items[0].name.familyNames[0] == contact1.name.familyNames[0]);
167 var output = 'Found '+items.length + ' contact(s):';
169 for (var i = 0, l = items.length; i < l; ++i) {
170 output += items[i].name.givenNames[0] + ' ' + items[i].name.familyNames[0] + '<br>';
172 setHTML('find', output);
174 // Test Case 5: remove specific contact.
175 contacts.remove(contact2.id).then(removeCallback, errorCallback);
176 addHTML('remove', "Will remove contact " + contact2.id);
179 function removeCallback() {
180 addHTML('remove', contact2.id + " is removed.<br>Will test clear()");
181 // Test Case 6: clear all contacts.
182 contacts.clear().then(tryFinalVerify, errorCallback);
185 // Check if contacts are all removed, which means all test steps are done. If so we verify all results.
186 function tryFinalVerify() {
187 if (removedIDs.length != 2) {
190 addHTML('remove', "All contacts are removed.");
191 testRemovedSecondFlag = ifcontains(removedIDs, contact2.id);
192 if (testNewContactFlag && testUpdateFirstFlag && testFindFirstFlag && testRemovedSecondFlag) {
193 document.title = 'Pass';
195 document.title = 'Fail';
199 function logEventListener(msg, data, output) {
200 for (var i = 0; i < data.length; ++i) {
201 msg += data[i] + '|';
203 output.value += msg + '\n';
210 <div class="title center">Contacts API Example</div>
213 <p class="subtitle center">Save Contact</p>
214 <p class="text center" id="save"></p>
218 <p class="subtitle center">Update Contact</p>
219 <p class="text center" id="update"></p>
223 <p class="subtitle center">Find Contact</p>
224 <p class="text center" id="find"></p>
228 <p class="subtitle center">Remove Contact</p>
229 <p class="text center" id="remove"></p>
233 <p class="subtitle center">Contact Event</p>
234 <textarea id="event" readonly=true></textarea>