This repository was archived by the owner on Mar 19, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathserviceworker_cookieStore_subscriptions_basic.js
62 lines (50 loc) · 2.1 KB
/
serviceworker_cookieStore_subscriptions_basic.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
self.GLOBAL = {
isWindow: function() { return false; },
isWorker: function() { return true; },
};
importScripts("/resources/testharness.js");
self.addEventListener('install', (event) => {
event.waitUntil((async () => {
try {
await cookieStore.subscribeToChanges([
{ name: 'cookie-name', matchType: 'equals',
url: '/cookie-store/scope/path' }]);
// If the worker enters the "redundant" state, the UA may terminate it
// before all tests have been reported to the client. Stifle errors in
// order to avoid this and ensure all tests are consistently reported.
} catch (err) {}
})());
});
// 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 cookieStore.getChangeSubscriptions();
assert_equals(subscriptions.length, 1);
assert_equals(subscriptions[0].name, 'cookie-name');
assert_equals('equals', subscriptions[0].matchType);
}, 'getChangeSubscriptions returns a subscription passed to subscribeToChanges');
promise_test(async testCase => {
await kServiceWorkerActivatedPromise;
const cookie_change_received_promise = new Promise((resolve) => {
self.addEventListener('cookiechange', (event) => {
resolve(event);
});
});
await cookieStore.set('cookie-name', 'cookie-value');
testCase.add_cleanup(async () => {
await cookieStore.delete('cookie-name');
});
const event = await cookie_change_received_promise;
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);
}, 'cookiechange dispatched with cookie change that matches subscription ' +
'to event handler registered with addEventListener');
done();