summaryrefslogtreecommitdiffstats
path: root/browser/components/urlbar/tests/browser/browser_whereToOpen.js
blob: 339a20d90ee04c2affa0b8889aa77c242b882696 (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
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

const NON_EMPTY_TAB = "example.com/non-empty";
const EMPTY_TAB = "about:blank";
const META_KEY = AppConstants.platform == "macosx" ? "metaKey" : "ctrlKey";
const ENTER = new KeyboardEvent("keydown", {});
const ALT_ENTER = new KeyboardEvent("keydown", { altKey: true });
const ALTGR_ENTER = new KeyboardEvent("keydown", { modifierAltGraph: true });
const CLICK = new MouseEvent("click", { button: 0 });
const META_CLICK = new MouseEvent("click", { button: 0, [META_KEY]: true });
const MIDDLE_CLICK = new MouseEvent("click", { button: 1 });

let old_openintab = Preferences.get("browser.urlbar.openintab");
registerCleanupFunction(async function () {
  Preferences.set("browser.urlbar.openintab", old_openintab);
});

add_task(async function openInTab() {
  // Open a non-empty tab.
  let tab = (gBrowser.selectedTab = await BrowserTestUtils.openNewForegroundTab(
    gBrowser,
    NON_EMPTY_TAB
  ));

  for (let test of [
    {
      pref: false,
      event: ALT_ENTER,
      desc: "Alt+Enter, non-empty tab, default prefs",
    },
    {
      pref: false,
      event: ALTGR_ENTER,
      desc: "AltGr+Enter, non-empty tab, default prefs",
    },
    {
      pref: false,
      event: META_CLICK,
      desc: "Meta+click, non-empty tab, default prefs",
    },
    {
      pref: false,
      event: MIDDLE_CLICK,
      desc: "Middle click, non-empty tab, default prefs",
    },
    { pref: true, event: ENTER, desc: "Enter, non-empty tab, openInTab" },
    {
      pref: true,
      event: CLICK,
      desc: "Normal click, non-empty tab, openInTab",
    },
  ]) {
    info(test.desc);

    Preferences.set("browser.urlbar.openintab", test.pref);
    let where = gURLBar._whereToOpen(test.event);
    is(where, "tab", "URL would be loaded in a new tab");
  }

  // Clean up.
  BrowserTestUtils.removeTab(tab);
});

add_task(async function keepEmptyTab() {
  // Open an empty tab.
  let tab = (gBrowser.selectedTab = await BrowserTestUtils.openNewForegroundTab(
    gBrowser,
    EMPTY_TAB
  ));

  for (let test of [
    {
      pref: false,
      event: META_CLICK,
      desc: "Meta+click, empty tab, default prefs",
    },
    {
      pref: false,
      event: MIDDLE_CLICK,
      desc: "Middle click, empty tab, default prefs",
    },
  ]) {
    info(test.desc);

    Preferences.set("browser.urlbar.openintab", test.pref);
    let where = gURLBar._whereToOpen(test.event);
    is(where, "tab", "URL would be loaded in a new tab");
  }

  // Clean up.
  BrowserTestUtils.removeTab(tab);
});

add_task(async function reuseEmptyTab() {
  // Open an empty tab.
  let tab = (gBrowser.selectedTab = await BrowserTestUtils.openNewForegroundTab(
    gBrowser,
    EMPTY_TAB
  ));

  for (let test of [
    {
      pref: false,
      event: ALT_ENTER,
      desc: "Alt+Enter, empty tab, default prefs",
    },
    {
      pref: false,
      event: ALTGR_ENTER,
      desc: "AltGr+Enter, empty tab, default prefs",
    },
    { pref: true, event: ENTER, desc: "Enter, empty tab, openInTab" },
    { pref: true, event: CLICK, desc: "Normal click, empty tab, openInTab" },
  ]) {
    info(test.desc);
    Preferences.set("browser.urlbar.openintab", test.pref);
    let where = gURLBar._whereToOpen(test.event);
    is(where, "current", "New URL would reuse the current empty tab");
  }

  // Clean up.
  BrowserTestUtils.removeTab(tab);
});

add_task(async function openInCurrentTab() {
  for (let test of [
    {
      pref: false,
      url: NON_EMPTY_TAB,
      event: ENTER,
      desc: "Enter, non-empty tab, default prefs",
    },
    {
      pref: false,
      url: NON_EMPTY_TAB,
      event: CLICK,
      desc: "Normal click, non-empty tab, default prefs",
    },
    {
      pref: false,
      url: EMPTY_TAB,
      event: ENTER,
      desc: "Enter, empty tab, default prefs",
    },
    {
      pref: false,
      url: EMPTY_TAB,
      event: CLICK,
      desc: "Normal click, empty tab, default prefs",
    },
    {
      pref: true,
      url: NON_EMPTY_TAB,
      event: ALT_ENTER,
      desc: "Alt+Enter, non-empty tab, openInTab",
    },
    {
      pref: true,
      url: NON_EMPTY_TAB,
      event: ALTGR_ENTER,
      desc: "AltGr+Enter, non-empty tab, openInTab",
    },
    {
      pref: true,
      url: NON_EMPTY_TAB,
      event: META_CLICK,
      desc: "Meta+click, non-empty tab, openInTab",
    },
    {
      pref: true,
      url: NON_EMPTY_TAB,
      event: MIDDLE_CLICK,
      desc: "Middle click, non-empty tab, openInTab",
    },
  ]) {
    info(test.desc);

    // Open a new tab.
    let tab = (gBrowser.selectedTab =
      await BrowserTestUtils.openNewForegroundTab(gBrowser, test.url));

    Preferences.set("browser.urlbar.openintab", test.pref);
    let where = gURLBar._whereToOpen(test.event);
    is(where, "current", "URL would open in the current tab");

    // Clean up.
    BrowserTestUtils.removeTab(tab);
  }
});