Skip to content

Commit

Permalink
[webnfc] Replace NDEFRecordData#data() with multiple getters
Browse files Browse the repository at this point in the history
The spec change was introduced at
w3c/web-nfc#243.

BUG=520391
TBR=dcheng
for just adding a TODO into
third_party/blink/renderer/modules/nfc/nfc_type_converters.h

Change-Id: Iee1841352417065cdf13b08a82ac79cfd6cdb7a5
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1755642
Commit-Queue: Leon Han <leon.han@intel.com>
Reviewed-by: Reilly Grant <reillyg@chromium.org>
Reviewed-by: Rijubrata Bhaumik <rijubrata.bhaumik@intel.com>
Cr-Commit-Position: refs/heads/master@{#688007}
  • Loading branch information
Leon Han authored and chromium-wpt-export-bot committed Aug 19, 2019
1 parent 1313b7c commit 2b79496
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 43 deletions.
10 changes: 7 additions & 3 deletions web-nfc/NDEFMessage_constructor.https.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,13 @@
assert_equals(message.records.length, 1, 'one text record');
assert_equals(message.records[0].recordType, 'text', 'messageType');
assert_equals(message.records[0].mediaType, 'text/plain', 'mediaType');
assert_true(typeof message.records[0].data() === 'string');
assert_equals(message.records[0].data(), test_text_data,
'data() contains the same text content');
assert_true(typeof message.records[0].toText() === 'string');
assert_equals(message.records[0].toText(), test_text_data,
'toText() contains the same text content');
assert_equals(message.records[0].toArrayBuffer(), null,
'toArrayBuffer() returns null');
assert_equals(message.records[0].toJSON(), undefined,
'toJSON() returns undefined');
}, 'NDEFMessage constructor with a text record');

</script>
38 changes: 21 additions & 17 deletions web-nfc/NDEFRecord_constructor.https.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
const record = new NDEFRecord(null);
assert_equals(record.recordType.length, 0, 'empty recordType');
assert_equals(record.mediaType.length, 0, 'empty mediaType');
assert_equals(record.data(), null, 'null data');
assert_equals(record.toText().length, 0, 'toText() gets an empty string');
assert_equals(record.toArrayBuffer(), null, 'toArrayBuffer() returns null');
assert_equals(record.toJSON(), undefined, 'toJSON() returns undefined');
}, 'NDEFRecord constructor with null init dict');

test(() => {
Expand All @@ -27,45 +29,47 @@
assert_equals(record.recordType, 'opaque', 'recordType');
assert_equals(record.mediaType, 'application/octet-stream', 'mediaType');

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

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

buffer_view.set([4, 3, 2, 1]);
const data_3 = record.data();
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 data() content');
'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 data() content');
'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 data() content');
'Modifying the original buffer does not affect toArrayBuffer() content');
}, 'NDEFRecord constructor with opaque data');

test(() => {
const record = new NDEFRecord(createJsonRecord(test_json_data));
assert_equals(record.recordType, 'json', 'recordType');
assert_equals(record.mediaType, 'application/json', 'mediaType');

const data_1 = record.data();
const data_1 = record.toJSON();
assert_true(typeof data_1 === 'object');
assert_not_equals(data_1, test_json_data, 'data() returns a new object');
assert_not_equals(data_1, test_json_data, 'toJSON() returns a new object');
assert_object_equals(data_1, test_json_data,
'data() has the same content with the original dictionary');
'toJSON() has the same content with the original dictionary');

const data_2 = record.data();
const data_2 = record.toJSON();
assert_true(typeof data_2 === 'object');
assert_not_equals(data_2, data_1, 'data() again returns another new object');
assert_not_equals(data_2, data_1,
'toJSON() again returns another new object');
assert_object_equals(data_2, test_json_data,
'data() has the same content with the original dictionary');
'toJSON() has the same content with the original dictionary');
}, 'NDEFRecord constructor with json data');

</script>
2 changes: 1 addition & 1 deletion web-nfc/NFCReadingEvent_constructor.https.html
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
const event = new NFCReadingEvent('type', {serialNumber: '', message: message});
assert_equals(event.type, 'type', 'type');
assert_equals(event.serialNumber, '', 'serialNumber');
assertWebNDEFMessagesEqual(event.message, message);
assertWebNDEFMessagesEqual(event.message, new NDEFMessage(message));
}, 'NFCReadingEvent constructor with valid parameters');

</script>
56 changes: 34 additions & 22 deletions web-nfc/resources/nfc-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,27 +136,39 @@ function assertNDEFMessagesEqual(providedMessage, receivedMessage) {
compareNDEFRecords(provided.records[i], receivedMessage.data[i]);
}

// Used to compare two WebNFC messages, one that is provided to mock NFC
// service and another that is received from NFCWriter.onreading() EventHandler.
function assertWebNDEFMessagesEqual(a, b) {
if (b.url) assert_equals(a.url, b.url);
assert_equals(a.records.length, b.records.length);
for(let i in a.records) {
let recordA = a.records[i];
let recordB = b.records[i];
assert_equals(recordA.recordType, recordB.recordType);
assert_equals(recordA.mediaType, recordB.mediaType);
if (recordA.data() == null) {
assert_true(recordB.data == null);
} else if (recordA.data() instanceof ArrayBuffer) {
assert_array_equals(new Uint8Array(recordA.data()),
new Uint8Array(recordB.data));
} else if (typeof recordA.data() === 'object') {
assert_object_equals(recordA.data(), recordB.data);
} else if (typeof recordA.data() === 'number'
|| typeof recordA.data() === 'string') {
assert_true(recordA.data() == recordB.data);
// Used to compare two NDEFMessage, one that is received from
// NFCWriter.onreading() EventHandler and another that is provided to mock NFC
// service.
function assertWebNDEFMessagesEqual(message, expectedMessage) {
if (expectedMessage.url)
assert_equals(message.url, expectedMessage.url);

assert_equals(message.records.length, expectedMessage.records.length);

for(let i in message.records) {
let record = message.records[i];
let expectedRecord = expectedMessage.records[i];
assert_equals(record.recordType, expectedRecord.recordType);
assert_equals(record.mediaType, expectedRecord.mediaType);

// Compares record data
assert_equals(record.toText(), expectedRecord.toText());
assert_array_equals(new Uint8Array(record.toArrayBuffer()),
new Uint8Array(expectedRecord.toArrayBuffer()));
let json;
try {
json = record.toJSON();
} catch (e) {
}
let expectedJson;
try {
expectedJson = expectedRecord.toJSON();
} catch (e) {
}
if (json === undefined)
assert_equals(expectedJson, undefined);
else
assert_object_equals(json, expectedJson);
}
}

Expand All @@ -176,7 +188,7 @@ function testNFCReaderOptions(message, readOptions, unmatchedReadOptions, desc)
const promise = readerWatcher.wait_for("reading").then(event => {
reader1.stop();
reader2.stop();
assertWebNDEFMessagesEqual(event.message, message);
assertWebNDEFMessagesEqual(event.message, new NDEFMessage(message));
});
// NFCReader#start() asynchronously dispatches the onreading event.
reader2.start();
Expand All @@ -192,7 +204,7 @@ function testReadingMultiMessages(message, readOptions, unmatchedMessage,

const promise = readerWatcher.wait_for("reading").then(event => {
reader.stop();
assertWebNDEFMessagesEqual(event.message, message);
assertWebNDEFMessagesEqual(event.message, new NDEFMessage(message));
});
// NFCReader#start() asynchronously dispatches the onreading event.
reader.start();
Expand Down

0 comments on commit 2b79496

Please sign in to comment.