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.

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

mercurial