|
21 | 21 | ]; |
22 | 22 |
|
23 | 23 | function waitSyntaxErrorPromise(t, scan_options) { |
24 | | - const reader = new NDEFReader(); |
25 | | - return promise_rejects_dom(t, 'SyntaxError', reader.scan(scan_options)); |
| 24 | + const ndef = new NDEFReader(); |
| 25 | + return promise_rejects_dom(t, 'SyntaxError', ndef.scan(scan_options)); |
26 | 26 | } |
27 | 27 |
|
28 | 28 | nfc_test(async t => { |
29 | | - const reader = new NDEFReader(); |
| 29 | + const ndef = new NDEFReader(); |
30 | 30 | const promises = []; |
31 | 31 | invalid_signals.forEach(invalid_signal => { |
32 | 32 | promises.push(promise_rejects_js(t, TypeError, |
33 | | - reader.scan({ signal: invalid_signal }))); |
| 33 | + ndef.scan({ signal: invalid_signal }))); |
34 | 34 | }); |
35 | 35 | await Promise.all(promises); |
36 | 36 | }, "Test that NDEFReader.scan rejects if signal is not an AbortSignal."); |
37 | 37 |
|
38 | 38 | nfc_test(async t => { |
39 | 39 | await test_driver.set_permission({ name: 'nfc' }, 'denied', false); |
40 | | - const reader = new NDEFReader(); |
41 | | - await promise_rejects_dom(t, 'NotAllowedError', reader.scan()); |
| 40 | + const ndef = new NDEFReader(); |
| 41 | + await promise_rejects_dom(t, 'NotAllowedError', ndef.scan()); |
42 | 42 | }, "NDEFReader.scan should fail if user permission is not granted."); |
43 | 43 |
|
44 | 44 | // We do not provide NFC mock here to simulate that there has no available |
45 | 45 | // implementation for NFC Mojo interface. |
46 | 46 | nfc_test(async (t, mockNFC) => { |
47 | 47 | mockNFC.simulateClosedPipe(); |
48 | | - const reader = new NDEFReader(); |
49 | | - await promise_rejects_dom(t, 'NotSupportedError', reader.scan()); |
50 | | -}, "NDEFReader.scan should faild if no implementation for NFC Mojo interface."); |
| 48 | + const ndef = new NDEFReader(); |
| 49 | + await promise_rejects_dom(t, 'NotSupportedError', ndef.scan()); |
| 50 | +}, "NDEFReader.scan should fail if no implementation for NFC Mojo interface."); |
51 | 51 |
|
52 | 52 | nfc_test(async (t, mockNFC) => { |
53 | 53 | mockNFC.setHWStatus(NFCHWStatus.DISABLED); |
54 | | - const reader = new NDEFReader(); |
55 | | - await promise_rejects_dom(t, 'NotReadableError', reader.scan()); |
| 54 | + const ndef = new NDEFReader(); |
| 55 | + await promise_rejects_dom(t, 'NotReadableError', ndef.scan()); |
56 | 56 | }, "NDEFReader.scan should fail if NFC HW is disabled."); |
57 | 57 |
|
58 | 58 | nfc_test(async (t, mockNFC) => { |
59 | 59 | mockNFC.setHWStatus(NFCHWStatus.NOT_SUPPORTED); |
60 | | - const reader = new NDEFReader(); |
61 | | - await promise_rejects_dom(t, 'NotSupportedError', reader.scan()); |
| 60 | + const ndef = new NDEFReader(); |
| 61 | + await promise_rejects_dom(t, 'NotSupportedError', ndef.scan()); |
62 | 62 | }, "NDEFReader.scan should fail if NFC HW is not supported."); |
63 | 63 |
|
| 64 | +nfc_test(async () => { |
| 65 | + await new Promise((resolve,reject) => { |
| 66 | + const iframe = document.createElement('iframe'); |
| 67 | + iframe.srcdoc = `<script> |
| 68 | + window.onmessage = async (message) => { |
| 69 | + if (message.data === "Ready") { |
| 70 | + try { |
| 71 | + const ndef = new NDEFReader(); |
| 72 | + await ndef.scan(); |
| 73 | + parent.postMessage("Failure", "*"); |
| 74 | + } catch (error) { |
| 75 | + if (error.name == "NotAllowedError") { |
| 76 | + parent.postMessage("Success", "*"); |
| 77 | + } else { |
| 78 | + parent.postMessage("Failure", "*"); |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + }; |
| 83 | + <\/script>`; |
| 84 | + iframe.onload = () => iframe.contentWindow.postMessage('Ready', '*'); |
| 85 | + document.body.appendChild(iframe); |
| 86 | + window.onmessage = message => { |
| 87 | + if (message.data == 'Success') { |
| 88 | + resolve(); |
| 89 | + } else if (message.data == 'Failure') { |
| 90 | + reject(); |
| 91 | + } |
| 92 | + } |
| 93 | + }); |
| 94 | +}, 'Test that WebNFC API is not accessible from iframe context.'); |
| 95 | + |
64 | 96 | nfc_test(async (t, mockNFC) => { |
65 | | - const reader = new NDEFReader(); |
| 97 | + const ndef = new NDEFReader(); |
66 | 98 | const controller = new AbortController(); |
67 | | - const readerWatcher = new EventWatcher(t, reader, ["reading", "readingerror"]); |
68 | | - const promise = readerWatcher.wait_for("reading").then(event => { |
| 99 | + const ndefWatcher = new EventWatcher(t, ndef, ["reading", "readingerror"]); |
| 100 | + const promise = ndefWatcher.wait_for("reading").then(event => { |
69 | 101 | assert_true(event instanceof NDEFReadingEvent); |
70 | 102 | controller.abort(); |
71 | 103 | }); |
72 | | - await reader.scan({signal : controller.signal}); |
| 104 | + await ndef.scan({signal : controller.signal}); |
73 | 105 |
|
74 | 106 | mockNFC.setReadingMessage(createMessage([createTextRecord(test_text_data)])); |
75 | 107 | await promise; |
76 | 108 | }, "Test that nfc watch success if NFC HW is enabled."); |
77 | 109 |
|
78 | 110 | nfc_test(async (t, mockNFC) => { |
79 | | - const reader = new NDEFReader(); |
| 111 | + const ndef = new NDEFReader(); |
80 | 112 | const controller = new AbortController(); |
81 | | - const readerWatcher = new EventWatcher(t, reader, ["reading", "readingerror"]); |
82 | | - const promise = readerWatcher.wait_for("reading").then(event => { |
| 113 | + const ndefWatcher = new EventWatcher(t, ndef, ["reading", "readingerror"]); |
| 114 | + const promise = ndefWatcher.wait_for("reading").then(event => { |
83 | 115 | assert_true(event instanceof NDEFReadingEvent); |
84 | 116 | controller.abort(); |
85 | 117 | }); |
86 | | - await reader.scan({signal : controller.signal}); |
| 118 | + await ndef.scan({signal : controller.signal}); |
87 | 119 |
|
88 | 120 | mockNFC.setReadingMessage(createMessage([createTextRecord(test_text_data)])); |
89 | 121 | await promise; |
90 | 122 | }, "Test that NDEFReader.scan matches any ids if NDEFScanOptions.id is undefined."); |
91 | 123 |
|
92 | 124 | nfc_test(async (t, mockNFC) => { |
93 | | - const reader = new NDEFReader(); |
| 125 | + const ndef = new NDEFReader(); |
94 | 126 | const controller = new AbortController(); |
95 | 127 | controller.abort(); |
96 | | - await promise_rejects_dom(t, 'AbortError', reader.scan({signal: controller.signal})); |
| 128 | + await promise_rejects_dom(t, 'AbortError', ndef.scan({signal: controller.signal})); |
97 | 129 | }, "Test that NDEFReader.scan rejects if NDEFScanOptions.signal is already aborted."); |
98 | 130 |
|
99 | 131 | nfc_test(async (t, mockNFC) => { |
100 | | - const reader = new NDEFReader(); |
| 132 | + const ndef = new NDEFReader(); |
101 | 133 | const controller = new AbortController(); |
102 | | - const promise = reader.scan({signal: controller.signal}); |
| 134 | + const promise = ndef.scan({signal: controller.signal}); |
103 | 135 | controller.abort(); |
104 | 136 | await promise_rejects_dom(t, 'AbortError', promise); |
105 | 137 | }, "Test that NDEFReader.scan rejects if NDEFScanOptions.signal aborts right after \ |
106 | 138 | the scan invocation."); |
107 | 139 |
|
108 | 140 | nfc_test(async (t, mockNFC) => { |
109 | | - const reader = new NDEFReader(); |
| 141 | + const ndef = new NDEFReader(); |
110 | 142 | const controller = new AbortController(); |
111 | | - const readerWatcher = new EventWatcher(t, reader, ["reading", "readingerror"]); |
| 143 | + const ndefWatcher = new EventWatcher(t, ndef, ["reading", "readingerror"]); |
112 | 144 | const message = createMessage([createTextRecord(test_text_data)]); |
113 | | - const promise = readerWatcher.wait_for("reading").then(event => { |
| 145 | + const promise = ndefWatcher.wait_for("reading").then(event => { |
114 | 146 | assert_true(event instanceof NDEFReadingEvent); |
115 | 147 | }); |
116 | | - await reader.scan({signal : controller.signal}); |
| 148 | + await ndef.scan({signal : controller.signal}); |
117 | 149 |
|
118 | 150 | mockNFC.setReadingMessage(message); |
119 | 151 | await promise; |
120 | 152 |
|
121 | | - reader.onreading = t.unreached_func("reading event should not be fired."); |
| 153 | + ndef.onreading = t.unreached_func("reading event should not be fired."); |
122 | 154 | mockNFC.setReadingMessage(message); |
123 | 155 | controller.abort(); |
124 | 156 | await new Promise((resolve, reject) => { |
|
127 | 159 | }, "Test that NDEFReader can not get any reading events once the signal aborts."); |
128 | 160 |
|
129 | 161 | nfc_test(async (t, mockNFC) => { |
130 | | - const reader = new NDEFReader(); |
| 162 | + const ndef = new NDEFReader(); |
131 | 163 | const controller = new AbortController(); |
132 | | - const readerWatcher = new EventWatcher(t, reader, ["reading", "readingerror"]); |
133 | | - const promise = readerWatcher.wait_for("reading").then(event => { |
| 164 | + const ndefWatcher = new EventWatcher(t, ndef, ["reading", "readingerror"]); |
| 165 | + const promise = ndefWatcher.wait_for("reading").then(event => { |
134 | 166 | controller.abort(); |
135 | 167 | assert_true(event instanceof NDEFReadingEvent); |
136 | 168 |
|
|
151 | 183 | assert_equals(decoder.decode(event.message.records[0].toRecords()[0].toRecords()[0].data), |
152 | 184 | test_text_data, 'data has the same content with the original dictionary'); |
153 | 185 | }); |
154 | | - await reader.scan({signal : controller.signal}); |
| 186 | + await ndef.scan({signal : controller.signal}); |
155 | 187 |
|
156 | 188 | // An external type record --contains-> a local type record --contains-> a text record. |
157 | 189 | const messageContainText = createMessage([createTextRecord(test_text_data)]); |
|
164 | 196 | }, "NDEFRecord.toRecords returns its embedded records correctly."); |
165 | 197 |
|
166 | 198 | nfc_test(async (t, mockNFC) => { |
167 | | - const reader = new NDEFReader(); |
| 199 | + const ndef = new NDEFReader(); |
168 | 200 | const controller = new AbortController(); |
169 | | - const readerWatcher = new EventWatcher(t, reader, ["reading", "readingerror"]); |
170 | | - const promise = readerWatcher.wait_for("reading").then(event => { |
| 201 | + const ndefWatcher = new EventWatcher(t, ndef, ["reading", "readingerror"]); |
| 202 | + const promise = ndefWatcher.wait_for("reading").then(event => { |
171 | 203 | controller.abort(); |
172 | 204 | assert_true(event instanceof NDEFReadingEvent); |
173 | 205 |
|
|
203 | 235 | assert_array_equals(embedded_record_types.sort(), ['text', 'url'], |
204 | 236 | 'smart-poster record\'s contained record types'); |
205 | 237 | }); |
206 | | - await reader.scan({signal : controller.signal}); |
| 238 | + await ndef.scan({signal : controller.signal}); |
207 | 239 |
|
208 | 240 | // A smart-poster record contains a uri record, text record. |
209 | 241 | const uri_record = createUrlRecord(test_url_data); |
|
218 | 250 | nfc_test(async (t, mockNFC) => { |
219 | 251 | const promises = []; |
220 | 252 |
|
221 | | - const reader1 = new NDEFReader(); |
222 | | - const readerWatcher1 = new EventWatcher(t, reader1, ["reading", "readingerror"]); |
223 | | - const promise1 = readerWatcher1.wait_for("readingerror"); |
| 253 | + const ndef1 = new NDEFReader(); |
| 254 | + const ndefWatcher1 = new EventWatcher(t, ndef1, ["reading", "readingerror"]); |
| 255 | + const promise1 = ndefWatcher1.wait_for("readingerror"); |
224 | 256 | promises.push(promise1); |
225 | | - await reader1.scan(); |
| 257 | + await ndef1.scan(); |
226 | 258 |
|
227 | | - const reader2 = new NDEFReader(); |
228 | | - const readerWatcher2 = new EventWatcher(t, reader2, ["reading", "readingerror"]); |
229 | | - const promise2 = readerWatcher2.wait_for("readingerror"); |
| 259 | + const ndef2 = new NDEFReader(); |
| 260 | + const ndefWatcher2 = new EventWatcher(t, ndef2, ["reading", "readingerror"]); |
| 261 | + const promise2 = ndefWatcher2.wait_for("readingerror"); |
230 | 262 | promises.push(promise2); |
231 | | - await reader2.scan(); |
| 263 | + await ndef2.scan(); |
232 | 264 |
|
233 | 265 | mockNFC.simulateNonNDEFTagDiscovered(); |
234 | 266 | await Promise.all(promises); |
235 | 267 | }, "Test that NDEFReader.onreadingerror should be fired if the NFC tag does not \ |
236 | 268 | expose NDEF technology."); |
237 | 269 |
|
238 | 270 | nfc_test(async (t, mockNFC) => { |
239 | | - const reader = new NDEFReader(); |
| 271 | + const ndef = new NDEFReader(); |
240 | 272 | const controller = new AbortController(); |
241 | | - const readerWatcher = new EventWatcher(t, reader, ["reading", "readingerror"]); |
242 | | - const promise = readerWatcher.wait_for("reading").then(event => { |
| 273 | + const ndefWatcher = new EventWatcher(t, ndef, ["reading", "readingerror"]); |
| 274 | + const promise = ndefWatcher.wait_for("reading").then(event => { |
243 | 275 | assert_equals(event.serialNumber, fake_tag_serial_number); |
244 | 276 | assert_equals(event.message.records.length, 0); |
245 | 277 | controller.abort(); |
246 | 278 | }); |
247 | | - await reader.scan({signal : controller.signal}); |
| 279 | + await ndef.scan({signal : controller.signal}); |
248 | 280 |
|
249 | 281 | mockNFC.setReadingMessage({ records: [] }); |
250 | 282 | await promise; |
251 | 283 | }, "Test that NDEFReader.onreading should be fired on an unformatted NFC tag \ |
252 | 284 | with empty records array for NDEFMessage."); |
253 | 285 |
|
254 | 286 | nfc_test(async (t, mockNFC) => { |
255 | | - const reader = new NDEFReader(); |
| 287 | + const ndef = new NDEFReader(); |
256 | 288 | const controller = new AbortController(); |
257 | 289 | const message = createMessage([createTextRecord(test_text_data), |
258 | 290 | createMimeRecordFromJson(test_json_data), |
|
262 | 294 | createUrlRecord(test_url_data, true), |
263 | 295 | createRecord('w3.org:xyz', test_buffer_data)], |
264 | 296 | test_message_origin); |
265 | | - const readerWatcher = new EventWatcher(t, reader, ["reading", "readingerror"]); |
266 | | - const promise = readerWatcher.wait_for("reading").then(event => { |
| 297 | + const ndefWatcher = new EventWatcher(t, ndef, ["reading", "readingerror"]); |
| 298 | + const promise = ndefWatcher.wait_for("reading").then(event => { |
267 | 299 | assert_equals(event.serialNumber, fake_tag_serial_number); |
268 | 300 | assertWebNDEFMessagesEqual(event.message, new NDEFMessage(message)); |
269 | 301 | controller.abort(); |
270 | 302 | }); |
271 | | - await reader.scan({signal : controller.signal}); |
| 303 | + await ndef.scan({signal : controller.signal}); |
272 | 304 |
|
273 | 305 | mockNFC.setReadingMessage(message); |
274 | 306 | await promise; |
275 | 307 | }, "Test that reading message with multiple records should succeed."); |
276 | 308 |
|
277 | 309 | nfc_test(async (t, mockNFC) => { |
278 | | - const reader = new NDEFReader(); |
279 | | - const promise1 = reader.scan(); |
280 | | - const promise2 = promise_rejects_dom(t, 'InvalidStateError', reader.scan()); |
| 310 | + const ndef = new NDEFReader(); |
| 311 | + const promise1 = ndef.scan(); |
| 312 | + const promise2 = promise_rejects_dom(t, 'InvalidStateError', ndef.scan()); |
281 | 313 | await promise1; |
282 | 314 | await promise2; |
283 | 315 | }, "Test that NDEFReader.scan rejects if there is already an ongoing scan."); |
284 | 316 |
|
285 | 317 | nfc_test(async (t, mockNFC) => { |
286 | | - const reader = new NDEFReader(); |
| 318 | + const ndef = new NDEFReader(); |
287 | 319 | const controller = new AbortController(); |
288 | | - await reader.scan({signal : controller.signal}); |
| 320 | + await ndef.scan({signal : controller.signal}); |
289 | 321 | controller.abort(); |
290 | | - await reader.scan(); |
| 322 | + await ndef.scan(); |
291 | 323 | }, "Test that NDEFReader.scan can be started after the previous scan is aborted."); |
292 | 324 | </script> |
0 commit comments