summaryrefslogtreecommitdiffstats
path: root/browser/base/content/test/contextMenu/browser_contextmenu_loadblobinnewtab.js
blob: cbf1b27590ef10f9ceaf780f11f586a8b5b296b5 (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
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

const RESOURCE_LINK =
  getRootDirectory(gTestPath).replace(
    "chrome://mochitests/content",
    "https://example.com"
  ) + "browser_contextmenu_loadblobinnewtab.html";

const blobDataAsString = `!"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ`;

// Helper method to right click on the provided link (selector as id),
// open in new tab and return the content of the first <pre> under the
// <body> of the new tab's document.
async function rightClickOpenInNewTabAndReturnContent(selector) {
  const loaded = BrowserTestUtils.browserLoaded(
    gBrowser.selectedBrowser,
    false,
    RESOURCE_LINK
  );
  BrowserTestUtils.loadURIString(gBrowser.selectedBrowser, RESOURCE_LINK);
  await loaded;

  const generatedBlobURL = await ContentTask.spawn(
    gBrowser.selectedBrowser,
    { selector },
    async args => {
      return content.document.getElementById(args.selector).href;
    }
  );

  const contextMenu = document.getElementById("contentAreaContextMenu");
  is(contextMenu.state, "closed", "checking if context menu is closed");

  let awaitPopupShown = BrowserTestUtils.waitForEvent(
    contextMenu,
    "popupshown"
  );

  await BrowserTestUtils.synthesizeMouseAtCenter(
    "#" + selector,
    { type: "contextmenu", button: 2 },
    gBrowser.selectedBrowser
  );
  await awaitPopupShown;

  let awaitPopupHidden = BrowserTestUtils.waitForEvent(
    contextMenu,
    "popuphidden"
  );

  const openPromise = BrowserTestUtils.waitForNewTab(
    gBrowser,
    generatedBlobURL,
    false
  );

  document.getElementById("context-openlinkintab").doCommand();

  contextMenu.hidePopup();
  await awaitPopupHidden;

  let openTab = await openPromise;

  await BrowserTestUtils.switchTab(gBrowser, gBrowser.tabs[1]);

  let blobDataFromContent = await ContentTask.spawn(
    gBrowser.selectedBrowser,
    null,
    async function () {
      while (!content.document.querySelector("body pre")) {
        await new Promise(resolve =>
          content.setTimeout(() => {
            resolve();
          }, 100)
        );
      }
      return content.document.body.firstElementChild.innerText.trim();
    }
  );

  let tabClosed = BrowserTestUtils.waitForTabClosing(openTab);
  await BrowserTestUtils.removeTab(openTab);
  await tabClosed;

  return blobDataFromContent;
}

// Helper method to open selected link in new tab (selector as id),
// and return the content of the first <pre> under the <body> of
// the new tab's document.
async function openInNewTabAndReturnContent(selector) {
  const loaded = BrowserTestUtils.browserLoaded(
    gBrowser.selectedBrowser,
    false,
    RESOURCE_LINK
  );
  BrowserTestUtils.loadURIString(gBrowser.selectedBrowser, RESOURCE_LINK);
  await loaded;

  const generatedBlobURL = await ContentTask.spawn(
    gBrowser.selectedBrowser,
    { selector },
    async args => {
      return content.document.getElementById(args.selector).href;
    }
  );

  let openTab = await BrowserTestUtils.openNewForegroundTab(
    gBrowser,
    generatedBlobURL
  );

  let blobDataFromContent = await ContentTask.spawn(
    gBrowser.selectedBrowser,
    null,
    async function () {
      while (!content.document.querySelector("body pre")) {
        await new Promise(resolve =>
          content.setTimeout(() => {
            resolve();
          }, 100)
        );
      }
      return content.document.body.firstElementChild.innerText.trim();
    }
  );

  let tabClosed = BrowserTestUtils.waitForTabClosing(openTab);
  await BrowserTestUtils.removeTab(openTab);
  await tabClosed;

  return blobDataFromContent;
}

add_setup(async function () {
  await SpecialPowers.pushPrefEnv({
    set: [["privacy.partition.bloburl_per_agent_cluster", false]],
  });
});

add_task(async function test_rightclick_open_bloburl_in_new_tab() {
  let blobDataFromLoadedPage = await rightClickOpenInNewTabAndReturnContent(
    "blob-url-link"
  );
  is(
    blobDataFromLoadedPage,
    blobDataAsString,
    "The blobURL is correctly loaded"
  );
});

add_task(async function test_rightclick_open_bloburl_referrer_in_new_tab() {
  let blobDataFromLoadedPage = await rightClickOpenInNewTabAndReturnContent(
    "blob-url-referrer-link"
  );
  is(
    blobDataFromLoadedPage,
    blobDataAsString,
    "The blobURL is correctly loaded"
  );
});

add_task(async function test_open_bloburl_in_new_tab() {
  let blobDataFromLoadedPage = await openInNewTabAndReturnContent(
    "blob-url-link"
  );
  is(
    blobDataFromLoadedPage,
    blobDataAsString,
    "The blobURL is correctly loaded"
  );
});

add_task(async function test_open_bloburl_referrer_in_new_tab() {
  let blobDataFromLoadedPage = await openInNewTabAndReturnContent(
    "blob-url-referrer-link"
  );
  is(
    blobDataFromLoadedPage,
    blobDataAsString,
    "The blobURL is correctly loaded"
  );
});