browser_privacy_cookieBannerHandling.js (5064B)
1 /* Any copyright is dedicated to the Public Domain. 2 * https://creativecommons.org/publicdomain/zero/1.0/ */ 3 4 // This file tests the Privacy pane's Cookie Banner Handling UI. 5 6 "use strict"; 7 8 const FEATURE_PREF = "cookiebanners.ui.desktop.enabled"; 9 const MODE_PREF = "cookiebanners.service.mode"; 10 const PBM_MODE_PREF = "cookiebanners.service.mode.privateBrowsing"; 11 12 const GROUPBOX_ID = "cookieBannerHandlingGroup"; 13 const CHECKBOX_ID = "handleCookieBanners"; 14 15 // Test the section is hidden on page load if the feature pref is disabled. 16 add_task(async function test_section_hidden_when_feature_flag_disabled() { 17 await SpecialPowers.pushPrefEnv({ 18 set: [ 19 [FEATURE_PREF, false], 20 [PBM_MODE_PREF, Ci.nsICookieBannerService.MODE_DISABLED], 21 ], 22 }); 23 24 await BrowserTestUtils.withNewTab( 25 { gBrowser, url: "about:preferences#privacy" }, 26 async function (browser) { 27 let groupbox = browser.contentDocument.getElementById(GROUPBOX_ID); 28 is_element_hidden(groupbox, "#cookieBannerHandlingGroup is hidden"); 29 } 30 ); 31 32 await SpecialPowers.popPrefEnv(); 33 }); 34 35 // Test the section is shown on page load if the feature pref is enabled. 36 add_task(async function test_section_shown_when_feature_flag_enabled() { 37 await SpecialPowers.pushPrefEnv({ 38 set: [ 39 [FEATURE_PREF, true], 40 [PBM_MODE_PREF, Ci.nsICookieBannerService.MODE_DISABLED], 41 ], 42 }); 43 44 await BrowserTestUtils.withNewTab( 45 { gBrowser, url: "about:preferences#privacy" }, 46 async function (browser) { 47 let groupbox = browser.contentDocument.getElementById(GROUPBOX_ID); 48 is_element_visible(groupbox, "#cookieBannerHandlingGroup is visible"); 49 } 50 ); 51 52 await SpecialPowers.popPrefEnv(); 53 }); 54 55 // Test the checkbox is unchecked in DISABLED mode. 56 add_task(async function test_checkbox_unchecked_disabled_mode() { 57 await SpecialPowers.pushPrefEnv({ 58 set: [ 59 [FEATURE_PREF, true], 60 [PBM_MODE_PREF, Ci.nsICookieBannerService.MODE_DISABLED], 61 ], 62 }); 63 64 await BrowserTestUtils.withNewTab( 65 { gBrowser, url: "about:preferences#privacy" }, 66 async function (browser) { 67 let checkbox = browser.contentDocument.getElementById(CHECKBOX_ID); 68 ok(!checkbox.checked, "checkbox is not checked in DISABLED mode"); 69 } 70 ); 71 72 await SpecialPowers.popPrefEnv(); 73 }); 74 75 // Test the checkbox is checked in REJECT mode. 76 add_task(async function test_checkbox_checked_reject_mode() { 77 await SpecialPowers.pushPrefEnv({ 78 set: [ 79 [FEATURE_PREF, true], 80 [PBM_MODE_PREF, Ci.nsICookieBannerService.MODE_REJECT], 81 ], 82 }); 83 84 await BrowserTestUtils.withNewTab( 85 { gBrowser, url: "about:preferences#privacy" }, 86 async function (browser) { 87 let checkbox = browser.contentDocument.getElementById(CHECKBOX_ID); 88 ok(checkbox.checked, "checkbox is checked in REJECT mode"); 89 } 90 ); 91 92 await SpecialPowers.popPrefEnv(); 93 }); 94 95 // Test that toggling the checkbox toggles the mode pref value as expected 96 add_task(async function test_checkbox_modifies_prefs() { 97 await SpecialPowers.pushPrefEnv({ 98 set: [ 99 [FEATURE_PREF, true], 100 [MODE_PREF, Ci.nsICookieBannerService.MODE_DISABLED], 101 [PBM_MODE_PREF, Ci.nsICookieBannerService.MODE_DISABLED], 102 ], 103 }); 104 105 await BrowserTestUtils.withNewTab( 106 { gBrowser, url: "about:preferences#privacy" }, 107 async function (browser) { 108 let checkboxSelector = "#" + CHECKBOX_ID; 109 let checkbox = browser.contentDocument.querySelector(checkboxSelector); 110 let section = browser.contentDocument.getElementById(GROUPBOX_ID); 111 112 section.scrollIntoView(); 113 114 Assert.ok( 115 !checkbox.checked, 116 "initially, the checkbox should be unchecked" 117 ); 118 119 await BrowserTestUtils.synthesizeMouseAtCenter( 120 checkboxSelector, 121 {}, 122 browser 123 ); 124 Assert.ok(checkbox.checked, "checkbox should be checked"); 125 126 Assert.equal( 127 Ci.nsICookieBannerService.MODE_REJECT, 128 Services.prefs.getIntPref(PBM_MODE_PREF), 129 "cookie banner handling mode for PBM should be set to REJECT after checking the checkbox" 130 ); 131 132 Assert.equal( 133 Ci.nsICookieBannerService.MODE_DISABLED, 134 Services.prefs.getIntPref(MODE_PREF), 135 "non-PBM cookie banner handling mode should be unchanged after checking the checkbox" 136 ); 137 138 await BrowserTestUtils.synthesizeMouseAtCenter( 139 checkboxSelector, 140 {}, 141 browser 142 ); 143 Assert.ok(!checkbox.checked, "checkbox should be unchecked"); 144 Assert.equal( 145 Ci.nsICookieBannerService.MODE_DISABLED, 146 Services.prefs.getIntPref(PBM_MODE_PREF), 147 "cookie banner handling mode for PBM should be set to DISABLED after unchecking the checkbox" 148 ); 149 150 // this ensures the off-ramp for non-PBM pref is working 151 Assert.equal( 152 Ci.nsICookieBannerService.MODE_DISABLED, 153 Services.prefs.getIntPref(MODE_PREF), 154 "non-PBM cookie banner handling mode should be set to DISABLED after unchecking the checkbox" 155 ); 156 } 157 ); 158 159 await SpecialPowers.popPrefEnv(); 160 });