Bug 1451513 Part 2: Convert mochikit to a webextension draft
authorAndrew Swan <aswan@mozilla.com>
Thu, 05 Jul 2018 15:42:11 -0700
changeset 816167 e8199765fe97abe8607367e1e62d9d5908e619c5
parent 815865 4021e1a901b3294cbf26f2180f02cf04f163e042
push id115762
push useraswan@mozilla.com
push dateTue, 10 Jul 2018 16:59:08 +0000
bugs1451513
milestone63.0a1
Bug 1451513 Part 2: Convert mochikit to a webextension This is another quick-and-dirty port. Some of the test logic could probably be moved to unprivileged extension code but there's no urgent reason to do that right now. MozReview-Commit-ID: Af0XQbakLsx
testing/mochitest/api.js
testing/mochitest/bootstrap.js
testing/mochitest/jar.mn
testing/mochitest/manifest.json
testing/mochitest/moz.build
testing/mochitest/redirect.html
testing/mochitest/schema.json
rename from testing/mochitest/bootstrap.js
rename to testing/mochitest/api.js
--- a/testing/mochitest/bootstrap.js
+++ b/testing/mochitest/api.js
@@ -1,12 +1,14 @@
 /* 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/. */
 
+/* globals ExtensionAPI */
+
 ChromeUtils.import("resource://gre/modules/AppConstants.jsm");
 ChromeUtils.import("resource://gre/modules/NetUtil.jsm");
 ChromeUtils.import("resource://gre/modules/Services.jsm");
 ChromeUtils.import("resource://gre/modules/XPCOMUtils.jsm");
 
 function loadChromeScripts(win) {
   Services.scriptloader.loadSubScript("chrome://mochikit/content/chrome-harness.js", win);
   Services.scriptloader.loadSubScript("chrome://mochikit/content/mochitest-e10s-utils.js", win);
@@ -33,20 +35,30 @@ const windowTracker = {
       if (documentURI !== "chrome://browser/content/browser.xul") {
         return;
       }
       loadChromeScripts(window);
     }
   },
 };
 
-function androidStartup(data, reason) {
+function androidStartup() {
   // Only browser chrome tests need help starting.
   let testRoot = Services.prefs.getStringPref("mochitest.testRoot", "");
   if (testRoot.endsWith("/chrome")) {
+    // The initial window is created from browser startup, which races
+    // against extension initialization.  If it has already been created,
+    // inject the test scripts now, otherwise wait for the browser window
+    // to show up.
+    let win = Services.wm.getMostRecentWindow("navigator:browser");
+    if (win) {
+      loadChromeScripts(win);
+      return;
+    }
+
     windowTracker.init();
   }
 }
 
 // ///// Desktop ///////
 
 var WindowListener = {
   // browser-test.js is only loaded into the first window. Setup that
@@ -89,34 +101,43 @@ function loadMochitest(e) {
   }
 
   WindowListener.setupWindow(win);
   Services.wm.addListener(WindowListener);
 
   loadChromeScripts(win);
 }
 
-function startup(data, reason) {
-  if (AppConstants.platform == "android") {
-    androidStartup(data, reason);
-  } else {
-    let win = Services.wm.getMostRecentWindow("navigator:browser");
-    // wait for event fired from start_desktop.js containing the
-    // suite and url to load
-    win.addEventListener("mochitest-load", loadMochitest);
+this.mochikit = class extends ExtensionAPI {
+  onStartup() {
+    let aomStartup = Cc["@mozilla.org/addons/addon-manager-startup;1"]
+                                 .getService(Ci.amIAddonManagerStartup);
+    const manifestURI = Services.io.newURI("manifest.json", null, this.extension.rootURI);
+    const targetURL = this.extension.rootURI.resolve("content/");
+    this.chromeHandle = aomStartup.registerChrome(manifestURI, [
+      ["content", "mochikit", targetURL],
+    ]);
+
+    if (AppConstants.platform == "android") {
+      androidStartup();
+    } else {
+      let win = Services.wm.getMostRecentWindow("navigator:browser");
+      // wait for event fired from start_desktop.js containing the
+      // suite and url to load
+      win.addEventListener("mochitest-load", loadMochitest);
+    }
   }
-}
 
-function shutdown(data, reason) {
-  if (AppConstants.platform != "android") {
-    let windows = Services.wm.getEnumerator("navigator:browser");
-    while (windows.hasMoreElements()) {
-      let win = windows.getNext().QueryInterface(Ci.nsIDOMWindow);
-      WindowListener.tearDownWindow(win);
+  onShutdown() {
+    if (AppConstants.platform != "android") {
+      let windows = Services.wm.getEnumerator("navigator:browser");
+      while (windows.hasMoreElements()) {
+        let win = windows.getNext().QueryInterface(Ci.nsIDOMWindow);
+        WindowListener.tearDownWindow(win);
+      }
+
+      Services.wm.removeListener(WindowListener);
     }
 
-    Services.wm.removeListener(WindowListener);
+    this.chromeHandle.destruct();
+    this.chromeHandle = null;
   }
-}
-
-function install(data, reason) {}
-function uninstall(data, reason) {}
-
+};
deleted file mode 100644
--- a/testing/mochitest/jar.mn
+++ /dev/null
@@ -1,42 +0,0 @@
-mochikit.jar:
-% content mochikit %content/
-  content/browser-harness.xul (browser-harness.xul)
-  content/browser-test.js (browser-test.js)
-  content/chrome-harness.js (chrome-harness.js)
-  content/mochitest-e10s-utils.js (mochitest-e10s-utils.js)
-  content/shutdown-leaks-collector.js (shutdown-leaks-collector.js)
-  content/ShutdownLeaksCollector.jsm (ShutdownLeaksCollector.jsm)
-  content/harness.xul (harness.xul)
-  content/redirect.html (redirect.html)
-  content/server.js (server.js)
-  content/chunkifyTests.js (chunkifyTests.js)
-  content/manifestLibrary.js (manifestLibrary.js)
-  content/nested_setup.js (nested_setup.js)
-  content/dynamic/getMyDirectory.sjs (dynamic/getMyDirectory.sjs)
-  content/static/harness.css (static/harness.css)
-  content/tests/SimpleTest/ChromePowers.js (tests/SimpleTest/ChromePowers.js)
-  content/tests/SimpleTest/EventUtils.js (tests/SimpleTest/EventUtils.js)
-  content/tests/SimpleTest/ExtensionTestUtils.js (tests/SimpleTest/ExtensionTestUtils.js)
-  content/tests/SimpleTest/AddTask.js (tests/SimpleTest/AddTask.js)
-  content/tests/SimpleTest/AsyncUtilsContent.js (tests/SimpleTest/AsyncUtilsContent.js)
-  content/tests/SimpleTest/LogController.js (tests/SimpleTest/LogController.js)
-  content/tests/SimpleTest/MemoryStats.js (tests/SimpleTest/MemoryStats.js)
-  content/tests/SimpleTest/MozillaLogger.js (../specialpowers/content/MozillaLogger.js)
-  content/tests/SimpleTest/specialpowers.js (../specialpowers/content/specialpowers.js)
-  content/tests/SimpleTest/SpecialPowersObserverAPI.js (../specialpowers/content/SpecialPowersObserverAPI.js)
-  content/tests/SimpleTest/specialpowersAPI.js (../specialpowers/content/specialpowersAPI.js)
-  content/tests/SimpleTest/setup.js (tests/SimpleTest/setup.js)
-  content/tests/SimpleTest/SimpleTest.js (tests/SimpleTest/SimpleTest.js)
-  content/tests/SimpleTest/StructuredLog.jsm (../modules/StructuredLog.jsm)
-  content/tests/SimpleTest/test.css (tests/SimpleTest/test.css)
-  content/tests/SimpleTest/TestRunner.js (tests/SimpleTest/TestRunner.js)
-  content/tests/SimpleTest/iframe-between-tests.html (tests/SimpleTest/iframe-between-tests.html)
-  content/tests/SimpleTest/WindowSnapshot.js (tests/SimpleTest/WindowSnapshot.js)
-  content/tests/SimpleTest/MockObjects.js (tests/SimpleTest/MockObjects.js)
-  content/tests/SimpleTest/NativeKeyCodes.js (tests/SimpleTest/NativeKeyCodes.js)
-  content/tests/SimpleTest/paint_listener.js (tests/SimpleTest/paint_listener.js)
-  content/tests/SimpleTest/docshell_helpers.js (../../docshell/test/chrome/docshell_helpers.js)
-  content/tests/BrowserTestUtils/content-task.js (BrowserTestUtils/content/content-task.js)
-  content/tests/BrowserTestUtils/content-about-page-utils.js (BrowserTestUtils/content/content-about-page-utils.js)
-  content/tests/BrowserTestUtils/content-utils.js (BrowserTestUtils/content/content-utils.js)
-
new file mode 100644
--- /dev/null
+++ b/testing/mochitest/manifest.json
@@ -0,0 +1,22 @@
+{
+  "manifest_version": 2,
+  "name": "Mochitest",
+  "version": "2.0",
+
+  "applications": {
+    "gecko": {
+      "id": "mochikit@mozilla.org"
+    }
+  },
+
+  "experiment_apis": {
+    "mochikit": {
+      "schema": "schema.json",
+      "parent": {
+        "scopes": ["addon_parent"],
+        "script": "api.js",
+        "events": ["startup"]
+      }
+    }
+  }
+}
--- a/testing/mochitest/moz.build
+++ b/testing/mochitest/moz.build
@@ -8,23 +8,77 @@ DIRS += [
     'manifests',
     'tests',
     'ssltunnel',
     'BrowserTestUtils',
 ]
 
 XPI_NAME = 'mochijar'
 
-JAR_MANIFESTS += ['jar.mn']
-
 USE_EXTENSION_MANIFEST = True
 
-FINAL_TARGET_PP_FILES += ['install.rdf']
+FINAL_TARGET_FILES += [
+    'api.js',
+    'manifest.json',
+    'schema.json',
+]
+
+FINAL_TARGET_FILES.content += [
+    'browser-harness.xul',
+    'browser-test.js',
+    'chrome-harness.js',
+    'chunkifyTests.js',
+    'harness.xul',
+    'manifestLibrary.js',
+    'mochitest-e10s-utils.js',
+    'nested_setup.js',
+    'redirect.html',
+    'server.js',
+    'shutdown-leaks-collector.js',
+    'ShutdownLeaksCollector.jsm',
+]
+
+FINAL_TARGET_FILES.content.dynamic += [
+    'dynamic/getMyDirectory.sjs',
+]
+
+FINAL_TARGET_FILES.content.static += [
+    'static/harness.css',
+]
 
-FINAL_TARGET_FILES += ['bootstrap.js']
+FINAL_TARGET_FILES.content.tests.SimpleTest += [
+    '../../docshell/test/chrome/docshell_helpers.js',
+    '../modules/StructuredLog.jsm',
+    '../specialpowers/content/MozillaLogger.js',
+    '../specialpowers/content/specialpowers.js',
+    '../specialpowers/content/specialpowersAPI.js',
+    '../specialpowers/content/SpecialPowersObserverAPI.js',
+    'tests/SimpleTest/AddTask.js',
+    'tests/SimpleTest/AsyncUtilsContent.js',
+    'tests/SimpleTest/ChromePowers.js',
+    'tests/SimpleTest/EventUtils.js',
+    'tests/SimpleTest/ExtensionTestUtils.js',
+    'tests/SimpleTest/iframe-between-tests.html',
+    'tests/SimpleTest/LogController.js',
+    'tests/SimpleTest/MemoryStats.js',
+    'tests/SimpleTest/MockObjects.js',
+    'tests/SimpleTest/NativeKeyCodes.js',
+    'tests/SimpleTest/paint_listener.js',
+    'tests/SimpleTest/setup.js',
+    'tests/SimpleTest/SimpleTest.js',
+    'tests/SimpleTest/test.css',
+    'tests/SimpleTest/TestRunner.js',
+    'tests/SimpleTest/WindowSnapshot.js',
+]
+
+FINAL_TARGET_FILES.content.tests.BrowserTestUtils += [
+    'BrowserTestUtils/content/content-about-page-utils.js',
+    'BrowserTestUtils/content/content-task.js',
+    'BrowserTestUtils/content/content-utils.js',
+]
 
 MOCHITEST_MANIFESTS += [
     'baselinecoverage/plain/mochitest.ini',
     'tests/MochiKit-1.4.2/tests/mochitest.ini',
 ]
 
 MOCHITEST_CHROME_MANIFESTS += [
     'baselinecoverage/chrome/chrome.ini',
--- a/testing/mochitest/redirect.html
+++ b/testing/mochitest/redirect.html
@@ -20,18 +20,19 @@
       redirect("chrome://mochikit/content/harness.xul");
     }
 
     function onLoad() {
       // Wait for MozAfterPaint, since the listener in browser-test.js is not
       // added until then.
       window.addEventListener("MozAfterPaint", function() {
         setTimeout(redirectToHarness, 0);
-        // In case the listener was not ready, try again after a few seconds.
-        setTimeout(redirectToHarness, 5000);
+
+        // In case the listener is not ready, re-try periodically
+        setInterval(redirectToHarness, 5000);
       }, {once: true});
 
     }
   </script>
 </head>
 
 <body onload="onLoad();">
 redirecting...
new file mode 100644
--- /dev/null
+++ b/testing/mochitest/schema.json
@@ -0,0 +1,1 @@
+[]