1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/addon-sdk/source/test/test-private-browsing.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,132 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 +'use strict'; 1.8 + 1.9 +const { Ci, Cu } = require('chrome'); 1.10 +const { safeMerge } = require('sdk/util/object'); 1.11 +const windows = require('sdk/windows').browserWindows; 1.12 +const tabs = require('sdk/tabs'); 1.13 +const winUtils = require('sdk/window/utils'); 1.14 +const { isWindowPrivate } = winUtils; 1.15 +const { isPrivateBrowsingSupported } = require('sdk/self'); 1.16 +const { is } = require('sdk/system/xul-app'); 1.17 +const { isPrivate } = require('sdk/private-browsing'); 1.18 +const { getOwnerWindow } = require('sdk/private-browsing/window/utils'); 1.19 +const { LoaderWithHookedConsole } = require("sdk/test/loader"); 1.20 +const { getMode, isGlobalPBSupported, 1.21 + isWindowPBSupported, isTabPBSupported } = require('sdk/private-browsing/utils'); 1.22 +const { pb } = require('./private-browsing/helper'); 1.23 +const prefs = require('sdk/preferences/service'); 1.24 +const { set: setPref } = require("sdk/preferences/service"); 1.25 +const DEPRECATE_PREF = "devtools.errorconsole.deprecation_warnings"; 1.26 + 1.27 +const { Services } = Cu.import("resource://gre/modules/Services.jsm", {}); 1.28 + 1.29 +const kAutoStartPref = "browser.privatebrowsing.autostart"; 1.30 + 1.31 +// is global pb is enabled? 1.32 +if (isGlobalPBSupported) { 1.33 + safeMerge(module.exports, require('./private-browsing/global')); 1.34 + 1.35 + exports.testGlobalOnlyOnFirefox = function(assert) { 1.36 + assert.ok(is("Firefox"), "isGlobalPBSupported is only true on Firefox"); 1.37 + } 1.38 +} 1.39 +else if (isWindowPBSupported) { 1.40 + safeMerge(module.exports, require('./private-browsing/windows')); 1.41 + 1.42 + exports.testPWOnlyOnFirefox = function(assert) { 1.43 + assert.ok(is("Firefox"), "isWindowPBSupported is only true on Firefox"); 1.44 + } 1.45 +} 1.46 +// only on Fennec 1.47 +else if (isTabPBSupported) { 1.48 + safeMerge(module.exports, require('./private-browsing/tabs')); 1.49 + 1.50 + exports.testPTOnlyOnFennec = function(assert) { 1.51 + assert.ok(is("Fennec"), "isTabPBSupported is only true on Fennec"); 1.52 + } 1.53 +} 1.54 + 1.55 +exports.testIsPrivateDefaults = function(assert) { 1.56 + assert.equal(isPrivate(), false, 'undefined is not private'); 1.57 + assert.equal(isPrivate('test'), false, 'strings are not private'); 1.58 + assert.equal(isPrivate({}), false, 'random objects are not private'); 1.59 + assert.equal(isPrivate(4), false, 'numbers are not private'); 1.60 + assert.equal(isPrivate(/abc/), false, 'regex are not private'); 1.61 + assert.equal(isPrivate(function() {}), false, 'functions are not private'); 1.62 +}; 1.63 + 1.64 +exports.testWindowDefaults = function(assert) { 1.65 + setPref(DEPRECATE_PREF, true); 1.66 + // Ensure that browserWindow still works while being deprecated 1.67 + let { loader, messages } = LoaderWithHookedConsole(module); 1.68 + let windows = loader.require("sdk/windows").browserWindows; 1.69 + assert.equal(windows.activeWindow.isPrivateBrowsing, false, 1.70 + 'window is not private browsing by default'); 1.71 + assert.ok(/DEPRECATED.+isPrivateBrowsing/.test(messages[0].msg), 1.72 + 'isPrivateBrowsing is deprecated'); 1.73 + 1.74 + let chromeWin = winUtils.getMostRecentBrowserWindow(); 1.75 + assert.equal(getMode(chromeWin), false); 1.76 + assert.equal(isWindowPrivate(chromeWin), false); 1.77 +}; 1.78 + 1.79 +// tests for the case where private browsing doesn't exist 1.80 +exports.testIsActiveDefault = function(assert) { 1.81 + assert.equal(pb.isActive, false, 1.82 + 'pb.isActive returns false when private browsing isn\'t supported'); 1.83 +}; 1.84 + 1.85 +exports.testIsPrivateBrowsingFalseDefault = function(assert) { 1.86 + assert.equal(isPrivateBrowsingSupported, false, 1.87 + 'isPrivateBrowsingSupported property is false by default'); 1.88 +}; 1.89 + 1.90 +exports.testGetOwnerWindow = function(assert, done) { 1.91 + let window = windows.activeWindow; 1.92 + let chromeWindow = getOwnerWindow(window); 1.93 + assert.ok(chromeWindow instanceof Ci.nsIDOMWindow, 'associated window is found'); 1.94 + 1.95 + tabs.open({ 1.96 + url: 'about:blank', 1.97 + isPrivate: true, 1.98 + onOpen: function(tab) { 1.99 + // test that getOwnerWindow works as expected 1.100 + if (is('Fennec')) { 1.101 + assert.notStrictEqual(chromeWindow, getOwnerWindow(tab)); 1.102 + assert.ok(getOwnerWindow(tab) instanceof Ci.nsIDOMWindow); 1.103 + } 1.104 + else { 1.105 + assert.strictEqual(chromeWindow, getOwnerWindow(tab), 'associated window is the same for window and window\'s tab'); 1.106 + } 1.107 + 1.108 + // test that the tab is not private 1.109 + // private flag should be ignored by default 1.110 + assert.ok(!isPrivate(tab)); 1.111 + assert.ok(!isPrivate(getOwnerWindow(tab))); 1.112 + 1.113 + tab.close(done); 1.114 + } 1.115 + }); 1.116 +}; 1.117 + 1.118 +exports.testNSIPrivateBrowsingChannel = function(assert) { 1.119 + let channel = Services.io.newChannel("about:blank", null, null); 1.120 + channel.QueryInterface(Ci.nsIPrivateBrowsingChannel); 1.121 + assert.equal(isPrivate(channel), false, 'isPrivate detects non-private channels'); 1.122 + channel.setPrivate(true); 1.123 + assert.ok(isPrivate(channel), 'isPrivate detects private channels'); 1.124 +} 1.125 + 1.126 +exports.testNewGlobalPBService = function(assert) { 1.127 + assert.equal(isPrivate(), false, 'isPrivate() is false by default'); 1.128 + prefs.set(kAutoStartPref, true); 1.129 + assert.equal(prefs.get(kAutoStartPref, false), true, kAutoStartPref + ' is true now'); 1.130 + assert.equal(isPrivate(), true, 'isPrivate() is true now'); 1.131 + prefs.set(kAutoStartPref, false); 1.132 + assert.equal(isPrivate(), false, 'isPrivate() is false again'); 1.133 +}; 1.134 + 1.135 +require('sdk/test').run(exports);