api.js (4538B)
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 5 /* globals AppConstants, ExtensionAPI, Services */ 6 7 function loadChromeScripts(win) { 8 Services.scriptloader.loadSubScript( 9 "chrome://mochikit/content/chrome-harness.js", 10 win 11 ); 12 Services.scriptloader.loadSubScript( 13 "chrome://mochikit/content/mochitest-e10s-utils.js", 14 win 15 ); 16 Services.scriptloader.loadSubScript( 17 "chrome://mochikit/content/browser-test.js", 18 win 19 ); 20 } 21 22 // ///// Android /////// 23 24 const windowTracker = { 25 init() { 26 Services.obs.addObserver(this, "chrome-document-global-created"); 27 }, 28 29 async observe(window, topic) { 30 if (topic === "chrome-document-global-created") { 31 await new Promise(resolve => 32 window.addEventListener("DOMContentLoaded", resolve, { once: true }) 33 ); 34 35 let { document } = window; 36 let { documentURI } = document; 37 38 if (documentURI !== AppConstants.BROWSER_CHROME_URL) { 39 return; 40 } 41 loadChromeScripts(window); 42 } 43 }, 44 }; 45 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 53 // to show up. 54 let win = Services.wm.getMostRecentWindow("navigator:browser"); 55 if (win) { 56 loadChromeScripts(win); 57 return; 58 } 59 60 windowTracker.init(); 61 } 62 63 Services.fog.initializeFOG(); 64 } 65 66 // ///// Desktop /////// 67 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"; 72 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. 76 setupWindow(win) { 77 win.nativeConsole = win.console; 78 let { ConsoleAPI } = ChromeUtils.importESModule( 79 "resource://gre/modules/Console.sys.mjs" 80 ); 81 win.console = new ConsoleAPI(); 82 }, 83 84 tearDownWindow(win) { 85 if (win.nativeConsole) { 86 win.console = win.nativeConsole; 87 win.nativeConsole = undefined; 88 } 89 }, 90 91 onOpenWindow(xulWin) { 92 let win = xulWin.docShell.domWindow; 93 94 win.addEventListener( 95 "load", 96 function () { 97 if ( 98 win.document.documentElement.getAttribute("windowtype") == WINDOW_TYPE 99 ) { 100 WindowListener.setupWindow(win); 101 } 102 }, 103 { once: true } 104 ); 105 }, 106 }; 107 108 function loadMochitest(e) { 109 let flavor = e.detail[0]; 110 let url = e.detail[1]; 111 112 let win = Services.wm.getMostRecentWindow(WINDOW_TYPE); 113 win.removeEventListener("mochitest-load", loadMochitest); 114 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(), 119 }); 120 } 121 if (flavor == "mochitest") { 122 return; 123 } 124 125 WindowListener.setupWindow(win); 126 Services.wm.addListener(WindowListener); 127 128 loadChromeScripts(win); 129 } 130 131 this.mochikit = class extends ExtensionAPI { 132 onStartup() { 133 let aomStartup = Cc[ 134 "@mozilla.org/addons/addon-manager-startup;1" 135 ].getService(Ci.amIAddonManagerStartup); 136 const manifestURI = Services.io.newURI( 137 "manifest.json", 138 null, 139 this.extension.rootURI 140 ); 141 const targetURL = this.extension.rootURI.resolve("content/"); 142 this.chromeHandle = aomStartup.registerChrome(manifestURI, [ 143 ["content", "mochikit", targetURL], 144 ]); 145 146 if (AppConstants.platform == "android") { 147 androidStartup(); 148 } else { 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); 153 } 154 } 155 156 onShutdown() { 157 if (AppConstants.platform != "android") { 158 for (let win of Services.wm.getEnumerator(WINDOW_TYPE)) { 159 WindowListener.tearDownWindow(win); 160 } 161 162 Services.wm.removeListener(WindowListener); 163 } 164 165 this.chromeHandle.destruct(); 166 this.chromeHandle = null; 167 } 168 };