Commit 88232485 authored by Staphany Park's avatar Staphany Park Committed by Commit Bot

Cookie Store: Refactor tests to also work in window context.

This refactor condenses service worker test files by converting the
service worker scripts into .any.js WPT files that test both window and
worker contexts.

Change-Id: Iee64f5cc68b939ab02b06910444b8ebffdf359a4
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1851232
Commit-Queue: Victor Costan <pwnall@chromium.org>
Reviewed-by: default avatarVictor Costan <pwnall@chromium.org>
Cr-Commit-Position: refs/heads/master@{#720381}
parent 341ff804
......@@ -70,5 +70,5 @@ crbug.com/896068 [ Linux ] webaudio/AudioBuffer/huge-buffer.html [ Crash ]
crbug.com/896068 [ Linux ] webaudio/dom-exceptions.html [ Pass Crash ]
# Sheriff 2019-07-31
crbug.com/989365 [ Linux ] external/wpt/cookie-store/serviceworker_cookieStore_subscriptions_eventhandler_attribute.tentative.https.html [ Pass Timeout ]
crbug.com/989365 [ Linux ] external/wpt/cookie-store/serviceworker_cookiechange_eventhandler_single_subscription.tentative.https.any.serviceworker.html [ Pass Timeout ]
......@@ -5639,7 +5639,7 @@ crbug.com/987138 [ Linux Win ] virtual/audio-service/media/controls/doubletap-to
# Sheriff 2019-07-26
crbug.com/835943 [ Debug ] http/tests/appcache/non-html.xhtml [ Crash Pass ]
crbug.com/874866 [ Linux Debug ] media/controls/doubletap-to-jump-backwards.html [ Failure ]
crbug.com/988246 external/wpt/cookie-store/serviceworker_cookieStore_subscriptions_mismatch.tentative.https.html [ Skip ]
crbug.com/988246 external/wpt/cookie-store/serviceworker_cookiechange_eventhandler_mismatched_subscription.tentative.https.any.serviceworker.html [ Skip ]
crbug.com/988252 [ Linux Debug ] virtual/threaded/transitions/composited-with-hit-testing.html [ Pass Failure ]
crbug.com/959129 virtual/threaded/http/tests/devtools/tracing/timeline-script-parse.js [ Pass Failure ]
crbug.com/959129 http/tests/devtools/tracing/timeline-script-parse.js [ Pass Failure ]
......@@ -5673,7 +5673,7 @@ crbug.com/979422 [ Mac ] fast/borders/border-radius-mask-canvas.html [ Failure ]
crbug.com/979422 [ Mac ] fast/borders/border-radius-mask-canvas-with-mask.html [ Failure ]
crbug.com/979422 [ Mac ] fast/borders/border-radius-mask-canvas-border.html [ Failure ]
crbug.com/989365 [ Win7 ] http/tests/security/img-crossorigin-redirect-credentials.https.html [ Pass Timeout ]
crbug.com/989014 [ Linux Debug ] external/wpt/cookie-store/serviceworker_cookieStore_subscriptions_eventhandler_attribute.tentative.https.html [ Pass Timeout ]
crbug.com/989014 [ Linux Debug ] external/wpt/cookie-store/serviceworker_cookiechange_eventhandler_single_subscription.tentative.https.any.serviceworker.html [ Pass Timeout ]
crbug.com/989665 [ Win Linux ] virtual/omt-worker-fetch/external/wpt/resource-timing/resource_timing_buffer_full_eventually.html [ Pass Timeout ]
# Sheriff 2019-08-01
......
// META: title=Cookie Store API: ServiceWorker without cookie change subscriptions
// META: global=!default,serviceworker,window
// META: script=/service-workers/service-worker/resources/test-helpers.sub.js
'use strict';
promise_test(async testCase => {
if (self.GLOBAL.isWindow()) {
const registration = await service_worker_unregister_and_register(
testCase, 'resources/empty_sw.js', 'resources/does/not/exist');
testCase.add_cleanup(() => registration.unregister());
// Wait for this service worker to become active before snapshotting the
// subscription state, for consistency with other tests.
await wait_for_state(testCase, registration.installing, 'activated');
self.registration = registration;
} else {
// Wait for this service worker to become active before snapshotting the
// subscription state, for consistency with other tests.
await new Promise(resolve => {
self.addEventListener('activate', event => { resolve(); });
});
}
const subscriptions = await registration.cookies.getSubscriptions();
assert_equals(subscriptions.length, 0);
}, 'getSubscriptions returns an empty array when there are no subscriptions');
// META: title=Cookie Store API: ServiceWorker with multiple cookie change subscriptions
// META: global=!default,serviceworker,window
// META: script=/service-workers/service-worker/resources/test-helpers.sub.js
'use strict';
// sort() comparator that uses the < operator.
//
// This is intended to be used for sorting strings. Using < is preferred to
// localeCompare() because the latter has some implementation-dependent
// behavior.
function CompareStrings(a, b) {
return a < b ? -1 : (b < a ? 1 : 0);
}
promise_test(async testCase => {
let scope;
if (self.GLOBAL.isWindow()) {
scope = '/cookie-store/resources/does/not/exist';
const registration = await service_worker_unregister_and_register(
testCase, 'resources/empty_sw.js', scope);
testCase.add_cleanup(() => registration.unregister());
// Must wait for the service worker to enter the 'activated' state before
// subscribing to cookiechange events.
await wait_for_state(testCase, registration.installing, 'activated');
self.registration = registration;
} else {
scope = '/cookie-store/does/not/exist';
// Must wait for the service worker to enter the 'activated' state before
// subscribing to cookiechange events.
await new Promise(resolve => {
self.addEventListener('activate', event => { resolve(); });
});
}
{
const subscriptions = [
{ name: 'cookie-name1', matchType: 'equals', url: `${scope}/path1` },
];
await registration.cookies.subscribe(subscriptions);
testCase.add_cleanup(() => registration.cookies.unsubscribe(subscriptions));
}
{
const subscriptions = [
{ }, // Test the default values for subscription properties.
{ name: 'cookie-prefix', matchType: 'starts-with' },
];
await registration.cookies.subscribe(subscriptions);
testCase.add_cleanup(() => registration.cookies.unsubscribe(subscriptions));
}
const subscriptions = await registration.cookies.getSubscriptions();
assert_equals(subscriptions.length, 3);
subscriptions.sort((a, b) => CompareStrings(`${a.name}`, `${b.name}`));
assert_equals(subscriptions[0].name, 'cookie-name1');
assert_equals('equals', subscriptions[0].matchType);
assert_equals(subscriptions[1].name, 'cookie-prefix');
assert_equals('starts-with', subscriptions[1].matchType);
assert_false('name' in subscriptions[2]);
assert_equals('starts-with', subscriptions[2].matchType);
}, 'getSubscriptions returns a subscription passed to subscribe');
// META: title=Cookie Store API: ServiceWorker with one cookie change subscription
// META: global=!default,serviceworker,window
// META: script=/service-workers/service-worker/resources/test-helpers.sub.js
'use strict';
promise_test(async testCase => {
let scope;
if (self.GLOBAL.isWindow()) {
scope = '/cookie-store/resources/does/not/exist';
const registration = await service_worker_unregister_and_register(
testCase, 'resources/empty_sw.js', scope);
testCase.add_cleanup(() => registration.unregister());
// Must wait for the service worker to enter the 'activated' state before
// subscribing to cookiechange events.
await wait_for_state(testCase, registration.installing, 'activated');
self.registration = registration;
} else {
scope = '/cookie-store/does/not/exist';
// Must wait for the service worker to enter the 'activated' state before
// subscribing to cookiechange events.
await new Promise(resolve => {
self.addEventListener('activate', event => { resolve(); });
});
}
{
const subscriptions = [
{ name: 'cookie-name', matchType: 'equals', url: `${scope}/path` }
];
await registration.cookies.subscribe(subscriptions);
testCase.add_cleanup(() => registration.cookies.unsubscribe(subscriptions));
}
const subscriptions = await registration.cookies.getSubscriptions();
assert_equals(subscriptions.length, 1);
assert_equals(subscriptions[0].name, 'cookie-name');
assert_equals(subscriptions[0].matchType, 'equals');
assert_equals(subscriptions[0].url,
(new URL(`${scope}/path`, self.location.href)).href);
}, 'getSubscriptions returns a subscription passed to subscribe');
<!doctype html>
<meta charset="utf-8">
<title>Async Cookies: cookie change events in ServiceWorker</title>
<link rel="help" href="https://github.com/WICG/cookie-store">
<link rel="author" href="pwnall@chromium.org" title="Victor Costan">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script>
'use strict';
(async () => {
// Not using an explicit scope here in order for script URL to be in scope,
// to cover implicit subscription URL construction.
const registration = await navigator.serviceWorker.register(
'serviceworker_cookieStore_subscriptions.js');
add_completion_callback(() => {
registration.unregister();
});
fetch_tests_from_worker(registration.installing);
})();
</script>
<!doctype html>
<meta charset="utf-8">
<title>Async Cookies: cookie change events in ServiceWorker</title>
<link rel="help" href="https://github.com/WICG/cookie-store">
<link rel="author" href="pwnall@chromium.org" title="Victor Costan">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script>
'use strict';
(async () => {
const scope = 'scope';
const registration = await navigator.serviceWorker.register(
'serviceworker_cookieStore_subscriptions_basic.js', {scope});
add_completion_callback(() => {
registration.unregister();
});
fetch_tests_from_worker(registration.installing);
})();
</script>
self.GLOBAL = {
isWindow: function() { return false; },
isWorker: function() { return true; },
};
importScripts("/resources/testharness.js");
// Resolves when the service worker receives the 'activate' event.
const kServiceWorkerActivatedPromise = new Promise(resolve => {
self.addEventListener('activate', event => { resolve(); });
});
promise_test(async testCase => {
await kServiceWorkerActivatedPromise;
const subscriptions = await registration.cookies.getSubscriptions();
assert_equals(subscriptions.length, 0);
}, 'getSubscriptions returns an empty array when there are no subscriptions');
done();
<!doctype html>
<meta charset="utf-8">
<title>Async Cookies: ServiceWorker without cookie change subscriptions</title>
<link rel="help" href="https://github.com/WICG/cookie-store">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script>
'use strict';
(async () => {
const scope = 'scope';
const registration = await navigator.serviceWorker.register(
'serviceworker_cookieStore_subscriptions_empty.js', {scope});
add_completion_callback(() => {
registration.unregister();
});
fetch_tests_from_worker(registration.installing);
})();
</script>
<!doctype html>
<meta charset="utf-8">
<title>Async Cookies: oncookiechange event handler attribute in ServiceWorker</title>
<link rel="help" href="https://github.com/WICG/cookie-store">
<link rel="author" href="pwnall@chromium.org" title="Victor Costan">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script>
'use strict';
(async () => {
const scope = 'scope';
const registration = await navigator.serviceWorker.register(
'serviceworker_cookieStore_subscriptions_eventhandler_attribute.js', {scope});
add_completion_callback(() => {
registration.unregister();
});
fetch_tests_from_worker(registration.installing);
})();
</script>
<!doctype html>
<meta charset="utf-8">
<title>Async Cookies: ServiceWorker cookiechange event filtering</title>
<link rel="help" href="https://github.com/WICG/cookie-store">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script>
'use strict';
(async () => {
const scope = 'scope';
const registration = await navigator.serviceWorker.register(
'serviceworker_cookieStore_subscriptions_mismatch.js', {scope});
add_completion_callback(() => {
registration.unregister();
});
fetch_tests_from_worker(registration.installing);
})();
</script>
<!doctype html>
<meta charset="utf-8">
<title>Async Cookies: reset cookie change subscription list</title>
<title>Cookie Store API: reset cookie change subscription list</title>
<link rel="help" href="https://github.com/WICG/cookie-store">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
......
self.GLOBAL = {
isWindow: function() { return false; },
isWorker: function() { return true; },
};
importScripts("/resources/testharness.js");
// META: title=Cookie Store API: cookiechange event in ServiceWorker with mismatched subscription
// META: global=!default,serviceworker
'use strict';
const kScope = '/cookie-store/does/not/exist';
// Resolves when the service worker receives the 'activate' event.
const kServiceWorkerActivatedPromise = new Promise((resolve) => {
self.addEventListener('activate', event => { resolve(); });
});
// Resolves when a cookiechange event is received.
const kCookieChangeReceivedPromise = new Promise((resolve) => {
self.addEventListener('cookiechange', (event) => {
resolve(event);
......@@ -19,8 +21,8 @@ promise_test(async testCase => {
await kServiceWorkerActivatedPromise;
const subscriptions = [
{ name: 'cookie-name', matchType: 'equals',
url: '/cookie-store/scope/path' }];
{ name: 'cookie-name', matchType: 'equals', url: `${kScope}/path` },
];
await registration.cookies.subscribe(subscriptions);
testCase.add_cleanup(() => registration.cookies.unsubscribe(subscriptions));
......@@ -38,6 +40,7 @@ promise_test(async testCase => {
assert_equals(event.changed.length, 1);
assert_equals(event.changed[0].name, 'cookie-name');
assert_equals(event.changed[0].value, 'cookie-value');
assert_equals(event.deleted.length, 0);
assert_true(event instanceof ExtendableCookieChangeEvent);
assert_true(event instanceof ExtendableEvent);
}, 'cookiechange not dispatched for change that does not match subscription');
done();
self.GLOBAL = {
isWindow: function() { return false; },
isWorker: function() { return true; },
};
importScripts("/resources/testharness.js");
// META: title=Cookie Store API: cookiechange event in ServiceWorker with multiple subscriptions
// META: global=!default,serviceworker
'use strict';
const kScope = '/cookie-store/does/not/exist';
// Resolves when the service worker receives the 'activate' event.
const kServiceWorkerActivatedPromise = new Promise(resolve => {
const kServiceWorkerActivatedPromise = new Promise((resolve) => {
self.addEventListener('activate', event => { resolve(); });
});
// sort() comparator that uses the < operator.
//
// This is intended to be used for sorting strings. Using < is preferred to
// localeCompare() because the latter has some implementation-dependent
// behavior.
function CompareStrings(a, b) {
return a < b ? -1 : (b < a ? 1 : 0);
}
promise_test(async testCase => {
await kServiceWorkerActivatedPromise;
{
const subscriptions = [
{ name: 'cookie-name1', matchType: 'equals', url: '/cookie-store/path1' },
];
await registration.cookies.subscribe(subscriptions);
testCase.add_cleanup(() => registration.cookies.unsubscribe(subscriptions));
}
{
const subscriptions = [
{ }, // Test the default values for subscription properties.
{ name: 'cookie-prefix', matchType: 'starts-with' },
];
await registration.cookies.subscribe(subscriptions);
testCase.add_cleanup(() => registration.cookies.unsubscribe(subscriptions));
}
const subscriptions = await registration.cookies.getSubscriptions();
assert_equals(subscriptions.length, 3);
subscriptions.sort((a, b) => CompareStrings(`${a.name}`, `${b.name}`));
assert_equals(subscriptions[0].name, 'cookie-name1');
assert_equals('equals', subscriptions[0].matchType);
assert_equals(subscriptions[1].name, 'cookie-prefix');
assert_equals('starts-with', subscriptions[1].matchType);
assert_false('name' in subscriptions[2]);
assert_equals('starts-with', subscriptions[2].matchType);
}, 'getSubscriptions returns subscriptions passed to subscribe');
// Accumulates cookiechange events dispatched to the service worker.
let g_cookie_changes = [];
......@@ -61,8 +19,9 @@ let g_cookie_change_received_promise = null;
let g_cookie_change_received_promise_resolver = null;
self.addEventListener('cookiechange', (event) => {
g_cookie_changes.push(event);
if (g_cookie_change_received_promise_resolver)
if (g_cookie_change_received_promise_resolver) {
g_cookie_change_received_promise_resolver();
}
});
function RearmCookieChangeReceivedPromise() {
g_cookie_change_received_promise = new Promise((resolve) => {
......@@ -76,7 +35,7 @@ promise_test(async testCase => {
{
const subscriptions = [
{ name: 'cookie-name1', matchType: 'equals', url: '/cookie-store/path1' },
{ name: 'cookie-name1', matchType: 'equals', url: `${kScope}/path1` },
];
await registration.cookies.subscribe(subscriptions);
testCase.add_cleanup(() => registration.cookies.unsubscribe(subscriptions));
......@@ -100,7 +59,7 @@ promise_test(async testCase => {
testCase.add_cleanup(() => RearmCookieChangeReceivedPromise());
assert_equals(g_cookie_changes.length, 1);
const event = g_cookie_changes[0]
const event = g_cookie_changes[0];
assert_equals(event.type, 'cookiechange');
assert_equals(event.changed.length, 1);
assert_equals(event.changed[0].name, 'cookie-name');
......@@ -109,5 +68,3 @@ promise_test(async testCase => {
assert_true(event instanceof ExtendableCookieChangeEvent);
assert_true(event instanceof ExtendableEvent);
}, 'cookiechange dispatched with cookie change that matches subscription');
done();
// META: title=Cookie Store API: cookiechange event in ServiceWorker with overlapping subscriptions
// META: global=!default,serviceworker
'use strict';
const kScope = '/cookie-store/does/not/exist';
// Resolves when the service worker receives the 'activate' event.
const kServiceWorkerActivatedPromise = new Promise((resolve) => {
self.addEventListener('activate', event => { resolve(); });
});
// Accumulates cookiechange events dispatched to the service worker.
let g_cookie_changes = [];
// Resolved when a cookiechange event is received. Rearmed by
// RearmCookieChangeReceivedPromise().
let g_cookie_change_received_promise = null;
let g_cookie_change_received_promise_resolver = null;
self.addEventListener('cookiechange', (event) => {
g_cookie_changes.push(event);
if (g_cookie_change_received_promise_resolver) {
g_cookie_change_received_promise_resolver();
RearmCookieChangeReceivedPromise();
}
});
function RearmCookieChangeReceivedPromise() {
g_cookie_change_received_promise = new Promise((resolve) => {
g_cookie_change_received_promise_resolver = resolve;
});
}
RearmCookieChangeReceivedPromise();
promise_test(async testCase => {
await kServiceWorkerActivatedPromise;
const subscriptions = [
{ name: 'coo', matchType: 'starts-with' },
{ name: 'cookie', matchType: 'starts-with' },
];
await registration.cookies.subscribe(subscriptions);
testCase.add_cleanup(() => registration.cookies.unsubscribe(subscriptions));
await cookieStore.set('cookie-name', 'cookie-value');
testCase.add_cleanup(async () => {
await cookieStore.delete('cookie-name');
});
testCase.add_cleanup(() => { g_cookie_changes = []; });
await g_cookie_change_received_promise;
testCase.add_cleanup(() => RearmCookieChangeReceivedPromise());
// To ensure that we are accounting for all events dispatched by the first
// cookie change, we initiate and listen for a final cookie change that we
// know will dispatch a single event.
await cookieStore.set('coo', 'coo-value');
testCase.add_cleanup(async () => {
await cookieStore.delete('coo');
});
testCase.add_cleanup(() => { g_cookie_changes = []; });
await g_cookie_change_received_promise;
testCase.add_cleanup(() => RearmCookieChangeReceivedPromise());
assert_equals(g_cookie_changes.length, 2);
{
const event = g_cookie_changes[0];
assert_equals(event.type, 'cookiechange');
assert_equals(event.changed.length, 1);
assert_equals(event.changed[0].name, 'cookie-name');
assert_equals(event.changed[0].value, 'cookie-value');
assert_equals(event.deleted.length, 0);
assert_true(event instanceof ExtendableCookieChangeEvent);
assert_true(event instanceof ExtendableEvent);
}
{
const event = g_cookie_changes[1];
assert_equals(event.type, 'cookiechange');
assert_equals(event.changed.length, 1);
assert_equals(event.changed[0].name, 'coo');
assert_equals(event.changed[0].value, 'coo-value');
assert_equals(event.deleted.length, 0);
assert_true(event instanceof ExtendableCookieChangeEvent);
assert_true(event instanceof ExtendableEvent);
}
}, '1 cookiechange event dispatched with cookie change that matches multiple ' +
'subscriptions');
self.GLOBAL = {
isWindow: function() { return false; },
isWorker: function() { return true; },
};
importScripts("/resources/testharness.js");
// META: title=Cookie Store API: cookiechange event in ServiceWorker with single subscription
// META: global=!default,serviceworker
'use strict';
const kScope = '/cookie-store/does/not/exist';
// Resolves when the service worker receives the 'activate' event.
const kServiceWorkerActivatedPromise = new Promise(resolve => {
const kServiceWorkerActivatedPromise = new Promise((resolve) => {
self.addEventListener('activate', event => { resolve(); });
});
promise_test(async testCase => {
await kServiceWorkerActivatedPromise;
{
const subscriptions = [
{ name: 'cookie-name', matchType: 'equals',
url: '/cookie-store/scope/path' }];
await registration.cookies.subscribe(subscriptions);
testCase.add_cleanup(() => registration.cookies.unsubscribe(subscriptions));
}
const subscriptions = await registration.cookies.getSubscriptions();
assert_equals(subscriptions.length, 1);
assert_equals(subscriptions[0].name, 'cookie-name');
assert_equals(subscriptions[0].matchType, 'equals');
assert_equals(subscriptions[0].url,
(new URL("/cookie-store/scope/path", self.location.href)).href);
}, 'getSubscriptions returns a subscription passed to subscribe');
const kCookieChangeReceivedPromise = new Promise((resolve) => {
// Resolves when a cookiechange event is received.
const kCookieChangeReceivedPromise = new Promise(resolve => {
self.addEventListener('cookiechange', event => { resolve(event); });
});
......@@ -37,8 +19,8 @@ promise_test(async testCase => {
await kServiceWorkerActivatedPromise;
const subscriptions = [
{ name: 'cookie-name', matchType: 'equals',
url: '/cookie-store/scope/path' }];
{ name: 'cookie-name', matchType: 'equals', url: `${kScope}/path` },
];
await registration.cookies.subscribe(subscriptions);
testCase.add_cleanup(() => registration.cookies.unsubscribe(subscriptions));
......@@ -56,6 +38,4 @@ promise_test(async testCase => {
assert_true(event instanceof ExtendableCookieChangeEvent);
assert_true(event instanceof ExtendableEvent);
}, 'cookiechange dispatched with cookie change that matches subscription ' +
'to event handler registered with addEventListener');
done();
'to cookiechange event handler registered with addEventListener');
self.GLOBAL = {
isWindow: function() { return false; },
isWorker: function() { return true; },
};
importScripts("/resources/testharness.js");
// META: title=Cookie Store API: oncookiechange event in ServiceWorker with single subscription
// META: global=!default,serviceworker
'use strict';
const kScope = '/cookie-store/does/not/exist';
// Resolves when the service worker receives the 'activate' event.
const kServiceWorkerActivatedPromise = new Promise((resolve) => {
self.addEventListener('activate', event => { resolve(); });
});
const kCookieChangeReceivedPromise = new Promise((resolve) => {
self.addEventListener('cookiechange', (event) => {
resolve(event);
});
// Resolves when a cookiechange event is received.
const kCookieChangeReceivedPromise = new Promise(resolve => {
self.oncookiechange = event => { resolve(event); };
});
promise_test(async testCase => {
await kServiceWorkerActivatedPromise;
const subscriptions = [
{ name: 'cookie-name', matchType: 'equals',
url: '/cookie-store/scope/path' }];
{ name: 'cookie-name', matchType: 'equals', url: `${kScope}/path` }
];
await registration.cookies.subscribe(subscriptions);
testCase.add_cleanup(() => registration.cookies.unsubscribe(subscriptions));
......@@ -38,6 +38,4 @@ promise_test(async testCase => {
assert_true(event instanceof ExtendableCookieChangeEvent);
assert_true(event instanceof ExtendableEvent);
}, 'cookiechange dispatched with cookie change that matches subscription ' +
'to event handler registered with oncookiechange');
done();
'to cookiechange event handler registered with addEventListener');
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