addon-sdk/source/test/test-tabs-common.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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 { Loader, LoaderWithHookedConsole } = require("sdk/test/loader");
michael@0 7 const { browserWindows } = require('sdk/windows');
michael@0 8 const tabs = require('sdk/tabs');
michael@0 9 const { isPrivate } = require('sdk/private-browsing');
michael@0 10 const { openDialog } = require('sdk/window/utils');
michael@0 11 const { isWindowPrivate } = require('sdk/window/utils');
michael@0 12 const { setTimeout } = require('sdk/timers');
michael@0 13 const { openWebpage } = require('./private-browsing/helper');
michael@0 14 const { isTabPBSupported, isWindowPBSupported } = require('sdk/private-browsing/utils');
michael@0 15 const app = require("sdk/system/xul-app");
michael@0 16
michael@0 17 const URL = 'data:text/html;charset=utf-8,<html><head><title>#title#</title></head></html>';
michael@0 18
michael@0 19 // TEST: tab count
michael@0 20 exports.testTabCounts = function(assert, done) {
michael@0 21 tabs.open({
michael@0 22 url: 'about:blank',
michael@0 23 onReady: function(tab) {
michael@0 24 let count1 = 0,
michael@0 25 count2 = 0;
michael@0 26 for each(let window in browserWindows) {
michael@0 27 count1 += window.tabs.length;
michael@0 28 for each(let tab in window.tabs) {
michael@0 29 count2 += 1;
michael@0 30 }
michael@0 31 }
michael@0 32
michael@0 33 assert.ok(tabs.length > 1, 'tab count is > 1');
michael@0 34 assert.equal(count1, tabs.length, 'tab count by length is correct');
michael@0 35 assert.equal(count2, tabs.length, 'tab count by iteration is correct');
michael@0 36
michael@0 37 // end test
michael@0 38 tab.close(done);
michael@0 39 }
michael@0 40 });
michael@0 41 };
michael@0 42
michael@0 43
michael@0 44 // TEST: tabs.activeTab getter
michael@0 45 exports.testActiveTab_getter = function(assert, done) {
michael@0 46 let evtCount = 0;
michael@0 47 let activeTab = null;
michael@0 48
michael@0 49 function endTest(type, tab) {
michael@0 50 if (type == 'activate') {
michael@0 51 assert.strictEqual(tabs.activeTab, tab, 'the active tab is the opened tab');
michael@0 52 activeTab = tabs.activeTab;
michael@0 53 }
michael@0 54 else {
michael@0 55 assert.equal(tab.url, url, 'the opened tab has the correct url');
michael@0 56 }
michael@0 57
michael@0 58 if (++evtCount != 2)
michael@0 59 return;
michael@0 60
michael@0 61 assert.strictEqual(activeTab, tab, 'the active tab is the ready tab');
michael@0 62 assert.strictEqual(tabs.activeTab, tab, 'the active tab is the ready tab');
michael@0 63
michael@0 64 tab.close(done);
michael@0 65 }
michael@0 66
michael@0 67 let url = URL.replace("#title#", "testActiveTab_getter");
michael@0 68 tabs.open({
michael@0 69 url: url,
michael@0 70 onReady: endTest.bind(null, 'ready'),
michael@0 71 onActivate: endTest.bind(null, 'activate')
michael@0 72 });
michael@0 73 };
michael@0 74
michael@0 75 // TEST: tab.activate()
michael@0 76 exports.testActiveTab_setter = function(assert, done) {
michael@0 77 let url = URL.replace("#title#", "testActiveTab_setter");
michael@0 78 let tab1URL = URL.replace("#title#", "tab1");
michael@0 79
michael@0 80 tabs.open({
michael@0 81 url: tab1URL,
michael@0 82 onReady: function(activeTab) {
michael@0 83 let activeTabURL = tabs.activeTab.url;
michael@0 84
michael@0 85 tabs.open({
michael@0 86 url: url,
michael@0 87 inBackground: true,
michael@0 88 onReady: function onReady(tab) {
michael@0 89 assert.equal(tabs.activeTab.url, activeTabURL, "activeTab url has not changed");
michael@0 90 assert.equal(tab.url, url, "url of new background tab matches");
michael@0 91
michael@0 92 tab.once('activate', function onActivate(eventTab) {
michael@0 93 assert.equal(tabs.activeTab.url, url, "url after activeTab setter matches");
michael@0 94 assert.equal(eventTab, tab, "event argument is the activated tab");
michael@0 95 assert.equal(eventTab, tabs.activeTab, "the tab is the active one");
michael@0 96
michael@0 97 activeTab.close(function() {
michael@0 98 tab.close(done);
michael@0 99 });
michael@0 100 });
michael@0 101
michael@0 102 tab.activate();
michael@0 103 }
michael@0 104 });
michael@0 105 }
michael@0 106 });
michael@0 107 };
michael@0 108
michael@0 109 // TEST: tab.close()
michael@0 110 exports.testTabClose_alt = function(assert, done) {
michael@0 111 let url = URL.replace('#title#', 'TabClose_alt');
michael@0 112 let tab1URL = URL.replace('#title#', 'tab1');
michael@0 113
michael@0 114 tabs.open({
michael@0 115 url: tab1URL,
michael@0 116 onReady: function(tab1) {
michael@0 117 // make sure that our tab is not active first
michael@0 118 assert.notEqual(tabs.activeTab.url, url, "tab is not the active tab");
michael@0 119
michael@0 120 tabs.open({
michael@0 121 url: url,
michael@0 122 onReady: function(tab) {
michael@0 123 assert.equal(tab.url, url, "tab is now the active tab");
michael@0 124 assert.equal(tabs.activeTab.url, url, "tab is now the active tab");
michael@0 125
michael@0 126 // another tab should be activated on close
michael@0 127 tabs.once('activate', function() {
michael@0 128 assert.notEqual(tabs.activeTab.url, url, "tab is no longer the active tab");
michael@0 129
michael@0 130 // end test
michael@0 131 tab1.close(done);
michael@0 132 });
michael@0 133
michael@0 134 tab.close();
michael@0 135 }
michael@0 136 });
michael@0 137 }
michael@0 138 });
michael@0 139 };
michael@0 140
michael@0 141 exports.testAttachOnOpen_alt = function (assert, done) {
michael@0 142 // Take care that attach has to be called on tab ready and not on tab open.
michael@0 143 tabs.open({
michael@0 144 url: "data:text/html;charset=utf-8,foobar",
michael@0 145 onOpen: function (tab) {
michael@0 146 let worker = tab.attach({
michael@0 147 contentScript: 'self.postMessage(document.location.href); ',
michael@0 148 onMessage: function (msg) {
michael@0 149 assert.equal(msg, "about:blank",
michael@0 150 "Worker document url is about:blank on open");
michael@0 151 worker.destroy();
michael@0 152 tab.close(done);
michael@0 153 }
michael@0 154 });
michael@0 155 }
michael@0 156 });
michael@0 157 };
michael@0 158
michael@0 159 exports.testAttachOnMultipleDocuments_alt = function (assert, done) {
michael@0 160 // Example of attach that process multiple tab documents
michael@0 161 let firstLocation = "data:text/html;charset=utf-8,foobar";
michael@0 162 let secondLocation = "data:text/html;charset=utf-8,bar";
michael@0 163 let thirdLocation = "data:text/html;charset=utf-8,fox";
michael@0 164 let onReadyCount = 0;
michael@0 165 let worker1 = null;
michael@0 166 let worker2 = null;
michael@0 167 let detachEventCount = 0;
michael@0 168
michael@0 169 tabs.open({
michael@0 170 url: firstLocation,
michael@0 171 onReady: function (tab) {
michael@0 172 onReadyCount++;
michael@0 173 if (onReadyCount == 1) {
michael@0 174 worker1 = tab.attach({
michael@0 175 contentScript: 'self.on("message", ' +
michael@0 176 ' function () self.postMessage(document.location.href)' +
michael@0 177 ');',
michael@0 178 onMessage: function (msg) {
michael@0 179 assert.equal(msg, firstLocation,
michael@0 180 "Worker url is equal to the 1st document");
michael@0 181 tab.url = secondLocation;
michael@0 182 },
michael@0 183 onDetach: function () {
michael@0 184 detachEventCount++;
michael@0 185 assert.pass("Got worker1 detach event");
michael@0 186 assert.throws(function () {
michael@0 187 worker1.postMessage("ex-1");
michael@0 188 },
michael@0 189 /Couldn't find the worker/,
michael@0 190 "postMessage throw because worker1 is destroyed");
michael@0 191 checkEnd();
michael@0 192 }
michael@0 193 });
michael@0 194 worker1.postMessage("new-doc-1");
michael@0 195 }
michael@0 196 else if (onReadyCount == 2) {
michael@0 197 worker2 = tab.attach({
michael@0 198 contentScript: 'self.on("message", ' +
michael@0 199 ' function () self.postMessage(document.location.href)' +
michael@0 200 ');',
michael@0 201 onMessage: function (msg) {
michael@0 202 assert.equal(msg, secondLocation,
michael@0 203 "Worker url is equal to the 2nd document");
michael@0 204 tab.url = thirdLocation;
michael@0 205 },
michael@0 206 onDetach: function () {
michael@0 207 detachEventCount++;
michael@0 208 assert.pass("Got worker2 detach event");
michael@0 209 assert.throws(function () {
michael@0 210 worker2.postMessage("ex-2");
michael@0 211 },
michael@0 212 /Couldn't find the worker/,
michael@0 213 "postMessage throw because worker2 is destroyed");
michael@0 214 checkEnd(tab);
michael@0 215 }
michael@0 216 });
michael@0 217 worker2.postMessage("new-doc-2");
michael@0 218 }
michael@0 219 else if (onReadyCount == 3) {
michael@0 220 tab.close();
michael@0 221 }
michael@0 222 }
michael@0 223 });
michael@0 224
michael@0 225 function checkEnd(tab) {
michael@0 226 if (detachEventCount != 2)
michael@0 227 return;
michael@0 228
michael@0 229 assert.pass("Got all detach events");
michael@0 230
michael@0 231 done();
michael@0 232 }
michael@0 233 };
michael@0 234
michael@0 235 exports.testAttachWrappers_alt = function (assert, done) {
michael@0 236 // Check that content script has access to wrapped values by default
michael@0 237
michael@0 238 let document = "data:text/html;charset=utf-8,<script>var globalJSVar = true; " +
michael@0 239 " document.getElementById = 3;</script>";
michael@0 240 let count = 0;
michael@0 241
michael@0 242 tabs.open({
michael@0 243 url: document,
michael@0 244 onReady: function (tab) {
michael@0 245 let worker = tab.attach({
michael@0 246 contentScript: 'try {' +
michael@0 247 ' self.postMessage(!("globalJSVar" in window));' +
michael@0 248 ' self.postMessage(typeof window.globalJSVar == "undefined");' +
michael@0 249 '} catch(e) {' +
michael@0 250 ' self.postMessage(e.message);' +
michael@0 251 '}',
michael@0 252 onMessage: function (msg) {
michael@0 253 assert.equal(msg, true, "Worker has wrapped objects ("+count+")");
michael@0 254 if (count++ == 1)
michael@0 255 tab.close(function() done());
michael@0 256 }
michael@0 257 });
michael@0 258 }
michael@0 259 });
michael@0 260 };
michael@0 261
michael@0 262 // TEST: activeWindow getter and activeTab getter on tab 'activate' event
michael@0 263 exports.testActiveWindowActiveTabOnActivate_alt = function(assert, done) {
michael@0 264
michael@0 265 let activateCount = 0;
michael@0 266 let newTabs = [];
michael@0 267 let tabs = browserWindows.activeWindow.tabs;
michael@0 268
michael@0 269 tabs.on('activate', function onActivate(tab) {
michael@0 270 assert.equal(tabs.activeTab, tab,
michael@0 271 "the active window's active tab is the tab provided");
michael@0 272
michael@0 273 if (++activateCount == 2) {
michael@0 274 tabs.removeListener('activate', onActivate);
michael@0 275
michael@0 276 newTabs.forEach(function(tab) {
michael@0 277 tab.close(function() {
michael@0 278 if (--activateCount == 0) {
michael@0 279 done();
michael@0 280 }
michael@0 281 });
michael@0 282 });
michael@0 283 }
michael@0 284 else if (activateCount > 2) {
michael@0 285 assert.fail("activateCount is greater than 2 for some reason..");
michael@0 286 }
michael@0 287 });
michael@0 288
michael@0 289 tabs.open({
michael@0 290 url: URL.replace("#title#", "tabs.open1"),
michael@0 291 onOpen: function(tab) newTabs.push(tab)
michael@0 292 });
michael@0 293 tabs.open({
michael@0 294 url: URL.replace("#title#", "tabs.open2"),
michael@0 295 onOpen: function(tab) newTabs.push(tab)
michael@0 296 });
michael@0 297 };
michael@0 298
michael@0 299 // TEST: tab properties
michael@0 300 exports.testTabContentTypeAndReload = function(assert, done) {
michael@0 301
michael@0 302 let url = "data:text/html;charset=utf-8,<html><head><title>foo</title></head><body>foo</body></html>";
michael@0 303 let urlXML = "data:text/xml;charset=utf-8,<foo>bar</foo>";
michael@0 304 tabs.open({
michael@0 305 url: url,
michael@0 306 onReady: function(tab) {
michael@0 307 if (tab.url === url) {
michael@0 308 assert.equal(tab.contentType, "text/html");
michael@0 309 tab.url = urlXML;
michael@0 310 }
michael@0 311 else {
michael@0 312 assert.equal(tab.contentType, "text/xml");
michael@0 313 tab.close(done);
michael@0 314 }
michael@0 315 }
michael@0 316 });
michael@0 317 };
michael@0 318
michael@0 319 // test that it isn't possible to open a private tab without the private permission
michael@0 320 exports.testTabOpenPrivate = function(assert, done) {
michael@0 321
michael@0 322 let url = 'about:blank';
michael@0 323 tabs.open({
michael@0 324 url: url,
michael@0 325 isPrivate: true,
michael@0 326 onReady: function(tab) {
michael@0 327 assert.equal(tab.url, url, 'opened correct tab');
michael@0 328 assert.equal(isPrivate(tab), false, 'private tabs are not supported by default');
michael@0 329
michael@0 330 tab.close(done);
michael@0 331 }
michael@0 332 });
michael@0 333 }
michael@0 334
michael@0 335 // We need permission flag in order to see private window's tabs
michael@0 336 exports.testPrivateAreNotListed = function (assert, done) {
michael@0 337 let originalTabCount = tabs.length;
michael@0 338
michael@0 339 let page = openWebpage("about:blank", true);
michael@0 340 if (!page) {
michael@0 341 assert.pass("Private browsing isn't supported in this release");
michael@0 342 return;
michael@0 343 }
michael@0 344
michael@0 345 page.ready.then(function (win) {
michael@0 346 if (isTabPBSupported || isWindowPBSupported) {
michael@0 347 assert.ok(isWindowPrivate(win), "the window is private");
michael@0 348 assert.equal(tabs.length, originalTabCount,
michael@0 349 'but the tab is *not* visible in tabs list');
michael@0 350 }
michael@0 351 else {
michael@0 352 assert.ok(!isWindowPrivate(win), "the window isn't private");
michael@0 353 assert.equal(tabs.length, originalTabCount + 1,
michael@0 354 'so that the tab is visible is tabs list');
michael@0 355 }
michael@0 356 page.close().then(done);
michael@0 357 });
michael@0 358 }
michael@0 359
michael@0 360 // If we close the tab while being in `onOpen` listener,
michael@0 361 // we end up synchronously consuming TabOpen, closing the tab and still
michael@0 362 // synchronously consuming the related TabClose event before the second
michael@0 363 // loader have a change to process the first TabOpen event!
michael@0 364 exports.testImmediateClosing = function (assert, done) {
michael@0 365 let tabURL = 'data:text/html,foo';
michael@0 366
michael@0 367 let { loader, messages } = LoaderWithHookedConsole(module, onMessage);
michael@0 368 let concurrentTabs = loader.require("sdk/tabs");
michael@0 369 concurrentTabs.on("open", function (tab) {
michael@0 370 // On Firefox, It shouldn't receive such event as the other loader will just
michael@0 371 // open and destroy the tab without giving a chance to other loader to even
michael@0 372 // know about the existance of this tab.
michael@0 373 if (app.is("Firefox")) {
michael@0 374 assert.fail("Concurrent loader received a tabs `open` event");
michael@0 375 }
michael@0 376 else {
michael@0 377 // On mobile, we can still receive an open event,
michael@0 378 // but not the related ready event
michael@0 379 tab.on("ready", function () {
michael@0 380 assert.fail("Concurrent loader received a tabs `ready` event");
michael@0 381 });
michael@0 382 }
michael@0 383 });
michael@0 384 function onMessage(type, msg) {
michael@0 385 assert.fail("Unexpected mesage on concurrent loader: " + msg);
michael@0 386 }
michael@0 387
michael@0 388 tabs.open({
michael@0 389 url: tabURL,
michael@0 390 onOpen: function(tab) {
michael@0 391 tab.close(function () {
michael@0 392 assert.pass("Tab succesfully removed");
michael@0 393 // Let a chance to the concurrent loader to receive a TabOpen event
michael@0 394 // on the next event loop turn
michael@0 395 setTimeout(function () {
michael@0 396 loader.unload();
michael@0 397 done();
michael@0 398 }, 0);
michael@0 399 });
michael@0 400 }
michael@0 401 });
michael@0 402 }
michael@0 403
michael@0 404 // TEST: tab.reload()
michael@0 405 exports.testTabReload = function(assert, done) {
michael@0 406
michael@0 407 let url = "data:text/html;charset=utf-8,<!doctype%20html><title></title>";
michael@0 408
michael@0 409 tabs.open({
michael@0 410 url: url,
michael@0 411 onReady: function onReady(tab) {
michael@0 412 tab.removeListener('ready', onReady);
michael@0 413
michael@0 414 tab.once(
michael@0 415 'ready',
michael@0 416 function onReload() {
michael@0 417 assert.pass("the tab was loaded again");
michael@0 418 assert.equal(tab.url, url, "the tab has the same URL");
michael@0 419
michael@0 420 tab.close(function() done());
michael@0 421 }
michael@0 422 );
michael@0 423
michael@0 424 tab.reload();
michael@0 425 }
michael@0 426 });
michael@0 427 };
michael@0 428
michael@0 429 exports.testOnPageShowEvent = function (assert, done) {
michael@0 430 let events = [];
michael@0 431 let firstUrl = 'data:text/html;charset=utf-8,First';
michael@0 432 let secondUrl = 'data:text/html;charset=utf-8,Second';
michael@0 433
michael@0 434 let counter = 0;
michael@0 435 function onPageShow (tab, persisted) {
michael@0 436 events.push('pageshow');
michael@0 437 counter++;
michael@0 438 if (counter === 1) {
michael@0 439 assert.equal(persisted, false, 'page should not be cached on initial load');
michael@0 440 tab.url = secondUrl;
michael@0 441 }
michael@0 442 else if (counter === 2) {
michael@0 443 assert.equal(persisted, false, 'second test page should not be cached either');
michael@0 444 tab.attach({
michael@0 445 contentScript: 'setTimeout(function () { window.history.back(); }, 0)'
michael@0 446 });
michael@0 447 }
michael@0 448 else {
michael@0 449 assert.equal(persisted, true, 'when we get back to the fist page, it has to' +
michael@0 450 'come from cache');
michael@0 451 tabs.removeListener('pageshow', onPageShow);
michael@0 452 tabs.removeListener('open', onOpen);
michael@0 453 tabs.removeListener('ready', onReady);
michael@0 454 tab.close(() => {
michael@0 455 ['open', 'ready', 'pageshow', 'ready',
michael@0 456 'pageshow', 'pageshow'].map((type, i) => {
michael@0 457 assert.equal(type, events[i], 'correct ordering of events');
michael@0 458 });
michael@0 459 done()
michael@0 460 });
michael@0 461 }
michael@0 462 }
michael@0 463
michael@0 464 function onOpen () events.push('open');
michael@0 465 function onReady () events.push('ready');
michael@0 466
michael@0 467 tabs.on('pageshow', onPageShow);
michael@0 468 tabs.on('open', onOpen);
michael@0 469 tabs.on('ready', onReady);
michael@0 470 tabs.open({
michael@0 471 url: firstUrl
michael@0 472 });
michael@0 473 };
michael@0 474
michael@0 475 exports.testOnPageShowEventDeclarative = function (assert, done) {
michael@0 476 let events = [];
michael@0 477 let firstUrl = 'data:text/html;charset=utf-8,First';
michael@0 478 let secondUrl = 'data:text/html;charset=utf-8,Second';
michael@0 479
michael@0 480 let counter = 0;
michael@0 481 function onPageShow (tab, persisted) {
michael@0 482 events.push('pageshow');
michael@0 483 counter++;
michael@0 484 if (counter === 1) {
michael@0 485 assert.equal(persisted, false, 'page should not be cached on initial load');
michael@0 486 tab.url = secondUrl;
michael@0 487 }
michael@0 488 else if (counter === 2) {
michael@0 489 assert.equal(persisted, false, 'second test page should not be cached either');
michael@0 490 tab.attach({
michael@0 491 contentScript: 'setTimeout(function () { window.history.back(); }, 0)'
michael@0 492 });
michael@0 493 }
michael@0 494 else {
michael@0 495 assert.equal(persisted, true, 'when we get back to the fist page, it has to' +
michael@0 496 'come from cache');
michael@0 497 tabs.removeListener('pageshow', onPageShow);
michael@0 498 tabs.removeListener('open', onOpen);
michael@0 499 tabs.removeListener('ready', onReady);
michael@0 500 tab.close(() => {
michael@0 501 ['open', 'ready', 'pageshow', 'ready',
michael@0 502 'pageshow', 'pageshow'].map((type, i) => {
michael@0 503 assert.equal(type, events[i], 'correct ordering of events');
michael@0 504 });
michael@0 505 done()
michael@0 506 });
michael@0 507 }
michael@0 508 }
michael@0 509
michael@0 510 function onOpen () events.push('open');
michael@0 511 function onReady () events.push('ready');
michael@0 512
michael@0 513 tabs.open({
michael@0 514 url: firstUrl,
michael@0 515 onPageShow: onPageShow,
michael@0 516 onOpen: onOpen,
michael@0 517 onReady: onReady
michael@0 518 });
michael@0 519 };
michael@0 520
michael@0 521 require('sdk/test').run(exports);

mercurial