Bug 616542 - Shorten file path length of mochitest; r=ted
[gecko.git] / browser / components / preferences / tests / privacypane_tests.js
blob8a439dd9cd27675472abc56aadceec212a4b0df6
1 /* ***** BEGIN LICENSE BLOCK *****
2  *   Version: MPL 1.1/GPL 2.0/LGPL 2.1
3  *
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/
8  * 
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
12  * License.
13  *
14  * The Original Code is Privacy PrefPane Test.
15  *
16  * The Initial Developer of the Original Code is
17  * Ehsan Akhgari.
18  * Portions created by the Initial Developer are Copyright (C) 2009
19  * the Initial Developer. All Rights Reserved.
20  *
21  * Contributor(s):
22  *   Ehsan Akhgari <ehsan.akhgari@gmail.com> (Original Author)
23  *
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.
35  * 
36  * ***** END LICENSE BLOCK ***** */
38 function runTestOnPrivacyPrefPane(testFunc) {
39   let observer = {
40     observe: function(aSubject, aTopic, aData) {
41       if (aTopic == "domwindowopened") {
42         Services.ww.unregisterNotification(this);
44         let win = aSubject.QueryInterface(Ci.nsIDOMEventTarget);
45         win.addEventListener("load", function() {
46           win.removeEventListener("load", arguments.callee, false);
47           testFunc(dialog.document.defaultView);
49           Services.ww.registerNotification(observer);
50           dialog.close();
51         }, false);
52       } else if (aTopic == "domwindowclosed") {
53         Services.ww.unregisterNotification(this);
54         testRunner.runNext();
55       }
56     }
57   };
58   Services.ww.registerNotification(observer);
60   let dialog = openDialog("chrome://browser/content/preferences/preferences.xul", "Preferences",
61                           "chrome,titlebar,toolbar,centerscreen,dialog=no", "panePrivacy");
64 function controlChanged(element) {
65   element.doCommand();
68 function test_pane_visibility(win) {
69   let modes = {
70     "remember": "historyRememberPane",
71     "dontremember": "historyDontRememberPane",
72     "custom": "historyCustomPane"
73   };
75   let historymode = win.document.getElementById("historyMode");
76   ok(historymode, "history mode menulist should exist");
77   let historypane = win.document.getElementById("historyPane");
78   ok(historypane, "history mode pane should exist");
80   for (let mode in modes) {
81     historymode.value = mode;
82     controlChanged(historymode);
83     is(historypane.selectedPanel, win.document.getElementById(modes[mode]),
84       "The correct pane should be selected for the " + mode + " mode");
85   }
88 function test_dependent_elements(win) {
89   let historymode = win.document.getElementById("historyMode");
90   ok(historymode, "history mode menulist should exist");
91   let pbautostart = win.document.getElementById("privateBrowsingAutoStart");
92   ok(pbautostart, "the private browsing auto-start checkbox should exist");
93   let controls = [
94     win.document.getElementById("rememberHistory"),
95     win.document.getElementById("rememberDownloads"),
96     win.document.getElementById("rememberForms"),
97     win.document.getElementById("keepUntil"),
98     win.document.getElementById("keepCookiesUntil"),
99     win.document.getElementById("alwaysClear"),
100   ];
101   controls.forEach(function(control) {
102     ok(control, "the dependent controls should exist");
103   });
104   let independents = [
105     win.document.getElementById("acceptCookies"),
106     win.document.getElementById("acceptThirdParty"),
107   ];
108   independents.forEach(function(control) {
109     ok(control, "the independent controls should exist");
110   });
111   let cookieexceptions = win.document.getElementById("cookieExceptions");
112   ok(cookieexceptions, "the cookie exceptions button should exist");
113   let keepuntil = win.document.getElementById("keepCookiesUntil");
114   ok(keepuntil, "the keep cookies until menulist should exist");
115   let alwaysclear = win.document.getElementById("alwaysClear");
116   ok(alwaysclear, "the clear data on close checkbox should exist");
117   let rememberhistory = win.document.getElementById("rememberHistory");
118   ok(rememberhistory, "the remember history checkbox should exist");
119   let rememberdownloads = win.document.getElementById("rememberDownloads");
120   ok(rememberdownloads, "the remember downloads checkbox should exist");
121   let rememberforms = win.document.getElementById("rememberForms");
122   ok(rememberforms, "the remember forms checkbox should exist");
123   let alwaysclearsettings = win.document.getElementById("clearDataSettings");
124   ok(alwaysclearsettings, "the clear data settings button should exist");
126   function expect_disabled(disabled) {
127     controls.forEach(function(control) {
128       is(control.disabled, disabled,
129         control.getAttribute("id") + " should " + (disabled ? "" : "not ") + "be disabled");
130     });
131     is(keepuntil.value, disabled ? 2 : 0,
132       "the keep cookies until menulist value should be as expected");
133     if (disabled) {
134      ok(!alwaysclear.checked,
135         "the clear data on close checkbox value should be as expected");
136      ok(!rememberhistory.checked,
137         "the remember history checkbox value should be as expected");
138      ok(!rememberdownloads.checked,
139         "the remember downloads checkbox value should be as expected");
140      ok(!rememberforms.checked,
141         "the remember forms checkbox value should be as expected");
142     }
143   }
144   function check_independents(expected) {
145     independents.forEach(function(control) {
146       is(control.disabled, expected,
147         control.getAttribute("id") + " should " + (expected ? "" : "not ") + "be disabled");
148     });
149     ok(!cookieexceptions.disabled,
150       "the cookie exceptions button should never be disabled");
151     ok(alwaysclearsettings.disabled,
152       "the clear data settings button should always be disabled");
153   }
155   // controls should only change in custom mode
156   historymode.value = "remember";
157   controlChanged(historymode);
158   expect_disabled(false);
159   check_independents(false);
161   // setting the mode to custom shouldn't change anything
162   historymode.value = "custom";
163   controlChanged(historymode);
164   expect_disabled(false);
165   check_independents(false);
167   // controls should only change in custom mode
168   historymode.value = "dontremember";
169   controlChanged(historymode);
170   expect_disabled(false);
171   check_independents(false);
173   // controls should only change in custom mode
174   historymode.value = "custom";
175   controlChanged(historymode);
176   expect_disabled(true);
177   check_independents(false);
179   // dependent controls should follow pbautostart
180   pbautostart.checked = false;
181   controlChanged(pbautostart);
182   expect_disabled(false);
183   check_independents(false);
185   // dependent controls should follow pbautostart
186   pbautostart.checked = true;
187   controlChanged(pbautostart);
188   expect_disabled(true);
189   check_independents(false);
192 function test_dependent_cookie_elements(win) {
193   let historymode = win.document.getElementById("historyMode");
194   ok(historymode, "history mode menulist should exist");
195   let pbautostart = win.document.getElementById("privateBrowsingAutoStart");
196   ok(pbautostart, "the private browsing auto-start checkbox should exist");
197   let controls = [
198     win.document.getElementById("acceptThirdParty"),
199     win.document.getElementById("keepUntil"),
200     win.document.getElementById("keepCookiesUntil"),
201   ];
202   controls.forEach(function(control) {
203     ok(control, "the dependent cookie controls should exist");
204   });
205   let acceptcookies = win.document.getElementById("acceptCookies");
206   ok(acceptcookies, "the accept cookies checkbox should exist");
208   function expect_disabled(disabled) {
209     controls.forEach(function(control) {
210       is(control.disabled, disabled,
211         control.getAttribute("id") + " should " + (disabled ? "" : "not ") + "be disabled");
212     });
213   }
215   historymode.value = "custom";
216   controlChanged(historymode);
217   pbautostart.checked = false;
218   controlChanged(pbautostart);
219   expect_disabled(false);
221   acceptcookies.checked = false;
222   controlChanged(acceptcookies);
223   expect_disabled(true);
225   // pbautostart shouldn't change anything now
226   pbautostart.checked = true;
227   controlChanged(pbautostart);
228   expect_disabled(true);
230   pbautostart.checked = false;
231   controlChanged(pbautostart);
232   expect_disabled(true);
234   acceptcookies.checked = true;
235   controlChanged(acceptcookies);
236   expect_disabled(false);
238   let accessthirdparty = controls.shift();
239   pbautostart.checked = true;
240   controlChanged(pbautostart);
241   expect_disabled(true);
242   ok(!accessthirdparty.disabled, "access third party button should be enabled");
244   acceptcookies.checked = false;
245   controlChanged(acceptcookies);
246   expect_disabled(true);
247   ok(accessthirdparty.disabled, "access third party button should be disabled");
249   pbautostart.checked = false;
250   controlChanged(pbautostart);
251   expect_disabled(true);
252   ok(accessthirdparty.disabled, "access third party button should be disabled");
254   acceptcookies.checked = true;
255   controlChanged(acceptcookies);
256   expect_disabled(false);
257   ok(!accessthirdparty.disabled, "access third party button should be enabled");
260 function test_dependent_clearonclose_elements(win) {
261   let historymode = win.document.getElementById("historyMode");
262   ok(historymode, "history mode menulist should exist");
263   let pbautostart = win.document.getElementById("privateBrowsingAutoStart");
264   ok(pbautostart, "the private browsing auto-start checkbox should exist");
265   let alwaysclear = win.document.getElementById("alwaysClear");
266   ok(alwaysclear, "the clear data on close checkbox should exist");
267   let alwaysclearsettings = win.document.getElementById("clearDataSettings");
268   ok(alwaysclearsettings, "the clear data settings button should exist");
270   function expect_disabled(disabled) {
271     is(alwaysclearsettings.disabled, disabled,
272       "the clear data settings should " + (disabled ? "" : "not ") + "be disabled");
273   }
275   historymode.value = "custom";
276   controlChanged(historymode);
277   pbautostart.checked = false;
278   controlChanged(pbautostart);
279   alwaysclear.checked = false;
280   controlChanged(alwaysclear);
281   expect_disabled(true);
283   alwaysclear.checked = true;
284   controlChanged(alwaysclear);
285   expect_disabled(false);
287   pbautostart.checked = true;
288   controlChanged(pbautostart);
289   expect_disabled(true);
291   pbautostart.checked = false;
292   controlChanged(pbautostart);
293   expect_disabled(false);
295   alwaysclear.checked = false;
296   controlChanged(alwaysclear);
297   expect_disabled(true);
300 function test_dependent_prefs(win) {
301   let historymode = win.document.getElementById("historyMode");
302   ok(historymode, "history mode menulist should exist");
303   let controls = [
304     win.document.getElementById("rememberHistory"),
305     win.document.getElementById("rememberDownloads"),
306     win.document.getElementById("rememberForms"),
307     win.document.getElementById("acceptCookies"),
308     win.document.getElementById("acceptThirdParty"),
309   ];
310   controls.forEach(function(control) {
311     ok(control, "the micro-management controls should exist");
312   });
314   function expect_checked(checked) {
315     controls.forEach(function(control) {
316       is(control.checked, checked,
317         control.getAttribute("id") + " should " + (checked ? "not " : "") + "be checked");
318     });
319   }
321   // controls should be checked in remember mode
322   historymode.value = "remember";
323   controlChanged(historymode);
324   expect_checked(true);
326   // even if they're unchecked in custom mode
327   historymode.value = "custom";
328   controlChanged(historymode);
329   controls.forEach(function(control) {
330     control.checked = false;
331     controlChanged(control);
332   });
333   expect_checked(false);
334   historymode.value = "remember";
335   controlChanged(historymode);
336   expect_checked(true);
339 function test_historymode_retention(mode, expect) {
340   return function(win) {
341     let historymode = win.document.getElementById("historyMode");
342     ok(historymode, "history mode menulist should exist");
344     if (expect !== undefined) {
345       is(historymode.value, expect,
346         "history mode is expected to remain " + expect);
347     }
349     historymode.value = mode;
350     controlChanged(historymode);
351   };
354 function test_custom_retention(controlToChange, expect, valueIncrement) {
355   return function(win) {
356     let historymode = win.document.getElementById("historyMode");
357     ok(historymode, "history mode menulist should exist");
359     if (expect !== undefined) {
360       is(historymode.value, expect,
361         "history mode is expected to remain " + expect);
362     }
364     historymode.value = "custom";
365     controlChanged(historymode);
367     controlToChange = win.document.getElementById(controlToChange);
368     ok(controlToChange, "the control to change should exist");
369     switch (controlToChange.localName) {
370     case "checkbox":
371       controlToChange.checked = !controlToChange.checked;
372       break;
373     case "textbox":
374       controlToChange.value = parseInt(controlToChange.value) + valueIncrement;
375       break;
376     case "menulist":
377       controlToChange.value = valueIncrement;
378       break;
379     }
380     controlChanged(controlToChange);
381   };
384 function test_locbar_suggestion_retention(mode, expect) {
385   return function(win) {
386     let locbarsuggest = win.document.getElementById("locationBarSuggestion");
387     ok(locbarsuggest, "location bar suggestion menulist should exist");
389     if (expect !== undefined) {
390       is(locbarsuggest.value, expect,
391         "location bar suggestion is expected to remain " + expect);
392     }
394     locbarsuggest.value = mode;
395     controlChanged(locbarsuggest);
396   };
399 function test_privatebrowsing_toggle(win) {
400   let historymode = win.document.getElementById("historyMode");
401   ok(historymode, "history mode menulist should exist");
402   let pbautostart = win.document.getElementById("privateBrowsingAutoStart");
403   ok(pbautostart, "the private browsing auto-start checkbox should exist");
405   let pbService = Cc["@mozilla.org/privatebrowsing;1"].
406                   getService(Ci.nsIPrivateBrowsingService);
408   // initial state
409   historymode.value = "remember";
410   controlChanged(historymode);
412   // switch to dontremember mode
413   historymode.value = "dontremember";
414   controlChanged(historymode);
415   ok(pbService.privateBrowsingEnabled, "private browsing should be activated");
417   // switch to remember mode
418   historymode.value = "remember";
419   controlChanged(historymode);
420   ok(!pbService.privateBrowsingEnabled, "private browsing should be deactivated");
422   // switch to custom mode
423   historymode.value = "custom";
424   controlChanged(historymode);
425   ok(!pbService.privateBrowsingEnabled, "private browsing should remain deactivated");
427   // check the autostart checkbox
428   pbautostart.checked = true;
429   controlChanged(pbautostart);
430   ok(pbService.privateBrowsingEnabled, "private browsing should be activated");
432   // uncheck the autostart checkbox
433   pbautostart.checked = false;
434   controlChanged(pbautostart);
435   ok(!pbService.privateBrowsingEnabled, "private browsing should be deactivated");
438 function test_privatebrowsing_ui(win) {
439   let historymode = win.document.getElementById("historyMode");
440   ok(historymode, "history mode menulist should exist");
441   let pbautostart = win.document.getElementById("privateBrowsingAutoStart");
442   ok(pbautostart, "the private browsing auto-start checkbox should exist");
444   let pbmenuitem = document.getElementById("privateBrowsingItem");
445   ok(pbmenuitem, "the private browsing menu item should exist");
446   let pbcommand = document.getElementById("Tools:PrivateBrowsing");
447   ok(pbcommand, "the private browsing command should exist");
449   // initial state
450   historymode.value = "remember";
451   controlChanged(historymode);
452   ok(!pbmenuitem.hasAttribute("disabled"),
453     "private browsing menu item should not be initially disabled");
454   ok(!pbcommand.hasAttribute("disabled"),
455     "private browsing command should not be initially disabled");
457   // switch to dontremember mode
458   historymode.value = "dontremember";
459   controlChanged(historymode);
460   ok(pbmenuitem.hasAttribute("disabled"),
461     "private browsing menu item should be disabled");
462   ok(pbcommand.hasAttribute("disabled"),
463     "private browsing command should be disabled");
465   // switch to remember mode
466   historymode.value = "remember";
467   controlChanged(historymode);
468   ok(!pbmenuitem.hasAttribute("disabled"),
469     "private browsing menu item should be enabled");
470   ok(!pbcommand.hasAttribute("disabled"),
471     "private browsing command should be enabled");
473   // switch to custom mode
474   historymode.value = "custom";
475   controlChanged(historymode);
476   ok(!pbmenuitem.hasAttribute("disabled"),
477     "private browsing menu item should remain enabled");
478   ok(!pbcommand.hasAttribute("disabled"),
479     "private browsing command should remain enabled");
481   // check the autostart checkbox
482   pbautostart.checked = true;
483   controlChanged(pbautostart);
484   ok(pbmenuitem.hasAttribute("disabled"),
485     "private browsing menu item should be disabled");
486   ok(pbcommand.hasAttribute("disabled"),
487     "private browsing command should be disabled");
489   // uncheck the autostart checkbox
490   pbautostart.checked = false;
491   controlChanged(pbautostart);
492   ok(!pbmenuitem.hasAttribute("disabled"),
493     "private browsing menu item should be enabled");
494   ok(!pbcommand.hasAttribute("disabled"),
495     "private browsing command should be enabled");
498 function enter_private_browsing(win) {
499   let pbService = Cc["@mozilla.org/privatebrowsing;1"].
500                   getService(Ci.nsIPrivateBrowsingService);
501   win.document.getElementById("browser.privatebrowsing.keep_current_session")
502               .value = true;
503   pbService.privateBrowsingEnabled = true;
506 function reset_preferences(win) {
507   let prefs = win.document.getElementsByTagName("preference");
508   for (let i = 0; i < prefs.length; ++i)
509     if (prefs[i].hasUserValue)
510       prefs[i].reset();
513 let testRunner;
514 function run_test_subset(subset) {
515   let instantApplyOrig = Services.prefs.getBoolPref("browser.preferences.instantApply");
516   Services.prefs.setBoolPref("browser.preferences.instantApply", true);
518   waitForExplicitFinish();
520   testRunner = {
521     tests: subset,
522     counter: 0,
523     runNext: function() {
524       if (this.counter == this.tests.length) {
525         // cleanup
526         Services.prefs.setBoolPref("browser.preferences.instantApply", instantApplyOrig);
527         finish();
528       } else {
529         let self = this;
530         setTimeout(function() {
531           runTestOnPrivacyPrefPane(self.tests[self.counter++]);
532         }, 0);
533       }
534     }
535   };
537   testRunner.runNext();