--- a/browser/base/content/browser-sidebar.js
+++ b/browser/base/content/browser-sidebar.js
@@ -23,16 +23,21 @@ var SidebarUI = {
// Avoid getting the browser element from init() to avoid triggering the
// <browser> constructor during startup if the sidebar is hidden.
get browser() {
if (this._browser)
return this._browser;
return this._browser = document.getElementById("sidebar");
},
POSITION_START_PREF: "sidebar.position_start",
+ DEFAULT_SIDEBAR_ID: "viewBookmarksSidebar",
+
+ // lastOpenedId is set in show() but unlike currentID it's not cleared out on hide
+ // and isn't persisted across windows
+ lastOpenedId: null,
_box: null,
// The constructor of this label accesses the browser element due to the
// control="sidebar" attribute, so avoid getting this label during startup.
get _title() {
if (this.__title)
return this.__title;
return this.__title = document.getElementById("sidebar-title");
@@ -58,16 +63,17 @@ var SidebarUI = {
});
},
uninit() {
let enumerator = Services.wm.getEnumerator(null);
enumerator.getNext();
if (!enumerator.hasMoreElements()) {
document.persist("sidebar-box", "sidebarcommand");
+ document.persist("sidebar-box", "checked");
document.persist("sidebar-box", "width");
document.persist("sidebar-title", "value");
}
},
/**
* Opens the switcher panel if it's closed, or closes it if it's open.
*/
@@ -252,25 +258,40 @@ var SidebarUI = {
get title() {
return this._title.value;
},
set title(value) {
this._title.value = value;
},
+ getBroadcasterById(id) {
+ let sidebarBroadcaster = document.getElementById(id);
+ if (sidebarBroadcaster && sidebarBroadcaster.localName == "broadcaster") {
+ return sidebarBroadcaster;
+ }
+ return null;
+ },
+
/**
* Toggle the visibility of the sidebar. If the sidebar is hidden or is open
* with a different commandID, then the sidebar will be opened using the
* specified commandID. Otherwise the sidebar will be hidden.
*
* @param {string} commandID ID of the xul:broadcaster element to use.
* @return {Promise}
*/
- toggle(commandID = this.currentID) {
+ toggle(commandID = this.lastOpenedId) {
+ // First priority for a default value is this.lastOpenedId which is set during show()
+ // and not reset in hide(), unlike currentID. If show() hasn't been called or the command
+ // doesn't exist anymore, then fallback to a default sidebar.
+ if (!commandID || !this.getBroadcasterById(commandID)) {
+ commandID = this.DEFAULT_SIDEBAR_ID;
+ }
+
if (this.isOpen && commandID == this.currentID) {
this.hide();
return Promise.resolve();
}
return this.show(commandID);
},
/**
@@ -290,18 +311,18 @@ var SidebarUI = {
/**
* Implementation for show. Also used internally for sidebars that are shown
* when a window is opened and we don't want to ping telemetry.
*
* @param {string} commandID ID of the xul:broadcaster element to use.
*/
_show(commandID) {
return new Promise((resolve, reject) => {
- let sidebarBroadcaster = document.getElementById(commandID);
- if (!sidebarBroadcaster || sidebarBroadcaster.localName != "broadcaster") {
+ let sidebarBroadcaster = this.getBroadcasterById(commandID);
+ if (!sidebarBroadcaster) {
reject(new Error("Invalid sidebar broadcaster specified: " + commandID));
return;
}
if (this.isOpen && commandID != this.currentID) {
BrowserUITelemetry.countSidebarEvent(this.currentID, "hide");
}
@@ -314,16 +335,17 @@ var SidebarUI = {
}
}
this._box.hidden = this._splitter.hidden = false;
this.setPosition();
this.hideSwitcherPanel();
+ this._box.setAttribute("checked", "true");
this._box.setAttribute("sidebarcommand", sidebarBroadcaster.id);
this.lastOpenedId = sidebarBroadcaster.id;
let title = sidebarBroadcaster.getAttribute("sidebartitle") ||
sidebarBroadcaster.getAttribute("label");
// When loading a web page in the sidebar there is no title set on the
// broadcaster, as it is instead set by openWebPanel. Don't clear out
@@ -383,16 +405,17 @@ var SidebarUI = {
// create a new content viewer because the old one doesn't get destroyed
// until about:blank has loaded (which does not happen as long as the
// element is hidden).
this.browser.setAttribute("src", "about:blank");
this.browser.docShell.createAboutBlankContentViewer(null);
sidebarBroadcaster.removeAttribute("checked");
this._box.setAttribute("sidebarcommand", "");
+ this._box.removeAttribute("checked");
this.title = "";
this._box.hidden = this._splitter.hidden = true;
let selBrowser = gBrowser.selectedBrowser;
selBrowser.focus();
selBrowser.messageManager.sendAsyncMessage("Sidebar:VisibilityChange",
{commandID, isOpen: false}
);
rename from browser/components/customizableui/test/browser_988072_sidebar_events.js
rename to browser/components/customizableui/test/browser_sidebar_toggle.js
--- a/browser/components/customizableui/test/browser_988072_sidebar_events.js
+++ b/browser/components/customizableui/test/browser_sidebar_toggle.js
@@ -1,392 +1,43 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
"use strict";
-var gSidebarMenu = document.getElementById("viewSidebarMenu");
-var gTestSidebarItem = null;
-
-var EVENTS = {
- click: 0, command: 0,
- onclick: 0, oncommand: 0
-};
-
-window.sawEvent = function(event, isattr) {
- let type = (isattr ? "on" : "") + event.type
- EVENTS[type]++;
-};
-
-registerCleanupFunction(() => {
- delete window.sawEvent;
+registerCleanupFunction(async function() {
+ await resetCustomization();
// Ensure sidebar is hidden after each test:
if (!document.getElementById("sidebar-box").hidden) {
SidebarUI.hide();
}
});
-function checkExpectedEvents(expected) {
- for (let type of Object.keys(EVENTS)) {
- let count = (type in expected ? expected[type] : 0);
- is(EVENTS[type], count, "Should have seen the right number of " + type + " events");
- EVENTS[type] = 0;
- }
-}
-
-function createSidebarItem() {
- gTestSidebarItem = document.createElement("menuitem");
- gTestSidebarItem.id = "testsidebar";
- gTestSidebarItem.setAttribute("label", "Test Sidebar");
- gSidebarMenu.insertBefore(gTestSidebarItem, gSidebarMenu.firstChild);
-}
-
-function addWidget() {
- CustomizableUI.addWidgetToArea("sidebar-button", "nav-bar");
- PanelUI.disableSingleSubviewPanelAnimations();
-}
-
-function removeWidget() {
- CustomizableUI.removeWidgetFromArea("sidebar-button");
- PanelUI.enableSingleSubviewPanelAnimations();
-}
+var showSidebar = async function() {
+ let button = document.getElementById("sidebar-button");
+ let sidebarFocusedPromise = BrowserTestUtils.waitForEvent(document, "SidebarFocused");
+ EventUtils.synthesizeMouseAtCenter(button, {});
+ await sidebarFocusedPromise;
+ ok(SidebarUI.isOpen, "Sidebar is opened");
+ ok(button.hasAttribute("checked"), "Toolbar button is checked");
+};
-// Filters out the trailing menuseparators from the sidebar list
-function getSidebarList() {
- let sidebars = [...gSidebarMenu.children].filter(sidebar => {
- if (sidebar.localName == "menuseparator")
- return false;
- if (sidebar.getAttribute("hidden") == "true")
- return false;
- return true;
- });
- return sidebars;
-}
-
-function compareElements(original, displayed) {
- let attrs = ["label", "key", "disabled", "hidden", "origin", "image", "checked"];
- for (let attr of attrs) {
- is(displayed.getAttribute(attr), original.getAttribute(attr), "Should have the same " + attr + " attribute");
- }
-}
-
-function compareList(original, displayed) {
- is(displayed.length, original.length, "Should have the same number of children");
-
- for (let i = 0; i < Math.min(original.length, displayed.length); i++) {
- compareElements(displayed[i], original[i]);
- }
-}
-
-var showSidebarPopup = async function() {
+var hideSidebar = async function() {
let button = document.getElementById("sidebar-button");
- let subview = document.getElementById("PanelUI-sidebar");
-
- let popupShownPromise = BrowserTestUtils.waitForEvent(document, "popupshown");
-
- let subviewShownPromise = subviewShown(subview);
EventUtils.synthesizeMouseAtCenter(button, {});
- return Promise.all([subviewShownPromise, popupShownPromise]);
+ ok(!SidebarUI.isOpen, "Sidebar is closed");
+ ok(!button.hasAttribute("checked"), "Toolbar button isn't checked");
};
// Check the sidebar widget shows the default items
add_task(async function() {
- addWidget();
-
- await showSidebarPopup();
-
- let sidebars = getSidebarList();
- let displayed = [...document.getElementById("PanelUI-sidebarItems").children];
- compareList(sidebars, displayed);
-
- let subview = document.getElementById("PanelUI-sidebar");
- let subviewHiddenPromise = subviewHidden(subview);
- document.getElementById("customizationui-widget-panel").hidePopup();
- await subviewHiddenPromise;
-
- removeWidget();
-});
-
-function add_sidebar_task(description, setup, teardown) {
- add_task(async function() {
- info(description);
- createSidebarItem();
- addWidget();
- await setup();
-
- CustomizableUI.addWidgetToArea("sidebar-button", "nav-bar");
-
- await showSidebarPopup();
-
- let sidebars = getSidebarList();
- let displayed = [...document.getElementById("PanelUI-sidebarItems").children];
- compareList(sidebars, displayed);
-
- is(displayed[0].label, "Test Sidebar", "Should have the right element at the top");
- let subview = document.getElementById("PanelUI-sidebar");
- let subviewHiddenPromise = subviewHidden(subview);
- EventUtils.synthesizeMouseAtCenter(displayed[0], {});
- await subviewHiddenPromise;
-
- await teardown();
- gTestSidebarItem.remove();
- removeWidget();
- });
-}
-
-add_sidebar_task(
- "Check that a sidebar that uses a command event listener works",
-function() {
- gTestSidebarItem.addEventListener("command", window.sawEvent);
-}, function() {
- checkExpectedEvents({ command: 1 });
-});
-
-add_sidebar_task(
- "Check that a sidebar that uses a click event listener works",
-function() {
- gTestSidebarItem.addEventListener("click", window.sawEvent);
-}, function() {
- checkExpectedEvents({ click: 1 });
-});
-
-add_sidebar_task(
- "Check that a sidebar that uses both click and command event listeners works",
-function() {
- gTestSidebarItem.addEventListener("command", window.sawEvent);
- gTestSidebarItem.addEventListener("click", window.sawEvent);
-}, function() {
- checkExpectedEvents({ command: 1, click: 1 });
-});
-
-add_sidebar_task(
- "Check that a sidebar that uses an oncommand attribute works",
-function() {
- gTestSidebarItem.setAttribute("oncommand", "window.sawEvent(event, true)");
-}, function() {
- checkExpectedEvents({ oncommand: 1 });
-});
-
-add_sidebar_task(
- "Check that a sidebar that uses an onclick attribute works",
-function() {
- gTestSidebarItem.setAttribute("onclick", "window.sawEvent(event, true)");
-}, function() {
- checkExpectedEvents({ onclick: 1 });
-});
-
-add_sidebar_task(
- "Check that a sidebar that uses both onclick and oncommand attributes works",
-function() {
- gTestSidebarItem.setAttribute("onclick", "window.sawEvent(event, true)");
- gTestSidebarItem.setAttribute("oncommand", "window.sawEvent(event, true)");
-}, function() {
- checkExpectedEvents({ onclick: 1, oncommand: 1 });
-});
-
-add_sidebar_task(
- "Check that a sidebar that uses an onclick attribute and a command listener works",
-function() {
- gTestSidebarItem.setAttribute("onclick", "window.sawEvent(event, true)");
- gTestSidebarItem.addEventListener("command", window.sawEvent);
-}, function() {
- checkExpectedEvents({ onclick: 1, command: 1 });
-});
-
-add_sidebar_task(
- "Check that a sidebar that uses an oncommand attribute and a click listener works",
-function() {
- gTestSidebarItem.setAttribute("oncommand", "window.sawEvent(event, true)");
- gTestSidebarItem.addEventListener("click", window.sawEvent);
-}, function() {
- checkExpectedEvents({ click: 1, oncommand: 1 });
-});
-
-add_sidebar_task(
- "A sidebar with both onclick attribute and click listener sees only one event :(",
-function() {
- gTestSidebarItem.setAttribute("onclick", "window.sawEvent(event, true)");
- gTestSidebarItem.addEventListener("click", window.sawEvent);
-}, function() {
- checkExpectedEvents({ onclick: 1 });
-});
-
-add_sidebar_task(
- "A sidebar with both oncommand attribute and command listener sees only one event :(",
-function() {
- gTestSidebarItem.setAttribute("oncommand", "window.sawEvent(event, true)");
- gTestSidebarItem.addEventListener("command", window.sawEvent);
-}, function() {
- checkExpectedEvents({ oncommand: 1 });
-});
-
-add_sidebar_task(
- "Check that a sidebar that uses a broadcaster with an oncommand attribute works",
-function() {
- let broadcaster = document.createElement("broadcaster");
- broadcaster.setAttribute("id", "testbroadcaster");
- broadcaster.setAttribute("oncommand", "window.sawEvent(event, true)");
- broadcaster.setAttribute("label", "Test Sidebar");
- document.getElementById("mainBroadcasterSet").appendChild(broadcaster);
-
- gTestSidebarItem.setAttribute("observes", "testbroadcaster");
-}, function() {
- checkExpectedEvents({ oncommand: 1 });
- document.getElementById("testbroadcaster").remove();
-});
+ CustomizableUI.addWidgetToArea("sidebar-button", "nav-bar");
-add_sidebar_task(
- "Check that a sidebar that uses a broadcaster with an onclick attribute works",
-function() {
- let broadcaster = document.createElement("broadcaster");
- broadcaster.setAttribute("id", "testbroadcaster");
- broadcaster.setAttribute("onclick", "window.sawEvent(event, true)");
- broadcaster.setAttribute("label", "Test Sidebar");
- document.getElementById("mainBroadcasterSet").appendChild(broadcaster);
-
- gTestSidebarItem.setAttribute("observes", "testbroadcaster");
-}, function() {
- checkExpectedEvents({ onclick: 1 });
- document.getElementById("testbroadcaster").remove();
-});
-
-add_sidebar_task(
- "Check that a sidebar that uses a broadcaster with both onclick and oncommand attributes works",
-function() {
- let broadcaster = document.createElement("broadcaster");
- broadcaster.setAttribute("id", "testbroadcaster");
- broadcaster.setAttribute("onclick", "window.sawEvent(event, true)");
- broadcaster.setAttribute("oncommand", "window.sawEvent(event, true)");
- broadcaster.setAttribute("label", "Test Sidebar");
- document.getElementById("mainBroadcasterSet").appendChild(broadcaster);
-
- gTestSidebarItem.setAttribute("observes", "testbroadcaster");
-}, function() {
- checkExpectedEvents({ onclick: 1, oncommand: 1 });
- document.getElementById("testbroadcaster").remove();
-});
-
-add_sidebar_task(
- "Check that a sidebar with a click listener and a broadcaster with an oncommand attribute works",
-function() {
- let broadcaster = document.createElement("broadcaster");
- broadcaster.setAttribute("id", "testbroadcaster");
- broadcaster.setAttribute("oncommand", "window.sawEvent(event, true)");
- broadcaster.setAttribute("label", "Test Sidebar");
- document.getElementById("mainBroadcasterSet").appendChild(broadcaster);
-
- gTestSidebarItem.setAttribute("observes", "testbroadcaster");
- gTestSidebarItem.addEventListener("click", window.sawEvent);
-}, function() {
- checkExpectedEvents({ click: 1, oncommand: 1 });
- document.getElementById("testbroadcaster").remove();
-});
-
-add_sidebar_task(
- "Check that a sidebar with a command listener and a broadcaster with an onclick attribute works",
-function() {
- let broadcaster = document.createElement("broadcaster");
- broadcaster.setAttribute("id", "testbroadcaster");
- broadcaster.setAttribute("onclick", "window.sawEvent(event, true)");
- broadcaster.setAttribute("label", "Test Sidebar");
- document.getElementById("mainBroadcasterSet").appendChild(broadcaster);
-
- gTestSidebarItem.setAttribute("observes", "testbroadcaster");
- gTestSidebarItem.addEventListener("command", window.sawEvent);
-}, function() {
- checkExpectedEvents({ onclick: 1, command: 1 });
- document.getElementById("testbroadcaster").remove();
-});
-
-add_sidebar_task(
- "Check that a sidebar with a click listener and a broadcaster with an onclick " +
- "attribute only sees one event :(",
-function() {
- let broadcaster = document.createElement("broadcaster");
- broadcaster.setAttribute("id", "testbroadcaster");
- broadcaster.setAttribute("onclick", "window.sawEvent(event, true)");
- broadcaster.setAttribute("label", "Test Sidebar");
- document.getElementById("mainBroadcasterSet").appendChild(broadcaster);
+ await showSidebar();
+ is(SidebarUI.currentID, "viewBookmarksSidebar", "Default sidebar selected");
+ await SidebarUI.show("viewHistorySidebar");
- gTestSidebarItem.setAttribute("observes", "testbroadcaster");
- gTestSidebarItem.addEventListener("click", window.sawEvent);
-}, function() {
- checkExpectedEvents({ onclick: 1 });
- document.getElementById("testbroadcaster").remove();
-});
-
-add_sidebar_task(
- "Check that a sidebar with a command listener and a broadcaster with an oncommand " +
- "attribute only sees one event :(",
-function() {
- let broadcaster = document.createElement("broadcaster");
- broadcaster.setAttribute("id", "testbroadcaster");
- broadcaster.setAttribute("oncommand", "window.sawEvent(event, true)");
- broadcaster.setAttribute("label", "Test Sidebar");
- document.getElementById("mainBroadcasterSet").appendChild(broadcaster);
-
- gTestSidebarItem.setAttribute("observes", "testbroadcaster");
- gTestSidebarItem.addEventListener("command", window.sawEvent);
-}, function() {
- checkExpectedEvents({ oncommand: 1 });
- document.getElementById("testbroadcaster").remove();
-});
-
-add_sidebar_task(
- "Check that a sidebar that uses a command element with a command event listener works",
-function() {
- let command = document.createElement("command");
- command.setAttribute("id", "testcommand");
- document.getElementById("mainCommandSet").appendChild(command);
- command.addEventListener("command", window.sawEvent);
-
- gTestSidebarItem.setAttribute("command", "testcommand");
-}, function() {
- checkExpectedEvents({ command: 1 });
- document.getElementById("testcommand").remove();
+ await hideSidebar();
+ await showSidebar();
+ is(SidebarUI.currentID, "viewHistorySidebar", "Selected sidebar remembered");
});
-
-add_sidebar_task(
- "Check that a sidebar that uses a command element with an oncommand attribute works",
-function() {
- let command = document.createElement("command");
- command.setAttribute("id", "testcommand");
- command.setAttribute("oncommand", "window.sawEvent(event, true)");
- document.getElementById("mainCommandSet").appendChild(command);
-
- gTestSidebarItem.setAttribute("command", "testcommand");
-}, function() {
- checkExpectedEvents({ oncommand: 1 });
- document.getElementById("testcommand").remove();
-});
-
-add_sidebar_task("Check that a sidebar that uses a command element with a " +
- "command event listener and oncommand attribute works",
-function() {
- let command = document.createElement("command");
- command.setAttribute("id", "testcommand");
- command.setAttribute("oncommand", "window.sawEvent(event, true)");
- document.getElementById("mainCommandSet").appendChild(command);
- command.addEventListener("command", window.sawEvent);
-
- gTestSidebarItem.setAttribute("command", "testcommand");
-}, function() {
- checkExpectedEvents({ command: 1, oncommand: 1 });
- document.getElementById("testcommand").remove();
-});
-
-add_sidebar_task(
- "A sidebar with a command element will still see click events",
-function() {
- let command = document.createElement("command");
- command.setAttribute("id", "testcommand");
- command.setAttribute("oncommand", "window.sawEvent(event, true)");
- document.getElementById("mainCommandSet").appendChild(command);
- command.addEventListener("command", window.sawEvent);
-
- gTestSidebarItem.setAttribute("command", "testcommand");
- gTestSidebarItem.addEventListener("click", window.sawEvent);
-}, function() {
- checkExpectedEvents({ click: 1, command: 1, oncommand: 1 });
- document.getElementById("testcommand").remove();
-});