Source code
Revision control
Copy as Markdown
Other Tools
Test Info:
/* Any copyright is dedicated to the Public Domain.
"use strict";
const { SiteDataTestUtils } = ChromeUtils.importESModule(
);
async function addServiceWorker(
origin,
topLevelOrigin = null,
userContextId = null
) {
let swURL =
"worker.js";
let registered = SiteDataTestUtils.promiseServiceWorkerRegistered(swURL);
await SiteDataTestUtils.addServiceWorker({
win: window,
path: swURL,
topLevelPath: topLevelOrigin,
userContextId,
});
await registered;
ok(true, `${origin} has a service worker`);
}
add_setup(async function () {
await SpecialPowers.pushPrefEnv({
set: [
["dom.serviceWorkers.enabled", true],
["dom.serviceWorkers.exemptFromPerDomainMax", true],
["dom.serviceWorkers.testing.enabled", true],
],
});
});
add_task(async function test_deleteFromHost() {
let unregistered = SiteDataTestUtils.promiseServiceWorkerUnregistered(
);
await new Promise(aResolve => {
Services.clearData.deleteDataFromHost(
"example.com",
true,
Ci.nsIClearDataService.CLEAR_DOM_QUOTA,
value => {
Assert.equal(value, 0);
aResolve();
}
);
});
await unregistered;
ok(
"example.com has no service worker"
);
ok(
"example.org has a service worker"
);
ok(
"test1.example.org has a service worker"
);
// Clearing the subdomain should not clear the entire site.
unregistered = SiteDataTestUtils.promiseServiceWorkerUnregistered(
);
await new Promise(aResolve => {
Services.clearData.deleteDataFromHost(
"test1.example.org",
true,
Ci.nsIClearDataService.CLEAR_DOM_QUOTA,
value => {
Assert.equal(value, 0);
aResolve();
}
);
});
await unregistered;
ok(
"test1.example.org has no service worker"
);
ok(
"example.org has a service worker"
);
unregistered = SiteDataTestUtils.promiseServiceWorkerUnregistered(
);
await new Promise(aResolve => {
Services.clearData.deleteDataFromHost(
"example.org",
true,
Ci.nsIClearDataService.CLEAR_DOM_QUOTA,
value => {
Assert.equal(value, 0);
aResolve();
}
);
});
await unregistered;
ok(
"example.org has no service worker"
);
ok(
"example.com has no service worker"
);
});
add_task(async function test_deleteFromSite() {
// Both of these partitioned service workers should be removed by
// deleteDataFromSite targeting 'example.com'. example.org sw partitioned
// under example.com
await addServiceWorker(
);
// example.com sw partitioned under example.org
let unregistered = SiteDataTestUtils.promiseServiceWorkerUnregistered(
);
let unregisteredSub1 = SiteDataTestUtils.promiseServiceWorkerUnregistered(
);
let unregisteredSub2 = SiteDataTestUtils.promiseServiceWorkerUnregistered(
);
let unregisteredPartitioned1 =
SiteDataTestUtils.promiseServiceWorkerUnregistered(
);
let unregisteredPartitioned2 =
SiteDataTestUtils.promiseServiceWorkerUnregistered(
);
await new Promise(aResolve => {
Services.clearData.deleteDataFromSite(
"example.com",
{}, // All OA
true,
Ci.nsIClearDataService.CLEAR_DOM_QUOTA,
value => {
Assert.equal(
value,
0,
"Call to nsIClearDataService should not return any errors."
);
aResolve();
}
);
});
await Promise.all([
unregistered,
unregisteredSub1,
unregisteredSub2,
unregisteredPartitioned1,
unregisteredPartitioned2,
]);
ok(
"example.com has no service worker"
);
ok(
"test1.example.com has no service worker"
);
ok(
"test2.example.com has no service worker"
);
ok(
"sub1.test1.example.org has no service worker"
);
ok(
"www.example.com has no service worker"
);
ok(
"example.org has a service worker"
);
unregistered = SiteDataTestUtils.promiseServiceWorkerUnregistered(
);
await new Promise(aResolve => {
Services.clearData.deleteDataFromSite(
"example.org",
{},
true,
Ci.nsIClearDataService.CLEAR_DOM_QUOTA,
value => {
Assert.equal(value, 0);
aResolve();
}
);
});
await unregistered;
ok(
"example.org has no service worker"
);
ok(
"example.com has no service worker"
);
ok(
"test1.example.com has no service worker"
);
ok(
"test2.example.com has no service worker"
);
ok(
"sub1.test1.example.org has no service worker"
);
ok(
"www.example.com has no service worker"
);
await SiteDataTestUtils.clear();
});
add_task(async function test_deleteFromSiteWithPattern() {
// Both of these partitioned service workers should be removed by
// deleteDataFromSite targeting 'example.com'. example.org sw partitioned
// under example.com
await addServiceWorker(
);
ok(
"sub1.test1.example.org initially has a service worker"
);
// example.com sw partitioned under example.org
let unregisteredSub2 = SiteDataTestUtils.promiseServiceWorkerUnregistered(
);
await new Promise(aResolve => {
Services.clearData.deleteDataFromSite(
"example.com",
{ userContextId: 3 }, // Only OA with userContextId == 3
true,
Ci.nsIClearDataService.CLEAR_DOM_QUOTA,
value => {
Assert.equal(value, 0);
aResolve();
}
);
});
await Promise.all([unregisteredSub2]);
ok(
"example.com has a service worker"
);
ok(
"test1.example.com has a service worker"
);
ok(
"test2.example.com has a service worker"
);
ok(
"sub1.test1.example.org has a service worker"
);
ok(
"www.example.com has no service worker"
);
ok(
"example.org has a service worker"
);
await SiteDataTestUtils.clear();
});
add_task(async function test_deleteFromPrincipal() {
let unregistered = SiteDataTestUtils.promiseServiceWorkerUnregistered(
);
let principal =
Services.scriptSecurityManager.createContentPrincipalFromOrigin(
);
await new Promise(aResolve => {
Services.clearData.deleteDataFromPrincipal(
principal,
true,
Ci.nsIClearDataService.CLEAR_DOM_QUOTA,
value => {
Assert.equal(value, 0);
aResolve();
}
);
});
await unregistered;
ok(
"test1.example.com has no service worker"
);
ok(
"test1.example.org has a service worker"
);
unregistered = SiteDataTestUtils.promiseServiceWorkerUnregistered(
);
principal = Services.scriptSecurityManager.createContentPrincipalFromOrigin(
);
await new Promise(aResolve => {
Services.clearData.deleteDataFromPrincipal(
principal,
true,
Ci.nsIClearDataService.CLEAR_DOM_QUOTA,
value => {
Assert.equal(value, 0);
aResolve();
}
);
});
await unregistered;
ok(
"test1.example.org has no service worker"
);
ok(
"test1.example.com has no service worker"
);
});
add_task(async function test_deleteAll() {
let unregistered = SiteDataTestUtils.promiseServiceWorkerUnregistered(
);
await new Promise(aResolve => {
Services.clearData.deleteData(
Ci.nsIClearDataService.CLEAR_DOM_QUOTA,
value => {
Assert.equal(value, 0);
aResolve();
}
);
});
await unregistered;
ok(
"test2.example.com has no service worker"
);
ok(
"test2.example.org has no service worker"
);
await SiteDataTestUtils.clear();
});