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

Unified Diff: third_party/WebKit/LayoutTests/imported/wpt/service-workers/service-worker/resources/test-helpers.sub.js

Issue 2415873002: Import w3c tests for the service workers (Closed)
Patch Set: Rebase Created 4 years, 2 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/imported/wpt/service-workers/service-worker/resources/test-helpers.sub.js
diff --git a/third_party/WebKit/LayoutTests/http/tests/serviceworker/resources/test-helpers.js b/third_party/WebKit/LayoutTests/imported/wpt/service-workers/service-worker/resources/test-helpers.sub.js
similarity index 71%
copy from third_party/WebKit/LayoutTests/http/tests/serviceworker/resources/test-helpers.js
copy to third_party/WebKit/LayoutTests/imported/wpt/service-workers/service-worker/resources/test-helpers.sub.js
index 0bcdcc3e348b37640b9f5dc12b62dff057348410..b0ffbd40625c93a882740771bb967149c13023e0 100644
--- a/third_party/WebKit/LayoutTests/http/tests/serviceworker/resources/test-helpers.js
+++ b/third_party/WebKit/LayoutTests/imported/wpt/service-workers/service-worker/resources/test-helpers.sub.js
@@ -13,10 +13,13 @@ function service_worker_unregister_and_register(test, url, scope) {
'unregister and register should not fail'));
}
-function service_worker_unregister(test, documentUrl) {
- return navigator.serviceWorker.getRegistration(documentUrl)
+// This unregisters the registration that precisely matches scope. Use this
+// when unregistering by scope. If no registration is found, it just resolves.
+function service_worker_unregister(test, scope) {
+ var absoluteScope = (new URL(scope, window.location).href);
+ return navigator.serviceWorker.getRegistration(scope)
.then(function(registration) {
- if (registration)
+ if (registration && registration.scope === absoluteScope)
return registration.unregister();
})
.catch(unreached_rejection(test, 'unregister should not fail'));
@@ -44,31 +47,14 @@ function unreached_rejection(test, prefix) {
}
// Adds an iframe to the document and returns a promise that resolves to the
-// iframe when it finishes loading. When |options.auto_remove| is set to
-// |false|, the caller is responsible for removing the iframe
-// later. Otherwise, the frame will be removed after all tests are finished.
-function with_iframe(url, options) {
+// iframe when it finishes loading. The caller is responsible for removing the
+// iframe later if needed.
+function with_iframe(url) {
return new Promise(function(resolve) {
var frame = document.createElement('iframe');
frame.src = url;
frame.onload = function() { resolve(frame); };
document.body.appendChild(frame);
- if (typeof options === 'undefined')
- options = {};
- if (typeof options.auto_remove === 'undefined')
- options.auto_remove = true;
- if (options.auto_remove)
- add_completion_callback(function() { frame.remove(); });
- });
-}
-
-function with_sandboxed_iframe(url, sandbox) {
- return new Promise(function(resolve) {
- var frame = document.createElement('iframe');
- frame.sandbox = sandbox;
- frame.src = url;
- frame.onload = function() { resolve(frame); };
- document.body.appendChild(frame);
});
}
@@ -149,17 +135,15 @@ function wait_for_state(test, worker, state) {
// The test will succeed if the specified service worker can be successfully
// registered and installed.
// - Creates a new ServiceWorker registration with a scope unique to the current
-// document URL and the script URL. This allows more than one
+// document URL. Note that this doesn't allow more than one
// service_worker_test() to be run from the same document.
// - Waits for the new worker to begin installing.
// - Imports tests results from tests running inside the ServiceWorker.
function service_worker_test(url, description) {
// If the document URL is https://example.com/document and the script URL is
// https://example.com/script/worker.js, then the scope would be
- // https://example.com/script/scope/document/script/worker.js.
- var document_path = window.location.pathname;
- var script_path = new URL(url, window.location).pathname;
- var scope = new URL('scope' + document_path + script_path,
+ // https://example.com/script/scope/document.
+ var scope = new URL('scope' + window.location.pathname,
new URL(url, window.location)).toString();
promise_test(function(test) {
return service_worker_unregister_and_register(test, url, scope)
@@ -182,8 +166,8 @@ function base_path() {
function test_login(test, origin, username, password, cookie) {
return new Promise(function(resolve, reject) {
with_iframe(
- origin +
- '/serviceworker/resources/fetch-access-control-login.html')
+ origin + base_path() +
+ 'resources/fetch-access-control-login.html')
.then(test.step_func(function(frame) {
var channel = new MessageChannel();
channel.port1.onmessage = test.step_func(function() {
@@ -197,12 +181,47 @@ function test_login(test, origin, username, password, cookie) {
});
}
-function login(test, local, remote) {
- var suffix = (local.indexOf("https") != -1) ? "s": "";
- return test_login(test, local, 'username1' + suffix, 'password1' + suffix,
- 'cookie1')
+function test_websocket(test, frame, url) {
+ return new Promise(function(resolve, reject) {
+ var ws = new frame.contentWindow.WebSocket(url, ['echo', 'chat']);
+ var openCalled = false;
+ ws.addEventListener('open', test.step_func(function(e) {
+ assert_equals(ws.readyState, 1, "The WebSocket should be open");
+ openCalled = true;
+ ws.close();
+ }), true);
+
+ ws.addEventListener('close', test.step_func(function(e) {
+ assert_true(openCalled, "The WebSocket should be closed after being opened");
+ resolve();
+ }), true);
+
+ ws.addEventListener('error', reject);
+ });
+}
+
+function login(test) {
+ return test_login(test, 'http://{{domains[www1]}}:{{ports[http][0]}}',
+ 'username1', 'password1', 'cookie1')
+ .then(function() {
+ return test_login(test, 'http://{{host}}:{{ports[http][0]}}',
+ 'username2', 'password2', 'cookie2');
+ });
+}
+
+function login_https(test) {
+ return test_login(test, 'https://{{domains[www1]}}:{{ports[https][0]}}',
+ 'username1s', 'password1s', 'cookie1')
.then(function() {
- return test_login(test, remote, 'username2' + suffix,
- 'password2' + suffix, 'cookie2');
+ return test_login(test, 'https://{{host}}:{{ports[https][0]}}',
+ 'username2s', 'password2s', 'cookie2');
});
}
+
+function websocket(test, frame) {
+ return test_websocket(test, frame, get_websocket_url());
+}
+
+function get_websocket_url() {
+ return 'wss://{{host}}:{{ports[wss][0]}}/echo';
+}

Powered by Google App Engine
This is Rietveld 408576698