From e679bb60687be3d99a00cc4d0609827b790c5566 Mon Sep 17 00:00:00 2001 From: Matthew Noorenberghe Date: Tue, 12 Nov 2013 19:19:09 -0800 Subject: [PATCH] Bug 936378 - Fix PanelUI.show() to not require an event. r=Unfocused --- .../customizableui/content/panelUI.js | 4 +- .../customizableui/test/browser.ini | 1 + .../browser_876926_customize_mode_wrapping.js | 26 +++------ .../test/browser_panel_toggle.js | 53 +++++++++++++++++++ .../components/customizableui/test/head.js | 22 ++++++++ 5 files changed, 85 insertions(+), 21 deletions(-) create mode 100644 browser/components/customizableui/test/browser_panel_toggle.js diff --git a/browser/components/customizableui/content/panelUI.js b/browser/components/customizableui/content/panelUI.js index c45c8f7e81d2..2071414eb3ea 100644 --- a/browser/components/customizableui/content/panelUI.js +++ b/browser/components/customizableui/content/panelUI.js @@ -129,7 +129,7 @@ const PanelUI = { } let anchor; - if (aEvent.type == "mousedown" || + if (!aEvent || aEvent.type == "command") { anchor = this.menuButton; } else { @@ -141,7 +141,7 @@ const PanelUI = { // Only focus the panel if it's opened using the keyboard, so that // cut/copy/paste buttons will work for mouse users. - let keyboardOpened = aEvent.sourceEvent && + let keyboardOpened = aEvent && aEvent.sourceEvent && aEvent.sourceEvent.target.localName == "key"; this.panel.setAttribute("noautofocus", !keyboardOpened); this.panel.openPopup(iconAnchor || anchor, "bottomcenter topright"); diff --git a/browser/components/customizableui/test/browser.ini b/browser/components/customizableui/test/browser.ini index ff9aafce5af7..63631d8908da 100644 --- a/browser/components/customizableui/test/browser.ini +++ b/browser/components/customizableui/test/browser.ini @@ -34,3 +34,4 @@ run-if = os == "mac" # Because this test is about the menubar, it can't be run on mac skip-if = os == "mac" +[browser_panel_toggle.js] diff --git a/browser/components/customizableui/test/browser_876926_customize_mode_wrapping.js b/browser/components/customizableui/test/browser_876926_customize_mode_wrapping.js index 4582a4ff4d53..9d4d4289e90f 100644 --- a/browser/components/customizableui/test/browser_876926_customize_mode_wrapping.js +++ b/browser/components/customizableui/test/browser_876926_customize_mode_wrapping.js @@ -114,8 +114,13 @@ let gTests = [ setup: startCustomizing, run: function() { otherWin = yield openAndLoadWindow(null, true); - // Open panel to force its construction: - yield afterPanelOpen(otherWin); + // Open and close the panel to force its construction: + let shownPromise = promisePanelShown(otherWin); + otherWin.PanelUI.toggle({type: "command"}); + yield shownPromise; + let hiddenPromise = promisePanelHidden(otherWin); + otherWin.PanelUI.toggle({type: "command"}); + yield hiddenPromise; ok(CustomizableUI.inDefaultState, "Should start in default state"); @@ -143,23 +148,6 @@ let gTests = [ } ]; -function afterPanelOpen(win) { - let panelEl = win.PanelUI.panel; - let deferred = Promise.defer(); - function onPanelClose(e) { - panelEl.removeEventListener("popuphidden", onPanelClose); - deferred.resolve(); - } - function onPanelOpen(e) { - panelEl.removeEventListener("popupshown", onPanelOpen); - panelEl.addEventListener("popuphidden", onPanelClose); - win.PanelUI.toggle({type: "command"}); - }; - panelEl.addEventListener("popupshown", onPanelOpen); - win.PanelUI.toggle({type: "command"}); - return deferred.promise; -} - function asyncCleanup() { Services.prefs.clearUserPref("browser.uiCustomization.skipSourceNodeCheck"); yield resetCustomization(); diff --git a/browser/components/customizableui/test/browser_panel_toggle.js b/browser/components/customizableui/test/browser_panel_toggle.js new file mode 100644 index 000000000000..2e6ef6ce84b4 --- /dev/null +++ b/browser/components/customizableui/test/browser_panel_toggle.js @@ -0,0 +1,53 @@ +/* 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/. */ + +/** + * Test opening and closing the menu panel UI. + */ + +let gTests = [ + { + desc: "Show and hide the menu panel programmatically without an event (like UITour.jsm would)", + setup: null, + run: function() { + let shownPromise = promisePanelShown(window); + PanelUI.show(); + yield shownPromise; + + is(PanelUI.panel.getAttribute("panelopen"), "true", "Check that panel has panelopen attribute"); + is(PanelUI.panel.state, "open", "Check that panel state is 'open'"); + + let hiddenPromise = promisePanelHidden(window); + PanelUI.hide(); + yield hiddenPromise; + + ok(!PanelUI.panel.hasAttribute("panelopen"), "Check that panel doesn't have the panelopen attribute"); + is(PanelUI.panel.state, "closed", "Check that panel state is 'closed'"); + }, + }, + { + desc: "Toggle the menu panel open and closed", + setup: null, + run: function() { + let shownPromise = promisePanelShown(window); + PanelUI.toggle({type: "command"}); + yield shownPromise; + + is(PanelUI.panel.getAttribute("panelopen"), "true", "Check that panel has panelopen attribute"); + is(PanelUI.panel.state, "open", "Check that panel state is 'open'"); + + let hiddenPromise = promisePanelHidden(window); + PanelUI.toggle({type: "command"}); + yield hiddenPromise; + + ok(!PanelUI.panel.hasAttribute("panelopen"), "Check that panel doesn't have the panelopen attribute"); + is(PanelUI.panel.state, "closed", "Check that panel state is 'closed'"); + }, + }, +]; + +function test() { + waitForExplicitFinish(); + runTests(gTests); +} diff --git a/browser/components/customizableui/test/head.js b/browser/components/customizableui/test/head.js index cbde7d15ad49..3e92df81e387 100644 --- a/browser/components/customizableui/test/head.js +++ b/browser/components/customizableui/test/head.js @@ -174,6 +174,28 @@ function openAndLoadWindow(aOptions, aWaitForDelayedStartup=false) { return deferred.promise; } +function promisePanelShown(win) { + let panelEl = win.PanelUI.panel; + let deferred = Promise.defer(); + function onPanelOpen(e) { + panelEl.removeEventListener("popupshown", onPanelOpen); + deferred.resolve(); + }; + panelEl.addEventListener("popupshown", onPanelOpen); + return deferred.promise; +} + +function promisePanelHidden(win) { + let panelEl = win.PanelUI.panel; + let deferred = Promise.defer(); + function onPanelClose(e) { + panelEl.removeEventListener("popuphidden", onPanelClose); + deferred.resolve(); + } + panelEl.addEventListener("popuphidden", onPanelClose); + return deferred.promise; +} + function waitForCondition(aConditionFn, aMaxTries=50, aCheckInterval=100) { function tryNow() { tries++;