|
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 = message => { |
| 69 | + if (message.data === "Ready") { |
| 70 | + const onSuccess = () => { |
| 71 | + parent.postMessage("Failure", "*"); |
| 72 | + }; |
| 73 | + const onError = error => { |
| 74 | + if (error.name == "NotAllowedError") { |
| 75 | + parent.postMessage("Success", "*"); |
| 76 | + } else { |
| 77 | + parent.postMessage("Failure", "*"); |
| 78 | + } |
| 79 | + }; |
| 80 | + try { |
| 81 | + const ndef = new NDEFReader(); |
| 82 | + ndef.scan().then(onSuccess, onError); |
| 83 | + } catch(e) { |
| 84 | + parent.postMessage("Failure", "*"); |
| 85 | + } |
| 86 | + } |
| 87 | + }; |
| 88 | + <\/script>`; |
| 89 | + iframe.onload = () => iframe.contentWindow.postMessage('Ready', '*'); |
| 90 | + document.body.appendChild(iframe); |
| 91 | + window.onmessage = message => { |
| 92 | + if (message.data == 'Success') { |
| 93 | + resolve(); |
| 94 | + } else if (message.data == 'Failure') { |
| 95 | + reject(); |
| 96 | + } |
| 97 | + } |
| 98 | + }); |
| 99 | +}, 'Test that WebNFC API is not accessible from iframe context.'); |
| 100 | + |
64 | 101 | nfc_test(async (t, mockNFC) => {
|
65 |
| - const reader = new NDEFReader(); |
| 102 | + const ndef = new NDEFReader(); |
66 | 103 | const controller = new AbortController();
|
67 |
| - const readerWatcher = new EventWatcher(t, reader, ["reading", "readingerror"]); |
68 |
| - const promise = readerWatcher.wait_for("reading").then(event => { |
| 104 | + const ndefWatcher = new EventWatcher(t, ndef, ["reading", "readingerror"]); |
| 105 | + const promise = ndefWatcher.wait_for("reading").then(event => { |
69 | 106 | assert_true(event instanceof NDEFReadingEvent);
|
70 | 107 | controller.abort();
|
71 | 108 | });
|
72 |
| - await reader.scan({signal : controller.signal}); |
| 109 | + await ndef.scan({signal : controller.signal}); |
73 | 110 |
|
74 | 111 | mockNFC.setReadingMessage(createMessage([createTextRecord(test_text_data)]));
|
75 | 112 | await promise;
|
76 | 113 | }, "Test that nfc watch success if NFC HW is enabled.");
|
77 | 114 |
|
78 | 115 | nfc_test(async (t, mockNFC) => {
|
79 |
| - const reader = new NDEFReader(); |
| 116 | + const ndef = new NDEFReader(); |
80 | 117 | const controller = new AbortController();
|
81 |
| - const readerWatcher = new EventWatcher(t, reader, ["reading", "readingerror"]); |
82 |
| - const promise = readerWatcher.wait_for("reading").then(event => { |
| 118 | + const ndefWatcher = new EventWatcher(t, ndef, ["reading", "readingerror"]); |
| 119 | + const promise = ndefWatcher.wait_for("reading").then(event => { |
83 | 120 | assert_true(event instanceof NDEFReadingEvent);
|
84 | 121 | controller.abort();
|
85 | 122 | });
|
86 |
| - await reader.scan({signal : controller.signal}); |
| 123 | + await ndef.scan({signal : controller.signal}); |
87 | 124 |
|
88 | 125 | mockNFC.setReadingMessage(createMessage([createTextRecord(test_text_data)]));
|
89 | 126 | await promise;
|
90 | 127 | }, "Test that NDEFReader.scan matches any ids if NDEFScanOptions.id is undefined.");
|
91 | 128 |
|
92 | 129 | nfc_test(async (t, mockNFC) => {
|
93 |
| - const reader = new NDEFReader(); |
| 130 | + const ndef = new NDEFReader(); |
94 | 131 | const controller = new AbortController();
|
95 | 132 | controller.abort();
|
96 |
| - await promise_rejects_dom(t, 'AbortError', reader.scan({signal: controller.signal})); |
| 133 | + await promise_rejects_dom(t, 'AbortError', ndef.scan({signal: controller.signal})); |
97 | 134 | }, "Test that NDEFReader.scan rejects if NDEFScanOptions.signal is already aborted.");
|
98 | 135 |
|
99 | 136 | nfc_test(async (t, mockNFC) => {
|
100 |
| - const reader = new NDEFReader(); |
| 137 | + const ndef = new NDEFReader(); |
101 | 138 | const controller = new AbortController();
|
102 |
| - const promise = reader.scan({signal: controller.signal}); |
| 139 | + const promise = ndef.scan({signal: controller.signal}); |
103 | 140 | controller.abort();
|
104 | 141 | await promise_rejects_dom(t, 'AbortError', promise);
|
105 | 142 | }, "Test that NDEFReader.scan rejects if NDEFScanOptions.signal aborts right after \
|
106 | 143 | the scan invocation.");
|
107 | 144 |
|
108 | 145 | nfc_test(async (t, mockNFC) => {
|
109 |
| - const reader = new NDEFReader(); |
| 146 | + const ndef = new NDEFReader(); |
110 | 147 | const controller = new AbortController();
|
111 |
| - const readerWatcher = new EventWatcher(t, reader, ["reading", "readingerror"]); |
| 148 | + const ndefWatcher = new EventWatcher(t, ndef, ["reading", "readingerror"]); |
112 | 149 | const message = createMessage([createTextRecord(test_text_data)]);
|
113 |
| - const promise = readerWatcher.wait_for("reading").then(event => { |
| 150 | + const promise = ndefWatcher.wait_for("reading").then(event => { |
114 | 151 | assert_true(event instanceof NDEFReadingEvent);
|
115 | 152 | });
|
116 |
| - await reader.scan({signal : controller.signal}); |
| 153 | + await ndef.scan({signal : controller.signal}); |
117 | 154 |
|
118 | 155 | mockNFC.setReadingMessage(message);
|
119 | 156 | await promise;
|
120 | 157 |
|
121 |
| - reader.onreading = t.unreached_func("reading event should not be fired."); |
| 158 | + ndef.onreading = t.unreached_func("reading event should not be fired."); |
122 | 159 | mockNFC.setReadingMessage(message);
|
123 | 160 | controller.abort();
|
124 | 161 | await new Promise((resolve, reject) => {
|
|
127 | 164 | }, "Test that NDEFReader can not get any reading events once the signal aborts.");
|
128 | 165 |
|
129 | 166 | nfc_test(async (t, mockNFC) => {
|
130 |
| - const reader = new NDEFReader(); |
| 167 | + const ndef = new NDEFReader(); |
131 | 168 | const controller = new AbortController();
|
132 |
| - const readerWatcher = new EventWatcher(t, reader, ["reading", "readingerror"]); |
133 |
| - const promise = readerWatcher.wait_for("reading").then(event => { |
| 169 | + const ndefWatcher = new EventWatcher(t, ndef, ["reading", "readingerror"]); |
| 170 | + const promise = ndefWatcher.wait_for("reading").then(event => { |
134 | 171 | controller.abort();
|
135 | 172 | assert_true(event instanceof NDEFReadingEvent);
|
136 | 173 |
|
|
151 | 188 | assert_equals(decoder.decode(event.message.records[0].toRecords()[0].toRecords()[0].data),
|
152 | 189 | test_text_data, 'data has the same content with the original dictionary');
|
153 | 190 | });
|
154 |
| - await reader.scan({signal : controller.signal}); |
| 191 | + await ndef.scan({signal : controller.signal}); |
155 | 192 |
|
156 | 193 | // An external type record --contains-> a local type record --contains-> a text record.
|
157 | 194 | const messageContainText = createMessage([createTextRecord(test_text_data)]);
|
|
164 | 201 | }, "NDEFRecord.toRecords returns its embedded records correctly.");
|
165 | 202 |
|
166 | 203 | nfc_test(async (t, mockNFC) => {
|
167 |
| - const reader = new NDEFReader(); |
| 204 | + const ndef = new NDEFReader(); |
168 | 205 | const controller = new AbortController();
|
169 |
| - const readerWatcher = new EventWatcher(t, reader, ["reading", "readingerror"]); |
170 |
| - const promise = readerWatcher.wait_for("reading").then(event => { |
| 206 | + const ndefWatcher = new EventWatcher(t, ndef, ["reading", "readingerror"]); |
| 207 | + const promise = ndefWatcher.wait_for("reading").then(event => { |
171 | 208 | controller.abort();
|
172 | 209 | assert_true(event instanceof NDEFReadingEvent);
|
173 | 210 |
|
|
203 | 240 | assert_array_equals(embedded_record_types.sort(), ['text', 'url'],
|
204 | 241 | 'smart-poster record\'s contained record types');
|
205 | 242 | });
|
206 |
| - await reader.scan({signal : controller.signal}); |
| 243 | + await ndef.scan({signal : controller.signal}); |
207 | 244 |
|
208 | 245 | // A smart-poster record contains a uri record, text record.
|
209 | 246 | const uri_record = createUrlRecord(test_url_data);
|
|
218 | 255 | nfc_test(async (t, mockNFC) => {
|
219 | 256 | const promises = [];
|
220 | 257 |
|
221 |
| - const reader1 = new NDEFReader(); |
222 |
| - const readerWatcher1 = new EventWatcher(t, reader1, ["reading", "readingerror"]); |
223 |
| - const promise1 = readerWatcher1.wait_for("readingerror"); |
| 258 | + const ndef1 = new NDEFReader(); |
| 259 | + const ndefWatcher1 = new EventWatcher(t, ndef1, ["reading", "readingerror"]); |
| 260 | + const promise1 = ndefWatcher1.wait_for("readingerror"); |
224 | 261 | promises.push(promise1);
|
225 |
| - await reader1.scan(); |
| 262 | + await ndef1.scan(); |
226 | 263 |
|
227 |
| - const reader2 = new NDEFReader(); |
228 |
| - const readerWatcher2 = new EventWatcher(t, reader2, ["reading", "readingerror"]); |
229 |
| - const promise2 = readerWatcher2.wait_for("readingerror"); |
| 264 | + const ndef2 = new NDEFReader(); |
| 265 | + const ndefWatcher2 = new EventWatcher(t, ndef2, ["reading", "readingerror"]); |
| 266 | + const promise2 = ndefWatcher2.wait_for("readingerror"); |
230 | 267 | promises.push(promise2);
|
231 |
| - await reader2.scan(); |
| 268 | + await ndef2.scan(); |
232 | 269 |
|
233 | 270 | mockNFC.simulateNonNDEFTagDiscovered();
|
234 | 271 | await Promise.all(promises);
|
235 | 272 | }, "Test that NDEFReader.onreadingerror should be fired if the NFC tag does not \
|
236 | 273 | expose NDEF technology.");
|
237 | 274 |
|
238 | 275 | nfc_test(async (t, mockNFC) => {
|
239 |
| - const reader = new NDEFReader(); |
| 276 | + const ndef = new NDEFReader(); |
240 | 277 | const controller = new AbortController();
|
241 |
| - const readerWatcher = new EventWatcher(t, reader, ["reading", "readingerror"]); |
242 |
| - const promise = readerWatcher.wait_for("reading").then(event => { |
| 278 | + const ndefWatcher = new EventWatcher(t, ndef, ["reading", "readingerror"]); |
| 279 | + const promise = ndefWatcher.wait_for("reading").then(event => { |
243 | 280 | assert_equals(event.serialNumber, fake_tag_serial_number);
|
244 | 281 | assert_equals(event.message.records.length, 0);
|
245 | 282 | controller.abort();
|
246 | 283 | });
|
247 |
| - await reader.scan({signal : controller.signal}); |
| 284 | + await ndef.scan({signal : controller.signal}); |
248 | 285 |
|
249 | 286 | mockNFC.setReadingMessage({ records: [] });
|
250 | 287 | await promise;
|
251 | 288 | }, "Test that NDEFReader.onreading should be fired on an unformatted NFC tag \
|
252 | 289 | with empty records array for NDEFMessage.");
|
253 | 290 |
|
254 | 291 | nfc_test(async (t, mockNFC) => {
|
255 |
| - const reader = new NDEFReader(); |
| 292 | + const ndef = new NDEFReader(); |
256 | 293 | const controller = new AbortController();
|
257 | 294 | const message = createMessage([createTextRecord(test_text_data),
|
258 | 295 | createMimeRecordFromJson(test_json_data),
|
|
262 | 299 | createUrlRecord(test_url_data, true),
|
263 | 300 | createRecord('w3.org:xyz', test_buffer_data)],
|
264 | 301 | test_message_origin);
|
265 |
| - const readerWatcher = new EventWatcher(t, reader, ["reading", "readingerror"]); |
266 |
| - const promise = readerWatcher.wait_for("reading").then(event => { |
| 302 | + const ndefWatcher = new EventWatcher(t, ndef, ["reading", "readingerror"]); |
| 303 | + const promise = ndefWatcher.wait_for("reading").then(event => { |
267 | 304 | assert_equals(event.serialNumber, fake_tag_serial_number);
|
268 | 305 | assertWebNDEFMessagesEqual(event.message, new NDEFMessage(message));
|
269 | 306 | controller.abort();
|
270 | 307 | });
|
271 |
| - await reader.scan({signal : controller.signal}); |
| 308 | + await ndef.scan({signal : controller.signal}); |
272 | 309 |
|
273 | 310 | mockNFC.setReadingMessage(message);
|
274 | 311 | await promise;
|
275 | 312 | }, "Test that reading message with multiple records should succeed.");
|
276 | 313 |
|
277 | 314 | 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()); |
| 315 | + const ndef = new NDEFReader(); |
| 316 | + const promise1 = ndef.scan(); |
| 317 | + const promise2 = promise_rejects_dom(t, 'InvalidStateError', ndef.scan()); |
281 | 318 | await promise1;
|
282 | 319 | await promise2;
|
283 | 320 | }, "Test that NDEFReader.scan rejects if there is already an ongoing scan.");
|
284 | 321 |
|
285 | 322 | nfc_test(async (t, mockNFC) => {
|
286 |
| - const reader = new NDEFReader(); |
| 323 | + const ndef = new NDEFReader(); |
287 | 324 | const controller = new AbortController();
|
288 |
| - await reader.scan({signal : controller.signal}); |
| 325 | + await ndef.scan({signal : controller.signal}); |
289 | 326 | controller.abort();
|
290 |
| - await reader.scan(); |
| 327 | + await ndef.scan(); |
291 | 328 | }, "Test that NDEFReader.scan can be started after the previous scan is aborted.");
|
292 | 329 | </script>
|
0 commit comments