Bug 1338727 add command support for sidebar-action, r?aswan
MozReview-Commit-ID: Jp1cuTzsDz4
--- a/browser/components/extensions/.eslintrc.js
+++ b/browser/components/extensions/.eslintrc.js
@@ -10,12 +10,13 @@ module.exports = { // eslint-disable-li
"TabContext": true,
"Window": true,
"WindowEventManager": true,
"browserActionFor": true,
"getCookieStoreIdForTab": true,
"getDevToolsTargetForContext": true,
"makeWidgetId": true,
"pageActionFor": true,
+ "sidebarActionFor": true,
"tabTracker": true,
"windowTracker": true,
},
};
--- a/browser/components/extensions/ext-commands.js
+++ b/browser/components/extensions/ext-commands.js
@@ -120,26 +120,32 @@ 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) => {
+ let action;
if (name == "_execute_page_action") {
- let win = event.target.ownerGlobal;
- pageActionFor(this.extension).triggerAction(win);
+ action = pageActionFor(this.extension);
} else if (name == "_execute_browser_action") {
- let win = event.target.ownerGlobal;
- browserActionFor(this.extension).triggerAction(win);
+ action = browserActionFor(this.extension);
+ } else if (name == "_execute_sidebar_action") {
+ action = sidebarActionFor(this.extension);
} else {
this.extension.tabManager
.addActiveTabPermission();
this.emit("command", name);
+ return;
+ }
+ if (action) {
+ let win = event.target.ownerGlobal;
+ action.triggerAction(win);
}
});
/* eslint-enable mozilla/balanced-listeners */
return keyElement;
},
/**
--- a/browser/components/extensions/ext-sidebarAction.js
+++ b/browser/components/extensions/ext-sidebarAction.js
@@ -253,16 +253,29 @@ class SidebarAction {
}
let broadcaster = document.getElementById(this.id);
if (broadcaster) {
broadcaster.remove();
}
}
windowTracker.removeOpenListener(this.windowOpenListener);
}
+
+ /**
+ * Triggers this sidebar action for the given window, with the same effects as
+ * if it were toggled via menu or toolbarbutton by a user.
+ *
+ * @param {ChromeWindow} window
+ */
+ triggerAction(window) {
+ let {SidebarUI} = window;
+ if (SidebarUI) {
+ SidebarUI.toggle(this.id);
+ }
+ }
}
SidebarAction.for = (extension) => {
return sidebarActionMap.get(extension);
};
global.sidebarActionFor = SidebarAction.for;
--- a/browser/components/extensions/test/browser/browser-common.ini
+++ b/browser/components/extensions/test/browser/browser-common.ini
@@ -33,16 +33,17 @@ support-files =
[browser_ext_browserAction_popup_resize.js]
[browser_ext_browserAction_simple.js]
[browser_ext_browsingData_formData.js]
[browser_ext_browsingData_history.js]
[browser_ext_browsingData_pluginData.js]
[browser_ext_browsingData_serviceWorkers.js]
[browser_ext_commands_execute_browser_action.js]
[browser_ext_commands_execute_page_action.js]
+[browser_ext_commands_execute_sidebar_action.js]
[browser_ext_commands_getAll.js]
[browser_ext_commands_onCommand.js]
[browser_ext_contentscript_connect.js]
[browser_ext_contextMenus.js]
[browser_ext_contextMenus_checkboxes.js]
[browser_ext_contextMenus_chrome.js]
[browser_ext_contextMenus_icons.js]
[browser_ext_contextMenus_onclick.js]
new file mode 100644
--- /dev/null
+++ b/browser/components/extensions/test/browser/browser_ext_commands_execute_sidebar_action.js
@@ -0,0 +1,52 @@
+/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
+/* vim: set sts=2 sw=2 et tw=80: */
+"use strict";
+
+add_task(function* test_execute_sidebar_action() {
+ let extension = ExtensionTestUtils.loadExtension({
+ manifest: {
+ "commands": {
+ "_execute_sidebar_action": {
+ "suggested_key": {
+ "default": "Alt+Shift+J",
+ },
+ },
+ },
+ "sidebar_action": {
+ "default_panel": "sidebar.html",
+ },
+ },
+ files: {
+ "sidebar.html": `
+ <!DOCTYPE html>
+ <html>
+ <head>
+ <meta charset="utf-8">
+ <script src="sidebar.js"></script>
+ </head>
+ </html>
+ `,
+
+ "sidebar.js": function() {
+ browser.runtime.sendMessage("from-sidebar-action");
+ },
+ },
+ background() {
+ browser.runtime.onMessage.addListener(msg => {
+ if (msg == "from-sidebar-action") {
+ browser.test.notifyPass("execute-sidebar-action-opened");
+ }
+ });
+ },
+ });
+
+ yield extension.startup();
+ yield SimpleTest.promiseFocus(window);
+ // Since we didn't set useAddonManager, the sidebar will not be automatically
+ // opened for this test.
+ ok(document.getElementById("sidebar-box").hidden, "sidebar box is not visible");
+ // Send the key to open the sidebar.
+ EventUtils.synthesizeKey("j", {altKey: true, shiftKey: true});
+ yield extension.awaitFinish("execute-sidebar-action-opened");
+ yield extension.unload();
+});