Commit 1905d7a9 authored by Rijubrata Bhaumik's avatar Rijubrata Bhaumik Committed by Commit Bot

[webnfc] Prefix NDEF related objects with NDEF instead of NFC.

Rename NFCMessage to NDEFMessage.

nfc_message.idl -> ndef_message.idl

git grep -l 'NFCMessage' | xargs sed -i 's/NFCMessage/NDEFMessage/g'

git grep -l 'NfcMessage' | xargs sed -i 's/NfcMessage/NdefMessage/g'

Java does not allow to import same className from different packages,
which in this case is NdefMessage
So not importing android.nfc.NdefMessage and using fully qualified name
to avoid ambiguity.

Repeat the same for NFCRecord -> NDEFRecord.

Spec changes for this corresponding CL:
https://github.com/w3c/web-nfc/commit/5b74305be004a8e758d4931f8409bd354bc4039e

Bug: 520391
Change-Id: Ib88e0db5c9cd7fa973170f93c5f5352dcda6630e
Reviewed-on: https://chromium-review.googlesource.com/c/1481296Reviewed-by: default avatarKentaro Hara <haraken@chromium.org>
Reviewed-by: default avatarKinuko Yasuda <kinuko@chromium.org>
Reviewed-by: default avatarReilly Grant <reillyg@chromium.org>
Commit-Queue: Rijubrata Bhaumik <rijubrata.bhaumik@intel.com>
Cr-Commit-Position: refs/heads/master@{#635927}
parent 764db2c5
......@@ -11,10 +11,10 @@ android_library("java") {
"//services/device:*",
]
java_files = [
"java/src/org/chromium/device/nfc/InvalidNfcMessageException.java",
"java/src/org/chromium/device/nfc/InvalidNdefMessageException.java",
"java/src/org/chromium/device/nfc/NfcProviderImpl.java",
"java/src/org/chromium/device/nfc/NfcImpl.java",
"java/src/org/chromium/device/nfc/NfcMessageValidator.java",
"java/src/org/chromium/device/nfc/NdefMessageValidator.java",
"java/src/org/chromium/device/nfc/NfcTagHandler.java",
"java/src/org/chromium/device/nfc/NfcTypeConverter.java",
]
......
......@@ -5,6 +5,6 @@
package org.chromium.device.nfc;
/**
* Exception that raised when NfcMessage is found to be invalid during conversion to NdefMessage.
* Exception that raised when NdefMessage is found to be invalid during conversion to NdefMessage.
*/
public final class InvalidNfcMessageException extends Exception {}
\ No newline at end of file
public final class InvalidNdefMessageException extends Exception {}
\ No newline at end of file
......@@ -4,21 +4,21 @@
package org.chromium.device.nfc;
import org.chromium.device.mojom.NfcMessage;
import org.chromium.device.mojom.NfcRecord;
import org.chromium.device.mojom.NfcRecordType;
import org.chromium.device.mojom.NdefMessage;
import org.chromium.device.mojom.NdefRecord;
import org.chromium.device.mojom.NdefRecordType;
/**
* Utility class that provides validation of NfcMessage.
* Utility class that provides validation of NdefMessage.
*/
public final class NfcMessageValidator {
public final class NdefMessageValidator {
/**
* Validates NfcMessage.
* Validates NdefMessage.
*
* @param message to be validated.
* @return true if message is valid, false otherwise.
*/
public static boolean isValid(NfcMessage message) {
public static boolean isValid(NdefMessage message) {
if (message == null || message.data == null || message.data.length == 0) {
return false;
}
......@@ -30,12 +30,12 @@ public final class NfcMessageValidator {
}
/**
* Checks that NfcRecord#data and NfcRecord#mediaType fields are valid. NfcRecord#data and
* NfcRecord#mediaType fields are omitted for the record with EMPTY type.
* Checks that NdefRecord#data and NdefRecord#mediaType fields are valid. NdefRecord#data and
* NdefRecord#mediaType fields are omitted for the record with EMPTY type.
*/
private static boolean isValid(NfcRecord record) {
private static boolean isValid(NdefRecord record) {
if (record == null) return false;
if (record.recordType == NfcRecordType.EMPTY) return true;
if (record.recordType == NdefRecordType.EMPTY) return true;
return record.data != null && record.mediaType != null && !record.mediaType.isEmpty();
}
}
......@@ -10,7 +10,6 @@ import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.nfc.FormatException;
import android.nfc.NdefMessage;
import android.nfc.NfcAdapter;
import android.nfc.NfcAdapter.ReaderCallback;
import android.nfc.NfcManager;
......@@ -24,11 +23,11 @@ import android.util.SparseArray;
import org.chromium.base.Callback;
import org.chromium.base.ContextUtils;
import org.chromium.base.Log;
import org.chromium.device.mojom.NdefMessage;
import org.chromium.device.mojom.Nfc;
import org.chromium.device.mojom.NfcClient;
import org.chromium.device.mojom.NfcError;
import org.chromium.device.mojom.NfcErrorType;
import org.chromium.device.mojom.NfcMessage;
import org.chromium.device.mojom.NfcPushOptions;
import org.chromium.device.mojom.NfcPushTarget;
import org.chromium.device.mojom.NfcWatchMode;
......@@ -81,7 +80,7 @@ public class NfcImpl implements Nfc {
/**
* Object that contains data that was passed to method
* #push(NfcMessage message, NfcPushOptions options, PushResponse callback)
* #push(NdefMessage message, NfcPushOptions options, PushResponse callback)
* @see PendingPushOperation
*/
private PendingPushOperation mPendingPushOperation;
......@@ -93,7 +92,7 @@ public class NfcImpl implements Nfc {
private NfcTagHandler mTagHandler;
/**
* Client interface used to deliver NFCMessages for registered watch operations.
* Client interface used to deliver NdefMessages for registered watch operations.
* @see #watch
*/
private NfcClient mClient;
......@@ -107,7 +106,7 @@ public class NfcImpl implements Nfc {
* Map of watchId <-> NfcWatchOptions. All NfcWatchOptions are matched against tag that is in
* proximity, when match algorithm (@see #matchesWatchOptions) returns true, watcher with
* corresponding ID would be notified using NfcClient interface.
* @see NfcClient#onWatch(int[] id, NfcMessage message)
* @see NfcClient#onWatch(int[] id, NdefMessage message)
*/
private final SparseArray<NfcWatchOptions> mWatchers = new SparseArray<>();
......@@ -164,7 +163,7 @@ public class NfcImpl implements Nfc {
/**
* Sets NfcClient. NfcClient interface is used to notify mojo NFC service client when NFC
* device is in proximity and has NfcMessage that matches NfcWatchOptions criteria.
* device is in proximity and has NdefMessage that matches NfcWatchOptions criteria.
* @see Nfc#watch(NfcWatchOptions options, WatchResponse callback)
*
* @param client @see NfcClient
......@@ -175,7 +174,7 @@ public class NfcImpl implements Nfc {
}
/**
* Pushes NfcMessage to Tag or Peer, whenever NFC device is in proximity. At the moment, only
* Pushes NdefMessage to Tag or Peer, whenever NFC device is in proximity. At the moment, only
* passive NFC devices are supported (NfcPushTarget.TAG).
*
* @param message that should be pushed to NFC device.
......@@ -183,10 +182,10 @@ public class NfcImpl implements Nfc {
* @param callback that is used to notify when push operation is completed.
*/
@Override
public void push(NfcMessage message, NfcPushOptions options, PushResponse callback) {
public void push(NdefMessage message, NfcPushOptions options, PushResponse callback) {
if (!checkIfReady(callback)) return;
if (!NfcMessageValidator.isValid(message)) {
if (!NdefMessageValidator.isValid(message)) {
callback.call(createError(NfcErrorType.INVALID_MESSAGE));
return;
}
......@@ -237,13 +236,13 @@ public class NfcImpl implements Nfc {
}
/**
* Watch method allows to set filtering criteria for NfcMessages that are found when NFC device
* Watch method allows to set filtering criteria for NdefMessages that are found when NFC device
* is within proximity. On success, watch ID is returned to caller through WatchResponse
* callback. When NfcMessage that matches NfcWatchOptions is found, it is passed to NfcClient
* callback. When NdefMessage that matches NfcWatchOptions is found, it is passed to NfcClient
* interface together with corresponding watch ID.
* @see NfcClient#onWatch(int[] id, NfcMessage message)
* @see NfcClient#onWatch(int[] id, NdefMessage message)
*
* @param options used to filter NfcMessages, @see NfcWatchOptions.
* @param options used to filter NdefMessages, @see NfcWatchOptions.
* @param callback that is used to notify caller when watch() is completed and return watch ID.
*/
@Override
......@@ -324,13 +323,13 @@ public class NfcImpl implements Nfc {
* Holds information about pending push operation.
*/
private static class PendingPushOperation {
public final NfcMessage nfcMessage;
public final NdefMessage ndefMessage;
public final NfcPushOptions nfcPushOptions;
private final PushResponse mPushResponseCallback;
public PendingPushOperation(
NfcMessage message, NfcPushOptions options, PushResponse callback) {
nfcMessage = message;
NdefMessage message, NfcPushOptions options, PushResponse callback) {
ndefMessage = message;
nfcPushOptions = options;
mPushResponseCallback = callback;
}
......@@ -500,10 +499,10 @@ public class NfcImpl implements Nfc {
try {
mTagHandler.connect();
mTagHandler.write(NfcTypeConverter.toNdefMessage(mPendingPushOperation.nfcMessage));
mTagHandler.write(NfcTypeConverter.toNdefMessage(mPendingPushOperation.ndefMessage));
pendingPushOperationCompleted(null);
} catch (InvalidNfcMessageException e) {
Log.w(TAG, "Cannot write data to NFC tag. Invalid NfcMessage.");
} catch (InvalidNdefMessageException e) {
Log.w(TAG, "Cannot write data to NFC tag. Invalid NdefMessage.");
pendingPushOperationCompleted(createError(NfcErrorType.INVALID_MESSAGE));
} catch (TagLostException e) {
Log.w(TAG, "Cannot write data to NFC tag. Tag is lost.");
......@@ -515,7 +514,7 @@ public class NfcImpl implements Nfc {
}
/**
* Reads NfcMessage from a tag and forwards message to matching method.
* Reads NdefMessage from a tag and forwards message to matching method.
*/
private void processPendingWatchOperations() {
if (mTagHandler == null || mClient == null || mWatchers.size() == 0) return;
......@@ -530,13 +529,13 @@ public class NfcImpl implements Nfc {
return;
}
NdefMessage message = null;
android.nfc.NdefMessage message = null;
try {
mTagHandler.connect();
message = mTagHandler.read();
if (message.getByteArrayLength() > NfcMessage.MAX_SIZE) {
Log.w(TAG, "Cannot read data from NFC tag. NfcMessage exceeds allowed size.");
if (message.getByteArrayLength() > NdefMessage.MAX_SIZE) {
Log.w(TAG, "Cannot read data from NFC tag. NdefMessage exceeds allowed size.");
return;
}
} catch (TagLostException e) {
......@@ -550,15 +549,15 @@ public class NfcImpl implements Nfc {
/**
* Iterates through active watchers and if any of those match NfcWatchOptions criteria,
* delivers NfcMessage to the client.
* delivers NdefMessage to the client.
*/
private void notifyMatchingWatchers(NdefMessage message) {
private void notifyMatchingWatchers(android.nfc.NdefMessage message) {
try {
NfcMessage nfcMessage = NfcTypeConverter.toNfcMessage(message);
NdefMessage ndefMessage = NfcTypeConverter.toNdefMessage(message);
List<Integer> watchIds = new ArrayList<Integer>();
for (int i = 0; i < mWatchers.size(); i++) {
NfcWatchOptions options = mWatchers.valueAt(i);
if (matchesWatchOptions(nfcMessage, options)) watchIds.add(mWatchers.keyAt(i));
if (matchesWatchOptions(ndefMessage, options)) watchIds.add(mWatchers.keyAt(i));
}
if (watchIds.size() != 0) {
......@@ -566,17 +565,17 @@ public class NfcImpl implements Nfc {
for (int i = 0; i < watchIds.size(); ++i) {
ids[i] = watchIds.get(i).intValue();
}
mClient.onWatch(ids, nfcMessage);
mClient.onWatch(ids, ndefMessage);
}
} catch (UnsupportedEncodingException e) {
Log.w(TAG, "Cannot convert NdefMessage to NfcMessage.");
Log.w(TAG, "Cannot convert NdefMessage to NdefMessage.");
}
}
/**
* Implements matching algorithm.
*/
private boolean matchesWatchOptions(NfcMessage message, NfcWatchOptions options) {
private boolean matchesWatchOptions(NdefMessage message, NfcWatchOptions options) {
// Valid WebNFC message must have non-empty url.
if (options.mode == NfcWatchMode.WEBNFC_ONLY
&& (message.url == null || message.url.isEmpty())) {
......
......@@ -16,7 +16,7 @@ enum NFCErrorType {
IO_ERROR
};
enum NFCRecordType {
enum NDEFRecordType {
EMPTY,
TEXT,
URL,
......@@ -46,20 +46,20 @@ struct NFCError {
NFCErrorType error_type;
};
struct NFCRecord {
// The type of NFCRecord.
NFCRecordType record_type;
struct NDEFRecord {
// The type of NDEFRecord.
NDEFRecordType record_type;
// Represents the IANA media type of the NFCRecord data field.
// Represents the IANA media type of the NDEFRecord data field.
string? media_type;
// Payload of the NFCRecord.
// Payload of the NDEFRecord.
array<uint8> data;
};
struct NFCMessage {
// The body of the NFCMessage is a collection of NFCRecord objects.
array<NFCRecord> data;
struct NDEFMessage {
// The body of the NDEFMessage is a collection of NDEFRecord objects.
array<NDEFRecord> data;
// The |url| field is an ASCII serialized origin, optionally followed by a URL
// path. It represents Web NFC id, that can be used for matching Web NFC
......@@ -82,8 +82,8 @@ struct NFCPushOptions {
bool ignore_read;
};
struct NFCRecordTypeFilter {
NFCRecordType record_type;
struct NDEFRecordTypeFilter {
NDEFRecordType record_type;
};
struct NFCWatchOptions {
......@@ -91,7 +91,7 @@ struct NFCWatchOptions {
string? url;
// Defines filtering constraint for NFC records with specified record type.
NFCRecordTypeFilter? record_filter;
NDEFRecordTypeFilter? record_filter;
// Defines media type filtering constraint.
string? media_type;
......@@ -101,7 +101,7 @@ struct NFCWatchOptions {
};
interface NFC {
// NFCClient interface is used to notify |client| when NFCMessage matches one
// NFCClient interface is used to notify |client| when NDEFMessage matches one
// or more pending watch operations.
SetClient(NFCClient client);
......@@ -109,7 +109,7 @@ interface NFC {
// NFCPushOptions specify timeout and type of device where data should be
// pushed. If timeout is defined and data is not pushed before timeout is
// expired, callback with corresponding error is called.
Push(NFCMessage message, NFCPushOptions? options) => (NFCError? error);
Push(NDEFMessage message, NFCPushOptions? options) => (NFCError? error);
// Cancels pending push request.
CancelPush(NFCPushTarget target) => (NFCError? error);
......@@ -133,5 +133,5 @@ interface NFC {
};
interface NFCClient {
OnWatch(array<uint32> watch_ids, NFCMessage message);
OnWatch(array<uint32> watch_ids, NDEFMessage message);
};
......@@ -68,8 +68,8 @@ bindings_modules_generated_union_type_files = [
"$bindings_modules_v8_output_dir/rendering_context.h",
"$bindings_modules_v8_output_dir/request_or_usv_string_or_request_or_usv_string_sequence.cc",
"$bindings_modules_v8_output_dir/request_or_usv_string_or_request_or_usv_string_sequence.h",
"$bindings_modules_v8_output_dir/string_or_array_buffer_or_nfc_message.cc",
"$bindings_modules_v8_output_dir/string_or_array_buffer_or_nfc_message.h",
"$bindings_modules_v8_output_dir/string_or_array_buffer_or_ndef_message.cc",
"$bindings_modules_v8_output_dir/string_or_array_buffer_or_ndef_message.h",
"$bindings_modules_v8_output_dir/string_or_canvas_gradient_or_canvas_pattern.cc",
"$bindings_modules_v8_output_dir/string_or_canvas_gradient_or_canvas_pattern.h",
"$bindings_modules_v8_output_dir/string_or_string_sequence_or_constrain_dom_string_parameters.cc",
......
......@@ -602,9 +602,9 @@ modules_dictionary_idl_files =
"mediastream/media_track_constraints.idl",
"mediastream/media_track_settings.idl",
"mediastream/media_track_supported_constraints.idl",
"nfc/nfc_message.idl",
"nfc/ndef_message.idl",
"nfc/nfc_push_options.idl",
"nfc/nfc_record.idl",
"nfc/ndef_record.idl",
"nfc/nfc_watch_options.idl",
"notifications/get_notification_options.idl",
"notifications/notification_action.idl",
......
......@@ -2,9 +2,9 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// https://w3c.github.io/web-nfc/#the-nfcmessage-dictionary
// https://w3c.github.io/web-nfc/#dom-ndefmessage
dictionary NFCMessage {
sequence<NFCRecord> records;
dictionary NDEFMessage {
sequence<NDEFRecord> records;
USVString url;
};
......@@ -4,14 +4,14 @@
// https://w3c.github.io/web-nfc/#the-nfcrecord-dictionary
enum NFCRecordType { "empty", "text", "url", "json", "opaque" };
enum NDEFRecordType { "empty", "text", "url", "json", "opaque" };
// TODO(shalamov): This is blocked by https://crbug.com/524424
// typedef (DOMString or unrestricted double or ArrayBuffer or Dictionary) NFCRecordData;
typedef any NFCRecordData;
// TODO(riju): Fix http://crbug.com/935912
// typedef (DOMString or unrestricted double or ArrayBuffer or Dictionary) NDEFRecordData;
typedef any NDEFRecordData;
dictionary NFCRecord {
NFCRecordType recordType;
dictionary NDEFRecord {
NDEFRecordType recordType;
USVString mediaType;
NFCRecordData data;
NDEFRecordData data;
};
......@@ -8,7 +8,7 @@
#include "mojo/public/cpp/bindings/binding.h"
#include "services/device/public/mojom/nfc.mojom-blink.h"
#include "third_party/blink/renderer/bindings/core/v8/script_promise.h"
#include "third_party/blink/renderer/bindings/modules/v8/string_or_array_buffer_or_nfc_message.h"
#include "third_party/blink/renderer/bindings/modules/v8/string_or_array_buffer_or_ndef_message.h"
#include "third_party/blink/renderer/bindings/modules/v8/v8_message_callback.h"
#include "third_party/blink/renderer/core/execution_context/context_lifecycle_observer.h"
#include "third_party/blink/renderer/core/page/page_visibility_observer.h"
......@@ -18,7 +18,7 @@
namespace blink {
class NFCPushOptions;
using NFCPushMessage = StringOrArrayBufferOrNFCMessage;
using NFCPushMessage = StringOrArrayBufferOrNDEFMessage;
class NFCWatchOptions;
class ScriptPromiseResolver;
......@@ -83,7 +83,7 @@ class NFC final : public ScriptWrappable,
// device::mojom::blink::NFCClient implementation.
void OnWatch(const Vector<uint32_t>& ids,
device::mojom::blink::NFCMessagePtr) override;
device::mojom::blink::NDEFMessagePtr) override;
private:
device::mojom::blink::NFCPtr nfc_;
......
......@@ -4,7 +4,7 @@
// https://w3c.github.io/web-nfc/#the-nfc-interface
typedef (DOMString or ArrayBuffer or NFCMessage) NFCPushMessage;
typedef (DOMString or ArrayBuffer or NDEFMessage) NFCPushMessage;
[
RuntimeEnabled=WebNFC,
......@@ -17,4 +17,4 @@ typedef (DOMString or ArrayBuffer or NFCMessage) NFCPushMessage;
};
// https://w3c.github.io/web-nfc/#dom-messagecallback
callback MessageCallback = void (NFCMessage message);
callback MessageCallback = void (NDEFMessage message);
......@@ -8,7 +8,7 @@ enum NFCWatchMode { "web-nfc-only", "any" };
dictionary NFCWatchOptions {
USVString url = "";
NFCRecordType? recordType;
NDEFRecordType? recordType;
USVString mediaType = "";
NFCWatchMode mode = "web-nfc-only";
};
This is a testharness.js-based test.
Harness Error. harness_status.status = 1 , harness_status.message = WebNFC is not supported.
PASS Test that promise is rejected with TypeError if NFCMessage is invalid.
PASS 'Test that promise is rejected with SyntaxError if NFCMessage contains invalid records.
PASS Test that promise is rejected with TypeError if NDEFMessage is invalid.
PASS 'Test that promise is rejected with SyntaxError if NDEFMessage contains invalid records.
PASS nfc.push should fail with TypeError when invalid timeout is provided.
PASS nfc.push should fail with TypeError when invalid negative timeout value is provided.
FAIL nfc.push should fail with TimeoutError when timer expires. assert_throws: function "function() { throw e }" threw object "NotSupportedError: NFC operation not supported." that is not a DOMException TimeoutError: property "code" is equal to 9, expected 23
......
......@@ -18,41 +18,41 @@ const invalid_type_messages =
// Invalid NFCPushMessage type
undefined,
// NFCMessage.records: should have at least 1 valid record.
// NDEFMessage.records: should have at least 1 valid record.
// https://w3c.github.io/web-nfc/#the-push-method - Step 8.
createMessage([{}]),
// https://w3c.github.io/web-nfc/#dfn-map-text-to-ndef
// NFCRecord must have data.
// NDEFRecord must have data.
createMessage([createTextRecord()]),
// NFCRecord.data for 'text' record must be number or string.
// NDEFRecord.data for 'text' record must be number or string.
createMessage([createTextRecord(test_buffer_data)]),
createMessage([createTextRecord(test_json_data)]),
// https://w3c.github.io/web-nfc/#dfn-map-a-json-object-to-ndef
// NFCRecord must have data.
// NDEFRecord must have data.
createMessage([createJsonRecord()]),
// NFCRecord.data for 'json' record must be object.
// NDEFRecord.data for 'json' record must be object.
createMessage([createJsonRecord(test_buffer_data)]),
createMessage([createJsonRecord(test_number_data)]),
createMessage([createJsonRecord(test_text_data)]),
// https://w3c.github.io/web-nfc/#dfn-map-a-url-to-ndef
// NFCRecord must have data.
// NDEFRecord must have data.
createMessage([createUrlRecord()]),
// NFCRecord.data for 'url' record must be string.
// NDEFRecord.data for 'url' record must be string.
createMessage([createUrlRecord(test_buffer_data)]),
createMessage([createUrlRecord(test_number_data)]),
createMessage([createUrlRecord(test_json_data)]),
// https://w3c.github.io/web-nfc/#dfn-map-binary-data-to-ndef
// NFCRecord must have data.
// NDEFRecord must have data.
createMessage([createOpaqueRecord()]),
// NFCRecord.data for 'opaque' record must be ArrayBuffer.
// NDEFRecord.data for 'opaque' record must be ArrayBuffer.
createMessage([createOpaqueRecord(test_text_data)]),
createMessage([createOpaqueRecord(test_number_data)]),
createMessage([createOpaqueRecord(test_json_data)])
......@@ -60,14 +60,14 @@ const invalid_type_messages =
const invalid_syntax_messages =
[
// NFCRecord.mediaType for 'text' record must be 'text/*'.
// NDEFRecord.mediaType for 'text' record must be 'text/*'.
createMessage([createRecord('text', 'application/json',
test_number_data)]),
// Data for 'url' record, must be a valid URL.
createMessage([createUrlRecord('Invalid URL:// Data')]),
// NFCRecord.mediaType for 'json' record must be 'application/json' or
// NDEFRecord.mediaType for 'json' record must be 'application/json' or
// starts with 'application/' and ends with '+json'.
createMessage([createRecord('json', 'image/png', test_json_data)]),
createMessage([createRecord('json', 'application/x+y', test_json_data)]),
......@@ -81,7 +81,7 @@ promise_test(t => {
promise_rejects(t, new TypeError(), navigator.nfc.push(message)));
});
return Promise.all(promises);
}, "Test that promise is rejected with TypeError if NFCMessage is invalid.");
}, "Test that promise is rejected with TypeError if NDEFMessage is invalid.");
promise_test(t => {
let promises = [];
......@@ -90,7 +90,7 @@ promise_test(t => {
promise_rejects(t, 'SyntaxError', navigator.nfc.push(message)));
});
return Promise.all(promises);
}, "'Test that promise is rejected with SyntaxError if NFCMessage contains invalid records.");
}, "'Test that promise is rejected with SyntaxError if NDEFMessage contains invalid records.");
promise_test(t => {
return promise_rejects(t, new TypeError(), navigator.nfc.push(test_text_data, { timeout: "invalid"}));
......
......@@ -29,6 +29,6 @@ let watchOptions = {
}
let message = createMessage([createRecord('empty', '')]);
testNFCMessage(message, watchOptions, desc);
testNDEFMessage(message, watchOptions, desc);
</script>
......@@ -29,6 +29,6 @@ let watchOptions = {
}
let message = createMessage([createJsonRecord(test_json_data)]);
testNFCMessage(message, watchOptions, desc);
testNDEFMessage(message, watchOptions, desc);
</script>
......@@ -29,6 +29,6 @@ let watchOptions = {
}
let message = createMessage([createOpaqueRecord(test_buffer_data)]);
testNFCMessage(message, watchOptions, desc);
testNDEFMessage(message, watchOptions, desc);
</script>
......@@ -29,6 +29,6 @@ let watchOptions = {
}
let message = createMessage([createTextRecord(test_text_data)]);
testNFCMessage(message, watchOptions, desc);
testNDEFMessage(message, watchOptions, desc);
</script>
......@@ -30,6 +30,6 @@ let watchOptions = {
}
let message = createMessage([createUrlRecord(test_url_data)]);
testNFCMessage(message, watchOptions, desc);
testNDEFMessage(message, watchOptions, desc);
</script>
......@@ -47,7 +47,7 @@ function createUrlRecord(url) {
return createRecord('url', 'text/plain', url);
}
function assertWebNFCMessagesEqual(a, b) {
function assertWebNDEFMessagesEqual(a, b) {
assert_equals(a.records.length, b.records.length);
for(let i in a.records) {
let recordA = a.records[i];
......@@ -67,14 +67,14 @@ function assertWebNFCMessagesEqual(a, b) {
}
}
function testNFCMessage(pushedMessage, watchOptions, desc) {
function testNDEFMessage(pushedMessage, watchOptions, desc) {
promise_test(t => {
return navigator.nfc.push(pushedMessage)
.then(() => {
return new Promise(resolve => {
navigator.nfc.watch((message) => resolve(message), watchOptions);
}).then((message) => {
assertWebNFCMessagesEqual(message, pushedMessage);
assertWebNDEFMessagesEqual(message, pushedMessage);
});
});
}, desc);
......
......@@ -13,40 +13,40 @@ const invalid_type_messages =
// Invalid NFCPushMessage type
undefined,
// NFCMessage.records: should have at least 1 valid record.
// NDEFMessage.records: should have at least 1 valid record.
// https://w3c.github.io/web-nfc/#the-push-method - Step 8.
createMessage([{}]),
// https://w3c.github.io/web-nfc/#dfn-map-text-to-ndef
// NFCRecord must have data.
// NDEFRecord must have data.
createMessage([createTextRecord()]),
// NFCRecord.data for 'text' record must be number or string.
// NDEFRecord.data for 'text' record must be number or string.
createMessage([createTextRecord(test_buffer_data)]),
createMessage([createTextRecord(test_json_data)]),
// https://w3c.github.io/web-nfc/#dfn-map-a-json-object-to-ndef
// NFCRecord must have data.
// NDEFRecord must have data.
createMessage([createJsonRecord()]),
// NFCRecord.data for 'json' record must be object.
// NDEFRecord.data for 'json' record must be object.
createMessage([createJsonRecord(test_buffer_data)]),
createMessage([createJsonRecord(test_number_data)]),
createMessage([createJsonRecord(test_text_data)]),
// https://w3c.github.io/web-nfc/#dfn-map-a-url-to-ndef
// NFCRecord must have data.
// NDEFRecord must have data.
createMessage([createUrlRecord()]),
// NFCRecord.data for 'url' record must be string.
// NDEFRecord.data for 'url' record must be string.
createMessage([createUrlRecord(test_buffer_data)]),
createMessage([createUrlRecord(test_number_data)]),
createMessage([createUrlRecord(test_json_data)]),
// https://w3c.github.io/web-nfc/#dfn-map-binary-data-to-ndef
// NFCRecord must have data.
// NDEFRecord must have data.
createMessage([createOpaqueRecord()]),
// NFCRecord.data for 'opaque' record must be ArrayBuffer.
// NDEFRecord.data for 'opaque' record must be ArrayBuffer.
createMessage([createOpaqueRecord(test_text_data)]),
createMessage([createOpaqueRecord(test_number_data)]),
createMessage([createOpaqueRecord(test_json_data)])
......@@ -54,14 +54,14 @@ const invalid_type_messages =
const invalid_syntax_messages =
[
// NFCRecord.mediaType for 'text' record must be 'text/*'.
// NDEFRecord.mediaType for 'text' record must be 'text/*'.
createMessage([createRecord('text', 'application/json',
test_number_data)]),
// Data for 'url' record, must be a valid URL.
createMessage([createUrlRecord('Invalid URL:// Data')]),
// NFCRecord.mediaType for 'json' record must be 'application/json' or
// NDEFRecord.mediaType for 'json' record must be 'application/json' or
// starts with 'application/' and ends with '+json'.
createMessage([createRecord('json', 'image/png', test_json_data)]),
createMessage([createRecord('json', 'application/x+y', test_json_data)]),
......@@ -75,7 +75,7 @@ nfc_test(() => {
assertRejectsWithError(navigator.nfc.push(message), 'TypeError'));
});
return Promise.all(promises);
}, 'Test that promise is rejected with TypeError if NFCMessage is invalid.');
}, 'Test that promise is rejected with TypeError if NDEFMessage is invalid.');
nfc_test(() => {
let promises = [];
......@@ -84,7 +84,7 @@ nfc_test(() => {
assertRejectsWithError(navigator.nfc.push(message), 'SyntaxError'));
});
return Promise.all(promises);
}, 'Test that promise is rejected with SyntaxError if NFCMessage contains' +
}, 'Test that promise is rejected with SyntaxError if NDEFMessage contains' +
' invalid records.');
nfc_test(() => {
......@@ -129,18 +129,18 @@ nfc_test(async () => {
createUrlRecord(test_url_data)],
test_message_origin);
await navigator.nfc.push(message);
assertNFCMessagesEqual(message, mockNFC.pushedMessage());
}, 'nfc.push NFCMessage containing text, json, opaque and url records with'
assertNDEFMessagesEqual(message, mockNFC.pushedMessage());
}, 'nfc.push NDEFMessage containing text, json, opaque and url records with'
+ ' default NFCPushOptions.');
nfc_test(async () => {
await navigator.nfc.push(test_text_data);
assertNFCMessagesEqual(test_text_data, mockNFC.pushedMessage());
assertNDEFMessagesEqual(test_text_data, mockNFC.pushedMessage());
}, 'nfc.push String with default NFCPushOptions.');
nfc_test(async () => {
await navigator.nfc.push(test_buffer_data);
assertNFCMessagesEqual(test_buffer_data, mockNFC.pushedMessage());
assertNDEFMessagesEqual(test_buffer_data, mockNFC.pushedMessage());
}, 'nfc.push ArrayBuffer with default NFCPushOptions.');
nfc_test(() => {
......
......@@ -67,19 +67,19 @@ function createUrlRecord(url) {
return createRecord('url', 'text/plain', url);
}
function toMojoNFCRecordType(type) {
function toMojoNDEFRecordType(type) {
switch (type) {
case 'text':
return device.mojom.NFCRecordType.TEXT;
return device.mojom.NDEFRecordType.TEXT;
case 'url':
return device.mojom.NFCRecordType.URL;
return device.mojom.NDEFRecordType.URL;
case 'json':
return device.mojom.NFCRecordType.JSON;
return device.mojom.NDEFRecordType.JSON;
case 'opaque':
return device.mojom.NFCRecordType.OPAQUE_RECORD;
return device.mojom.NDEFRecordType.OPAQUE_RECORD;
}
return device.mojom.NFCRecordType.EMPTY;
return device.mojom.NDEFRecordType.EMPTY;
}
function toMojoNFCPushTarget(target) {
......@@ -101,20 +101,20 @@ function toMojoNFCWatchMode(mode) {
return device.mojom.NFCWatchMode.ANY;
}
// Converts between NFCMessage https://w3c.github.io/web-nfc/#dom-nfcmessage
// and mojo::NFCMessage structure, so that nfc.watch function can be tested.
function toMojoNFCMessage(message) {
let nfcMessage = new device.mojom.NFCMessage();
nfcMessage.url = message.url;
nfcMessage.data = [];
// Converts between NDEFMessage https://w3c.github.io/web-nfc/#dom-ndefmessage
// and mojom.NDEFMessage structure, so that nfc.watch function can be tested.
function toMojoNDEFMessage(message) {
let ndefMessage = new device.mojom.NDEFMessage();
ndefMessage.url = message.url;
ndefMessage.data = [];
for (let record of message.records)
nfcMessage.data.push(toMojoNFCRecord(record));
return nfcMessage;
ndefMessage.data.push(toMojoNDEFRecord(record));
return ndefMessage;
}
function toMojoNFCRecord(record) {
let nfcRecord = new device.mojom.NFCRecord();
nfcRecord.recordType = toMojoNFCRecordType(record.recordType);
function toMojoNDEFRecord(record) {
let nfcRecord = new device.mojom.NDEFRecord();
nfcRecord.recordType = toMojoNDEFRecordType(record.recordType);
nfcRecord.mediaType = record.mediaType;
nfcRecord.data = toByteArray(record.data);
return nfcRecord;
......@@ -136,12 +136,12 @@ function toByteArray(data) {
return byteArray;
}
// Compares NFCMessage that was provided to the API
// (e.g. navigator.nfc.push), and NFCMessage that was received by the
// Compares NDEFMessage that was provided to the API
// (e.g. navigator.nfc.push), and NDEFMessage that was received by the
// mock NFC service.
function assertNFCMessagesEqual(providedMessage, receivedMessage) {
function assertNDEFMessagesEqual(providedMessage, receivedMessage) {
// If simple data type is passed, e.g. String or ArrayBuffer, convert it
// to NFCMessage before comparing.
// to NDEFMessage before comparing.
// https://w3c.github.io/web-nfc/#idl-def-nfcpushmessage
let provided = providedMessage;
if (providedMessage instanceof ArrayBuffer)
......@@ -150,17 +150,17 @@ function assertNFCMessagesEqual(providedMessage, receivedMessage) {
provided = createMessage([createTextRecord(providedMessage)]);
assert_equals(provided.records.length, receivedMessage.data.length,
'NFCMessages must have same number of NFCRecords');
'NDEFMessages must have same number of NDEFRecords');
// Compare contents of each individual NFCRecord
// Compare contents of each individual NDEFRecord
for (let i = 0; i < provided.records.length; ++i)
compareNFCRecords(provided.records[i], receivedMessage.data[i]);
compareNDEFRecords(provided.records[i], receivedMessage.data[i]);
}
// Used to compare two WebNFC messages, one that is provided to mock NFC
// service through triggerWatchCallback and another that is received by
// callback that is provided to navigator.nfc.watch function.
function assertWebNFCMessagesEqual(a, b) {
function assertWebNDEFMessagesEqual(a, b) {
assert_equals(a.url, b.url);
assert_equals(a.records.length, b.records.length);
for(let i in a.records) {
......@@ -183,9 +183,9 @@ function assertWebNFCMessagesEqual(a, b) {
}
}
// Compares NFCRecords that were provided / received by the mock service.
function compareNFCRecords(providedRecord, receivedRecord) {
assert_equals(toMojoNFCRecordType(providedRecord.recordType),
// Compares NDEFRecords that were provided / received by the mock service.
function compareNDEFRecords(providedRecord, receivedRecord) {
assert_equals(toMojoNDEFRecordType(providedRecord.recordType),
receivedRecord.recordType);
// Compare media types without charset.
......@@ -194,8 +194,8 @@ function compareNFCRecords(providedRecord, receivedRecord) {
assert_equals(providedRecord.mediaType,
receivedRecord.mediaType.substring(0, providedRecord.mediaType.length));
assert_false(toMojoNFCRecordType(providedRecord.recordType) ==
device.mojom.NFCRecordType.EMPTY);
assert_false(toMojoNDEFRecordType(providedRecord.recordType) ==
device.mojom.NDEFRecordType.EMPTY);
assert_array_equals(toByteArray(providedRecord.data),
new Uint8Array(receivedRecord.data));
......@@ -240,7 +240,7 @@ function assertNFCWatchOptionsEqual(provided, received) {
if (provided.recordType !== undefined) {
assert_equals(!+received.record_filter, true);
assert_equals(toMojoNFCRecordType(provided.recordType),
assert_equals(toMojoNDEFRecordType(provided.recordType),
received.recordFilter.recordType);
}
}
......@@ -424,7 +424,7 @@ class MockNFC {
triggerWatchCallback(id, message) {
assert_true(this.client_ !== null);
if (this.watchers_.length > 0) {
this.client_.onWatch([id], toMojoNFCMessage(message));
this.client_.onWatch([id], toMojoNDEFMessage(message));
}
}
}
......
......@@ -62,7 +62,7 @@ nfc_test(async () => {
let messageCallback;
let promise = new Promise(resolve => {
messageCallback = receivedMessage => {
assertWebNFCMessagesEqual(message, receivedMessage);
assertWebNDEFMessagesEqual(message, receivedMessage);
resolve();
}
});
......@@ -70,7 +70,7 @@ nfc_test(async () => {
let id = await navigator.nfc.watch(messageCallback);
mockNFC.triggerWatchCallback(id, message);
return promise;
}, 'Test that watch callback is triggered with valid NFCMessage.');
}, 'Test that watch callback is triggered with valid NDEFMessage.');
nfc_test(() => {
return assertRejectsWithError(navigator.nfc.watch(noop, {url:"www.a.com"}),
......
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