1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/addon-sdk/source/test/addons/private-browsing-supported/test-windows.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,248 @@ 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 { Cc, Ci } = require('chrome'); 1.10 +const { isPrivate } = require('sdk/private-browsing'); 1.11 +const { isWindowPBSupported } = require('sdk/private-browsing/utils'); 1.12 +const { onFocus, getMostRecentWindow, getWindowTitle, getInnerId, 1.13 + getFrames, windows, open: openWindow, isWindowPrivate } = require('sdk/window/utils'); 1.14 +const { open, close, focus, promise } = require('sdk/window/helpers'); 1.15 +const { browserWindows } = require("sdk/windows"); 1.16 +const winUtils = require("sdk/deprecated/window-utils"); 1.17 +const { fromIterator: toArray } = require('sdk/util/array'); 1.18 +const tabs = require('sdk/tabs'); 1.19 + 1.20 +const WM = Cc['@mozilla.org/appshell/window-mediator;1'].getService(Ci.nsIWindowMediator); 1.21 + 1.22 +const BROWSER = 'chrome://browser/content/browser.xul'; 1.23 + 1.24 +function makeEmptyBrowserWindow(options) { 1.25 + options = options || {}; 1.26 + return open(BROWSER, { 1.27 + features: { 1.28 + chrome: true, 1.29 + private: !!options.private 1.30 + } 1.31 + }).then(focus); 1.32 +} 1.33 + 1.34 +exports.testWindowTrackerIgnoresPrivateWindows = function(assert, done) { 1.35 + var myNonPrivateWindowId, myPrivateWindowId; 1.36 + var privateWindowClosed = false; 1.37 + var privateWindowOpened = false; 1.38 + var trackedWindowIds = []; 1.39 + 1.40 + let wt = winUtils.WindowTracker({ 1.41 + onTrack: function(window) { 1.42 + let id = getInnerId(window); 1.43 + trackedWindowIds.push(id); 1.44 + }, 1.45 + onUntrack: function(window) { 1.46 + let id = getInnerId(window); 1.47 + if (id === myPrivateWindowId) { 1.48 + privateWindowClosed = true; 1.49 + } 1.50 + 1.51 + if (id === myNonPrivateWindowId) { 1.52 + assert.equal(privateWindowClosed, true, 'private window was untracked'); 1.53 + wt.unload(); 1.54 + done(); 1.55 + } 1.56 + } 1.57 + }); 1.58 + 1.59 + // make a new private window 1.60 + makeEmptyBrowserWindow({ private: true }).then(function(window) { 1.61 + myPrivateWindowId = getInnerId(window); 1.62 + 1.63 + assert.ok(trackedWindowIds.indexOf(myPrivateWindowId) >= 0, 'private window was tracked'); 1.64 + assert.equal(isPrivate(window), isWindowPBSupported, 'private window isPrivate'); 1.65 + assert.equal(isWindowPrivate(window), isWindowPBSupported); 1.66 + assert.ok(getFrames(window).length > 1, 'there are frames for private window'); 1.67 + assert.equal(getWindowTitle(window), window.document.title, 1.68 + 'getWindowTitle works'); 1.69 + 1.70 + return close(window).then(function() { 1.71 + assert.pass('private window was closed'); 1.72 + 1.73 + return makeEmptyBrowserWindow().then(function(window) { 1.74 + myNonPrivateWindowId = getInnerId(window); 1.75 + assert.notEqual(myPrivateWindowId, myNonPrivateWindowId, 'non private window was opened'); 1.76 + return close(window); 1.77 + }); 1.78 + }); 1.79 + }).then(null, assert.fail); 1.80 +}; 1.81 + 1.82 +// Test setting activeWIndow and onFocus for private windows 1.83 +exports.testSettingActiveWindowDoesNotIgnorePrivateWindow = function(assert, done) { 1.84 + let browserWindow = WM.getMostRecentWindow("navigator:browser"); 1.85 + let testSteps; 1.86 + 1.87 + assert.equal(winUtils.activeBrowserWindow, browserWindow, 1.88 + "Browser window is the active browser window."); 1.89 + assert.ok(!isPrivate(browserWindow), "Browser window is not private."); 1.90 + 1.91 + // make a new private window 1.92 + makeEmptyBrowserWindow({ 1.93 + private: true 1.94 + }).then(function(window) { 1.95 + let continueAfterFocus = function(window) onFocus(window).then(nextTest); 1.96 + 1.97 + // PWPB case 1.98 + if (isWindowPBSupported) { 1.99 + assert.ok(isPrivate(window), "window is private"); 1.100 + assert.notDeepEqual(winUtils.activeBrowserWindow, browserWindow); 1.101 + } 1.102 + // Global case 1.103 + else { 1.104 + assert.ok(!isPrivate(window), "window is not private"); 1.105 + } 1.106 + 1.107 + assert.strictEqual(winUtils.activeBrowserWindow, window, 1.108 + "Correct active browser window pb supported"); 1.109 + assert.notStrictEqual(browserWindow, window, 1.110 + "The window is not the old browser window"); 1.111 + 1.112 + testSteps = [ 1.113 + function() { 1.114 + // test setting a non private window 1.115 + continueAfterFocus(winUtils.activeWindow = browserWindow); 1.116 + }, 1.117 + function() { 1.118 + assert.strictEqual(winUtils.activeWindow, browserWindow, 1.119 + "Correct active window [1]"); 1.120 + assert.strictEqual(winUtils.activeBrowserWindow, browserWindow, 1.121 + "Correct active browser window [1]"); 1.122 + 1.123 + // test focus(window) 1.124 + focus(window).then(nextTest); 1.125 + }, 1.126 + function(w) { 1.127 + assert.strictEqual(w, window, 'require("sdk/window/helpers").focus on window works'); 1.128 + assert.strictEqual(winUtils.activeBrowserWindow, window, 1.129 + "Correct active browser window [2]"); 1.130 + assert.strictEqual(winUtils.activeWindow, window, 1.131 + "Correct active window [2]"); 1.132 + 1.133 + // test setting a private window 1.134 + continueAfterFocus(winUtils.activeWindow = window); 1.135 + }, 1.136 + function() { 1.137 + assert.deepEqual(winUtils.activeBrowserWindow, window, 1.138 + "Correct active browser window [3]"); 1.139 + assert.deepEqual(winUtils.activeWindow, window, 1.140 + "Correct active window [3]"); 1.141 + 1.142 + // just to get back to original state 1.143 + continueAfterFocus(winUtils.activeWindow = browserWindow); 1.144 + }, 1.145 + function() { 1.146 + assert.deepEqual(winUtils.activeBrowserWindow, browserWindow, 1.147 + "Correct active browser window when pb mode is supported [4]"); 1.148 + assert.deepEqual(winUtils.activeWindow, browserWindow, 1.149 + "Correct active window when pb mode is supported [4]"); 1.150 + 1.151 + close(window).then(done).then(null, assert.fail); 1.152 + } 1.153 + ]; 1.154 + 1.155 + function nextTest() { 1.156 + let args = arguments; 1.157 + if (testSteps.length) { 1.158 + require('sdk/timers').setTimeout(function() { 1.159 + (testSteps.shift()).apply(null, args); 1.160 + }, 0); 1.161 + } 1.162 + } 1.163 + nextTest(); 1.164 + }); 1.165 +}; 1.166 + 1.167 +exports.testActiveWindowDoesNotIgnorePrivateWindow = function(assert, done) { 1.168 + // make a new private window 1.169 + makeEmptyBrowserWindow({ 1.170 + private: true 1.171 + }).then(function(window) { 1.172 + // PWPB case 1.173 + if (isWindowPBSupported) { 1.174 + assert.equal(isPrivate(winUtils.activeWindow), true, 1.175 + "active window is private"); 1.176 + assert.equal(isPrivate(winUtils.activeBrowserWindow), true, 1.177 + "active browser window is private"); 1.178 + assert.ok(isWindowPrivate(window), "window is private"); 1.179 + assert.ok(isPrivate(window), "window is private"); 1.180 + 1.181 + // pb mode is supported 1.182 + assert.ok( 1.183 + isWindowPrivate(winUtils.activeWindow), 1.184 + "active window is private when pb mode is supported"); 1.185 + assert.ok( 1.186 + isWindowPrivate(winUtils.activeBrowserWindow), 1.187 + "active browser window is private when pb mode is supported"); 1.188 + assert.ok(isPrivate(winUtils.activeWindow), 1.189 + "active window is private when pb mode is supported"); 1.190 + assert.ok(isPrivate(winUtils.activeBrowserWindow), 1.191 + "active browser window is private when pb mode is supported"); 1.192 + } 1.193 + // Global case 1.194 + else { 1.195 + assert.equal(isPrivate(winUtils.activeWindow), false, 1.196 + "active window is not private"); 1.197 + assert.equal(isPrivate(winUtils.activeBrowserWindow), false, 1.198 + "active browser window is not private"); 1.199 + assert.equal(isWindowPrivate(window), false, "window is not private"); 1.200 + assert.equal(isPrivate(window), false, "window is not private"); 1.201 + } 1.202 + 1.203 + return close(window); 1.204 + }).then(done).then(null, assert.fail); 1.205 +} 1.206 + 1.207 +exports.testWindowIteratorIgnoresPrivateWindows = function(assert, done) { 1.208 + // make a new private window 1.209 + makeEmptyBrowserWindow({ 1.210 + private: true 1.211 + }).then(function(window) { 1.212 + assert.equal(isWindowPrivate(window), isWindowPBSupported); 1.213 + assert.ok(toArray(winUtils.windowIterator()).indexOf(window) > -1, 1.214 + "window is in windowIterator()"); 1.215 + 1.216 + return close(window); 1.217 + }).then(done).then(null, assert.fail); 1.218 +}; 1.219 + 1.220 +// test that it is not possible to find a private window in 1.221 +// windows module's iterator 1.222 +exports.testWindowIteratorPrivateDefault = function(assert, done) { 1.223 + // there should only be one window open here, if not give us the 1.224 + // the urls 1.225 + if (browserWindows.length > 1) { 1.226 + for each (let tab in tabs) { 1.227 + assert.fail("TAB URL: " + tab.url); 1.228 + } 1.229 + } 1.230 + else { 1.231 + assert.equal(browserWindows.length, 1, 'only one window open'); 1.232 + } 1.233 + 1.234 + open('chrome://browser/content/browser.xul', { 1.235 + features: { 1.236 + private: true, 1.237 + chrome: true 1.238 + } 1.239 + }).then(focus).then(function(window) { 1.240 + // test that there is a private window opened 1.241 + assert.equal(isPrivate(window), isWindowPBSupported, 'there is a private window open'); 1.242 + assert.equal(isPrivate(winUtils.activeWindow), isWindowPBSupported); 1.243 + assert.equal(isPrivate(getMostRecentWindow()), isWindowPBSupported); 1.244 + assert.equal(isPrivate(browserWindows.activeWindow), isWindowPBSupported); 1.245 + 1.246 + assert.equal(browserWindows.length, 2, '2 windows open'); 1.247 + assert.equal(windows(null, { includePrivate: true }).length, 2); 1.248 + 1.249 + return close(window); 1.250 + }).then(done).then(null, assert.fail); 1.251 +};