1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/addon-sdk/source/test/test-window-utils-global-private-browsing.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,152 @@ 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 windowUtils = require('sdk/deprecated/window-utils'); 1.10 +const { isWindowPBSupported, isGlobalPBSupported } = require('sdk/private-browsing/utils'); 1.11 +const { getFrames, getWindowTitle, onFocus, isWindowPrivate, windows, isBrowser } = require('sdk/window/utils'); 1.12 +const { open, close, focus } = require('sdk/window/helpers'); 1.13 +const { isPrivate } = require('sdk/private-browsing'); 1.14 +const { pb } = require('./private-browsing/helper'); 1.15 +const { fromIterator: toArray } = require('sdk/util/array'); 1.16 + 1.17 +function makeEmptyBrowserWindow(options) { 1.18 + options = options || {}; 1.19 + return open('chrome://browser/content/browser.xul', { 1.20 + features: { 1.21 + chrome: true, 1.22 + private: !!options.private, 1.23 + toolbar: true 1.24 + } 1.25 + }); 1.26 +} 1.27 + 1.28 +exports.testShowPanelAndWidgetOnPrivateWindow = function(assert, done) { 1.29 + var myPrivateWindow; 1.30 + var finished = false; 1.31 + var privateWindow; 1.32 + var privateWindowClosed = false; 1.33 + var { Panel } = require('sdk/panel'); 1.34 + var { Widget } = require('sdk/widget'); 1.35 + 1.36 + pb.once('start', function() { 1.37 + assert.pass('private browsing mode started'); 1.38 + 1.39 + // make a new private window 1.40 + makeEmptyBrowserWindow().then(function(window) { 1.41 + myPrivateWindow = window; 1.42 + 1.43 + let wt = windowUtils.WindowTracker({ 1.44 + onTrack: function(window) { 1.45 + if (!isBrowser(window) || window !== myPrivateWindow) return; 1.46 + 1.47 + assert.ok(isWindowPrivate(window), 'window is private onTrack!'); 1.48 + let panel = Panel({ 1.49 + onShow: function() { 1.50 + assert.ok(this.isShowing, 'the panel is showing on the private window'); 1.51 + 1.52 + let count = 0; 1.53 + let widget = Widget({ 1.54 + id: "testShowPanelAndWidgetOnPrivateWindow-id", 1.55 + label: "My Hello Widget", 1.56 + content: "Hello!", 1.57 + onAttach: function(mod) { 1.58 + count++; 1.59 + if (count == 2) { 1.60 + panel.destroy(); 1.61 + widget.destroy(); 1.62 + close(window); 1.63 + } 1.64 + } 1.65 + }); 1.66 + } 1.67 + }).show(null, window.gBrowser); 1.68 + }, 1.69 + onUntrack: function(window) { 1.70 + if (window === myPrivateWindow) { 1.71 + wt.unload(); 1.72 + 1.73 + pb.once('stop', function() { 1.74 + assert.pass('private browsing mode end'); 1.75 + done(); 1.76 + }); 1.77 + 1.78 + pb.deactivate(); 1.79 + } 1.80 + } 1.81 + }); 1.82 + 1.83 + assert.equal(isWindowPrivate(window), true, 'the opened window is private'); 1.84 + assert.equal(isPrivate(window), true, 'the opened window is private'); 1.85 + assert.ok(getFrames(window).length > 1, 'there are frames for private window'); 1.86 + assert.equal(getWindowTitle(window), window.document.title, 1.87 + 'getWindowTitle works'); 1.88 + }); 1.89 + }); 1.90 + pb.activate(); 1.91 +}; 1.92 + 1.93 +exports.testWindowTrackerDoesNotIgnorePrivateWindows = function(assert, done) { 1.94 + var myPrivateWindow; 1.95 + var count = 0; 1.96 + 1.97 + let wt = windowUtils.WindowTracker({ 1.98 + onTrack: function(window) { 1.99 + if (!isBrowser(window) || !isWindowPrivate(window)) return; 1.100 + assert.ok(isWindowPrivate(window), 'window is private onTrack!'); 1.101 + if (++count == 1) 1.102 + close(window); 1.103 + }, 1.104 + onUntrack: function(window) { 1.105 + if (count == 1 && isWindowPrivate(window)) { 1.106 + wt.unload(); 1.107 + 1.108 + pb.once('stop', function() { 1.109 + assert.pass('private browsing mode end'); 1.110 + done(); 1.111 + }); 1.112 + pb.deactivate(); 1.113 + } 1.114 + } 1.115 + }); 1.116 + 1.117 + pb.once('start', function() { 1.118 + assert.pass('private browsing mode started'); 1.119 + makeEmptyBrowserWindow(); 1.120 + }); 1.121 + pb.activate(); 1.122 +} 1.123 + 1.124 +exports.testWindowIteratorDoesNotIgnorePrivateWindows = function(assert, done) { 1.125 + pb.once('start', function() { 1.126 + // make a new private window 1.127 + makeEmptyBrowserWindow().then(function(window) { 1.128 + assert.ok(isWindowPrivate(window), "window is private"); 1.129 + assert.equal(isPrivate(window), true, 'the opened window is private'); 1.130 + assert.ok(toArray(windowUtils.windowIterator()).indexOf(window) > -1, 1.131 + "window is in windowIterator()"); 1.132 + assert.ok(windows(null, { includePrivate: true }).indexOf(window) > -1, 1.133 + "window is in windows()"); 1.134 + 1.135 + close(window).then(function() { 1.136 + pb.once('stop', function() { 1.137 + done(); 1.138 + }); 1.139 + pb.deactivate(); 1.140 + }); 1.141 + }); 1.142 + }); 1.143 + pb.activate(); 1.144 +}; 1.145 + 1.146 +if (!isGlobalPBSupported) { 1.147 + module.exports = { 1.148 + "test Unsupported Test": function UnsupportedTest (assert) { 1.149 + assert.pass( 1.150 + "Skipping global private browsing tests"); 1.151 + } 1.152 + } 1.153 +} 1.154 + 1.155 +require("test").run(exports);