summaryrefslogtreecommitdiffstats
path: root/browser/base/content/test/general/browser_tabkeynavigation.js
blob: 765bf5c21d3be7e418a46776610ba1bfa55726a1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/*
 * This test checks that keyboard navigation for tabs isn't blocked by content
 */
add_task(async function test() {
  let testPage1 =
    "data:text/html,<html id='tab1'><body><button id='button1'>Tab 1</button></body></html>";
  let testPage2 =
    "data:text/html,<html id='tab2'><body><button id='button2'>Tab 2</button><script>function preventDefault(event) { event.preventDefault(); event.stopImmediatePropagation(); } window.addEventListener('keydown', preventDefault, true); window.addEventListener('keypress', preventDefault, true);</script></body></html>";
  let testPage3 =
    "data:text/html,<html id='tab3'><body><button id='button3'>Tab 3</button></body></html>";

  let tab1 = await BrowserTestUtils.openNewForegroundTab(gBrowser, testPage1);
  let browser1 = gBrowser.getBrowserForTab(tab1);
  let tab2 = await BrowserTestUtils.openNewForegroundTab(gBrowser, testPage2);
  let tab3 = await BrowserTestUtils.openNewForegroundTab(gBrowser, testPage3);

  await SpecialPowers.pushPrefEnv({
    set: [["browser.ctrlTab.sortByRecentlyUsed", false]],
  });

  // Disable tab animations
  gReduceMotionOverride = true;

  gBrowser.selectedTab = tab1;
  browser1.focus();

  is(gBrowser.selectedTab, tab1, "Tab1 should be activated");
  EventUtils.synthesizeKey("VK_TAB", { ctrlKey: true });
  is(
    gBrowser.selectedTab,
    tab2,
    "Tab2 should be activated by pressing Ctrl+Tab on Tab1"
  );

  EventUtils.synthesizeKey("VK_TAB", { ctrlKey: true });
  is(
    gBrowser.selectedTab,
    tab3,
    "Tab3 should be activated by pressing Ctrl+Tab on Tab2"
  );

  EventUtils.synthesizeKey("VK_TAB", { ctrlKey: true, shiftKey: true });
  is(
    gBrowser.selectedTab,
    tab2,
    "Tab2 should be activated by pressing Ctrl+Shift+Tab on Tab3"
  );

  EventUtils.synthesizeKey("VK_TAB", { ctrlKey: true, shiftKey: true });
  is(
    gBrowser.selectedTab,
    tab1,
    "Tab1 should be activated by pressing Ctrl+Shift+Tab on Tab2"
  );

  gBrowser.selectedTab = tab1;
  browser1.focus();

  is(gBrowser.selectedTab, tab1, "Tab1 should be activated");
  EventUtils.synthesizeKey("VK_PAGE_DOWN", { ctrlKey: true });
  is(
    gBrowser.selectedTab,
    tab2,
    "Tab2 should be activated by pressing Ctrl+PageDown on Tab1"
  );

  EventUtils.synthesizeKey("VK_PAGE_DOWN", { ctrlKey: true });
  is(
    gBrowser.selectedTab,
    tab3,
    "Tab3 should be activated by pressing Ctrl+PageDown on Tab2"
  );

  EventUtils.synthesizeKey("VK_PAGE_UP", { ctrlKey: true });
  is(
    gBrowser.selectedTab,
    tab2,
    "Tab2 should be activated by pressing Ctrl+PageUp on Tab3"
  );

  EventUtils.synthesizeKey("VK_PAGE_UP", { ctrlKey: true });
  is(
    gBrowser.selectedTab,
    tab1,
    "Tab1 should be activated by pressing Ctrl+PageUp on Tab2"
  );

  if (gBrowser.tabbox._handleMetaAltArrows) {
    gBrowser.selectedTab = tab1;
    browser1.focus();

    let ltr = window.getComputedStyle(gBrowser.tabbox).direction == "ltr";
    let advanceKey = ltr ? "VK_RIGHT" : "VK_LEFT";
    let reverseKey = ltr ? "VK_LEFT" : "VK_RIGHT";

    is(gBrowser.selectedTab, tab1, "Tab1 should be activated");
    EventUtils.synthesizeKey(advanceKey, { altKey: true, metaKey: true });
    is(
      gBrowser.selectedTab,
      tab2,
      "Tab2 should be activated by pressing Ctrl+" + advanceKey + " on Tab1"
    );

    EventUtils.synthesizeKey(advanceKey, { altKey: true, metaKey: true });
    is(
      gBrowser.selectedTab,
      tab3,
      "Tab3 should be activated by pressing Ctrl+" + advanceKey + " on Tab2"
    );

    EventUtils.synthesizeKey(reverseKey, { altKey: true, metaKey: true });
    is(
      gBrowser.selectedTab,
      tab2,
      "Tab2 should be activated by pressing Ctrl+" + reverseKey + " on Tab3"
    );

    EventUtils.synthesizeKey(reverseKey, { altKey: true, metaKey: true });
    is(
      gBrowser.selectedTab,
      tab1,
      "Tab1 should be activated by pressing Ctrl+" + reverseKey + " on Tab2"
    );
  }

  gBrowser.selectedTab = tab2;
  is(gBrowser.selectedTab, tab2, "Tab2 should be activated");
  is(gBrowser.tabContainer.selectedIndex, 2, "Tab2 index should be 2");

  EventUtils.synthesizeKey("VK_PAGE_DOWN", { ctrlKey: true, shiftKey: true });
  is(
    gBrowser.selectedTab,
    tab2,
    "Tab2 should be activated after Ctrl+Shift+PageDown"
  );
  is(
    gBrowser.tabContainer.selectedIndex,
    3,
    "Tab2 index should be 1 after Ctrl+Shift+PageDown"
  );

  EventUtils.synthesizeKey("VK_PAGE_UP", { ctrlKey: true, shiftKey: true });
  is(
    gBrowser.selectedTab,
    tab2,
    "Tab2 should be activated after Ctrl+Shift+PageUp"
  );
  is(
    gBrowser.tabContainer.selectedIndex,
    2,
    "Tab2 index should be 2 after Ctrl+Shift+PageUp"
  );

  if (navigator.platform.indexOf("Mac") == 0) {
    gBrowser.selectedTab = tab1;
    browser1.focus();

    // XXX Currently, Command + "{" and "}" don't work if keydown event is
    //     consumed because following keypress event isn't fired.

    let ltr = window.getComputedStyle(gBrowser.tabbox).direction == "ltr";
    let advanceKey = ltr ? "}" : "{";
    let reverseKey = ltr ? "{" : "}";

    is(gBrowser.selectedTab, tab1, "Tab1 should be activated");

    EventUtils.synthesizeKey(advanceKey, { metaKey: true });
    is(
      gBrowser.selectedTab,
      tab2,
      "Tab2 should be activated by pressing Ctrl+" + advanceKey + " on Tab1"
    );

    EventUtils.synthesizeKey(advanceKey, { metaKey: true });
    is(
      gBrowser.selectedTab,
      tab3,
      "Tab3 should be activated by pressing Ctrl+" + advanceKey + " on Tab2"
    );

    EventUtils.synthesizeKey(reverseKey, { metaKey: true });
    is(
      gBrowser.selectedTab,
      tab2,
      "Tab2 should be activated by pressing Ctrl+" + reverseKey + " on Tab3"
    );

    EventUtils.synthesizeKey(reverseKey, { metaKey: true });
    is(
      gBrowser.selectedTab,
      tab1,
      "Tab1 should be activated by pressing Ctrl+" + reverseKey + " on Tab2"
    );
  } else {
    gBrowser.selectedTab = tab2;
    EventUtils.synthesizeKey("VK_F4", { type: "keydown", ctrlKey: true });

    isnot(
      gBrowser.selectedTab,
      tab2,
      "Tab2 should be closed by pressing Ctrl+F4 on Tab2"
    );
    is(
      gBrowser.tabs.length,
      3,
      "The count of tabs should be 3 since tab2 should be closed"
    );

    // NOTE: keypress event shouldn't be fired since the keydown event should
    //       be consumed by tab2.
    EventUtils.synthesizeKey("VK_F4", { type: "keyup", ctrlKey: true });
    is(
      gBrowser.tabs.length,
      3,
      "The count of tabs should be 3 since renaming key events shouldn't close other tabs"
    );
  }

  gBrowser.selectedTab = tab3;
  while (gBrowser.tabs.length > 1) {
    gBrowser.removeCurrentTab();
  }
});