Commit 55ac9203 authored by Leon Han's avatar Leon Han Committed by Commit Bot

[webnfc] NFCReader.html -> NFCReader.https.html and make it pass

This CL renames the wpt test NFCReader.html to NFCReader.https.html
because WebNFC API is available only in secure contexts, then fixes the
code to make it pass.

BUG=520391

Change-Id: Id8f584c7ced901f15b087aec5f96fd83ba8f6d68
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1720154Reviewed-by: default avatarReilly Grant <reillyg@chromium.org>
Reviewed-by: default avatarRijubrata Bhaumik <rijubrata.bhaumik@intel.com>
Commit-Queue: Leon Han <leon.han@intel.com>
Cr-Commit-Position: refs/heads/master@{#681752}
parent ee1975ac
...@@ -15,6 +15,7 @@ ...@@ -15,6 +15,7 @@
#include "third_party/blink/renderer/modules/nfc/nfc_reader_options.h" #include "third_party/blink/renderer/modules/nfc/nfc_reader_options.h"
#include "third_party/blink/renderer/modules/nfc/nfc_reading_event.h" #include "third_party/blink/renderer/modules/nfc/nfc_reading_event.h"
#include "third_party/blink/renderer/modules/nfc/nfc_utils.h" #include "third_party/blink/renderer/modules/nfc/nfc_utils.h"
#include "third_party/blink/renderer/platform/weborigin/kurl.h"
namespace blink { namespace blink {
...@@ -45,6 +46,18 @@ bool NFCReader::HasPendingActivity() const { ...@@ -45,6 +46,18 @@ bool NFCReader::HasPendingActivity() const {
void NFCReader::start() { void NFCReader::start() {
if (!CheckSecurity()) if (!CheckSecurity())
return; return;
// https://w3c.github.io/web-nfc/#dom-nfcreader-start (Step 3.4)
if (options_->hasURL() && !options_->url().IsEmpty()) {
KURL pattern_url(options_->url());
if (!pattern_url.IsValid() || pattern_url.Protocol() != kNfcProtocolHttps) {
DispatchEvent(*MakeGarbageCollected<NFCErrorEvent>(
event_type_names::kError,
MakeGarbageCollected<DOMException>(DOMExceptionCode::kSyntaxError,
kNfcUrlPatternError)));
}
}
GetNfcProxy()->StartReading(this); GetNfcProxy()->StartReading(this);
} }
......
This is a testharness.js-based test.
FAIL Test that NFCReader.start fails if NFCReaderOptions.url is missing components. promise_test: Unhandled rejection with value: object "ReferenceError: NFCReader is not defined"
FAIL Test that NFCReader.start fails if NFCReaderOptions.url is invalid. promise_test: Unhandled rejection with value: object "ReferenceError: NFCReader is not defined"
FAIL Test that NFCReader.start fails if NFCReaderOptions.url has wrong protocol. promise_test: Unhandled rejection with value: object "ReferenceError: NFCReader is not defined"
Harness: the test ran to completion.
...@@ -12,28 +12,29 @@ ...@@ -12,28 +12,29 @@
"use strict"; "use strict";
promise_test(async t => { function waitSyntaxErrorPromise(t, reader) {
const reader = new NFCReader({url: "www.a.com"});
const readerWatcher = new EventWatcher(t, reader, ["reading", "error"]); const readerWatcher = new EventWatcher(t, reader, ["reading", "error"]);
const promise = readerWatcher.wait_for("error").then(event => {
assert_equals(event.error.name, 'SyntaxError');
});
// NFCReader#start() synchronously dispatches the syntax error event.
reader.start(); reader.start();
const event = await readerWatcher.wait_for("error"); return promise;
assert_equals(event.error.name, 'SyntaxError'); }
promise_test(t => {
const reader = new NFCReader({url: "www.a.com"});
return waitSyntaxErrorPromise(t, reader);
}, 'Test that NFCReader.start fails if NFCReaderOptions.url is missing components.'); }, 'Test that NFCReader.start fails if NFCReaderOptions.url is missing components.');
promise_test(async t => { promise_test(t => {
const reader = new NFCReader({url: "invalid"}); const reader = new NFCReader({url: "invalid"});
const readerWatcher = new EventWatcher(t, reader, ["reading", "error"]); return waitSyntaxErrorPromise(t, reader);
reader.start();
const event = await readerWatcher.wait_for("error");
assert_equals(event.error.name, 'SyntaxError');
}, 'Test that NFCReader.start fails if NFCReaderOptions.url is invalid.'); }, 'Test that NFCReader.start fails if NFCReaderOptions.url is invalid.');
promise_test(async t => { promise_test(t => {
const reader = new NFCReader({url: "http://a.com"}); const reader = new NFCReader({url: "http://a.com"});
const readerWatcher = new EventWatcher(t, reader, ["reading", "error"]); return waitSyntaxErrorPromise(t, reader);
reader.start();
const event = await readerWatcher.wait_for("error");
assert_equals(event.error.name, 'SyntaxError');
}, 'Test that NFCReader.start fails if NFCReaderOptions.url has wrong protocol.'); }, 'Test that NFCReader.start fails if NFCReaderOptions.url has wrong protocol.');
</script> </script>
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