Skip to content

Commit

Permalink
Bug 1579806 [wpt PR 18926] - [webnfc] Support read/write external typ…
Browse files Browse the repository at this point in the history
…e records, a=testonly

Automatic update from web-platform-tests
[webnfc] Support read/write external type records

Note: not support sub-records yet.

The corresponding spec changes:
w3c/web-nfc#278
w3c/web-nfc#326
and the relevant issue:
w3c/web-nfc#331

BUG=520391,995234

Change-Id: I5b99543bddf505975567ecad22c0d5390842337f
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1791533
Reviewed-by: Reilly Grant <reillygchromium.org>
Commit-Queue: Leon Han <leon.hanintel.com>
Cr-Commit-Position: refs/heads/master{#705377}

--

wpt-commits: e4c65276b686cd788b2c4f8c395025df371e84a1
wpt-pr: 18926

UltraBlame original commit: a6cbcd03a32381253779e10837c27a3d5a27f996
  • Loading branch information
marco-c committed Oct 27, 2019
1 parent deef0f7 commit d6bd5d1
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 5 deletions.
Expand Up @@ -63,7 +63,7 @@
'Modifying the original buffer does not affect toArrayBuffer() content');
assert_array_equals(new Uint8Array(data_3), original_data,
'Modifying the original buffer does not affect toArrayBuffer() content');
}, 'NDEFRecord constructor with opaque data');
}, 'NDEFRecord constructor with opaque record type');

test(() => {
const record = new NDEFRecord(createJsonRecord(test_json_data));
Expand All @@ -82,7 +82,40 @@
'toJSON() again returns another new object');
assert_object_equals(data_2, test_json_data,
'toJSON() has the same content with the original dictionary');
}, 'NDEFRecord constructor with json data');
}, 'NDEFRecord constructor with JSON record type');

test(() => {
let buffer = new ArrayBuffer(4);
let buffer_view = new Uint8Array(buffer);
let original_data = new Uint8Array([1, 2, 3, 4]);
buffer_view.set(original_data);
const record = new NDEFRecord(createRecord('foo.eXamPle.coM:bAr*-', undefined, buffer));
assert_equals(record.recordType, 'foo.example.com:bAr*-', 'recordType');
assert_equals(record.mediaType, 'application/octet-stream', 'mediaType');

const data_1 = record.toArrayBuffer();
assert_true(data_1 instanceof ArrayBuffer);
assert_not_equals(data_1, buffer, 'toArrayBuffer() returns a new object');
assert_array_equals(new Uint8Array(data_1), original_data,
'toArrayBuffer() has the same content with the original buffer');

const data_2 = record.toArrayBuffer();
assert_true(data_2 instanceof ArrayBuffer);
assert_not_equals(data_2, data_1,
'toArrayBuffer() again returns another new object');
assert_array_equals(new Uint8Array(data_2), original_data,
'toArrayBuffer() has the same content with the original buffer');

buffer_view.set([4, 3, 2, 1]);
const data_3 = record.toArrayBuffer();
assert_true(data_3 instanceof ArrayBuffer);
assert_array_equals(new Uint8Array(data_1), original_data,
'Modifying the original buffer does not affect toArrayBuffer() content');
assert_array_equals(new Uint8Array(data_2), original_data,
'Modifying the original buffer does not affect toArrayBuffer() content');
assert_array_equals(new Uint8Array(data_3), original_data,
'Modifying the original buffer does not affect toArrayBuffer() content');
}, 'NDEFRecord constructor with external record type');

test(() => {
assert_throws(new TypeError, () => new NDEFRecord(createRecord('EMptY')),
Expand All @@ -99,4 +132,17 @@
'Unknown record type.');
}, 'NDEFRecord constructor with record type string being treated as case sensitive');

test(() => {
assert_throws(new TypeError, () => new NDEFRecord(createRecord(
':xyz', '', test_buffer_data)), 'The domain should not be empty.');
assert_throws(new TypeError, () => new NDEFRecord(createRecord(
'[:xyz', '', test_buffer_data)), '"[" is not a valid FQDN.');
assert_throws(new TypeError, () => new NDEFRecord(createRecord(
'example.com:', '', test_buffer_data)), 'The type should not be empty.');
assert_throws(new TypeError, () => new NDEFRecord(createRecord(
'example.com:xyz~', '', test_buffer_data)), 'The type should not contain \'~\'.');
assert_throws(new TypeError, () => new NDEFRecord(createRecord(
'example.com:xyz/', '', test_buffer_data)), 'The type should not contain \'/\'.');
}, 'NDEFRecord constructor with invalid external record type');

</script>
16 changes: 16 additions & 0 deletions testing/web-platform/tests/web-nfc/NFCReader_options.https.html
Expand Up @@ -47,6 +47,14 @@
unmatchedScanOptions: {recordType: "json"},
message: createMessage([createUrlRecord(test_url_data)])
},
{
desc: "Test that reading data succeed when NFCScanOptions'" +
" recordType is set to a custom type for external type records.",
scanOptions: {recordType: "w3.org:xyz"},
unmatchedScanOptions: {recordType: "opaque"},
message: createMessage([createRecord('w3.org:xyz', 'application/octet-stream',
test_buffer_data)])
},
{
desc: "Test that the url of NFCScanOptions filters relevant data" +
" sources correctly.",
Expand Down Expand Up @@ -101,6 +109,14 @@
message: createMessage([createUrlRecord(test_url_data)]),
unmatchedMessage: createMessage([createTextRecord(test_text_data)])
},
{
desc: "Test that filtering external record from different messages" +
" correctly with NFCScanOptions' recordType is set to the custom type.",
scanOptions: {recordType: "w3.org:xyz"},
message: createMessage([createRecord('w3.org:xyz', 'application/octet-stream',
test_buffer_data)]),
unmatchedMessage: createMessage([createTextRecord(test_text_data)])
},
{
desc: "Test that filtering 'text' record from different messages" +
" correctly with NFCScanOptions' url set.",
Expand Down
21 changes: 18 additions & 3 deletions testing/web-platform/tests/web-nfc/NFCWriter_push.https.html
Expand Up @@ -48,7 +48,21 @@
// NDEFRecord.data for 'opaque' record must be ArrayBuffer.
createMessage([createOpaqueRecord(test_text_data)]),
createMessage([createOpaqueRecord(test_number_data)]),
createMessage([createOpaqueRecord(test_json_data)])
createMessage([createOpaqueRecord(test_json_data)]),

// https://w3c.github.io/web-nfc/#dfn-map-external-data-to-ndef
// NDEFRecord must have data.
createMessage([createRecord('w3.org:xyz', '', undefined)]),

// NDEFRecord.data for external record must be ArrayBuffer.
createMessage([createRecord('w3.org:xyz', '', test_text_data)]),
createMessage([createRecord('w3.org:xyz', '', test_number_data)]),
createMessage([createRecord('w3.org:xyz', '', test_json_data)]),

// https://w3c.github.io/web-nfc/#the-ndefrecordtype-string
// The record type is neither a known type ('text', 'json' etc.) nor a
// valid custom type for an external type record.
createMessage([createRecord('unmatched_type', '', test_buffer_data)])
];

const invalid_syntax_messages =
Expand Down Expand Up @@ -266,11 +280,12 @@
createJsonRecord(test_json_data),
createJsonRecord(test_number_data),
createOpaqueRecord(test_buffer_data),
createUrlRecord(test_url_data)],
createUrlRecord(test_url_data),
createRecord('w3.org:xyz', '', test_buffer_data)],
test_message_origin);
await writer.push(message);
assertNDEFMessagesEqual(message, mockNFC.pushedMessage());
}, "NFCWriter.push NDEFMessage containing text, json, opaque and url records \
}, "NFCWriter.push NDEFMessage containing text, json, opaque, url and external records \
with default NFCPushOptions.");

nfc_test(async (t, mockNFC) => {
Expand Down

0 comments on commit d6bd5d1

Please sign in to comment.