Bug 1338727 add command support for sidebar-action, r=aswan

MozReview-Commit-ID: Jp1cuTzsDz4

--HG--
extra : rebase_source : b323170662d5e8ac241314b2bb4210c8d4264af7
This commit is contained in:
Shane Caraveo 2017-02-14 16:24:03 -08:00
parent 44146ebe96
commit c5a8a0ef53
5 changed files with 77 additions and 4 deletions

View File

@ -15,6 +15,7 @@ module.exports = { // eslint-disable-line no-undef
"getDevToolsTargetForContext": true,
"makeWidgetId": true,
"pageActionFor": true,
"sidebarActionFor": true,
"tabTracker": true,
"windowTracker": true,
},

View File

@ -125,16 +125,22 @@ CommandList.prototype = {
// 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 */

View File

@ -252,6 +252,19 @@ class SidebarAction {
}
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) => {

View File

@ -38,6 +38,7 @@ support-files =
[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]

View File

@ -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();
});