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: module.metadata = { michael@0: 'engines': { michael@0: 'Firefox': '*' michael@0: } michael@0: }; michael@0: michael@0: const { Loader } = require('sdk/test/loader'); michael@0: const { show, hide } = require('sdk/ui/sidebar/actions'); michael@0: const { isShowing } = require('sdk/ui/sidebar/utils'); michael@0: const { getMostRecentBrowserWindow, isWindowPrivate } = require('sdk/window/utils'); michael@0: const { open, close, focus, promise: windowPromise } = require('sdk/window/helpers'); michael@0: const { setTimeout } = require('sdk/timers'); michael@0: const { isPrivate } = require('sdk/private-browsing'); michael@0: const { data } = require('sdk/self'); michael@0: const { URL } = require('sdk/url'); michael@0: michael@0: const { BUILTIN_SIDEBAR_MENUITEMS, isSidebarShowing, michael@0: getSidebarMenuitems, getExtraSidebarMenuitems, makeID, simulateCommand, michael@0: simulateClick, isChecked } = require('./sidebar/utils'); michael@0: michael@0: exports.testSideBarIsNotInNewPrivateWindows = function(assert, done) { michael@0: const { Sidebar } = require('sdk/ui/sidebar'); michael@0: let testName = 'testSideBarIsNotInNewPrivateWindows'; michael@0: let sidebar = Sidebar({ michael@0: id: testName, michael@0: title: testName, michael@0: url: 'data:text/html;charset=utf-8,'+testName michael@0: }); michael@0: michael@0: let startWindow = getMostRecentBrowserWindow(); michael@0: let ele = startWindow.document.getElementById(makeID(testName)); michael@0: assert.ok(ele, 'sidebar element was added'); michael@0: michael@0: open(null, { features: { private: true } }).then(function(window) { michael@0: let ele = window.document.getElementById(makeID(testName)); michael@0: assert.ok(isPrivate(window), 'the new window is private'); michael@0: assert.equal(ele, null, 'sidebar element was not added'); michael@0: michael@0: sidebar.destroy(); michael@0: assert.ok(!window.document.getElementById(makeID(testName)), 'sidebar id DNE'); michael@0: assert.ok(!startWindow.document.getElementById(makeID(testName)), 'sidebar id DNE'); michael@0: michael@0: close(window).then(done, assert.fail); michael@0: }) michael@0: } michael@0: michael@0: exports.testSidebarIsNotOpenInNewPrivateWindow = function(assert, done) { michael@0: const { Sidebar } = require('sdk/ui/sidebar'); michael@0: let testName = 'testSidebarIsNotOpenInNewPrivateWindow'; michael@0: let window = getMostRecentBrowserWindow(); michael@0: michael@0: let sidebar = Sidebar({ michael@0: id: testName, michael@0: title: testName, michael@0: url: 'data:text/html;charset=utf-8,'+testName michael@0: }); michael@0: michael@0: sidebar.on('show', function() { michael@0: assert.equal(isPrivate(window), false, 'the new window is not private'); michael@0: assert.equal(isSidebarShowing(window), true, 'the sidebar is showing'); michael@0: assert.equal(isShowing(sidebar), true, 'the sidebar is showing'); michael@0: michael@0: let window2 = window.OpenBrowserWindow({private: true}); michael@0: windowPromise(window2, 'load').then(focus).then(function() { michael@0: // TODO: find better alt to setTimeout... michael@0: setTimeout(function() { michael@0: assert.equal(isPrivate(window2), true, 'the new window is private'); michael@0: assert.equal(isSidebarShowing(window), true, 'the sidebar is showing in old window still'); michael@0: assert.equal(isSidebarShowing(window2), false, 'the sidebar is not showing in the new private window'); michael@0: assert.equal(isShowing(sidebar), false, 'the sidebar is not showing'); michael@0: michael@0: sidebar.destroy(); michael@0: close(window2).then(done); michael@0: }, 500); michael@0: }) michael@0: }); michael@0: michael@0: sidebar.show(); michael@0: } michael@0: michael@0: // TEST: edge case where web panel is destroyed while loading michael@0: exports.testDestroyEdgeCaseBugWithPrivateWindow = function(assert, done) { michael@0: const { Sidebar } = require('sdk/ui/sidebar'); michael@0: let testName = 'testDestroyEdgeCaseBug'; michael@0: let window = getMostRecentBrowserWindow(); michael@0: michael@0: let sidebar = Sidebar({ michael@0: id: testName, michael@0: title: testName, michael@0: url: 'data:text/html;charset=utf-8,'+testName michael@0: }); michael@0: michael@0: // NOTE: purposely not listening to show event b/c the event happens michael@0: // between now and then. michael@0: sidebar.show(); michael@0: michael@0: assert.equal(isPrivate(window), false, 'the new window is not private'); michael@0: assert.equal(isSidebarShowing(window), true, 'the sidebar is showing'); michael@0: michael@0: //assert.equal(isShowing(sidebar), true, 'the sidebar is showing'); michael@0: michael@0: open(null, { features: { private: true } }).then(focus).then(function(window2) { michael@0: assert.equal(isPrivate(window2), true, 'the new window is private'); michael@0: assert.equal(isSidebarShowing(window2), false, 'the sidebar is not showing'); michael@0: assert.equal(isShowing(sidebar), false, 'the sidebar is not showing'); michael@0: michael@0: sidebar.destroy(); michael@0: assert.pass('destroying the sidebar'); michael@0: michael@0: close(window2).then(function() { michael@0: let loader = Loader(module); michael@0: michael@0: assert.equal(isPrivate(window), false, 'the current window is not private'); michael@0: michael@0: let sidebar = loader.require('sdk/ui/sidebar').Sidebar({ michael@0: id: testName, michael@0: title: testName, michael@0: url: 'data:text/html;charset=utf-8,'+ testName, michael@0: onShow: function() { michael@0: assert.pass('onShow works for Sidebar'); michael@0: loader.unload(); michael@0: michael@0: let sidebarMI = getSidebarMenuitems(); michael@0: for (let mi of sidebarMI) { michael@0: assert.ok(BUILTIN_SIDEBAR_MENUITEMS.indexOf(mi.getAttribute('id')) >= 0, 'the menuitem is for a built-in sidebar') michael@0: assert.ok(!isChecked(mi), 'no sidebar menuitem is checked'); michael@0: } michael@0: assert.ok(!window.document.getElementById(makeID(testName)), 'sidebar id DNE'); michael@0: assert.equal(isSidebarShowing(window), false, 'the sidebar is not showing'); michael@0: michael@0: done(); michael@0: } michael@0: }) michael@0: michael@0: sidebar.show(); michael@0: assert.pass('showing the sidebar'); michael@0: michael@0: }); michael@0: }); michael@0: } michael@0: michael@0: exports.testShowInPrivateWindow = function(assert, done) { michael@0: const { Sidebar } = require('sdk/ui/sidebar'); michael@0: let testName = 'testShowInPrivateWindow'; michael@0: let window = getMostRecentBrowserWindow(); michael@0: let { document } = window; michael@0: let url = 'data:text/html;charset=utf-8,'+testName; michael@0: michael@0: let sidebar1 = Sidebar({ michael@0: id: testName, michael@0: title: testName, michael@0: url: url michael@0: }); michael@0: michael@0: assert.equal(sidebar1.url, url, 'url getter works'); michael@0: assert.equal(isShowing(sidebar1), false, 'the sidebar is not showing'); michael@0: assert.ok(!isChecked(document.getElementById(makeID(sidebar1.id))), michael@0: 'the menuitem is not checked'); michael@0: assert.equal(isSidebarShowing(window), false, 'the new window sidebar is not showing'); michael@0: michael@0: windowPromise(window.OpenBrowserWindow({ private: true }), 'load').then(function(window) { michael@0: let { document } = window; michael@0: assert.equal(isWindowPrivate(window), true, 'new window is private'); michael@0: assert.equal(isPrivate(window), true, 'new window is private'); michael@0: michael@0: sidebar1.show().then( michael@0: function bad() { michael@0: assert.fail('a successful show should not happen here..'); michael@0: }, michael@0: function good() { michael@0: assert.equal(isShowing(sidebar1), false, 'the sidebar is still not showing'); michael@0: assert.equal(document.getElementById(makeID(sidebar1.id)), michael@0: null, michael@0: 'the menuitem dne on the private window'); michael@0: assert.equal(isSidebarShowing(window), false, 'the new window sidebar is not showing'); michael@0: michael@0: sidebar1.destroy(); michael@0: close(window).then(done); michael@0: }); michael@0: }, assert.fail); michael@0: } michael@0: michael@0: // If the module doesn't support the app we're being run in, require() will michael@0: // throw. In that case, remove all tests above from exports, and add one dummy michael@0: // test that passes. michael@0: try { michael@0: require('sdk/ui/sidebar'); michael@0: } michael@0: catch (err) { michael@0: if (!/^Unsupported Application/.test(err.message)) michael@0: throw err; michael@0: michael@0: module.exports = { michael@0: 'test Unsupported Application': assert => assert.pass(err.message) michael@0: } michael@0: } michael@0: michael@0: require('sdk/test').run(exports);