addon-sdk/source/test/test-ui-sidebar-private-browsing.js

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4 'use strict';
michael@0 5
michael@0 6 module.metadata = {
michael@0 7 'engines': {
michael@0 8 'Firefox': '*'
michael@0 9 }
michael@0 10 };
michael@0 11
michael@0 12 const { Loader } = require('sdk/test/loader');
michael@0 13 const { show, hide } = require('sdk/ui/sidebar/actions');
michael@0 14 const { isShowing } = require('sdk/ui/sidebar/utils');
michael@0 15 const { getMostRecentBrowserWindow, isWindowPrivate } = require('sdk/window/utils');
michael@0 16 const { open, close, focus, promise: windowPromise } = require('sdk/window/helpers');
michael@0 17 const { setTimeout } = require('sdk/timers');
michael@0 18 const { isPrivate } = require('sdk/private-browsing');
michael@0 19 const { data } = require('sdk/self');
michael@0 20 const { URL } = require('sdk/url');
michael@0 21
michael@0 22 const { BUILTIN_SIDEBAR_MENUITEMS, isSidebarShowing,
michael@0 23 getSidebarMenuitems, getExtraSidebarMenuitems, makeID, simulateCommand,
michael@0 24 simulateClick, isChecked } = require('./sidebar/utils');
michael@0 25
michael@0 26 exports.testSideBarIsNotInNewPrivateWindows = function(assert, done) {
michael@0 27 const { Sidebar } = require('sdk/ui/sidebar');
michael@0 28 let testName = 'testSideBarIsNotInNewPrivateWindows';
michael@0 29 let sidebar = Sidebar({
michael@0 30 id: testName,
michael@0 31 title: testName,
michael@0 32 url: 'data:text/html;charset=utf-8,'+testName
michael@0 33 });
michael@0 34
michael@0 35 let startWindow = getMostRecentBrowserWindow();
michael@0 36 let ele = startWindow.document.getElementById(makeID(testName));
michael@0 37 assert.ok(ele, 'sidebar element was added');
michael@0 38
michael@0 39 open(null, { features: { private: true } }).then(function(window) {
michael@0 40 let ele = window.document.getElementById(makeID(testName));
michael@0 41 assert.ok(isPrivate(window), 'the new window is private');
michael@0 42 assert.equal(ele, null, 'sidebar element was not added');
michael@0 43
michael@0 44 sidebar.destroy();
michael@0 45 assert.ok(!window.document.getElementById(makeID(testName)), 'sidebar id DNE');
michael@0 46 assert.ok(!startWindow.document.getElementById(makeID(testName)), 'sidebar id DNE');
michael@0 47
michael@0 48 close(window).then(done, assert.fail);
michael@0 49 })
michael@0 50 }
michael@0 51
michael@0 52 exports.testSidebarIsNotOpenInNewPrivateWindow = function(assert, done) {
michael@0 53 const { Sidebar } = require('sdk/ui/sidebar');
michael@0 54 let testName = 'testSidebarIsNotOpenInNewPrivateWindow';
michael@0 55 let window = getMostRecentBrowserWindow();
michael@0 56
michael@0 57 let sidebar = Sidebar({
michael@0 58 id: testName,
michael@0 59 title: testName,
michael@0 60 url: 'data:text/html;charset=utf-8,'+testName
michael@0 61 });
michael@0 62
michael@0 63 sidebar.on('show', function() {
michael@0 64 assert.equal(isPrivate(window), false, 'the new window is not private');
michael@0 65 assert.equal(isSidebarShowing(window), true, 'the sidebar is showing');
michael@0 66 assert.equal(isShowing(sidebar), true, 'the sidebar is showing');
michael@0 67
michael@0 68 let window2 = window.OpenBrowserWindow({private: true});
michael@0 69 windowPromise(window2, 'load').then(focus).then(function() {
michael@0 70 // TODO: find better alt to setTimeout...
michael@0 71 setTimeout(function() {
michael@0 72 assert.equal(isPrivate(window2), true, 'the new window is private');
michael@0 73 assert.equal(isSidebarShowing(window), true, 'the sidebar is showing in old window still');
michael@0 74 assert.equal(isSidebarShowing(window2), false, 'the sidebar is not showing in the new private window');
michael@0 75 assert.equal(isShowing(sidebar), false, 'the sidebar is not showing');
michael@0 76
michael@0 77 sidebar.destroy();
michael@0 78 close(window2).then(done);
michael@0 79 }, 500);
michael@0 80 })
michael@0 81 });
michael@0 82
michael@0 83 sidebar.show();
michael@0 84 }
michael@0 85
michael@0 86 // TEST: edge case where web panel is destroyed while loading
michael@0 87 exports.testDestroyEdgeCaseBugWithPrivateWindow = function(assert, done) {
michael@0 88 const { Sidebar } = require('sdk/ui/sidebar');
michael@0 89 let testName = 'testDestroyEdgeCaseBug';
michael@0 90 let window = getMostRecentBrowserWindow();
michael@0 91
michael@0 92 let sidebar = Sidebar({
michael@0 93 id: testName,
michael@0 94 title: testName,
michael@0 95 url: 'data:text/html;charset=utf-8,'+testName
michael@0 96 });
michael@0 97
michael@0 98 // NOTE: purposely not listening to show event b/c the event happens
michael@0 99 // between now and then.
michael@0 100 sidebar.show();
michael@0 101
michael@0 102 assert.equal(isPrivate(window), false, 'the new window is not private');
michael@0 103 assert.equal(isSidebarShowing(window), true, 'the sidebar is showing');
michael@0 104
michael@0 105 //assert.equal(isShowing(sidebar), true, 'the sidebar is showing');
michael@0 106
michael@0 107 open(null, { features: { private: true } }).then(focus).then(function(window2) {
michael@0 108 assert.equal(isPrivate(window2), true, 'the new window is private');
michael@0 109 assert.equal(isSidebarShowing(window2), false, 'the sidebar is not showing');
michael@0 110 assert.equal(isShowing(sidebar), false, 'the sidebar is not showing');
michael@0 111
michael@0 112 sidebar.destroy();
michael@0 113 assert.pass('destroying the sidebar');
michael@0 114
michael@0 115 close(window2).then(function() {
michael@0 116 let loader = Loader(module);
michael@0 117
michael@0 118 assert.equal(isPrivate(window), false, 'the current window is not private');
michael@0 119
michael@0 120 let sidebar = loader.require('sdk/ui/sidebar').Sidebar({
michael@0 121 id: testName,
michael@0 122 title: testName,
michael@0 123 url: 'data:text/html;charset=utf-8,'+ testName,
michael@0 124 onShow: function() {
michael@0 125 assert.pass('onShow works for Sidebar');
michael@0 126 loader.unload();
michael@0 127
michael@0 128 let sidebarMI = getSidebarMenuitems();
michael@0 129 for (let mi of sidebarMI) {
michael@0 130 assert.ok(BUILTIN_SIDEBAR_MENUITEMS.indexOf(mi.getAttribute('id')) >= 0, 'the menuitem is for a built-in sidebar')
michael@0 131 assert.ok(!isChecked(mi), 'no sidebar menuitem is checked');
michael@0 132 }
michael@0 133 assert.ok(!window.document.getElementById(makeID(testName)), 'sidebar id DNE');
michael@0 134 assert.equal(isSidebarShowing(window), false, 'the sidebar is not showing');
michael@0 135
michael@0 136 done();
michael@0 137 }
michael@0 138 })
michael@0 139
michael@0 140 sidebar.show();
michael@0 141 assert.pass('showing the sidebar');
michael@0 142
michael@0 143 });
michael@0 144 });
michael@0 145 }
michael@0 146
michael@0 147 exports.testShowInPrivateWindow = function(assert, done) {
michael@0 148 const { Sidebar } = require('sdk/ui/sidebar');
michael@0 149 let testName = 'testShowInPrivateWindow';
michael@0 150 let window = getMostRecentBrowserWindow();
michael@0 151 let { document } = window;
michael@0 152 let url = 'data:text/html;charset=utf-8,'+testName;
michael@0 153
michael@0 154 let sidebar1 = Sidebar({
michael@0 155 id: testName,
michael@0 156 title: testName,
michael@0 157 url: url
michael@0 158 });
michael@0 159
michael@0 160 assert.equal(sidebar1.url, url, 'url getter works');
michael@0 161 assert.equal(isShowing(sidebar1), false, 'the sidebar is not showing');
michael@0 162 assert.ok(!isChecked(document.getElementById(makeID(sidebar1.id))),
michael@0 163 'the menuitem is not checked');
michael@0 164 assert.equal(isSidebarShowing(window), false, 'the new window sidebar is not showing');
michael@0 165
michael@0 166 windowPromise(window.OpenBrowserWindow({ private: true }), 'load').then(function(window) {
michael@0 167 let { document } = window;
michael@0 168 assert.equal(isWindowPrivate(window), true, 'new window is private');
michael@0 169 assert.equal(isPrivate(window), true, 'new window is private');
michael@0 170
michael@0 171 sidebar1.show().then(
michael@0 172 function bad() {
michael@0 173 assert.fail('a successful show should not happen here..');
michael@0 174 },
michael@0 175 function good() {
michael@0 176 assert.equal(isShowing(sidebar1), false, 'the sidebar is still not showing');
michael@0 177 assert.equal(document.getElementById(makeID(sidebar1.id)),
michael@0 178 null,
michael@0 179 'the menuitem dne on the private window');
michael@0 180 assert.equal(isSidebarShowing(window), false, 'the new window sidebar is not showing');
michael@0 181
michael@0 182 sidebar1.destroy();
michael@0 183 close(window).then(done);
michael@0 184 });
michael@0 185 }, assert.fail);
michael@0 186 }
michael@0 187
michael@0 188 // If the module doesn't support the app we're being run in, require() will
michael@0 189 // throw. In that case, remove all tests above from exports, and add one dummy
michael@0 190 // test that passes.
michael@0 191 try {
michael@0 192 require('sdk/ui/sidebar');
michael@0 193 }
michael@0 194 catch (err) {
michael@0 195 if (!/^Unsupported Application/.test(err.message))
michael@0 196 throw err;
michael@0 197
michael@0 198 module.exports = {
michael@0 199 'test Unsupported Application': assert => assert.pass(err.message)
michael@0 200 }
michael@0 201 }
michael@0 202
michael@0 203 require('sdk/test').run(exports);

mercurial