DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Mercurial (c68fe15a81fc)

VCS Links

Line Code
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
<!DOCTYPE html>
<meta charset=utf-8>
<title>Web NFC: NDEFReader.scan tests</title>
<title>Web NFC: NDEFReader.scan tests</title>
<link rel="author" title="Intel" href="http://www.intel.com"/>
<link rel="help" href="https://w3c.github.io/web-nfc/"/>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="resources/nfc-helpers.js"></script>
<script>

"use strict";

const invalid_signals = [
const invalid_signals = [
  "string",
  123,
  {},
  true,
  Symbol(),
  Symbol(),
  () => {},
  self
];

function waitSyntaxErrorPromise(t, scan_options) {
function waitSyntaxErrorPromise(t, scan_options) {
  const reader = new NDEFReader();
  return promise_rejects_dom(t, 'SyntaxError', reader.scan(scan_options));
}

nfc_test(async t => {
  const reader = new NDEFReader();
  const reader = new NDEFReader();
  const promises = [];
  invalid_signals.forEach(invalid_signal => {
    promises.push(promise_rejects_js(t, TypeError,
        reader.scan({ signal: invalid_signal })));
  });
  });
  await Promise.all(promises);
}, "Test that NDEFReader.scan rejects if signal is not an AbortSignal.");

nfc_test(async t => {
  await test_driver.set_permission({ name: 'nfc' }, 'denied', false);
  await test_driver.set_permission({ name: 'nfc' }, 'denied', false);
  const reader = new NDEFReader();
  await promise_rejects_dom(t, 'NotAllowedError', reader.scan());
}, "NDEFReader.scan should fail if user permission is not granted.");

// We do not provide NFC mock here to simulate that there has no available
// We do not provide NFC mock here to simulate that there has no available
// implementation for NFC Mojo interface.
nfc_test(async (t, mockNFC) => {
  mockNFC.simulateClosedPipe();
  const reader = new NDEFReader();
  const readerWatcher = new EventWatcher(t, reader, ["reading", "error"]);
  const readerWatcher = new EventWatcher(t, reader, ["reading", "error"]);
  const promise = readerWatcher.wait_for("error").then(event => {
    assert_true(event instanceof ErrorEvent);
  });
  await promise_rejects_dom(t, 'NotSupportedError', reader.scan());
  await promise;
}, "Test that an error event happens if no implementation for NFC Mojo interface \
is available.");


nfc_test(async (t, mockNFC) => {
  mockNFC.setHWStatus(NFCHWStatus.DISABLED);
  const reader = new NDEFReader();
  await promise_rejects_dom(t, 'NotReadableError', reader.scan());
}, "NDEFReader.scan should fail if NFC HW is disabled.");
}, "NDEFReader.scan should fail if NFC HW is disabled.");

nfc_test(async (t, mockNFC) => {
  mockNFC.setHWStatus(NFCHWStatus.NOT_SUPPORTED);
  const reader = new NDEFReader();
  await promise_rejects_dom(t, 'NotSupportedError', reader.scan());
  await promise_rejects_dom(t, 'NotSupportedError', reader.scan());
}, "NDEFReader.scan should fail if NFC HW is not supported.");

nfc_test(async (t, mockNFC) => {
  const reader = new NDEFReader();
  const controller = new AbortController();
  const controller = new AbortController();
  const readerWatcher = new EventWatcher(t, reader, ["reading", "error"]);
  const promise = readerWatcher.wait_for("reading").then(event => {
    assert_true(event instanceof NDEFReadingEvent);
    controller.abort();
  });
  });
  await reader.scan({signal : controller.signal});

  mockNFC.setReadingMessage(createMessage([createTextRecord(test_text_data)]));
  await promise;
}, "Test that nfc watch success if NFC HW is enabled.");

nfc_test(async (t, mockNFC) => {
nfc_test(async (t, mockNFC) => {
  const reader = new NDEFReader();
  const controller = new AbortController();
  const readerWatcher = new EventWatcher(t, reader, ["reading", "error"]);
  const promise = readerWatcher.wait_for("reading").then(event => {
    assert_true(event instanceof NDEFReadingEvent);
    assert_true(event instanceof NDEFReadingEvent);
    controller.abort();
  });
  await reader.scan({signal : controller.signal});

  mockNFC.setReadingMessage(createMessage([createTextRecord(test_text_data)]));
  mockNFC.setReadingMessage(createMessage([createTextRecord(test_text_data)]));
  await promise;
}, "Test that NDEFReader.scan matches any ids if NDEFScanOptions.id is undefined.");

nfc_test(async (t, mockNFC) => {
  const reader = new NDEFReader();
  const reader = new NDEFReader();
  const controller = new AbortController();
  controller.abort();
  await promise_rejects_dom(t, 'AbortError', reader.scan({signal: controller.signal}));
}, "Test that NDEFReader.scan rejects if NDEFScanOptions.signal is already aborted.");


nfc_test(async (t, mockNFC) => {
  const reader = new NDEFReader();
  const controller = new AbortController();
  const promise = reader.scan({signal: controller.signal});
  controller.abort();
  await promise_rejects_dom(t, 'AbortError', promise);
}, "Test that NDEFReader.scan rejects if NDEFScanOptions.signal aborts right after \
}, "Test that NDEFReader.scan rejects if NDEFScanOptions.signal aborts right after \
the scan invocation.");

nfc_test(async (t, mockNFC) => {
  const reader = new NDEFReader();
  const controller = new AbortController();
  const controller = new AbortController();
  const readerWatcher = new EventWatcher(t, reader, ["reading", "error"]);
  const message = createMessage([createTextRecord(test_text_data)]);
  const promise = readerWatcher.wait_for("reading").then(event => {
    assert_true(event instanceof NDEFReadingEvent);
  });
  });
  await reader.scan({signal : controller.signal});

  mockNFC.setReadingMessage(message);
  await promise;


  reader.onreading = t.unreached_func("reading event should not be fired.");
  mockNFC.setReadingMessage(message);
  controller.abort();
  await new Promise((resolve, reject) => {
  await new Promise((resolve, reject) => {
    t.step_timeout(resolve, 100);
  });
}, "Test that NDEFReader can not get any reading events once the signal aborts.");

nfc_test(async (t, mockNFC) => {
  const reader = new NDEFReader();
  const controller = new AbortController();
  const readerWatcher = new EventWatcher(t, reader, ["reading", "error"]);
  const readerWatcher = new EventWatcher(t, reader, ["reading", "error"]);
  const promise = readerWatcher.wait_for("reading").then(event => {
    controller.abort();
    assert_true(event instanceof NDEFReadingEvent);

    // The message in the event contains only the external type record.
    // The message in the event contains only the external type record.
    assert_equals(event.message.records.length, 1);
    assert_equals(event.message.records[0].recordType, 'example.com:containsLocalRecord',
        'recordType');

    // The external type record contains only the local type record.
    // The external type record contains only the local type record.
    assert_equals(event.message.records[0].toRecords().length, 1);
    assert_equals(event.message.records[0].toRecords()[0].recordType, ':containsTextRecord',
        'recordType');

    // The local type record contains only the text record.
    // The local type record contains only the text record.
    assert_equals(event.message.records[0].toRecords()[0].toRecords()[0].recordType, 'text',
        'recordType');
    const decoder = new TextDecoder();
    assert_equals(decoder.decode(event.message.records[0].toRecords()[0].toRecords()[0].data),
        test_text_data, 'data has the same content with the original dictionary');
        test_text_data, 'data has the same content with the original dictionary');
  });
  await reader.scan({signal : controller.signal});


  // An external type record --contains-> a local type record --contains-> a text record.
  const messageContainText = createMessage([createTextRecord(test_text_data)]);
  const messageContainLocal= createMessage([createRecord(':containsTextRecord',
          messageContainText)]);
  const message = createMessage([createRecord('example.com:containsLocalRecord',
  const message = createMessage([createRecord('example.com:containsLocalRecord',
          messageContainLocal)]);
  mockNFC.setReadingMessage(message);
  await promise;
}, "NDEFRecord.toRecords returns its embedded records correctly.");


nfc_test(async (t, mockNFC) => {
  const reader = new NDEFReader();
  const controller = new AbortController();
  const readerWatcher = new EventWatcher(t, reader, ["reading", "error"]);
  const promise = readerWatcher.wait_for("reading").then(event => {
  const promise = readerWatcher.wait_for("reading").then(event => {
    controller.abort();
    assert_true(event instanceof NDEFReadingEvent);

    // The message in the event contains only the smart-poster record.
    assert_equals(event.message.records.length, 1);
    assert_equals(event.message.records.length, 1);
    assert_equals(event.message.records[0].recordType, 'smart-poster', 'recordType');
    assert_equals(event.message.records[0].mediaType, null, 'mediaType');
    assert_equals(event.message.records[0].id, 'dummy_record_id', 'id');

    // The smart-poster record contains one uri record and one text record.
    // The smart-poster record contains one uri record and one text record.
    const embedded_records = event.message.records[0].toRecords();
    assert_equals(embedded_records.length, 2);


    const decoder = new TextDecoder();
    let embedded_record_types = [];
    for(let record of embedded_records) {
      embedded_record_types.push(record.recordType);
      switch(record.recordType) {
      switch(record.recordType) {
        case 'url':
          assert_equals(record.mediaType, null, 'url record\'s mediaType');
          assert_equals(record.id, test_record_id, 'url record\'s id');
          assert_equals(decoder.decode(record.data), test_url_data, 'url record\'s data');
          break;
          break;
        case 'text':
          assert_equals(record.mediaType, null, 'text record\'s mediaType');
          assert_equals(record.id, test_record_id, 'text record\'s id');
          assert_equals(decoder.decode(record.data), test_text_data, 'text record\'s data');
          break;
          break;
        default:
          assert_unreached("Unknown recordType");
      }
    }
    assert_array_equals(embedded_record_types.sort(), ['text', 'url'],
    assert_array_equals(embedded_record_types.sort(), ['text', 'url'],
        'smart-poster record\'s contained record types');
  });
  await reader.scan({signal : controller.signal});

  // A smart-poster record contains a uri record, text record.
  // A smart-poster record contains a uri record, text record.
  const uri_record = createUrlRecord(test_url_data);
  const text_record = createTextRecord(test_text_data);
  const payload_message = createMessage([uri_record, text_record]);
  const message = createMessage([createRecord(
      'smart-poster', payload_message, "dummy_record_id")]);
      'smart-poster', payload_message, "dummy_record_id")]);
  mockNFC.setReadingMessage(message);
  await promise;
}, "NDEFReader.scan returns smart-poster record correctly.");

nfc_test(async (t, mockNFC) => {
nfc_test(async (t, mockNFC) => {
  const promises = [];

  const reader1 = new NDEFReader();
  const readerWatcher1 = new EventWatcher(t, reader1, ["reading", "error"]);
  const promise1 = readerWatcher1.wait_for("error").then(event => {
  const promise1 = readerWatcher1.wait_for("error").then(event => {
    assert_true(event instanceof ErrorEvent);
  });
  promises.push(promise1);
  await reader1.scan();
  await reader1.scan();

  const reader2 = new NDEFReader();
  const readerWatcher2 = new EventWatcher(t, reader2, ["reading", "error"]);
  const promise2 = readerWatcher2.wait_for("error").then(event => {
    assert_true(event instanceof ErrorEvent);
    assert_true(event instanceof ErrorEvent);
  });
  promises.push(promise2);
  await reader2.scan();


  mockNFC.simulateNonNDEFTagDiscovered();
  await Promise.all(promises);
}, "Test that NDEFReader.onerror should be fired if the NFC tag does not \
expose NDEF technology.");


nfc_test(async (t, mockNFC) => {
  const reader = new NDEFReader();
  const controller = new AbortController();
  const readerWatcher = new EventWatcher(t, reader, ["reading", "error"]);
  const promise = readerWatcher.wait_for("reading").then(event => {
  const promise = readerWatcher.wait_for("reading").then(event => {
    assert_equals(event.serialNumber, fake_tag_serial_number);
    assert_equals(event.message.records.length, 0);
    controller.abort();
  });
  await reader.scan({signal : controller.signal});
  await reader.scan({signal : controller.signal});

  mockNFC.setReadingMessage({ records: [] });
  await promise;
}, "Test that NDEFReader.onreading should be fired on an unformatted NFC tag \
with empty records array for NDEFMessage.");
with empty records array for NDEFMessage.");

nfc_test(async (t, mockNFC) => {
  const reader = new NDEFReader();
  const controller = new AbortController();
  const message = createMessage([createTextRecord(test_text_data),
  const message = createMessage([createTextRecord(test_text_data),
                                createMimeRecordFromJson(test_json_data),
                                createMimeRecord(test_buffer_data),
                                createUnknownRecord(test_buffer_data),
                                createUrlRecord(test_url_data),
                                createUrlRecord(test_url_data, true),
                                createUrlRecord(test_url_data, true),
                                createRecord('w3.org:xyz', test_buffer_data)],
                                test_message_origin);
  const readerWatcher = new EventWatcher(t, reader, ["reading", "error"]);
  const promise = readerWatcher.wait_for("reading").then(event => {
    assert_equals(event.serialNumber, fake_tag_serial_number);
    assert_equals(event.serialNumber, fake_tag_serial_number);
    assertWebNDEFMessagesEqual(event.message, new NDEFMessage(message));
    controller.abort();
  });
  await reader.scan({signal : controller.signal});


  mockNFC.setReadingMessage(message);
  await promise;
}, "Test that reading message with multiple records should succeed.");

</script>
</script>