Bug 1613536 - Disable compose UI while browser.compose.onBeforeSend is fired. r=mkmelin
authorGeoff Lankow <geoff@darktrojan.net>
Wed, 05 Feb 2020 17:45:37 +1300
changeset 37213 dbc7f4937bb1a317544c21b5f3ed0a6684bcc30f
parent 37212 ef09d4e957808535d27b33c5d2ff627216afb53e
child 37214 3fc1169f58c9ef46511cfc88bc86831297f6af79
push id2552
push userclokep@gmail.com
push dateMon, 10 Feb 2020 21:24:16 +0000
treeherdercomm-beta@f95a6f4408a3 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmkmelin
bugs1613536
Bug 1613536 - Disable compose UI while browser.compose.onBeforeSend is fired. r=mkmelin
mail/components/extensions/parent/ext-compose.js
mail/components/extensions/test/browser/browser_ext_compose_onBeforeSend.js
--- a/mail/components/extensions/parent/ext-compose.js
+++ b/mail/components/extensions/parent/ext-compose.js
@@ -153,32 +153,38 @@ var composeEventTracker = new (class ext
     }
   }
   async handleEvent(event) {
     event.preventDefault();
 
     let msgType = event.detail;
     let composeWindow = event.target;
 
+    composeWindow.ToggleWindowLock(true);
+
     let results = await this.emit(
       "compose-before-send",
       getComposeState(composeWindow)
     );
-    if (results && results.length > 0) {
+    if (results) {
       for (let result of results) {
-        if (result) {
-          if (result.cancel) {
-            return;
-          }
-          if (result.details) {
-            setComposeState(composeWindow, result.details);
-          }
+        if (!result) {
+          continue;
+        }
+        if (result.cancel) {
+          composeWindow.ToggleWindowLock(false);
+          return;
+        }
+        if (result.details) {
+          setComposeState(composeWindow, result.details);
         }
       }
     }
+
+    composeWindow.ToggleWindowLock(false);
     composeWindow.CompleteGenericSendMessage(msgType);
   }
 })();
 
 this.compose = class extends ExtensionAPI {
   getAPI(context) {
     function getComposeTab(tabId) {
       let tab = tabManager.get(tabId);
--- a/mail/components/extensions/test/browser/browser_ext_compose_onBeforeSend.js
+++ b/mail/components/extensions/test/browser/browser_ext_compose_onBeforeSend.js
@@ -17,34 +17,34 @@ add_task(async () => {
           let listener = window => {
             browser.windows[eventName].removeListener(listener);
             resolve(window);
           };
           browser.windows[eventName].addListener(listener);
         });
       }
 
-      async function beginSend(sendIsFailure) {
+      async function beginSend(sendExpected, lockExpected) {
         await new Promise(resolve => {
           browser.test.onMessage.addListener(function listener() {
             browser.test.onMessage.removeListener(listener);
             resolve();
           });
           browser.test.sendMessage("beginSend");
         });
-        return checkIfSent(sendIsFailure);
+        return checkIfSent(sendExpected, lockExpected);
       }
 
-      function checkIfSent(sendIsFailure) {
+      function checkIfSent(sendExpected, lockExpected = null) {
         return new Promise(resolve => {
           browser.test.onMessage.addListener(function listener() {
             browser.test.onMessage.removeListener(listener);
             resolve();
           });
-          browser.test.sendMessage("checkIfSent", sendIsFailure);
+          browser.test.sendMessage("checkIfSent", sendExpected, lockExpected);
         });
       }
 
       function checkWindow(expected) {
         return new Promise(resolve => {
           browser.test.onMessage.addListener(function listener() {
             browser.test.onMessage.removeListener(listener);
             resolve();
@@ -64,73 +64,73 @@ add_task(async () => {
       });
       let createdWindow = await createdWindowPromise;
       browser.test.assertEq("messageCompose", createdWindow.type);
 
       await checkWindow({ to: ["test@test.invalid"], subject: "Test" });
 
       // Send the message. No listeners exist, so sending should continue.
 
-      await beginSend(false);
+      await beginSend(true);
 
       // Add a non-cancelling listener. Sending should continue.
 
       let listener1 = () => {
         listener1.fired = true;
         return {};
       };
       browser.compose.onBeforeSend.addListener(listener1);
-      await beginSend(false);
+      await beginSend(true);
       browser.test.assertTrue(listener1.fired, "listener1 was fired");
       browser.compose.onBeforeSend.removeListener(listener1);
 
       // Add a cancelling listener. Sending should not continue.
 
       let listener2 = () => {
         listener2.fired = true;
         return { cancel: true };
       };
       browser.compose.onBeforeSend.addListener(listener2);
-      await beginSend(true);
+      await beginSend(false, false);
       browser.test.assertTrue(listener2.fired, "listener2 was fired");
       browser.compose.onBeforeSend.removeListener(listener2);
-      await beginSend(false); // Removing the listener worked.
+      await beginSend(true); // Removing the listener worked.
 
       // Add a listener returning a Promise. Resolve the Promise to unblock.
       // Sending should continue.
 
       let listener3 = () => {
         listener3.fired = true;
         return new Promise(resolve => {
           listener3.resolve = resolve;
         });
       };
       browser.compose.onBeforeSend.addListener(listener3);
-      await beginSend(true);
+      await beginSend(false, true);
       browser.test.assertTrue(listener3.fired, "listener3 was fired");
       listener3.resolve({ cancel: false });
-      await checkIfSent(false);
+      await checkIfSent(true);
       browser.compose.onBeforeSend.removeListener(listener3);
 
       // Add a listener returning a Promise. Resolve the Promise to cancel.
       // Sending should not continue.
 
       let listener4 = () => {
         listener4.fired = true;
         return new Promise(resolve => {
           listener4.resolve = resolve;
         });
       };
       browser.compose.onBeforeSend.addListener(listener4);
-      await beginSend(true);
+      await beginSend(false, true);
       browser.test.assertTrue(listener4.fired, "listener4 was fired");
       listener4.resolve({ cancel: true });
-      await checkIfSent(true);
+      await checkIfSent(false, false);
       browser.compose.onBeforeSend.removeListener(listener4);
-      await beginSend(false); // Removing the listener worked.
+      await beginSend(true); // Removing the listener worked.
 
       // Add a listener that changes the subject. Sending should continue and
       // the subject should change. This is largely the same code as tested in
       // browser_ext_compose_details.js, so just test that the change happens.
 
       // First check that the original headers are unmodified.
       await checkWindow({ to: ["test@test.invalid"], subject: "Test" });
 
@@ -139,17 +139,17 @@ add_task(async () => {
         listener5.details = details;
         return {
           details: {
             subject: "Changed by listener5",
           },
         };
       };
       browser.compose.onBeforeSend.addListener(listener5);
-      await beginSend(false);
+      await beginSend(true);
       browser.test.assertTrue(listener5.fired, "listener5 was fired");
       browser.test.assertEq(1, listener5.details.to.length);
       browser.test.assertEq(
         "test@test.invalid",
         listener5.details.to[0],
         "listener5 recipient correct"
       );
       browser.test.assertEq(
@@ -196,21 +196,23 @@ add_task(async () => {
   extension.onMessage("beginSend", async () => {
     let composeWindows = [...Services.wm.getEnumerator("msgcompose")];
     is(composeWindows.length, 1);
 
     composeWindows[0].GenericSendMessage(Ci.nsIMsgCompDeliverMode.Now);
     extension.sendMessage();
   });
 
-  extension.onMessage("checkIfSent", async sendIsFailure => {
-    if (didTryToSendMessage) {
-      ok(!sendIsFailure, "tried to send a message, but should not have");
-    } else {
-      ok(sendIsFailure, "didn't try to send a message, but should have");
+  extension.onMessage("checkIfSent", async (sendExpected, lockExpected) => {
+    is(didTryToSendMessage, sendExpected, "did try to send a message");
+
+    if (lockExpected !== null) {
+      let composeWindows = [...Services.wm.getEnumerator("msgcompose")];
+      is(composeWindows.length, 1);
+      is(composeWindows[0].gWindowLocked, lockExpected, "window is locked");
     }
 
     didTryToSendMessage = false;
     extension.sendMessage();
   });
 
   extension.onMessage("checkWindow", async expected => {
     await checkComposeHeaders(expected);