Sat, 03 Jan 2015 20:18:00 +0100
Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.
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 { Cu } = require('chrome'); |
michael@0 | 13 | const { Loader } = require('sdk/test/loader'); |
michael@0 | 14 | const { show, hide } = require('sdk/ui/sidebar/actions'); |
michael@0 | 15 | const { isShowing } = require('sdk/ui/sidebar/utils'); |
michael@0 | 16 | const { getMostRecentBrowserWindow } = require('sdk/window/utils'); |
michael@0 | 17 | const { open, close, focus, promise: windowPromise } = require('sdk/window/helpers'); |
michael@0 | 18 | const { setTimeout, setImmediate } = require('sdk/timers'); |
michael@0 | 19 | const { isPrivate } = require('sdk/private-browsing'); |
michael@0 | 20 | const data = require('./fixtures'); |
michael@0 | 21 | const { URL } = require('sdk/url'); |
michael@0 | 22 | const { once, off, emit } = require('sdk/event/core'); |
michael@0 | 23 | const { defer, all } = require('sdk/core/promise'); |
michael@0 | 24 | |
michael@0 | 25 | const { BUILTIN_SIDEBAR_MENUITEMS, isSidebarShowing, |
michael@0 | 26 | getSidebarMenuitems, getExtraSidebarMenuitems, makeID, simulateCommand, |
michael@0 | 27 | simulateClick, isChecked } = require('./sidebar/utils'); |
michael@0 | 28 | |
michael@0 | 29 | exports.testSidebarBasicLifeCycle = function(assert, done) { |
michael@0 | 30 | const { Sidebar } = require('sdk/ui/sidebar'); |
michael@0 | 31 | let testName = 'testSidebarBasicLifeCycle'; |
michael@0 | 32 | let window = getMostRecentBrowserWindow(); |
michael@0 | 33 | assert.ok(!window.document.getElementById(makeID(testName)), 'sidebar id DNE'); |
michael@0 | 34 | let sidebarXUL = window.document.getElementById('sidebar'); |
michael@0 | 35 | assert.ok(sidebarXUL, 'sidebar xul element does exist'); |
michael@0 | 36 | assert.ok(!getExtraSidebarMenuitems().length, 'there are no extra sidebar menuitems'); |
michael@0 | 37 | |
michael@0 | 38 | assert.equal(isSidebarShowing(window), false, 'sidebar is not showing 1'); |
michael@0 | 39 | let sidebarDetails = { |
michael@0 | 40 | id: testName, |
michael@0 | 41 | title: 'test', |
michael@0 | 42 | url: 'data:text/html;charset=utf-8,'+testName |
michael@0 | 43 | }; |
michael@0 | 44 | let sidebar = Sidebar(sidebarDetails); |
michael@0 | 45 | |
michael@0 | 46 | // test the sidebar attributes |
michael@0 | 47 | for (let key of Object.keys(sidebarDetails)) { |
michael@0 | 48 | assert.equal(sidebarDetails[key], sidebar[key], 'the attributes match the input'); |
michael@0 | 49 | } |
michael@0 | 50 | |
michael@0 | 51 | assert.pass('The Sidebar constructor worked'); |
michael@0 | 52 | |
michael@0 | 53 | let extraMenuitems = getExtraSidebarMenuitems(); |
michael@0 | 54 | assert.equal(extraMenuitems.length, 1, 'there is one extra sidebar menuitems'); |
michael@0 | 55 | |
michael@0 | 56 | let ele = window.document.getElementById(makeID(testName)); |
michael@0 | 57 | assert.equal(ele, extraMenuitems[0], 'the only extra menuitem is the one for our sidebar.') |
michael@0 | 58 | assert.ok(ele, 'sidebar element was added'); |
michael@0 | 59 | assert.ok(!isChecked(ele), 'the sidebar is not displayed'); |
michael@0 | 60 | assert.equal(ele.getAttribute('label'), sidebar.title, 'the sidebar title is the menuitem label') |
michael@0 | 61 | |
michael@0 | 62 | assert.equal(isSidebarShowing(window), false, 'sidebar is not showing 2'); |
michael@0 | 63 | sidebar.on('show', function() { |
michael@0 | 64 | assert.pass('the show event was fired'); |
michael@0 | 65 | assert.equal(isSidebarShowing(window), true, 'sidebar is not showing 3'); |
michael@0 | 66 | assert.equal(isShowing(sidebar), true, 'the sidebar is showing'); |
michael@0 | 67 | assert.ok(isChecked(ele), 'the sidebar is displayed'); |
michael@0 | 68 | |
michael@0 | 69 | sidebar.once('hide', function() { |
michael@0 | 70 | assert.pass('the hide event was fired'); |
michael@0 | 71 | assert.ok(!isChecked(ele), 'the sidebar menuitem is not checked'); |
michael@0 | 72 | assert.equal(isShowing(sidebar), false, 'the sidebar is not showing'); |
michael@0 | 73 | assert.equal(isSidebarShowing(window), false, 'the sidebar elemnt is hidden'); |
michael@0 | 74 | |
michael@0 | 75 | sidebar.once('detach', function() { |
michael@0 | 76 | // calling destroy twice should not matter |
michael@0 | 77 | sidebar.destroy(); |
michael@0 | 78 | sidebar.destroy(); |
michael@0 | 79 | |
michael@0 | 80 | let sidebarMI = getSidebarMenuitems(); |
michael@0 | 81 | for (let mi of sidebarMI) { |
michael@0 | 82 | assert.ok(BUILTIN_SIDEBAR_MENUITEMS.indexOf(mi.getAttribute('id')) >= 0, 'the menuitem is for a built-in sidebar') |
michael@0 | 83 | assert.ok(!isChecked(mi), 'no sidebar menuitem is checked'); |
michael@0 | 84 | } |
michael@0 | 85 | |
michael@0 | 86 | assert.ok(!window.document.getElementById(makeID(testName)), 'sidebar id DNE'); |
michael@0 | 87 | assert.pass('calling destroy worked without error'); |
michael@0 | 88 | |
michael@0 | 89 | done(); |
michael@0 | 90 | }); |
michael@0 | 91 | }); |
michael@0 | 92 | |
michael@0 | 93 | sidebar.hide(); |
michael@0 | 94 | assert.pass('hiding sidebar..'); |
michael@0 | 95 | }); |
michael@0 | 96 | |
michael@0 | 97 | sidebar.show(); |
michael@0 | 98 | assert.pass('showing sidebar..'); |
michael@0 | 99 | } |
michael@0 | 100 | |
michael@0 | 101 | exports.testSideBarIsInNewWindows = function(assert, done) { |
michael@0 | 102 | const { Sidebar } = require('sdk/ui/sidebar'); |
michael@0 | 103 | let testName = 'testSideBarIsInNewWindows'; |
michael@0 | 104 | let sidebar = Sidebar({ |
michael@0 | 105 | id: testName, |
michael@0 | 106 | title: testName, |
michael@0 | 107 | url: 'data:text/html;charset=utf-8,'+testName |
michael@0 | 108 | }); |
michael@0 | 109 | |
michael@0 | 110 | let startWindow = getMostRecentBrowserWindow(); |
michael@0 | 111 | let ele = startWindow.document.getElementById(makeID(testName)); |
michael@0 | 112 | assert.ok(ele, 'sidebar element was added'); |
michael@0 | 113 | |
michael@0 | 114 | open().then(function(window) { |
michael@0 | 115 | let ele = window.document.getElementById(makeID(testName)); |
michael@0 | 116 | assert.ok(ele, 'sidebar element was added'); |
michael@0 | 117 | |
michael@0 | 118 | // calling destroy twice should not matter |
michael@0 | 119 | sidebar.destroy(); |
michael@0 | 120 | sidebar.destroy(); |
michael@0 | 121 | |
michael@0 | 122 | assert.ok(!window.document.getElementById(makeID(testName)), 'sidebar id DNE'); |
michael@0 | 123 | assert.ok(!startWindow.document.getElementById(makeID(testName)), 'sidebar id DNE'); |
michael@0 | 124 | |
michael@0 | 125 | close(window).then(done, assert.fail); |
michael@0 | 126 | }) |
michael@0 | 127 | } |
michael@0 | 128 | |
michael@0 | 129 | exports.testSideBarIsShowingInNewWindows = function(assert, done) { |
michael@0 | 130 | const { Sidebar } = require('sdk/ui/sidebar'); |
michael@0 | 131 | let testName = 'testSideBarIsShowingInNewWindows'; |
michael@0 | 132 | let sidebar = Sidebar({ |
michael@0 | 133 | id: testName, |
michael@0 | 134 | title: testName, |
michael@0 | 135 | url: URL('data:text/html;charset=utf-8,'+testName) |
michael@0 | 136 | }); |
michael@0 | 137 | |
michael@0 | 138 | let startWindow = getMostRecentBrowserWindow(); |
michael@0 | 139 | let ele = startWindow.document.getElementById(makeID(testName)); |
michael@0 | 140 | assert.ok(ele, 'sidebar element was added'); |
michael@0 | 141 | |
michael@0 | 142 | let oldEle = ele; |
michael@0 | 143 | sidebar.once('attach', function() { |
michael@0 | 144 | assert.pass('attach event fired'); |
michael@0 | 145 | |
michael@0 | 146 | sidebar.once('show', function() { |
michael@0 | 147 | assert.pass('show event fired'); |
michael@0 | 148 | |
michael@0 | 149 | sidebar.once('show', function() { |
michael@0 | 150 | let window = getMostRecentBrowserWindow(); |
michael@0 | 151 | assert.notEqual(startWindow, window, 'window is new'); |
michael@0 | 152 | |
michael@0 | 153 | let sb = window.document.getElementById('sidebar'); |
michael@0 | 154 | if (sb && sb.docShell && sb.contentDocument && sb.contentDocument.getElementById('web-panels-browser')) { |
michael@0 | 155 | end(); |
michael@0 | 156 | } |
michael@0 | 157 | else { |
michael@0 | 158 | sb.addEventListener('DOMWindowCreated', end, false); |
michael@0 | 159 | } |
michael@0 | 160 | |
michael@0 | 161 | function end() { |
michael@0 | 162 | sb.removeEventListener('DOMWindowCreated', end, false); |
michael@0 | 163 | let webPanelBrowser = sb.contentDocument.getElementById('web-panels-browser'); |
michael@0 | 164 | |
michael@0 | 165 | let ele = window.document.getElementById(makeID(testName)); |
michael@0 | 166 | |
michael@0 | 167 | assert.ok(ele, 'sidebar element was added 2'); |
michael@0 | 168 | assert.ok(isChecked(ele), 'the sidebar is checked'); |
michael@0 | 169 | assert.notEqual(ele, oldEle, 'there are two different sidebars'); |
michael@0 | 170 | |
michael@0 | 171 | assert.equal(isShowing(sidebar), true, 'the sidebar is showing in new window'); |
michael@0 | 172 | |
michael@0 | 173 | |
michael@0 | 174 | sidebar.destroy(); |
michael@0 | 175 | |
michael@0 | 176 | assert.equal(isShowing(sidebar), false, 'the sidebar is not showing'); |
michael@0 | 177 | assert.ok(!isSidebarShowing(window), 'sidebar in most recent window is not showing'); |
michael@0 | 178 | assert.ok(!isSidebarShowing(startWindow), 'sidebar in most start window is not showing'); |
michael@0 | 179 | assert.ok(!window.document.getElementById(makeID(testName)), 'sidebar id DNE'); |
michael@0 | 180 | assert.ok(!startWindow.document.getElementById(makeID(testName)), 'sidebar id DNE'); |
michael@0 | 181 | |
michael@0 | 182 | setTimeout(function() { |
michael@0 | 183 | close(window).then(done, assert.fail); |
michael@0 | 184 | }); |
michael@0 | 185 | } |
michael@0 | 186 | }); |
michael@0 | 187 | |
michael@0 | 188 | startWindow.OpenBrowserWindow(); |
michael@0 | 189 | }); |
michael@0 | 190 | }); |
michael@0 | 191 | |
michael@0 | 192 | show(sidebar); |
michael@0 | 193 | assert.pass('showing the sidebar'); |
michael@0 | 194 | } |
michael@0 | 195 | |
michael@0 | 196 | // TODO: determine if this is acceptable.. |
michael@0 | 197 | /* |
michael@0 | 198 | exports.testAddonGlobalSimple = function(assert, done) { |
michael@0 | 199 | const { Sidebar } = require('sdk/ui/sidebar'); |
michael@0 | 200 | let testName = 'testAddonGlobalSimple'; |
michael@0 | 201 | let sidebar = Sidebar({ |
michael@0 | 202 | id: testName, |
michael@0 | 203 | title: testName, |
michael@0 | 204 | url: data.url('test-sidebar-addon-global.html') |
michael@0 | 205 | }); |
michael@0 | 206 | |
michael@0 | 207 | sidebar.on('show', function({worker}) { |
michael@0 | 208 | assert.pass('sidebar was attached'); |
michael@0 | 209 | assert.ok(!!worker, 'attach event has worker'); |
michael@0 | 210 | |
michael@0 | 211 | worker.port.on('X', function(msg) { |
michael@0 | 212 | assert.equal(msg, '23', 'the final message is correct'); |
michael@0 | 213 | |
michael@0 | 214 | sidebar.destroy(); |
michael@0 | 215 | |
michael@0 | 216 | done(); |
michael@0 | 217 | }); |
michael@0 | 218 | worker.port.emit('X', '2'); |
michael@0 | 219 | }); |
michael@0 | 220 | show(sidebar); |
michael@0 | 221 | } |
michael@0 | 222 | */ |
michael@0 | 223 | |
michael@0 | 224 | exports.testAddonGlobalComplex = function(assert, done) { |
michael@0 | 225 | const { Sidebar } = require('sdk/ui/sidebar'); |
michael@0 | 226 | let testName = 'testAddonGlobalComplex'; |
michael@0 | 227 | let sidebar = Sidebar({ |
michael@0 | 228 | id: testName, |
michael@0 | 229 | title: testName, |
michael@0 | 230 | url: data.url('test-sidebar-addon-global.html') |
michael@0 | 231 | }); |
michael@0 | 232 | |
michael@0 | 233 | sidebar.on('attach', function(worker) { |
michael@0 | 234 | assert.pass('sidebar was attached'); |
michael@0 | 235 | assert.ok(!!worker, 'attach event has worker'); |
michael@0 | 236 | |
michael@0 | 237 | worker.port.once('Y', function(msg) { |
michael@0 | 238 | assert.equal(msg, '1', 'got event from worker'); |
michael@0 | 239 | |
michael@0 | 240 | worker.port.on('X', function(msg) { |
michael@0 | 241 | assert.equal(msg, '123', 'the final message is correct'); |
michael@0 | 242 | |
michael@0 | 243 | sidebar.destroy(); |
michael@0 | 244 | |
michael@0 | 245 | done(); |
michael@0 | 246 | }); |
michael@0 | 247 | worker.port.emit('X', msg + '2'); |
michael@0 | 248 | }) |
michael@0 | 249 | }); |
michael@0 | 250 | |
michael@0 | 251 | show(sidebar); |
michael@0 | 252 | } |
michael@0 | 253 | |
michael@0 | 254 | exports.testAddonReady = function(assert, done) { |
michael@0 | 255 | const { Sidebar } = require('sdk/ui/sidebar'); |
michael@0 | 256 | let testName = 'testAddonReady'; |
michael@0 | 257 | let sidebar = Sidebar({ |
michael@0 | 258 | id: testName, |
michael@0 | 259 | title: testName, |
michael@0 | 260 | url: data.url('test-sidebar-addon-global.html'), |
michael@0 | 261 | onReady: function(worker) { |
michael@0 | 262 | assert.pass('sidebar was attached'); |
michael@0 | 263 | assert.ok(!!worker, 'attach event has worker'); |
michael@0 | 264 | |
michael@0 | 265 | worker.port.on('X', function(msg) { |
michael@0 | 266 | assert.equal(msg, '123', 'the final message is correct'); |
michael@0 | 267 | |
michael@0 | 268 | sidebar.destroy(); |
michael@0 | 269 | |
michael@0 | 270 | done(); |
michael@0 | 271 | }); |
michael@0 | 272 | |
michael@0 | 273 | worker.port.emit('X', '12'); |
michael@0 | 274 | } |
michael@0 | 275 | }); |
michael@0 | 276 | |
michael@0 | 277 | show(sidebar); |
michael@0 | 278 | } |
michael@0 | 279 | |
michael@0 | 280 | exports.testShowingOneSidebarAfterAnother = function(assert, done) { |
michael@0 | 281 | const { Sidebar } = require('sdk/ui/sidebar'); |
michael@0 | 282 | let testName = 'testShowingOneSidebarAfterAnother'; |
michael@0 | 283 | |
michael@0 | 284 | let sidebar1 = Sidebar({ |
michael@0 | 285 | id: testName + '1', |
michael@0 | 286 | title: testName + '1', |
michael@0 | 287 | url: 'data:text/html;charset=utf-8,'+ testName + 1 |
michael@0 | 288 | }); |
michael@0 | 289 | let sidebar2 = Sidebar({ |
michael@0 | 290 | id: testName + '2', |
michael@0 | 291 | title: testName + '2', |
michael@0 | 292 | url: 'data:text/html;charset=utf-8,'+ testName + 2 |
michael@0 | 293 | }); |
michael@0 | 294 | |
michael@0 | 295 | let window = getMostRecentBrowserWindow(); |
michael@0 | 296 | let IDs = [ sidebar1.id, sidebar2.id ]; |
michael@0 | 297 | |
michael@0 | 298 | let extraMenuitems = getExtraSidebarMenuitems(window); |
michael@0 | 299 | assert.equal(extraMenuitems.length, 2, 'there are two extra sidebar menuitems'); |
michael@0 | 300 | |
michael@0 | 301 | function testShowing(sb1, sb2, sbEle) { |
michael@0 | 302 | assert.equal(isShowing(sidebar1), sb1); |
michael@0 | 303 | assert.equal(isShowing(sidebar2), sb2); |
michael@0 | 304 | assert.equal(isSidebarShowing(window), sbEle); |
michael@0 | 305 | } |
michael@0 | 306 | testShowing(false, false, false); |
michael@0 | 307 | |
michael@0 | 308 | sidebar1.once('show', function() { |
michael@0 | 309 | testShowing(true, false, true); |
michael@0 | 310 | for (let mi of getExtraSidebarMenuitems(window)) { |
michael@0 | 311 | let menuitemID = mi.getAttribute('id').replace(/^jetpack-sidebar-/, ''); |
michael@0 | 312 | assert.ok(IDs.indexOf(menuitemID) >= 0, 'the extra menuitem is for one of our test sidebars'); |
michael@0 | 313 | assert.equal(isChecked(mi), menuitemID == sidebar1.id, 'the test sidebar menuitem has the correct checked value'); |
michael@0 | 314 | } |
michael@0 | 315 | |
michael@0 | 316 | sidebar2.once('show', function() { |
michael@0 | 317 | testShowing(false, true, true); |
michael@0 | 318 | for (let mi of getExtraSidebarMenuitems(window)) { |
michael@0 | 319 | let menuitemID = mi.getAttribute('id').replace(/^jetpack-sidebar-/, ''); |
michael@0 | 320 | assert.ok(IDs.indexOf(menuitemID) >= 0, 'the extra menuitem is for one of our test sidebars'); |
michael@0 | 321 | assert.equal(isChecked(mi), menuitemID == sidebar2.id, 'the test sidebar menuitem has the correct checked value'); |
michael@0 | 322 | } |
michael@0 | 323 | |
michael@0 | 324 | sidebar1.destroy(); |
michael@0 | 325 | sidebar2.destroy(); |
michael@0 | 326 | |
michael@0 | 327 | testShowing(false, false, false); |
michael@0 | 328 | |
michael@0 | 329 | done(); |
michael@0 | 330 | }); |
michael@0 | 331 | |
michael@0 | 332 | show(sidebar2); |
michael@0 | 333 | assert.pass('showing sidebar 2'); |
michael@0 | 334 | }) |
michael@0 | 335 | show(sidebar1); |
michael@0 | 336 | assert.pass('showing sidebar 1'); |
michael@0 | 337 | } |
michael@0 | 338 | |
michael@0 | 339 | exports.testSidebarUnload = function(assert, done) { |
michael@0 | 340 | const { Sidebar } = require('sdk/ui/sidebar'); |
michael@0 | 341 | let testName = 'testSidebarUnload'; |
michael@0 | 342 | let loader = Loader(module); |
michael@0 | 343 | |
michael@0 | 344 | let window = getMostRecentBrowserWindow(); |
michael@0 | 345 | |
michael@0 | 346 | assert.equal(isPrivate(window), false, 'the current window is not private'); |
michael@0 | 347 | |
michael@0 | 348 | let sidebar = loader.require('sdk/ui/sidebar').Sidebar({ |
michael@0 | 349 | id: testName, |
michael@0 | 350 | title: testName, |
michael@0 | 351 | url: 'data:text/html;charset=utf-8,'+ testName, |
michael@0 | 352 | onShow: function() { |
michael@0 | 353 | assert.pass('onShow works for Sidebar'); |
michael@0 | 354 | loader.unload(); |
michael@0 | 355 | |
michael@0 | 356 | let sidebarMI = getSidebarMenuitems(); |
michael@0 | 357 | for (let mi of sidebarMI) { |
michael@0 | 358 | assert.ok(BUILTIN_SIDEBAR_MENUITEMS.indexOf(mi.getAttribute('id')) >= 0, 'the menuitem is for a built-in sidebar') |
michael@0 | 359 | assert.ok(!isChecked(mi), 'no sidebar menuitem is checked'); |
michael@0 | 360 | } |
michael@0 | 361 | assert.ok(!window.document.getElementById(makeID(testName)), 'sidebar id DNE'); |
michael@0 | 362 | assert.equal(isSidebarShowing(window), false, 'the sidebar is not showing'); |
michael@0 | 363 | |
michael@0 | 364 | done(); |
michael@0 | 365 | } |
michael@0 | 366 | }) |
michael@0 | 367 | |
michael@0 | 368 | sidebar.show(); |
michael@0 | 369 | assert.pass('showing the sidebar'); |
michael@0 | 370 | } |
michael@0 | 371 | |
michael@0 | 372 | exports.testRemoteContent = function(assert) { |
michael@0 | 373 | const { Sidebar } = require('sdk/ui/sidebar'); |
michael@0 | 374 | let testName = 'testRemoteContent'; |
michael@0 | 375 | try { |
michael@0 | 376 | let sidebar = Sidebar({ |
michael@0 | 377 | id: testName, |
michael@0 | 378 | title: testName, |
michael@0 | 379 | url: 'http://dne.xyz.mozilla.org' |
michael@0 | 380 | }); |
michael@0 | 381 | assert.fail('a bad sidebar was created..'); |
michael@0 | 382 | sidebar.destroy(); |
michael@0 | 383 | } |
michael@0 | 384 | catch(e) { |
michael@0 | 385 | assert.ok(/The option "url" must be a valid local URI\./.test(e), 'remote content is not acceptable'); |
michael@0 | 386 | } |
michael@0 | 387 | } |
michael@0 | 388 | |
michael@0 | 389 | exports.testInvalidURL = function(assert) { |
michael@0 | 390 | const { Sidebar } = require('sdk/ui/sidebar'); |
michael@0 | 391 | let testName = 'testInvalidURL'; |
michael@0 | 392 | try { |
michael@0 | 393 | let sidebar = Sidebar({ |
michael@0 | 394 | id: testName, |
michael@0 | 395 | title: testName, |
michael@0 | 396 | url: 'http:mozilla.org' |
michael@0 | 397 | }); |
michael@0 | 398 | assert.fail('a bad sidebar was created..'); |
michael@0 | 399 | sidebar.destroy(); |
michael@0 | 400 | } |
michael@0 | 401 | catch(e) { |
michael@0 | 402 | assert.ok(/The option "url" must be a valid local URI\./.test(e), 'invalid URIs are not acceptable'); |
michael@0 | 403 | } |
michael@0 | 404 | } |
michael@0 | 405 | |
michael@0 | 406 | exports.testInvalidURLType = function(assert) { |
michael@0 | 407 | const { Sidebar } = require('sdk/ui/sidebar'); |
michael@0 | 408 | let testName = 'testInvalidURLType'; |
michael@0 | 409 | try { |
michael@0 | 410 | let sidebar = Sidebar({ |
michael@0 | 411 | id: testName, |
michael@0 | 412 | title: testName |
michael@0 | 413 | }); |
michael@0 | 414 | assert.fail('a bad sidebar was created..'); |
michael@0 | 415 | sidebar.destroy(); |
michael@0 | 416 | } |
michael@0 | 417 | catch(e) { |
michael@0 | 418 | assert.ok(/The option "url" must be a valid local URI\./.test(e), 'invalid URIs are not acceptable'); |
michael@0 | 419 | } |
michael@0 | 420 | } |
michael@0 | 421 | |
michael@0 | 422 | exports.testInvalidTitle = function(assert) { |
michael@0 | 423 | const { Sidebar } = require('sdk/ui/sidebar'); |
michael@0 | 424 | let testName = 'testInvalidTitle'; |
michael@0 | 425 | try { |
michael@0 | 426 | let sidebar = Sidebar({ |
michael@0 | 427 | id: testName, |
michael@0 | 428 | title: '', |
michael@0 | 429 | url: 'data:text/html;charset=utf-8,'+testName |
michael@0 | 430 | }); |
michael@0 | 431 | assert.fail('a bad sidebar was created..'); |
michael@0 | 432 | sidebar.destroy(); |
michael@0 | 433 | } |
michael@0 | 434 | catch(e) { |
michael@0 | 435 | assert.equal('The option "title" must be one of the following types: string', e.message, 'invalid titles are not acceptable'); |
michael@0 | 436 | } |
michael@0 | 437 | } |
michael@0 | 438 | |
michael@0 | 439 | exports.testInvalidID = function(assert) { |
michael@0 | 440 | const { Sidebar } = require('sdk/ui/sidebar'); |
michael@0 | 441 | let testName = 'testInvalidID'; |
michael@0 | 442 | try { |
michael@0 | 443 | let sidebar = Sidebar({ |
michael@0 | 444 | id: '!', |
michael@0 | 445 | title: testName, |
michael@0 | 446 | url: 'data:text/html;charset=utf-8,'+testName |
michael@0 | 447 | }); |
michael@0 | 448 | assert.fail('a bad sidebar was created..'); |
michael@0 | 449 | sidebar.destroy(); |
michael@0 | 450 | } |
michael@0 | 451 | catch(e) { |
michael@0 | 452 | assert.ok(/The option "id" must be a valid alphanumeric id/.test(e), 'invalid ids are not acceptable'); |
michael@0 | 453 | } |
michael@0 | 454 | } |
michael@0 | 455 | |
michael@0 | 456 | exports.testInvalidBlankID = function(assert) { |
michael@0 | 457 | const { Sidebar } = require('sdk/ui/sidebar'); |
michael@0 | 458 | let testName = 'testInvalidBlankID'; |
michael@0 | 459 | try { |
michael@0 | 460 | let sidebar = Sidebar({ |
michael@0 | 461 | id: '', |
michael@0 | 462 | title: testName, |
michael@0 | 463 | url: 'data:text/html;charset=utf-8,'+testName |
michael@0 | 464 | }); |
michael@0 | 465 | assert.fail('a bad sidebar was created..'); |
michael@0 | 466 | sidebar.destroy(); |
michael@0 | 467 | } |
michael@0 | 468 | catch(e) { |
michael@0 | 469 | assert.ok(/The option "id" must be a valid alphanumeric id/.test(e), 'invalid ids are not acceptable'); |
michael@0 | 470 | } |
michael@0 | 471 | } |
michael@0 | 472 | |
michael@0 | 473 | exports.testInvalidNullID = function(assert) { |
michael@0 | 474 | const { Sidebar } = require('sdk/ui/sidebar'); |
michael@0 | 475 | let testName = 'testInvalidNullID'; |
michael@0 | 476 | try { |
michael@0 | 477 | let sidebar = Sidebar({ |
michael@0 | 478 | id: null, |
michael@0 | 479 | title: testName, |
michael@0 | 480 | url: 'data:text/html;charset=utf-8,'+testName |
michael@0 | 481 | }); |
michael@0 | 482 | assert.fail('a bad sidebar was created..'); |
michael@0 | 483 | sidebar.destroy(); |
michael@0 | 484 | } |
michael@0 | 485 | catch(e) { |
michael@0 | 486 | assert.ok(/The option "id" must be a valid alphanumeric id/.test(e), 'invalid ids are not acceptable'); |
michael@0 | 487 | } |
michael@0 | 488 | } |
michael@0 | 489 | |
michael@0 | 490 | exports.testUndefinedID = function(assert) { |
michael@0 | 491 | const { Sidebar } = require('sdk/ui/sidebar'); |
michael@0 | 492 | let testName = 'testInvalidUndefinedID'; |
michael@0 | 493 | |
michael@0 | 494 | try { |
michael@0 | 495 | let sidebar = Sidebar({ |
michael@0 | 496 | title: testName, |
michael@0 | 497 | url: 'data:text/html;charset=utf-8,' + testName |
michael@0 | 498 | }); |
michael@0 | 499 | |
michael@0 | 500 | assert.ok(sidebar.id, 'an undefined id was accepted, id was creawted: ' + sidebar.id); |
michael@0 | 501 | assert.ok(getMostRecentBrowserWindow().document.getElementById(makeID(sidebar.id)), 'the sidebar element was found'); |
michael@0 | 502 | |
michael@0 | 503 | sidebar.destroy(); |
michael@0 | 504 | } |
michael@0 | 505 | catch(e) { |
michael@0 | 506 | assert.fail('undefined ids are acceptable'); |
michael@0 | 507 | assert.fail(e.message); |
michael@0 | 508 | } |
michael@0 | 509 | } |
michael@0 | 510 | |
michael@0 | 511 | // TEST: edge case where web panel is destroyed while loading |
michael@0 | 512 | exports.testDestroyEdgeCaseBug = function(assert, done) { |
michael@0 | 513 | const { Sidebar } = require('sdk/ui/sidebar'); |
michael@0 | 514 | let testName = 'testDestroyEdgeCaseBug'; |
michael@0 | 515 | let window = getMostRecentBrowserWindow(); |
michael@0 | 516 | let sidebar = Sidebar({ |
michael@0 | 517 | id: testName, |
michael@0 | 518 | title: testName, |
michael@0 | 519 | url: 'data:text/html;charset=utf-8,'+testName |
michael@0 | 520 | }); |
michael@0 | 521 | |
michael@0 | 522 | // NOTE: purposely not listening to show event b/c the event happens |
michael@0 | 523 | // between now and then. |
michael@0 | 524 | sidebar.show(); |
michael@0 | 525 | |
michael@0 | 526 | assert.equal(isPrivate(window), false, 'the new window is not private'); |
michael@0 | 527 | assert.equal(isSidebarShowing(window), true, 'the sidebar is showing'); |
michael@0 | 528 | |
michael@0 | 529 | //assert.equal(isShowing(sidebar), true, 'the sidebar is showing'); |
michael@0 | 530 | |
michael@0 | 531 | open().then(focus).then(function(window2) { |
michael@0 | 532 | assert.equal(isPrivate(window2), false, 'the new window is not private'); |
michael@0 | 533 | assert.equal(isSidebarShowing(window2), false, 'the sidebar is not showing'); |
michael@0 | 534 | assert.equal(isShowing(sidebar), false, 'the sidebar is not showing'); |
michael@0 | 535 | |
michael@0 | 536 | sidebar.destroy(); |
michael@0 | 537 | assert.pass('destroying the sidebar'); |
michael@0 | 538 | |
michael@0 | 539 | close(window2).then(function() { |
michael@0 | 540 | let loader = Loader(module); |
michael@0 | 541 | |
michael@0 | 542 | assert.equal(isPrivate(window), false, 'the current window is not private'); |
michael@0 | 543 | |
michael@0 | 544 | let sidebar = loader.require('sdk/ui/sidebar').Sidebar({ |
michael@0 | 545 | id: testName, |
michael@0 | 546 | title: testName, |
michael@0 | 547 | url: 'data:text/html;charset=utf-8,'+ testName, |
michael@0 | 548 | onShow: function() { |
michael@0 | 549 | assert.pass('onShow works for Sidebar'); |
michael@0 | 550 | loader.unload(); |
michael@0 | 551 | |
michael@0 | 552 | let sidebarMI = getSidebarMenuitems(); |
michael@0 | 553 | for (let mi of sidebarMI) { |
michael@0 | 554 | assert.ok(BUILTIN_SIDEBAR_MENUITEMS.indexOf(mi.getAttribute('id')) >= 0, 'the menuitem is for a built-in sidebar') |
michael@0 | 555 | assert.ok(!isChecked(mi), 'no sidebar menuitem is checked'); |
michael@0 | 556 | } |
michael@0 | 557 | assert.ok(!window.document.getElementById(makeID(testName)), 'sidebar id DNE'); |
michael@0 | 558 | assert.equal(isSidebarShowing(window), false, 'the sidebar is not showing'); |
michael@0 | 559 | |
michael@0 | 560 | done(); |
michael@0 | 561 | } |
michael@0 | 562 | }) |
michael@0 | 563 | |
michael@0 | 564 | sidebar.show(); |
michael@0 | 565 | assert.pass('showing the sidebar'); |
michael@0 | 566 | |
michael@0 | 567 | }); |
michael@0 | 568 | }); |
michael@0 | 569 | } |
michael@0 | 570 | |
michael@0 | 571 | exports.testClickingACheckedMenuitem = function(assert, done) { |
michael@0 | 572 | const { Sidebar } = require('sdk/ui/sidebar'); |
michael@0 | 573 | let testName = 'testClickingACheckedMenuitem'; |
michael@0 | 574 | let window = getMostRecentBrowserWindow(); |
michael@0 | 575 | let sidebar = Sidebar({ |
michael@0 | 576 | id: testName, |
michael@0 | 577 | title: testName, |
michael@0 | 578 | url: 'data:text/html;charset=utf-8,'+testName, |
michael@0 | 579 | }); |
michael@0 | 580 | |
michael@0 | 581 | sidebar.show().then(function() { |
michael@0 | 582 | assert.pass('the show callback works'); |
michael@0 | 583 | |
michael@0 | 584 | sidebar.once('hide', function() { |
michael@0 | 585 | assert.pass('clicking the menuitem after the sidebar has shown hides it.'); |
michael@0 | 586 | sidebar.destroy(); |
michael@0 | 587 | done(); |
michael@0 | 588 | }); |
michael@0 | 589 | |
michael@0 | 590 | let menuitem = window.document.getElementById(makeID(sidebar.id)); |
michael@0 | 591 | simulateCommand(menuitem); |
michael@0 | 592 | }); |
michael@0 | 593 | }; |
michael@0 | 594 | |
michael@0 | 595 | exports.testTitleSetter = function(assert, done) { |
michael@0 | 596 | const { Sidebar } = require('sdk/ui/sidebar'); |
michael@0 | 597 | let testName = 'testTitleSetter'; |
michael@0 | 598 | let { document } = getMostRecentBrowserWindow(); |
michael@0 | 599 | |
michael@0 | 600 | let sidebar1 = Sidebar({ |
michael@0 | 601 | id: testName, |
michael@0 | 602 | title: testName, |
michael@0 | 603 | url: 'data:text/html;charset=utf-8,'+testName, |
michael@0 | 604 | }); |
michael@0 | 605 | |
michael@0 | 606 | assert.equal(sidebar1.title, testName, 'title getter works'); |
michael@0 | 607 | |
michael@0 | 608 | sidebar1.show().then(function() { |
michael@0 | 609 | assert.equal(document.getElementById(makeID(sidebar1.id)).getAttribute('label'), |
michael@0 | 610 | testName, |
michael@0 | 611 | 'the menuitem label is correct'); |
michael@0 | 612 | |
michael@0 | 613 | assert.equal(document.getElementById('sidebar-title').value, testName, 'the menuitem label is correct'); |
michael@0 | 614 | |
michael@0 | 615 | sidebar1.title = 'foo'; |
michael@0 | 616 | |
michael@0 | 617 | assert.equal(sidebar1.title, 'foo', 'title getter works'); |
michael@0 | 618 | |
michael@0 | 619 | assert.equal(document.getElementById(makeID(sidebar1.id)).getAttribute('label'), |
michael@0 | 620 | 'foo', |
michael@0 | 621 | 'the menuitem label was updated'); |
michael@0 | 622 | |
michael@0 | 623 | assert.equal(document.getElementById('sidebar-title').value, 'foo', 'the sidebar title was updated'); |
michael@0 | 624 | |
michael@0 | 625 | sidebar1.destroy(); |
michael@0 | 626 | done(); |
michael@0 | 627 | }, assert.fail); |
michael@0 | 628 | } |
michael@0 | 629 | |
michael@0 | 630 | exports.testURLSetter = function(assert, done) { |
michael@0 | 631 | const { Sidebar } = require('sdk/ui/sidebar'); |
michael@0 | 632 | let testName = 'testURLSetter'; |
michael@0 | 633 | let window = getMostRecentBrowserWindow(); |
michael@0 | 634 | let { document } = window; |
michael@0 | 635 | let url = 'data:text/html;charset=utf-8,'+testName; |
michael@0 | 636 | |
michael@0 | 637 | let sidebar1 = Sidebar({ |
michael@0 | 638 | id: testName, |
michael@0 | 639 | title: testName, |
michael@0 | 640 | url: url |
michael@0 | 641 | }); |
michael@0 | 642 | |
michael@0 | 643 | assert.equal(sidebar1.url, url, 'url getter works'); |
michael@0 | 644 | assert.equal(isShowing(sidebar1), false, 'the sidebar is not showing'); |
michael@0 | 645 | assert.ok(!isChecked(document.getElementById(makeID(sidebar1.id))), |
michael@0 | 646 | 'the menuitem is not checked'); |
michael@0 | 647 | assert.equal(isSidebarShowing(window), false, 'the new window sidebar is not showing'); |
michael@0 | 648 | |
michael@0 | 649 | windowPromise(window.OpenBrowserWindow(), 'load').then(function(window) { |
michael@0 | 650 | let { document } = window; |
michael@0 | 651 | assert.pass('new window was opened'); |
michael@0 | 652 | |
michael@0 | 653 | sidebar1.show().then(function() { |
michael@0 | 654 | assert.equal(isShowing(sidebar1), true, 'the sidebar is showing'); |
michael@0 | 655 | assert.ok(isChecked(document.getElementById(makeID(sidebar1.id))), |
michael@0 | 656 | 'the menuitem is checked'); |
michael@0 | 657 | assert.ok(isSidebarShowing(window), 'the new window sidebar is showing'); |
michael@0 | 658 | |
michael@0 | 659 | sidebar1.once('show', function() { |
michael@0 | 660 | assert.pass('setting the sidebar.url causes a show event'); |
michael@0 | 661 | |
michael@0 | 662 | assert.equal(isShowing(sidebar1), true, 'the sidebar is showing'); |
michael@0 | 663 | assert.ok(isSidebarShowing(window), 'the new window sidebar is still showing'); |
michael@0 | 664 | |
michael@0 | 665 | assert.ok(isChecked(document.getElementById(makeID(sidebar1.id))), |
michael@0 | 666 | 'the menuitem is still checked'); |
michael@0 | 667 | |
michael@0 | 668 | sidebar1.destroy(); |
michael@0 | 669 | |
michael@0 | 670 | close(window).then(done); |
michael@0 | 671 | }); |
michael@0 | 672 | |
michael@0 | 673 | sidebar1.url = (url + '1'); |
michael@0 | 674 | |
michael@0 | 675 | assert.equal(sidebar1.url, (url + '1'), 'url getter works'); |
michael@0 | 676 | assert.equal(isShowing(sidebar1), true, 'the sidebar is showing'); |
michael@0 | 677 | assert.ok(isSidebarShowing(window), 'the new window sidebar is showing'); |
michael@0 | 678 | }, assert.fail); |
michael@0 | 679 | }, assert.fail); |
michael@0 | 680 | } |
michael@0 | 681 | |
michael@0 | 682 | exports.testDuplicateID = function(assert) { |
michael@0 | 683 | const { Sidebar } = require('sdk/ui/sidebar'); |
michael@0 | 684 | let testName = 'testDuplicateID'; |
michael@0 | 685 | let window = getMostRecentBrowserWindow(); |
michael@0 | 686 | let { document } = window; |
michael@0 | 687 | let url = 'data:text/html;charset=utf-8,'+testName; |
michael@0 | 688 | |
michael@0 | 689 | let sidebar1 = Sidebar({ |
michael@0 | 690 | id: testName, |
michael@0 | 691 | title: testName, |
michael@0 | 692 | url: url |
michael@0 | 693 | }); |
michael@0 | 694 | |
michael@0 | 695 | assert.throws(function() { |
michael@0 | 696 | Sidebar({ |
michael@0 | 697 | id: testName, |
michael@0 | 698 | title: testName + 1, |
michael@0 | 699 | url: url + 2 |
michael@0 | 700 | }).destroy(); |
michael@0 | 701 | }, /The ID .+ seems already used\./i, 'duplicate IDs will throw errors'); |
michael@0 | 702 | |
michael@0 | 703 | sidebar1.destroy(); |
michael@0 | 704 | } |
michael@0 | 705 | |
michael@0 | 706 | exports.testURLSetterToSameValueReloadsSidebar = function(assert, done) { |
michael@0 | 707 | const { Sidebar } = require('sdk/ui/sidebar'); |
michael@0 | 708 | let testName = 'testURLSetterToSameValueReloadsSidebar'; |
michael@0 | 709 | let window = getMostRecentBrowserWindow(); |
michael@0 | 710 | let { document } = window; |
michael@0 | 711 | let url = 'data:text/html;charset=utf-8,'+testName; |
michael@0 | 712 | |
michael@0 | 713 | let sidebar1 = Sidebar({ |
michael@0 | 714 | id: testName, |
michael@0 | 715 | title: testName, |
michael@0 | 716 | url: url |
michael@0 | 717 | }); |
michael@0 | 718 | |
michael@0 | 719 | assert.equal(sidebar1.url, url, 'url getter works'); |
michael@0 | 720 | assert.equal(isShowing(sidebar1), false, 'the sidebar is not showing'); |
michael@0 | 721 | assert.ok(!isChecked(document.getElementById(makeID(sidebar1.id))), |
michael@0 | 722 | 'the menuitem is not checked'); |
michael@0 | 723 | assert.equal(isSidebarShowing(window), false, 'the new window sidebar is not showing'); |
michael@0 | 724 | |
michael@0 | 725 | windowPromise(window.OpenBrowserWindow(), 'load').then(function(window) { |
michael@0 | 726 | let { document } = window; |
michael@0 | 727 | assert.pass('new window was opened'); |
michael@0 | 728 | |
michael@0 | 729 | sidebar1.show().then(function() { |
michael@0 | 730 | assert.equal(isShowing(sidebar1), true, 'the sidebar is showing'); |
michael@0 | 731 | assert.ok(isChecked(document.getElementById(makeID(sidebar1.id))), |
michael@0 | 732 | 'the menuitem is checked'); |
michael@0 | 733 | assert.ok(isSidebarShowing(window), 'the new window sidebar is showing'); |
michael@0 | 734 | |
michael@0 | 735 | sidebar1.once('show', function() { |
michael@0 | 736 | assert.pass('setting the sidebar.url causes a show event'); |
michael@0 | 737 | |
michael@0 | 738 | assert.equal(isShowing(sidebar1), true, 'the sidebar is showing'); |
michael@0 | 739 | assert.ok(isSidebarShowing(window), 'the new window sidebar is still showing'); |
michael@0 | 740 | |
michael@0 | 741 | assert.ok(isChecked(document.getElementById(makeID(sidebar1.id))), |
michael@0 | 742 | 'the menuitem is still checked'); |
michael@0 | 743 | |
michael@0 | 744 | sidebar1.destroy(); |
michael@0 | 745 | |
michael@0 | 746 | close(window).then(done); |
michael@0 | 747 | }); |
michael@0 | 748 | |
michael@0 | 749 | sidebar1.url = url; |
michael@0 | 750 | |
michael@0 | 751 | assert.equal(sidebar1.url, url, 'url getter works'); |
michael@0 | 752 | assert.equal(isShowing(sidebar1), true, 'the sidebar is showing'); |
michael@0 | 753 | assert.ok(isSidebarShowing(window), 'the new window sidebar is showing'); |
michael@0 | 754 | }, assert.fail); |
michael@0 | 755 | }, assert.fail); |
michael@0 | 756 | } |
michael@0 | 757 | |
michael@0 | 758 | exports.testShowingInOneWindowDoesNotAffectOtherWindows = function(assert, done) { |
michael@0 | 759 | const { Sidebar } = require('sdk/ui/sidebar'); |
michael@0 | 760 | let testName = 'testShowingInOneWindowDoesNotAffectOtherWindows'; |
michael@0 | 761 | let window1 = getMostRecentBrowserWindow(); |
michael@0 | 762 | let url = 'data:text/html;charset=utf-8,'+testName; |
michael@0 | 763 | |
michael@0 | 764 | let sidebar1 = Sidebar({ |
michael@0 | 765 | id: testName, |
michael@0 | 766 | title: testName, |
michael@0 | 767 | url: url |
michael@0 | 768 | }); |
michael@0 | 769 | |
michael@0 | 770 | assert.equal(sidebar1.url, url, 'url getter works'); |
michael@0 | 771 | assert.equal(isShowing(sidebar1), false, 'the sidebar is not showing'); |
michael@0 | 772 | let checkCount = 1; |
michael@0 | 773 | function checkSidebarShowing(window, expected) { |
michael@0 | 774 | assert.pass('check count ' + checkCount++); |
michael@0 | 775 | |
michael@0 | 776 | let mi = window.document.getElementById(makeID(sidebar1.id)); |
michael@0 | 777 | if (mi) { |
michael@0 | 778 | assert.equal(isChecked(mi), expected, |
michael@0 | 779 | 'the menuitem is not checked'); |
michael@0 | 780 | } |
michael@0 | 781 | assert.equal(isSidebarShowing(window), expected || false, 'the new window sidebar is not showing'); |
michael@0 | 782 | } |
michael@0 | 783 | checkSidebarShowing(window1, false); |
michael@0 | 784 | |
michael@0 | 785 | windowPromise(window1.OpenBrowserWindow(), 'load').then(function(window) { |
michael@0 | 786 | let { document } = window; |
michael@0 | 787 | assert.pass('new window was opened!'); |
michael@0 | 788 | |
michael@0 | 789 | // waiting for show |
michael@0 | 790 | sidebar1.once('show', function() { |
michael@0 | 791 | // check state of the new window |
michael@0 | 792 | assert.equal(isShowing(sidebar1), true, 'the sidebar is showing'); |
michael@0 | 793 | checkSidebarShowing(window, true); |
michael@0 | 794 | |
michael@0 | 795 | // check state of old window |
michael@0 | 796 | checkSidebarShowing(window1, false); |
michael@0 | 797 | |
michael@0 | 798 | // waiting for show using url setter |
michael@0 | 799 | sidebar1.once('show', function() { |
michael@0 | 800 | assert.pass('setting the sidebar.url causes a new show event'); |
michael@0 | 801 | |
michael@0 | 802 | // check state of the new window |
michael@0 | 803 | assert.equal(isShowing(sidebar1), true, 'the sidebar is showing'); |
michael@0 | 804 | checkSidebarShowing(window, true); |
michael@0 | 805 | |
michael@0 | 806 | // check state of old window |
michael@0 | 807 | checkSidebarShowing(window1, false); |
michael@0 | 808 | |
michael@0 | 809 | // calling destroy() twice should not matter |
michael@0 | 810 | sidebar1.destroy(); |
michael@0 | 811 | sidebar1.destroy(); |
michael@0 | 812 | |
michael@0 | 813 | // check state of the new window |
michael@0 | 814 | assert.equal(isShowing(sidebar1), false, 'the sidebar is not showing'); |
michael@0 | 815 | checkSidebarShowing(window, undefined); |
michael@0 | 816 | |
michael@0 | 817 | // check state of old window |
michael@0 | 818 | checkSidebarShowing(window1, undefined); |
michael@0 | 819 | |
michael@0 | 820 | close(window).then(done); |
michael@0 | 821 | }); |
michael@0 | 822 | |
michael@0 | 823 | assert.pass('setting sidebar1.url'); |
michael@0 | 824 | sidebar1.url += '1'; |
michael@0 | 825 | assert.pass('set sidebar1.url'); |
michael@0 | 826 | }); |
michael@0 | 827 | |
michael@0 | 828 | sidebar1.show(); |
michael@0 | 829 | }, assert.fail); |
michael@0 | 830 | } |
michael@0 | 831 | |
michael@0 | 832 | exports.testHidingAHiddenSidebarRejects = function(assert, done) { |
michael@0 | 833 | const { Sidebar } = require('sdk/ui/sidebar'); |
michael@0 | 834 | let testName = 'testHidingAHiddenSidebarRejects'; |
michael@0 | 835 | let url = 'data:text/html;charset=utf-8,'+testName; |
michael@0 | 836 | let sidebar = Sidebar({ |
michael@0 | 837 | id: testName, |
michael@0 | 838 | title: testName, |
michael@0 | 839 | url: url |
michael@0 | 840 | }); |
michael@0 | 841 | |
michael@0 | 842 | sidebar.hide().then(assert.fail, assert.pass).then(function() { |
michael@0 | 843 | sidebar.destroy(); |
michael@0 | 844 | done(); |
michael@0 | 845 | }, assert.fail); |
michael@0 | 846 | } |
michael@0 | 847 | |
michael@0 | 848 | exports.testGCdSidebarsOnUnload = function(assert, done) { |
michael@0 | 849 | const loader = Loader(module); |
michael@0 | 850 | const { Sidebar } = loader.require('sdk/ui/sidebar'); |
michael@0 | 851 | const window = getMostRecentBrowserWindow(); |
michael@0 | 852 | |
michael@0 | 853 | let testName = 'testGCdSidebarsOnUnload'; |
michael@0 | 854 | let url = 'data:text/html;charset=utf-8,'+testName; |
michael@0 | 855 | |
michael@0 | 856 | assert.equal(isSidebarShowing(window), false, 'the sidebar is not showing'); |
michael@0 | 857 | |
michael@0 | 858 | // IMPORTANT: make no reference to the sidebar instance, so it is GC'd |
michael@0 | 859 | let sidebar = Sidebar({ |
michael@0 | 860 | id: testName, |
michael@0 | 861 | title: testName, |
michael@0 | 862 | url: url |
michael@0 | 863 | }); |
michael@0 | 864 | |
michael@0 | 865 | sidebar.show().then(function() { |
michael@0 | 866 | sidebar = null; |
michael@0 | 867 | |
michael@0 | 868 | assert.equal(isSidebarShowing(window), true, 'the sidebar is showing'); |
michael@0 | 869 | |
michael@0 | 870 | let menuitemID = makeID(testName); |
michael@0 | 871 | |
michael@0 | 872 | assert.ok(!!window.document.getElementById(menuitemID), 'the menuitem was found'); |
michael@0 | 873 | |
michael@0 | 874 | Cu.schedulePreciseGC(function() { |
michael@0 | 875 | loader.unload(); |
michael@0 | 876 | |
michael@0 | 877 | assert.equal(isSidebarShowing(window), false, 'the sidebar is not showing after unload'); |
michael@0 | 878 | assert.ok(!window.document.getElementById(menuitemID), 'the menuitem was removed'); |
michael@0 | 879 | |
michael@0 | 880 | done(); |
michael@0 | 881 | }) |
michael@0 | 882 | }, assert.fail).then(null, assert.fail); |
michael@0 | 883 | } |
michael@0 | 884 | |
michael@0 | 885 | exports.testGCdShowingSidebarsOnUnload = function(assert, done) { |
michael@0 | 886 | const loader = Loader(module); |
michael@0 | 887 | const { Sidebar } = loader.require('sdk/ui/sidebar'); |
michael@0 | 888 | const window = getMostRecentBrowserWindow(); |
michael@0 | 889 | |
michael@0 | 890 | let testName = 'testGCdShowingSidebarsOnUnload'; |
michael@0 | 891 | let url = 'data:text/html;charset=utf-8,'+testName; |
michael@0 | 892 | |
michael@0 | 893 | assert.equal(isSidebarShowing(window), false, 'the sidebar is not showing'); |
michael@0 | 894 | |
michael@0 | 895 | let sidebar = Sidebar({ |
michael@0 | 896 | id: testName, |
michael@0 | 897 | title: testName, |
michael@0 | 898 | url: url |
michael@0 | 899 | }); |
michael@0 | 900 | |
michael@0 | 901 | sidebar.on('show', function() { |
michael@0 | 902 | sidebar = null; |
michael@0 | 903 | |
michael@0 | 904 | assert.equal(isSidebarShowing(window), true, 'the sidebar is showing'); |
michael@0 | 905 | |
michael@0 | 906 | let menuitemID = makeID(testName); |
michael@0 | 907 | |
michael@0 | 908 | assert.ok(!!window.document.getElementById(menuitemID), 'the menuitem was found'); |
michael@0 | 909 | |
michael@0 | 910 | Cu.schedulePreciseGC(function() { |
michael@0 | 911 | assert.equal(isSidebarShowing(window), true, 'the sidebar is still showing after gc'); |
michael@0 | 912 | assert.ok(!!window.document.getElementById(menuitemID), 'the menuitem was found after gc'); |
michael@0 | 913 | |
michael@0 | 914 | loader.unload(); |
michael@0 | 915 | |
michael@0 | 916 | assert.equal(isSidebarShowing(window), false, 'the sidebar is not showing after unload'); |
michael@0 | 917 | assert.ok(!window.document.getElementById(menuitemID), 'the menuitem was removed'); |
michael@0 | 918 | |
michael@0 | 919 | done(); |
michael@0 | 920 | }) |
michael@0 | 921 | }); |
michael@0 | 922 | |
michael@0 | 923 | sidebar.show(); |
michael@0 | 924 | } |
michael@0 | 925 | |
michael@0 | 926 | exports.testDetachEventOnWindowClose = function(assert, done) { |
michael@0 | 927 | const loader = Loader(module); |
michael@0 | 928 | const { Sidebar } = loader.require('sdk/ui/sidebar'); |
michael@0 | 929 | const window = getMostRecentBrowserWindow(); |
michael@0 | 930 | |
michael@0 | 931 | let testName = 'testDetachEventOnWindowClose'; |
michael@0 | 932 | let url = 'data:text/html;charset=utf-8,' + testName; |
michael@0 | 933 | |
michael@0 | 934 | |
michael@0 | 935 | windowPromise(window.OpenBrowserWindow(), 'load').then(focus).then(function(window) { |
michael@0 | 936 | let sidebar = Sidebar({ |
michael@0 | 937 | id: testName, |
michael@0 | 938 | title: testName, |
michael@0 | 939 | url: url, |
michael@0 | 940 | onAttach: function() { |
michael@0 | 941 | assert.pass('the attach event is fired'); |
michael@0 | 942 | window.close(); |
michael@0 | 943 | }, |
michael@0 | 944 | onDetach: function() { |
michael@0 | 945 | assert.pass('the detach event is fired when the window showing it closes'); |
michael@0 | 946 | loader.unload(); |
michael@0 | 947 | done(); |
michael@0 | 948 | } |
michael@0 | 949 | }); |
michael@0 | 950 | |
michael@0 | 951 | sidebar.show(); |
michael@0 | 952 | }).then(null, assert.fail); |
michael@0 | 953 | } |
michael@0 | 954 | |
michael@0 | 955 | exports.testHideEventOnWindowClose = function(assert, done) { |
michael@0 | 956 | const loader = Loader(module); |
michael@0 | 957 | const { Sidebar } = loader.require('sdk/ui/sidebar'); |
michael@0 | 958 | const window = getMostRecentBrowserWindow(); |
michael@0 | 959 | |
michael@0 | 960 | let testName = 'testDetachEventOnWindowClose'; |
michael@0 | 961 | let url = 'data:text/html;charset=utf-8,' + testName; |
michael@0 | 962 | |
michael@0 | 963 | |
michael@0 | 964 | windowPromise(window.OpenBrowserWindow(), 'load').then(focus).then(function(window) { |
michael@0 | 965 | let sidebar = Sidebar({ |
michael@0 | 966 | id: testName, |
michael@0 | 967 | title: testName, |
michael@0 | 968 | url: url, |
michael@0 | 969 | onAttach: function() { |
michael@0 | 970 | assert.pass('the attach event is fired'); |
michael@0 | 971 | window.close(); |
michael@0 | 972 | }, |
michael@0 | 973 | onHide: function() { |
michael@0 | 974 | assert.pass('the hide event is fired when the window showing it closes'); |
michael@0 | 975 | loader.unload(); |
michael@0 | 976 | done(); |
michael@0 | 977 | } |
michael@0 | 978 | }); |
michael@0 | 979 | |
michael@0 | 980 | sidebar.show(); |
michael@0 | 981 | }).then(null, assert.fail); |
michael@0 | 982 | } |
michael@0 | 983 | |
michael@0 | 984 | exports.testGCdHiddenSidebarsOnUnload = function(assert, done) { |
michael@0 | 985 | const loader = Loader(module); |
michael@0 | 986 | const { Sidebar } = loader.require('sdk/ui/sidebar'); |
michael@0 | 987 | const window = getMostRecentBrowserWindow(); |
michael@0 | 988 | |
michael@0 | 989 | let testName = 'testGCdHiddenSidebarsOnUnload'; |
michael@0 | 990 | let url = 'data:text/html;charset=utf-8,'+testName; |
michael@0 | 991 | |
michael@0 | 992 | assert.equal(isSidebarShowing(window), false, 'the sidebar is not showing'); |
michael@0 | 993 | |
michael@0 | 994 | // IMPORTANT: make no reference to the sidebar instance, so it is GC'd |
michael@0 | 995 | Sidebar({ |
michael@0 | 996 | id: testName, |
michael@0 | 997 | title: testName, |
michael@0 | 998 | url: url |
michael@0 | 999 | }); |
michael@0 | 1000 | |
michael@0 | 1001 | let menuitemID = makeID(testName); |
michael@0 | 1002 | |
michael@0 | 1003 | assert.ok(!!window.document.getElementById(menuitemID), 'the menuitem was found'); |
michael@0 | 1004 | |
michael@0 | 1005 | Cu.schedulePreciseGC(function() { |
michael@0 | 1006 | assert.ok(!!window.document.getElementById(menuitemID), 'the menuitem was found after gc'); |
michael@0 | 1007 | |
michael@0 | 1008 | loader.unload(); |
michael@0 | 1009 | |
michael@0 | 1010 | assert.ok(!window.document.getElementById(menuitemID), 'the menuitem was removed'); |
michael@0 | 1011 | |
michael@0 | 1012 | done(); |
michael@0 | 1013 | }); |
michael@0 | 1014 | } |
michael@0 | 1015 | |
michael@0 | 1016 | exports.testSidebarGettersAndSettersAfterDestroy = function(assert) { |
michael@0 | 1017 | const { Sidebar } = require('sdk/ui/sidebar'); |
michael@0 | 1018 | let testName = 'testSidebarGettersAndSettersAfterDestroy'; |
michael@0 | 1019 | let url = 'data:text/html;charset=utf-8,'+testName; |
michael@0 | 1020 | |
michael@0 | 1021 | let sidebar = Sidebar({ |
michael@0 | 1022 | id: testName, |
michael@0 | 1023 | title: testName, |
michael@0 | 1024 | url: url |
michael@0 | 1025 | }); |
michael@0 | 1026 | |
michael@0 | 1027 | sidebar.destroy(); |
michael@0 | 1028 | |
michael@0 | 1029 | assert.equal(sidebar.id, undefined, 'sidebar after destroy has no id'); |
michael@0 | 1030 | |
michael@0 | 1031 | assert.throws(() => sidebar.id = 'foo-tang', |
michael@0 | 1032 | /^setting a property that has only a getter/, |
michael@0 | 1033 | 'id cannot be set at runtime'); |
michael@0 | 1034 | |
michael@0 | 1035 | assert.equal(sidebar.id, undefined, 'sidebar after destroy has no id'); |
michael@0 | 1036 | |
michael@0 | 1037 | assert.equal(sidebar.title, undefined, 'sidebar after destroy has no title'); |
michael@0 | 1038 | sidebar.title = 'boo-tang'; |
michael@0 | 1039 | assert.equal(sidebar.title, undefined, 'sidebar after destroy has no title'); |
michael@0 | 1040 | |
michael@0 | 1041 | assert.equal(sidebar.url, undefined, 'sidebar after destroy has no url'); |
michael@0 | 1042 | sidebar.url = url + 'barz'; |
michael@0 | 1043 | assert.equal(sidebar.url, undefined, 'sidebar after destroy has no url'); |
michael@0 | 1044 | } |
michael@0 | 1045 | |
michael@0 | 1046 | |
michael@0 | 1047 | exports.testSidebarLeakCheckDestroyAfterAttach = function(assert, done) { |
michael@0 | 1048 | const { Sidebar } = require('sdk/ui/sidebar'); |
michael@0 | 1049 | let testName = 'testSidebarLeakCheckDestroyAfterAttach'; |
michael@0 | 1050 | let window = getMostRecentBrowserWindow(); |
michael@0 | 1051 | let sidebar = Sidebar({ |
michael@0 | 1052 | id: testName, |
michael@0 | 1053 | title: testName, |
michael@0 | 1054 | url: 'data:text/html;charset=utf-8,'+testName |
michael@0 | 1055 | }); |
michael@0 | 1056 | |
michael@0 | 1057 | sidebar.on('attach', function() { |
michael@0 | 1058 | assert.pass('the sidebar was shown'); |
michael@0 | 1059 | |
michael@0 | 1060 | sidebar.on('show', function() { |
michael@0 | 1061 | assert.fail('the sidebar show listener should have been removed'); |
michael@0 | 1062 | }); |
michael@0 | 1063 | assert.pass('added a sidebar show listener'); |
michael@0 | 1064 | |
michael@0 | 1065 | sidebar.on('hide', function() { |
michael@0 | 1066 | assert.fail('the sidebar hide listener should have been removed'); |
michael@0 | 1067 | }); |
michael@0 | 1068 | assert.pass('added a sidebar hide listener'); |
michael@0 | 1069 | |
michael@0 | 1070 | let panelBrowser = window.document.getElementById('sidebar').contentDocument.getElementById('web-panels-browser'); |
michael@0 | 1071 | panelBrowser.contentWindow.addEventListener('unload', function onUnload() { |
michael@0 | 1072 | panelBrowser.contentWindow.removeEventListener('unload', onUnload, false); |
michael@0 | 1073 | // wait a tick.. |
michael@0 | 1074 | setTimeout(function() { |
michael@0 | 1075 | assert.pass('the sidebar web panel was unloaded properly'); |
michael@0 | 1076 | done(); |
michael@0 | 1077 | }) |
michael@0 | 1078 | }, false); |
michael@0 | 1079 | |
michael@0 | 1080 | sidebar.destroy(); |
michael@0 | 1081 | }); |
michael@0 | 1082 | |
michael@0 | 1083 | assert.pass('showing the sidebar'); |
michael@0 | 1084 | sidebar.show(); |
michael@0 | 1085 | } |
michael@0 | 1086 | |
michael@0 | 1087 | exports.testSidebarLeakCheckUnloadAfterAttach = function(assert, done) { |
michael@0 | 1088 | const loader = Loader(module); |
michael@0 | 1089 | const { Sidebar } = loader.require('sdk/ui/sidebar'); |
michael@0 | 1090 | let testName = 'testSidebarLeakCheckUnloadAfterAttach'; |
michael@0 | 1091 | let window = getMostRecentBrowserWindow(); |
michael@0 | 1092 | let sidebar = Sidebar({ |
michael@0 | 1093 | id: testName, |
michael@0 | 1094 | title: testName, |
michael@0 | 1095 | url: 'data:text/html;charset=utf-8,'+testName |
michael@0 | 1096 | }); |
michael@0 | 1097 | |
michael@0 | 1098 | sidebar.on('attach', function() { |
michael@0 | 1099 | assert.pass('the sidebar was shown'); |
michael@0 | 1100 | |
michael@0 | 1101 | sidebar.on('show', function() { |
michael@0 | 1102 | assert.fail('the sidebar show listener should have been removed'); |
michael@0 | 1103 | }); |
michael@0 | 1104 | assert.pass('added a sidebar show listener'); |
michael@0 | 1105 | |
michael@0 | 1106 | sidebar.on('hide', function() { |
michael@0 | 1107 | assert.fail('the sidebar hide listener should have been removed'); |
michael@0 | 1108 | }); |
michael@0 | 1109 | assert.pass('added a sidebar hide listener'); |
michael@0 | 1110 | |
michael@0 | 1111 | let panelBrowser = window.document.getElementById('sidebar').contentDocument.getElementById('web-panels-browser'); |
michael@0 | 1112 | panelBrowser.contentWindow.addEventListener('unload', function onUnload() { |
michael@0 | 1113 | panelBrowser.contentWindow.removeEventListener('unload', onUnload, false); |
michael@0 | 1114 | // wait a tick.. |
michael@0 | 1115 | setTimeout(function() { |
michael@0 | 1116 | assert.pass('the sidebar web panel was unloaded properly'); |
michael@0 | 1117 | done(); |
michael@0 | 1118 | }) |
michael@0 | 1119 | }, false); |
michael@0 | 1120 | |
michael@0 | 1121 | loader.unload(); |
michael@0 | 1122 | }); |
michael@0 | 1123 | |
michael@0 | 1124 | assert.pass('showing the sidebar'); |
michael@0 | 1125 | sidebar.show(); |
michael@0 | 1126 | } |
michael@0 | 1127 | |
michael@0 | 1128 | exports.testTwoSidebarsWithSameTitleAndURL = function(assert) { |
michael@0 | 1129 | const { Sidebar } = require('sdk/ui/sidebar'); |
michael@0 | 1130 | let testName = 'testTwoSidebarsWithSameTitleAndURL'; |
michael@0 | 1131 | |
michael@0 | 1132 | let title = testName; |
michael@0 | 1133 | let url = 'data:text/html;charset=utf-8,' + testName; |
michael@0 | 1134 | |
michael@0 | 1135 | let sidebar1 = Sidebar({ |
michael@0 | 1136 | id: testName + 1, |
michael@0 | 1137 | title: title, |
michael@0 | 1138 | url: url |
michael@0 | 1139 | }); |
michael@0 | 1140 | |
michael@0 | 1141 | assert.throws(function() { |
michael@0 | 1142 | Sidebar({ |
michael@0 | 1143 | id: testName + 2, |
michael@0 | 1144 | title: title, |
michael@0 | 1145 | url: url |
michael@0 | 1146 | }).destroy(); |
michael@0 | 1147 | }, /title.+url.+invalid/i, 'Creating two sidebars with the same title + url is not allowed'); |
michael@0 | 1148 | |
michael@0 | 1149 | let sidebar2 = Sidebar({ |
michael@0 | 1150 | id: testName + 2, |
michael@0 | 1151 | title: title, |
michael@0 | 1152 | url: 'data:text/html;charset=utf-8,X' |
michael@0 | 1153 | }); |
michael@0 | 1154 | |
michael@0 | 1155 | assert.throws(function() { |
michael@0 | 1156 | sidebar2.url = url; |
michael@0 | 1157 | }, /title.+url.+invalid/i, 'Creating two sidebars with the same title + url is not allowed'); |
michael@0 | 1158 | |
michael@0 | 1159 | sidebar2.title = 'foo'; |
michael@0 | 1160 | sidebar2.url = url; |
michael@0 | 1161 | |
michael@0 | 1162 | assert.throws(function() { |
michael@0 | 1163 | sidebar2.title = title; |
michael@0 | 1164 | }, /title.+url.+invalid/i, 'Creating two sidebars with the same title + url is not allowed'); |
michael@0 | 1165 | |
michael@0 | 1166 | sidebar1.destroy(); |
michael@0 | 1167 | sidebar2.destroy(); |
michael@0 | 1168 | } |
michael@0 | 1169 | |
michael@0 | 1170 | exports.testChangingURLBackToOriginalValue = function(assert) { |
michael@0 | 1171 | const { Sidebar } = require('sdk/ui/sidebar'); |
michael@0 | 1172 | let testName = 'testChangingURLBackToOriginalValue'; |
michael@0 | 1173 | |
michael@0 | 1174 | let title = testName; |
michael@0 | 1175 | let url = 'data:text/html;charset=utf-8,' + testName; |
michael@0 | 1176 | let count = 0; |
michael@0 | 1177 | |
michael@0 | 1178 | let sidebar = Sidebar({ |
michael@0 | 1179 | id: testName, |
michael@0 | 1180 | title: title, |
michael@0 | 1181 | url: url |
michael@0 | 1182 | }); |
michael@0 | 1183 | |
michael@0 | 1184 | sidebar.url = url + 2; |
michael@0 | 1185 | assert.equal(sidebar.url, url + 2, 'the sidebar.url is correct'); |
michael@0 | 1186 | sidebar.url = url; |
michael@0 | 1187 | assert.equal(sidebar.url, url, 'the sidebar.url is correct'); |
michael@0 | 1188 | |
michael@0 | 1189 | sidebar.title = 'foo'; |
michael@0 | 1190 | assert.equal(sidebar.title, 'foo', 'the sidebar.title is correct'); |
michael@0 | 1191 | sidebar.title = title; |
michael@0 | 1192 | assert.equal(sidebar.title, title, 'the sidebar.title is correct'); |
michael@0 | 1193 | |
michael@0 | 1194 | sidebar.destroy(); |
michael@0 | 1195 | |
michael@0 | 1196 | assert.pass('Changing values back to originals works'); |
michael@0 | 1197 | } |
michael@0 | 1198 | |
michael@0 | 1199 | exports.testShowToOpenXToClose = function(assert, done) { |
michael@0 | 1200 | const { Sidebar } = require('sdk/ui/sidebar'); |
michael@0 | 1201 | let testName = 'testShowToOpenXToClose'; |
michael@0 | 1202 | |
michael@0 | 1203 | let title = testName; |
michael@0 | 1204 | let url = 'data:text/html;charset=utf-8,' + testName; |
michael@0 | 1205 | let window = getMostRecentBrowserWindow(); |
michael@0 | 1206 | |
michael@0 | 1207 | let sidebar = Sidebar({ |
michael@0 | 1208 | id: testName, |
michael@0 | 1209 | title: testName, |
michael@0 | 1210 | url: url, |
michael@0 | 1211 | onShow: function() { |
michael@0 | 1212 | assert.ok(isChecked(menuitem), 'menuitem is checked'); |
michael@0 | 1213 | |
michael@0 | 1214 | let closeButton = window.document.querySelector('#sidebar-header > toolbarbutton.close-icon'); |
michael@0 | 1215 | simulateCommand(closeButton); |
michael@0 | 1216 | }, |
michael@0 | 1217 | onHide: function() { |
michael@0 | 1218 | assert.ok(!isChecked(menuitem), 'menuitem is not checked'); |
michael@0 | 1219 | |
michael@0 | 1220 | sidebar.destroy(); |
michael@0 | 1221 | done(); |
michael@0 | 1222 | } |
michael@0 | 1223 | }); |
michael@0 | 1224 | |
michael@0 | 1225 | let menuitem = window.document.getElementById(makeID(sidebar.id)); |
michael@0 | 1226 | |
michael@0 | 1227 | assert.ok(!isChecked(menuitem), 'menuitem is not checked'); |
michael@0 | 1228 | |
michael@0 | 1229 | sidebar.show(); |
michael@0 | 1230 | } |
michael@0 | 1231 | |
michael@0 | 1232 | exports.testShowToOpenMenuitemToClose = function(assert, done) { |
michael@0 | 1233 | const { Sidebar } = require('sdk/ui/sidebar'); |
michael@0 | 1234 | let testName = 'testShowToOpenMenuitemToClose'; |
michael@0 | 1235 | |
michael@0 | 1236 | let title = testName; |
michael@0 | 1237 | let url = 'data:text/html;charset=utf-8,' + testName; |
michael@0 | 1238 | let window = getMostRecentBrowserWindow(); |
michael@0 | 1239 | |
michael@0 | 1240 | let sidebar = Sidebar({ |
michael@0 | 1241 | id: testName, |
michael@0 | 1242 | title: testName, |
michael@0 | 1243 | url: url, |
michael@0 | 1244 | onShow: function() { |
michael@0 | 1245 | assert.ok(isChecked(menuitem), 'menuitem is checked'); |
michael@0 | 1246 | |
michael@0 | 1247 | simulateCommand(menuitem); |
michael@0 | 1248 | }, |
michael@0 | 1249 | onHide: function() { |
michael@0 | 1250 | assert.ok(!isChecked(menuitem), 'menuitem is not checked'); |
michael@0 | 1251 | |
michael@0 | 1252 | sidebar.destroy(); |
michael@0 | 1253 | done(); |
michael@0 | 1254 | } |
michael@0 | 1255 | }); |
michael@0 | 1256 | |
michael@0 | 1257 | let menuitem = window.document.getElementById(makeID(sidebar.id)); |
michael@0 | 1258 | |
michael@0 | 1259 | assert.ok(!isChecked(menuitem), 'menuitem is not checked'); |
michael@0 | 1260 | |
michael@0 | 1261 | sidebar.show(); |
michael@0 | 1262 | } |
michael@0 | 1263 | |
michael@0 | 1264 | exports.testDestroyWhileNonBrowserWindowIsOpen = function(assert, done) { |
michael@0 | 1265 | const { Sidebar } = require('sdk/ui/sidebar'); |
michael@0 | 1266 | let testName = 'testDestroyWhileNonBrowserWindowIsOpen'; |
michael@0 | 1267 | let url = 'data:text/html;charset=utf-8,' + testName; |
michael@0 | 1268 | |
michael@0 | 1269 | let sidebar = Sidebar({ |
michael@0 | 1270 | id: testName, |
michael@0 | 1271 | title: testName, |
michael@0 | 1272 | url: url |
michael@0 | 1273 | }); |
michael@0 | 1274 | |
michael@0 | 1275 | open('chrome://browser/content/preferences/preferences.xul').then(function(window) { |
michael@0 | 1276 | try { |
michael@0 | 1277 | sidebar.show(); |
michael@0 | 1278 | assert.equal(isSidebarShowing(getMostRecentBrowserWindow()), true, 'the sidebar is showing'); |
michael@0 | 1279 | |
michael@0 | 1280 | sidebar.destroy(); |
michael@0 | 1281 | |
michael@0 | 1282 | assert.pass('sidebar was destroyed while a non browser window was open'); |
michael@0 | 1283 | } |
michael@0 | 1284 | catch(e) { |
michael@0 | 1285 | assert.fail(e); |
michael@0 | 1286 | } |
michael@0 | 1287 | |
michael@0 | 1288 | return window; |
michael@0 | 1289 | }).then(close).then(function() { |
michael@0 | 1290 | assert.equal(isSidebarShowing(getMostRecentBrowserWindow()), false, 'the sidebar is not showing'); |
michael@0 | 1291 | }).then(done, assert.fail); |
michael@0 | 1292 | } |
michael@0 | 1293 | |
michael@0 | 1294 | exports.testEventListeners = function(assert, done) { |
michael@0 | 1295 | const { Sidebar } = require('sdk/ui/sidebar'); |
michael@0 | 1296 | let testName = 'testWhatThisIsInSidebarEventListeners'; |
michael@0 | 1297 | let eventListenerOrder = []; |
michael@0 | 1298 | |
michael@0 | 1299 | let constructorOnShow = defer(); |
michael@0 | 1300 | let constructorOnHide = defer(); |
michael@0 | 1301 | let constructorOnAttach = defer(); |
michael@0 | 1302 | let constructorOnReady = defer(); |
michael@0 | 1303 | |
michael@0 | 1304 | let onShow = defer(); |
michael@0 | 1305 | let onHide = defer(); |
michael@0 | 1306 | let onAttach = defer(); |
michael@0 | 1307 | let onReady = defer(); |
michael@0 | 1308 | |
michael@0 | 1309 | let onceShow = defer(); |
michael@0 | 1310 | let onceHide = defer(); |
michael@0 | 1311 | let onceAttach = defer(); |
michael@0 | 1312 | let onceReady = defer(); |
michael@0 | 1313 | |
michael@0 | 1314 | function testThis() { |
michael@0 | 1315 | assert(this, sidebar, '`this` is correct'); |
michael@0 | 1316 | } |
michael@0 | 1317 | |
michael@0 | 1318 | let sidebar = Sidebar({ |
michael@0 | 1319 | id: testName, |
michael@0 | 1320 | title: testName, |
michael@0 | 1321 | url: 'data:text/html;charset=utf-8,' + testName, |
michael@0 | 1322 | onShow: function() { |
michael@0 | 1323 | assert.equal(this, sidebar, '`this` is correct in onShow'); |
michael@0 | 1324 | eventListenerOrder.push('onShow'); |
michael@0 | 1325 | constructorOnShow.resolve(); |
michael@0 | 1326 | }, |
michael@0 | 1327 | onAttach: function() { |
michael@0 | 1328 | assert.equal(this, sidebar, '`this` is correct in onAttach'); |
michael@0 | 1329 | eventListenerOrder.push('onAttach'); |
michael@0 | 1330 | constructorOnAttach.resolve(); |
michael@0 | 1331 | }, |
michael@0 | 1332 | onReady: function() { |
michael@0 | 1333 | assert.equal(this, sidebar, '`this` is correct in onReady'); |
michael@0 | 1334 | eventListenerOrder.push('onReady'); |
michael@0 | 1335 | constructorOnReady.resolve(); |
michael@0 | 1336 | }, |
michael@0 | 1337 | onHide: function() { |
michael@0 | 1338 | assert.equal(this, sidebar, '`this` is correct in onHide'); |
michael@0 | 1339 | eventListenerOrder.push('onHide'); |
michael@0 | 1340 | constructorOnHide.resolve(); |
michael@0 | 1341 | } |
michael@0 | 1342 | }); |
michael@0 | 1343 | |
michael@0 | 1344 | sidebar.once('show', function() { |
michael@0 | 1345 | assert.equal(this, sidebar, '`this` is correct in once show'); |
michael@0 | 1346 | eventListenerOrder.push('once show'); |
michael@0 | 1347 | onceShow.resolve(); |
michael@0 | 1348 | }); |
michael@0 | 1349 | sidebar.once('attach', function() { |
michael@0 | 1350 | assert.equal(this, sidebar, '`this` is correct in once attach'); |
michael@0 | 1351 | eventListenerOrder.push('once attach'); |
michael@0 | 1352 | onceAttach.resolve(); |
michael@0 | 1353 | }); |
michael@0 | 1354 | sidebar.once('ready', function() { |
michael@0 | 1355 | assert.equal(this, sidebar, '`this` is correct in once ready'); |
michael@0 | 1356 | eventListenerOrder.push('once ready'); |
michael@0 | 1357 | onceReady.resolve(); |
michael@0 | 1358 | }); |
michael@0 | 1359 | sidebar.once('hide', function() { |
michael@0 | 1360 | assert.equal(this, sidebar, '`this` is correct in once hide'); |
michael@0 | 1361 | eventListenerOrder.push('once hide'); |
michael@0 | 1362 | onceHide.resolve(); |
michael@0 | 1363 | }); |
michael@0 | 1364 | |
michael@0 | 1365 | sidebar.on('show', function() { |
michael@0 | 1366 | assert.equal(this, sidebar, '`this` is correct in on show'); |
michael@0 | 1367 | eventListenerOrder.push('on show'); |
michael@0 | 1368 | onShow.resolve(); |
michael@0 | 1369 | |
michael@0 | 1370 | sidebar.hide(); |
michael@0 | 1371 | }); |
michael@0 | 1372 | sidebar.on('attach', function() { |
michael@0 | 1373 | assert.equal(this, sidebar, '`this` is correct in on attach'); |
michael@0 | 1374 | eventListenerOrder.push('on attach'); |
michael@0 | 1375 | onAttach.resolve(); |
michael@0 | 1376 | }); |
michael@0 | 1377 | sidebar.on('ready', function() { |
michael@0 | 1378 | assert.equal(this, sidebar, '`this` is correct in on ready'); |
michael@0 | 1379 | eventListenerOrder.push('on ready'); |
michael@0 | 1380 | onReady.resolve(); |
michael@0 | 1381 | }); |
michael@0 | 1382 | sidebar.on('hide', function() { |
michael@0 | 1383 | assert.equal(this, sidebar, '`this` is correct in on hide'); |
michael@0 | 1384 | eventListenerOrder.push('on hide'); |
michael@0 | 1385 | onHide.resolve(); |
michael@0 | 1386 | }); |
michael@0 | 1387 | |
michael@0 | 1388 | all([constructorOnShow.promise, |
michael@0 | 1389 | constructorOnAttach.promise, |
michael@0 | 1390 | constructorOnReady.promise, |
michael@0 | 1391 | constructorOnHide.promise, |
michael@0 | 1392 | onceShow.promise, |
michael@0 | 1393 | onceAttach.promise, |
michael@0 | 1394 | onceReady.promise, |
michael@0 | 1395 | onceHide.promise, |
michael@0 | 1396 | onShow.promise, |
michael@0 | 1397 | onAttach.promise, |
michael@0 | 1398 | onReady.promise, |
michael@0 | 1399 | onHide.promise]).then(function() { |
michael@0 | 1400 | assert.equal(eventListenerOrder.join(), [ |
michael@0 | 1401 | 'onAttach', |
michael@0 | 1402 | 'once attach', |
michael@0 | 1403 | 'on attach', |
michael@0 | 1404 | 'onReady', |
michael@0 | 1405 | 'once ready', |
michael@0 | 1406 | 'on ready', |
michael@0 | 1407 | 'onShow', |
michael@0 | 1408 | 'once show', |
michael@0 | 1409 | 'on show', |
michael@0 | 1410 | 'onHide', |
michael@0 | 1411 | 'once hide', |
michael@0 | 1412 | 'on hide' |
michael@0 | 1413 | ].join(), 'the event order was correct'); |
michael@0 | 1414 | sidebar.destroy(); |
michael@0 | 1415 | }).then(done, assert.fail); |
michael@0 | 1416 | |
michael@0 | 1417 | sidebar.show(); |
michael@0 | 1418 | } |
michael@0 | 1419 | |
michael@0 | 1420 | // For more information see Bug 920780 |
michael@0 | 1421 | exports.testAttachDoesNotEmitWhenShown = function(assert, done) { |
michael@0 | 1422 | const { Sidebar } = require('sdk/ui/sidebar'); |
michael@0 | 1423 | let testName = 'testSidebarLeakCheckUnloadAfterAttach'; |
michael@0 | 1424 | let count = 0; |
michael@0 | 1425 | |
michael@0 | 1426 | let sidebar = Sidebar({ |
michael@0 | 1427 | id: testName, |
michael@0 | 1428 | title: testName, |
michael@0 | 1429 | url: 'data:text/html;charset=utf-8,'+testName, |
michael@0 | 1430 | onAttach: function() { |
michael@0 | 1431 | if (count > 2) { |
michael@0 | 1432 | assert.fail('sidebar was attached again..'); |
michael@0 | 1433 | } |
michael@0 | 1434 | else { |
michael@0 | 1435 | assert.pass('sidebar was attached ' + count + ' time(s)'); |
michael@0 | 1436 | } |
michael@0 | 1437 | |
michael@0 | 1438 | if (++count == 1) { |
michael@0 | 1439 | setImmediate(function() { |
michael@0 | 1440 | let shownFired = 0; |
michael@0 | 1441 | let onShow = () => shownFired++; |
michael@0 | 1442 | sidebar.on('show', onShow); |
michael@0 | 1443 | |
michael@0 | 1444 | sidebar.show() |
michael@0 | 1445 | .then(() => assert.equal(shownFired, 0, 'shown should not be fired again when already showing from after attach')) |
michael@0 | 1446 | .then(sidebar.hide.bind(sidebar)) |
michael@0 | 1447 | .then(sidebar.show.bind(sidebar)) |
michael@0 | 1448 | .then(() => assert.equal(shownFired, 1, 'shown was emitted when `show` called after being hidden')) |
michael@0 | 1449 | .then(sidebar.show.bind(sidebar)) |
michael@0 | 1450 | .then(() => { |
michael@0 | 1451 | assert.equal(shownFired, 1, 'shown was not emitted again if already being shown'); |
michael@0 | 1452 | sidebar.off('show', onShow); |
michael@0 | 1453 | sidebar.destroy(); |
michael@0 | 1454 | }).catch(assert.fail).then(done); |
michael@0 | 1455 | }); |
michael@0 | 1456 | } |
michael@0 | 1457 | } |
michael@0 | 1458 | }); |
michael@0 | 1459 | |
michael@0 | 1460 | sidebar.show(); |
michael@0 | 1461 | } |
michael@0 | 1462 | |
michael@0 | 1463 | // If the module doesn't support the app we're being run in, require() will |
michael@0 | 1464 | // throw. In that case, remove all tests above from exports, and add one dummy |
michael@0 | 1465 | // test that passes. |
michael@0 | 1466 | try { |
michael@0 | 1467 | require('sdk/ui/sidebar'); |
michael@0 | 1468 | } |
michael@0 | 1469 | catch (err) { |
michael@0 | 1470 | if (!/^Unsupported Application/.test(err.message)) |
michael@0 | 1471 | throw err; |
michael@0 | 1472 | |
michael@0 | 1473 | module.exports = { |
michael@0 | 1474 | 'test Unsupported Application': assert => assert.pass(err.message) |
michael@0 | 1475 | } |
michael@0 | 1476 | } |
michael@0 | 1477 | |
michael@0 | 1478 | require('sdk/test').run(exports); |