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, data) {
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);
64 // ///// Desktop ///////
66 // Special case for Thunderbird windows.
67 const IS_THUNDERBIRD =
68 Services.appinfo.ID == "{3550f703-e582-4d05-9a08-453d09bdfdc6}";
69 const WINDOW_TYPE = IS_THUNDERBIRD ? "mail:3pane" : "navigator:browser";
71 var WindowListener = {
72 // browser-test.js is only loaded into the first window. Setup that
73 // needs to happen in all navigator:browser windows should go here.
75 win.nativeConsole = win.console;
76 ChromeUtils.defineModuleGetter(
79 "resource://gre/modules/Console.jsm"
84 if (win.nativeConsole) {
85 win.console = win.nativeConsole;
86 win.nativeConsole = undefined;
90 onOpenWindow(xulWin) {
91 let win = xulWin.docShell.domWindow;
97 win.document.documentElement.getAttribute("windowtype") == WINDOW_TYPE
99 WindowListener.setupWindow(win);
107 function loadMochitest(e) {
108 let flavor = e.detail[0];
109 let url = e.detail[1];
111 let win = Services.wm.getMostRecentWindow(WINDOW_TYPE);
112 win.removeEventListener("mochitest-load", loadMochitest);
114 // for mochitest-plain, navigating to the url is all we need
115 if (!IS_THUNDERBIRD) {
125 Services.scriptSecurityManager.getSystemPrincipal()
128 if (flavor == "mochitest") {
132 WindowListener.setupWindow(win);
133 Services.wm.addListener(WindowListener);
135 loadChromeScripts(win);
138 this.mochikit = class extends ExtensionAPI {
141 "@mozilla.org/addons/addon-manager-startup;1"
142 ].getService(Ci.amIAddonManagerStartup);
143 const manifestURI = Services.io.newURI(
146 this.extension.rootURI
148 const targetURL = this.extension.rootURI.resolve("content/");
149 this.chromeHandle = aomStartup.registerChrome(manifestURI, [
150 ["content", "mochikit", targetURL],
153 if (AppConstants.platform == "android") {
156 let win = Services.wm.getMostRecentWindow(WINDOW_TYPE);
157 // wait for event fired from start_desktop.js containing the
158 // suite and url to load
159 win.addEventListener("mochitest-load", loadMochitest);
164 if (AppConstants.platform != "android") {
165 for (let win of Services.wm.getEnumerator(WINDOW_TYPE)) {
166 WindowListener.tearDownWindow(win);
169 Services.wm.removeListener(WindowListener);
172 this.chromeHandle.destruct();
173 this.chromeHandle = null;