Bug 936378 - Fix PanelUI.show() to not require an event. r=Unfocused

This commit is contained in:
Matthew Noorenberghe 2013-11-12 19:19:09 -08:00
Родитель 8a1e4738cc
Коммит e679bb6068
5 изменённых файлов: 85 добавлений и 21 удалений

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

@ -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");

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

@ -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]

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

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

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

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

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

@ -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++;