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/. */
5 /* globals AppConstants, ExtensionAPI, Services */
7 function loadChromeScripts(win) {
8 Services.scriptloader.loadSubScript(
9 "chrome://mochikit/content/chrome-harness.js",
12 Services.scriptloader.loadSubScript(
13 "chrome://mochikit/content/mochitest-e10s-utils.js",
16 Services.scriptloader.loadSubScript(
17 "chrome://mochikit/content/browser-test.js",
22 // ///// Android ///////
24 const windowTracker = {
26 Services.obs.addObserver(this, "chrome-document-global-created");
29 async observe(window, topic) {
30 if (topic === "chrome-document-global-created") {
31 await new Promise(resolve =>
32 window.addEventListener("DOMContentLoaded", resolve, { once: true })
35 let { document } = window;
36 let { documentURI } = document;
38 if (documentURI !== AppConstants.BROWSER_CHROME_URL) {
41 loadChromeScripts(window);
46 function androidStartup() {
47 // Only browser chrome tests need help starting.
48 let testRoot = Services.prefs.getStringPref("mochitest.testRoot", "");
49 if (testRoot.endsWith("/chrome")) {
50 // The initial window is created from browser startup, which races
51 // against extension initialization. If it has already been created,
52 // inject the test scripts now, otherwise wait for the browser window
54 let win = Services.wm.getMostRecentWindow("navigator:browser");
56 loadChromeScripts(win);
63 Services.fog.initializeFOG();
66 // ///// Desktop ///////
68 // Special case for Thunderbird windows.
69 const IS_THUNDERBIRD =
70 Services.appinfo.ID == "{3550f703-e582-4d05-9a08-453d09bdfdc6}";
71 const WINDOW_TYPE = IS_THUNDERBIRD ? "mail:3pane" : "navigator:browser";
73 var WindowListener = {
74 // browser-test.js is only loaded into the first window. Setup that
75 // needs to happen in all navigator:browser windows should go here.
77 win.nativeConsole = win.console;
78 let { ConsoleAPI } = ChromeUtils.importESModule(
79 "resource://gre/modules/Console.sys.mjs"
81 win.console = new ConsoleAPI();
85 if (win.nativeConsole) {
86 win.console = win.nativeConsole;
87 win.nativeConsole = undefined;
91 onOpenWindow(xulWin) {
92 let win = xulWin.docShell.domWindow;
98 win.document.documentElement.getAttribute("windowtype") == WINDOW_TYPE
100 WindowListener.setupWindow(win);
108 function loadMochitest(e) {
109 let flavor = e.detail[0];
110 let url = e.detail[1];
112 let win = Services.wm.getMostRecentWindow(WINDOW_TYPE);
113 win.removeEventListener("mochitest-load", loadMochitest);
115 // for mochitest-plain, navigating to the url is all we need
116 if (!IS_THUNDERBIRD) {
117 win.openLinkIn(url, "current", {
118 triggeringPrincipal: Services.scriptSecurityManager.getSystemPrincipal(),
121 if (flavor == "mochitest") {
125 WindowListener.setupWindow(win);
126 Services.wm.addListener(WindowListener);
128 loadChromeScripts(win);
131 this.mochikit = class extends ExtensionAPI {
134 "@mozilla.org/addons/addon-manager-startup;1"
135 ].getService(Ci.amIAddonManagerStartup);
136 const manifestURI = Services.io.newURI(
139 this.extension.rootURI
141 const targetURL = this.extension.rootURI.resolve("content/");
142 this.chromeHandle = aomStartup.registerChrome(manifestURI, [
143 ["content", "mochikit", targetURL],
146 if (AppConstants.platform == "android") {
149 let win = Services.wm.getMostRecentWindow(WINDOW_TYPE);
150 // wait for event fired from start_desktop.js containing the
151 // suite and url to load
152 win.addEventListener("mochitest-load", loadMochitest);
157 if (AppConstants.platform != "android") {
158 for (let win of Services.wm.getEnumerator(WINDOW_TYPE)) {
159 WindowListener.tearDownWindow(win);
162 Services.wm.removeListener(WindowListener);
165 this.chromeHandle.destruct();
166 this.chromeHandle = null;