summaryrefslogtreecommitdiffstats
path: root/browser/base/content/test/tabs/browser_middle_click_new_tab_button_loads_clipboard.js
blob: 07d0be0232645ce8fb7cde87e68da9edd94779bf (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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

const searchclipboardforPref = "browser.tabs.searchclipboardfor.middleclick";

const { SearchTestUtils } = ChromeUtils.importESModule(
  "resource://testing-common/SearchTestUtils.sys.mjs"
);

add_setup(async function () {
  await SpecialPowers.pushPrefEnv({
    set: [
      [searchclipboardforPref, true],
      // set preloading to false so we can await the new tab being opened.
      ["browser.newtab.preload", false],
    ],
  });
  NewTabPagePreloading.removePreloadedBrowser(window);
  // Create an engine to use for the test.
  SearchTestUtils.init(this);
  await SearchTestUtils.installSearchExtension(
    {
      name: "MozSearch",
      search_url: "https://example.org/",
      search_url_get_params: "q={searchTerms}",
    },
    { setAsDefaultPrivate: true }
  );
  // We overflow tabs, close all the extra ones.
  registerCleanupFunction(() => {
    while (gBrowser.tabs.length > 1) {
      BrowserTestUtils.removeTab(gBrowser.selectedTab);
    }
  });
});

add_task(async function middleclick_tabs_newtab_button_with_url_in_clipboard() {
  var previousTabsLength = gBrowser.tabs.length;
  info("Previous tabs count is " + previousTabsLength);

  let url = "javascript:https://www.example.com/";
  let safeUrl = "https://www.example.com/";
  await new Promise((resolve, reject) => {
    SimpleTest.waitForClipboard(
      url,
      () => {
        Cc["@mozilla.org/widget/clipboardhelper;1"]
          .getService(Ci.nsIClipboardHelper)
          .copyString(url);
      },
      resolve,
      () => {
        ok(false, "Clipboard copy failed");
        reject();
      }
    );
  });

  info("Middle clicking 'new tab' button");
  let promiseTabLoaded = BrowserTestUtils.waitForNewTab(
    gBrowser,
    safeUrl,
    true
  );
  EventUtils.synthesizeMouseAtCenter(
    document.getElementById("tabs-newtab-button"),
    { button: 1 }
  );

  await promiseTabLoaded;
  is(gBrowser.tabs.length, previousTabsLength + 1, "We created a tab");
  is(
    gBrowser.currentURI.spec,
    safeUrl,
    "New Tab URL is the safe content of the clipboard"
  );

  BrowserTestUtils.removeTab(gBrowser.selectedTab);
});

add_task(
  async function middleclick_tabs_newtab_button_with_word_in_clipboard() {
    var previousTabsLength = gBrowser.tabs.length;
    info("Previous tabs count is " + previousTabsLength);

    let word = "word";
    let searchUrl = "https://example.org/?q=word";
    await new Promise((resolve, reject) => {
      SimpleTest.waitForClipboard(
        word,
        () => {
          Cc["@mozilla.org/widget/clipboardhelper;1"]
            .getService(Ci.nsIClipboardHelper)
            .copyString(word);
        },
        resolve,
        () => {
          ok(false, "Clipboard copy failed");
          reject();
        }
      );
    });

    info("Middle clicking 'new tab' button");
    let promiseTabLoaded = BrowserTestUtils.waitForNewTab(
      gBrowser,
      searchUrl,
      true
    );
    EventUtils.synthesizeMouseAtCenter(
      document.getElementById("tabs-newtab-button"),
      { button: 1 }
    );

    await promiseTabLoaded;
    is(gBrowser.tabs.length, previousTabsLength + 1, "We created a tab");
    is(
      gBrowser.currentURI.spec,
      searchUrl,
      "New Tab URL is the search engine with the content of the clipboard"
    );

    BrowserTestUtils.removeTab(gBrowser.selectedTab);
  }
);

add_task(async function middleclick_new_tab_button_with_url_in_clipboard() {
  await BrowserTestUtils.overflowTabs(registerCleanupFunction, window);
  await BrowserTestUtils.waitForCondition(() => {
    return Array.from(gBrowser.tabs).every(tab => tab._fullyOpen);
  });

  var previousTabsLength = gBrowser.tabs.length;
  info("Previous tabs count is " + previousTabsLength);

  let url = "javascript:https://www.example.com/";
  let safeUrl = "https://www.example.com/";
  await new Promise((resolve, reject) => {
    SimpleTest.waitForClipboard(
      url,
      () => {
        Cc["@mozilla.org/widget/clipboardhelper;1"]
          .getService(Ci.nsIClipboardHelper)
          .copyString(url);
      },
      resolve,
      () => {
        ok(false, "Clipboard copy failed");
        reject();
      }
    );
  });

  info("Middle clicking 'new tab' button");
  let promiseTabLoaded = BrowserTestUtils.waitForNewTab(
    gBrowser,
    safeUrl,
    true
  );
  EventUtils.synthesizeMouseAtCenter(
    document.getElementById("new-tab-button"),
    { button: 1 }
  );

  await promiseTabLoaded;
  is(gBrowser.tabs.length, previousTabsLength + 1, "We created a tab");
  is(
    gBrowser.currentURI.spec,
    safeUrl,
    "New Tab URL is the safe content of the clipboard"
  );

  BrowserTestUtils.removeTab(gBrowser.selectedTab);
});

add_task(async function middleclick_new_tab_button_with_word_in_clipboard() {
  var previousTabsLength = gBrowser.tabs.length;
  info("Previous tabs count is " + previousTabsLength);

  let word = "word";
  let searchUrl = "https://example.org/?q=word";
  await new Promise((resolve, reject) => {
    SimpleTest.waitForClipboard(
      word,
      () => {
        Cc["@mozilla.org/widget/clipboardhelper;1"]
          .getService(Ci.nsIClipboardHelper)
          .copyString(word);
      },
      resolve,
      () => {
        ok(false, "Clipboard copy failed");
        reject();
      }
    );
  });

  info("Middle clicking 'new tab' button");
  let promiseTabLoaded = BrowserTestUtils.waitForNewTab(
    gBrowser,
    searchUrl,
    true
  );
  EventUtils.synthesizeMouseAtCenter(
    document.getElementById("new-tab-button"),
    { button: 1 }
  );

  await promiseTabLoaded;
  is(gBrowser.tabs.length, previousTabsLength + 1, "We created a tab");
  is(
    gBrowser.currentURI.spec,
    searchUrl,
    "New Tab URL is the search engine with the content of the clipboard"
  );

  BrowserTestUtils.removeTab(gBrowser.selectedTab);
});

add_task(async function middleclick_new_tab_button_with_spaces_in_clipboard() {
  let spaces = "    \n    ";
  await new Promise((resolve, reject) => {
    SimpleTest.waitForClipboard(
      spaces,
      () => {
        Cc["@mozilla.org/widget/clipboardhelper;1"]
          .getService(Ci.nsIClipboardHelper)
          .copyString(spaces);
      },
      resolve,
      () => {
        ok(false, "Clipboard copy failed");
        reject();
      }
    );
  });

  info("Middle clicking 'new tab' button");
  let promiseTabOpened = BrowserTestUtils.waitForNewTab(gBrowser);
  EventUtils.synthesizeMouseAtCenter(
    document.getElementById("new-tab-button"),
    { button: 1 }
  );

  await promiseTabOpened;
  is(
    gBrowser.currentURI.spec,
    "about:newtab",
    "New Tab URL is the regular new tab page."
  );

  BrowserTestUtils.removeTab(gBrowser.selectedTab);
});