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.

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

mercurial