privacypane_tests.js
Go to the documentation of this file.
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 ***** */
37 
38 function runTestOnPrivacyPrefPane(testFunc) {
39  let ww = Cc["@mozilla.org/embedcomp/window-watcher;1"].
40  getService(Ci.nsIWindowWatcher);
41  let observer = {
42  observe: function(aSubject, aTopic, aData) {
43  if (aTopic == "domwindowopened") {
44  ww.unregisterNotification(this);
45 
46  let win = aSubject.QueryInterface(Ci.nsIDOMEventTarget);
47  win.addEventListener("load", function() {
48  win.removeEventListener("load", arguments.callee, false);
49  testFunc(dialog.document.defaultView);
50 
51  ww.registerNotification(observer);
52  dialog.close();
53  }, false);
54  } else if (aTopic == "domwindowclosed") {
55  ww.unregisterNotification(this);
56  testRunner.runNext();
57  }
58  }
59  };
60  ww.registerNotification(observer);
61 
62  let dialog = openDialog("chrome://browser/content/preferences/preferences.xul", "Preferences",
63  "chrome,titlebar,toolbar,centerscreen,dialog=no", "panePrivacy");
64 }
65 
67  let event = doc.createEvent("Events");
68  event.initEvent("command", true, true);
69  element.dispatchEvent(event);
70 }
71 
73  let texts = ["none", "bookmarkhistory", "history", "bookmark"];
74 
75  let locbarlist = win.document.getElementById("locationBarSuggestion");
76  ok(locbarlist, "location bar suggestion menulist should exist");
77 
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");
83  }
84 }
85 
87  let modes = {
88  "remember": "historyRememberPane",
89  "dontremember": "historyDontRememberPane",
90  "custom": "historyCustomPane"
91  };
92 
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");
97 
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");
103  }
104 }
105 
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");
111  let controls = [
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"),
120  ];
121  controls.forEach(function(control) {
122  ok(control, "the dependent controls should exist");
123  });
124  let independents = [
125  win.document.getElementById("acceptCookies"),
126  win.document.getElementById("acceptThirdParty"),
127  ];
128  independents.forEach(function(control) {
129  ok(control, "the independent controls should exist");
130  });
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");
145 
146  function expect_disabled(disabled) {
147  controls.forEach(function(control) {
148  is(control.disabled, disabled,
149  control.getAttribute("id") + " should " + (disabled ? "" : "not ") + "be disabled");
150  });
151  is(keepuntil.value, disabled ? 2 : 0,
152  "the keep cookies until menulist value should be as expected");
153  if (disabled) {
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");
162  }
163  }
164  function check_independents(expected) {
165  independents.forEach(function(control) {
166  is(control.disabled, expected,
167  control.getAttribute("id") + " should " + (expected ? "" : "not ") + "be disabled");
168  });
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");
173  }
174 
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);
180 
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);
186 
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);
192 
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);
198 
199  // dependent controls should follow pbautostart
200  pbautostart.checked = false;
201  controlChanged(win.document, pbautostart);
202  expect_disabled(false);
203  check_independents(false);
204 
205  // dependent controls should follow pbautostart
206  pbautostart.checked = true;
207  controlChanged(win.document, pbautostart);
208  expect_disabled(true);
209  check_independents(false);
210 }
211 
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");
217  let controls = [
218  win.document.getElementById("acceptThirdParty"),
219  win.document.getElementById("keepUntil"),
220  win.document.getElementById("keepCookiesUntil"),
221  ];
222  controls.forEach(function(control) {
223  ok(control, "the dependent cookie controls should exist");
224  });
225  let acceptcookies = win.document.getElementById("acceptCookies");
226  ok(acceptcookies, "the accept cookies checkbox should exist");
227 
228  function expect_disabled(disabled) {
229  controls.forEach(function(control) {
230  is(control.disabled, disabled,
231  control.getAttribute("id") + " should " + (disabled ? "" : "not ") + "be disabled");
232  });
233  }
234 
235  historymode.value = "custom";
236  controlChanged(win.document, historymode);
237  pbautostart.checked = false;
238  controlChanged(win.document, pbautostart);
239  expect_disabled(false);
240 
241  acceptcookies.checked = false;
242  controlChanged(win.document, acceptcookies);
243  expect_disabled(true);
244 
245  // pbautostart shouldn't change anything now
246  pbautostart.checked = true;
247  controlChanged(win.document, pbautostart);
248  expect_disabled(true);
249 
250  pbautostart.checked = false;
251  controlChanged(win.document, pbautostart);
252  expect_disabled(true);
253 
254  acceptcookies.checked = true;
255  controlChanged(win.document, acceptcookies);
256  expect_disabled(false);
257 
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");
263 
264  acceptcookies.checked = false;
265  controlChanged(win.document, acceptcookies);
266  expect_disabled(true);
267  ok(accessthirdparty.disabled, "access third party button should be disabled");
268 
269  pbautostart.checked = false;
270  controlChanged(win.document, pbautostart);
271  expect_disabled(true);
272  ok(accessthirdparty.disabled, "access third party button should be disabled");
273 
274  acceptcookies.checked = true;
275  controlChanged(win.document, acceptcookies);
276  expect_disabled(false);
277  ok(!accessthirdparty.disabled, "access third party button should be enabled");
278 }
279 
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");
289 
290  function expect_disabled(disabled) {
291  is(alwaysclearsettings.disabled, disabled,
292  "the clear data settings should " + (disabled ? "" : "not ") + "be disabled");
293  }
294 
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);
302 
303  alwaysclear.checked = true;
304  controlChanged(win.document, alwaysclear);
305  expect_disabled(false);
306 
307  pbautostart.checked = true;
308  controlChanged(win.document, pbautostart);
309  expect_disabled(true);
310 
311  pbautostart.checked = false;
312  controlChanged(win.document, pbautostart);
313  expect_disabled(false);
314 
315  alwaysclear.checked = false;
316  controlChanged(win.document, alwaysclear);
317  expect_disabled(true);
318 }
319 
321  let historymode = win.document.getElementById("historyMode");
322  ok(historymode, "history mode menulist should exist");
323  let controls = [
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"),
329  ];
330  controls.forEach(function(control) {
331  ok(control, "the micro-management controls should exist");
332  });
333 
334  function expect_checked(checked) {
335  controls.forEach(function(control) {
336  is(control.checked, checked,
337  control.getAttribute("id") + " should " + (checked ? "not " : "") + "be checked");
338  });
339  }
340 
341  // controls should be checked in remember mode
342  historymode.value = "remember";
343  controlChanged(win.document, historymode);
344  expect_checked(true);
345 
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);
352  });
353  expect_checked(false);
354  historymode.value = "remember";
355  controlChanged(win.document, historymode);
356  expect_checked(true);
357 }
358 
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");
363 
364  if (expect !== undefined) {
365  is(historymode.value, expect,
366  "history mode is expected to remain " + expect);
367  }
368 
369  historymode.value = mode;
370  controlChanged(win.document, historymode);
371  };
372 }
373 
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");
378 
379  if (expect !== undefined) {
380  is(historymode.value, expect,
381  "history mode is expected to remain " + expect);
382  }
383 
384  historymode.value = "custom";
385  controlChanged(win.document, historymode);
386 
387  controlToChange = win.document.getElementById(controlToChange);
388  ok(controlToChange, "the control to change should exist");
389  switch (controlToChange.localName) {
390  case "checkbox":
391  controlToChange.checked = !controlToChange.checked;
392  break;
393  case "textbox":
394  controlToChange.value = parseInt(controlToChange.value) + valueIncrement;
395  break;
396  case "menulist":
397  controlToChange.value = valueIncrement;
398  break;
399  }
400  controlChanged(win.document, controlToChange);
401  };
402 }
403 
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");
408 
409  if (expect !== undefined) {
410  is(locbarsuggest.value, expect,
411  "location bar suggestion is expected to remain " + expect);
412  }
413 
414  locbarsuggest.value = mode;
415  controlChanged(win.document, locbarsuggest);
416  };
417 }
418 
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");
424 
425  let pbService = Cc["@mozilla.org/privatebrowsing;1"].
426  getService(Ci.nsIPrivateBrowsingService);
427 
428  // initial state
429  historymode.value = "remember";
430  controlChanged(win.document, historymode);
431 
432  // switch to dontremember mode
433  historymode.value = "dontremember";
434  controlChanged(win.document, historymode);
435  ok(pbService.privateBrowsingEnabled, "private browsing should be activated");
436 
437  // switch to remember mode
438  historymode.value = "remember";
439  controlChanged(win.document, historymode);
440  ok(!pbService.privateBrowsingEnabled, "private browsing should be deactivated");
441 
442  // switch to custom mode
443  historymode.value = "custom";
444  controlChanged(win.document, historymode);
445  ok(!pbService.privateBrowsingEnabled, "private browsing should remain deactivated");
446 
447  // check the autostart checkbox
448  pbautostart.checked = true;
449  controlChanged(win.document, pbautostart);
450  ok(pbService.privateBrowsingEnabled, "private browsing should be activated");
451 
452  // uncheck the autostart checkbox
453  pbautostart.checked = false;
454  controlChanged(win.document, pbautostart);
455  ok(!pbService.privateBrowsingEnabled, "private browsing should be deactivated");
456 }
457 
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");
463 
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 command should exist");
468 
469  // initial state
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");
476 
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");
484 
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");
492 
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");
500 
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");
508 
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");
516 }
517 
519  let pbService = Cc["@mozilla.org/privatebrowsing;1"].
520  getService(Ci.nsIPrivateBrowsingService);
521  win.document.getElementById("browser.privatebrowsing.keep_current_session")
522  .value = true;
523  pbService.privateBrowsingEnabled = true;
524 }
525 
527  let prefs = win.document.getElementsByTagName("preference");
528  for (let i = 0; i < prefs.length; ++i)
529  if (prefs[i].hasUserValue)
530  prefs[i].reset();
531 }
532 
534 function run_test_subset(subset) {
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);
539 
541 
542  testRunner = {
543  tests: subset,
544  counter: 0,
545  runNext: function() {
546  if (this.counter == this.tests.length) {
547  // cleanup
548  psvc.setBoolPref("browser.preferences.instantApply", instantApplyOrig);
549  finish();
550  } else {
551  let self = this;
552  setTimeout(function() {
553  runTestOnPrivacyPrefPane(self.tests[self.counter++]);
554  }, 0);
555  }
556  }
557  };
558 
559  testRunner.runNext();
560 }
function test_dependent_cookie_elements(win)
function controlChanged(doc, element)
const Cc
function test_privatebrowsing_ui(win)
function doc() browser.contentDocument
function test_locbar_emptyText(win)
var event
function test_dependent_clearonclose_elements(win)
function test_locbar_suggestion_retention(mode, expect)
function test_pane_visibility(win)
function test_custom_retention(controlToChange, expect, valueIncrement)
getService(Ci.sbIFaceplateManager)
function enter_private_browsing(win)
var dialog
Definition: openLocation.js:43
function runTestOnPrivacyPrefPane(testFunc)
function test_dependent_elements(win)
let testRunner
waitForExplicitFinish()
aWindow setTimeout(function(){_this.restoreHistory(aWindow, aTabs, aTabData, aIdMap);}, 0)
function test_dependent_prefs(win)
function test_historymode_retention(mode, expect)
function test_privatebrowsing_toggle(win)
var expected
return!aWindow arguments!aWindow arguments[0]
var prefs
Definition: FeedWriter.js:1169
const Ci
function reset_preferences(win)
function run_test_subset(subset)
_getSelectedPageStyle s i
let observer
_updateTextAndScrollDataForFrame aData
sbDeviceFirmwareAutoCheckForUpdate prototype observe