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

branch
TOR_BUG_9701
changeset 10
ac0c01689b40
equal deleted inserted replaced
-1:000000000000 0:98de795939d3
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
5 "use strict";
6
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");
15
16 // The primary test tab
17 var primaryTab;
18
19 // We have an auxiliary tab to test background tabs.
20 var auxTab;
21
22 // The window for the outer iframe in the primary test page
23 var iframeWin;
24
25 exports["test GetTabForWindow"] = function(assert, done) {
26
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");
31
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+'" />');
40
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 };
52
53 function step2(url, assert) {
54
55 tabs.open({
56 url: url,
57 onReady: function(tab) {
58 primaryTab = tab;
59 let window = windowUtils.activeBrowserWindow.content;
60
61 let matchedTab = getTabForWindow(window);
62 assert.equal(matchedTab, tab,
63 "We are able to find the tab with his content window object");
64
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;
85
86 matchedTab = getTabForWindow(iframeWin);
87 assert.equal(matchedTab, tab,
88 "We are able to find the tab with an iframe window object");
89
90 matchedTab = getTabForWindow(subIframeWin);
91 assert.equal(matchedTab, tab,
92 "We are able to find the tab with a sub-iframe window object");
93
94 matchedTab = getTabForWindow(subSubIframeWin);
95 assert.equal(matchedTab, tab,
96 "We are able to find the tab with a sub-sub-iframe window object");
97
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 }
106
107 function step3(assert, done) {
108
109 let matchedTab = getTabForWindow(iframeWin);
110 assert.equal(matchedTab, primaryTab,
111 "We get the correct tab even when it's in the background");
112
113 primaryTab.close(function () {
114 auxTab.close(function () { done();});
115 });
116 }
117
118 exports["test behavior on close"] = function(assert, done) {
119
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 }
137
138 done();
139 });
140 }
141 });
142 };
143
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");
152
153 tab.close(defer(done));
154 }));
155
156 tabs.open({ url: "about:mozilla" });
157 };
158
159
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");
169
170 tab.close(defer(done));
171 }
172 });
173 };
174
175 require("test").run(exports);

mercurial