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 { Cc, Ci } = require('chrome'); michael@0: const { isPrivate } = require('sdk/private-browsing'); michael@0: const { isWindowPBSupported } = require('sdk/private-browsing/utils'); michael@0: const { onFocus, getMostRecentWindow, getWindowTitle, getInnerId, michael@0: getFrames, windows, open: openWindow, isWindowPrivate } = require('sdk/window/utils'); michael@0: const { open, close, focus, promise } = require('sdk/window/helpers'); michael@0: const { browserWindows } = require("sdk/windows"); michael@0: const winUtils = require("sdk/deprecated/window-utils"); michael@0: const { fromIterator: toArray } = require('sdk/util/array'); michael@0: const tabs = require('sdk/tabs'); michael@0: michael@0: const WM = Cc['@mozilla.org/appshell/window-mediator;1'].getService(Ci.nsIWindowMediator); michael@0: michael@0: const BROWSER = 'chrome://browser/content/browser.xul'; michael@0: michael@0: function makeEmptyBrowserWindow(options) { michael@0: options = options || {}; michael@0: return open(BROWSER, { michael@0: features: { michael@0: chrome: true, michael@0: private: !!options.private michael@0: } michael@0: }).then(focus); michael@0: } michael@0: michael@0: exports.testWindowTrackerIgnoresPrivateWindows = function(assert, done) { michael@0: var myNonPrivateWindowId, myPrivateWindowId; michael@0: var privateWindowClosed = false; michael@0: var privateWindowOpened = false; michael@0: var trackedWindowIds = []; michael@0: michael@0: let wt = winUtils.WindowTracker({ michael@0: onTrack: function(window) { michael@0: let id = getInnerId(window); michael@0: trackedWindowIds.push(id); michael@0: }, michael@0: onUntrack: function(window) { michael@0: let id = getInnerId(window); michael@0: if (id === myPrivateWindowId) { michael@0: privateWindowClosed = true; michael@0: } michael@0: michael@0: if (id === myNonPrivateWindowId) { michael@0: assert.equal(privateWindowClosed, true, 'private window was untracked'); michael@0: wt.unload(); michael@0: done(); michael@0: } michael@0: } michael@0: }); michael@0: michael@0: // make a new private window michael@0: makeEmptyBrowserWindow({ private: true }).then(function(window) { michael@0: myPrivateWindowId = getInnerId(window); michael@0: michael@0: assert.ok(trackedWindowIds.indexOf(myPrivateWindowId) >= 0, 'private window was tracked'); michael@0: assert.equal(isPrivate(window), isWindowPBSupported, 'private window isPrivate'); michael@0: assert.equal(isWindowPrivate(window), isWindowPBSupported); michael@0: assert.ok(getFrames(window).length > 1, 'there are frames for private window'); michael@0: assert.equal(getWindowTitle(window), window.document.title, michael@0: 'getWindowTitle works'); michael@0: michael@0: return close(window).then(function() { michael@0: assert.pass('private window was closed'); michael@0: michael@0: return makeEmptyBrowserWindow().then(function(window) { michael@0: myNonPrivateWindowId = getInnerId(window); michael@0: assert.notEqual(myPrivateWindowId, myNonPrivateWindowId, 'non private window was opened'); michael@0: return close(window); michael@0: }); michael@0: }); michael@0: }).then(null, assert.fail); michael@0: }; michael@0: michael@0: // Test setting activeWIndow and onFocus for private windows michael@0: exports.testSettingActiveWindowDoesNotIgnorePrivateWindow = function(assert, done) { michael@0: let browserWindow = WM.getMostRecentWindow("navigator:browser"); michael@0: let testSteps; michael@0: michael@0: assert.equal(winUtils.activeBrowserWindow, browserWindow, michael@0: "Browser window is the active browser window."); michael@0: assert.ok(!isPrivate(browserWindow), "Browser window is not private."); michael@0: michael@0: // make a new private window michael@0: makeEmptyBrowserWindow({ michael@0: private: true michael@0: }).then(function(window) { michael@0: let continueAfterFocus = function(window) onFocus(window).then(nextTest); michael@0: michael@0: // PWPB case michael@0: if (isWindowPBSupported) { michael@0: assert.ok(isPrivate(window), "window is private"); michael@0: assert.notDeepEqual(winUtils.activeBrowserWindow, browserWindow); michael@0: } michael@0: // Global case michael@0: else { michael@0: assert.ok(!isPrivate(window), "window is not private"); michael@0: } michael@0: michael@0: assert.strictEqual(winUtils.activeBrowserWindow, window, michael@0: "Correct active browser window pb supported"); michael@0: assert.notStrictEqual(browserWindow, window, michael@0: "The window is not the old browser window"); michael@0: michael@0: testSteps = [ michael@0: function() { michael@0: // test setting a non private window michael@0: continueAfterFocus(winUtils.activeWindow = browserWindow); michael@0: }, michael@0: function() { michael@0: assert.strictEqual(winUtils.activeWindow, browserWindow, michael@0: "Correct active window [1]"); michael@0: assert.strictEqual(winUtils.activeBrowserWindow, browserWindow, michael@0: "Correct active browser window [1]"); michael@0: michael@0: // test focus(window) michael@0: focus(window).then(nextTest); michael@0: }, michael@0: function(w) { michael@0: assert.strictEqual(w, window, 'require("sdk/window/helpers").focus on window works'); michael@0: assert.strictEqual(winUtils.activeBrowserWindow, window, michael@0: "Correct active browser window [2]"); michael@0: assert.strictEqual(winUtils.activeWindow, window, michael@0: "Correct active window [2]"); michael@0: michael@0: // test setting a private window michael@0: continueAfterFocus(winUtils.activeWindow = window); michael@0: }, michael@0: function() { michael@0: assert.deepEqual(winUtils.activeBrowserWindow, window, michael@0: "Correct active browser window [3]"); michael@0: assert.deepEqual(winUtils.activeWindow, window, michael@0: "Correct active window [3]"); michael@0: michael@0: // just to get back to original state michael@0: continueAfterFocus(winUtils.activeWindow = browserWindow); michael@0: }, michael@0: function() { michael@0: assert.deepEqual(winUtils.activeBrowserWindow, browserWindow, michael@0: "Correct active browser window when pb mode is supported [4]"); michael@0: assert.deepEqual(winUtils.activeWindow, browserWindow, michael@0: "Correct active window when pb mode is supported [4]"); michael@0: michael@0: close(window).then(done).then(null, assert.fail); michael@0: } michael@0: ]; michael@0: michael@0: function nextTest() { michael@0: let args = arguments; michael@0: if (testSteps.length) { michael@0: require('sdk/timers').setTimeout(function() { michael@0: (testSteps.shift()).apply(null, args); michael@0: }, 0); michael@0: } michael@0: } michael@0: nextTest(); michael@0: }); michael@0: }; michael@0: michael@0: exports.testActiveWindowDoesNotIgnorePrivateWindow = function(assert, done) { michael@0: // make a new private window michael@0: makeEmptyBrowserWindow({ michael@0: private: true michael@0: }).then(function(window) { michael@0: // PWPB case michael@0: if (isWindowPBSupported) { michael@0: assert.equal(isPrivate(winUtils.activeWindow), true, michael@0: "active window is private"); michael@0: assert.equal(isPrivate(winUtils.activeBrowserWindow), true, michael@0: "active browser window is private"); michael@0: assert.ok(isWindowPrivate(window), "window is private"); michael@0: assert.ok(isPrivate(window), "window is private"); michael@0: michael@0: // pb mode is supported michael@0: assert.ok( michael@0: isWindowPrivate(winUtils.activeWindow), michael@0: "active window is private when pb mode is supported"); michael@0: assert.ok( michael@0: isWindowPrivate(winUtils.activeBrowserWindow), michael@0: "active browser window is private when pb mode is supported"); michael@0: assert.ok(isPrivate(winUtils.activeWindow), michael@0: "active window is private when pb mode is supported"); michael@0: assert.ok(isPrivate(winUtils.activeBrowserWindow), michael@0: "active browser window is private when pb mode is supported"); michael@0: } michael@0: // Global case michael@0: else { michael@0: assert.equal(isPrivate(winUtils.activeWindow), false, michael@0: "active window is not private"); michael@0: assert.equal(isPrivate(winUtils.activeBrowserWindow), false, michael@0: "active browser window is not private"); michael@0: assert.equal(isWindowPrivate(window), false, "window is not private"); michael@0: assert.equal(isPrivate(window), false, "window is not private"); michael@0: } michael@0: michael@0: return close(window); michael@0: }).then(done).then(null, assert.fail); michael@0: } michael@0: michael@0: exports.testWindowIteratorIgnoresPrivateWindows = function(assert, done) { michael@0: // make a new private window michael@0: makeEmptyBrowserWindow({ michael@0: private: true michael@0: }).then(function(window) { michael@0: assert.equal(isWindowPrivate(window), isWindowPBSupported); michael@0: assert.ok(toArray(winUtils.windowIterator()).indexOf(window) > -1, michael@0: "window is in windowIterator()"); michael@0: michael@0: return close(window); michael@0: }).then(done).then(null, assert.fail); michael@0: }; michael@0: michael@0: // test that it is not possible to find a private window in michael@0: // windows module's iterator michael@0: exports.testWindowIteratorPrivateDefault = function(assert, done) { michael@0: // there should only be one window open here, if not give us the michael@0: // the urls michael@0: if (browserWindows.length > 1) { michael@0: for each (let tab in tabs) { michael@0: assert.fail("TAB URL: " + tab.url); michael@0: } michael@0: } michael@0: else { michael@0: assert.equal(browserWindows.length, 1, 'only one window open'); michael@0: } michael@0: michael@0: open('chrome://browser/content/browser.xul', { michael@0: features: { michael@0: private: true, michael@0: chrome: true michael@0: } michael@0: }).then(focus).then(function(window) { michael@0: // test that there is a private window opened michael@0: assert.equal(isPrivate(window), isWindowPBSupported, 'there is a private window open'); michael@0: assert.equal(isPrivate(winUtils.activeWindow), isWindowPBSupported); michael@0: assert.equal(isPrivate(getMostRecentWindow()), isWindowPBSupported); michael@0: assert.equal(isPrivate(browserWindows.activeWindow), isWindowPBSupported); michael@0: michael@0: assert.equal(browserWindows.length, 2, '2 windows open'); michael@0: assert.equal(windows(null, { includePrivate: true }).length, 2); michael@0: michael@0: return close(window); michael@0: }).then(done).then(null, assert.fail); michael@0: };