Bug 1309003 - [webext] Test an hybrid SDK addon which uses the current SDK module loader. r=kmag draft
authorLuca Greco <lgreco@mozilla.com>
Thu, 26 May 2016 16:25:25 +0200
changeset 423819 f414e3412b7f8552dc7f30e7a0317af15be47edd
parent 423199 7be6b348c431d69f96f0765af3a0c0a0fe56d4bf
child 533530 c08c39ee9c4956c42f486fc6f9d3c3fad312bf1c
push id31996
push userluca.greco@alcacoop.it
push dateTue, 11 Oct 2016 18:20:50 +0000
reviewerskmag
bugs1309003
milestone52.0a1
Bug 1309003 - [webext] Test an hybrid SDK addon which uses the current SDK module loader. r=kmag MozReview-Commit-ID: HS3kacLmpf9
toolkit/components/extensions/test/mochitest/chrome.ini
toolkit/components/extensions/test/mochitest/test_chrome_ext_hybrid_addons.html
--- a/toolkit/components/extensions/test/mochitest/chrome.ini
+++ b/toolkit/components/extensions/test/mochitest/chrome.ini
@@ -7,16 +7,17 @@ tags = webextensions
 
 [test_chrome_ext_background_debug_global.html]
 skip-if = (os == 'android') # android doesn't have devtools
 [test_chrome_ext_background_page.html]
 skip-if = (toolkit == 'android') # android doesn't have devtools
 [test_chrome_ext_eventpage_warning.html]
 [test_chrome_ext_contentscript_unrecognizedprop_warning.html]
 skip-if = (os == 'android') # browser.tabs is undefined. Bug 1258975 on android.
+[test_chrome_ext_hybrid_addons.html]
 [test_chrome_ext_trustworthy_origin.html]
 [test_chrome_ext_webnavigation_resolved_urls.html]
 skip-if = (os == 'android') # browser.tabs is undefined. Bug 1258975 on android.
 [test_chrome_ext_shutdown_cleanup.html]
 [test_chrome_native_messaging_paths.html]
 skip-if = os != "mac" && os != "linux"
 [test_ext_cookies_expiry.html]
 [test_ext_cookies_permissions.html]
new file mode 100644
--- /dev/null
+++ b/toolkit/components/extensions/test/mochitest/test_chrome_ext_hybrid_addons.html
@@ -0,0 +1,142 @@
+<!DOCTYPE HTML>
+<html>
+<head>
+  <title>Test for hybrid addons: SDK or bootstrap.js + embedded WebExtension</title>
+  <script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
+  <script src="chrome://mochikit/content/tests/SimpleTest/SpawnTask.js"></script>
+  <script src="chrome://mochikit/content/tests/SimpleTest/ExtensionTestUtils.js"></script>
+  <script type="text/javascript" src="head.js"></script>
+  <link rel="stylesheet" href="chrome://mochikit/contents/tests/SimpleTest/test.css"/>
+</head>
+<body>
+
+<script type="text/javascript">
+"use strict";
+
+/**
+ * This test contains additional tests that ensure that an SDK hybrid addon
+ * which is using the new module loader can embed a webextension correctly:
+ *
+ * while the other tests related to the "Embedded WebExtension" are focused
+ * on unit testing a specific component, these tests are testing that a complete
+ * hybrid SDK addon works as expected.
+ *
+ * NOTE: this tests are also the only ones which tests an SDK hybrid addon that
+ * uses the new module loader (the one actually used in production by real world
+ * addons these days), while the Addon SDK "embedded-webextension" test addon
+ * uses the old deprecated module loader (as all the other Addon SDK test addons).
+ */
+
+function generateClassicExtensionFiles({id, files}) {
+  // The addon install.rdf file, as it would be generated by jpm from the addon
+  // package.json metadata.
+  files["install.rdf"] = `<?xml version="1.0" encoding="utf-8"?>
+    <RDF xmlns="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
+         xmlns:em="http://www.mozilla.org/2004/em-rdf#">
+      <Description about="urn:mozilla:install-manifest">
+        <em:id>${id}</em:id>
+        <em:type>2</em:type>
+        <em:bootstrap>true</em:bootstrap>
+        <em:hasEmbeddedWebExtension>true</em:hasEmbeddedWebExtension>
+        <em:unpack>false</em:unpack>
+        <em:version>0.1.0</em:version>
+        <em:name>Fake Hybrid Addon</em:name>
+        <em:description>A fake hybrid addon</em:description>
+
+        <!-- Firefox -->
+        <em:targetApplication>
+          <Description>
+            <em:id>{ec8030f7-c20a-464f-9b0e-13a3a9e97384}</em:id>
+            <em:minVersion>51.0a1</em:minVersion>
+            <em:maxVersion>*</em:maxVersion>
+          </Description>
+        </em:targetApplication>
+
+        <!-- Fennec -->
+        <em:targetApplication>
+          <Description>
+            <em:id>{aa3c5121-dab2-40e2-81ca-7ea25febc110}</em:id>
+            <em:minVersion>51.0a1</em:minVersion>
+            <em:maxVersion>*</em:maxVersion>
+          </Description>
+        </em:targetApplication>
+      </Description>
+    </RDF>`;
+
+  // The addon package.json file.
+  files["package.json"] = `{
+    "id": "${id}",
+    "name": "hybrid-addon",
+    "version": "0.1.0",
+    "description": "A fake hybrid addon",
+    "main": "index.js",
+    "engines": {
+      "firefox": ">= 51.0a1",
+      "fennec": ">= 51.0a1"
+    },
+    "license": "MPL-2.0",
+    "hasEmbeddedWebExtension": true
+  }`;
+
+  // The bootstrap file that jpm bundle in any SDK addon built with it.
+  files["bootstrap.js"] = `
+    const { utils: Cu } = Components;
+    const rootURI = __SCRIPT_URI_SPEC__.replace("bootstrap.js", "");
+    const COMMONJS_URI = "resource://gre/modules/commonjs";
+    const { require } = Cu.import(COMMONJS_URI + "/toolkit/require.js", {});
+    const { Bootstrap } = require(COMMONJS_URI + "/sdk/addon/bootstrap.js");
+    var { startup, shutdown, install, uninstall } = new Bootstrap(rootURI);
+  `;
+
+  return files;
+}
+
+add_task(function* test_sdk_hybrid_addon_with_jpm_module_loader() {
+  function backgroundScript() {
+    browser.runtime.sendMessage("background message", (reply) => {
+      browser.test.assertEq("sdk received message: background message", reply,
+                            "Got the expected reply from the SDK context");
+      browser.test.notifyPass("sdk.webext-api.onmessage");
+    });
+  }
+
+  function sdkMainScript() {
+    /* globals require */
+    const webext = require("sdk/webextension");
+    webext.startup().then(({browser}) => {
+      browser.runtime.onMessage.addListener((msg, sender, sendReply) => {
+        sendReply(`sdk received message: ${msg}`);
+      });
+    });
+  }
+
+  let id = "fake@sdk.hybrid.addon";
+  let extension = ExtensionTestUtils.loadExtension({
+    useAddonManager: "temporary",
+    files: generateClassicExtensionFiles({
+      id,
+      files: {
+        "index.js": sdkMainScript,
+        "webextension/manifest.json": {
+          name: "embedded webextension name",
+          manifest_version: 2,
+          version: "0.1.0",
+          background: {
+            scripts: ["bg.js"],
+          },
+        },
+        "webextension/bg.js": backgroundScript,
+      },
+    }),
+  }, id);
+
+  extension.startup();
+
+  yield extension.awaitFinish("sdk.webext-api.onmessage");
+
+  yield extension.unload();
+});
+</script>
+
+</body>
+</html>