Bug 1246035 - Add support for _execute_page_action r?kmag draft
authorMatthew Wein <mwein@mozilla.com>
Sat, 05 Mar 2016 16:52:22 -0800
changeset 337225 8b91e971d000ccf541065f9d3e7f48303931a655
parent 337224 46210f3ae07855edfe1383210d78433e38a6b564
child 515602 06902dc0573dfd9f016a4837fe0c98162a41bb48
push id12290
push usermwein@mozilla.com
push dateSun, 06 Mar 2016 01:08:44 +0000
reviewerskmag
bugs1246035
milestone47.0a1
Bug 1246035 - Add support for _execute_page_action r?kmag MozReview-Commit-ID: LPQAC7uJTkr
browser/components/extensions/ext-commands.js
browser/components/extensions/test/browser/browser_ext_commands_onCommand.js
--- a/browser/components/extensions/ext-commands.js
+++ b/browser/components/extensions/ext-commands.js
@@ -12,17 +12,19 @@ var {
 
 const XUL_NS = "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul";
 
 // WeakMap[Extension -> CommandList]
 var commandsMap = new WeakMap();
 
 function CommandList(commandsObj, extensionID) {
   this.commands = this.loadCommandsFromManifest(commandsObj);
-  this.keysetID = `ext-keyset-id-${makeWidgetId(extensionID)}`;
+  this.id = makeWidgetId(extensionID);
+  this.pageActionID = `${this.id}-page-action`;
+  this.keysetID = `ext-keyset-id-${this.id}`;
   this.windowOpenListener = null;
   this.register();
   EventEmitter.decorate(this);
 }
 
 CommandList.prototype = {
   /**
    * Registers the commands to all open windows and to any which
@@ -105,17 +107,24 @@ CommandList.prototype = {
     // We need to have the attribute "oncommand" for the "command" listener to fire,
     // and it is currently ignored when set to the empty string.
     keyElement.setAttribute("oncommand", "//");
 
     /* eslint-disable mozilla/balanced-listeners */
     // We remove all references to the key elements when the extension is shutdown,
     // therefore the listeners for these elements will be garbage collected.
     keyElement.addEventListener("command", (event) => {
-      this.emit("command", name);
+      if (name == "_execute_page_action") {
+        let elem = doc.getElementById(this.pageActionID);
+        if (elem && !elem.hidden) {
+          elem.click();
+        }
+      } else {
+        this.emit("command", name);
+      }
     });
     /* eslint-enable mozilla/balanced-listeners */
 
     return keyElement;
   },
 
   /**
    * Builds a XUL Key element from the provided shortcut.
--- a/browser/components/extensions/test/browser/browser_ext_commands_onCommand.js
+++ b/browser/components/extensions/test/browser/browser_ext_commands_onCommand.js
@@ -1,21 +1,20 @@
 /* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
 /* vim: set sts=2 sw=2 et tw=80: */
 "use strict";
 
-add_task(function* () {
+add_task(function* test_user_defined_commands() {
   // Create a window before the extension is loaded.
   let win1 = yield BrowserTestUtils.openNewBrowserWindow();
   yield BrowserTestUtils.loadURI(win1.gBrowser.selectedBrowser, "about:robots");
   yield BrowserTestUtils.browserLoaded(win1.gBrowser.selectedBrowser);
 
   let extension = ExtensionTestUtils.loadExtension({
     manifest: {
-      "name": "Commands Extension",
       "commands": {
         "toggle-feature-using-alt-shift-3": {
           "suggested_key": {
             "default": "Alt+Shift+3",
           },
         },
         "toggle-feature-using-alt-shift-comma": {
           "suggested_key": {
@@ -67,8 +66,53 @@ add_task(function* () {
   is(keyset, null, "Expected keyset to be removed from the window");
 
   keyset = win2.document.getElementById(keysetID);
   is(keyset, null, "Expected keyset to be removed from the window");
 
   yield BrowserTestUtils.closeWindow(win1);
   yield BrowserTestUtils.closeWindow(win2);
 });
+
+add_task(function* test_reserved_commands() {
+  let extension = ExtensionTestUtils.loadExtension({
+    manifest: {
+      "commands": {
+        "_execute_page_action": {
+          "suggested_key": {
+            "default": "Alt+Shift+J",
+          },
+        },
+      },
+      "page_action": {},
+    },
+
+    background: function() {
+      browser.commands.onCommand.addListener((message) => {
+        if (message == "_execute_page_action") {
+          browser.test.fail("The onCommand listener should never fire for ${message}.");
+        }
+      });
+
+      browser.tabs.query({}, tabs => {
+        tabs.forEach(tab => {
+          browser.pageAction.show(tab.id);
+        });
+      });
+
+      browser.pageAction.onClicked.addListener(() => {
+        browser.test.sendMessage("page-action-clicked");
+      });
+
+      browser.test.sendMessage("ready");
+    },
+  });
+
+  yield extension.startup();
+  yield extension.awaitMessage("ready");
+
+  EventUtils.synthesizeKey("j", {altKey: true, shiftKey: true});
+  yield extension.awaitMessage("page-action-clicked");
+
+  yield extension.unload();
+});
+
+