Bug 1447796 - fix closing page action panels using commands, r?rpl draft
authorShane Caraveo <scaraveo@mozilla.com>
Tue, 27 Mar 2018 10:33:31 -0500
changeset 773165 779a7144c27d75b1a354dc71cf3faa40d0a1abf4
parent 773144 b906009d875d1f5d29b0d1252cdb43a9b1a5889c
push id104157
push usermixedpuppy@gmail.com
push dateTue, 27 Mar 2018 15:34:07 +0000
reviewersrpl
bugs1447796
milestone61.0a1
Bug 1447796 - fix closing page action panels using commands, r?rpl If commands are used with the page action panel, they will toggle open or closed. We need to keep a handle on our custom panel so we can close it. MozReview-Commit-ID: JfxwlyK8g8g
browser/components/extensions/ext-pageAction.js
browser/components/extensions/test/browser/browser_ext_commands_execute_page_action.js
--- a/browser/components/extensions/ext-pageAction.js
+++ b/browser/components/extensions/ext-pageAction.js
@@ -269,21 +269,33 @@ this.pageAction = class extends Extensio
 
     this.tabManager.addActiveTabPermission(tab);
 
     // If the widget has a popup URL defined, we open a popup, but do not
     // dispatch a click event to the extension.
     // If it has no popup URL defined, we dispatch a click event, but do not
     // open a popup.
     if (popupURL) {
-      let popup = new PanelPopup(this.extension, window.document, popupURL,
-                                 this.browserStyle);
-      await popup.contentReady;
+      if (this.popupNode && this.popupNode.panel.state !== "closed") {
+        // The panel is being toggled closed.
+        TelemetryStopwatch.cancel(popupOpenTimingHistogram, this);
+        window.BrowserPageActions.togglePanelForAction(this.browserPageAction,
+                                                       this.popupNode.panel);
+        return;
+      }
+
+      this.popupNode = new PanelPopup(this.extension, window.document, popupURL,
+                                      this.browserStyle);
+      // Remove popupNode when it is closed.
+      this.popupNode.panel.addEventListener("popuphiding", () => {
+        this.popupNode = undefined;
+      }, {once: true});
+      await this.popupNode.contentReady;
       window.BrowserPageActions.togglePanelForAction(this.browserPageAction,
-                                                     popup.panel);
+                                                     this.popupNode.panel);
       TelemetryStopwatch.finish(popupOpenTimingHistogram, this);
     } else {
       TelemetryStopwatch.cancel(popupOpenTimingHistogram, this);
       this.emit("click", tab);
     }
   }
 
   /**
--- a/browser/components/extensions/test/browser/browser_ext_commands_execute_page_action.js
+++ b/browser/components/extensions/test/browser/browser_ext_commands_execute_page_action.js
@@ -158,12 +158,21 @@ add_task(async function test_execute_pag
         }, {once: true});
       },
     },
   });
 
   await extension.startup();
   let tab = await BrowserTestUtils.openNewForegroundTab(window.gBrowser, "http://example.com/");
   EventUtils.synthesizeKey("j", {altKey: true, shiftKey: true});
+  info("Waiting for pageAction open.");
   await extension.awaitFinish("page-action-with-popup");
+
+  // Bug 1447796 make sure the key command can close the page action
+  let panel = document.getElementById(`${makeWidgetId(extension.id)}-panel`);
+  let hidden = promisePopupHidden(panel);
+  EventUtils.synthesizeKey("j", {altKey: true, shiftKey: true});
+  info("Waiting for pageAction close.");
+  await hidden;
+
   await extension.unload();
   BrowserTestUtils.removeTab(tab);
 });