Bug 1252215 - [webext] add sendMessage support and mochitest to ClassicExtensionContext. r=aswan,kmag draft
authorLuca Greco <lgreco@mozilla.com>
Tue, 17 May 2016 14:24:52 +0200
changeset 371014 891b200f33958c1614b3f526941fd05aa6454188
parent 371013 0baa6359ce5dff8f6ee158e8a124415c2810d835
child 371015 dcc55af962e6e96cbf57c52e2aeb44d41036818e
push id19202
push userluca.greco@alcacoop.it
push dateWed, 25 May 2016 20:04:11 +0000
reviewersaswan, kmag
bugs1252215
milestone49.0a1
Bug 1252215 - [webext] add sendMessage support and mochitest to ClassicExtensionContext. r=aswan,kmag MozReview-Commit-ID: 4tR6UVGBfQv
toolkit/components/extensions/ClassicExtensionsUtils.jsm
toolkit/components/extensions/test/mochitest/test_chrome_ext_classic_extension_context.html
toolkit/components/extensions/test/mochitest/test_chrome_ext_classic_extension_context_contentscript.html
--- a/toolkit/components/extensions/ClassicExtensionsUtils.jsm
+++ b/toolkit/components/extensions/ClassicExtensionsUtils.jsm
@@ -91,16 +91,17 @@ class ClassicExtensionContext extends Ba
     this.messenger = new Messenger(this, [Services.mm, Services.ppmm],
                                        sender, filter, delegate);
 
     this._cloneScope = Cu.Sandbox(this.addonPrincipal, {});
     Cu.setSandboxMetadata(this._cloneScope, {addonId: targetExtension.id});
 
     this.api = {
       onConnect: this.messenger.onConnect("runtime.onConnect"),
+      onMessage: this.messenger.onMessage("runtime.onMessage"),
     };
   }
 
   /**
    * Signal that the context is shutting down and call the unload method.
    * Called when the extension shuts down.
    */
   shutdown() {
--- a/toolkit/components/extensions/test/mochitest/test_chrome_ext_classic_extension_context.html
+++ b/toolkit/components/extensions/test/mochitest/test_chrome_ext_classic_extension_context.html
@@ -38,23 +38,29 @@ add_task(function* test_classic_extensio
       bgURL,
       // Extract the assigned uuid from the background page url.
       uuid: bgURL.match("://(.+?)/")[1],
     };
 
     browser.test.sendMessage("webextension-ready", extensionInfo);
 
     browser.test.onMessage.addListener(msg => {
-      if (msg == "do-connect") {
+      if (msg == "do-send-message") {
+        browser.runtime.sendMessage("webextension -> classic_extension message", (reply) => {
+          browser.test.assertEq("classic_extension -> webextension reply", reply,
+                                "Got the expected message from the ClassicExtensionContext");
+          browser.test.sendMessage("got-reply-message");
+        });
+      } else if (msg == "do-connect") {
         let port = browser.runtime.connect();
 
         port.onMessage.addListener(msg => {
-          browser.test.assertEq("classic_extension -> webextension", msg,
+          browser.test.assertEq("classic_extension -> webextension port message", msg,
                                 "Got the expected message from the ClassicExtensionContext");
-          port.postMessage("webextension -> classic_extension");
+          port.postMessage("webextension -> classic_extension port message");
         });
       }
     });
   }
 
   let extensionData = {
     background: "new " + backgroundScript,
   };
@@ -80,16 +86,37 @@ add_task(function* test_classic_extensio
 
   ok(classicContext, "Got a ClassicExtensionContext instance");
 
   is(classicContext.type, "classic_extension",
      "ClassicExtensionContext instance has the expected type");
 
   ok(classicContext.api, "Got the API object");
 
+  let waitMessage = new Promise(resolve => {
+    classicContext.api.onMessage.addListener((singleMsg, msgSender, sendReply) => {
+      sendReply("classic_extension -> webextension reply");
+      resolve({singleMsg, msgSender});
+    });
+  });
+
+  extension.sendMessage("do-send-message");
+
+  let {singleMsg, msgSender} = yield waitMessage;
+  is(singleMsg, "webextension -> classic_extension message",
+     "Got the expected message");
+  ok(msgSender, "Got a message sender object");
+
+  if (msgSender) {
+    is(msgSender.id, extensionInfo.uuid, "The sender has the expected id property");
+    is(msgSender.url, extensionInfo.bgURL, "The sender has the expected url property");
+  }
+  // Wait confirmation that the reply has been received.
+  yield extension.awaitMessage("got-reply-message");
+
   let waitConnectPort = new Promise(resolve => {
     classicContext.api.onConnect.addListener(port => {
       resolve(port);
     });
   });
 
   extension.sendMessage("do-connect");
 
@@ -106,21 +133,21 @@ add_task(function* test_classic_extensio
   }
 
   let waitPortMessage = new Promise(resolve => {
     port.onMessage.addListener((msg) => {
       resolve(msg);
     });
   });
 
-  port.postMessage("classic_extension -> webextension");
+  port.postMessage("classic_extension -> webextension port message");
 
   let msg = yield waitPortMessage;
 
-  is(msg, "webextension -> classic_extension",
+  is(msg, "webextension -> classic_extension port message",
      "ClassicExtensionContext received the expected message from the webextension");
 
   let waitForDisconnect = new Promise(resolve => {
     port.onDisconnect.addListener(resolve);
   });
 
   yield extension.unload();
 
--- a/toolkit/components/extensions/test/mochitest/test_chrome_ext_classic_extension_context_contentscript.html
+++ b/toolkit/components/extensions/test/mochitest/test_chrome_ext_classic_extension_context_contentscript.html
@@ -45,22 +45,28 @@ add_task(function* test_classic_extensio
           .catch(() => browser.test.notifyFail("current-tab-closed"));
       }
     });
 
     browser.test.sendMessage("ready");
   }
 
   function contentScript() {
+    browser.runtime.sendMessage("webextension -> classic_extension message", (reply) => {
+      browser.test.assertEq("classic_extension -> webextension reply", reply,
+                           "Got the expected reply from the ClassicExtensionContext");
+      browser.test.sendMessage("got-reply-message");
+    });
+
     let port = browser.runtime.connect();
 
     port.onMessage.addListener(msg => {
-      browser.test.assertEq("classic_extension -> webextension", msg,
+      browser.test.assertEq("classic_extension -> webextension port message", msg,
                             "Got the expected message from the ClassicExtensionContext");
-      port.postMessage("webextension -> classic_extension");
+      port.postMessage("webextension -> classic_extension port message");
     });
   }
 
   let extensionData = {
     background: `new ${backgroundScript}`,
     manifest: {
       "content_scripts": [
         {
@@ -103,22 +109,47 @@ add_task(function* test_classic_extensio
   ok(classicContext.api, "Got the API object");
 
   let waitConnectPort = new Promise(resolve => {
     classicContext.api.onConnect.addListener(port => {
       resolve(port);
     });
   });
 
+  let waitMessage = new Promise(resolve => {
+    classicContext.api.onMessage.addListener((singleMsg, msgSender, sendReply) => {
+      sendReply("classic_extension -> webextension reply");
+      resolve({singleMsg, msgSender});
+    });
+  });
+
   yield extension.awaitMessage("ready");
 
   extension.sendMessage("open-test-tab");
 
   let {uuid, tab} = yield extension.awaitMessage("get-expected-sender-info");
 
+  let {singleMsg, msgSender} = yield waitMessage;
+  is(singleMsg, "webextension -> classic_extension message",
+     "Got the expected message");
+  ok(msgSender, "Got a message sender object");
+
+  if (msgSender) {
+    is(msgSender.id, uuid, "The sender has the expected id property");
+    is(msgSender.url, "http://example.com/", "The sender has the expected url property");
+    ok(msgSender.tab, "The sender has a tab property");
+
+    if (msgSender.tab) {
+      is(msgSender.tab.id, tab.id, "The port sender has the expected tab.id");
+    }
+  }
+
+  // Wait confirmation that the reply has been received.
+  yield extension.awaitMessage("got-reply-message");
+
   let port = yield waitConnectPort;
 
   ok(port, "Got the Port API object");
   ok(port.sender, "The port has a sender property");
 
   if (port.sender) {
     is(port.sender.id, uuid, "The port sender has an id property");
     is(port.sender.url, "http://example.com/", "The port sender has the expected url property");
@@ -130,21 +161,21 @@ add_task(function* test_classic_extensio
   }
 
   let waitPortMessage = new Promise(resolve => {
     port.onMessage.addListener((msg) => {
       resolve(msg);
     });
   });
 
-  port.postMessage("classic_extension -> webextension");
+  port.postMessage("classic_extension -> webextension port message");
 
   let msg = yield waitPortMessage;
 
-  is(msg, "webextension -> classic_extension",
+  is(msg, "webextension -> classic_extension port message",
      "ClassicExtensionContext received the expected message from the webextension");
 
   let waitForDisconnect = new Promise(resolve => {
     port.onDisconnect.addListener(resolve);
   });
 
   extension.sendMessage("close-current-tab");