Bug 1472338: part 1) Add Chrome tests for the async Clipboard API. r=NeilDeakin
[gecko.git] / testing / mochitest / api.js
blob0ec26b9d037604ec91acb50cd69aed044b9e028d
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",
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   );
22 // ///// Android ///////
24 const windowTracker = {
25   init() {
26     Services.obs.addObserver(this, "chrome-document-global-created");
27   },
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 })
33       );
35       let { document } = window;
36       let { documentURI } = document;
38       if (documentURI !== AppConstants.BROWSER_CHROME_URL) {
39         return;
40       }
41       loadChromeScripts(window);
42     }
43   },
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     }
60     windowTracker.init();
61   }
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.
74   setupWindow(win) {
75     win.nativeConsole = win.console;
76     ChromeUtils.defineModuleGetter(
77       win,
78       "console",
79       "resource://gre/modules/Console.jsm"
80     );
81   },
83   tearDownWindow(win) {
84     if (win.nativeConsole) {
85       win.console = win.nativeConsole;
86       win.nativeConsole = undefined;
87     }
88   },
90   onOpenWindow(xulWin) {
91     let win = xulWin.docShell.domWindow;
93     win.addEventListener(
94       "load",
95       function() {
96         if (
97           win.document.documentElement.getAttribute("windowtype") == WINDOW_TYPE
98         ) {
99           WindowListener.setupWindow(win);
100         }
101       },
102       { once: true }
103     );
104   },
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) {
116     win.loadURI(
117       url,
118       null,
119       null,
120       null,
121       null,
122       null,
123       null,
124       null,
125       Services.scriptSecurityManager.getSystemPrincipal()
126     );
127   }
128   if (flavor == "mochitest") {
129     return;
130   }
132   WindowListener.setupWindow(win);
133   Services.wm.addListener(WindowListener);
135   loadChromeScripts(win);
138 this.mochikit = class extends ExtensionAPI {
139   onStartup() {
140     let aomStartup = Cc[
141       "@mozilla.org/addons/addon-manager-startup;1"
142     ].getService(Ci.amIAddonManagerStartup);
143     const manifestURI = Services.io.newURI(
144       "manifest.json",
145       null,
146       this.extension.rootURI
147     );
148     const targetURL = this.extension.rootURI.resolve("content/");
149     this.chromeHandle = aomStartup.registerChrome(manifestURI, [
150       ["content", "mochikit", targetURL],
151     ]);
153     if (AppConstants.platform == "android") {
154       androidStartup();
155     } else {
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);
160     }
161   }
163   onShutdown() {
164     if (AppConstants.platform != "android") {
165       for (let win of Services.wm.getEnumerator(WINDOW_TYPE)) {
166         WindowListener.tearDownWindow(win);
167       }
169       Services.wm.removeListener(WindowListener);
170     }
172     this.chromeHandle.destruct();
173     this.chromeHandle = null;
174   }