michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: 'use strict'; michael@0: michael@0: const { Ci, Cu } = require('chrome'); michael@0: const { safeMerge } = require('sdk/util/object'); michael@0: const windows = require('sdk/windows').browserWindows; michael@0: const tabs = require('sdk/tabs'); michael@0: const winUtils = require('sdk/window/utils'); michael@0: const { isWindowPrivate } = winUtils; michael@0: const { isPrivateBrowsingSupported } = require('sdk/self'); michael@0: const { is } = require('sdk/system/xul-app'); michael@0: const { isPrivate } = require('sdk/private-browsing'); michael@0: const { getOwnerWindow } = require('sdk/private-browsing/window/utils'); michael@0: const { LoaderWithHookedConsole } = require("sdk/test/loader"); michael@0: const { getMode, isGlobalPBSupported, michael@0: isWindowPBSupported, isTabPBSupported } = require('sdk/private-browsing/utils'); michael@0: const { pb } = require('./private-browsing/helper'); michael@0: const prefs = require('sdk/preferences/service'); michael@0: const { set: setPref } = require("sdk/preferences/service"); michael@0: const DEPRECATE_PREF = "devtools.errorconsole.deprecation_warnings"; michael@0: michael@0: const { Services } = Cu.import("resource://gre/modules/Services.jsm", {}); michael@0: michael@0: const kAutoStartPref = "browser.privatebrowsing.autostart"; michael@0: michael@0: // is global pb is enabled? michael@0: if (isGlobalPBSupported) { michael@0: safeMerge(module.exports, require('./private-browsing/global')); michael@0: michael@0: exports.testGlobalOnlyOnFirefox = function(assert) { michael@0: assert.ok(is("Firefox"), "isGlobalPBSupported is only true on Firefox"); michael@0: } michael@0: } michael@0: else if (isWindowPBSupported) { michael@0: safeMerge(module.exports, require('./private-browsing/windows')); michael@0: michael@0: exports.testPWOnlyOnFirefox = function(assert) { michael@0: assert.ok(is("Firefox"), "isWindowPBSupported is only true on Firefox"); michael@0: } michael@0: } michael@0: // only on Fennec michael@0: else if (isTabPBSupported) { michael@0: safeMerge(module.exports, require('./private-browsing/tabs')); michael@0: michael@0: exports.testPTOnlyOnFennec = function(assert) { michael@0: assert.ok(is("Fennec"), "isTabPBSupported is only true on Fennec"); michael@0: } michael@0: } michael@0: michael@0: exports.testIsPrivateDefaults = function(assert) { michael@0: assert.equal(isPrivate(), false, 'undefined is not private'); michael@0: assert.equal(isPrivate('test'), false, 'strings are not private'); michael@0: assert.equal(isPrivate({}), false, 'random objects are not private'); michael@0: assert.equal(isPrivate(4), false, 'numbers are not private'); michael@0: assert.equal(isPrivate(/abc/), false, 'regex are not private'); michael@0: assert.equal(isPrivate(function() {}), false, 'functions are not private'); michael@0: }; michael@0: michael@0: exports.testWindowDefaults = function(assert) { michael@0: setPref(DEPRECATE_PREF, true); michael@0: // Ensure that browserWindow still works while being deprecated michael@0: let { loader, messages } = LoaderWithHookedConsole(module); michael@0: let windows = loader.require("sdk/windows").browserWindows; michael@0: assert.equal(windows.activeWindow.isPrivateBrowsing, false, michael@0: 'window is not private browsing by default'); michael@0: assert.ok(/DEPRECATED.+isPrivateBrowsing/.test(messages[0].msg), michael@0: 'isPrivateBrowsing is deprecated'); michael@0: michael@0: let chromeWin = winUtils.getMostRecentBrowserWindow(); michael@0: assert.equal(getMode(chromeWin), false); michael@0: assert.equal(isWindowPrivate(chromeWin), false); michael@0: }; michael@0: michael@0: // tests for the case where private browsing doesn't exist michael@0: exports.testIsActiveDefault = function(assert) { michael@0: assert.equal(pb.isActive, false, michael@0: 'pb.isActive returns false when private browsing isn\'t supported'); michael@0: }; michael@0: michael@0: exports.testIsPrivateBrowsingFalseDefault = function(assert) { michael@0: assert.equal(isPrivateBrowsingSupported, false, michael@0: 'isPrivateBrowsingSupported property is false by default'); michael@0: }; michael@0: michael@0: exports.testGetOwnerWindow = function(assert, done) { michael@0: let window = windows.activeWindow; michael@0: let chromeWindow = getOwnerWindow(window); michael@0: assert.ok(chromeWindow instanceof Ci.nsIDOMWindow, 'associated window is found'); michael@0: michael@0: tabs.open({ michael@0: url: 'about:blank', michael@0: isPrivate: true, michael@0: onOpen: function(tab) { michael@0: // test that getOwnerWindow works as expected michael@0: if (is('Fennec')) { michael@0: assert.notStrictEqual(chromeWindow, getOwnerWindow(tab)); michael@0: assert.ok(getOwnerWindow(tab) instanceof Ci.nsIDOMWindow); michael@0: } michael@0: else { michael@0: assert.strictEqual(chromeWindow, getOwnerWindow(tab), 'associated window is the same for window and window\'s tab'); michael@0: } michael@0: michael@0: // test that the tab is not private michael@0: // private flag should be ignored by default michael@0: assert.ok(!isPrivate(tab)); michael@0: assert.ok(!isPrivate(getOwnerWindow(tab))); michael@0: michael@0: tab.close(done); michael@0: } michael@0: }); michael@0: }; michael@0: michael@0: exports.testNSIPrivateBrowsingChannel = function(assert) { michael@0: let channel = Services.io.newChannel("about:blank", null, null); michael@0: channel.QueryInterface(Ci.nsIPrivateBrowsingChannel); michael@0: assert.equal(isPrivate(channel), false, 'isPrivate detects non-private channels'); michael@0: channel.setPrivate(true); michael@0: assert.ok(isPrivate(channel), 'isPrivate detects private channels'); michael@0: } michael@0: michael@0: exports.testNewGlobalPBService = function(assert) { michael@0: assert.equal(isPrivate(), false, 'isPrivate() is false by default'); michael@0: prefs.set(kAutoStartPref, true); michael@0: assert.equal(prefs.get(kAutoStartPref, false), true, kAutoStartPref + ' is true now'); michael@0: assert.equal(isPrivate(), true, 'isPrivate() is true now'); michael@0: prefs.set(kAutoStartPref, false); michael@0: assert.equal(isPrivate(), false, 'isPrivate() is false again'); michael@0: }; michael@0: michael@0: require('sdk/test').run(exports);