1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/browser/components/preferences/tests/privacypane_tests_perwindow.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,360 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +function runTestOnPrivacyPrefPane(testFunc) { 1.9 + let observer = { 1.10 + observe: function(aSubject, aTopic, aData) { 1.11 + if (aTopic == "domwindowopened") { 1.12 + Services.ww.unregisterNotification(this); 1.13 + 1.14 + let win = aSubject.QueryInterface(Ci.nsIDOMEventTarget); 1.15 + win.addEventListener("load", function() { 1.16 + win.removeEventListener("load", arguments.callee, false); 1.17 + testFunc(dialog.document.defaultView); 1.18 + 1.19 + Services.ww.registerNotification(observer); 1.20 + dialog.close(); 1.21 + }, false); 1.22 + } else if (aTopic == "domwindowclosed") { 1.23 + Services.ww.unregisterNotification(this); 1.24 + testRunner.runNext(); 1.25 + } 1.26 + } 1.27 + }; 1.28 + Services.ww.registerNotification(observer); 1.29 + 1.30 + let dialog = openDialog("chrome://browser/content/preferences/preferences.xul", "Preferences", 1.31 + "chrome,titlebar,toolbar,centerscreen,dialog=no", "panePrivacy"); 1.32 +} 1.33 + 1.34 +function controlChanged(element) { 1.35 + element.doCommand(); 1.36 +} 1.37 + 1.38 +// We can only test the panes that don't trigger a preference update 1.39 +function test_pane_visibility(win) { 1.40 + let modes = { 1.41 + "remember": "historyRememberPane", 1.42 + "custom": "historyCustomPane" 1.43 + }; 1.44 + 1.45 + let historymode = win.document.getElementById("historyMode"); 1.46 + ok(historymode, "history mode menulist should exist"); 1.47 + let historypane = win.document.getElementById("historyPane"); 1.48 + ok(historypane, "history mode pane should exist"); 1.49 + 1.50 + for (let mode in modes) { 1.51 + historymode.value = mode; 1.52 + controlChanged(historymode); 1.53 + is(historypane.selectedPanel, win.document.getElementById(modes[mode]), 1.54 + "The correct pane should be selected for the " + mode + " mode"); 1.55 + } 1.56 +} 1.57 + 1.58 +function test_dependent_elements(win) { 1.59 + let historymode = win.document.getElementById("historyMode"); 1.60 + ok(historymode, "history mode menulist should exist"); 1.61 + let pbautostart = win.document.getElementById("privateBrowsingAutoStart"); 1.62 + ok(pbautostart, "the private browsing auto-start checkbox should exist"); 1.63 + let controls = [ 1.64 + win.document.getElementById("rememberHistory"), 1.65 + win.document.getElementById("rememberForms"), 1.66 + win.document.getElementById("keepUntil"), 1.67 + win.document.getElementById("keepCookiesUntil"), 1.68 + win.document.getElementById("alwaysClear"), 1.69 + ]; 1.70 + controls.forEach(function(control) { 1.71 + ok(control, "the dependent controls should exist"); 1.72 + }); 1.73 + let independents = [ 1.74 + win.document.getElementById("acceptCookies"), 1.75 + win.document.getElementById("acceptThirdPartyLabel"), 1.76 + win.document.getElementById("acceptThirdPartyMenu") 1.77 + ]; 1.78 + independents.forEach(function(control) { 1.79 + ok(control, "the independent controls should exist"); 1.80 + }); 1.81 + let cookieexceptions = win.document.getElementById("cookieExceptions"); 1.82 + ok(cookieexceptions, "the cookie exceptions button should exist"); 1.83 + let keepuntil = win.document.getElementById("keepCookiesUntil"); 1.84 + ok(keepuntil, "the keep cookies until menulist should exist"); 1.85 + let alwaysclear = win.document.getElementById("alwaysClear"); 1.86 + ok(alwaysclear, "the clear data on close checkbox should exist"); 1.87 + let rememberhistory = win.document.getElementById("rememberHistory"); 1.88 + ok(rememberhistory, "the remember history checkbox should exist"); 1.89 + let rememberforms = win.document.getElementById("rememberForms"); 1.90 + ok(rememberforms, "the remember forms checkbox should exist"); 1.91 + let alwaysclearsettings = win.document.getElementById("clearDataSettings"); 1.92 + ok(alwaysclearsettings, "the clear data settings button should exist"); 1.93 + 1.94 + function expect_disabled(disabled) { 1.95 + controls.forEach(function(control) { 1.96 + is(control.disabled, disabled, 1.97 + control.getAttribute("id") + " should " + (disabled ? "" : "not ") + "be disabled"); 1.98 + }); 1.99 + is(keepuntil.value, disabled ? 2 : 0, 1.100 + "the keep cookies until menulist value should be as expected"); 1.101 + if (disabled) { 1.102 + ok(!alwaysclear.checked, 1.103 + "the clear data on close checkbox value should be as expected"); 1.104 + ok(!rememberhistory.checked, 1.105 + "the remember history checkbox value should be as expected"); 1.106 + ok(!rememberforms.checked, 1.107 + "the remember forms checkbox value should be as expected"); 1.108 + } 1.109 + } 1.110 + function check_independents(expected) { 1.111 + independents.forEach(function(control) { 1.112 + is(control.disabled, expected, 1.113 + control.getAttribute("id") + " should " + (expected ? "" : "not ") + "be disabled"); 1.114 + }); 1.115 + ok(!cookieexceptions.disabled, 1.116 + "the cookie exceptions button should never be disabled"); 1.117 + ok(alwaysclearsettings.disabled, 1.118 + "the clear data settings button should always be disabled"); 1.119 + } 1.120 + 1.121 + // controls should only change in custom mode 1.122 + historymode.value = "remember"; 1.123 + controlChanged(historymode); 1.124 + expect_disabled(false); 1.125 + check_independents(false); 1.126 + 1.127 + // setting the mode to custom shouldn't change anything 1.128 + historymode.value = "custom"; 1.129 + controlChanged(historymode); 1.130 + expect_disabled(false); 1.131 + check_independents(false); 1.132 +} 1.133 + 1.134 +function test_dependent_cookie_elements(win) { 1.135 + let historymode = win.document.getElementById("historyMode"); 1.136 + ok(historymode, "history mode menulist should exist"); 1.137 + let pbautostart = win.document.getElementById("privateBrowsingAutoStart"); 1.138 + ok(pbautostart, "the private browsing auto-start checkbox should exist"); 1.139 + let controls = [ 1.140 + win.document.getElementById("acceptThirdPartyLabel"), 1.141 + win.document.getElementById("acceptThirdPartyMenu"), 1.142 + win.document.getElementById("keepUntil"), 1.143 + win.document.getElementById("keepCookiesUntil"), 1.144 + ]; 1.145 + controls.forEach(function(control) { 1.146 + ok(control, "the dependent cookie controls should exist"); 1.147 + }); 1.148 + let acceptcookies = win.document.getElementById("acceptCookies"); 1.149 + ok(acceptcookies, "the accept cookies checkbox should exist"); 1.150 + 1.151 + function expect_disabled(disabled) { 1.152 + controls.forEach(function(control) { 1.153 + is(control.disabled, disabled, 1.154 + control.getAttribute("id") + " should " + (disabled ? "" : "not ") + "be disabled"); 1.155 + }); 1.156 + } 1.157 + 1.158 + historymode.value = "custom"; 1.159 + controlChanged(historymode); 1.160 + pbautostart.checked = false; 1.161 + controlChanged(pbautostart); 1.162 + expect_disabled(false); 1.163 + 1.164 + acceptcookies.checked = false; 1.165 + controlChanged(acceptcookies); 1.166 + expect_disabled(true); 1.167 + 1.168 + acceptcookies.checked = true; 1.169 + controlChanged(acceptcookies); 1.170 + expect_disabled(false); 1.171 + 1.172 + let accessthirdparty = controls.shift(); 1.173 + acceptcookies.checked = false; 1.174 + controlChanged(acceptcookies); 1.175 + expect_disabled(true); 1.176 + ok(accessthirdparty.disabled, "access third party button should be disabled"); 1.177 + 1.178 + pbautostart.checked = false; 1.179 + controlChanged(pbautostart); 1.180 + expect_disabled(true); 1.181 + ok(accessthirdparty.disabled, "access third party button should be disabled"); 1.182 + 1.183 + acceptcookies.checked = true; 1.184 + controlChanged(acceptcookies); 1.185 + expect_disabled(false); 1.186 + ok(!accessthirdparty.disabled, "access third party button should be enabled"); 1.187 +} 1.188 + 1.189 +function test_dependent_clearonclose_elements(win) { 1.190 + let historymode = win.document.getElementById("historyMode"); 1.191 + ok(historymode, "history mode menulist should exist"); 1.192 + let pbautostart = win.document.getElementById("privateBrowsingAutoStart"); 1.193 + ok(pbautostart, "the private browsing auto-start checkbox should exist"); 1.194 + let alwaysclear = win.document.getElementById("alwaysClear"); 1.195 + ok(alwaysclear, "the clear data on close checkbox should exist"); 1.196 + let alwaysclearsettings = win.document.getElementById("clearDataSettings"); 1.197 + ok(alwaysclearsettings, "the clear data settings button should exist"); 1.198 + 1.199 + function expect_disabled(disabled) { 1.200 + is(alwaysclearsettings.disabled, disabled, 1.201 + "the clear data settings should " + (disabled ? "" : "not ") + "be disabled"); 1.202 + } 1.203 + 1.204 + historymode.value = "custom"; 1.205 + controlChanged(historymode); 1.206 + pbautostart.checked = false; 1.207 + controlChanged(pbautostart); 1.208 + alwaysclear.checked = false; 1.209 + controlChanged(alwaysclear); 1.210 + expect_disabled(true); 1.211 + 1.212 + alwaysclear.checked = true; 1.213 + controlChanged(alwaysclear); 1.214 + expect_disabled(false); 1.215 + 1.216 + alwaysclear.checked = false; 1.217 + controlChanged(alwaysclear); 1.218 + expect_disabled(true); 1.219 +} 1.220 + 1.221 +function test_dependent_prefs(win) { 1.222 + let historymode = win.document.getElementById("historyMode"); 1.223 + ok(historymode, "history mode menulist should exist"); 1.224 + let controls = [ 1.225 + win.document.getElementById("rememberHistory"), 1.226 + win.document.getElementById("rememberForms"), 1.227 + win.document.getElementById("acceptCookies"), 1.228 + ]; 1.229 + controls.forEach(function(control) { 1.230 + ok(control, "the micro-management controls should exist"); 1.231 + }); 1.232 + 1.233 + let thirdPartyCookieMenu = win.document.getElementById("acceptThirdPartyMenu"); 1.234 + ok(thirdPartyCookieMenu, "the third-party cookie control should exist"); 1.235 + 1.236 + function expect_checked(checked) { 1.237 + controls.forEach(function(control) { 1.238 + is(control.checked, checked, 1.239 + control.getAttribute("id") + " should " + (checked ? "not " : "") + "be checked"); 1.240 + }); 1.241 + 1.242 + is(thirdPartyCookieMenu.value == "always" || thirdPartyCookieMenu.value == "visited", checked, "third-party cookies should " + (checked ? "not " : "") + "be limited"); 1.243 + } 1.244 + 1.245 + // controls should be checked in remember mode 1.246 + historymode.value = "remember"; 1.247 + controlChanged(historymode); 1.248 + expect_checked(true); 1.249 + 1.250 + // even if they're unchecked in custom mode 1.251 + historymode.value = "custom"; 1.252 + controlChanged(historymode); 1.253 + thirdPartyCookieMenu.value = "never"; 1.254 + controlChanged(thirdPartyCookieMenu); 1.255 + controls.forEach(function(control) { 1.256 + control.checked = false; 1.257 + controlChanged(control); 1.258 + }); 1.259 + expect_checked(false); 1.260 + historymode.value = "remember"; 1.261 + controlChanged(historymode); 1.262 + expect_checked(true); 1.263 +} 1.264 + 1.265 +function test_historymode_retention(mode, expect) { 1.266 + return function(win) { 1.267 + let historymode = win.document.getElementById("historyMode"); 1.268 + ok(historymode, "history mode menulist should exist"); 1.269 + 1.270 + if ((historymode.value == "remember" && mode == "dontremember") || 1.271 + (historymode.value == "dontremember" && mode == "remember") || 1.272 + (historymode.value == "custom" && mode == "dontremember")) { 1.273 + return; 1.274 + } 1.275 + 1.276 + if (expect !== undefined) { 1.277 + is(historymode.value, expect, 1.278 + "history mode is expected to remain " + expect); 1.279 + } 1.280 + 1.281 + historymode.value = mode; 1.282 + controlChanged(historymode); 1.283 + }; 1.284 +} 1.285 + 1.286 +function test_custom_retention(controlToChange, expect, valueIncrement) { 1.287 + return function(win) { 1.288 + let historymode = win.document.getElementById("historyMode"); 1.289 + ok(historymode, "history mode menulist should exist"); 1.290 + 1.291 + if (expect !== undefined) { 1.292 + is(historymode.value, expect, 1.293 + "history mode is expected to remain " + expect); 1.294 + } 1.295 + 1.296 + historymode.value = "custom"; 1.297 + controlChanged(historymode); 1.298 + 1.299 + controlToChange = win.document.getElementById(controlToChange); 1.300 + ok(controlToChange, "the control to change should exist"); 1.301 + switch (controlToChange.localName) { 1.302 + case "checkbox": 1.303 + controlToChange.checked = !controlToChange.checked; 1.304 + break; 1.305 + case "textbox": 1.306 + controlToChange.value = parseInt(controlToChange.value) + valueIncrement; 1.307 + break; 1.308 + case "menulist": 1.309 + controlToChange.value = valueIncrement; 1.310 + break; 1.311 + } 1.312 + controlChanged(controlToChange); 1.313 + }; 1.314 +} 1.315 + 1.316 +function test_locbar_suggestion_retention(mode, expect) { 1.317 + return function(win) { 1.318 + let locbarsuggest = win.document.getElementById("locationBarSuggestion"); 1.319 + ok(locbarsuggest, "location bar suggestion menulist should exist"); 1.320 + 1.321 + if (expect !== undefined) { 1.322 + is(locbarsuggest.value, expect, 1.323 + "location bar suggestion is expected to remain " + expect); 1.324 + } 1.325 + 1.326 + locbarsuggest.value = mode; 1.327 + controlChanged(locbarsuggest); 1.328 + }; 1.329 +} 1.330 + 1.331 +function reset_preferences(win) { 1.332 + let prefs = win.document.getElementsByTagName("preference"); 1.333 + for (let i = 0; i < prefs.length; ++i) 1.334 + if (prefs[i].hasUserValue) 1.335 + prefs[i].reset(); 1.336 +} 1.337 + 1.338 +let testRunner; 1.339 +function run_test_subset(subset) { 1.340 + let instantApplyOrig = Services.prefs.getBoolPref("browser.preferences.instantApply"); 1.341 + Services.prefs.setBoolPref("browser.preferences.instantApply", true); 1.342 + 1.343 + waitForExplicitFinish(); 1.344 + 1.345 + testRunner = { 1.346 + tests: subset, 1.347 + counter: 0, 1.348 + runNext: function() { 1.349 + if (this.counter == this.tests.length) { 1.350 + // cleanup 1.351 + Services.prefs.setBoolPref("browser.preferences.instantApply", instantApplyOrig); 1.352 + finish(); 1.353 + } else { 1.354 + let self = this; 1.355 + setTimeout(function() { 1.356 + runTestOnPrivacyPrefPane(self.tests[self.counter++]); 1.357 + }, 0); 1.358 + } 1.359 + } 1.360 + }; 1.361 + 1.362 + testRunner.runNext(); 1.363 +}