Bug 1642671 - Add tests for falsey result from runtime.onMessage r=zombie,geckoview-reviewers,agi
authorRob Wu <rob@robwu.nl>
Wed, 03 Jun 2020 17:26:43 +0000
changeset 597820 36f295b440e6e9968e0a6dfb10741a0ad6776e9d
parent 597819 c593e06b6cf405c5056e20337f2c3acb9883f428
child 597821 ca5509801aab67bed7c465bc0f22f40d130cc23a
push id13310
push userffxbld-merge
push dateMon, 29 Jun 2020 14:50:06 +0000
treeherdermozilla-beta@15a59a0afa5c [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerszombie, geckoview-reviewers, agi
bugs1642671
milestone79.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1642671 - Add tests for falsey result from runtime.onMessage r=zombie,geckoview-reviewers,agi Differential Revision: https://phabricator.services.mozilla.com/D77895
browser/components/extensions/test/browser/browser_ext_tabs_sendMessage.js
mobile/android/components/extensions/test/mochitest/test_ext_tabs_sendMessage.html
toolkit/components/extensions/test/xpcshell/test_ext_runtime_sendMessage.js
--- a/browser/components/extensions/test/browser/browser_ext_tabs_sendMessage.js
+++ b/browser/components/extensions/test/browser/browser_ext_tabs_sendMessage.js
@@ -27,16 +27,18 @@ add_task(async function tabsSendMessageR
               promiseResponse,
 
               browser.tabs.sendMessage(tabId, "respond-now"),
               browser.tabs.sendMessage(tabId, "respond-now-2"),
               new Promise(resolve =>
                 browser.tabs.sendMessage(tabId, "respond-soon", resolve)
               ),
               browser.tabs.sendMessage(tabId, "respond-promise"),
+              browser.tabs.sendMessage(tabId, "respond-promise-false"),
+              browser.tabs.sendMessage(tabId, "respond-false"),
               browser.tabs.sendMessage(tabId, "respond-never"),
               new Promise(resolve => {
                 browser.runtime.sendMessage("respond-never", response => {
                   resolve(response);
                 });
               }),
 
               browser.tabs
@@ -52,16 +54,18 @@ add_task(async function tabsSendMessageR
             ])
               .then(
                 ([
                   response,
                   respondNow,
                   respondNow2,
                   respondSoon,
                   respondPromise,
+                  respondPromiseFalse,
+                  respondFalse,
                   respondNever,
                   respondNever2,
                   respondError,
                   throwError,
                   noListener,
                 ]) => {
                   browser.test.assertEq(
                     "expected-response",
@@ -85,16 +89,26 @@ add_task(async function tabsSendMessageR
                     "Got the expected delayed response"
                   );
                   browser.test.assertEq(
                     "respond-promise",
                     respondPromise,
                     "Got the expected promise response"
                   );
                   browser.test.assertEq(
+                    false,
+                    respondPromiseFalse,
+                    "Got the expected false value as a promise result"
+                  );
+                  browser.test.assertEq(
+                    undefined,
+                    respondFalse,
+                    "Got the expected no-response when onMessage returns false"
+                  );
+                  browser.test.assertEq(
                     undefined,
                     respondNever,
                     "Got the expected no-response resolution"
                   );
                   browser.test.assertEq(
                     undefined,
                     respondNever2,
                     "Got the expected no-response resolution"
@@ -146,16 +160,22 @@ add_task(async function tabsSendMessageR
             respond(msg);
           } else if (msg == "respond-soon") {
             setTimeout(() => {
               respond(msg);
             }, 0);
             return true;
           } else if (msg == "respond-promise") {
             return Promise.resolve(msg);
+          } else if (msg == "respond-promise-false") {
+            return Promise.resolve(false);
+          } else if (msg == "respond-false") {
+            // return false means that respond() is not expected to be called.
+            setTimeout(() => respond("should be ignored"));
+            return false;
           } else if (msg == "respond-never") {
             return undefined;
           } else if (msg == "respond-error") {
             return Promise.reject(new Error(msg));
           } else if (msg == "throw-error") {
             throw new Error(msg);
           }
         });
--- a/mobile/android/components/extensions/test/mochitest/test_ext_tabs_sendMessage.html
+++ b/mobile/android/components/extensions/test/mochitest/test_ext_tabs_sendMessage.html
@@ -40,32 +40,36 @@ add_task(async function tabsSendMessageR
 
             Promise.all([
               promiseResponse,
 
               browser.tabs.sendMessage(tabId, "respond-now"),
               browser.tabs.sendMessage(tabId, "respond-now-2"),
               new Promise(resolve => browser.tabs.sendMessage(tabId, "respond-soon", resolve)),
               browser.tabs.sendMessage(tabId, "respond-promise"),
+              browser.tabs.sendMessage(tabId, "respond-promise-false"),
+              browser.tabs.sendMessage(tabId, "respond-false"),
               browser.tabs.sendMessage(tabId, "respond-never"),
               new Promise(resolve => {
                 browser.runtime.sendMessage("respond-never", response => { resolve(response); });
               }),
 
               browser.tabs.sendMessage(tabId, "respond-error").catch(error => Promise.resolve({error})),
               browser.tabs.sendMessage(tabId, "throw-error").catch(error => Promise.resolve({error})),
 
               browser.tabs.sendMessage(firstTab, "no-listener").catch(error => Promise.resolve({error})),
-            ]).then(([response, respondNow, respondNow2, respondSoon, respondPromise, respondNever, respondNever2, respondError, throwError, noListener]) => {
+            ]).then(([response, respondNow, respondNow2, respondSoon, respondPromise, respondPromiseFalse, respondFalse, respondNever, respondNever2, respondError, throwError, noListener]) => {
               browser.test.assertEq("expected-response", response, "Content script got the expected response");
 
               browser.test.assertEq("respond-now", respondNow, "Got the expected immediate response");
               browser.test.assertEq("respond-now-2", respondNow2, "Got the expected immediate response from the second listener");
               browser.test.assertEq("respond-soon", respondSoon, "Got the expected delayed response");
               browser.test.assertEq("respond-promise", respondPromise, "Got the expected promise response");
+              browser.test.assertEq(false, respondPromiseFalse, "Got the expected false value as a promise result");
+              browser.test.assertEq(undefined, respondFalse, "Got the expected no-response when onMessage returns false");
               browser.test.assertEq(undefined, respondNever, "Got the expected no-response resolution");
               browser.test.assertEq(undefined, respondNever2, "Got the expected no-response resolution");
 
               browser.test.assertEq("respond-error", respondError.error.message, "Got the expected error response");
               browser.test.assertEq("throw-error", throwError.error.message, "Got the expected thrown error response");
 
               browser.test.assertEq("Could not establish connection. Receiving end does not exist.",
                                     noListener.error.message,
@@ -93,16 +97,22 @@ add_task(async function tabsSendMessageR
         browser.runtime.onMessage.addListener((msg, sender, respond) => {
           if (msg == "respond-now") {
             respond(msg);
           } else if (msg == "respond-soon") {
             setTimeout(() => { respond(msg); }, 0);
             return true;
           } else if (msg == "respond-promise") {
             return Promise.resolve(msg);
+          } else if (msg == "respond-promise-false") {
+            return Promise.resolve(false);
+          } else if (msg == "respond-false") {
+            // return false means that respond() is not expected to be called.
+            setTimeout(() => respond("should be ignored"));
+            return false;
           } else if (msg == "respond-never") {
             return undefined;
           } else if (msg == "respond-error") {
             return Promise.reject(new Error(msg));
           } else if (msg == "throw-error") {
             throw new Error(msg);
           }
         });
--- a/toolkit/components/extensions/test/xpcshell/test_ext_runtime_sendMessage.js
+++ b/toolkit/components/extensions/test/xpcshell/test_ext_runtime_sendMessage.js
@@ -1,24 +1,30 @@
 /* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
 /* vim: set sts=2 sw=2 et tw=80: */
 "use strict";
 
-add_task(async function tabsSendMessageReply() {
+add_task(async function runtimeSendMessageReply() {
   function background() {
     browser.runtime.onMessage.addListener((msg, sender, respond) => {
       if (msg == "respond-now") {
         respond(msg);
       } else if (msg == "respond-soon") {
         setTimeout(() => {
           respond(msg);
         }, 0);
         return true;
       } else if (msg == "respond-promise") {
         return Promise.resolve(msg);
+      } else if (msg == "respond-promise-false") {
+        return Promise.resolve(false);
+      } else if (msg == "respond-false") {
+        // return false means that respond() is not expected to be called.
+        setTimeout(() => respond("should be ignored"));
+        return false;
       } else if (msg == "respond-never") {
         return undefined;
       } else if (msg == "respond-error") {
         return Promise.reject(new Error(msg));
       } else if (msg == "throw-error") {
         throw new Error(msg);
       }
     });
@@ -39,16 +45,18 @@ add_task(async function tabsSendMessageR
   function senderScript() {
     Promise.all([
       browser.runtime.sendMessage("respond-now"),
       browser.runtime.sendMessage("respond-now-2"),
       new Promise(resolve =>
         browser.runtime.sendMessage("respond-soon", resolve)
       ),
       browser.runtime.sendMessage("respond-promise"),
+      browser.runtime.sendMessage("respond-promise-false"),
+      browser.runtime.sendMessage("respond-false"),
       browser.runtime.sendMessage("respond-never"),
       new Promise(resolve => {
         browser.runtime.sendMessage("respond-never", response => {
           resolve(response);
         });
       }),
 
       browser.runtime
@@ -59,16 +67,18 @@ add_task(async function tabsSendMessageR
         .catch(error => Promise.resolve({ error })),
     ])
       .then(
         ([
           respondNow,
           respondNow2,
           respondSoon,
           respondPromise,
+          respondPromiseFalse,
+          respondFalse,
           respondNever,
           respondNever2,
           respondError,
           throwError,
         ]) => {
           browser.test.assertEq(
             "respond-now",
             respondNow,
@@ -85,16 +95,26 @@ add_task(async function tabsSendMessageR
             "Got the expected delayed response"
           );
           browser.test.assertEq(
             "respond-promise",
             respondPromise,
             "Got the expected promise response"
           );
           browser.test.assertEq(
+            false,
+            respondPromiseFalse,
+            "Got the expected false value as a promise result"
+          );
+          browser.test.assertEq(
+            undefined,
+            respondFalse,
+            "Got the expected no-response when onMessage returns false"
+          );
+          browser.test.assertEq(
             undefined,
             respondNever,
             "Got the expected no-response resolution"
           );
           browser.test.assertEq(
             undefined,
             respondNever2,
             "Got the expected no-response resolution"
@@ -128,17 +148,17 @@ add_task(async function tabsSendMessageR
     },
   });
 
   await extension.startup();
   await extension.awaitFinish("sendMessage");
   await extension.unload();
 });
 
-add_task(async function tabsSendMessageBlob() {
+add_task(async function runtimeSendMessageBlob() {
   function background() {
     browser.runtime.onMessage.addListener(msg => {
       browser.test.assertTrue(msg.blob instanceof Blob, "Message is a blob");
       return Promise.resolve(msg);
     });
 
     let childFrame = document.createElement("iframe");
     childFrame.src = "extensionpage.html";