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 @@
#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_utils.h"
#include "third_party/blink/renderer/platform/weborigin/kurl.h"
namespace blink {
......@@ -45,6 +46,18 @@ bool NFCReader::HasPendingActivity() const {
void NFCReader::start() {
if (!CheckSecurity())
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);
}
......
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 @@
"use strict";
promise_test(async t => {
const reader = new NFCReader({url: "www.a.com"});
function waitSyntaxErrorPromise(t, reader) {
const readerWatcher = new EventWatcher(t, reader, ["reading", "error"]);
reader.start();
const event = await readerWatcher.wait_for("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();
return promise;
}
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.');
promise_test(async t => {
promise_test(t => {
const reader = new NFCReader({url: "invalid"});
const readerWatcher = new EventWatcher(t, reader, ["reading", "error"]);
reader.start();
const event = await readerWatcher.wait_for("error");
assert_equals(event.error.name, 'SyntaxError');
return waitSyntaxErrorPromise(t, reader);
}, '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 readerWatcher = new EventWatcher(t, reader, ["reading", "error"]);
reader.start();
const event = await readerWatcher.wait_for("error");
assert_equals(event.error.name, 'SyntaxError');
return waitSyntaxErrorPromise(t, reader);
}, 'Test that NFCReader.start fails if NFCReaderOptions.url has wrong protocol.');
</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