summaryrefslogtreecommitdiffstats
path: root/browser/base/content/test/tabs/browser_paste_event_at_middle_click_on_link.js
blob: a6b7f964105714cb44f5e0c978b1f5924a8430f3 (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
"use strict";

add_task(async function doCheckPasteEventAtMiddleClickOnAnchorElement() {
  await SpecialPowers.pushPrefEnv({
    set: [
      ["browser.tabs.opentabfor.middleclick", true],
      ["middlemouse.paste", true],
      ["middlemouse.contentLoadURL", false],
      ["general.autoScroll", false],
    ],
  });

  await new Promise((resolve, reject) => {
    SimpleTest.waitForClipboard(
      "Text in the clipboard",
      () => {
        Cc["@mozilla.org/widget/clipboardhelper;1"]
          .getService(Ci.nsIClipboardHelper)
          .copyString("Text in the clipboard");
      },
      resolve,
      () => {
        ok(false, "Clipboard copy failed");
        reject();
      }
    );
  });

  is(
    gBrowser.tabs.length,
    1,
    "Number of tabs should be 1 at starting this test #1"
  );

  let pageURL = getRootDirectory(gTestPath).replace(
    "chrome://mochitests/content",
    "https://example.com"
  );
  pageURL = `${pageURL}file_anchor_elements.html`;
  let tab = await BrowserTestUtils.openNewForegroundTab(gBrowser, pageURL);

  let pasteEventCount = 0;
  BrowserTestUtils.addContentEventListener(
    gBrowser.selectedBrowser,
    "paste",
    () => {
      ++pasteEventCount;
    }
  );

  // Click the usual link.
  ok(true, "Clicking on usual link...");
  let newTabPromise = BrowserTestUtils.waitForNewTab(
    gBrowser,
    "https://example.com/#a_with_href",
    true
  );
  await BrowserTestUtils.synthesizeMouseAtCenter(
    "#a_with_href",
    { button: 1 },
    gBrowser.selectedBrowser
  );
  let openTabForUsualLink = await newTabPromise;
  is(
    openTabForUsualLink.linkedBrowser.currentURI.spec,
    "https://example.com/#a_with_href",
    "Middle click should open site to correct url at clicking on usual link"
  );
  is(
    pasteEventCount,
    0,
    "paste event should be suppressed when clicking on usual link"
  );

  // Click the link in editing host.
  is(
    gBrowser.tabs.length,
    3,
    "Number of tabs should be 3 at starting this test #2"
  );
  ok(true, "Clicking on editable link...");
  await BrowserTestUtils.synthesizeMouseAtCenter(
    "#editable_a_with_href",
    { button: 1 },
    gBrowser.selectedBrowser
  );
  await TestUtils.waitForCondition(
    () => pasteEventCount >= 1,
    "Waiting for paste event caused by clicking on editable link"
  );
  is(
    pasteEventCount,
    1,
    "paste event should be suppressed when clicking on editable link"
  );
  is(
    gBrowser.tabs.length,
    3,
    "Clicking on editable link shouldn't open new tab"
  );

  // Click the link in non-editable area in editing host.
  ok(true, "Clicking on non-editable link in an editing host...");
  newTabPromise = BrowserTestUtils.waitForNewTab(
    gBrowser,
    "https://example.com/#non-editable_a_with_href",
    true
  );
  await BrowserTestUtils.synthesizeMouseAtCenter(
    "#non-editable_a_with_href",
    { button: 1 },
    gBrowser.selectedBrowser
  );
  let openTabForNonEditableLink = await newTabPromise;
  is(
    openTabForNonEditableLink.linkedBrowser.currentURI.spec,
    "https://example.com/#non-editable_a_with_href",
    "Middle click should open site to correct url at clicking on non-editable link in an editing host."
  );
  is(
    pasteEventCount,
    1,
    "paste event should be suppressed when clicking on non-editable link in an editing host"
  );

  // Click the <a> element without href attribute.
  is(
    gBrowser.tabs.length,
    4,
    "Number of tabs should be 4 at starting this test #3"
  );
  ok(true, "Clicking on anchor element without href...");
  await BrowserTestUtils.synthesizeMouseAtCenter(
    "#a_with_name",
    { button: 1 },
    gBrowser.selectedBrowser
  );
  await TestUtils.waitForCondition(
    () => pasteEventCount >= 2,
    "Waiting for paste event caused by clicking on anchor element without href"
  );
  is(
    pasteEventCount,
    2,
    "paste event should be suppressed when clicking on anchor element without href"
  );
  is(
    gBrowser.tabs.length,
    4,
    "Clicking on anchor element without href shouldn't open new tab"
  );

  BrowserTestUtils.removeTab(tab);
  BrowserTestUtils.removeTab(openTabForUsualLink);
  BrowserTestUtils.removeTab(openTabForNonEditableLink);
});