Bug 1899500 - Implement explicit resource management in Baseline compiler. r=arai
[gecko.git] / browser / components / preferences / tests / browser_security-1.js
blob80f3e6902bfb0ab74e8a6fc5d2831ef252a1c819
1 const PREFS = [
2   "browser.safebrowsing.phishing.enabled",
3   "browser.safebrowsing.malware.enabled",
5   "browser.safebrowsing.downloads.enabled",
7   "browser.safebrowsing.downloads.remote.block_potentially_unwanted",
8   "browser.safebrowsing.downloads.remote.block_uncommon",
9 ];
11 let originals = PREFS.map(pref => [pref, Services.prefs.getBoolPref(pref)]);
12 let originalMalwareTable = Services.prefs.getCharPref(
13   "urlclassifier.malwareTable"
15 registerCleanupFunction(function () {
16   originals.forEach(([pref, val]) => Services.prefs.setBoolPref(pref, val));
17   Services.prefs.setCharPref(
18     "urlclassifier.malwareTable",
19     originalMalwareTable
20   );
21 });
23 // This test only opens the Preferences once, and then reloads the page
24 // each time that it wants to test various preference combinations. We
25 // only use one tab (instead of opening/closing for each test) for all
26 // to help improve test times on debug builds.
27 add_setup(async function () {
28   await openPreferencesViaOpenPreferencesAPI("privacy", { leaveOpen: true });
29   registerCleanupFunction(async function () {
30     BrowserTestUtils.removeTab(gBrowser.selectedTab);
31   });
32 });
34 // test the safebrowsing preference
35 add_task(async function () {
36   async function checkPrefSwitch(val1, val2) {
37     Services.prefs.setBoolPref("browser.safebrowsing.phishing.enabled", val1);
38     Services.prefs.setBoolPref("browser.safebrowsing.malware.enabled", val2);
40     gBrowser.reload();
41     await BrowserTestUtils.browserLoaded(gBrowser.selectedBrowser);
43     let doc = gBrowser.selectedBrowser.contentDocument;
44     let checkbox = doc.getElementById("enableSafeBrowsing");
45     let blockDownloads = doc.getElementById("blockDownloads");
46     let blockUncommon = doc.getElementById("blockUncommonUnwanted");
47     let checked = checkbox.checked;
48     is(
49       blockDownloads.hasAttribute("disabled"),
50       !checked,
51       "block downloads checkbox is set correctly"
52     );
54     is(
55       checked,
56       val1 && val2,
57       "safebrowsing preference is initialized correctly"
58     );
59     // should be disabled when checked is false (= pref is turned off)
60     is(
61       blockUncommon.hasAttribute("disabled"),
62       !checked,
63       "block uncommon checkbox is set correctly"
64     );
66     // scroll the checkbox into the viewport and click checkbox
67     checkbox.scrollIntoView();
68     EventUtils.synthesizeMouseAtCenter(
69       checkbox,
70       {},
71       gBrowser.selectedBrowser.contentWindow
72     );
74     // check that both settings are now turned on or off
75     is(
76       Services.prefs.getBoolPref("browser.safebrowsing.phishing.enabled"),
77       !checked,
78       "safebrowsing.enabled is set correctly"
79     );
80     is(
81       Services.prefs.getBoolPref("browser.safebrowsing.malware.enabled"),
82       !checked,
83       "safebrowsing.malware.enabled is set correctly"
84     );
86     // check if the other checkboxes have updated
87     checked = checkbox.checked;
88     if (blockDownloads) {
89       is(
90         blockDownloads.hasAttribute("disabled"),
91         !checked,
92         "block downloads checkbox is set correctly"
93       );
94       is(
95         blockUncommon.hasAttribute("disabled"),
96         !checked || !blockDownloads.checked,
97         "block uncommon checkbox is set correctly"
98       );
99     }
100   }
102   await checkPrefSwitch(true, true);
103   await checkPrefSwitch(false, true);
104   await checkPrefSwitch(true, false);
105   await checkPrefSwitch(false, false);