michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: "use strict"; michael@0: michael@0: let openUILinkInCalled = false; michael@0: let expectOpenUILinkInCall = false; michael@0: this.originalOpenUILinkIn = openUILinkIn; michael@0: openUILinkIn = (aUrl, aWhichTab) => { michael@0: is(aUrl, "about:home", "about:home should be requested to open."); michael@0: is(aWhichTab, "current", "Should use the current tab for the search page."); michael@0: openUILinkInCalled = true; michael@0: if (!expectOpenUILinkInCall) { michael@0: ok(false, "OpenUILinkIn was called when it shouldn't have been."); michael@0: } michael@0: }; michael@0: logActiveElement(); michael@0: michael@0: function* waitForSearchBarFocus() michael@0: { michael@0: let searchbar = document.getElementById("searchbar"); michael@0: yield waitForCondition(function () { michael@0: logActiveElement(); michael@0: return document.activeElement === searchbar.textbox.inputField; michael@0: }); michael@0: } michael@0: michael@0: // Ctrl+K should open the menu panel and focus the search bar if the search bar is in the panel. michael@0: add_task(function() { michael@0: let searchbar = document.getElementById("searchbar"); michael@0: gCustomizeMode.addToPanel(searchbar); michael@0: let placement = CustomizableUI.getPlacementOfWidget("search-container"); michael@0: is(placement.area, CustomizableUI.AREA_PANEL, "Should be in panel"); michael@0: michael@0: let shownPanelPromise = promisePanelShown(window); michael@0: sendWebSearchKeyCommand(); michael@0: yield shownPanelPromise; michael@0: michael@0: yield waitForSearchBarFocus(); michael@0: michael@0: let hiddenPanelPromise = promisePanelHidden(window); michael@0: EventUtils.synthesizeKey("VK_ESCAPE", {}); michael@0: yield hiddenPanelPromise; michael@0: CustomizableUI.reset(); michael@0: }); michael@0: michael@0: // Ctrl+K should give focus to the searchbar when the searchbar is in the menupanel and the panel is already opened. michael@0: add_task(function() { michael@0: let searchbar = document.getElementById("searchbar"); michael@0: gCustomizeMode.addToPanel(searchbar); michael@0: let placement = CustomizableUI.getPlacementOfWidget("search-container"); michael@0: is(placement.area, CustomizableUI.AREA_PANEL, "Should be in panel"); michael@0: michael@0: let shownPanelPromise = promisePanelShown(window); michael@0: PanelUI.toggle({type: "command"}); michael@0: yield shownPanelPromise; michael@0: michael@0: sendWebSearchKeyCommand(); michael@0: michael@0: yield waitForSearchBarFocus(); michael@0: michael@0: let hiddenPanelPromise = promisePanelHidden(window); michael@0: EventUtils.synthesizeKey("VK_ESCAPE", {}); michael@0: yield hiddenPanelPromise; michael@0: CustomizableUI.reset(); michael@0: }); michael@0: michael@0: // Ctrl+K should open the overflow panel and focus the search bar if the search bar is overflowed. michael@0: add_task(function() { michael@0: this.originalWindowWidth = window.outerWidth; michael@0: let navbar = document.getElementById(CustomizableUI.AREA_NAVBAR); michael@0: ok(!navbar.hasAttribute("overflowing"), "Should start with a non-overflowing toolbar."); michael@0: ok(CustomizableUI.inDefaultState, "Should start in default state."); michael@0: michael@0: window.resizeTo(360, window.outerHeight); michael@0: yield waitForCondition(() => navbar.getAttribute("overflowing") == "true"); michael@0: ok(!navbar.querySelector("#search-container"), "Search container should be overflowing"); michael@0: michael@0: let shownPanelPromise = promiseOverflowShown(window); michael@0: sendWebSearchKeyCommand(); michael@0: yield shownPanelPromise; michael@0: michael@0: let chevron = document.getElementById("nav-bar-overflow-button"); michael@0: yield waitForCondition(function() chevron.open); michael@0: michael@0: yield waitForSearchBarFocus(); michael@0: michael@0: let hiddenPanelPromise = promiseOverflowHidden(window); michael@0: EventUtils.synthesizeKey("VK_ESCAPE", {}); michael@0: yield hiddenPanelPromise; michael@0: let navbar = document.getElementById(CustomizableUI.AREA_NAVBAR); michael@0: window.resizeTo(this.originalWindowWidth, window.outerHeight); michael@0: yield waitForCondition(() => !navbar.hasAttribute("overflowing")); michael@0: ok(!navbar.hasAttribute("overflowing"), "Should not have an overflowing toolbar."); michael@0: }); michael@0: michael@0: // Ctrl+K should focus the search bar if it is in the navbar and not overflowing. michael@0: add_task(function() { michael@0: let placement = CustomizableUI.getPlacementOfWidget("search-container"); michael@0: is(placement.area, CustomizableUI.AREA_NAVBAR, "Should be in nav-bar"); michael@0: michael@0: sendWebSearchKeyCommand(); michael@0: michael@0: yield waitForSearchBarFocus(); michael@0: }); michael@0: michael@0: // Ctrl+K should open the search page if the search bar has been customized out. michael@0: add_task(function() { michael@0: try { michael@0: expectOpenUILinkInCall = true; michael@0: CustomizableUI.removeWidgetFromArea("search-container"); michael@0: let placement = CustomizableUI.getPlacementOfWidget("search-container"); michael@0: is(placement, null, "Search container should be in palette"); michael@0: michael@0: openUILinkInCalled = false; michael@0: michael@0: sendWebSearchKeyCommand(); michael@0: yield waitForCondition(function() openUILinkInCalled); michael@0: ok(openUILinkInCalled, "The search page should have been opened.") michael@0: expectOpenUILinkInCall = false; michael@0: } catch (e) { michael@0: ok(false, e); michael@0: } michael@0: CustomizableUI.reset(); michael@0: }); michael@0: michael@0: registerCleanupFunction(function() { michael@0: openUILinkIn = this.originalOpenUILinkIn; michael@0: delete this.originalOpenUILinkIn; michael@0: }); michael@0: michael@0: function sendWebSearchKeyCommand() { michael@0: if (Services.appinfo.OS === "Darwin") michael@0: EventUtils.synthesizeKey("k", { accelKey: true }); michael@0: else michael@0: EventUtils.synthesizeKey("k", { ctrlKey: true }); michael@0: } michael@0: michael@0: function logActiveElement() { michael@0: let element = document.activeElement; michael@0: let str = ""; michael@0: while (element && element.parentNode) { michael@0: str = " (" + element.localName + "#" + element.id + "." + [...element.classList].join(".") + ") >" + str; michael@0: element = element.parentNode; michael@0: } michael@0: info("Active element: " + element ? str : "null"); michael@0: }