|
1 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
2 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
4 |
|
5 "use strict"; |
|
6 |
|
7 let openUILinkInCalled = false; |
|
8 let expectOpenUILinkInCall = false; |
|
9 this.originalOpenUILinkIn = openUILinkIn; |
|
10 openUILinkIn = (aUrl, aWhichTab) => { |
|
11 is(aUrl, "about:home", "about:home should be requested to open."); |
|
12 is(aWhichTab, "current", "Should use the current tab for the search page."); |
|
13 openUILinkInCalled = true; |
|
14 if (!expectOpenUILinkInCall) { |
|
15 ok(false, "OpenUILinkIn was called when it shouldn't have been."); |
|
16 } |
|
17 }; |
|
18 logActiveElement(); |
|
19 |
|
20 function* waitForSearchBarFocus() |
|
21 { |
|
22 let searchbar = document.getElementById("searchbar"); |
|
23 yield waitForCondition(function () { |
|
24 logActiveElement(); |
|
25 return document.activeElement === searchbar.textbox.inputField; |
|
26 }); |
|
27 } |
|
28 |
|
29 // Ctrl+K should open the menu panel and focus the search bar if the search bar is in the panel. |
|
30 add_task(function() { |
|
31 let searchbar = document.getElementById("searchbar"); |
|
32 gCustomizeMode.addToPanel(searchbar); |
|
33 let placement = CustomizableUI.getPlacementOfWidget("search-container"); |
|
34 is(placement.area, CustomizableUI.AREA_PANEL, "Should be in panel"); |
|
35 |
|
36 let shownPanelPromise = promisePanelShown(window); |
|
37 sendWebSearchKeyCommand(); |
|
38 yield shownPanelPromise; |
|
39 |
|
40 yield waitForSearchBarFocus(); |
|
41 |
|
42 let hiddenPanelPromise = promisePanelHidden(window); |
|
43 EventUtils.synthesizeKey("VK_ESCAPE", {}); |
|
44 yield hiddenPanelPromise; |
|
45 CustomizableUI.reset(); |
|
46 }); |
|
47 |
|
48 // Ctrl+K should give focus to the searchbar when the searchbar is in the menupanel and the panel is already opened. |
|
49 add_task(function() { |
|
50 let searchbar = document.getElementById("searchbar"); |
|
51 gCustomizeMode.addToPanel(searchbar); |
|
52 let placement = CustomizableUI.getPlacementOfWidget("search-container"); |
|
53 is(placement.area, CustomizableUI.AREA_PANEL, "Should be in panel"); |
|
54 |
|
55 let shownPanelPromise = promisePanelShown(window); |
|
56 PanelUI.toggle({type: "command"}); |
|
57 yield shownPanelPromise; |
|
58 |
|
59 sendWebSearchKeyCommand(); |
|
60 |
|
61 yield waitForSearchBarFocus(); |
|
62 |
|
63 let hiddenPanelPromise = promisePanelHidden(window); |
|
64 EventUtils.synthesizeKey("VK_ESCAPE", {}); |
|
65 yield hiddenPanelPromise; |
|
66 CustomizableUI.reset(); |
|
67 }); |
|
68 |
|
69 // Ctrl+K should open the overflow panel and focus the search bar if the search bar is overflowed. |
|
70 add_task(function() { |
|
71 this.originalWindowWidth = window.outerWidth; |
|
72 let navbar = document.getElementById(CustomizableUI.AREA_NAVBAR); |
|
73 ok(!navbar.hasAttribute("overflowing"), "Should start with a non-overflowing toolbar."); |
|
74 ok(CustomizableUI.inDefaultState, "Should start in default state."); |
|
75 |
|
76 window.resizeTo(360, window.outerHeight); |
|
77 yield waitForCondition(() => navbar.getAttribute("overflowing") == "true"); |
|
78 ok(!navbar.querySelector("#search-container"), "Search container should be overflowing"); |
|
79 |
|
80 let shownPanelPromise = promiseOverflowShown(window); |
|
81 sendWebSearchKeyCommand(); |
|
82 yield shownPanelPromise; |
|
83 |
|
84 let chevron = document.getElementById("nav-bar-overflow-button"); |
|
85 yield waitForCondition(function() chevron.open); |
|
86 |
|
87 yield waitForSearchBarFocus(); |
|
88 |
|
89 let hiddenPanelPromise = promiseOverflowHidden(window); |
|
90 EventUtils.synthesizeKey("VK_ESCAPE", {}); |
|
91 yield hiddenPanelPromise; |
|
92 let navbar = document.getElementById(CustomizableUI.AREA_NAVBAR); |
|
93 window.resizeTo(this.originalWindowWidth, window.outerHeight); |
|
94 yield waitForCondition(() => !navbar.hasAttribute("overflowing")); |
|
95 ok(!navbar.hasAttribute("overflowing"), "Should not have an overflowing toolbar."); |
|
96 }); |
|
97 |
|
98 // Ctrl+K should focus the search bar if it is in the navbar and not overflowing. |
|
99 add_task(function() { |
|
100 let placement = CustomizableUI.getPlacementOfWidget("search-container"); |
|
101 is(placement.area, CustomizableUI.AREA_NAVBAR, "Should be in nav-bar"); |
|
102 |
|
103 sendWebSearchKeyCommand(); |
|
104 |
|
105 yield waitForSearchBarFocus(); |
|
106 }); |
|
107 |
|
108 // Ctrl+K should open the search page if the search bar has been customized out. |
|
109 add_task(function() { |
|
110 try { |
|
111 expectOpenUILinkInCall = true; |
|
112 CustomizableUI.removeWidgetFromArea("search-container"); |
|
113 let placement = CustomizableUI.getPlacementOfWidget("search-container"); |
|
114 is(placement, null, "Search container should be in palette"); |
|
115 |
|
116 openUILinkInCalled = false; |
|
117 |
|
118 sendWebSearchKeyCommand(); |
|
119 yield waitForCondition(function() openUILinkInCalled); |
|
120 ok(openUILinkInCalled, "The search page should have been opened.") |
|
121 expectOpenUILinkInCall = false; |
|
122 } catch (e) { |
|
123 ok(false, e); |
|
124 } |
|
125 CustomizableUI.reset(); |
|
126 }); |
|
127 |
|
128 registerCleanupFunction(function() { |
|
129 openUILinkIn = this.originalOpenUILinkIn; |
|
130 delete this.originalOpenUILinkIn; |
|
131 }); |
|
132 |
|
133 function sendWebSearchKeyCommand() { |
|
134 if (Services.appinfo.OS === "Darwin") |
|
135 EventUtils.synthesizeKey("k", { accelKey: true }); |
|
136 else |
|
137 EventUtils.synthesizeKey("k", { ctrlKey: true }); |
|
138 } |
|
139 |
|
140 function logActiveElement() { |
|
141 let element = document.activeElement; |
|
142 let str = ""; |
|
143 while (element && element.parentNode) { |
|
144 str = " (" + element.localName + "#" + element.id + "." + [...element.classList].join(".") + ") >" + str; |
|
145 element = element.parentNode; |
|
146 } |
|
147 info("Active element: " + element ? str : "null"); |
|
148 } |