addon-sdk/source/test/test-tab.js

Thu, 15 Jan 2015 15:59:08 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 15 Jan 2015 15:59:08 +0100
branch
TOR_BUG_9701
changeset 10
ac0c01689b40
permissions
-rw-r--r--

Implement a real Private Browsing Mode condition by changing the API/ABI;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

     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/. */
     5 "use strict";
     7 const tabs = require("sdk/tabs"); // From addon-kit
     8 const windowUtils = require("sdk/deprecated/window-utils");
     9 const { getTabForWindow } = require('sdk/tabs/helpers');
    10 const app = require("sdk/system/xul-app");
    11 const { viewFor } = require("sdk/view/core");
    12 const { modelFor } = require("sdk/model/core");
    13 const { getTabId, isTab } = require("sdk/tabs/utils");
    14 const { defer } = require("sdk/lang/functional");
    16 // The primary test tab
    17 var primaryTab;
    19 // We have an auxiliary tab to test background tabs.
    20 var auxTab;
    22 // The window for the outer iframe in the primary test page
    23 var iframeWin;
    25 exports["test GetTabForWindow"] = function(assert, done) {
    27   assert.equal(getTabForWindow(windowUtils.activeWindow), null,
    28     "getTabForWindow return null on topwindow");
    29   assert.equal(getTabForWindow(windowUtils.activeBrowserWindow), null,
    30     "getTabForWindow return null on topwindow");
    32   let subSubDocument = encodeURIComponent(
    33     'Sub iframe<br/>'+
    34     '<iframe id="sub-sub-iframe" src="data:text/html;charset=utf-8,SubSubIframe" />');
    35   let subDocument = encodeURIComponent(
    36     'Iframe<br/>'+
    37     '<iframe id="sub-iframe" src="data:text/html;charset=utf-8,'+subSubDocument+'" />');
    38   let url = 'data:text/html;charset=utf-8,' + encodeURIComponent(
    39     'Content<br/><iframe id="iframe" src="data:text/html;charset=utf-8,'+subDocument+'" />');
    41   // Open up a new tab in the background.
    42   //
    43   // This lets us test whether GetTabForWindow works even when the tab in
    44   // question is not active.
    45   tabs.open({
    46     inBackground: true,
    47     url: "about:mozilla",
    48     onReady: function(tab) { auxTab = tab; step2(url, assert);},
    49     onActivate: function(tab) { step3(assert, done); }
    50   });
    51 };
    53 function step2(url, assert) {
    55   tabs.open({
    56     url: url,
    57     onReady: function(tab) {
    58       primaryTab = tab;
    59       let window = windowUtils.activeBrowserWindow.content;
    61       let matchedTab = getTabForWindow(window);
    62       assert.equal(matchedTab, tab,
    63         "We are able to find the tab with his content window object");
    65       let timer = require("sdk/timers");
    66       function waitForFrames() {
    67         let iframe = window.document.getElementById("iframe");
    68         if (!iframe) {
    69           timer.setTimeout(waitForFrames, 100);
    70           return;
    71         }
    72         iframeWin = iframe.contentWindow;
    73         let subIframe = iframeWin.document.getElementById("sub-iframe");
    74         if (!subIframe) {
    75           timer.setTimeout(waitForFrames, 100);
    76           return;
    77         }
    78         let subIframeWin = subIframe.contentWindow;
    79         let subSubIframe = subIframeWin.document.getElementById("sub-sub-iframe");
    80         if (!subSubIframe) {
    81           timer.setTimeout(waitForFrames, 100);
    82           return;
    83         }
    84         let subSubIframeWin = subSubIframe.contentWindow;
    86         matchedTab = getTabForWindow(iframeWin);
    87         assert.equal(matchedTab, tab,
    88           "We are able to find the tab with an iframe window object");
    90         matchedTab = getTabForWindow(subIframeWin);
    91         assert.equal(matchedTab, tab,
    92           "We are able to find the tab with a sub-iframe window object");
    94         matchedTab = getTabForWindow(subSubIframeWin);
    95         assert.equal(matchedTab, tab,
    96           "We are able to find the tab with a sub-sub-iframe window object");
    98         // Put our primary tab in the background and test again.
    99         // The onActivate listener will take us to step3.
   100         auxTab.activate();
   101       }
   102       waitForFrames();
   103     }
   104   });
   105 }
   107 function step3(assert, done) {
   109   let matchedTab = getTabForWindow(iframeWin);
   110   assert.equal(matchedTab, primaryTab,
   111     "We get the correct tab even when it's in the background");
   113   primaryTab.close(function () {
   114       auxTab.close(function () { done();});
   115     });
   116 }
   118 exports["test behavior on close"] = function(assert, done) {
   120   tabs.open({
   121     url: "about:mozilla",
   122     onReady: function(tab) {
   123       assert.equal(tab.url, "about:mozilla", "Tab has the expected url");
   124       // if another test ends before closing a tab then index != 1 here
   125       assert.ok(tab.index >= 1, "Tab has the expected index, a value greater than 0");
   126       tab.close(function () {
   127         assert.equal(tab.url, undefined,
   128                      "After being closed, tab attributes are undefined (url)");
   129         assert.equal(tab.index, undefined,
   130                      "After being closed, tab attributes are undefined (index)");
   131         if (app.is("Firefox")) {
   132           // Ensure that we can call destroy multiple times without throwing;
   133           // Fennec doesn't use this internal utility
   134           tab.destroy();
   135           tab.destroy();
   136         }
   138         done();
   139       });
   140     }
   141   });
   142 };
   144 exports["test viewFor(tab)"] = (assert, done) => {
   145   // Note we defer handlers as length collection is updated after
   146   // handler is invoked, so if test is finished before counnts are
   147   // updated wrong length will show up in followup tests.
   148   tabs.once("open", defer(tab => {
   149     const view = viewFor(tab);
   150     assert.ok(view, "view is returned");
   151     assert.equal(getTabId(view), tab.id, "tab has a same id");
   153     tab.close(defer(done));
   154   }));
   156   tabs.open({ url: "about:mozilla" });
   157 };
   160 exports["test modelFor(xulTab)"] = (assert, done) => {
   161   tabs.open({
   162     url: "about:mozilla",
   163     onReady: tab => {
   164       const view = viewFor(tab);
   165       assert.ok(view, "view is returned");
   166       assert.ok(isTab(view), "view is underlaying tab");
   167       assert.equal(getTabId(view), tab.id, "tab has a same id");
   168       assert.equal(modelFor(view), tab, "modelFor(view) is SDK tab");
   170       tab.close(defer(done));
   171     }
   172   });
   173 };
   175 require("test").run(exports);

mercurial