Skip to content

Commit

Permalink
[webnfc] Impl "typedef DOMString NDEFRecordType"
Browse files Browse the repository at this point in the history
This CL switches the IDL type definition from
"enum NDEFRecordType { "empty", "text", "url", "json", "opaque" };"
to
"typedef DOMString NDEFRecordType" according to web-nfc spec.

This is in preparation for supporting 'smart-poster' and external type
records in the future, which was added in
w3c/web-nfc#278.

BUG=520391

Change-Id: I45ec31afb3841cc5b12c954ffc2821da357fef04
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1781510
Commit-Queue: Leon Han <leon.han@intel.com>
Reviewed-by: Daniel Cheng <dcheng@chromium.org>
Reviewed-by: Reilly Grant <reillyg@chromium.org>
Reviewed-by: Rijubrata Bhaumik <rijubrata.bhaumik@intel.com>
Cr-Commit-Position: refs/heads/master@{#694635}
  • Loading branch information
Leon Han authored and chromium-wpt-export-bot committed Sep 9, 2019
1 parent 79e931c commit 40b84d0
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 26 deletions.
31 changes: 6 additions & 25 deletions resources/chromium/nfc-mock.js
@@ -1,20 +1,5 @@
'use strict';

function toMojoNDEFRecordType(type) {
switch (type) {
case 'text':
return device.mojom.NDEFRecordType.TEXT;
case 'url':
return device.mojom.NDEFRecordType.URL;
case 'json':
return device.mojom.NDEFRecordType.JSON;
case 'opaque':
return device.mojom.NDEFRecordType.OPAQUE_RECORD;
}

return device.mojom.NDEFRecordType.EMPTY;
}

function toMojoNFCPushTarget(target) {
switch (target) {
case 'peer':
Expand Down Expand Up @@ -48,7 +33,7 @@ function toMojoNDEFMessage(message) {

function toMojoNDEFRecord(record) {
let nfcRecord = new device.mojom.NDEFRecord();
nfcRecord.recordType = toMojoNDEFRecordType(record.recordType);
nfcRecord.recordType = record.recordType;
nfcRecord.mediaType = record.mediaType;
nfcRecord.data = toByteArray(record.data);
return nfcRecord;
Expand All @@ -74,17 +59,15 @@ function toByteArray(data) {
// TODO: Use different getters to get received record data,
// see spec changes at https://github.com/w3c/web-nfc/pull/243
function compareNDEFRecords(providedRecord, receivedRecord) {
assert_equals(toMojoNDEFRecordType(providedRecord.recordType),
receivedRecord.recordType);
assert_equals(providedRecord.recordType, receivedRecord.recordType);

// Compare media types without charset.
// Charset should be compared when watch method is implemented, in order
// to check that written and read strings are equal.
assert_equals(providedRecord.mediaType,
receivedRecord.mediaType.substring(0, providedRecord.mediaType.length));

assert_false(toMojoNDEFRecordType(providedRecord.recordType) ==
device.mojom.NDEFRecordType.EMPTY);
assert_not_equals(providedRecord.recordType, 'empty');

assert_array_equals(toByteArray(providedRecord.data),
new Uint8Array(receivedRecord.data));
Expand Down Expand Up @@ -140,8 +123,7 @@ function assertNFCReaderOptionsEqual(provided, received) {

if (provided.recordType !== undefined) {
assert_equals(!+received.record_filter, true);
assert_equals(toMojoNDEFRecordType(provided.recordType),
received.recordFilter.recordType);
assert_equals(provided.recordType, received.recordFilter.recordType);
}
}

Expand Down Expand Up @@ -169,9 +151,8 @@ function matchesWatchOptions(message, compatibility, options) {
&& options.mediaType !== record.mediaType) {
return false;
}
if (options.recordFilter != null
&& options.recordFilter.recordType
!== toMojoNDEFRecordType(record.recordType)) {
if (options.recordFilter != null &&
options.recordFilter.recordType !== record.recordType) {
return false;
}
}
Expand Down
33 changes: 32 additions & 1 deletion web-nfc/NDEFRecord_constructor.https.html
Expand Up @@ -13,9 +13,25 @@

test(() => {
assert_throws(new TypeError, () => new NDEFRecord(null),
"The record has neither type nor data.");
'The record has neither type nor data.');
}, 'NDEFRecord constructor with null init dict');

test(() => {
const record = new NDEFRecord(createTextRecord(test_text_data));
assert_equals(record.recordType, 'text', 'recordType');
assert_equals(record.mediaType, 'text/plain', 'mediaType');
assert_equals(record.toText(), test_text_data,
'toText() has the same content with the original dictionary');
}, 'NDEFRecord constructor with text record type');

test(() => {
const record = new NDEFRecord(createUrlRecord(test_url_data));
assert_equals(record.recordType, 'url', 'recordType');
assert_equals(record.mediaType, 'text/plain', 'mediaType');
assert_equals(record.toText(), test_url_data,
'toText() has the same content with the original dictionary');
}, 'NDEFRecord constructor with url record type');

test(() => {
let buffer = new ArrayBuffer(4);
let buffer_view = new Uint8Array(buffer);
Expand Down Expand Up @@ -68,4 +84,19 @@
'toJSON() has the same content with the original dictionary');
}, 'NDEFRecord constructor with json data');

test(() => {
assert_throws(new TypeError, () => new NDEFRecord(createRecord('EMptY')),
'Unknown record type.');
assert_throws(new TypeError, () => new NDEFRecord(createRecord('TeXt', '', test_text_data)),
'Unknown record type.');
assert_throws(new TypeError, () => new NDEFRecord(createRecord('uRL', '', test_url_data)),
'Unknown record type.');
assert_throws(new TypeError, () => new NDEFRecord(createRecord('jSoN', '', test_json_data)),
'Unknown record type.');
assert_throws(new TypeError, () => new NDEFRecord(createRecord('OpaQUE', '', test_buffer_data)),
'Unknown record type.');
assert_throws(new TypeError, () => new NDEFRecord(createRecord('sMart-PosTER', '', test_url_data)),
'Unknown record type.');
}, 'NDEFRecord constructor with record type string being treated as case sensitive');

</script>

0 comments on commit 40b84d0

Please sign in to comment.