fune/browser/base/content/test/general/browser_newWindowDrop.js
Kris Maglione e930b89c34 Bug 1514594: Part 3 - Change ChromeUtils.import API.
***
Bug 1514594: Part 3a - Change ChromeUtils.import to return an exports object; not pollute global. r=mccr8

This changes the behavior of ChromeUtils.import() to return an exports object,
rather than a module global, in all cases except when `null` is passed as a
second argument, and changes the default behavior not to pollute the global
scope with the module's exports. Thus, the following code written for the old
model:

  ChromeUtils.import("resource://gre/modules/Services.jsm");

is approximately the same as the following, in the new model:

  var {Services} = ChromeUtils.import("resource://gre/modules/Services.jsm");

Since the two behaviors are mutually incompatible, this patch will land with a
scripted rewrite to update all existing callers to use the new model rather
than the old.
***
Bug 1514594: Part 3b - Mass rewrite all JS code to use the new ChromeUtils.import API. rs=Gijs

This was done using the followng script:

https://bitbucket.org/kmaglione/m-c-rewrites/src/tip/processors/cu-import-exports.jsm
***
Bug 1514594: Part 3c - Update ESLint plugin for ChromeUtils.import API changes. r=Standard8

Differential Revision: https://phabricator.services.mozilla.com/D16747
***
Bug 1514594: Part 3d - Remove/fix hundreds of duplicate imports from sync tests. r=Gijs

Differential Revision: https://phabricator.services.mozilla.com/D16748
***
Bug 1514594: Part 3e - Remove no-op ChromeUtils.import() calls. r=Gijs

Differential Revision: https://phabricator.services.mozilla.com/D16749
***
Bug 1514594: Part 3f.1 - Cleanup various test corner cases after mass rewrite. r=Gijs
***
Bug 1514594: Part 3f.2 - Cleanup various non-test corner cases after mass rewrite. r=Gijs

Differential Revision: https://phabricator.services.mozilla.com/D16750

--HG--
extra : rebase_source : 359574ee3064c90f33bf36c2ebe3159a24cc8895
extra : histedit_source : b93c8f42808b1599f9122d7842d2c0b3e656a594%2C64a3a4e3359dc889e2ab2b49461bab9e27fc10a7
2019-01-17 10:18:31 -08:00

209 lines
7 KiB
JavaScript

registerCleanupFunction(function cleanup() {
Services.search.defaultEngine = originalEngine;
let engine = Services.search.getEngineByName("MozSearch");
Services.search.removeEngine(engine);
});
let originalEngine;
add_task(async function test_setup() {
// Opening multiple windows on debug build takes too long time.
requestLongerTimeout(10);
// Stop search-engine loads from hitting the network
Services.search.addEngineWithDetails("MozSearch", "", "", "", "GET",
"http://example.com/?q={searchTerms}");
let engine = Services.search.getEngineByName("MozSearch");
originalEngine = Services.search.defaultEngine;
Services.search.defaultEngine = engine;
// Move New Window button to nav bar, to make it possible to drag and drop.
let {CustomizableUI} = ChromeUtils.import("resource:///modules/CustomizableUI.jsm");
let origPlacement = CustomizableUI.getPlacementOfWidget("new-window-button");
if (!origPlacement || origPlacement.area != CustomizableUI.AREA_NAVBAR) {
CustomizableUI.addWidgetToArea("new-window-button",
CustomizableUI.AREA_NAVBAR,
0);
CustomizableUI.ensureWidgetPlacedInWindow("new-window-button", window);
registerCleanupFunction(function() {
CustomizableUI.removeWidgetFromArea("new-window-button");
});
}
});
// New Window Button opens any link.
add_task(async function single_url() {
await dropText("mochi.test/first",
["http://www.mochi.test/first"]);
});
add_task(async function single_javascript() {
await dropText("javascript:'bad'",
["javascript:'bad'"]);
});
add_task(async function single_javascript_capital() {
await dropText("jAvascript:'bad'",
["javascript:'bad'"]);
});
add_task(async function single_url2() {
await dropText("mochi.test/second",
["http://www.mochi.test/second"]);
});
add_task(async function single_data_url() {
await dropText("data:text/html,bad",
["data:text/html,bad"]);
});
add_task(async function single_url3() {
await dropText("mochi.test/third",
["http://www.mochi.test/third"]);
});
// Single text/plain item, with multiple links.
add_task(async function multiple_urls() {
await dropText("mochi.test/1\nmochi.test/2",
[
"http://www.mochi.test/1",
"http://www.mochi.test/2",
]);
});
add_task(async function multiple_urls_javascript() {
await dropText("javascript:'bad1'\nmochi.test/3",
[
"javascript:'bad1'",
"http://www.mochi.test/3",
]);
});
add_task(async function multiple_urls_data() {
await dropText("mochi.test/4\ndata:text/html,bad1",
[
"http://www.mochi.test/4",
"data:text/html,bad1",
]);
});
// Multiple text/plain items, with single and multiple links.
add_task(async function multiple_items_single_and_multiple_links() {
await drop([[{type: "text/plain",
data: "mochi.test/5"}],
[{type: "text/plain",
data: "mochi.test/6\nmochi.test/7"}]],
[
"http://www.mochi.test/5",
"http://www.mochi.test/6",
"http://www.mochi.test/7",
]);
});
// Single text/x-moz-url item, with multiple links.
// "text/x-moz-url" has titles in even-numbered lines.
add_task(async function single_moz_url_multiple_links() {
await drop([[{type: "text/x-moz-url",
data: "mochi.test/8\nTITLE8\nmochi.test/9\nTITLE9"}]],
[
"http://www.mochi.test/8",
"http://www.mochi.test/9",
]);
});
// Single item with multiple types.
add_task(async function single_item_multiple_types() {
await drop([[{type: "text/plain",
data: "mochi.test/10"},
{type: "text/x-moz-url",
data: "mochi.test/11\nTITLE11"}]],
["http://www.mochi.test/11"]);
});
// Warn when too many URLs are dropped.
add_task(async function multiple_tabs_under_max() {
let urls = [];
for (let i = 0; i < 5; i++) {
urls.push("mochi.test/multi" + i);
}
await dropText(urls.join("\n"),
[
"http://www.mochi.test/multi0",
"http://www.mochi.test/multi1",
"http://www.mochi.test/multi2",
"http://www.mochi.test/multi3",
"http://www.mochi.test/multi4",
]);
});
add_task(async function multiple_tabs_over_max_accept() {
await pushPrefs(["browser.tabs.maxOpenBeforeWarn", 4]);
let confirmPromise = BrowserTestUtils.promiseAlertDialog("accept");
let urls = [];
for (let i = 0; i < 5; i++) {
urls.push("mochi.test/accept" + i);
}
await dropText(urls.join("\n"),
[
"http://www.mochi.test/accept0",
"http://www.mochi.test/accept1",
"http://www.mochi.test/accept2",
"http://www.mochi.test/accept3",
"http://www.mochi.test/accept4",
],
true);
await confirmPromise;
await popPrefs();
});
add_task(async function multiple_tabs_over_max_cancel() {
await pushPrefs(["browser.tabs.maxOpenBeforeWarn", 4]);
let confirmPromise = BrowserTestUtils.promiseAlertDialog("cancel");
let urls = [];
for (let i = 0; i < 5; i++) {
urls.push("mochi.test/cancel" + i);
}
await dropText(urls.join("\n"), [], true);
await confirmPromise;
await popPrefs();
});
function dropText(text, expectedURLs,
ignoreFirstWindow = false) {
return drop([[{type: "text/plain", data: text}]], expectedURLs,
ignoreFirstWindow);
}
async function drop(dragData, expectedURLs,
ignoreFirstWindow = false) {
let dragDataString = JSON.stringify(dragData);
info(`Starting test for dragData:${dragDataString}; expectedURLs.length:${expectedURLs.length}`);
let EventUtils = {};
Services.scriptloader.loadSubScript("chrome://mochikit/content/tests/SimpleTest/EventUtils.js", EventUtils);
// Since synthesizeDrop triggers the srcElement, need to use another button.
let dragSrcElement = document.getElementById("downloads-button");
ok(dragSrcElement, "Downloads button exists");
let newWindowButton = document.getElementById("new-window-button");
ok(newWindowButton, "New Window button exists");
let tmp = {};
ChromeUtils.import("resource://testing-common/TestUtils.jsm", tmp);
let awaitDrop = BrowserTestUtils.waitForEvent(newWindowButton, "drop");
let loadedPromises = expectedURLs.map(
url => BrowserTestUtils.waitForNewWindow({
url,
anyWindow: true,
maybeErrorPage: true,
}));
EventUtils.synthesizeDrop(dragSrcElement, newWindowButton, dragData, "link", window);
let windows = await Promise.all(loadedPromises);
for (let window of windows) {
await BrowserTestUtils.closeWindow(window);
}
await awaitDrop;
ok(true, "Got drop event");
}