Skip to content
This repository was archived by the owner on Jul 9, 2025. It is now read-only.

Commit 5490faf

Browse files
committed
Bug 849009 followup, add missing files from Add-on SDK uplift in 0745394ef7b1, r=KWierso
1 parent adbfc7c commit 5490faf

16 files changed

Lines changed: 731 additions & 0 deletions

File tree

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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';
5+
6+
const { defer } = require('../core/promise');
7+
const { open: openWindow, onFocus } = require('./utils');
8+
9+
function open(uri, options) {
10+
return promise(openWindow.apply(null, arguments), 'load');
11+
}
12+
exports.open = open;
13+
14+
function close(window) {
15+
// unload event could happen so fast that it is not resolved
16+
// if we listen to unload after calling close()
17+
let p = promise(window, 'unload');
18+
window.close();
19+
return p;
20+
}
21+
exports.close = close;
22+
23+
function focus(window) {
24+
let p = onFocus(window);
25+
window.focus();
26+
return p;
27+
}
28+
exports.focus = focus;
29+
30+
function promise(target, evt, capture) {
31+
let deferred = defer();
32+
capture = !!capture;
33+
34+
target.addEventListener(evt, function eventHandler() {
35+
target.removeEventListener(evt, eventHandler, capture);
36+
deferred.resolve(target);
37+
}, capture);
38+
39+
return deferred.promise;
40+
}
41+
exports.promise = promise;
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
const tabs = require('sdk/tabs');
2+
const { is } = require('sdk/system/xul-app');
3+
const { isPrivate } = require('sdk/private-browsing');
4+
const pbUtils = require('sdk/private-browsing/utils');
5+
const { getOwnerWindow } = require('sdk/private-browsing/window/utils');
6+
7+
exports.testPrivateTabsAreListed = function (assert, done) {
8+
let originalTabCount = tabs.length;
9+
10+
tabs.open({
11+
url: 'about:blank',
12+
isPrivate: true,
13+
onOpen: function(tab) {
14+
let win = getOwnerWindow(tab);
15+
// PWPB case
16+
if (pbUtils.isWindowPBSupported || pbUtils.isTabPBSupported) {
17+
assert.ok(isPrivate(tab), "tab is private");
18+
assert.equal(tabs.length, originalTabCount + 1,
19+
'New private window\'s tab are visible in tabs list');
20+
}
21+
else {
22+
// Global case, openDialog didn't opened a private window/tab
23+
assert.ok(!isPrivate(tab), "tab isn't private");
24+
assert.equal(tabs.length, originalTabCount + 1,
25+
'New non-private window\'s tab is visible in tabs list');
26+
}
27+
tab.close(done);
28+
}
29+
});
30+
}
Lines changed: 245 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,245 @@
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';
5+
6+
const { Cc, Ci } = require('chrome');
7+
const { isPrivate } = require('sdk/private-browsing');
8+
const { isWindowPBSupported } = require('sdk/private-browsing/utils');
9+
const { onFocus, getMostRecentWindow, getWindowTitle,
10+
getFrames, windows, open: openWindow, isWindowPrivate } = require('sdk/window/utils');
11+
const { open, close, focus, promise } = require('sdk/window/helpers');
12+
const { browserWindows } = require("sdk/windows");
13+
const winUtils = require("sdk/deprecated/window-utils");
14+
const { fromIterator: toArray } = require('sdk/util/array');
15+
16+
const WM = Cc['@mozilla.org/appshell/window-mediator;1'].getService(Ci.nsIWindowMediator);
17+
18+
const BROWSER = 'chrome://browser/content/browser.xul';
19+
20+
function makeEmptyBrowserWindow(options) {
21+
options = options || {};
22+
return open(BROWSER, {
23+
features: {
24+
chrome: true,
25+
private: !!options.private
26+
}
27+
});
28+
}
29+
30+
exports.testWindowTrackerIgnoresPrivateWindows = function(assert, done) {
31+
var myNonPrivateWindow, myPrivateWindow;
32+
var finished = false;
33+
var privateWindow;
34+
var privateWindowClosed = false;
35+
var privateWindowOpened = false;
36+
37+
let wt = winUtils.WindowTracker({
38+
onTrack: function(window) {
39+
if (window === myPrivateWindow) {
40+
assert.equal(isPrivate(window), isWindowPBSupported);
41+
privateWindowOpened = true;
42+
}
43+
},
44+
onUntrack: function(window) {
45+
if (window === myPrivateWindow && isWindowPBSupported) {
46+
privateWindowClosed = true;
47+
}
48+
49+
if (window === myNonPrivateWindow) {
50+
assert.equal(privateWindowClosed, isWindowPBSupported);
51+
assert.ok(privateWindowOpened);
52+
wt.unload();
53+
done();
54+
}
55+
}
56+
});
57+
58+
// make a new private window
59+
myPrivateWindow = openWindow(BROWSER, {
60+
features: {
61+
private: true
62+
}
63+
});
64+
promise(myPrivateWindow, 'load').then(function(window) {
65+
assert.equal(isPrivate(window), isWindowPBSupported, 'private window isPrivate');
66+
assert.equal(isWindowPrivate(window), isWindowPBSupported);
67+
assert.ok(getFrames(window).length > 1, 'there are frames for private window');
68+
assert.equal(getWindowTitle(window), window.document.title,
69+
'getWindowTitle works');
70+
71+
close(myPrivateWindow).then(function() {
72+
assert.pass('private window was closed');
73+
makeEmptyBrowserWindow().then(function(window) {
74+
myNonPrivateWindow = window;
75+
assert.notDeepEqual(myPrivateWindow, myNonPrivateWindow);
76+
assert.pass('opened new window');
77+
close(myNonPrivateWindow).then(function() {
78+
assert.pass('non private window was closed');
79+
})
80+
});
81+
});
82+
});
83+
};
84+
85+
// Test setting activeWIndow and onFocus for private windows
86+
exports.testSettingActiveWindowDoesNotIgnorePrivateWindow = function(assert, done) {
87+
let browserWindow = WM.getMostRecentWindow("navigator:browser");
88+
let testSteps;
89+
90+
assert.equal(winUtils.activeBrowserWindow, browserWindow,
91+
"Browser window is the active browser window.");
92+
assert.ok(!isPrivate(browserWindow), "Browser window is not private.");
93+
94+
// make a new private window
95+
makeEmptyBrowserWindow({
96+
private: true
97+
}).then(focus).then(function(window) {
98+
let continueAfterFocus = function(window) onFocus(window).then(nextTest);
99+
100+
// PWPB case
101+
if (isWindowPBSupported) {
102+
assert.ok(isPrivate(window), "window is private");
103+
assert.notDeepEqual(winUtils.activeBrowserWindow, browserWindow);
104+
}
105+
// Global case
106+
else {
107+
assert.ok(!isPrivate(window), "window is not private");
108+
}
109+
110+
assert.strictEqual(winUtils.activeBrowserWindow, window,
111+
"Correct active browser window pb supported");
112+
assert.notStrictEqual(browserWindow, window,
113+
"The window is not the old browser window");
114+
115+
testSteps = [
116+
function() {
117+
// test setting a non private window
118+
continueAfterFocus(winUtils.activeWindow = browserWindow);
119+
},
120+
function() {
121+
assert.strictEqual(winUtils.activeWindow, browserWindow,
122+
"Correct active window [1]");
123+
assert.strictEqual(winUtils.activeBrowserWindow, browserWindow,
124+
"Correct active browser window [1]");
125+
126+
// test focus(window)
127+
focus(window).then(nextTest);
128+
},
129+
function(w) {
130+
assert.strictEqual(w, window, 'require("sdk/window/helpers").focus on window works');
131+
assert.strictEqual(winUtils.activeBrowserWindow, window,
132+
"Correct active browser window [2]");
133+
assert.strictEqual(winUtils.activeWindow, window,
134+
"Correct active window [2]");
135+
136+
// test setting a private window
137+
continueAfterFocus(winUtils.activeWindow = window);
138+
},
139+
function() {
140+
assert.deepEqual(winUtils.activeBrowserWindow, window,
141+
"Correct active browser window [3]");
142+
assert.deepEqual(winUtils.activeWindow, window,
143+
"Correct active window [3]");
144+
145+
// just to get back to original state
146+
continueAfterFocus(winUtils.activeWindow = browserWindow);
147+
},
148+
function() {
149+
assert.deepEqual(winUtils.activeBrowserWindow, browserWindow,
150+
"Correct active browser window when pb mode is supported [4]");
151+
assert.deepEqual(winUtils.activeWindow, browserWindow,
152+
"Correct active window when pb mode is supported [4]");
153+
154+
close(window).then(done);
155+
}
156+
];
157+
158+
function nextTest() {
159+
let args = arguments;
160+
if (testSteps.length) {
161+
require('sdk/timers').setTimeout(function() {
162+
(testSteps.shift()).apply(null, args);
163+
}, 0);
164+
}
165+
}
166+
nextTest();
167+
});
168+
};
169+
170+
exports.testActiveWindowDoesNotIgnorePrivateWindow = function(assert, done) {
171+
// make a new private window
172+
makeEmptyBrowserWindow({
173+
private: true
174+
}).then(function(window) {
175+
// PWPB case
176+
if (isWindowPBSupported) {
177+
assert.equal(isPrivate(winUtils.activeWindow), true,
178+
"active window is private");
179+
assert.equal(isPrivate(winUtils.activeBrowserWindow), true,
180+
"active browser window is private");
181+
assert.ok(isWindowPrivate(window), "window is private");
182+
assert.ok(isPrivate(window), "window is private");
183+
184+
// pb mode is supported
185+
assert.ok(
186+
isWindowPrivate(winUtils.activeWindow),
187+
"active window is private when pb mode is supported");
188+
assert.ok(
189+
isWindowPrivate(winUtils.activeBrowserWindow),
190+
"active browser window is private when pb mode is supported");
191+
assert.ok(isPrivate(winUtils.activeWindow),
192+
"active window is private when pb mode is supported");
193+
assert.ok(isPrivate(winUtils.activeBrowserWindow),
194+
"active browser window is private when pb mode is supported");
195+
}
196+
// Global case
197+
else {
198+
assert.equal(isPrivate(winUtils.activeWindow), false,
199+
"active window is not private");
200+
assert.equal(isPrivate(winUtils.activeBrowserWindow), false,
201+
"active browser window is not private");
202+
assert.equal(isWindowPrivate(window), false, "window is not private");
203+
assert.equal(isPrivate(window), false, "window is not private");
204+
}
205+
206+
close(window).then(done);
207+
});
208+
}
209+
210+
exports.testWindowIteratorIgnoresPrivateWindows = function(assert, done) {
211+
// make a new private window
212+
makeEmptyBrowserWindow({
213+
private: true
214+
}).then(function(window) {
215+
assert.equal(isWindowPrivate(window), isWindowPBSupported);
216+
assert.ok(toArray(winUtils.windowIterator()).indexOf(window) > -1,
217+
"window is in windowIterator()");
218+
219+
close(window).then(done);
220+
});
221+
};
222+
223+
// test that it is not possible to find a private window in
224+
// windows module's iterator
225+
exports.testWindowIteratorPrivateDefault = function(assert, done) {
226+
assert.equal(browserWindows.length, 1, 'only one window open');
227+
228+
open('chrome://browser/content/browser.xul', {
229+
features: {
230+
private: true,
231+
chrome: true
232+
}
233+
}).then(function(window) {
234+
// test that there is a private window opened
235+
assert.equal(isPrivate(window), isWindowPBSupported, 'there is a private window open');
236+
assert.equal(isPrivate(winUtils.activeWindow), isWindowPBSupported);
237+
assert.equal(isPrivate(getMostRecentWindow()), isWindowPBSupported);
238+
assert.equal(isPrivate(browserWindows.activeWindow), isWindowPBSupported);
239+
240+
assert.equal(browserWindows.length, 2, '2 windows open');
241+
assert.equal(windows(null, { includePrivate: true }).length, 2);
242+
243+
close(window).then(done);
244+
});
245+
};
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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";
5+
6+
exports["test local vs sdk module"] = function (assert) {
7+
assert.notEqual(require("memory"),
8+
require("sdk/deprecated/memory"),
9+
"Local module takes the priority over sdk modules");
10+
assert.ok(require("memory").local,
11+
"this module is really the local one");
12+
}
13+
14+
exports["test 3rd party vs sdk module"] = function (assert) {
15+
// We are testing with a 3rd party package called `panel` with 3 modules
16+
// main, page-mod and third-party
17+
18+
// the only way to require 3rd party package modules are to use absolute paths
19+
// require("panel/main"), require("panel/page-mod"),
20+
// require("panel/third-party") and also require("panel") which will refer
21+
// to panel's main package module.
22+
23+
// So require(page-mod) shouldn't map the 3rd party
24+
assert.equal(require("page-mod"),
25+
require("sdk/page-mod"),
26+
"Third party modules don't overload sdk modules");
27+
assert.ok(require("page-mod").PageMod,
28+
"page-mod module is really the sdk one");
29+
30+
assert.equal(require("panel/page-mod").id, "page-mod",
31+
"panel/page-mod is the 3rd party");
32+
33+
// But require(panel) will map to 3rd party main module
34+
// *and* overload the sdk module
35+
// and also any local module with the same name
36+
assert.equal(require("panel").id, "panel-main",
37+
"Third party main module overload sdk modules");
38+
assert.equal(require("panel"),
39+
require("panel/main"),
40+
"require(panel) maps to require(panel/main)");
41+
// So that you have to use relative path to ensure getting the local module
42+
assert.equal(require("./panel").id,
43+
"local-panel",
44+
"require(./panel) maps to the local module");
45+
46+
// It should still be possible to require sdk module with absolute path
47+
assert.ok(require("sdk/panel").Panel,
48+
"We can bypass this overloading with absolute path to sdk modules");
49+
assert.equal(require("sdk/panel"),
50+
require("addon-kit/panel"),
51+
"Old and new layout both work");
52+
}
53+
54+
// /!\ Always use distinct module for each test.
55+
// Otherwise, the linker can correctly parse and allow the first usage of it
56+
// but still silently fail on the second.
57+
58+
exports.testRelativeRequire = function (assert) {
59+
assert.equal(require('./same-folder').id, 'same-folder');
60+
}
61+
62+
exports.testRelativeSubFolderRequire = function (assert) {
63+
assert.equal(require('./sub-folder/module').id, 'sub-folder');
64+
}
65+
66+
exports.testMultipleRequirePerLine = function (assert) {
67+
var a=require('./multiple/a'),b=require('./multiple/b');
68+
assert.equal(a.id, 'a');
69+
assert.equal(b.id, 'b');
70+
}
71+
72+
exports.testSDKRequire = function (assert) {
73+
assert.deepEqual(Object.keys(require('sdk/widget')), ['Widget']);
74+
assert.equal(require('widget'), require('sdk/widget'));
75+
}
76+
77+
require("sdk/test/runner").runTestsFromModule(module);

0 commit comments

Comments
 (0)