Bug 1290598: Refactor native messaging test setup code into separate head file. r?aswan draft
authorKris Maglione <maglione.k@gmail.com>
Fri, 29 Jul 2016 14:42:03 -0700
changeset 394509 88190e4232c2c89da612def0a77aa05a910a2d9a
parent 394508 657d868280184ce67533c1a5d3116fca607fc143
child 394510 13602468a15cfaa404112477e45caefca9164529
child 394686 df7e30ac15ad8b6a63e9061ee9f33032bb55d5b8
push id24591
push usermaglione.k@gmail.com
push dateFri, 29 Jul 2016 21:57:51 +0000
reviewersaswan
bugs1290598
milestone50.0a1
Bug 1290598: Refactor native messaging test setup code into separate head file. r?aswan MozReview-Commit-ID: BN9oeXt79eG
toolkit/components/extensions/moz.build
toolkit/components/extensions/test/xpcshell/head_native_messaging.js
toolkit/components/extensions/test/xpcshell/native_messaging.ini
toolkit/components/extensions/test/xpcshell/test_ext_native_messaging.js
toolkit/components/extensions/test/xpcshell/test_ext_native_messaging_perf.js
toolkit/components/extensions/test/xpcshell/xpcshell.ini
--- a/toolkit/components/extensions/moz.build
+++ b/toolkit/components/extensions/moz.build
@@ -24,9 +24,12 @@ TESTING_JS_MODULES += [
 ]
 
 DIRS += ['schemas']
 
 JAR_MANIFESTS += ['jar.mn']
 
 MOCHITEST_MANIFESTS += ['test/mochitest/mochitest.ini']
 MOCHITEST_CHROME_MANIFESTS += ['test/mochitest/chrome.ini']
-XPCSHELL_TESTS_MANIFESTS += ['test/xpcshell/xpcshell.ini']
+XPCSHELL_TESTS_MANIFESTS += [
+    'test/xpcshell/native_messaging.ini',
+    'test/xpcshell/xpcshell.ini',
+]
new file mode 100644
--- /dev/null
+++ b/toolkit/components/extensions/test/xpcshell/head_native_messaging.js
@@ -0,0 +1,112 @@
+/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
+/* vim: set sts=2 sw=2 et tw=80: */
+"use strict";
+
+/* globals AppConstants, FileUtils */
+/* exported setupHosts */
+
+XPCOMUtils.defineLazyModuleGetter(this, "MockRegistry",
+                                  "resource://testing-common/MockRegistry.jsm");
+XPCOMUtils.defineLazyModuleGetter(this, "OS",
+                                  "resource://gre/modules/osfile.jsm");
+
+Cu.import("resource://gre/modules/Subprocess.jsm");
+
+
+let tmpDir = FileUtils.getDir("TmpD", ["NativeMessaging"]);
+tmpDir.createUnique(Ci.nsIFile.DIRECTORY_TYPE, FileUtils.PERMS_DIRECTORY);
+
+do_register_cleanup(() => {
+  tmpDir.remove(true);
+});
+
+function getPath(filename) {
+  return OS.Path.join(tmpDir.path, filename);
+}
+
+const ID = "native@tests.mozilla.org";
+
+
+function* setupHosts(scripts) {
+  const PERMS = {unixMode: 0o755};
+
+  const env = Cc["@mozilla.org/process/environment;1"].getService(Ci.nsIEnvironment);
+  const pythonPath = yield Subprocess.pathSearch(env.get("PYTHON"));
+
+  function* writeManifest(script, scriptPath, path) {
+    let body = `#!${pythonPath} -u\n${script.script}`;
+
+    yield OS.File.writeAtomic(scriptPath, body);
+    yield OS.File.setPermissions(scriptPath, PERMS);
+
+    let manifest = {
+      name: script.name,
+      description: script.description,
+      path,
+      type: "stdio",
+      allowed_extensions: [ID],
+    };
+
+    let manifestPath = getPath(`${script.name}.json`);
+    yield OS.File.writeAtomic(manifestPath, JSON.stringify(manifest));
+
+    return manifestPath;
+  }
+
+  switch (AppConstants.platform) {
+    case "macosx":
+    case "linux":
+      let dirProvider = {
+        getFile(property) {
+          if (property == "XREUserNativeMessaging") {
+            return tmpDir.clone();
+          } else if (property == "XRESysNativeMessaging") {
+            return tmpDir.clone();
+          }
+          return null;
+        },
+      };
+
+      Services.dirsvc.registerProvider(dirProvider);
+      do_register_cleanup(() => {
+        Services.dirsvc.unregisterProvider(dirProvider);
+      });
+
+      for (let script of scripts) {
+        let path = getPath(`${script.name}.py`);
+
+        yield writeManifest(script, path, path);
+      }
+      break;
+
+    case "win":
+      const REGKEY = String.raw`Software\Mozilla\NativeMessagingHosts`;
+
+      let registry = new MockRegistry();
+      do_register_cleanup(() => {
+        registry.shutdown();
+      });
+
+      for (let script of scripts) {
+        let batPath = getPath(`${script.name}.bat`);
+        let scriptPath = getPath(`${script.name}.py`);
+
+        let batBody = `@ECHO OFF\n${pythonPath} -u ${scriptPath} %*\n`;
+        yield OS.File.writeAtomic(batPath, batBody);
+
+        // Create absolute and relative path versions of the entry.
+        for (let [name, path] of [[script.name, batPath],
+                                  [`relative.${script.name}`, OS.Path.basename(batPath)]]) {
+          script.name = name;
+          let manifestPath = yield writeManifest(script, scriptPath, path);
+
+          registry.setValue(Ci.nsIWindowsRegKey.ROOT_KEY_CURRENT_USER,
+                            `${REGKEY}\\${script.name}`, "", manifestPath);
+        }
+      }
+      break;
+
+    default:
+      ok(false, `Native messaging is not supported on ${AppConstants.platform}`);
+  }
+}
new file mode 100644
--- /dev/null
+++ b/toolkit/components/extensions/test/xpcshell/native_messaging.ini
@@ -0,0 +1,10 @@
+[DEFAULT]
+head = head.js head_native_messaging.js
+tail =
+firefox-appdir = browser
+skip-if = toolkit == 'gonk' || appname == "thunderbird" || os == "android"
+subprocess = true
+support-files =
+  data/**
+
+[test_ext_native_messaging_perf.js]
rename from toolkit/components/extensions/test/xpcshell/test_ext_native_messaging.js
rename to toolkit/components/extensions/test/xpcshell/test_ext_native_messaging_perf.js
--- a/toolkit/components/extensions/test/xpcshell/test_ext_native_messaging.js
+++ b/toolkit/components/extensions/test/xpcshell/test_ext_native_messaging_perf.js
@@ -7,29 +7,16 @@ XPCOMUtils.defineLazyModuleGetter(this, 
 XPCOMUtils.defineLazyModuleGetter(this, "OS",
                                   "resource://gre/modules/osfile.jsm");
 
 Cu.import("resource://gre/modules/Subprocess.jsm");
 
 const MAX_ROUND_TRIP_TIME_MS = AppConstants.DEBUG || AppConstants.ASAN ? 36 : 18;
 
 
-let tmpDir = FileUtils.getDir("TmpD", ["NativeMessaging"]);
-tmpDir.createUnique(Ci.nsIFile.DIRECTORY_TYPE, FileUtils.PERMS_DIRECTORY);
-
-do_register_cleanup(() => {
-  tmpDir.remove(true);
-});
-
-function getPath(filename) {
-  return OS.Path.join(tmpDir.path, filename);
-}
-
-const ID = "native@tests.mozilla.org";
-
 const ECHO_BODY = String.raw`
   import struct
   import sys
 
   while True:
       rawlen = sys.stdin.read(4)
       if len(rawlen) == 0:
           sys.exit(0)
@@ -45,93 +32,17 @@ const SCRIPTS = [
   {
     name: "echo",
     description: "A native app that echoes back messages it receives",
     script: ECHO_BODY.replace(/^ {2}/gm, ""),
   },
 ];
 
 add_task(function* setup() {
-  const PERMS = {unixMode: 0o755};
-
-  let pythonPath = yield Subprocess.pathSearch("python2.7").catch(err => {
-    return Subprocess.pathSearch("python");
-  });
-
-  function* writeManifest(script, scriptPath, path) {
-    let body = `#!${pythonPath} -u\n${script.script}`;
-
-    yield OS.File.writeAtomic(scriptPath, body);
-    yield OS.File.setPermissions(scriptPath, PERMS);
-
-    let manifest = {
-      name: script.name,
-      description: script.description,
-      path,
-      type: "stdio",
-      allowed_extensions: [ID],
-    };
-
-    let manifestPath = getPath(`${script.name}.json`);
-    yield OS.File.writeAtomic(manifestPath, JSON.stringify(manifest));
-
-    return manifestPath;
-  }
-
-  switch (AppConstants.platform) {
-    case "macosx":
-    case "linux":
-      let dirProvider = {
-        getFile(property) {
-          if (property == "XREUserNativeMessaging") {
-            return tmpDir.clone();
-          } else if (property == "XRESysNativeMessaging") {
-            return tmpDir.clone();
-          }
-          return null;
-        },
-      };
-
-      Services.dirsvc.registerProvider(dirProvider);
-      do_register_cleanup(() => {
-        Services.dirsvc.unregisterProvider(dirProvider);
-      });
-
-      for (let script of SCRIPTS) {
-        let path = getPath(`${script.name}.py`);
-
-        yield writeManifest(script, path, path);
-      }
-      break;
-
-    case "win":
-      const REGKEY = String.raw`Software\Mozilla\NativeMessagingHosts`;
-
-      let registry = new MockRegistry();
-      do_register_cleanup(() => {
-        registry.shutdown();
-      });
-
-      for (let script of SCRIPTS) {
-        let batPath = getPath(`${script.name}.bat`);
-        let scriptPath = getPath(`${script.name}.py`);
-
-        let batBody = `@ECHO OFF\n${pythonPath} -u ${scriptPath} %*\n`;
-        yield OS.File.writeAtomic(batPath, batBody);
-
-        let manifestPath = yield writeManifest(script, scriptPath, batPath);
-
-        registry.setValue(Ci.nsIWindowsRegKey.ROOT_KEY_CURRENT_USER,
-                          `${REGKEY}\\${script.name}`, "", manifestPath);
-      }
-      break;
-
-    default:
-      ok(false, `Native messaging is not supported on ${AppConstants.platform}`);
-  }
+  yield setupHosts(SCRIPTS);
 });
 
 add_task(function* test_round_trip_perf() {
   let extension = ExtensionTestUtils.loadExtension({
     background() {
       let port = browser.runtime.connectNative("echo");
 
       function next() {
--- a/toolkit/components/extensions/test/xpcshell/xpcshell.ini
+++ b/toolkit/components/extensions/test/xpcshell/xpcshell.ini
@@ -27,17 +27,16 @@ skip-if = os == "android"
 [test_ext_downloads_search.js]
 skip-if = os == "android"
 [test_ext_extension.js]
 [test_ext_idle.js]
 [test_ext_json_parser.js]
 [test_ext_localStorage.js]
 [test_ext_manifest_content_security_policy.js]
 [test_ext_manifest_incognito.js]
-[test_ext_native_messaging.js]
 [test_ext_onmessage_removelistener.js]
 [test_ext_runtime_getPlatformInfo.js]
 [test_ext_runtime_sendMessage.js]
 [test_ext_schemas.js]
 [test_ext_simple.js]
 [test_ext_storage.js]
 [test_getAPILevelForWindow.js]
 [test_locale_converter.js]