Commit 97a49fd0 authored by Wanming Lin's avatar Wanming Lin Committed by Commit Bot

[WebNFC] Enables SuspendNFCOperations/ResumeNFCOperations in mock NFC backend

This CL enables SuspendNFCOperations/ResumeNFCOperations in mock NFC backend
and handles NFC push operations and reading operations in suspended state.



Bug: 996250, 520391
Change-Id: Id32f8b720cab8a2e80f20772a9c7d02578671206
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1864391
Commit-Queue: Wanming Lin <wanming.lin@intel.com>
Reviewed-by: default avatarRijubrata Bhaumik <rijubrata.bhaumik@intel.com>
Cr-Commit-Position: refs/heads/master@{#707324}
parent c324a854
......@@ -5767,3 +5767,6 @@ crbug.com/1014812 virtual/threaded/external/wpt/animation-worklet/playback-rate.
crbug.com/1014950 [ Mac ] http/tests/devtools/tracing/timeline-js/timeline-js-line-level-profile-no-url-end-to-end.js [ Pass Timeout ]
crbug.com/1014950 [ Mac ] virtual/threaded/http/tests/devtools/tracing/timeline-js/timeline-js-line-level-profile-no-url-end-to-end.js [ Pass Timeout ]
crbug.com/1015187 [ Linux Mac ] virtual/cross-origin-embedder-policy/external/wpt/html/cross-origin-embedder-policy/none.https.html [ Pass Failure ]
# Sheriff 2019-10-18
crbug.com/996250 external/wpt/web-nfc/NFCReader_scan_iframe.https.html [ Timeout ]
......@@ -32,7 +32,7 @@ function toMojoNDEFRecord(record) {
}
function toByteArray(data) {
// Convert JS objects to byte array
// Convert JS objects to byte array.
let byteArray = new Uint8Array(0);
let tmpData = data;
......@@ -49,7 +49,7 @@ function toByteArray(data) {
// Compares NDEFRecords that were provided / received by the mock service.
// TODO: Use different getters to get received record data,
// see spec changes at https://github.com/w3c/web-nfc/pull/243
// see spec changes at https://github.com/w3c/web-nfc/pull/243.
function compareNDEFRecords(providedRecord, receivedRecord) {
assert_equals(providedRecord.recordType, receivedRecord.recordType);
......@@ -105,7 +105,7 @@ function assertNFCReaderOptionsEqual(provided, received) {
// Checks whether NFCReaderOptions are matched with given message.
function matchesWatchOptions(message, options) {
// Filter by Web NFC id
// Filter by Web NFC id.
if (!matchesWebNfcId(message.url, options.url)) return false;
// Matches any record / media type.
......@@ -114,7 +114,7 @@ function matchesWatchOptions(message, options) {
return true;
}
// Filter by mediaType and recordType
// Filter by mediaType and recordType.
for (let record of message.records) {
if (options.mediaType != null && options.mediaType !== ""
&& options.mediaType !== record.mediaType) {
......@@ -175,14 +175,15 @@ var WebNFCTest = (() => {
this.client_ = null;
this.watchers_ = [];
this.reading_messages_ = [];
this.operations_suspended_ = false;
}
// NFC delegate functions
// NFC delegate functions.
async push(message, options) {
let error = this.getHWError();
if (error)
return error;
// Cancel previous pending push operation
// Cancel previous pending push operation.
if (this.pending_promise_func_) {
this.cancelPendingPushOperation();
}
......@@ -192,9 +193,12 @@ var WebNFCTest = (() => {
return new Promise(resolve => {
this.pending_promise_func_ = resolve;
if (options.timeout && options.timeout !== Infinity &&
// Pend push operation if NFC operation is suspended.
if (this.operations_suspended_) {
// Do nothing, pends push operation.
} else if (options.timeout && options.timeout !== Infinity &&
!this.push_completed_) {
// Resolve with TimeoutError, else pending push operation.
// Resolve with TimeoutError, else pend push operation.
if (this.push_should_timeout_) {
resolve(
createNFCError(device.mojom.NFCErrorType.TIMER_EXPIRED));
......@@ -226,13 +230,16 @@ var WebNFCTest = (() => {
}
this.watchers_.push({id: id, options: options});
// Triggers onWatch if the new watcher matches existing messages
// Ignore reading if NFC operation is suspended.
if(!this.operations_suspended_) {
// Triggers onWatch if the new watcher matches existing messages.
for (let message of this.reading_messages_) {
if (matchesWatchOptions(message, options)) {
this.client_.onWatch(
[id], fake_tag_serial_number, toMojoNDEFMessage(message));
}
}
}
return createNFCError(null);
}
......@@ -290,6 +297,7 @@ var WebNFCTest = (() => {
this.push_completed_ = true;
this.watchers_ = [];
this.reading_messages_ = [];
this.operations_suspended_ = false;
this.cancelPendingPushOperation();
this.bindingSet_.closeAllBindings();
this.interceptor_.stop();
......@@ -311,10 +319,12 @@ var WebNFCTest = (() => {
// Sets message that is used to deliver NFC reading updates.
setReadingMessage(message) {
this.reading_messages_.push(message);
// Ignore reading if NFCPushOptions.ignoreRead is true
// Ignore reading if NFC operation is suspended.
if(this.operations_suspended_) return;
// Ignore reading if NFCPushOptions.ignoreRead is true.
if(this.push_options_ && this.push_options_.ignoreRead)
return;
// Triggers onWatch if the new message matches existing watchers
// Triggers onWatch if the new message matches existing watchers.
for (let watcher of this.watchers_) {
if (matchesWatchOptions(message, watcher.options)) {
this.client_.onWatch(
......@@ -327,6 +337,31 @@ var WebNFCTest = (() => {
setPushShouldTimeout(result) {
this.push_should_timeout_ = result;
}
// Suspends all pending NFC operations. Could be used when web page
// visibility is lost.
suspendNFCOperations() {
this.operations_suspended_ = true;
}
// Resumes all suspended NFC operations.
resumeNFCOperations() {
this.operations_suspended_ = false;
// Resumes pending NFC reading.
for (let watcher of this.watchers_) {
for (let message of this.reading_messages_) {
if (matchesWatchOptions(message, watcher.options)) {
this.client_.onWatch(
[watcher.id], fake_tag_serial_number,
toMojoNDEFMessage(message));
}
}
}
// Resumes pending push operation.
if (this.pending_promise_func_) {
this.pending_promise_func_(createNFCError(null));
}
}
}
let testInternal = {
......
......@@ -25,10 +25,13 @@ nfc_test(async (t, mockNFC) => {
const iframeLoadWatcher = new EventWatcher(t, iframe, 'load');
document.body.appendChild(iframe);
await iframeLoadWatcher.wait_for('load');
// Focus on iframe
// Focus on iframe.
iframe.contentWindow.document.getElementById('foo').focus();
assert_true(iframe.contentDocument.hasFocus(), 'iframe gains the focus');
// Suspend NFC operations is async in blink, use setTimeout as a workaround.
// TODO(wanming.lin@intel.com): find a good way to eliminate this race
// condition.
await new Promise(resolve => t.step_timeout(resolve, 0));
mockNFC.setReadingMessage(createMessage([createTextRecord(test_text_data)]));
await promise;
......
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