test-private-browsing.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  4. 'use strict';
  5. const { Ci } = require('chrome');
  6. const { safeMerge } = require('sdk/util/object');
  7. const windows = require('sdk/windows').browserWindows;
  8. const tabs = require('sdk/tabs');
  9. const winUtils = require('sdk/window/utils');
  10. const { isWindowPrivate } = winUtils;
  11. const { isPrivateBrowsingSupported } = require('sdk/self');
  12. const { is } = require('sdk/system/xul-app');
  13. const { isPrivate } = require('sdk/private-browsing');
  14. const { getOwnerWindow } = require('sdk/private-browsing/window/utils');
  15. const { LoaderWithHookedConsole } = require("sdk/test/loader");
  16. const { getMode, isGlobalPBSupported,
  17. isWindowPBSupported, isTabPBSupported } = require('sdk/private-browsing/utils');
  18. const { pb } = require('./private-browsing/helper');
  19. const prefs = require('sdk/preferences/service');
  20. const { set: setPref } = require("sdk/preferences/service");
  21. const DEPRECATE_PREF = "devtools.errorconsole.deprecation_warnings";
  22. const kAutoStartPref = "browser.privatebrowsing.autostart";
  23. // is global pb is enabled?
  24. if (isGlobalPBSupported) {
  25. safeMerge(module.exports, require('./private-browsing/global'));
  26. exports.testGlobalOnlyOnFirefox = function(assert) {
  27. assert.ok(is("Firefox"), "isGlobalPBSupported is only true on Firefox");
  28. }
  29. }
  30. else if (isWindowPBSupported) {
  31. safeMerge(module.exports, require('./private-browsing/windows'));
  32. exports.testPWOnlyOnFirefox = function(assert) {
  33. assert.ok(is("Firefox"), "isWindowPBSupported is only true on Firefox");
  34. }
  35. }
  36. // only on Fennec
  37. else if (isTabPBSupported) {
  38. safeMerge(module.exports, require('./private-browsing/tabs'));
  39. exports.testPTOnlyOnFennec = function(assert) {
  40. assert.ok(is("Fennec"), "isTabPBSupported is only true on Fennec");
  41. }
  42. }
  43. exports.testIsPrivateDefaults = function(assert) {
  44. assert.equal(isPrivate(), false, 'undefined is not private');
  45. assert.equal(isPrivate('test'), false, 'strings are not private');
  46. assert.equal(isPrivate({}), false, 'random objects are not private');
  47. assert.equal(isPrivate(4), false, 'numbers are not private');
  48. assert.equal(isPrivate(/abc/), false, 'regex are not private');
  49. assert.equal(isPrivate(function() {}), false, 'functions are not private');
  50. };
  51. exports.testWindowDefaults = function(assert) {
  52. setPref(DEPRECATE_PREF, true);
  53. // Ensure that browserWindow still works while being deprecated
  54. let { loader, messages } = LoaderWithHookedConsole(module);
  55. let windows = loader.require("sdk/windows").browserWindows;
  56. assert.equal(windows.activeWindow.isPrivateBrowsing, false,
  57. 'window is not private browsing by default');
  58. assert.ok(/DEPRECATED.+isPrivateBrowsing/.test(messages[0].msg),
  59. 'isPrivateBrowsing is deprecated');
  60. let chromeWin = winUtils.getMostRecentBrowserWindow();
  61. assert.equal(getMode(chromeWin), false);
  62. assert.equal(isWindowPrivate(chromeWin), false);
  63. };
  64. // tests for the case where private browsing doesn't exist
  65. exports.testIsActiveDefault = function(assert) {
  66. assert.equal(pb.isActive, false,
  67. 'pb.isActive returns false when private browsing isn\'t supported');
  68. };
  69. exports.testIsPrivateBrowsingFalseDefault = function(assert) {
  70. assert.equal(isPrivateBrowsingSupported, false,
  71. 'isPrivateBrowsingSupported property is false by default');
  72. };
  73. exports.testGetOwnerWindow = function(assert, done) {
  74. let window = windows.activeWindow;
  75. let chromeWindow = getOwnerWindow(window);
  76. assert.ok(chromeWindow instanceof Ci.nsIDOMWindow, 'associated window is found');
  77. tabs.open({
  78. url: 'about:blank',
  79. isPrivate: true,
  80. onOpen: function(tab) {
  81. // test that getOwnerWindow works as expected
  82. if (is('Fennec')) {
  83. assert.notStrictEqual(chromeWindow, getOwnerWindow(tab));
  84. assert.ok(getOwnerWindow(tab) instanceof Ci.nsIDOMWindow);
  85. }
  86. else {
  87. assert.strictEqual(chromeWindow, getOwnerWindow(tab), 'associated window is the same for window and window\'s tab');
  88. }
  89. // test that the tab is not private
  90. // private flag should be ignored by default
  91. assert.ok(!isPrivate(tab));
  92. assert.ok(!isPrivate(getOwnerWindow(tab)));
  93. tab.close(done);
  94. }
  95. });
  96. };
  97. exports.testNewGlobalPBService = function(assert) {
  98. assert.equal(isPrivate(), false, 'isPrivate() is false by default');
  99. prefs.set(kAutoStartPref, true);
  100. assert.equal(prefs.get(kAutoStartPref, false), true, kAutoStartPref + ' is true now');
  101. assert.equal(isPrivate(), true, 'isPrivate() is true now');
  102. prefs.set(kAutoStartPref, false);
  103. assert.equal(isPrivate(), false, 'isPrivate() is false again');
  104. };
  105. require('sdk/test').run(exports);