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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
|
/* Any copyright is dedicated to the Public Domain.
* http://creativecommons.org/publicdomain/zero/1.0/ */
"use strict";
const TEST_QUOTA_USAGE_HOST = "example.com";
const TEST_QUOTA_USAGE_ORIGIN = "https://" + TEST_QUOTA_USAGE_HOST;
const TEST_QUOTA_USAGE_URL =
getRootDirectory(gTestPath).replace(
"chrome://mochitests/content",
TEST_QUOTA_USAGE_ORIGIN
) + "/site_data_test.html";
const TEST_OFFLINE_HOST = "example.org";
const TEST_OFFLINE_ORIGIN = "https://" + TEST_OFFLINE_HOST;
const TEST_OFFLINE_URL =
getRootDirectory(gTestPath).replace(
"chrome://mochitests/content",
TEST_OFFLINE_ORIGIN
) + "/offline/offline.html";
const TEST_SERVICE_WORKER_URL =
getRootDirectory(gTestPath).replace(
"chrome://mochitests/content",
TEST_OFFLINE_ORIGIN
) + "/service_worker_test.html";
const REMOVE_DIALOG_URL =
"chrome://browser/content/preferences/dialogs/siteDataRemoveSelected.xhtml";
ChromeUtils.defineESModuleGetters(this, {
SiteDataTestUtils: "resource://testing-common/SiteDataTestUtils.sys.mjs",
});
XPCOMUtils.defineLazyServiceGetter(
this,
"serviceWorkerManager",
"@mozilla.org/serviceworkers/manager;1",
"nsIServiceWorkerManager"
);
function promiseSiteDataManagerSitesUpdated() {
return TestUtils.topicObserved("sitedatamanager:sites-updated", () => true);
}
function is_element_visible(aElement, aMsg) {
isnot(aElement, null, "Element should not be null, when checking visibility");
ok(!BrowserTestUtils.isHidden(aElement), aMsg);
}
function is_element_hidden(aElement, aMsg) {
isnot(aElement, null, "Element should not be null, when checking visibility");
ok(BrowserTestUtils.isHidden(aElement), aMsg);
}
function promiseLoadSubDialog(aURL) {
return new Promise(resolve => {
content.gSubDialog._dialogStack.addEventListener(
"dialogopen",
function dialogopen(aEvent) {
if (
aEvent.detail.dialog._frame.contentWindow.location == "about:blank"
) {
return;
}
content.gSubDialog._dialogStack.removeEventListener(
"dialogopen",
dialogopen
);
is(
aEvent.detail.dialog._frame.contentWindow.location.toString(),
aURL,
"Check the proper URL is loaded"
);
// Check visibility
is_element_visible(aEvent.detail.dialog._overlay, "Overlay is visible");
// Check that stylesheets were injected
let expectedStyleSheetURLs =
aEvent.detail.dialog._injectedStyleSheets.slice(0);
for (let styleSheet of aEvent.detail.dialog._frame.contentDocument
.styleSheets) {
let i = expectedStyleSheetURLs.indexOf(styleSheet.href);
if (i >= 0) {
info("found " + styleSheet.href);
expectedStyleSheetURLs.splice(i, 1);
}
}
is(
expectedStyleSheetURLs.length,
0,
"All expectedStyleSheetURLs should have been found"
);
// Wait for the next event tick to make sure the remaining part of the
// testcase runs after the dialog gets ready for input.
executeSoon(() => resolve(aEvent.detail.dialog._frame.contentWindow));
}
);
});
}
function openPreferencesViaOpenPreferencesAPI(aPane, aOptions) {
return new Promise(resolve => {
let finalPrefPaneLoaded = TestUtils.topicObserved(
"sync-pane-loaded",
() => true
);
gBrowser.selectedTab = BrowserTestUtils.addTab(gBrowser, "about:blank");
openPreferences(aPane);
let newTabBrowser = gBrowser.selectedBrowser;
newTabBrowser.addEventListener(
"Initialized",
function () {
newTabBrowser.contentWindow.addEventListener(
"load",
async function () {
let win = gBrowser.contentWindow;
let selectedPane = win.history.state;
await finalPrefPaneLoaded;
if (!aOptions || !aOptions.leaveOpen) {
gBrowser.removeCurrentTab();
}
resolve({ selectedPane });
},
{ once: true }
);
},
{ capture: true, once: true }
);
});
}
function openSiteDataSettingsDialog() {
let doc = gBrowser.selectedBrowser.contentDocument;
let settingsBtn = doc.getElementById("siteDataSettings");
let dialogOverlay = content.gSubDialog._preloadDialog._overlay;
let dialogLoadPromise = promiseLoadSubDialog(
"chrome://browser/content/preferences/dialogs/siteDataSettings.xhtml"
);
let dialogInitPromise = TestUtils.topicObserved(
"sitedata-settings-init",
() => true
);
let fullyLoadPromise = Promise.all([
dialogLoadPromise,
dialogInitPromise,
]).then(() => {
is_element_visible(dialogOverlay, "The Settings dialog should be visible");
});
settingsBtn.doCommand();
return fullyLoadPromise;
}
function promiseSettingsDialogClose() {
return new Promise(resolve => {
let win = gBrowser.selectedBrowser.contentWindow;
let dialogOverlay = win.gSubDialog._topDialog._overlay;
let dialogWin = win.gSubDialog._topDialog._frame.contentWindow;
dialogWin.addEventListener(
"unload",
function unload() {
if (
dialogWin.document.documentURI ===
"chrome://browser/content/preferences/dialogs/siteDataSettings.xhtml"
) {
is_element_hidden(
dialogOverlay,
"The Settings dialog should be hidden"
);
resolve();
}
},
{ once: true }
);
});
}
function assertSitesListed(doc, hosts) {
let frameDoc = content.gSubDialog._topDialog._frame.contentDocument;
let removeAllBtn = frameDoc.getElementById("removeAll");
let sitesList = frameDoc.getElementById("sitesList");
let totalSitesNumber = sitesList.getElementsByTagName("richlistitem").length;
is(totalSitesNumber, hosts.length, "Should list the right sites number");
hosts.forEach(host => {
let site = sitesList.querySelector(`richlistitem[host="${host}"]`);
ok(site, `Should list the site of ${host}`);
});
is(removeAllBtn.disabled, false, "Should enable the removeAllBtn button");
}
// Counter used by addTestData to generate unique cookie names across function
// calls.
let cookieID = 0;
async function addTestData(data) {
let hosts = new Set();
for (let site of data) {
is(
typeof site.origin,
"string",
"Passed an origin string into addTestData."
);
if (site.persisted) {
await SiteDataTestUtils.persist(site.origin);
}
if (site.usage) {
await SiteDataTestUtils.addToIndexedDB(site.origin, site.usage);
}
for (let i = 0; i < (site.cookies || 0); i++) {
SiteDataTestUtils.addToCookies({
origin: site.origin,
name: `cookie${cookieID++}`,
});
}
let principal =
Services.scriptSecurityManager.createContentPrincipalFromOrigin(
site.origin
);
hosts.add(principal.baseDomain || principal.host);
}
return Array.from(hosts);
}
function promiseCookiesCleared() {
return TestUtils.topicObserved("cookie-changed", subj => {
return (
subj.QueryInterface(Ci.nsICookieNotification).action ==
Ci.nsICookieNotification.ALL_COOKIES_CLEARED
);
});
}
async function loadServiceWorkerTestPage(url) {
let tab = await BrowserTestUtils.openNewForegroundTab(gBrowser, url);
await TestUtils.waitForCondition(() => {
return SpecialPowers.spawn(
tab.linkedBrowser,
[],
() =>
content.document.body.getAttribute(
"data-test-service-worker-registered"
) === "true"
);
}, `Fail to load service worker test ${url}`);
BrowserTestUtils.removeTab(tab);
}
function promiseServiceWorkersCleared() {
return TestUtils.waitForCondition(() => {
let serviceWorkers = serviceWorkerManager.getAllRegistrations();
if (!serviceWorkers.length) {
ok(true, "Cleared all service workers");
return true;
}
return false;
}, "Should clear all service workers");
}
function promiseServiceWorkerRegisteredFor(url) {
return TestUtils.waitForCondition(() => {
try {
let principal =
Services.scriptSecurityManager.createContentPrincipalFromOrigin(url);
let sw = serviceWorkerManager.getRegistrationByPrincipal(
principal,
principal.spec
);
if (sw) {
ok(true, `Found the service worker registered for ${url}`);
return true;
}
} catch (e) {}
return false;
}, `Should register service worker for ${url}`);
}
|