1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/browser/components/customizableui/test/browser_901207_searchbar_in_panel.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,148 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +"use strict"; 1.9 + 1.10 +let openUILinkInCalled = false; 1.11 +let expectOpenUILinkInCall = false; 1.12 +this.originalOpenUILinkIn = openUILinkIn; 1.13 +openUILinkIn = (aUrl, aWhichTab) => { 1.14 + is(aUrl, "about:home", "about:home should be requested to open."); 1.15 + is(aWhichTab, "current", "Should use the current tab for the search page."); 1.16 + openUILinkInCalled = true; 1.17 + if (!expectOpenUILinkInCall) { 1.18 + ok(false, "OpenUILinkIn was called when it shouldn't have been."); 1.19 + } 1.20 +}; 1.21 +logActiveElement(); 1.22 + 1.23 +function* waitForSearchBarFocus() 1.24 +{ 1.25 + let searchbar = document.getElementById("searchbar"); 1.26 + yield waitForCondition(function () { 1.27 + logActiveElement(); 1.28 + return document.activeElement === searchbar.textbox.inputField; 1.29 + }); 1.30 +} 1.31 + 1.32 +// Ctrl+K should open the menu panel and focus the search bar if the search bar is in the panel. 1.33 +add_task(function() { 1.34 + let searchbar = document.getElementById("searchbar"); 1.35 + gCustomizeMode.addToPanel(searchbar); 1.36 + let placement = CustomizableUI.getPlacementOfWidget("search-container"); 1.37 + is(placement.area, CustomizableUI.AREA_PANEL, "Should be in panel"); 1.38 + 1.39 + let shownPanelPromise = promisePanelShown(window); 1.40 + sendWebSearchKeyCommand(); 1.41 + yield shownPanelPromise; 1.42 + 1.43 + yield waitForSearchBarFocus(); 1.44 + 1.45 + let hiddenPanelPromise = promisePanelHidden(window); 1.46 + EventUtils.synthesizeKey("VK_ESCAPE", {}); 1.47 + yield hiddenPanelPromise; 1.48 + CustomizableUI.reset(); 1.49 +}); 1.50 + 1.51 +// Ctrl+K should give focus to the searchbar when the searchbar is in the menupanel and the panel is already opened. 1.52 +add_task(function() { 1.53 + let searchbar = document.getElementById("searchbar"); 1.54 + gCustomizeMode.addToPanel(searchbar); 1.55 + let placement = CustomizableUI.getPlacementOfWidget("search-container"); 1.56 + is(placement.area, CustomizableUI.AREA_PANEL, "Should be in panel"); 1.57 + 1.58 + let shownPanelPromise = promisePanelShown(window); 1.59 + PanelUI.toggle({type: "command"}); 1.60 + yield shownPanelPromise; 1.61 + 1.62 + sendWebSearchKeyCommand(); 1.63 + 1.64 + yield waitForSearchBarFocus(); 1.65 + 1.66 + let hiddenPanelPromise = promisePanelHidden(window); 1.67 + EventUtils.synthesizeKey("VK_ESCAPE", {}); 1.68 + yield hiddenPanelPromise; 1.69 + CustomizableUI.reset(); 1.70 +}); 1.71 + 1.72 +// Ctrl+K should open the overflow panel and focus the search bar if the search bar is overflowed. 1.73 +add_task(function() { 1.74 + this.originalWindowWidth = window.outerWidth; 1.75 + let navbar = document.getElementById(CustomizableUI.AREA_NAVBAR); 1.76 + ok(!navbar.hasAttribute("overflowing"), "Should start with a non-overflowing toolbar."); 1.77 + ok(CustomizableUI.inDefaultState, "Should start in default state."); 1.78 + 1.79 + window.resizeTo(360, window.outerHeight); 1.80 + yield waitForCondition(() => navbar.getAttribute("overflowing") == "true"); 1.81 + ok(!navbar.querySelector("#search-container"), "Search container should be overflowing"); 1.82 + 1.83 + let shownPanelPromise = promiseOverflowShown(window); 1.84 + sendWebSearchKeyCommand(); 1.85 + yield shownPanelPromise; 1.86 + 1.87 + let chevron = document.getElementById("nav-bar-overflow-button"); 1.88 + yield waitForCondition(function() chevron.open); 1.89 + 1.90 + yield waitForSearchBarFocus(); 1.91 + 1.92 + let hiddenPanelPromise = promiseOverflowHidden(window); 1.93 + EventUtils.synthesizeKey("VK_ESCAPE", {}); 1.94 + yield hiddenPanelPromise; 1.95 + let navbar = document.getElementById(CustomizableUI.AREA_NAVBAR); 1.96 + window.resizeTo(this.originalWindowWidth, window.outerHeight); 1.97 + yield waitForCondition(() => !navbar.hasAttribute("overflowing")); 1.98 + ok(!navbar.hasAttribute("overflowing"), "Should not have an overflowing toolbar."); 1.99 +}); 1.100 + 1.101 +// Ctrl+K should focus the search bar if it is in the navbar and not overflowing. 1.102 +add_task(function() { 1.103 + let placement = CustomizableUI.getPlacementOfWidget("search-container"); 1.104 + is(placement.area, CustomizableUI.AREA_NAVBAR, "Should be in nav-bar"); 1.105 + 1.106 + sendWebSearchKeyCommand(); 1.107 + 1.108 + yield waitForSearchBarFocus(); 1.109 +}); 1.110 + 1.111 +// Ctrl+K should open the search page if the search bar has been customized out. 1.112 +add_task(function() { 1.113 + try { 1.114 + expectOpenUILinkInCall = true; 1.115 + CustomizableUI.removeWidgetFromArea("search-container"); 1.116 + let placement = CustomizableUI.getPlacementOfWidget("search-container"); 1.117 + is(placement, null, "Search container should be in palette"); 1.118 + 1.119 + openUILinkInCalled = false; 1.120 + 1.121 + sendWebSearchKeyCommand(); 1.122 + yield waitForCondition(function() openUILinkInCalled); 1.123 + ok(openUILinkInCalled, "The search page should have been opened.") 1.124 + expectOpenUILinkInCall = false; 1.125 + } catch (e) { 1.126 + ok(false, e); 1.127 + } 1.128 + CustomizableUI.reset(); 1.129 +}); 1.130 + 1.131 +registerCleanupFunction(function() { 1.132 + openUILinkIn = this.originalOpenUILinkIn; 1.133 + delete this.originalOpenUILinkIn; 1.134 +}); 1.135 + 1.136 +function sendWebSearchKeyCommand() { 1.137 + if (Services.appinfo.OS === "Darwin") 1.138 + EventUtils.synthesizeKey("k", { accelKey: true }); 1.139 + else 1.140 + EventUtils.synthesizeKey("k", { ctrlKey: true }); 1.141 +} 1.142 + 1.143 +function logActiveElement() { 1.144 + let element = document.activeElement; 1.145 + let str = ""; 1.146 + while (element && element.parentNode) { 1.147 + str = " (" + element.localName + "#" + element.id + "." + [...element.classList].join(".") + ") >" + str; 1.148 + element = element.parentNode; 1.149 + } 1.150 + info("Active element: " + element ? str : "null"); 1.151 +}