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 | const { Cc, Ci } = require('chrome'); |
michael@0 | 7 | const { Loader, LoaderWithHookedConsole } = require('sdk/test/loader'); |
michael@0 | 8 | const timer = require('sdk/timers'); |
michael@0 | 9 | const tabs = require('sdk/tabs'); |
michael@0 | 10 | const windows = require('sdk/windows'); |
michael@0 | 11 | const { set: setPref } = require("sdk/preferences/service"); |
michael@0 | 12 | const DEPRECATE_PREF = "devtools.errorconsole.deprecation_warnings"; |
michael@0 | 13 | |
michael@0 | 14 | const tabsLen = tabs.length; |
michael@0 | 15 | const URL = 'data:text/html;charset=utf-8,<html><head><title>#title#</title></head></html>'; |
michael@0 | 16 | |
michael@0 | 17 | // Fennec error message dispatched on all currently unimplement tab features, |
michael@0 | 18 | // that match LoaderWithHookedConsole messages object pattern |
michael@0 | 19 | const ERR_FENNEC_MSG = { |
michael@0 | 20 | type: "error", |
michael@0 | 21 | msg: "This method is not yet supported by Fennec" |
michael@0 | 22 | }; |
michael@0 | 23 | |
michael@0 | 24 | // TEST: tab unloader |
michael@0 | 25 | exports.testAutomaticDestroy = function(assert, done) { |
michael@0 | 26 | let called = false; |
michael@0 | 27 | |
michael@0 | 28 | let loader2 = Loader(module); |
michael@0 | 29 | let loader3 = Loader(module); |
michael@0 | 30 | let tabs2 = loader2.require('sdk/tabs'); |
michael@0 | 31 | let tabs3 = loader3.require('sdk/tabs'); |
michael@0 | 32 | let tabs2Len = tabs2.length; |
michael@0 | 33 | |
michael@0 | 34 | tabs2.on('open', function onOpen(tab) { |
michael@0 | 35 | assert.fail("an onOpen listener was called that should not have been"); |
michael@0 | 36 | called = true; |
michael@0 | 37 | }); |
michael@0 | 38 | tabs2.on('ready', function onReady(tab) { |
michael@0 | 39 | assert.fail("an onReady listener was called that should not have been"); |
michael@0 | 40 | called = true; |
michael@0 | 41 | }); |
michael@0 | 42 | tabs2.on('select', function onSelect(tab) { |
michael@0 | 43 | assert.fail("an onSelect listener was called that should not have been"); |
michael@0 | 44 | called = true; |
michael@0 | 45 | }); |
michael@0 | 46 | tabs2.on('close', function onClose(tab) { |
michael@0 | 47 | assert.fail("an onClose listener was called that should not have been"); |
michael@0 | 48 | called = true; |
michael@0 | 49 | }); |
michael@0 | 50 | loader2.unload(); |
michael@0 | 51 | |
michael@0 | 52 | tabs3.on('open', function onOpen(tab) { |
michael@0 | 53 | assert.pass("an onOpen listener was called for tabs3"); |
michael@0 | 54 | |
michael@0 | 55 | tab.on('ready', function onReady(tab) { |
michael@0 | 56 | assert.fail("an onReady listener was called that should not have been"); |
michael@0 | 57 | called = true; |
michael@0 | 58 | }); |
michael@0 | 59 | tab.on('select', function onSelect(tab) { |
michael@0 | 60 | assert.fail("an onSelect listener was called that should not have been"); |
michael@0 | 61 | called = true; |
michael@0 | 62 | }); |
michael@0 | 63 | tab.on('close', function onClose(tab) { |
michael@0 | 64 | assert.fail("an onClose listener was called that should not have been"); |
michael@0 | 65 | called = true; |
michael@0 | 66 | }); |
michael@0 | 67 | }); |
michael@0 | 68 | tabs3.open(URL.replace(/#title#/, 'tabs3')); |
michael@0 | 69 | loader3.unload(); |
michael@0 | 70 | |
michael@0 | 71 | // Fire a tab event and ensure that the destroyed tab is inactive |
michael@0 | 72 | tabs.once('open', function(tab) { |
michael@0 | 73 | assert.pass('tabs.once("open") works!'); |
michael@0 | 74 | |
michael@0 | 75 | assert.equal(tabs2Len, tabs2.length, "tabs2 length was not changed"); |
michael@0 | 76 | assert.equal(tabs.length, (tabs2.length+2), "tabs.length > tabs2.length"); |
michael@0 | 77 | |
michael@0 | 78 | tab.once('ready', function() { |
michael@0 | 79 | assert.pass('tab.once("ready") works!'); |
michael@0 | 80 | |
michael@0 | 81 | tab.once('close', function() { |
michael@0 | 82 | assert.pass('tab.once("close") works!'); |
michael@0 | 83 | |
michael@0 | 84 | timer.setTimeout(function () { |
michael@0 | 85 | assert.ok(!called, "Unloaded tab module is destroyed and inactive"); |
michael@0 | 86 | |
michael@0 | 87 | // end test |
michael@0 | 88 | done(); |
michael@0 | 89 | }); |
michael@0 | 90 | }); |
michael@0 | 91 | |
michael@0 | 92 | tab.close(); |
michael@0 | 93 | }); |
michael@0 | 94 | }); |
michael@0 | 95 | |
michael@0 | 96 | tabs.open('data:text/html;charset=utf-8,foo'); |
michael@0 | 97 | }; |
michael@0 | 98 | |
michael@0 | 99 | // TEST: tab properties |
michael@0 | 100 | exports.testTabProperties = function(assert, done) { |
michael@0 | 101 | setPref(DEPRECATE_PREF, true); |
michael@0 | 102 | let { loader, messages } = LoaderWithHookedConsole(); |
michael@0 | 103 | let tabs = loader.require('sdk/tabs'); |
michael@0 | 104 | |
michael@0 | 105 | let url = "data:text/html;charset=utf-8,<html><head><title>foo</title></head><body>foo</body></html>"; |
michael@0 | 106 | let tabsLen = tabs.length; |
michael@0 | 107 | tabs.open({ |
michael@0 | 108 | url: url, |
michael@0 | 109 | onReady: function(tab) { |
michael@0 | 110 | assert.equal(tab.title, "foo", "title of the new tab matches"); |
michael@0 | 111 | assert.equal(tab.url, url, "URL of the new tab matches"); |
michael@0 | 112 | assert.ok(tab.favicon, "favicon of the new tab is not empty"); |
michael@0 | 113 | // TODO: remove need for this test by implementing the favicon feature |
michael@0 | 114 | assert.equal(messages[0].msg, |
michael@0 | 115 | "tab.favicon is deprecated, and " + |
michael@0 | 116 | "currently favicon helpers are not yet supported " + |
michael@0 | 117 | "by Fennec", |
michael@0 | 118 | "favicon logs an error for now"); |
michael@0 | 119 | assert.equal(tab.style, null, "style of the new tab matches"); |
michael@0 | 120 | assert.equal(tab.index, tabsLen, "index of the new tab matches"); |
michael@0 | 121 | assert.notEqual(tab.getThumbnail(), null, "thumbnail of the new tab matches"); |
michael@0 | 122 | assert.notEqual(tab.id, null, "a tab object always has an id property"); |
michael@0 | 123 | |
michael@0 | 124 | tab.close(function() { |
michael@0 | 125 | loader.unload(); |
michael@0 | 126 | |
michael@0 | 127 | // end test |
michael@0 | 128 | done(); |
michael@0 | 129 | }); |
michael@0 | 130 | } |
michael@0 | 131 | }); |
michael@0 | 132 | }; |
michael@0 | 133 | |
michael@0 | 134 | // TEST: tabs iterator and length property |
michael@0 | 135 | exports.testTabsIteratorAndLength = function(assert, done) { |
michael@0 | 136 | let newTabs = []; |
michael@0 | 137 | let startCount = 0; |
michael@0 | 138 | for each (let t in tabs) startCount++; |
michael@0 | 139 | |
michael@0 | 140 | assert.equal(startCount, tabs.length, "length property is correct"); |
michael@0 | 141 | |
michael@0 | 142 | let url = "data:text/html;charset=utf-8,testTabsIteratorAndLength"; |
michael@0 | 143 | tabs.open({url: url, onOpen: function(tab) newTabs.push(tab)}); |
michael@0 | 144 | tabs.open({url: url, onOpen: function(tab) newTabs.push(tab)}); |
michael@0 | 145 | tabs.open({ |
michael@0 | 146 | url: url, |
michael@0 | 147 | onOpen: function(tab) { |
michael@0 | 148 | let count = 0; |
michael@0 | 149 | for each (let t in tabs) count++; |
michael@0 | 150 | assert.equal(count, startCount + 3, "iterated tab count matches"); |
michael@0 | 151 | assert.equal(startCount + 3, tabs.length, "iterated tab count matches length property"); |
michael@0 | 152 | |
michael@0 | 153 | let newTabsLength = newTabs.length; |
michael@0 | 154 | newTabs.forEach(function(t) t.close(function() { |
michael@0 | 155 | if (--newTabsLength > 0) return; |
michael@0 | 156 | |
michael@0 | 157 | tab.close(done); |
michael@0 | 158 | })); |
michael@0 | 159 | } |
michael@0 | 160 | }); |
michael@0 | 161 | }; |
michael@0 | 162 | |
michael@0 | 163 | // TEST: tab.url setter |
michael@0 | 164 | exports.testTabLocation = function(assert, done) { |
michael@0 | 165 | let url1 = "data:text/html;charset=utf-8,foo"; |
michael@0 | 166 | let url2 = "data:text/html;charset=utf-8,bar"; |
michael@0 | 167 | |
michael@0 | 168 | tabs.on('ready', function onReady(tab) { |
michael@0 | 169 | if (tab.url != url2) |
michael@0 | 170 | return; |
michael@0 | 171 | |
michael@0 | 172 | tabs.removeListener('ready', onReady); |
michael@0 | 173 | assert.pass("tab loaded the correct url"); |
michael@0 | 174 | |
michael@0 | 175 | tab.close(done); |
michael@0 | 176 | }); |
michael@0 | 177 | |
michael@0 | 178 | tabs.open({ |
michael@0 | 179 | url: url1, |
michael@0 | 180 | onOpen: function(tab) { |
michael@0 | 181 | tab.url = url2; |
michael@0 | 182 | } |
michael@0 | 183 | }); |
michael@0 | 184 | }; |
michael@0 | 185 | |
michael@0 | 186 | // TEST: tab.move() |
michael@0 | 187 | exports.testTabMove = function(assert, done) { |
michael@0 | 188 | let { loader, messages } = LoaderWithHookedConsole(); |
michael@0 | 189 | let tabs = loader.require('sdk/tabs'); |
michael@0 | 190 | |
michael@0 | 191 | let url = "data:text/html;charset=utf-8,testTabMove"; |
michael@0 | 192 | |
michael@0 | 193 | tabs.open({ |
michael@0 | 194 | url: url, |
michael@0 | 195 | onOpen: function(tab1) { |
michael@0 | 196 | assert.ok(tab1.index >= 0, "opening a tab returns a tab w/ valid index"); |
michael@0 | 197 | |
michael@0 | 198 | tabs.open({ |
michael@0 | 199 | url: url, |
michael@0 | 200 | onOpen: function(tab) { |
michael@0 | 201 | let i = tab.index; |
michael@0 | 202 | assert.ok(tab.index > tab1.index, "2nd tab has valid index"); |
michael@0 | 203 | tab.index = 0; |
michael@0 | 204 | assert.equal(tab.index, i, "tab index after move matches"); |
michael@0 | 205 | assert.equal(JSON.stringify(messages), |
michael@0 | 206 | JSON.stringify([ERR_FENNEC_MSG]), |
michael@0 | 207 | "setting tab.index logs error"); |
michael@0 | 208 | // end test |
michael@0 | 209 | tab1.close(function() tab.close(function() { |
michael@0 | 210 | loader.unload(); |
michael@0 | 211 | done(); |
michael@0 | 212 | })); |
michael@0 | 213 | } |
michael@0 | 214 | }); |
michael@0 | 215 | } |
michael@0 | 216 | }); |
michael@0 | 217 | }; |
michael@0 | 218 | |
michael@0 | 219 | // TEST: open tab with default options |
michael@0 | 220 | exports.testTabsOpen_alt = function(assert, done) { |
michael@0 | 221 | let { loader, messages } = LoaderWithHookedConsole(); |
michael@0 | 222 | let tabs = loader.require('sdk/tabs'); |
michael@0 | 223 | let url = "data:text/html;charset=utf-8,default"; |
michael@0 | 224 | |
michael@0 | 225 | tabs.open({ |
michael@0 | 226 | url: url, |
michael@0 | 227 | onReady: function(tab) { |
michael@0 | 228 | assert.equal(tab.url, url, "URL of the new tab matches"); |
michael@0 | 229 | assert.equal(tabs.activeTab, tab, "URL of active tab in the current window matches"); |
michael@0 | 230 | assert.equal(tab.isPinned, false, "The new tab is not pinned"); |
michael@0 | 231 | assert.equal(messages.length, 1, "isPinned logs error"); |
michael@0 | 232 | |
michael@0 | 233 | // end test |
michael@0 | 234 | tab.close(function() { |
michael@0 | 235 | loader.unload(); |
michael@0 | 236 | done(); |
michael@0 | 237 | }); |
michael@0 | 238 | } |
michael@0 | 239 | }); |
michael@0 | 240 | }; |
michael@0 | 241 | |
michael@0 | 242 | // TEST: open pinned tab |
michael@0 | 243 | exports.testOpenPinned_alt = function(assert, done) { |
michael@0 | 244 | let { loader, messages } = LoaderWithHookedConsole(); |
michael@0 | 245 | let tabs = loader.require('sdk/tabs'); |
michael@0 | 246 | let url = "about:blank"; |
michael@0 | 247 | |
michael@0 | 248 | tabs.open({ |
michael@0 | 249 | url: url, |
michael@0 | 250 | isPinned: true, |
michael@0 | 251 | onOpen: function(tab) { |
michael@0 | 252 | assert.equal(tab.isPinned, false, "The new tab is pinned"); |
michael@0 | 253 | // We get two error message: one for tabs.open's isPinned argument |
michael@0 | 254 | // and another one for tab.isPinned |
michael@0 | 255 | assert.equal(JSON.stringify(messages), |
michael@0 | 256 | JSON.stringify([ERR_FENNEC_MSG, ERR_FENNEC_MSG]), |
michael@0 | 257 | "isPinned logs error"); |
michael@0 | 258 | |
michael@0 | 259 | // end test |
michael@0 | 260 | tab.close(function() { |
michael@0 | 261 | loader.unload(); |
michael@0 | 262 | done(); |
michael@0 | 263 | }); |
michael@0 | 264 | } |
michael@0 | 265 | }); |
michael@0 | 266 | }; |
michael@0 | 267 | |
michael@0 | 268 | // TEST: pin/unpin opened tab |
michael@0 | 269 | exports.testPinUnpin_alt = function(assert, done) { |
michael@0 | 270 | let { loader, messages } = LoaderWithHookedConsole(); |
michael@0 | 271 | let tabs = loader.require('sdk/tabs'); |
michael@0 | 272 | let url = "data:text/html;charset=utf-8,default"; |
michael@0 | 273 | |
michael@0 | 274 | tabs.open({ |
michael@0 | 275 | url: url, |
michael@0 | 276 | onOpen: function(tab) { |
michael@0 | 277 | tab.pin(); |
michael@0 | 278 | assert.equal(tab.isPinned, false, "The tab was pinned correctly"); |
michael@0 | 279 | assert.equal(JSON.stringify(messages), |
michael@0 | 280 | JSON.stringify([ERR_FENNEC_MSG, ERR_FENNEC_MSG]), |
michael@0 | 281 | "tab.pin() logs error"); |
michael@0 | 282 | |
michael@0 | 283 | // Clear console messages for the following test |
michael@0 | 284 | messages.length = 0; |
michael@0 | 285 | |
michael@0 | 286 | tab.unpin(); |
michael@0 | 287 | assert.equal(tab.isPinned, false, "The tab was unpinned correctly"); |
michael@0 | 288 | assert.equal(JSON.stringify(messages), |
michael@0 | 289 | JSON.stringify([ERR_FENNEC_MSG, ERR_FENNEC_MSG]), |
michael@0 | 290 | "tab.unpin() logs error"); |
michael@0 | 291 | |
michael@0 | 292 | // end test |
michael@0 | 293 | tab.close(function() { |
michael@0 | 294 | loader.unload(); |
michael@0 | 295 | done(); |
michael@0 | 296 | }); |
michael@0 | 297 | } |
michael@0 | 298 | }); |
michael@0 | 299 | }; |
michael@0 | 300 | |
michael@0 | 301 | // TEST: open tab in background |
michael@0 | 302 | exports.testInBackground = function(assert, done) { |
michael@0 | 303 | let activeUrl = tabs.activeTab.url; |
michael@0 | 304 | let url = "data:text/html;charset=utf-8,background"; |
michael@0 | 305 | let window = windows.browserWindows.activeWindow; |
michael@0 | 306 | tabs.once('ready', function onReady(tab) { |
michael@0 | 307 | assert.equal(tabs.activeTab.url, activeUrl, "URL of active tab has not changed"); |
michael@0 | 308 | assert.equal(tab.url, url, "URL of the new background tab matches"); |
michael@0 | 309 | assert.equal(windows.browserWindows.activeWindow, window, "a new window was not opened"); |
michael@0 | 310 | assert.notEqual(tabs.activeTab.url, url, "URL of active tab is not the new URL"); |
michael@0 | 311 | |
michael@0 | 312 | // end test |
michael@0 | 313 | tab.close(done); |
michael@0 | 314 | }); |
michael@0 | 315 | |
michael@0 | 316 | tabs.open({ |
michael@0 | 317 | url: url, |
michael@0 | 318 | inBackground: true |
michael@0 | 319 | }); |
michael@0 | 320 | }; |
michael@0 | 321 | |
michael@0 | 322 | // TEST: open tab in new window |
michael@0 | 323 | exports.testOpenInNewWindow = function(assert, done) { |
michael@0 | 324 | let url = "data:text/html;charset=utf-8,newwindow"; |
michael@0 | 325 | let window = windows.browserWindows.activeWindow; |
michael@0 | 326 | |
michael@0 | 327 | tabs.open({ |
michael@0 | 328 | url: url, |
michael@0 | 329 | inNewWindow: true, |
michael@0 | 330 | onReady: function(tab) { |
michael@0 | 331 | assert.equal(windows.browserWindows.length, 1, "a new window was not opened"); |
michael@0 | 332 | assert.equal(windows.browserWindows.activeWindow, window, "old window is active"); |
michael@0 | 333 | assert.equal(tab.url, url, "URL of the new tab matches"); |
michael@0 | 334 | assert.equal(tabs.activeTab, tab, "tab is the activeTab"); |
michael@0 | 335 | |
michael@0 | 336 | tab.close(done); |
michael@0 | 337 | } |
michael@0 | 338 | }); |
michael@0 | 339 | }; |
michael@0 | 340 | |
michael@0 | 341 | // TEST: onOpen event handler |
michael@0 | 342 | exports.testTabsEvent_onOpen = function(assert, done) { |
michael@0 | 343 | let url = URL.replace('#title#', 'testTabsEvent_onOpen'); |
michael@0 | 344 | let eventCount = 0; |
michael@0 | 345 | |
michael@0 | 346 | // add listener via property assignment |
michael@0 | 347 | function listener1(tab) { |
michael@0 | 348 | eventCount++; |
michael@0 | 349 | }; |
michael@0 | 350 | tabs.on('open', listener1); |
michael@0 | 351 | |
michael@0 | 352 | // add listener via collection add |
michael@0 | 353 | tabs.on('open', function listener2(tab) { |
michael@0 | 354 | assert.equal(++eventCount, 2, "both listeners notified"); |
michael@0 | 355 | tabs.removeListener('open', listener1); |
michael@0 | 356 | tabs.removeListener('open', listener2); |
michael@0 | 357 | |
michael@0 | 358 | // ends test |
michael@0 | 359 | tab.close(done); |
michael@0 | 360 | }); |
michael@0 | 361 | |
michael@0 | 362 | tabs.open(url); |
michael@0 | 363 | }; |
michael@0 | 364 | |
michael@0 | 365 | // TEST: onClose event handler |
michael@0 | 366 | exports.testTabsEvent_onClose = function(assert, done) { |
michael@0 | 367 | let url = "data:text/html;charset=utf-8,onclose"; |
michael@0 | 368 | let eventCount = 0; |
michael@0 | 369 | |
michael@0 | 370 | // add listener via property assignment |
michael@0 | 371 | function listener1(tab) { |
michael@0 | 372 | eventCount++; |
michael@0 | 373 | } |
michael@0 | 374 | tabs.on('close', listener1); |
michael@0 | 375 | |
michael@0 | 376 | // add listener via collection add |
michael@0 | 377 | tabs.on('close', function listener2(tab) { |
michael@0 | 378 | assert.equal(++eventCount, 2, "both listeners notified"); |
michael@0 | 379 | tabs.removeListener('close', listener1); |
michael@0 | 380 | tabs.removeListener('close', listener2); |
michael@0 | 381 | |
michael@0 | 382 | // end test |
michael@0 | 383 | done(); |
michael@0 | 384 | }); |
michael@0 | 385 | |
michael@0 | 386 | tabs.on('ready', function onReady(tab) { |
michael@0 | 387 | tabs.removeListener('ready', onReady); |
michael@0 | 388 | tab.close(); |
michael@0 | 389 | }); |
michael@0 | 390 | |
michael@0 | 391 | tabs.open(url); |
michael@0 | 392 | }; |
michael@0 | 393 | |
michael@0 | 394 | // TEST: onClose event handler when a window is closed |
michael@0 | 395 | exports.testTabsEvent_onCloseWindow = function(assert, done) { |
michael@0 | 396 | let closeCount = 0, individualCloseCount = 0; |
michael@0 | 397 | function listener() { |
michael@0 | 398 | closeCount++; |
michael@0 | 399 | } |
michael@0 | 400 | tabs.on('close', listener); |
michael@0 | 401 | |
michael@0 | 402 | // One tab is already open with the window |
michael@0 | 403 | let openTabs = 0; |
michael@0 | 404 | function testCasePossiblyLoaded(tab) { |
michael@0 | 405 | tab.close(function() { |
michael@0 | 406 | if (++openTabs == 3) { |
michael@0 | 407 | tabs.removeListener("close", listener); |
michael@0 | 408 | |
michael@0 | 409 | assert.equal(closeCount, 3, "Correct number of close events received"); |
michael@0 | 410 | assert.equal(individualCloseCount, 3, |
michael@0 | 411 | "Each tab with an attached onClose listener received a close " + |
michael@0 | 412 | "event when the window was closed"); |
michael@0 | 413 | |
michael@0 | 414 | done(); |
michael@0 | 415 | } |
michael@0 | 416 | }); |
michael@0 | 417 | } |
michael@0 | 418 | |
michael@0 | 419 | tabs.open({ |
michael@0 | 420 | url: "data:text/html;charset=utf-8,tab2", |
michael@0 | 421 | onOpen: testCasePossiblyLoaded, |
michael@0 | 422 | onClose: function() individualCloseCount++ |
michael@0 | 423 | }); |
michael@0 | 424 | |
michael@0 | 425 | tabs.open({ |
michael@0 | 426 | url: "data:text/html;charset=utf-8,tab3", |
michael@0 | 427 | onOpen: testCasePossiblyLoaded, |
michael@0 | 428 | onClose: function() individualCloseCount++ |
michael@0 | 429 | }); |
michael@0 | 430 | |
michael@0 | 431 | tabs.open({ |
michael@0 | 432 | url: "data:text/html;charset=utf-8,tab4", |
michael@0 | 433 | onOpen: testCasePossiblyLoaded, |
michael@0 | 434 | onClose: function() individualCloseCount++ |
michael@0 | 435 | }); |
michael@0 | 436 | }; |
michael@0 | 437 | |
michael@0 | 438 | // TEST: onReady event handler |
michael@0 | 439 | exports.testTabsEvent_onReady = function(assert, done) { |
michael@0 | 440 | let url = "data:text/html;charset=utf-8,onready"; |
michael@0 | 441 | let eventCount = 0; |
michael@0 | 442 | |
michael@0 | 443 | // add listener via property assignment |
michael@0 | 444 | function listener1(tab) { |
michael@0 | 445 | eventCount++; |
michael@0 | 446 | }; |
michael@0 | 447 | tabs.on('ready', listener1); |
michael@0 | 448 | |
michael@0 | 449 | // add listener via collection add |
michael@0 | 450 | tabs.on('ready', function listener2(tab) { |
michael@0 | 451 | assert.equal(++eventCount, 2, "both listeners notified"); |
michael@0 | 452 | tabs.removeListener('ready', listener1); |
michael@0 | 453 | tabs.removeListener('ready', listener2); |
michael@0 | 454 | |
michael@0 | 455 | // end test |
michael@0 | 456 | tab.close(done); |
michael@0 | 457 | }); |
michael@0 | 458 | |
michael@0 | 459 | tabs.open(url); |
michael@0 | 460 | }; |
michael@0 | 461 | |
michael@0 | 462 | // TEST: onActivate event handler |
michael@0 | 463 | exports.testTabsEvent_onActivate = function(assert, done) { |
michael@0 | 464 | let url = "data:text/html;charset=utf-8,onactivate"; |
michael@0 | 465 | let eventCount = 0; |
michael@0 | 466 | |
michael@0 | 467 | // add listener via property assignment |
michael@0 | 468 | function listener1(tab) { |
michael@0 | 469 | eventCount++; |
michael@0 | 470 | }; |
michael@0 | 471 | tabs.on('activate', listener1); |
michael@0 | 472 | |
michael@0 | 473 | // add listener via collection add |
michael@0 | 474 | tabs.on('activate', function listener2(tab) { |
michael@0 | 475 | assert.equal(++eventCount, 2, "both listeners notified"); |
michael@0 | 476 | assert.equal(tab, tabs.activeTab, 'the active tab is correct'); |
michael@0 | 477 | tabs.removeListener('activate', listener1); |
michael@0 | 478 | tabs.removeListener('activate', listener2); |
michael@0 | 479 | |
michael@0 | 480 | // end test |
michael@0 | 481 | tab.close(done); |
michael@0 | 482 | }); |
michael@0 | 483 | |
michael@0 | 484 | tabs.open(url); |
michael@0 | 485 | }; |
michael@0 | 486 | |
michael@0 | 487 | // TEST: onDeactivate event handler |
michael@0 | 488 | exports.testTabsEvent_onDeactivate = function(assert, done) { |
michael@0 | 489 | let url = "data:text/html;charset=utf-8,ondeactivate"; |
michael@0 | 490 | let eventCount = 0; |
michael@0 | 491 | |
michael@0 | 492 | // add listener via property assignment |
michael@0 | 493 | function listener1(tab) { |
michael@0 | 494 | eventCount++; |
michael@0 | 495 | }; |
michael@0 | 496 | tabs.on('deactivate', listener1); |
michael@0 | 497 | |
michael@0 | 498 | // add listener via collection add |
michael@0 | 499 | tabs.on('deactivate', function listener2(tab) { |
michael@0 | 500 | assert.equal(++eventCount, 2, 'both listeners notified'); |
michael@0 | 501 | assert.notEqual(tab, tabs.activeTab, 'the active tab is not the deactivated tab'); |
michael@0 | 502 | tabs.removeListener('deactivate', listener1); |
michael@0 | 503 | tabs.removeListener('deactivate', listener2); |
michael@0 | 504 | |
michael@0 | 505 | // end test |
michael@0 | 506 | tab.close(done); |
michael@0 | 507 | }); |
michael@0 | 508 | |
michael@0 | 509 | tabs.on('activate', function onActivate(tab) { |
michael@0 | 510 | tabs.removeListener('activate', onActivate); |
michael@0 | 511 | tabs.open("data:text/html;charset=utf-8,foo"); |
michael@0 | 512 | tab.close(); |
michael@0 | 513 | }); |
michael@0 | 514 | |
michael@0 | 515 | tabs.open(url); |
michael@0 | 516 | }; |
michael@0 | 517 | |
michael@0 | 518 | // TEST: per-tab event handlers |
michael@0 | 519 | exports.testPerTabEvents = function(assert, done) { |
michael@0 | 520 | let eventCount = 0; |
michael@0 | 521 | |
michael@0 | 522 | tabs.open({ |
michael@0 | 523 | url: "data:text/html;charset=utf-8,foo", |
michael@0 | 524 | onOpen: function(tab) { |
michael@0 | 525 | // add listener via property assignment |
michael@0 | 526 | function listener1() { |
michael@0 | 527 | eventCount++; |
michael@0 | 528 | }; |
michael@0 | 529 | tab.on('ready', listener1); |
michael@0 | 530 | |
michael@0 | 531 | // add listener via collection add |
michael@0 | 532 | tab.on('ready', function listener2() { |
michael@0 | 533 | assert.equal(eventCount, 1, "both listeners notified"); |
michael@0 | 534 | tab.removeListener('ready', listener1); |
michael@0 | 535 | tab.removeListener('ready', listener2); |
michael@0 | 536 | |
michael@0 | 537 | // end test |
michael@0 | 538 | tab.close(done); |
michael@0 | 539 | }); |
michael@0 | 540 | } |
michael@0 | 541 | }); |
michael@0 | 542 | }; |
michael@0 | 543 | |
michael@0 | 544 | exports.testUniqueTabIds = function(assert, done) { |
michael@0 | 545 | var tabs = require('sdk/tabs'); |
michael@0 | 546 | var tabIds = {}; |
michael@0 | 547 | var steps = [ |
michael@0 | 548 | function (index) { |
michael@0 | 549 | tabs.open({ |
michael@0 | 550 | url: "data:text/html;charset=utf-8,foo", |
michael@0 | 551 | onOpen: function(tab) { |
michael@0 | 552 | tabIds['tab1'] = tab.id; |
michael@0 | 553 | next(index); |
michael@0 | 554 | } |
michael@0 | 555 | }); |
michael@0 | 556 | }, |
michael@0 | 557 | function (index) { |
michael@0 | 558 | tabs.open({ |
michael@0 | 559 | url: "data:text/html;charset=utf-8,bar", |
michael@0 | 560 | onOpen: function(tab) { |
michael@0 | 561 | tabIds['tab2'] = tab.id; |
michael@0 | 562 | next(index); |
michael@0 | 563 | } |
michael@0 | 564 | }); |
michael@0 | 565 | }, |
michael@0 | 566 | function (index) { |
michael@0 | 567 | assert.notEqual(tabIds.tab1, tabIds.tab2, "Tab ids should be unique."); |
michael@0 | 568 | done(); |
michael@0 | 569 | } |
michael@0 | 570 | ]; |
michael@0 | 571 | |
michael@0 | 572 | function next(index) { |
michael@0 | 573 | if (index === steps.length) { |
michael@0 | 574 | return; |
michael@0 | 575 | } |
michael@0 | 576 | let fn = steps[index]; |
michael@0 | 577 | index++; |
michael@0 | 578 | fn(index); |
michael@0 | 579 | } |
michael@0 | 580 | |
michael@0 | 581 | next(0); |
michael@0 | 582 | } |
michael@0 | 583 | |
michael@0 | 584 | exports.testOnLoadEventWithDOM = function(assert, done) { |
michael@0 | 585 | let count = 0; |
michael@0 | 586 | let title = 'testOnLoadEventWithDOM'; |
michael@0 | 587 | |
michael@0 | 588 | tabs.open({ |
michael@0 | 589 | url: 'data:text/html;charset=utf-8,<title>' + title + '</title>', |
michael@0 | 590 | inBackground: true, |
michael@0 | 591 | onLoad: function(tab) { |
michael@0 | 592 | assert.equal(tab.title, title, 'tab passed in as arg, load called'); |
michael@0 | 593 | |
michael@0 | 594 | if (++count > 1) { |
michael@0 | 595 | assert.pass('onLoad event called on reload'); |
michael@0 | 596 | tab.close(done); |
michael@0 | 597 | } |
michael@0 | 598 | else { |
michael@0 | 599 | assert.pass('first onLoad event occured'); |
michael@0 | 600 | tab.reload(); |
michael@0 | 601 | } |
michael@0 | 602 | } |
michael@0 | 603 | }); |
michael@0 | 604 | }; |
michael@0 | 605 | |
michael@0 | 606 | require('sdk/test').run(exports); |