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