browser/components/preferences/in-content/tests/privacypane_tests_perwindow.js

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

mercurial