Backed out 2 changesets (bug 1864896) for causing node failures. CLOSED TREE
[gecko.git] / browser / components / preferences / tests / browser_cookies_exceptions.js
blobd2d538a48a048c4abbe91d29d7b14103f23ea9a1
1 /* Any copyright is dedicated to the Public Domain.
2  * http://creativecommons.org/publicdomain/zero/1.0/ */
4 requestLongerTimeout(3);
6 add_task(async function testAllow() {
7   await runTest(
8     async (params, observeAllPromise, apply) => {
9       assertListContents(params, []);
11       params.url.value = "test.com";
12       params.btnAllow.doCommand();
14       assertListContents(params, [
15         ["http://test.com", params.allowL10nId],
16         ["https://test.com", params.allowL10nId],
17       ]);
19       apply();
20       await observeAllPromise;
21     },
22     params => {
23       return [
24         {
25           type: "cookie",
26           origin: "http://test.com",
27           data: "added",
28           capability: Ci.nsIPermissionManager.ALLOW_ACTION,
29         },
30         {
31           type: "cookie",
32           origin: "https://test.com",
33           data: "added",
34           capability: Ci.nsIPermissionManager.ALLOW_ACTION,
35         },
36       ];
37     }
38   );
39 });
41 add_task(async function testBlock() {
42   await runTest(
43     async (params, observeAllPromise, apply) => {
44       params.url.value = "test.com";
45       params.btnBlock.doCommand();
47       assertListContents(params, [
48         ["http://test.com", params.denyL10nId],
49         ["https://test.com", params.denyL10nId],
50       ]);
52       apply();
53       await observeAllPromise;
54     },
55     params => {
56       return [
57         {
58           type: "cookie",
59           origin: "http://test.com",
60           data: "changed",
61           capability: Ci.nsIPermissionManager.DENY_ACTION,
62         },
63         {
64           type: "cookie",
65           origin: "https://test.com",
66           data: "changed",
67           capability: Ci.nsIPermissionManager.DENY_ACTION,
68         },
69       ];
70     }
71   );
72 });
74 add_task(async function testAllowAgain() {
75   await runTest(
76     async (params, observeAllPromise, apply) => {
77       params.url.value = "test.com";
78       params.btnAllow.doCommand();
80       assertListContents(params, [
81         ["http://test.com", params.allowL10nId],
82         ["https://test.com", params.allowL10nId],
83       ]);
85       apply();
86       await observeAllPromise;
87     },
88     params => {
89       return [
90         {
91           type: "cookie",
92           origin: "http://test.com",
93           data: "changed",
94           capability: Ci.nsIPermissionManager.ALLOW_ACTION,
95         },
96         {
97           type: "cookie",
98           origin: "https://test.com",
99           data: "changed",
100           capability: Ci.nsIPermissionManager.ALLOW_ACTION,
101         },
102       ];
103     }
104   );
107 add_task(async function testRemove() {
108   await runTest(
109     async (params, observeAllPromise, apply) => {
110       while (params.richlistbox.itemCount) {
111         params.richlistbox.selectedIndex = 0;
112         params.btnRemove.doCommand();
113       }
114       assertListContents(params, []);
116       apply();
117       await observeAllPromise;
118     },
119     params => {
120       let richlistItems = params.richlistbox.getElementsByAttribute(
121         "origin",
122         "*"
123       );
124       let observances = [];
125       for (let item of richlistItems) {
126         observances.push({
127           type: "cookie",
128           origin: item.getAttribute("origin"),
129           data: "deleted",
130         });
131       }
132       return observances;
133     }
134   );
137 add_task(async function testAdd() {
138   await runTest(
139     async (params, observeAllPromise, apply) => {
140       let uri = Services.io.newURI("http://test.com");
141       PermissionTestUtils.add(
142         uri,
143         "popup",
144         Ci.nsIPermissionManager.DENY_ACTION
145       );
147       info("Adding unrelated permission should not change display.");
148       assertListContents(params, []);
150       apply();
151       await observeAllPromise;
153       PermissionTestUtils.remove(uri, "popup");
154     },
155     params => {
156       return [
157         {
158           type: "popup",
159           origin: "http://test.com",
160           data: "added",
161           capability: Ci.nsIPermissionManager.DENY_ACTION,
162         },
163       ];
164     }
165   );
168 add_task(async function testAllowHTTPSWithPort() {
169   await runTest(
170     async (params, observeAllPromise, apply) => {
171       params.url.value = "https://test.com:12345";
172       params.btnAllow.doCommand();
174       assertListContents(params, [
175         ["https://test.com:12345", params.allowL10nId],
176       ]);
178       apply();
179       await observeAllPromise;
180     },
181     params => {
182       return [
183         {
184           type: "cookie",
185           origin: "https://test.com:12345",
186           data: "added",
187           capability: Ci.nsIPermissionManager.ALLOW_ACTION,
188         },
189       ];
190     }
191   );
194 add_task(async function testBlockHTTPSWithPort() {
195   await runTest(
196     async (params, observeAllPromise, apply) => {
197       params.url.value = "https://test.com:12345";
198       params.btnBlock.doCommand();
200       assertListContents(params, [
201         ["https://test.com:12345", params.denyL10nId],
202       ]);
204       apply();
205       await observeAllPromise;
206     },
207     params => {
208       return [
209         {
210           type: "cookie",
211           origin: "https://test.com:12345",
212           data: "changed",
213           capability: Ci.nsIPermissionManager.DENY_ACTION,
214         },
215       ];
216     }
217   );
220 add_task(async function testAllowAgainHTTPSWithPort() {
221   await runTest(
222     async (params, observeAllPromise, apply) => {
223       params.url.value = "https://test.com:12345";
224       params.btnAllow.doCommand();
226       assertListContents(params, [
227         ["https://test.com:12345", params.allowL10nId],
228       ]);
230       apply();
231       await observeAllPromise;
232     },
233     params => {
234       return [
235         {
236           type: "cookie",
237           origin: "https://test.com:12345",
238           data: "changed",
239           capability: Ci.nsIPermissionManager.ALLOW_ACTION,
240         },
241       ];
242     }
243   );
246 add_task(async function testRemoveHTTPSWithPort() {
247   await runTest(
248     async (params, observeAllPromise, apply) => {
249       while (params.richlistbox.itemCount) {
250         params.richlistbox.selectedIndex = 0;
251         params.btnRemove.doCommand();
252       }
254       assertListContents(params, []);
256       apply();
257       await observeAllPromise;
258     },
259     params => {
260       let richlistItems = params.richlistbox.getElementsByAttribute(
261         "origin",
262         "*"
263       );
264       let observances = [];
265       for (let item of richlistItems) {
266         observances.push({
267           type: "cookie",
268           origin: item.getAttribute("origin"),
269           data: "deleted",
270         });
271       }
272       return observances;
273     }
274   );
277 add_task(async function testAllowPort() {
278   await runTest(
279     async (params, observeAllPromise, apply) => {
280       params.url.value = "localhost:12345";
281       params.btnAllow.doCommand();
283       assertListContents(params, [
284         ["http://localhost:12345", params.allowL10nId],
285         ["https://localhost:12345", params.allowL10nId],
286       ]);
288       apply();
289       await observeAllPromise;
290     },
291     params => {
292       return [
293         {
294           type: "cookie",
295           origin: "http://localhost:12345",
296           data: "added",
297           capability: Ci.nsIPermissionManager.ALLOW_ACTION,
298         },
299         {
300           type: "cookie",
301           origin: "https://localhost:12345",
302           data: "added",
303           capability: Ci.nsIPermissionManager.ALLOW_ACTION,
304         },
305       ];
306     }
307   );
310 add_task(async function testBlockPort() {
311   await runTest(
312     async (params, observeAllPromise, apply) => {
313       params.url.value = "localhost:12345";
314       params.btnBlock.doCommand();
316       assertListContents(params, [
317         ["http://localhost:12345", params.denyL10nId],
318         ["https://localhost:12345", params.denyL10nId],
319       ]);
321       apply();
322       await observeAllPromise;
323     },
324     params => {
325       return [
326         {
327           type: "cookie",
328           origin: "http://localhost:12345",
329           data: "changed",
330           capability: Ci.nsIPermissionManager.DENY_ACTION,
331         },
332         {
333           type: "cookie",
334           origin: "https://localhost:12345",
335           data: "changed",
336           capability: Ci.nsIPermissionManager.DENY_ACTION,
337         },
338       ];
339     }
340   );
343 add_task(async function testAllowAgainPort() {
344   await runTest(
345     async (params, observeAllPromise, apply) => {
346       params.url.value = "localhost:12345";
347       params.btnAllow.doCommand();
349       assertListContents(params, [
350         ["http://localhost:12345", params.allowL10nId],
351         ["https://localhost:12345", params.allowL10nId],
352       ]);
354       apply();
355       await observeAllPromise;
356     },
357     params => {
358       return [
359         {
360           type: "cookie",
361           origin: "http://localhost:12345",
362           data: "changed",
363           capability: Ci.nsIPermissionManager.ALLOW_ACTION,
364         },
365         {
366           type: "cookie",
367           origin: "https://localhost:12345",
368           data: "changed",
369           capability: Ci.nsIPermissionManager.ALLOW_ACTION,
370         },
371       ];
372     }
373   );
376 add_task(async function testRemovePort() {
377   await runTest(
378     async (params, observeAllPromise, apply) => {
379       while (params.richlistbox.itemCount) {
380         params.richlistbox.selectedIndex = 0;
381         params.btnRemove.doCommand();
382       }
384       assertListContents(params, []);
386       apply();
387       await observeAllPromise;
388     },
389     params => {
390       let richlistItems = params.richlistbox.getElementsByAttribute(
391         "origin",
392         "*"
393       );
394       let observances = [];
395       for (let item of richlistItems) {
396         observances.push({
397           type: "cookie",
398           origin: item.getAttribute("origin"),
399           data: "deleted",
400         });
401       }
402       return observances;
403     }
404   );
407 add_task(async function testSort() {
408   await runTest(
409     async (params, observeAllPromise, apply) => {
410       // Sort by site name.
411       EventUtils.synthesizeMouseAtCenter(
412         params.doc.getElementById("siteCol"),
413         {},
414         params.doc.defaultView
415       );
417       for (let URL of ["http://a", "http://z", "http://b"]) {
418         let URI = Services.io.newURI(URL);
419         PermissionTestUtils.add(
420           URI,
421           "cookie",
422           Ci.nsIPermissionManager.ALLOW_ACTION
423         );
424       }
426       assertListContents(params, [
427         ["http://a", params.allowL10nId],
428         ["http://b", params.allowL10nId],
429         ["http://z", params.allowL10nId],
430       ]);
432       // Sort by site name in descending order.
433       EventUtils.synthesizeMouseAtCenter(
434         params.doc.getElementById("siteCol"),
435         {},
436         params.doc.defaultView
437       );
439       assertListContents(params, [
440         ["http://z", params.allowL10nId],
441         ["http://b", params.allowL10nId],
442         ["http://a", params.allowL10nId],
443       ]);
445       apply();
446       await observeAllPromise;
448       for (let URL of ["http://a", "http://z", "http://b"]) {
449         let uri = Services.io.newURI(URL);
450         PermissionTestUtils.remove(uri, "cookie");
451       }
452     },
453     params => {
454       return [
455         {
456           type: "cookie",
457           origin: "http://a",
458           data: "added",
459           capability: Ci.nsIPermissionManager.ALLOW_ACTION,
460         },
461         {
462           type: "cookie",
463           origin: "http://z",
464           data: "added",
465           capability: Ci.nsIPermissionManager.ALLOW_ACTION,
466         },
467         {
468           type: "cookie",
469           origin: "http://b",
470           data: "added",
471           capability: Ci.nsIPermissionManager.ALLOW_ACTION,
472         },
473       ];
474     }
475   );
478 add_task(async function testPrivateBrowsingSessionPermissionsAreHidden() {
479   await runTest(
480     async (params, observeAllPromise, apply) => {
481       assertListContents(params, []);
483       let uri = Services.io.newURI("http://test.com");
484       let privateBrowsingPrincipal =
485         Services.scriptSecurityManager.createContentPrincipal(uri, {
486           privateBrowsingId: 1,
487         });
489       // Add a session permission for private browsing.
490       PermissionTestUtils.add(
491         privateBrowsingPrincipal,
492         "cookie",
493         Services.perms.ALLOW_ACTION,
494         Services.perms.EXPIRE_SESSION
495       );
497       assertListContents(params, []);
499       PermissionTestUtils.remove(uri, "cookie");
500     },
501     params => {
502       return [];
503     }
504   );
507 function assertListContents(params, expected) {
508   Assert.equal(params.richlistbox.itemCount, expected.length);
510   for (let i = 0; i < expected.length; i++) {
511     let website = expected[i][0];
512     let elements = params.richlistbox.getElementsByAttribute("origin", website);
513     Assert.equal(elements.length, 1); // "It should find only one coincidence"
514     Assert.equal(
515       elements[0]
516         .querySelector(".website-capability-value")
517         .getAttribute("data-l10n-id"),
518       expected[i][1]
519     );
520   }
523 async function runTest(test, getObservances) {
524   registerCleanupFunction(function () {
525     Services.prefs.clearUserPref("privacy.history.custom");
526   });
528   await openPreferencesViaOpenPreferencesAPI("panePrivacy", {
529     leaveOpen: true,
530   });
532   let doc = gBrowser.contentDocument;
533   let historyMode = doc.getElementById("historyMode");
534   historyMode.value = "custom";
535   historyMode.doCommand();
537   let promiseSubDialogLoaded = promiseLoadSubDialog(
538     "chrome://browser/content/preferences/dialogs/permissions.xhtml"
539   );
540   doc.getElementById("cookieExceptions").doCommand();
542   let win = await promiseSubDialogLoaded;
544   doc = win.document;
545   let params = {
546     doc,
547     richlistbox: doc.getElementById("permissionsBox"),
548     url: doc.getElementById("url"),
549     btnAllow: doc.getElementById("btnAllow"),
550     btnBlock: doc.getElementById("btnBlock"),
551     btnRemove: doc.getElementById("removePermission"),
552     allowL10nId: win.gPermissionManager._getCapabilityL10nId(
553       Ci.nsIPermissionManager.ALLOW_ACTION
554     ),
555     denyL10nId: win.gPermissionManager._getCapabilityL10nId(
556       Ci.nsIPermissionManager.DENY_ACTION
557     ),
558     allow: Ci.nsIPermissionManager.ALLOW_ACTION,
559     deny: Ci.nsIPermissionManager.DENY_ACTION,
560   };
561   let btnApplyChanges = doc.querySelector("dialog").getButton("accept");
562   let observances = getObservances(params);
563   let observeAllPromise = createObserveAllPromise(observances);
565   await test(params, observeAllPromise, () => btnApplyChanges.doCommand());
567   BrowserTestUtils.removeTab(gBrowser.selectedTab);