Skip to content

Commit

Permalink
[webnfc] Use AbortController for NFCReader
Browse files Browse the repository at this point in the history
This CL uses AbortController to replace the stop() method of NFCReader
interface.

The corresponding spec change is from:
w3c/web-nfc#300

BUG=520391

Change-Id: Ia28bfb283a7ad66fb9c4cb0fc744582671a62fa0
  • Loading branch information
Leon Han authored and chromium-wpt-export-bot committed Aug 28, 2019
1 parent 7cbcb4b commit 2121613
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 96 deletions.
79 changes: 41 additions & 38 deletions web-nfc/NFCReader.https.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,122 +10,125 @@

"use strict";

function waitSyntaxErrorPromise(t, reader) {
function waitSyntaxErrorPromise(t, scan_options) {
const reader = new NFCReader();
const readerWatcher = new EventWatcher(t, reader, ["reading", "error"]);
const promise = readerWatcher.wait_for("error").then(event => {
assert_equals(event.error.name, 'SyntaxError');
});
// NFCReader#start() asynchronously dispatches the syntax error event.
reader.start();
// NFCReader#scan() asynchronously dispatches the syntax error event.
reader.scan(scan_options);
return promise;
}

promise_test(async t => {
const reader = new NFCReader({url: "www.a.com"});
await waitSyntaxErrorPromise(t, reader);
}, "Test that NFCReader.start fails if NFCReaderOptions.url is missing \
await waitSyntaxErrorPromise(t, {url: "www.a.com"});
}, "Test that NFCReader.scan fails if NFCScanOptions.url is missing \
components.");

promise_test(async t => {
const reader = new NFCReader({url: "invalid"});
await waitSyntaxErrorPromise(t, reader);
}, "Test that NFCReader.start fails if NFCReaderOptions.url is invalid.");
await waitSyntaxErrorPromise(t, {url: "invalid"});
}, "Test that NFCReader.scan fails if NFCScanOptions.url is invalid.");

promise_test(async t => {
const reader = new NFCReader({url: "http://a.com"});
await waitSyntaxErrorPromise(t, reader);
}, "Test that NFCReader.start fails if NFCReaderOptions.url has wrong \
await waitSyntaxErrorPromise(t, {url: "http://a.com"});
}, "Test that NFCReader.scan fails if NFCScanOptions.url has wrong \
protocol.");

nfc_test(async (t, mockNFC) => {
const reader = new NFCReader();
const readerWatcher = new EventWatcher(t, reader, ["reading", "error"]);
reader.start();
reader.scan();
mockNFC.setHWStatus(NFCHWStatus.DISABLED);
const event = await readerWatcher.wait_for("error");
assert_equals(event.error.name, 'NotReadableError');
}, "NFCReader.start should fail if NFC HW is disabled.");
}, "NFCReader.scan should fail if NFC HW is disabled.");

nfc_test(async (t, mockNFC) => {
const reader = new NFCReader();
const readerWatcher = new EventWatcher(t, reader, ["reading", "error"]);
reader.start();
reader.scan();
mockNFC.setHWStatus(NFCHWStatus.NOT_SUPPORTED);
const event = await readerWatcher.wait_for("error");
assert_equals(event.error.name, 'NotSupportedError');
}, "NFCReader.start should fail if NFC HW is not supported.");
}, "NFCReader.scan should fail if NFC HW is not supported.");

nfc_test(async (t, mockNFC) => {
const reader = new NFCReader();
const controller = new AbortController();
const readerWatcher = new EventWatcher(t, reader, ["reading", "error"]);

mockNFC.setReadingMessage(createMessage([createTextRecord(test_text_data)]));
const promise = readerWatcher.wait_for("reading").then(event => {
assert_true(event instanceof NFCReadingEvent);
reader.stop();
controller.abort();
});
// NFCReader#start() asynchronously dispatches the reading event.
reader.start();
// NFCReader#scan() asynchronously dispatches the reading event.
reader.scan({signal : controller.signal});
await promise;
}, "Test that nfc watch success if NFC HW is enabled.");

nfc_test(async (t, mockNFC) => {
const reader = new NFCReader({url: "https://a.com"});
const reader = new NFCReader();
const controller = new AbortController();
const readerWatcher = new EventWatcher(t, reader, ["reading", "error"]);

mockNFC.setReadingMessage(createMessage([createTextRecord(test_text_data)]));
const promise = readerWatcher.wait_for("reading").then(event => {
assert_true(event instanceof NFCReadingEvent);
reader.stop();
controller.abort();
});
// NFCReader#start() asynchronously dispatches the reading event.
reader.start();
// NFCReader#scan() asynchronously dispatches the reading event.
reader.scan({signal : controller.signal, url: "https://a.com"});
await promise;
}, "Test that NFCReader.start succeeds if NFCReaderOptions.url is valid URL.");
}, "Test that NFCReader.scan succeeds if NFCScanOptions.url is valid URL.");

nfc_test(async (t, mockNFC) => {
const reader = new NFCReader({url: "https://a.com/*"});
const reader = new NFCReader();
const controller = new AbortController();
const readerWatcher = new EventWatcher(t, reader, ["reading", "error"]);

mockNFC.setReadingMessage(createMessage([createTextRecord(test_text_data)]));
const promise = readerWatcher.wait_for("reading").then(event => {
assert_true(event instanceof NFCReadingEvent);
reader.stop();
controller.abort();
});
// NFCReader#start() asynchronously dispatches the reading event.
reader.start();
// NFCReader#scan() asynchronously dispatches the reading event.
reader.scan({signal : controller.signal, url: "https://a.com/*"});
await promise;
}, "Test that NFCReader.start succeeds if NFCReaderOptions.url is valid URL \
}, "Test that NFCReader.scan succeeds if NFCScanOptions.url is valid URL \
with '*' wildcard character in path.");

nfc_test(async (t, mockNFC) => {
const reader = new NFCReader({url: "https://a.com/*/bar"});
const reader = new NFCReader();
const controller = new AbortController();
const readerWatcher = new EventWatcher(t, reader, ["reading", "error"]);

mockNFC.setReadingMessage(createMessage([createTextRecord(test_text_data)]));
const promise = readerWatcher.wait_for("reading").then(event => {
assert_true(event instanceof NFCReadingEvent);
reader.stop();
controller.abort();
});
// NFCReader#start() asynchronously dispatches the reading event.
reader.start();
// NFCReader#scan() asynchronously dispatches the reading event.
reader.scan({signal : controller.signal, url: "https://a.com/*/bar"});
await promise;
}, "Test that NFCReader.start succeeds if NFCReaderOptions.url is valid URL \
}, "Test that NFCReader.scan succeeds if NFCScanOptions.url is valid URL \
with '*' wildcard character in the beginning of path component followed by \
subpath.");

nfc_test(async (t, mockNFC) => {
const reader = new NFCReader({url: ""});
const controller = new AbortController();
const readerWatcher = new EventWatcher(t, reader, ["reading", "error"]);

mockNFC.setReadingMessage(createMessage([createTextRecord(test_text_data)]));
const promise = readerWatcher.wait_for("reading").then(event => {
assert_true(event instanceof NFCReadingEvent);
reader.stop();
controller.abort();
});
// NFCReader#start() asynchronously dispatches the reading event.
reader.start();
// NFCReader#scan() asynchronously dispatches the reading event.
reader.scan({signal : controller.signal, url: ""});
await promise;
}, "Test that NFCReader.start succeeds if NFCReaderOptions.url is empty.");
}, "Test that NFCReader.scan succeeds if NFCScanOptions.url is empty.");

</script>
88 changes: 44 additions & 44 deletions web-nfc/NFCReader_options.https.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,60 +13,60 @@
const NFCReaderOptionTests =
[
{
desc: "Test that reading data succeed when NFCReaderOptions'" +
desc: "Test that reading data succeed when NFCScanOptions'" +
" recordType is set to 'empty'.",
readOptions: {recordType: "empty"},
unmatchedReadOptions: {recordType: "json"},
scanOptions: {recordType: "empty"},
unmatchedScanOptions: {recordType: "json"},
message: createMessage([createRecord('empty', '')])
},
{
desc: "Test that reading data succeed when NFCReaderOptions'" +
desc: "Test that reading data succeed when NFCScanOptions'" +
" recordType is set to 'json'.",
readOptions: {recordType: "json"},
unmatchedReadOptions: {recordType: "url"},
scanOptions: {recordType: "json"},
unmatchedScanOptions: {recordType: "url"},
message: createMessage([createJsonRecord(test_json_data)])
},
{
desc: "Test that reading data succeed when NFCReaderOptions'" +
desc: "Test that reading data succeed when NFCScanOptions'" +
" recordType is set to 'opaque'.",
readOptions: {recordType: "opaque"},
unmatchedReadOptions: {recordType: "json"},
scanOptions: {recordType: "opaque"},
unmatchedScanOptions: {recordType: "json"},
message: createMessage([createOpaqueRecord(test_buffer_data)])
},
{
desc: "Test that reading data succeed when NFCReaderOptions'" +
desc: "Test that reading data succeed when NFCScanOptions'" +
" recordType is set to 'text'.",
readOptions: {recordType: "text"},
unmatchedReadOptions: {recordType: "json"},
scanOptions: {recordType: "text"},
unmatchedScanOptions: {recordType: "json"},
message: createMessage([createTextRecord(test_text_data)])
},
{
desc: "Test that reading data succeed when NFCReaderOptions'" +
desc: "Test that reading data succeed when NFCScanOptions'" +
" recordType is set to 'url'.",
readOptions: {recordType: "url"},
unmatchedReadOptions: {recordType: "json"},
scanOptions: {recordType: "url"},
unmatchedScanOptions: {recordType: "json"},
message: createMessage([createUrlRecord(test_url_data)])
},
{
desc: "Test that the url of NFCReaderOptions filters relevant data" +
desc: "Test that the url of NFCScanOptions filters relevant data" +
" sources correctly.",
readOptions: {url: `${location.origin}/custom/path`},
unmatchedReadOptions: {url: `${location.origin}/custom/invalid`},
scanOptions: {url: `${location.origin}/custom/path`},
unmatchedScanOptions: {url: `${location.origin}/custom/invalid`},
message: {url: `${location.origin}/custom/path/update`,
records: [createTextRecord(test_text_data)]}
},
{
desc: "Test that the mediaType of NFCReaderOptions filters relevant data" +
desc: "Test that the mediaType of NFCScanOptions filters relevant data" +
" sources correctly.",
readOptions: {mediaType: "application/octet-stream"},
unmatchedReadOptions: {mediaType: "application/json"},
scanOptions: {mediaType: "application/octet-stream"},
unmatchedScanOptions: {mediaType: "application/json"},
message: createMessage([createOpaqueRecord(test_buffer_data)])
},
{
desc: "Test that the compatibility of NFCReaderOptions filters relevant data" +
desc: "Test that the compatibility of NFCScanOptions filters relevant data" +
" sources correctly.",
readOptions: {compatibility: "vendor"},
unmatchedReadOptions: {compatibility: "nfc-forum"},
scanOptions: {compatibility: "vendor"},
unmatchedScanOptions: {compatibility: "nfc-forum"},
message: createMessage([createTextRecord(test_text_data)]),
}
];
Expand All @@ -75,70 +75,70 @@
[
{
desc: "Test that filtering 'empty' record from different messages" +
" correctly with NFCReaderOptions' recordType is set to 'empty'.",
readOptions: {recordType: "empty"},
" correctly with NFCScanOptions' recordType is set to 'empty'.",
scanOptions: {recordType: "empty"},
message: createMessage([createRecord('empty', '')]),
unmatchedMessage: createMessage([createJsonRecord(test_json_data)]),
},
{
desc: "Test that filtering 'json' record from different messages" +
" correctly with NFCReaderOptions' recordType is set to 'json'.",
readOptions: {recordType: "json"},
" correctly with NFCScanOptions' recordType is set to 'json'.",
scanOptions: {recordType: "json"},
message: createMessage([createJsonRecord(test_json_data)]),
unmatchedMessage: createMessage([createUrlRecord(test_url_data)])
},
{
desc: "Test that filtering 'opaque' record from different messages" +
" correctly with NFCReaderOptions' recordType is set to 'opaque'.",
readOptions: {recordType: "opaque"},
" correctly with NFCScanOptions' recordType is set to 'opaque'.",
scanOptions: {recordType: "opaque"},
message: createMessage([createOpaqueRecord(test_buffer_data)]),
unmatchedMessage: createMessage([createJsonRecord(test_json_data)])
},
{
desc: "Test that filtering 'text' record from different messages" +
" correctly with NFCReaderOptions' recordType is set to 'text'.",
readOptions: {recordType: "text"},
" correctly with NFCScanOptions' recordType is set to 'text'.",
scanOptions: {recordType: "text"},
message: createMessage([createTextRecord(test_text_data)]),
unmatchedMessage: createMessage([createUrlRecord(test_url_data)])
},
{
desc: "Test that filtering 'url' record from different messages" +
" correctly with NFCReaderOptions' recordType is set to 'url'.",
readOptions: {recordType: "url"},
" correctly with NFCScanOptions' recordType is set to 'url'.",
scanOptions: {recordType: "url"},
message: createMessage([createUrlRecord(test_url_data)]),
unmatchedMessage: createMessage([createTextRecord(test_text_data)])
},
{
desc: "Test that filtering 'text' record from different messages" +
" correctly with NFCReaderOptions' url set.",
readOptions: {url: `${location.origin}/custom/path`},
" correctly with NFCScanOptions' url set.",
scanOptions: {url: `${location.origin}/custom/path`},
message: {url: `${location.origin}/custom/path/update`,
records: [createTextRecord(test_text_data)]},
unmatchedMessage: {url: `${location.origin}/custom/invalid`,
records: [createUrlRecord(test_url_data)]}
},
{
desc: "Test that filtering 'opaque' record from different messages" +
" correctly with NFCReaderOptions' mediaType set.",
readOptions: {mediaType: "application/octet-stream"},
" correctly with NFCScanOptions' mediaType set.",
scanOptions: {mediaType: "application/octet-stream"},
message: createMessage([createOpaqueRecord(test_buffer_data)]),
unmatchedMessage: createMessage([createJsonRecord(test_json_data)])
},
{
desc: "Test that filtering 'text' record from different messages" +
" correctly with NFCReaderOptions' compatibility set.",
readOptions: {compatibility: "nfc-forum"},
" correctly with NFCScanOptions' compatibility set.",
scanOptions: {compatibility: "nfc-forum"},
message: createMessage([createTextRecord(test_text_data)]),
unmatchedMessage: createMessage([createJsonRecord(test_json_data)]),
unmatchedCompatibility: "vendor"
}
];

for (let NFCReaderOptionTest of NFCReaderOptionTests) {
testNFCReaderOptions(
testNFCScanOptions(
NFCReaderOptionTest.message,
NFCReaderOptionTest.readOptions,
NFCReaderOptionTest.unmatchedReadOptions,
NFCReaderOptionTest.scanOptions,
NFCReaderOptionTest.unmatchedScanOptions,
NFCReaderOptionTest.desc
);
}
Expand All @@ -151,7 +151,7 @@

testReadingMultiMessages(
readMultiMessagesTest.message,
readMultiMessagesTest.readOptions,
readMultiMessagesTest.scanOptions,
readMultiMessagesTest.unmatchedMessage,
unmatchedCompatibility,
readMultiMessagesTest.desc
Expand Down
Loading

0 comments on commit 2121613

Please sign in to comment.