Commit 9b1e4190 authored by Leon Han's avatar Leon Han Committed by Commit Bot

[webnfc] NDEFRecordInit#recordType should be a required field.

The spec issue and PR:
https://github.com/w3c/web-nfc/issues/497
https://github.com/w3c/web-nfc/pull/498

BUG=520391

Change-Id: Ie6e3af86f0441bad03b7454b3b7285c6824d34fe
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1983433
Commit-Queue: Leon Han <leon.han@intel.com>
Reviewed-by: default avatarRijubrata Bhaumik <rijubrata.bhaumik@intel.com>
Cr-Commit-Position: refs/heads/master@{#728114}
parent 39ea06a7
...@@ -251,21 +251,11 @@ static NDEFRecord* CreateExternalRecord(const String& custom_type, ...@@ -251,21 +251,11 @@ static NDEFRecord* CreateExternalRecord(const String& custom_type,
NDEFRecord* NDEFRecord::Create(const ExecutionContext* execution_context, NDEFRecord* NDEFRecord::Create(const ExecutionContext* execution_context,
const NDEFRecordInit* init, const NDEFRecordInit* init,
ExceptionState& exception_state) { ExceptionState& exception_state) {
// https://w3c.github.io/web-nfc/#creating-web-nfc-message // https://w3c.github.io/web-nfc/#creating-ndef-record
String record_type;
if (!init->hasRecordType()) { // NDEFRecordInit#recordType is a required field.
if (!init->hasData()) { DCHECK(init->hasRecordType());
exception_state.ThrowTypeError("The record has neither type nor data."); const String& record_type = init->recordType();
return nullptr;
}
if (init->data().IsString()) {
record_type = "text";
} else {
record_type = "mime";
}
} else {
record_type = init->recordType();
}
// https://w3c.github.io/web-nfc/#dom-ndefrecordinit-mediatype // https://w3c.github.io/web-nfc/#dom-ndefrecordinit-mediatype
if (init->hasMediaType() && record_type != "mime") { if (init->hasMediaType() && record_type != "mime") {
......
...@@ -13,7 +13,7 @@ typedef (DOMString or ArrayBuffer or ArrayBufferView or NDEFMessageInit) ...@@ -13,7 +13,7 @@ typedef (DOMString or ArrayBuffer or ArrayBufferView or NDEFMessageInit)
// https://w3c.github.io/web-nfc/#dom-ndefrecordinit // https://w3c.github.io/web-nfc/#dom-ndefrecordinit
dictionary NDEFRecordInit { dictionary NDEFRecordInit {
USVString recordType; required USVString recordType;
USVString mediaType; USVString mediaType;
USVString id; USVString id;
......
...@@ -13,9 +13,14 @@ ...@@ -13,9 +13,14 @@
test(() => { test(() => {
assert_throws(new TypeError, () => new NDEFRecord(null), assert_throws(new TypeError, () => new NDEFRecord(null),
'The record has neither type nor data.'); 'NDEFRecordInit#recordType is a required field.');
}, 'NDEFRecord constructor with null init dict'); }, 'NDEFRecord constructor with null init dict');
test(() => {
assert_throws(new TypeError, () => new NDEFRecord({id: test_record_id, data: test_text_data}),
'NDEFRecordInit#recordType is a required field.');
}, 'NDEFRecord constructor without NDEFRecordInit#recordType field');
test(() => { test(() => {
assert_throws(new TypeError, () => new NDEFRecord( assert_throws(new TypeError, () => new NDEFRecord(
createRecord('empty', test_text_data, test_record_id)), createRecord('empty', test_text_data, test_record_id)),
......
...@@ -23,8 +23,6 @@ PASS Check that provided NDEFPushOptions values are correctly converted. ...@@ -23,8 +23,6 @@ PASS Check that provided NDEFPushOptions values are correctly converted.
PASS NDEFWriter.push should read data when ignoreRead is false. PASS NDEFWriter.push should read data when ignoreRead is false.
PASS NDEFWriter.push should ignore reading data when ignoreRead is true. PASS NDEFWriter.push should ignore reading data when ignoreRead is true.
PASS NDEFWriter.push should replace all previously configured push operations. PASS NDEFWriter.push should replace all previously configured push operations.
PASS Test that recordType should be set to 'text' if NDEFRecordInit.record's recordType is undefined and NDEFRecordInit.record's data is DOMString.
PASS Test that recordType should be set to 'mime' if NDEFRecordInit.record's recordType is undefined and NDEFRecordInit.record's data is not DOMString.
PASS Test that mediaType should be set to 'application/octet-stream' if NDEFRecordInit.record's recordType is 'mime' and NDEFRecordInit.record's mediaType is undefined. PASS Test that mediaType should be set to 'application/octet-stream' if NDEFRecordInit.record's recordType is 'mime' and NDEFRecordInit.record's mediaType is undefined.
PASS NDEFWriter.push should fail when the NFC device coming up does not expose NDEF technology. PASS NDEFWriter.push should fail when the NFC device coming up does not expose NDEF technology.
PASS NDEFWriter.push should succeed to push data to an unformatted NFC device when the NDEFPushOptions.overwrite is false. PASS NDEFWriter.push should succeed to push data to an unformatted NFC device when the NDEFPushOptions.overwrite is false.
......
...@@ -386,20 +386,6 @@ nfc_test(async (t, mockNFC) => { ...@@ -386,20 +386,6 @@ nfc_test(async (t, mockNFC) => {
}); });
}, "NDEFWriter.push should replace all previously configured push operations."); }, "NDEFWriter.push should replace all previously configured push operations.");
nfc_test(async (t, mockNFC) => {
const writer = new NDEFWriter();
await writer.push({ records: [{ data: test_text_data}] });
assertNDEFMessagesEqual(test_text_data, mockNFC.pushedMessage());
}, "Test that recordType should be set to 'text' if NDEFRecordInit.record's \
recordType is undefined and NDEFRecordInit.record's data is DOMString.");
nfc_test(async (t, mockNFC) => {
const writer = new NDEFWriter();
await writer.push({ records: [{ data: test_buffer_data}] });
assertNDEFMessagesEqual(test_buffer_data, mockNFC.pushedMessage());
}, "Test that recordType should be set to 'mime' if NDEFRecordInit.record's \
recordType is undefined and NDEFRecordInit.record's data is not DOMString.");
nfc_test(async (t, mockNFC) => { nfc_test(async (t, mockNFC) => {
const writer = new NDEFWriter(); const writer = new NDEFWriter();
await writer.push({ records: [{ recordType: "mime", data: test_buffer_data }] }); await writer.push({ records: [{ recordType: "mime", data: test_buffer_data }] });
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment