summaryrefslogtreecommitdiffstats
path: root/browser/base/content/test/general/browser_plainTextLinks.js
blob: 706f21387cda19ce9e2fdc4d4ff66ad9ee4b3ebc (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
/* eslint-disable mozilla/no-arbitrary-setTimeout */
function testExpected(expected, msg) {
  is(
    document.getElementById("context-openlinkincurrent").hidden,
    expected,
    msg
  );
}

function testLinkExpected(expected, msg) {
  is(gContextMenu.linkURL, expected, msg);
}

add_task(async function () {
  const url =
    "data:text/html;charset=UTF-8,Test For Non-Hyperlinked url selection";
  await BrowserTestUtils.openNewForegroundTab(gBrowser, url);

  await SimpleTest.promiseFocus(gBrowser.selectedBrowser);

  // Initial setup of the content area.
  await SpecialPowers.spawn(gBrowser.selectedBrowser, [], async function (arg) {
    let doc = content.document;
    let range = doc.createRange();
    let selection = content.getSelection();

    let mainDiv = doc.createElement("div");
    let div = doc.createElement("div");
    let div2 = doc.createElement("div");
    let span1 = doc.createElement("span");
    let span2 = doc.createElement("span");
    let span3 = doc.createElement("span");
    let span4 = doc.createElement("span");
    let p1 = doc.createElement("p");
    let p2 = doc.createElement("p");
    // eslint-disable-next-line @microsoft/sdl/no-insecure-url
    span1.textContent = "http://index.";
    span2.textContent = "example.com example.com";
    span3.textContent = " - Test";
    span4.innerHTML =
      "<a href='http://www.example.com'>http://www.example.com/example</a>";
    p1.textContent = "mailto:test.com ftp.example.com";
    p2.textContent = "example.com   -";
    div.appendChild(span1);
    div.appendChild(span2);
    div.appendChild(span3);
    div.appendChild(span4);
    div.appendChild(p1);
    div.appendChild(p2);
    let p3 = doc.createElement("p");
    p3.textContent = "main.example.com";
    div2.appendChild(p3);
    mainDiv.appendChild(div);
    mainDiv.appendChild(div2);
    doc.body.appendChild(mainDiv);

    function setSelection(el1, el2, index1, index2) {
      while (el1.nodeType != el1.TEXT_NODE) {
        el1 = el1.firstChild;
      }
      while (el2.nodeType != el1.TEXT_NODE) {
        el2 = el2.firstChild;
      }

      selection.removeAllRanges();
      range.setStart(el1, index1);
      range.setEnd(el2, index2);
      selection.addRange(range);

      return range;
    }

    // Each of these tests creates a selection and returns a range within it.
    content.tests = [
      () => setSelection(span1.firstChild, span2.firstChild, 0, 11),
      () => setSelection(span1.firstChild, span2.firstChild, 7, 11),
      () => setSelection(span1.firstChild, span2.firstChild, 8, 11),
      () => setSelection(span2.firstChild, span2.firstChild, 0, 11),
      () => setSelection(span2.firstChild, span2.firstChild, 11, 23),
      () => setSelection(span2.firstChild, span2.firstChild, 0, 10),
      () => setSelection(span2.firstChild, span3.firstChild, 12, 7),
      () => setSelection(span2.firstChild, span2.firstChild, 12, 19),
      () => setSelection(p1.firstChild, p1.firstChild, 0, 15),
      () => setSelection(p1.firstChild, p1.firstChild, 16, 31),
      () => setSelection(p2.firstChild, p2.firstChild, 0, 14),
      () => {
        selection.selectAllChildren(div2);
        return selection.getRangeAt(0);
      },
      () => {
        selection.selectAllChildren(span4);
        return selection.getRangeAt(0);
      },
      () => {
        mainDiv.innerHTML = "(open-suse.ru)";
        return setSelection(mainDiv, mainDiv, 1, 13);
      },
      () => setSelection(mainDiv, mainDiv, 1, 14),
    ];
  });

  let checks = [
    () =>
      testExpected(
        false,
        "The link context menu should show for http://www.example.com"
      ),
    () =>
      testExpected(
        false,
        "The link context menu should show for www.example.com"
      ),
    () =>
      testExpected(
        true,
        "The link context menu should not show for ww.example.com"
      ),
    () => {
      testExpected(false, "The link context menu should show for example.com");
      testLinkExpected(
        // eslint-disable-next-line @microsoft/sdl/no-insecure-url
        "http://example.com/",
        "url for example.com selection should not prepend www"
      );
    },
    () =>
      testExpected(false, "The link context menu should show for example.com"),
    () =>
      testExpected(
        true,
        "Link options should not show for selection that's not at a word boundary"
      ),
    () =>
      testExpected(
        true,
        "Link options should not show for selection that has whitespace"
      ),
    () =>
      testExpected(
        true,
        "Link options should not show unless a url is selected"
      ),
    () => testExpected(true, "Link options should not show for mailto: links"),
    () => {
      testExpected(false, "Link options should show for ftp.example.com");
      testLinkExpected(
        // eslint-disable-next-line @microsoft/sdl/no-insecure-url
        "http://ftp.example.com/",
        "ftp.example.com should be preceeded with http://"
      );
    },
    () => testExpected(false, "Link options should show for www.example.com  "),
    () =>
      testExpected(
        false,
        "Link options should show for triple-click selections"
      ),
    () =>
      testLinkExpected(
        // eslint-disable-next-line @microsoft/sdl/no-insecure-url
        "http://www.example.com/",
        "Linkified text should open the correct link"
      ),
    () => {
      testExpected(false, "Link options should show for open-suse.ru");
      testLinkExpected(
        // eslint-disable-next-line @microsoft/sdl/no-insecure-url
        "http://open-suse.ru/",
        "Linkified text should open the correct link"
      );
    },
    () =>
      testExpected(true, "Link options should not show for 'open-suse.ru)'"),
  ];

  let contentAreaContextMenu = document.getElementById(
    "contentAreaContextMenu"
  );

  for (let testid = 0; testid < checks.length; testid++) {
    let menuPosition = await SpecialPowers.spawn(
      gBrowser.selectedBrowser,
      [{ testid }],
      async function (arg) {
        let range = content.tests[arg.testid]();

        // Get the range of the selection and determine its coordinates. These
        // coordinates will be returned to the parent process and the context menu
        // will be opened at that location.
        let rangeRect = range.getBoundingClientRect();
        return [rangeRect.x + 3, rangeRect.y + 3];
      }
    );

    // Trigger a mouse event until we receive the popupshown event.
    let sawPopup = false;
    let popupShownPromise = BrowserTestUtils.waitForEvent(
      contentAreaContextMenu,
      "popupshown",
      false,
      () => {
        sawPopup = true;
        return true;
      }
    );
    while (!sawPopup) {
      await BrowserTestUtils.synthesizeMouseAtPoint(
        menuPosition[0],
        menuPosition[1],
        { type: "contextmenu", button: 2 },
        gBrowser.selectedBrowser
      );
      if (!sawPopup) {
        await new Promise(r => setTimeout(r, 100));
      }
    }
    await popupShownPromise;

    checks[testid]();

    // On Linux non-e10s it's possible the menu was closed by a focus-out event
    // on the window. Work around this by calling hidePopup only if the menu
    // hasn't been closed yet. See bug 1352709 comment 36.
    if (contentAreaContextMenu.state === "closed") {
      continue;
    }

    let popupHiddenPromise = BrowserTestUtils.waitForEvent(
      contentAreaContextMenu,
      "popuphidden"
    );
    contentAreaContextMenu.hidePopup();
    await popupHiddenPromise;
  }

  gBrowser.removeCurrentTab();
});