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