1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/addon-sdk/source/test/addons/private-browsing-supported/test-sidebar.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,213 @@ 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 { Loader } = require('sdk/test/loader'); 1.10 +const { show, hide } = require('sdk/ui/sidebar/actions'); 1.11 +const { isShowing } = require('sdk/ui/sidebar/utils'); 1.12 +const { getMostRecentBrowserWindow, isWindowPrivate } = require('sdk/window/utils'); 1.13 +const { open, close, focus, promise: windowPromise } = require('sdk/window/helpers'); 1.14 +const { setTimeout } = require('sdk/timers'); 1.15 +const { isPrivate } = require('sdk/private-browsing'); 1.16 +const { data } = require('sdk/self'); 1.17 +const { URL } = require('sdk/url'); 1.18 + 1.19 +const { BUILTIN_SIDEBAR_MENUITEMS, isSidebarShowing, 1.20 + getSidebarMenuitems, getExtraSidebarMenuitems, makeID, simulateCommand, 1.21 + simulateClick, isChecked } = require('./sidebar/utils'); 1.22 + 1.23 +exports.testSideBarIsInNewPrivateWindows = function(assert, done) { 1.24 + const { Sidebar } = require('sdk/ui/sidebar'); 1.25 + let testName = 'testSideBarIsInNewPrivateWindows'; 1.26 + let sidebar = Sidebar({ 1.27 + id: testName, 1.28 + title: testName, 1.29 + url: 'data:text/html;charset=utf-8,'+testName 1.30 + }); 1.31 + 1.32 + let startWindow = getMostRecentBrowserWindow(); 1.33 + let ele = startWindow.document.getElementById(makeID(testName)); 1.34 + assert.ok(ele, 'sidebar element was added'); 1.35 + 1.36 + open(null, { features: { private: true } }).then(function(window) { 1.37 + let ele = window.document.getElementById(makeID(testName)); 1.38 + assert.ok(isPrivate(window), 'the new window is private'); 1.39 + assert.ok(!!ele, 'sidebar element was added'); 1.40 + 1.41 + sidebar.destroy(); 1.42 + assert.ok(!window.document.getElementById(makeID(testName)), 'sidebar id DNE'); 1.43 + assert.ok(!startWindow.document.getElementById(makeID(testName)), 'sidebar id DNE'); 1.44 + 1.45 + return close(window); 1.46 + }).then(done).then(null, assert.fail); 1.47 +} 1.48 + 1.49 +// Disabled in order to land other fixes, see bug 910647 for further details. 1.50 +/* 1.51 +exports.testSidebarIsOpenInNewPrivateWindow = function(assert, done) { 1.52 + const { Sidebar } = require('sdk/ui/sidebar'); 1.53 + let testName = 'testSidebarIsOpenInNewPrivateWindow'; 1.54 + let window = getMostRecentBrowserWindow(); 1.55 + 1.56 + let sidebar = Sidebar({ 1.57 + id: testName, 1.58 + title: testName, 1.59 + url: 'data:text/html;charset=utf-8,'+testName 1.60 + }); 1.61 + 1.62 + assert.equal(isPrivate(window), false, 'the window is not private'); 1.63 + 1.64 + sidebar.on('show', function() { 1.65 + assert.equal(isSidebarShowing(window), true, 'the sidebar is showing'); 1.66 + assert.equal(isShowing(sidebar), true, 'the sidebar is showing'); 1.67 + 1.68 + windowPromise(window.OpenBrowserWindow({private: true}), 'DOMContentLoaded').then(function(window2) { 1.69 + assert.equal(isPrivate(window2), true, 'the new window is private'); 1.70 + 1.71 + let sidebarEle = window2.document.getElementById('sidebar'); 1.72 + 1.73 + // wait for the sidebar to load something 1.74 + function onSBLoad() { 1.75 + sidebarEle.contentDocument.getElementById('web-panels-browser').addEventListener('load', function() { 1.76 + assert.equal(isSidebarShowing(window), true, 'the sidebar is showing in old window still'); 1.77 + assert.equal(isSidebarShowing(window2), true, 'the sidebar is showing in the new private window'); 1.78 + assert.equal(isShowing(sidebar), true, 'the sidebar is showing'); 1.79 + 1.80 + sidebar.destroy(); 1.81 + close(window2).then(done); 1.82 + }, true); 1.83 + } 1.84 + 1.85 + sidebarEle.addEventListener('load', onSBLoad, true); 1.86 + 1.87 + assert.pass('waiting for the sidebar to open...'); 1.88 + }, assert.fail).then(null, assert.fail); 1.89 + }); 1.90 + 1.91 + sidebar.show(); 1.92 +} 1.93 +*/ 1.94 +// TEST: edge case where web panel is destroyed while loading 1.95 +exports.testDestroyEdgeCaseBugWithPrivateWindow = function(assert, done) { 1.96 + const { Sidebar } = require('sdk/ui/sidebar'); 1.97 + let testName = 'testDestroyEdgeCaseBug'; 1.98 + let window = getMostRecentBrowserWindow(); 1.99 + let sidebar = Sidebar({ 1.100 + id: testName, 1.101 + title: testName, 1.102 + url: 'data:text/html;charset=utf-8,'+testName 1.103 + }); 1.104 + 1.105 + // NOTE: purposely not listening to show event b/c the event happens 1.106 + // between now and then. 1.107 + sidebar.show(); 1.108 + 1.109 + assert.equal(isPrivate(window), false, 'the new window is not private'); 1.110 + assert.equal(isSidebarShowing(window), true, 'the sidebar is showing'); 1.111 + 1.112 + //assert.equal(isShowing(sidebar), true, 'the sidebar is showing'); 1.113 + 1.114 + open(null, { features: { private: true } }).then(focus).then(function(window2) { 1.115 + assert.equal(isPrivate(window2), true, 'the new window is private'); 1.116 + assert.equal(isSidebarShowing(window2), false, 'the sidebar is not showing'); 1.117 + assert.equal(isShowing(sidebar), false, 'the sidebar is not showing'); 1.118 + 1.119 + sidebar.destroy(); 1.120 + assert.pass('destroying the sidebar'); 1.121 + 1.122 + close(window2).then(function() { 1.123 + let loader = Loader(module); 1.124 + 1.125 + assert.equal(isPrivate(window), false, 'the current window is not private'); 1.126 + 1.127 + let sidebar = loader.require('sdk/ui/sidebar').Sidebar({ 1.128 + id: testName, 1.129 + title: testName, 1.130 + url: 'data:text/html;charset=utf-8,'+ testName, 1.131 + onShow: function() { 1.132 + assert.pass('onShow works for Sidebar'); 1.133 + loader.unload(); 1.134 + 1.135 + let sidebarMI = getSidebarMenuitems(); 1.136 + for (let mi of sidebarMI) { 1.137 + assert.ok(BUILTIN_SIDEBAR_MENUITEMS.indexOf(mi.getAttribute('id')) >= 0, 'the menuitem is for a built-in sidebar') 1.138 + assert.ok(!isChecked(mi), 'no sidebar menuitem is checked'); 1.139 + } 1.140 + assert.ok(!window.document.getElementById(makeID(testName)), 'sidebar id DNE'); 1.141 + assert.equal(isSidebarShowing(window), false, 'the sidebar is not showing'); 1.142 + 1.143 + done(); 1.144 + } 1.145 + }) 1.146 + 1.147 + sidebar.show(); 1.148 + assert.pass('showing the sidebar'); 1.149 + }).then(null, assert.fail); 1.150 + }).then(null, assert.fail); 1.151 +} 1.152 + 1.153 +exports.testShowInPrivateWindow = function(assert, done) { 1.154 + const { Sidebar } = require('sdk/ui/sidebar'); 1.155 + let testName = 'testShowInPrivateWindow'; 1.156 + let window1 = getMostRecentBrowserWindow(); 1.157 + let url = 'data:text/html;charset=utf-8,'+testName; 1.158 + 1.159 + let sidebar1 = Sidebar({ 1.160 + id: testName, 1.161 + title: testName, 1.162 + url: url 1.163 + }); 1.164 + let menuitemID = makeID(sidebar1.id); 1.165 + 1.166 + assert.equal(sidebar1.url, url, 'url getter works'); 1.167 + assert.equal(isShowing(sidebar1), false, 'the sidebar is not showing'); 1.168 + assert.ok(!isChecked(window1.document.getElementById(menuitemID)), 1.169 + 'the menuitem is not checked'); 1.170 + assert.equal(isSidebarShowing(window1), false, 'the new window sidebar is not showing'); 1.171 + 1.172 + windowPromise(window1.OpenBrowserWindow({ private: true }), 'load').then(function(window) { 1.173 + let { document } = window; 1.174 + assert.equal(isWindowPrivate(window), true, 'new window is private'); 1.175 + assert.equal(isPrivate(window), true, 'new window is private'); 1.176 + 1.177 + sidebar1.show().then( 1.178 + function good() { 1.179 + assert.equal(isShowing(sidebar1), true, 'the sidebar is showing'); 1.180 + assert.ok(!!document.getElementById(menuitemID), 1.181 + 'the menuitem exists on the private window'); 1.182 + assert.equal(isSidebarShowing(window), true, 'the new window sidebar is showing'); 1.183 + 1.184 + sidebar1.destroy(); 1.185 + assert.equal(isSidebarShowing(window), false, 'the new window sidebar is showing'); 1.186 + assert.ok(!window1.document.getElementById(menuitemID), 1.187 + 'the menuitem on the new window dne'); 1.188 + 1.189 + // test old window state 1.190 + assert.equal(isSidebarShowing(window1), false, 'the old window sidebar is not showing'); 1.191 + assert.equal(window1.document.getElementById(menuitemID), 1.192 + null, 1.193 + 'the menuitem on the old window dne'); 1.194 + 1.195 + close(window).then(done).then(null, assert.fail); 1.196 + }, 1.197 + function bad() { 1.198 + assert.fail('a successful show should not happen here..'); 1.199 + }); 1.200 + }).then(null, assert.fail); 1.201 +} 1.202 + 1.203 +// If the module doesn't support the app we're being run in, require() will 1.204 +// throw. In that case, remove all tests above from exports, and add one dummy 1.205 +// test that passes. 1.206 +try { 1.207 + require('sdk/ui/sidebar'); 1.208 +} 1.209 +catch (err) { 1.210 + if (!/^Unsupported Application/.test(err.message)) 1.211 + throw err; 1.212 + 1.213 + module.exports = { 1.214 + 'test Unsupported Application': assert => assert.pass(err.message) 1.215 + } 1.216 +}