|
1 function test() { |
|
2 gPrefService.setBoolPref("browser.ctrlTab.previews", true); |
|
3 |
|
4 gBrowser.addTab(); |
|
5 gBrowser.addTab(); |
|
6 gBrowser.addTab(); |
|
7 |
|
8 checkTabs(4); |
|
9 |
|
10 ctrlTabTest([2] , 1, 0); |
|
11 ctrlTabTest([2, 3, 1], 2, 2); |
|
12 ctrlTabTest([] , 4, 2); |
|
13 |
|
14 { |
|
15 let selectedIndex = gBrowser.tabContainer.selectedIndex; |
|
16 pressCtrlTab(); |
|
17 pressCtrlTab(true); |
|
18 releaseCtrl(); |
|
19 is(gBrowser.tabContainer.selectedIndex, selectedIndex, |
|
20 "Ctrl+Tab -> Ctrl+Shift+Tab keeps the selected tab"); |
|
21 } |
|
22 |
|
23 { // test for bug 445369 |
|
24 let tabs = gBrowser.tabs.length; |
|
25 pressCtrlTab(); |
|
26 EventUtils.synthesizeKey("w", { ctrlKey: true }); |
|
27 is(gBrowser.tabs.length, tabs - 1, "Ctrl+Tab -> Ctrl+W removes one tab"); |
|
28 releaseCtrl(); |
|
29 } |
|
30 |
|
31 { // test for bug 667314 |
|
32 let tabs = gBrowser.tabs.length; |
|
33 pressCtrlTab(); |
|
34 pressCtrlTab(true); |
|
35 EventUtils.synthesizeKey("w", { ctrlKey: true }); |
|
36 is(gBrowser.tabs.length, tabs - 1, "Ctrl+Tab -> Ctrl+W removes the selected tab"); |
|
37 releaseCtrl(); |
|
38 } |
|
39 |
|
40 gBrowser.addTab(); |
|
41 checkTabs(3); |
|
42 ctrlTabTest([2, 1, 0], 7, 1); |
|
43 |
|
44 gBrowser.addTab(); |
|
45 checkTabs(4); |
|
46 |
|
47 { // test for bug 445369 |
|
48 selectTabs([1, 2, 0]); |
|
49 |
|
50 let selectedTab = gBrowser.selectedTab; |
|
51 let tabToRemove = gBrowser.tabs[1]; |
|
52 |
|
53 pressCtrlTab(); |
|
54 pressCtrlTab(); |
|
55 EventUtils.synthesizeKey("w", { ctrlKey: true }); |
|
56 ok(!tabToRemove.parentNode, |
|
57 "Ctrl+Tab*2 -> Ctrl+W removes the second most recently selected tab"); |
|
58 |
|
59 pressCtrlTab(true); |
|
60 pressCtrlTab(true); |
|
61 releaseCtrl(); |
|
62 ok(selectedTab.selected, |
|
63 "Ctrl+Tab*2 -> Ctrl+W -> Ctrl+Shift+Tab*2 keeps the selected tab"); |
|
64 } |
|
65 gBrowser.removeTab(gBrowser.tabContainer.lastChild); |
|
66 checkTabs(2); |
|
67 |
|
68 ctrlTabTest([1], 1, 0); |
|
69 |
|
70 gBrowser.removeTab(gBrowser.tabContainer.lastChild); |
|
71 checkTabs(1); |
|
72 |
|
73 { // test for bug 445768 |
|
74 let focusedWindow = document.commandDispatcher.focusedWindow; |
|
75 let eventConsumed = true; |
|
76 let detectKeyEvent = function (event) { |
|
77 eventConsumed = event.defaultPrevented; |
|
78 }; |
|
79 document.addEventListener("keypress", detectKeyEvent, false); |
|
80 pressCtrlTab(); |
|
81 document.removeEventListener("keypress", detectKeyEvent, false); |
|
82 ok(eventConsumed, "Ctrl+Tab consumed by the tabbed browser if one tab is open"); |
|
83 is(focusedWindow, document.commandDispatcher.focusedWindow, |
|
84 "Ctrl+Tab doesn't change focus if one tab is open"); |
|
85 } |
|
86 |
|
87 // cleanup |
|
88 if (gPrefService.prefHasUserValue("browser.ctrlTab.previews")) |
|
89 gPrefService.clearUserPref("browser.ctrlTab.previews"); |
|
90 |
|
91 /* private utility functions */ |
|
92 |
|
93 function pressCtrlTab(aShiftKey) |
|
94 EventUtils.synthesizeKey("VK_TAB", { ctrlKey: true, shiftKey: !!aShiftKey }); |
|
95 |
|
96 function releaseCtrl() |
|
97 EventUtils.synthesizeKey("VK_CONTROL", { type: "keyup" }); |
|
98 |
|
99 function isOpen() |
|
100 ctrlTab.isOpen; |
|
101 |
|
102 function checkTabs(aTabs) { |
|
103 var tabs = gBrowser.tabs.length; |
|
104 if (tabs != aTabs) { |
|
105 while (gBrowser.tabs.length > 1) |
|
106 gBrowser.removeCurrentTab(); |
|
107 throw "expected " + aTabs + " open tabs, got " + tabs; |
|
108 } |
|
109 } |
|
110 |
|
111 function selectTabs(tabs) { |
|
112 tabs.forEach(function (index) { |
|
113 gBrowser.selectedTab = gBrowser.tabs[index]; |
|
114 }); |
|
115 } |
|
116 |
|
117 function ctrlTabTest(tabsToSelect, tabTimes, expectedIndex) { |
|
118 selectTabs(tabsToSelect); |
|
119 |
|
120 var indexStart = gBrowser.tabContainer.selectedIndex; |
|
121 var tabCount = gBrowser.tabs.length; |
|
122 var normalized = tabTimes % tabCount; |
|
123 var where = normalized == 1 ? "back to the previously selected tab" : |
|
124 normalized + " tabs back in most-recently-selected order"; |
|
125 |
|
126 for (let i = 0; i < tabTimes; i++) { |
|
127 pressCtrlTab(); |
|
128 |
|
129 if (tabCount > 2) |
|
130 is(gBrowser.tabContainer.selectedIndex, indexStart, |
|
131 "Selected tab doesn't change while tabbing"); |
|
132 } |
|
133 |
|
134 if (tabCount > 2) { |
|
135 ok(isOpen(), |
|
136 "With " + tabCount + " tabs open, Ctrl+Tab opens the preview panel"); |
|
137 |
|
138 releaseCtrl(); |
|
139 |
|
140 ok(!isOpen(), |
|
141 "Releasing Ctrl closes the preview panel"); |
|
142 } else { |
|
143 ok(!isOpen(), |
|
144 "With " + tabCount + " tabs open, Ctrl+Tab doesn't open the preview panel"); |
|
145 } |
|
146 |
|
147 is(gBrowser.tabContainer.selectedIndex, expectedIndex, |
|
148 "With "+ tabCount +" tabs open and tab " + indexStart |
|
149 + " selected, Ctrl+Tab*" + tabTimes + " goes " + where); |
|
150 } |
|
151 } |