1 /* ***** BEGIN LICENSE BLOCK *****
2 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 * The contents of this file are subject to the Mozilla Public License Version
5 * 1.1 (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 * http://www.mozilla.org/MPL/
9 * Software distributed under the License is distributed on an "AS IS" basis,
10 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 * for the specific language governing rights and limitations under the
14 * The Original Code is Privacy PrefPane Test.
16 * The Initial Developer of the Original Code is
18 * Portions created by the Initial Developer are Copyright (C) 2009
19 * the Initial Developer. All Rights Reserved.
22 * Ehsan Akhgari <ehsan.akhgari@gmail.com> (Original Author)
24 * Alternatively, the contents of this file may be used under the terms of
25 * either the GNU General Public License Version 2 or later (the "GPL"), or
26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
38 function runTestOnPrivacyPrefPane(testFunc) {
39 let ww = Cc["@mozilla.org/embedcomp/window-watcher;1"].
40 getService(Ci.nsIWindowWatcher);
42 observe: function(aSubject, aTopic, aData) {
43 if (aTopic == "domwindowopened") {
44 ww.unregisterNotification(this);
46 let win = aSubject.QueryInterface(Ci.nsIDOMEventTarget);
47 win.addEventListener("load", function() {
48 win.removeEventListener("load", arguments.callee, false);
49 testFunc(dialog.document.defaultView);
51 ww.registerNotification(observer);
54 } else if (aTopic == "domwindowclosed") {
55 ww.unregisterNotification(this);
60 ww.registerNotification(observer);
62 let dialog = openDialog("chrome://browser/content/preferences/preferences.xul", "Preferences",
63 "chrome,titlebar,toolbar,centerscreen,dialog=no", "panePrivacy");
66 function controlChanged(doc, element) {
67 let event = doc.createEvent("Events");
68 event.initEvent("command", true, true);
69 element.dispatchEvent(event);
72 function test_locbar_emptyText(win) {
73 let texts = ["none", "bookmarkhistory", "history", "bookmark"];
75 let locbarlist = win.document.getElementById("locationBarSuggestion");
76 ok(locbarlist, "location bar suggestion menulist should exist");
78 for (let level = -1; level <= 2; ++level) {
79 locbarlist.value = level;
80 controlChanged(win.document, locbarlist);
81 is(gURLBar.emptyText, gURLBar.getAttribute(texts[level + 1] + "emptytext"),
82 "location bar empty text for for level " + level + " is correctly set");
86 function test_pane_visibility(win) {
88 "remember": "historyRememberPane",
89 "dontremember": "historyDontRememberPane",
90 "custom": "historyCustomPane"
93 let historymode = win.document.getElementById("historyMode");
94 ok(historymode, "history mode menulist should exist");
95 let historypane = win.document.getElementById("historyPane");
96 ok(historypane, "history mode pane should exist");
98 for (let mode in modes) {
99 historymode.value = mode;
100 controlChanged(win.document, historymode);
101 is(historypane.selectedPanel, win.document.getElementById(modes[mode]),
102 "The correct pane should be selected for the " + mode + " mode");
106 function test_dependent_elements(win) {
107 let historymode = win.document.getElementById("historyMode");
108 ok(historymode, "history mode menulist should exist");
109 let pbautostart = win.document.getElementById("privateBrowsingAutoStart");
110 ok(pbautostart, "the private browsing auto-start checkbox should exist");
112 win.document.getElementById("rememberHistoryDays"),
113 win.document.getElementById("historyDays"),
114 win.document.getElementById("rememberAfter"),
115 win.document.getElementById("rememberDownloads"),
116 win.document.getElementById("rememberForms"),
117 win.document.getElementById("keepUntil"),
118 win.document.getElementById("keepCookiesUntil"),
119 win.document.getElementById("alwaysClear"),
121 controls.forEach(function(control) {
122 ok(control, "the dependent controls should exist");
125 win.document.getElementById("acceptCookies"),
126 win.document.getElementById("acceptThirdParty"),
128 independents.forEach(function(control) {
129 ok(control, "the independent controls should exist");
131 let cookieexceptions = win.document.getElementById("cookieExceptions");
132 ok(cookieexceptions, "the cookie exceptions button should exist");
133 let keepuntil = win.document.getElementById("keepCookiesUntil");
134 ok(keepuntil, "the keep cookies until menulist should exist");
135 let alwaysclear = win.document.getElementById("alwaysClear");
136 ok(alwaysclear, "the clear data on close checkbox should exist");
137 let rememberhistory = win.document.getElementById("rememberHistoryDays");
138 ok(rememberhistory, "the remember history checkbox should exist");
139 let rememberdownloads = win.document.getElementById("rememberDownloads");
140 ok(rememberdownloads, "the remember downloads checkbox should exist");
141 let rememberforms = win.document.getElementById("rememberForms");
142 ok(rememberforms, "the remember forms checkbox should exist");
143 let alwaysclearsettings = win.document.getElementById("clearDataSettings");
144 ok(alwaysclearsettings, "the clear data settings button should exist");
146 function expect_disabled(disabled) {
147 controls.forEach(function(control) {
148 is(control.disabled, disabled,
149 control.getAttribute("id") + " should " + (disabled ? "" : "not ") + "be disabled");
151 is(keepuntil.value, disabled ? 2 : 0,
152 "the keep cookies until menulist value should be as expected");
154 ok(!alwaysclear.checked,
155 "the clear data on close checkbox value should be as expected");
156 ok(!rememberhistory.checked,
157 "the remember history checkbox value should be as expected");
158 ok(!rememberdownloads.checked,
159 "the remember downloads checkbox value should be as expected");
160 ok(!rememberforms.checked,
161 "the remember forms checkbox value should be as expected");
164 function check_independents(expected) {
165 independents.forEach(function(control) {
166 is(control.disabled, expected,
167 control.getAttribute("id") + " should " + (expected ? "" : "not ") + "be disabled");
169 ok(!cookieexceptions.disabled,
170 "the cookie exceptions button should never be disabled");
171 ok(alwaysclearsettings.disabled,
172 "the clear data settings button should always be disabled");
175 // controls should only change in custom mode
176 historymode.value = "remember";
177 controlChanged(win.document, historymode);
178 expect_disabled(false);
179 check_independents(false);
181 // setting the mode to custom shouldn't change anything
182 historymode.value = "custom";
183 controlChanged(win.document, historymode);
184 expect_disabled(false);
185 check_independents(false);
187 // controls should only change in custom mode
188 historymode.value = "dontremember";
189 controlChanged(win.document, historymode);
190 expect_disabled(false);
191 check_independents(false);
193 // controls should only change in custom mode
194 historymode.value = "custom";
195 controlChanged(win.document, historymode);
196 expect_disabled(true);
197 check_independents(false);
199 // dependent controls should follow pbautostart
200 pbautostart.checked = false;
201 controlChanged(win.document, pbautostart);
202 expect_disabled(false);
203 check_independents(false);
205 // dependent controls should follow pbautostart
206 pbautostart.checked = true;
207 controlChanged(win.document, pbautostart);
208 expect_disabled(true);
209 check_independents(false);
212 function test_dependent_cookie_elements(win) {
213 let historymode = win.document.getElementById("historyMode");
214 ok(historymode, "history mode menulist should exist");
215 let pbautostart = win.document.getElementById("privateBrowsingAutoStart");
216 ok(pbautostart, "the private browsing auto-start checkbox should exist");
218 win.document.getElementById("acceptThirdParty"),
219 win.document.getElementById("keepUntil"),
220 win.document.getElementById("keepCookiesUntil"),
222 controls.forEach(function(control) {
223 ok(control, "the dependent cookie controls should exist");
225 let acceptcookies = win.document.getElementById("acceptCookies");
226 ok(acceptcookies, "the accept cookies checkbox should exist");
228 function expect_disabled(disabled) {
229 controls.forEach(function(control) {
230 is(control.disabled, disabled,
231 control.getAttribute("id") + " should " + (disabled ? "" : "not ") + "be disabled");
235 historymode.value = "custom";
236 controlChanged(win.document, historymode);
237 pbautostart.checked = false;
238 controlChanged(win.document, pbautostart);
239 expect_disabled(false);
241 acceptcookies.checked = false;
242 controlChanged(win.document, acceptcookies);
243 expect_disabled(true);
245 // pbautostart shouldn't change anything now
246 pbautostart.checked = true;
247 controlChanged(win.document, pbautostart);
248 expect_disabled(true);
250 pbautostart.checked = false;
251 controlChanged(win.document, pbautostart);
252 expect_disabled(true);
254 acceptcookies.checked = true;
255 controlChanged(win.document, acceptcookies);
256 expect_disabled(false);
258 let accessthirdparty = controls.shift();
259 pbautostart.checked = true;
260 controlChanged(win.document, pbautostart);
261 expect_disabled(true);
262 ok(!accessthirdparty.disabled, "access third party button should be enabled");
264 acceptcookies.checked = false;
265 controlChanged(win.document, acceptcookies);
266 expect_disabled(true);
267 ok(accessthirdparty.disabled, "access third party button should be disabled");
269 pbautostart.checked = false;
270 controlChanged(win.document, pbautostart);
271 expect_disabled(true);
272 ok(accessthirdparty.disabled, "access third party button should be disabled");
274 acceptcookies.checked = true;
275 controlChanged(win.document, acceptcookies);
276 expect_disabled(false);
277 ok(!accessthirdparty.disabled, "access third party button should be enabled");
280 function test_dependent_clearonclose_elements(win) {
281 let historymode = win.document.getElementById("historyMode");
282 ok(historymode, "history mode menulist should exist");
283 let pbautostart = win.document.getElementById("privateBrowsingAutoStart");
284 ok(pbautostart, "the private browsing auto-start checkbox should exist");
285 let alwaysclear = win.document.getElementById("alwaysClear");
286 ok(alwaysclear, "the clear data on close checkbox should exist");
287 let alwaysclearsettings = win.document.getElementById("clearDataSettings");
288 ok(alwaysclearsettings, "the clear data settings button should exist");
290 function expect_disabled(disabled) {
291 is(alwaysclearsettings.disabled, disabled,
292 "the clear data settings should " + (disabled ? "" : "not ") + "be disabled");
295 historymode.value = "custom";
296 controlChanged(win.document, historymode);
297 pbautostart.checked = false;
298 controlChanged(win.document, pbautostart);
299 alwaysclear.checked = false;
300 controlChanged(win.document, alwaysclear);
301 expect_disabled(true);
303 alwaysclear.checked = true;
304 controlChanged(win.document, alwaysclear);
305 expect_disabled(false);
307 pbautostart.checked = true;
308 controlChanged(win.document, pbautostart);
309 expect_disabled(true);
311 pbautostart.checked = false;
312 controlChanged(win.document, pbautostart);
313 expect_disabled(false);
315 alwaysclear.checked = false;
316 controlChanged(win.document, alwaysclear);
317 expect_disabled(true);
320 function test_dependent_prefs(win) {
321 let historymode = win.document.getElementById("historyMode");
322 ok(historymode, "history mode menulist should exist");
324 win.document.getElementById("rememberHistoryDays"),
325 win.document.getElementById("rememberDownloads"),
326 win.document.getElementById("rememberForms"),
327 win.document.getElementById("acceptCookies"),
328 win.document.getElementById("acceptThirdParty"),
330 controls.forEach(function(control) {
331 ok(control, "the micro-management controls should exist");
334 function expect_checked(checked) {
335 controls.forEach(function(control) {
336 is(control.checked, checked,
337 control.getAttribute("id") + " should " + (checked ? "not " : "") + "be checked");
341 // controls should be checked in remember mode
342 historymode.value = "remember";
343 controlChanged(win.document, historymode);
344 expect_checked(true);
346 // even if they're unchecked in custom mode
347 historymode.value = "custom";
348 controlChanged(win.document, historymode);
349 controls.forEach(function(control) {
350 control.checked = false;
351 controlChanged(win.document, control);
353 expect_checked(false);
354 historymode.value = "remember";
355 controlChanged(win.document, historymode);
356 expect_checked(true);
359 function test_historymode_retention(mode, expect) {
360 return function(win) {
361 let historymode = win.document.getElementById("historyMode");
362 ok(historymode, "history mode menulist should exist");
364 if (expect !== undefined) {
365 is(historymode.value, expect,
366 "history mode is expected to remain " + expect);
369 historymode.value = mode;
370 controlChanged(win.document, historymode);
374 function test_custom_retention(controlToChange, expect, valueIncrement) {
375 return function(win) {
376 let historymode = win.document.getElementById("historyMode");
377 ok(historymode, "history mode menulist should exist");
379 if (expect !== undefined) {
380 is(historymode.value, expect,
381 "history mode is expected to remain " + expect);
384 historymode.value = "custom";
385 controlChanged(win.document, historymode);
387 controlToChange = win.document.getElementById(controlToChange);
388 ok(controlToChange, "the control to change should exist");
389 switch (controlToChange.localName) {
391 controlToChange.checked = !controlToChange.checked;
394 controlToChange.value = parseInt(controlToChange.value) + valueIncrement;
397 controlToChange.value = valueIncrement;
400 controlChanged(win.document, controlToChange);
404 function test_locbar_suggestion_retention(mode, expect) {
405 return function(win) {
406 let locbarsuggest = win.document.getElementById("locationBarSuggestion");
407 ok(locbarsuggest, "location bar suggestion menulist should exist");
409 if (expect !== undefined) {
410 is(locbarsuggest.value, expect,
411 "location bar suggestion is expected to remain " + expect);
414 locbarsuggest.value = mode;
415 controlChanged(win.document, locbarsuggest);
419 function test_privatebrowsing_toggle(win) {
420 let historymode = win.document.getElementById("historyMode");
421 ok(historymode, "history mode menulist should exist");
422 let pbautostart = win.document.getElementById("privateBrowsingAutoStart");
423 ok(pbautostart, "the private browsing auto-start checkbox should exist");
425 let pbService = Cc["@mozilla.org/privatebrowsing;1"].
426 getService(Ci.nsIPrivateBrowsingService);
429 historymode.value = "remember";
430 controlChanged(win.document, historymode);
432 // switch to dontremember mode
433 historymode.value = "dontremember";
434 controlChanged(win.document, historymode);
435 ok(pbService.privateBrowsingEnabled, "private browsing should be activated");
437 // switch to remember mode
438 historymode.value = "remember";
439 controlChanged(win.document, historymode);
440 ok(!pbService.privateBrowsingEnabled, "private browsing should be deactivated");
442 // switch to custom mode
443 historymode.value = "custom";
444 controlChanged(win.document, historymode);
445 ok(!pbService.privateBrowsingEnabled, "private browsing should remain deactivated");
447 // check the autostart checkbox
448 pbautostart.checked = true;
449 controlChanged(win.document, pbautostart);
450 ok(pbService.privateBrowsingEnabled, "private browsing should be activated");
452 // uncheck the autostart checkbox
453 pbautostart.checked = false;
454 controlChanged(win.document, pbautostart);
455 ok(!pbService.privateBrowsingEnabled, "private browsing should be deactivated");
458 function test_privatebrowsing_ui(win) {
459 let historymode = win.document.getElementById("historyMode");
460 ok(historymode, "history mode menulist should exist");
461 let pbautostart = win.document.getElementById("privateBrowsingAutoStart");
462 ok(pbautostart, "the private browsing auto-start checkbox should exist");
464 let pbmenuitem = document.getElementById("privateBrowsingItem");
465 ok(pbmenuitem, "the private browsing menu item should exist");
466 let pbcommand = document.getElementById("Tools:PrivateBrowsing");
467 ok(pbcommand, "the private browsing menu item should exist");
470 historymode.value = "remember";
471 controlChanged(win.document, historymode);
472 ok(!pbmenuitem.hasAttribute("disabled"),
473 "private browsing menu item should not be initially disabled");
474 ok(!pbcommand.hasAttribute("disabled"),
475 "private browsing command should not be initially disabled");
477 // switch to dontremember mode
478 historymode.value = "dontremember";
479 controlChanged(win.document, historymode);
480 ok(pbmenuitem.hasAttribute("disabled"),
481 "private browsing menu item should be disabled");
482 ok(pbcommand.hasAttribute("disabled"),
483 "private browsing command should be disabled");
485 // switch to remember mode
486 historymode.value = "remember";
487 controlChanged(win.document, historymode);
488 ok(!pbmenuitem.hasAttribute("disabled"),
489 "private browsing menu item should be enabled");
490 ok(!pbcommand.hasAttribute("disabled"),
491 "private browsing command should be enabled");
493 // switch to custom mode
494 historymode.value = "custom";
495 controlChanged(win.document, historymode);
496 ok(!pbmenuitem.hasAttribute("disabled"),
497 "private browsing menu item should remain enabled");
498 ok(!pbcommand.hasAttribute("disabled"),
499 "private browsing command should remain enabled");
501 // check the autostart checkbox
502 pbautostart.checked = true;
503 controlChanged(win.document, pbautostart);
504 ok(pbmenuitem.hasAttribute("disabled"),
505 "private browsing menu item should be disabled");
506 ok(pbcommand.hasAttribute("disabled"),
507 "private browsing command should be disabled");
509 // uncheck the autostart checkbox
510 pbautostart.checked = false;
511 controlChanged(win.document, pbautostart);
512 ok(!pbmenuitem.hasAttribute("disabled"),
513 "private browsing menu item should be enabled");
514 ok(!pbcommand.hasAttribute("disabled"),
515 "private browsing command should be enabled");
518 function enter_private_browsing(win) {
519 let pbService = Cc["@mozilla.org/privatebrowsing;1"].
520 getService(Ci.nsIPrivateBrowsingService);
521 win.document.getElementById("browser.privatebrowsing.keep_current_session")
523 pbService.privateBrowsingEnabled = true;
526 function reset_preferences(win) {
527 let prefs = win.document.getElementsByTagName("preference");
528 for (let i = 0; i < prefs.length; ++i)
529 if (prefs[i].hasUserValue)
535 let psvc = Cc["@mozilla.org/preferences-service;1"].
536 getService(Ci.nsIPrefBranch);
537 let instantApplyOrig = psvc.getBoolPref("browser.preferences.instantApply");
538 psvc.setBoolPref("browser.preferences.instantApply", true);
540 waitForExplicitFinish();
544 test_locbar_emptyText,
545 test_pane_visibility,
546 test_dependent_elements,
547 test_dependent_cookie_elements,
548 test_dependent_clearonclose_elements,
549 test_dependent_prefs,
550 test_historymode_retention("remember", undefined),
551 test_historymode_retention("dontremember", "remember"),
552 test_historymode_retention("custom", "dontremember"),
553 // custom without any micro-prefs changed won't retain
554 test_historymode_retention("remember", "dontremember"),
555 test_historymode_retention("custom", "remember"),
556 // custom without any micro-prefs changed won't retain
557 test_historymode_retention("remember", "remember"),
558 test_custom_retention("rememberHistoryDays", "remember"),
559 test_custom_retention("rememberHistoryDays", "custom"),
560 test_custom_retention("historyDays", "remember", 1),
561 test_custom_retention("historyDays", "custom", -1),
562 test_custom_retention("rememberDownloads", "remember"),
563 test_custom_retention("rememberDownloads", "custom"),
564 test_custom_retention("rememberForms", "remember"),
565 test_custom_retention("rememberForms", "custom"),
566 test_custom_retention("acceptCookies", "remember"),
567 test_custom_retention("acceptCookies", "custom"),
568 test_custom_retention("acceptThirdParty", "remember"),
569 test_custom_retention("acceptThirdParty", "custom"),
570 test_custom_retention("keepCookiesUntil", "remember", 1),
571 test_custom_retention("keepCookiesUntil", "custom", 2),
572 test_custom_retention("keepCookiesUntil", "custom", 0),
573 test_custom_retention("alwaysClear", "remember"),
574 test_custom_retention("alwaysClear", "custom"),
575 test_historymode_retention("remember", "remember"),
576 test_locbar_suggestion_retention(-1, undefined),
577 test_locbar_suggestion_retention(1, -1),
578 test_locbar_suggestion_retention(2, 1),
579 test_locbar_suggestion_retention(0, 2),
580 test_locbar_suggestion_retention(0, 0),
581 test_privatebrowsing_toggle,
582 enter_private_browsing, // once again, test with PB initially enabled
583 test_privatebrowsing_toggle,
584 test_privatebrowsing_ui,
585 enter_private_browsing, // once again, test with PB initially enabled
586 test_privatebrowsing_ui,
588 // reset all preferences to their default values once we're done
592 runNext: function() {
593 if (this.counter == this.tests.length) {
595 psvc.setBoolPref("browser.preferences.instantApply", instantApplyOrig);
599 setTimeout(function() {
600 runTestOnPrivacyPrefPane(self.tests[self.counter++]);
606 testRunner.runNext();