summaryrefslogtreecommitdiffstats
path: root/browser/base/content/test/contextMenu/browser_contextmenu_badiframe.js
blob: 89e7fe15e083c62d0f582f2c021d4e8c1b566069 (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
/* Tests for proper behaviour of "Show this frame" context menu options with a valid frame and
   a frame with an invalid url.
 */

// Two frames, one with text content, the other an error page
var invalidPage = "http://127.0.0.1:55555/";
// eslint-disable-next-line @microsoft/sdl/no-insecure-url
var validPage = "http://example.com/";
var testPage =
  'data:text/html,<frameset cols="400,400"><frame src="' +
  validPage +
  '"><frame src="' +
  invalidPage +
  '"></frameset>';

async function openTestPage() {
  let tab = await BrowserTestUtils.openNewForegroundTab(
    gBrowser,
    "about:blank",
    true,
    true
  );
  let browser = tab.linkedBrowser;

  // The test page has a top-level document and two subframes. One of
  // those subframes is an error page, which doesn't fire a load event.
  // We'll use BrowserTestUtils.browserLoaded and have it wait for all
  // 3 loads before resolving.
  let expectedLoads = 3;
  let pageAndIframesLoaded = BrowserTestUtils.browserLoaded(
    browser,
    true /* includeSubFrames */,
    url => {
      expectedLoads--;
      return !expectedLoads;
    },
    true /* maybeErrorPage */
  );
  BrowserTestUtils.loadURIString(browser, testPage);
  await pageAndIframesLoaded;

  // Make sure both the top-level document and the iframe documents have
  // had a chance to present. We need this so that the context menu event
  // gets dispatched properly.
  for (let bc of [
    ...browser.browsingContext.children,
    browser.browsingContext,
  ]) {
    await SpecialPowers.spawn(bc, [], async function () {
      await new Promise(resolve => {
        content.requestAnimationFrame(resolve);
      });
    });
  }
}

async function selectFromFrameMenu(frameNumber, menuId) {
  const contextMenu = document.getElementById("contentAreaContextMenu");

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

  await BrowserTestUtils.synthesizeMouseAtPoint(
    40,
    40,
    {
      type: "contextmenu",
      button: 2,
    },
    gBrowser.selectedBrowser.browsingContext.children[frameNumber]
  );

  await popupShownPromise;

  let frameItem = document.getElementById("frame");
  let framePopup = frameItem.menupopup;
  let subPopupShownPromise = BrowserTestUtils.waitForEvent(
    framePopup,
    "popupshown"
  );

  frameItem.openMenu(true);
  await subPopupShownPromise;

  let subPopupHiddenPromise = BrowserTestUtils.waitForEvent(
    framePopup,
    "popuphidden"
  );
  let contextMenuHiddenPromise = BrowserTestUtils.waitForEvent(
    contextMenu,
    "popuphidden"
  );
  contextMenu.activateItem(document.getElementById(menuId));
  await subPopupHiddenPromise;
  await contextMenuHiddenPromise;
}

add_task(async function testOpenFrame() {
  for (let frameNumber = 0; frameNumber < 2; frameNumber++) {
    await openTestPage();

    let expectedResultURI = [validPage, invalidPage][frameNumber];

    info("show only this frame for " + expectedResultURI);

    let browserLoadedPromise = BrowserTestUtils.browserLoaded(
      gBrowser.selectedBrowser,
      false,
      expectedResultURI,
      frameNumber == 1
    );

    await selectFromFrameMenu(frameNumber, "context-showonlythisframe");
    await browserLoadedPromise;

    is(
      gBrowser.selectedBrowser.currentURI.spec,
      expectedResultURI,
      "Should navigate to page url, not about:neterror"
    );

    gBrowser.removeCurrentTab();
  }
});

add_task(async function testOpenFrameInTab() {
  for (let frameNumber = 0; frameNumber < 2; frameNumber++) {
    await openTestPage();

    let expectedResultURI = [validPage, invalidPage][frameNumber];

    info("open frame in tab for " + expectedResultURI);

    let newTabPromise = BrowserTestUtils.waitForNewTab(
      gBrowser,
      expectedResultURI,
      false
    );
    await selectFromFrameMenu(frameNumber, "context-openframeintab");
    let newTab = await newTabPromise;

    await BrowserTestUtils.switchTab(gBrowser, newTab);

    // We should now have the error page in a new, active tab.
    is(
      gBrowser.selectedBrowser.currentURI.spec,
      expectedResultURI,
      "New tab should have page url, not about:neterror"
    );

    gBrowser.removeCurrentTab();
    gBrowser.removeCurrentTab();
  }
});

add_task(async function testOpenFrameInWindow() {
  for (let frameNumber = 0; frameNumber < 2; frameNumber++) {
    await openTestPage();

    let expectedResultURI = [validPage, invalidPage][frameNumber];

    info("open frame in window for " + expectedResultURI);

    let newWindowPromise = BrowserTestUtils.waitForNewWindow({
      url: frameNumber == 1 ? invalidPage : validPage,
      maybeErrorPage: frameNumber == 1,
    });
    await selectFromFrameMenu(frameNumber, "context-openframe");
    let newWindow = await newWindowPromise;

    is(
      newWindow.gBrowser.selectedBrowser.currentURI.spec,
      expectedResultURI,
      "New window should have page url, not about:neterror"
    );

    newWindow.close();
    gBrowser.removeCurrentTab();
  }
});