gecko-dev/layout/tools/reftest/api.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

121 lines
4.3 KiB
JavaScript

/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
const Cm = Components.manager;
const {Services} = ChromeUtils.import("resource://gre/modules/Services.jsm");
const {XPCOMUtils} = ChromeUtils.import("resource://gre/modules/XPCOMUtils.jsm");
var OnRefTestLoad, OnRefTestUnload;
XPCOMUtils.defineLazyServiceGetter(this, "resProto",
"@mozilla.org/network/protocol;1?name=resource",
"nsISubstitutingProtocolHandler");
XPCOMUtils.defineLazyServiceGetter(this, "aomStartup",
"@mozilla.org/addons/addon-manager-startup;1",
"amIAddonManagerStartup");
function processTerminated() {
return new Promise(resolve => {
Services.obs.addObserver(function observe(subject, topic) {
if (topic == "ipc:content-shutdown") {
Services.obs.removeObserver(observe, topic);
resolve();
}
}, "ipc:content-shutdown");
});
}
function startAndroid(win) {
// Add setTimeout here because windows.innerWidth/Height are not set yet.
win.setTimeout(function() {OnRefTestLoad(win);}, 0);
}
var WindowListener = {
onOpenWindow: function(xulWin) {
Services.wm.removeListener(WindowListener);
let win = xulWin.docShell.domWindow;
win.addEventListener("load", function listener() {
// Load into any existing windows.
for (win of Services.wm.getEnumerator("navigator:browser")) {
break;
}
win.addEventListener("pageshow", function() {
startAndroid(win);
}, {once: true});
}, {once: true});
}
};
this.reftest = class extends ExtensionAPI {
onStartup() {
let uri = Services.io.newURI("chrome/reftest/res/", null, this.extension.rootURI);
resProto.setSubstitutionWithFlags("reftest", uri, resProto.ALLOW_CONTENT_ACCESS);
const manifestURI = Services.io.newURI("manifest.json", null, this.extension.rootURI);
this.chromeHandle = aomStartup.registerChrome(manifestURI, [
["content", "reftest", "chrome/reftest/content/", "contentaccessible=yes"],
]);
// Starting tests is handled quite differently on android and desktop.
// On Android, OnRefTestLoad() takes over the main browser window so
// we just need to call it as soon as the browser window is available.
// On desktop, a separate window (dummy) is created and explicitly given
// focus (see bug 859339 for details), then tests are launched in a new
// top-level window.
let win = Services.wm.getMostRecentWindow("navigator:browser");
if (!win) {
// There is no navigator:browser in the geckoview TestRunnerActivity;
// try navigator.geckoview instead.
win = Services.wm.getMostRecentWindow("navigator:geckoview");
}
if (Services.appinfo.OS == "Android") {
({OnRefTestLoad, OnRefTestUnload} = ChromeUtils.import("resource://reftest/reftest.jsm"));
if (win) {
startAndroid(win);
} else {
Services.wm.addListener(WindowListener);
}
return;
}
Services.io.manageOfflineStatus = false;
Services.io.offline = false;
let dummy = Services.ww.openWindow(null, "about:blank", "dummy",
"chrome,dialog=no,left=800,height=200,width=200,all",null);
dummy.onload = async function() {
// Close pre-existing window
win.close();
const {PerTestCoverageUtils} = ChromeUtils.import("resource://reftest/PerTestCoverageUtils.jsm");
if (PerTestCoverageUtils.enabled) {
// In PerTestCoverage mode, wait for the process belonging to the window we just closed
// to be terminated, to avoid its shutdown interfering when we reset the counters.
await processTerminated();
}
dummy.focus();
Services.ww.openWindow(null, "chrome://reftest/content/reftest.xul",
"_blank", "chrome,dialog=no,all", {});
};
}
onShutdown() {
resProto.setSubstitution("reftest", null);
this.chromeHandle.destruct();
this.chromeHandle = null;
if (Services.appinfo.OS == "Android") {
Services.wm.removeListener(WindowListener);
OnRefTestUnload();
Cu.unload("resource://reftest/reftest.jsm");
}
}
};