Bug 1453355 - Add sidebarAction.toggle() to WebExtensions API r=mixedpuppy

Differential Revision: https://phabricator.services.mozilla.com/D54005

--HG--
extra : moz-landing-system : lando
This commit is contained in:
Mélanie Chauvel (ariasuni) 2019-12-09 14:52:45 +00:00
Родитель 85b60ab28d
Коммит 6180dbb2e7
3 изменённых файлов: 55 добавлений и 0 удалений

Просмотреть файл

@ -420,6 +420,24 @@ this.sidebarAction = class extends ExtensionAPI {
} }
} }
/**
* Toogles this sidebar action for the given window
*
* @param {ChromeWindow} window
*/
toggle(window) {
let { SidebarUI } = window;
if (!SidebarUI || !this.extension.canAccessWindow(window)) {
return;
}
if (!this.isOpen(window)) {
SidebarUI.show(this.id);
} else {
SidebarUI.hide();
}
}
/** /**
* Checks whether this sidebar action is open in the given window. * Checks whether this sidebar action is open in the given window.
* *
@ -488,6 +506,13 @@ this.sidebarAction = class extends ExtensionAPI {
} }
}, },
toggle() {
let window = windowTracker.topWindow;
if (context.canAccessWindow(window)) {
sidebarAction.toggle(window);
}
},
isOpen(details) { isOpen(details) {
let { windowId } = details; let { windowId } = details;
if (windowId == null) { if (windowId == null) {

Просмотреть файл

@ -241,6 +241,14 @@
"async": true, "async": true,
"parameters": [] "parameters": []
}, },
{
"name": "toggle",
"type": "function",
"requireUserInput": true,
"description": "Toggles the extension sidebar in the active window.",
"async": true,
"parameters": []
},
{ {
"name": "isOpen", "name": "isOpen",
"type": "function", "type": "function",

Просмотреть файл

@ -35,6 +35,11 @@ add_task(async function test_openPopup_requires_user_interaction() {
"sidebarAction.close may only be called from a user input handler", "sidebarAction.close may only be called from a user input handler",
"The error is informative." "The error is informative."
); );
await browser.test.assertRejects(
browser.sidebarAction.toggle(),
"sidebarAction.toggle may only be called from a user input handler",
"The error is informative."
);
browser.runtime.onMessage.addListener(async msg => { browser.runtime.onMessage.addListener(async msg => {
browser.test.assertEq(msg, "from-panel", "correct message received"); browser.test.assertEq(msg, "from-panel", "correct message received");
@ -68,6 +73,7 @@ add_task(async function test_openPopup_requires_user_interaction() {
<button id="openPageAction">openPageAction</button> <button id="openPageAction">openPageAction</button>
<button id="openSidebarAction">openSidebarAction</button> <button id="openSidebarAction">openSidebarAction</button>
<button id="closeSidebarAction">closeSidebarAction</button> <button id="closeSidebarAction">closeSidebarAction</button>
<button id="toggleSidebarAction">toggleSidebarAction</button>
<script src="tab.js"></script> <script src="tab.js"></script>
</body></html> </body></html>
`, `,
@ -106,6 +112,13 @@ add_task(async function test_openPopup_requires_user_interaction() {
}, },
{ once: true } { once: true }
); );
/* eslint-disable mozilla/balanced-listeners */
document
.getElementById("toggleSidebarAction")
.addEventListener("click", () => {
browser.sidebarAction.toggle();
});
/* eslint-enable mozilla/balanced-listeners */
}, },
"panel.js": function() { "panel.js": function() {
browser.runtime.sendMessage("from-panel"); browser.runtime.sendMessage("from-panel");
@ -155,6 +168,15 @@ add_task(async function test_openPopup_requires_user_interaction() {
); );
await BrowserTestUtils.waitForCondition(() => !SidebarUI.isOpen); await BrowserTestUtils.waitForCondition(() => !SidebarUI.isOpen);
await click("#toggleSidebarAction");
await BrowserTestUtils.waitForCondition(() => SidebarUI.isOpen);
await BrowserTestUtils.synthesizeMouseAtCenter(
"#toggleSidebarAction",
{},
gBrowser.selectedBrowser
);
await BrowserTestUtils.waitForCondition(() => !SidebarUI.isOpen);
BrowserTestUtils.removeTab(gBrowser.selectedTab); BrowserTestUtils.removeTab(gBrowser.selectedTab);
await extension.unload(); await extension.unload();