|
| 1 | +// META: script=/resources/testdriver.js |
| 2 | +// META: script=/resources/testdriver-vendor.js |
| 3 | +// META: script=resources/helpers.js |
| 4 | +'use strict'; |
| 5 | + |
| 6 | +// Verifies that |func|, when invoked, throws a TypeError exception. |
| 7 | +async function expectTypeError(func) { |
| 8 | + try { |
| 9 | + await func(); |
| 10 | + } catch (e) { |
| 11 | + assert_equals(e.name, 'TypeError'); |
| 12 | + return; |
| 13 | + } |
| 14 | + |
| 15 | + assert_unreached('expected a TypeError, but none was thrown'); |
| 16 | +} |
| 17 | + |
| 18 | +promise_test(async () => { |
| 19 | + try { |
| 20 | + await navigator.contacts.select(['name']); |
| 21 | + assert_unreached('expected a SecurityError, but none was thrown'); |
| 22 | + } catch (e) { |
| 23 | + assert_equals(e.name, 'SecurityError'); |
| 24 | + } |
| 25 | +}, 'The Contact API requires a user gesture') |
| 26 | + |
| 27 | +contactsTestWithUserActivation(async (test, setSelectedContacts) => { |
| 28 | + // At least one property must be provided. |
| 29 | + await expectTypeError(() => navigator.contacts.select()); |
| 30 | + await expectTypeError(() => navigator.contacts.select([])); |
| 31 | + |
| 32 | + // Per WebIDL parsing, no invalid values may be provided. |
| 33 | + await expectTypeError(() => |
| 34 | + navigator.contacts.select([''])); |
| 35 | + await expectTypeError(() => |
| 36 | + navigator.contacts.select(['foo'])); |
| 37 | + await expectTypeError(() => |
| 38 | + navigator.contacts.select(['name', 'photo'])); |
| 39 | + |
| 40 | +}, 'The Contact API requires valid properties to be provided'); |
| 41 | + |
| 42 | +contactsTestWithUserActivation(async (test, setSelectedContacts) => { |
| 43 | + // Returns a NULL result, indicating that no results are available. |
| 44 | + setSelectedContacts(null); |
| 45 | + |
| 46 | + await expectTypeError(() => navigator.contacts.select(['name'])); |
| 47 | + |
| 48 | +}, 'The Contact API can fail when the selector cannot be opened'); |
| 49 | + |
| 50 | +contactsTestWithUserActivation(async (test, setSelectedContacts) => { |
| 51 | + // Returns two contacts with all information available. |
| 52 | + setSelectedContacts([ |
| 53 | + { name: ['Dwight Schrute'], email: ['[email protected]'], tel: ['000-0000'] }, |
| 54 | + { name: ['Michael Scott', 'Prison Mike'], email: ['[email protected]'], tel: [] }, |
| 55 | + ]); |
| 56 | + |
| 57 | + let results = await navigator.contacts.select(['name', 'email', 'tel'], { multiple: true }); |
| 58 | + assert_equals(results.length, 2); |
| 59 | + results = results.sort((c1, c2) => JSON.stringify(c1) < JSON.stringify(c2) ? -1 : 1); |
| 60 | + |
| 61 | + { |
| 62 | + const dwight = results[0]; |
| 63 | + |
| 64 | + assert_own_property(dwight, 'name'); |
| 65 | + assert_own_property(dwight, 'email'); |
| 66 | + assert_own_property(dwight, 'tel'); |
| 67 | + |
| 68 | + assert_array_equals(dwight.name, ['Dwight Schrute']); |
| 69 | + assert_array_equals(dwight.email, ['[email protected]']); |
| 70 | + assert_array_equals(dwight.tel, ['000-0000']); |
| 71 | + } |
| 72 | + |
| 73 | + { |
| 74 | + const michael = results[1]; |
| 75 | + |
| 76 | + assert_own_property(michael, 'name'); |
| 77 | + assert_own_property(michael, 'email'); |
| 78 | + assert_own_property(michael, 'tel'); |
| 79 | + |
| 80 | + assert_array_equals(michael.name, ['Michael Scott', 'Prison Mike']); |
| 81 | + assert_array_equals(michael.email, ['[email protected]']); |
| 82 | + assert_array_equals(michael.tel, []); |
| 83 | + } |
| 84 | +}, 'The Contact API correctly returns ContactInfo entries'); |
| 85 | + |
| 86 | +contactsTestWithUserActivation(async (test, setSelectedContacts) => { |
| 87 | + // Returns two contacts with all information available. |
| 88 | + setSelectedContacts([ |
| 89 | + { name: ['Dwight Schrute'], email: ['[email protected]'], tel: ['000-0000'] }, |
| 90 | + { name: ['Michael Scott', 'Prison Mike'], email: ['[email protected]'], tel: [] }, |
| 91 | + ]); |
| 92 | + |
| 93 | + const results = await navigator.contacts.select(['name', 'email', 'tel']); |
| 94 | + assert_equals(results.length, 1); |
| 95 | + |
| 96 | +}, 'Only one contact is returned if `multiple` is not set.'); |
| 97 | + |
| 98 | +contactsTestWithUserActivation(async (test, setSelectedContacts) => { |
| 99 | + // Returns partial information since no e-mail addresses are requested. |
| 100 | + setSelectedContacts([{ name: ['Creed'], email: ['[email protected]'], tel: [] }]); |
| 101 | + |
| 102 | + const results = await navigator.contacts.select(['name']); |
| 103 | + |
| 104 | + assert_equals(results.length, 1); |
| 105 | + |
| 106 | + { |
| 107 | + const creed = results[0]; |
| 108 | + |
| 109 | + assert_array_equals(creed.name, ['Creed']); |
| 110 | + assert_equals(creed.email, undefined); |
| 111 | + assert_equals(creed.tel, undefined); |
| 112 | + } |
| 113 | +}, 'The Contact API does not include fields that were not requested'); |
| 114 | + |
| 115 | +contactsTestWithUserActivation(async (test, setSelectedContacts) => { |
| 116 | + // Returns partial information since no e-mail addresses are requested. |
| 117 | + setSelectedContacts([{ name: ['Kelly'], email: [], tel: [] }]); |
| 118 | + |
| 119 | + // First request should work. |
| 120 | + const promise1 = new Promise((resolve, reject) => { |
| 121 | + navigator.contacts.select(['name']).then(resolve) |
| 122 | + .catch(e => reject(e.message)); |
| 123 | + }); |
| 124 | + |
| 125 | + // Second request should fail (since the first one didn't complete yet). |
| 126 | + const promise2 = new Promise((resolve, reject) => { |
| 127 | + navigator.contacts.select(['name']).then(contacts => reject('This was supposed to fail')) |
| 128 | + .catch(e => resolve(e.name)); |
| 129 | + }); |
| 130 | + |
| 131 | + const results = await Promise.all([promise1, promise2]); |
| 132 | + const contacts = results[0]; |
| 133 | + assert_equals(contacts.length, 1); |
| 134 | + const contact = contacts[0]; |
| 135 | + assert_equals(contact.name[0], 'Kelly'); |
| 136 | + assert_equals(results[1], 'InvalidStateError'); |
| 137 | + |
| 138 | +}, 'The Contact API cannot be used again until the first operation is complete.'); |
| 139 | + |
| 140 | +contactsTestWithUserActivation(async (test, setSelectedContacts) => { |
| 141 | + const iframe = document.createElement('iframe'); |
| 142 | + document.body.appendChild(iframe); |
| 143 | + iframe.src = 'resources/non-main-frame-select.html'; |
| 144 | + await new Promise(resolve => window.addEventListener('message', event => resolve(event.data))) |
| 145 | + .then(data => assert_equals(data.errorMsg, 'InvalidStateError')) |
| 146 | + .finally(() => iframe.remove()) |
| 147 | + |
| 148 | +}, 'Test contacts.select() throws an InvalidStateError in a sub-frame'); |
0 commit comments