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: // Fennec support tracked in bug #809412 michael@0: module.metadata = { michael@0: 'engines': { michael@0: 'Firefox': '*' michael@0: } michael@0: }; michael@0: michael@0: const windowUtils = require('sdk/deprecated/window-utils'); michael@0: const { Cc, Ci } = require('chrome'); michael@0: const { isWindowPBSupported } = require('sdk/private-browsing/utils'); michael@0: const { getFrames, getWindowTitle, onFocus, isWindowPrivate } = require('sdk/window/utils'); michael@0: const { open, close, focus } = require('sdk/window/helpers'); michael@0: const WM = Cc['@mozilla.org/appshell/window-mediator;1'].getService(Ci.nsIWindowMediator); michael@0: const { isPrivate } = require('sdk/private-browsing'); michael@0: const { fromIterator: toArray } = require('sdk/util/array'); michael@0: const { defer } = require('sdk/core/promise'); michael@0: const { setTimeout } = require('sdk/timers'); michael@0: michael@0: function tick() { michael@0: let deferred = defer(); michael@0: setTimeout(deferred.resolve); michael@0: return deferred.promise; michael@0: } michael@0: michael@0: function makeEmptyBrowserWindow(options) { michael@0: options = options || {}; michael@0: return open('chrome://browser/content/browser.xul', { michael@0: features: { michael@0: chrome: true, michael@0: private: !!options.private michael@0: } michael@0: }); michael@0: } michael@0: michael@0: exports.testWindowTrackerIgnoresPrivateWindows = function(assert, done) { michael@0: var myNonPrivateWindow, myPrivateWindow; michael@0: var finished = false; michael@0: var privateWindow; michael@0: var privateWindowClosed = false; michael@0: michael@0: let wt = windowUtils.WindowTracker({ michael@0: onTrack: function(window) { michael@0: assert.ok(!isWindowPrivate(window), 'private window was not tracked!'); michael@0: }, michael@0: onUntrack: function(window) { michael@0: assert.ok(!isWindowPrivate(window), 'private window was not tracked!'); michael@0: // PWPB case michael@0: if (window === myPrivateWindow && isWindowPBSupported) { michael@0: privateWindowClosed = true; michael@0: } michael@0: if (window === myNonPrivateWindow) { michael@0: assert.ok(!privateWindowClosed); 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({ michael@0: private: true michael@0: }).then(function(window) { michael@0: myPrivateWindow = window; michael@0: 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: return makeEmptyBrowserWindow().then(function(window) { michael@0: myNonPrivateWindow = window; michael@0: assert.pass('opened new window'); 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: michael@0: assert.equal(windowUtils.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({ private: true }).then(focus).then(window => { michael@0: // PWPB case michael@0: if (isWindowPBSupported) { michael@0: assert.ok(isPrivate(window), "window is private"); michael@0: assert.notDeepEqual(windowUtils.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(windowUtils.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: michael@0: return onFocus(windowUtils.activeWindow = browserWindow).then(_ => { michael@0: assert.strictEqual(windowUtils.activeWindow, browserWindow, michael@0: "Correct active window [1]"); michael@0: assert.strictEqual(windowUtils.activeBrowserWindow, browserWindow, michael@0: "Correct active browser window [1]"); michael@0: michael@0: // test focus(window) michael@0: return focus(window).then(w => { michael@0: assert.strictEqual(w, window, 'require("sdk/window/helpers").focus on window works'); michael@0: }).then(tick); michael@0: }).then(_ => { michael@0: assert.strictEqual(windowUtils.activeBrowserWindow, window, michael@0: "Correct active browser window [2]"); michael@0: assert.strictEqual(windowUtils.activeWindow, window, michael@0: "Correct active window [2]"); michael@0: michael@0: // test setting a private window michael@0: return onFocus(windowUtils.activeWindow = window); michael@0: }).then(function() { michael@0: assert.deepEqual(windowUtils.activeBrowserWindow, window, michael@0: "Correct active browser window [3]"); michael@0: assert.deepEqual(windowUtils.activeWindow, window, michael@0: "Correct active window [3]"); michael@0: michael@0: // just to get back to original state michael@0: return onFocus(windowUtils.activeWindow = browserWindow); michael@0: }).then(_ => { michael@0: assert.deepEqual(windowUtils.activeBrowserWindow, browserWindow, michael@0: "Correct active browser window when pb mode is supported [4]"); michael@0: assert.deepEqual(windowUtils.activeWindow, browserWindow, michael@0: "Correct active window when pb mode is supported [4]"); michael@0: michael@0: return close(window); michael@0: }) michael@0: }).then(done).then(null, assert.fail); 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(windowUtils.activeWindow), true, michael@0: "active window is private"); michael@0: assert.equal(isPrivate(windowUtils.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(windowUtils.activeWindow), michael@0: "active window is private when pb mode is supported"); michael@0: assert.ok( michael@0: isWindowPrivate(windowUtils.activeBrowserWindow), michael@0: "active browser window is private when pb mode is supported"); michael@0: assert.ok(isPrivate(windowUtils.activeWindow), michael@0: "active window is private when pb mode is supported"); michael@0: assert.ok(isPrivate(windowUtils.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(windowUtils.activeWindow), false, michael@0: "active window is not private"); michael@0: assert.equal(isPrivate(windowUtils.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: // PWPB case michael@0: if (isWindowPBSupported) { michael@0: assert.ok(isWindowPrivate(window), "window is private"); michael@0: assert.equal(toArray(windowUtils.windowIterator()).indexOf(window), -1, michael@0: "window is not in windowIterator()"); michael@0: } michael@0: // Global case michael@0: else { michael@0: assert.equal(isWindowPrivate(window), false, "window is not private"); michael@0: assert.ok(toArray(windowUtils.windowIterator()).indexOf(window) > -1, michael@0: "window is in windowIterator()"); michael@0: } michael@0: michael@0: return close(window); michael@0: }).then(done).then(null, assert.fail); michael@0: }; michael@0: michael@0: require("sdk/test").run(exports);