Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(712)

Unified Diff: third_party/WebKit/LayoutTests/nfc/resources/nfc-helpers.js

Issue 1759373003: [webnfc] Implement nfc.watch in blink nfc module. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@implement_nfc_push_in_android
Patch Set: Rebased. Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: third_party/WebKit/LayoutTests/nfc/resources/nfc-helpers.js
diff --git a/third_party/WebKit/LayoutTests/nfc/resources/nfc-helpers.js b/third_party/WebKit/LayoutTests/nfc/resources/nfc-helpers.js
index cede4dacff017628d9cd12c8f0604af5d562d67b..cede7cbb2be26b86606a3a56e9ae96a7df91e1a8 100644
--- a/third_party/WebKit/LayoutTests/nfc/resources/nfc-helpers.js
+++ b/third_party/WebKit/LayoutTests/nfc/resources/nfc-helpers.js
@@ -37,6 +37,10 @@ function createNFCPushOptions(target, timeout, ignoreRead) {
return { target: target, timeout: timeout, ignoreRead: ignoreRead };
}
+function createNFCWatchOptions(url, recordType, mediaType, mode) {
+ return { url: url, recordType: recordType, mediaType: mediaType, mode: mode};
kenneth.christiansen 2016/04/25 08:22:48 Actually if you use the same name this is not need
shalamov 2016/04/25 11:31:03 Done.
+}
+
function createTextRecord(text) {
return createRecord('text', 'text/plain', text);
}
@@ -86,6 +90,36 @@ function nfc_mocks(mojo) {
return nfc.NFCPushTarget.ANY;
}
+ function toMojoNFCWatchMode(mode) {
+ switch (mode) {
+ case 'web-nfc-only':
+ return nfc.NFCWatchMode.WEBNFC_ONLY;
+ case 'any':
+ return nfc.NFCWatchMode.ANY;
kenneth.christiansen 2016/04/25 08:31:47 You can add a default here instead of the return b
shalamov 2016/04/25 11:31:03 Done.
+ }
+
+ return nfc.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 nfc.NFCMessage();
+ nfcMessage.url = message.url;
+ nfcMessage.data = new Array;
kenneth.christiansen 2016/04/25 08:31:47 wouldn't = [] do?
shalamov 2016/04/25 11:31:03 Done.
+ for(let i in message.data)
kenneth.christiansen 2016/04/25 08:31:46 I would add a space after 'for' Don't you mean 'o
shalamov 2016/04/25 11:31:03 Done.
+ nfcMessage.data.push(toMojoNFCRecord(message.data[i]));
+ return nfcMessage;
+ }
+
+ function toMojoNFCRecord(record) {
+ let nfcRecord = new nfc.NFCRecord();
+ nfcRecord.recordType = toMojoNFCRecordType(record.recordType);
+ nfcRecord.mediaType = record.mediaType;
+ nfcRecord.data = toByteArray(record.data);
+ return nfcRecord;
+ }
+
function toByteArray(data) {
// Convert JS objects to byte array
let byteArray;
@@ -123,6 +157,32 @@ function nfc_mocks(mojo) {
compareNFCRecords(provided.data[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) {
+ assert_equals(a.url, b.url);
+ assert_equals(a.data.length, b.data.length);
+ for(let i in a.data) {
+ let recordA = a.data[i];
+ let recordB = b.data[i];
+ assert_equals(recordA.recordType, recordB.recordType);
+ assert_equals(recordA.mediaType, recordB.mediaType);
+
+ 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);
+ }
+
+ if (typeof recordA.data === 'number'
+ || typeof recordA.data === 'string') {
+ assert_true(recordA.data == recordB.data);
+ }
+ }
+ }
+
// Compares NFCRecords that were provided / received by the mock service.
function compareNFCRecords(providedRecord, receivedRecord) {
assert_equals(toMojoNFCRecordType(providedRecord.recordType),
@@ -160,6 +220,31 @@ function nfc_mocks(mojo) {
assert_equals(received.target, nfc.NFCPushTarget.ANY);
}
+ // Compares NFCWatchOptions structures that were provided to API and
+ // received by the mock mojo service.
+ function assertNFCWatchOptionsEqual(provided, received) {
+ if (provided.url !== undefined)
+ assert_equals(provided.url, received.url);
+ else
+ assert_equals(received.url, '');
+
+ if (provided.mediaType !== undefined)
+ assert_equals(provided.mediaType, received.mediaType);
+ else
+ assert_equals(received.mediaType, '');
+
+ if (provided.mode !== undefined)
+ assert_equals(toMojoNFCWatchMode(provided.mode), received.mode);
+ else
+ assert_equals(received.mode, nfc.NFCWatchMode.WEBNFC_ONLY);
+
+ if (provided.recordType !== undefined) {
+ assert_equals(!+received.recordFilter, true);
+ assert_equals(toMojoNFCRecordType(provided.recordType),
+ received.recordFilter.recordType);
+ }
+ }
+
function createNFCError(type) {
return { error: type ?
new nfc.NFCError({ error_type: type }) : null };
@@ -175,6 +260,9 @@ function nfc_mocks(mojo) {
this.pending_promise_func_ = null;
this.push_timeout_id_ = null;
this.push_completed_ = true;
+ this.client_ = null;
+ this.watch_id_ = 0;
+ this.watchers_ = new Array;
kenneth.christiansen 2016/04/25 08:31:46 = [] ?
shalamov 2016/04/25 11:31:03 Done.
}
// NFC.stubClass delegate functions
@@ -209,6 +297,40 @@ function nfc_mocks(mojo) {
return Promise.resolve(createNFCError(null));
}
+ setClient(client) {
+ this.client_ = new nfc.NFCClient.proxyClass(new mojo.router.Router(client));
+ }
+
+ watch(options) {
+ let error = this.isReady();
+ if (error) {
+ error.id = 0;
+ return Promise.resolve(error);
+ }
+
+ let retVal = createNFCError(null);
+ retVal.id = ++this.watch_id_;
+ this.watchers_.push({id: this.watch_id_, options: options});
+ return Promise.resolve(retVal);
+ }
+
+ cancelWatch(id) {
+ let index = this.watchers_.findIndex(value => { return value.id === id;});
kenneth.christiansen 2016/04/25 08:31:47 you can remove the "{ return " and the "}" part :)
shalamov 2016/04/25 11:31:03 Done.
+ if (index === -1)
kenneth.christiansen 2016/04/25 08:31:47 we usually add {} even for single lines in JS
shalamov 2016/04/25 11:31:03 Done.
+ return Promise.resolve(createNFCError(nfc.NFCErrorType.NOT_FOUND));
+
+ this.watchers_.splice(index, 1);
+ return Promise.resolve(createNFCError(null));
+ }
+
+ cancelAllWatches() {
+ if (this.watchers_.length === 0)
+ return Promise.resolve(createNFCError(nfc.NFCErrorType.NOT_FOUND));
+
+ this.watchers_.splice(0, this.watchers_.length);
+ return Promise.resolve(createNFCError(null));
+ }
+
// Mock utility functions
bindToPipe(pipe) {
assert_equals(this.router_, null);
@@ -236,6 +358,10 @@ function nfc_mocks(mojo) {
return this.push_options_;
}
+ watchOptions() {
+ return this.watchers_[this.watchers_.length - 1].options;
kenneth.christiansen 2016/04/25 08:31:46 what if there are no watchers?
shalamov 2016/04/25 11:31:03 Done.
+ }
+
setPendingPushCompleted(result) {
this.push_completed_ = result;
}
@@ -243,18 +369,27 @@ function nfc_mocks(mojo) {
reset() {
this.hw_status_ = NFCHWStatus.ENABLED;
this.push_completed_ = true;
+ this.watch_id_ = 0;
+ this.watchers_ = new Array;
this.cancelPendingPushOperation();
- if (this.push_timeout_id_)
- window.clearTimeout(this.push_timeout_id_);
}
cancelPendingPushOperation() {
+ if (this.push_timeout_id_)
+ window.clearTimeout(this.push_timeout_id_);
if (this.pending_promise_func_)
this.pending_promise_func_(createNFCError(nfc.NFCErrorType.OPERATION_CANCELLED));
this.pushed_message_ = null;
this.push_options_ = null;
this.pending_promise_func_ = null;
}
+
+ triggerWatchCallback(id, message) {
+ assert_true(this.client_ !== null);
+ if (this.watchers_.length > 0)
kenneth.christiansen 2016/04/25 08:31:47 this definitely needs {}
shalamov 2016/04/25 11:31:03 Done.
+ this.client_.onWatch(id,
+ toMojoNFCMessage(message));
+ }
}
let mockNFC = new MockNFC;
@@ -268,6 +403,8 @@ function nfc_mocks(mojo) {
mockNFC: mockNFC,
assertNFCMessagesEqual: assertNFCMessagesEqual,
assertNFCPushOptionsEqual: assertNFCPushOptionsEqual,
+ assertWebNFCMessagesEqual: assertWebNFCMessagesEqual,
+ assertNFCWatchOptionsEqual: assertNFCWatchOptionsEqual,
});
});
}
« no previous file with comments | « no previous file | third_party/WebKit/LayoutTests/nfc/watch.html » ('j') | third_party/WebKit/LayoutTests/nfc/watch.html » ('J')

Powered by Google App Engine
This is Rietveld 408576698