summaryrefslogtreecommitdiffstats
path: root/browser/components/extensions/test/browser/browser_ext_webNavigation_onCreatedNavigationTarget_contextmenu.js
blob: 8fd94af4f1222ca2bf708989bc01dba5fff563ba (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
/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
/* vim: set sts=2 sw=2 et tw=80: */
"use strict";

loadTestSubscript("head_webNavigation.js");

SpecialPowers.pushPrefEnv({
  set: [["security.allow_eval_with_system_principal", true]],
});

async function clickContextMenuItem({
  pageElementSelector,
  contextMenuItemLabel,
  frameIndex,
}) {
  let contentAreaContextMenu;
  if (frameIndex == null) {
    contentAreaContextMenu = await openContextMenu(pageElementSelector);
  } else {
    contentAreaContextMenu = await openContextMenuInFrame(
      pageElementSelector,
      frameIndex
    );
  }
  const item = contentAreaContextMenu.getElementsByAttribute(
    "label",
    contextMenuItemLabel
  );
  is(item.length, 1, `found contextMenu item for "${contextMenuItemLabel}"`);
  const closed = promiseContextMenuClosed(contentAreaContextMenu);
  contentAreaContextMenu.activateItem(item[0]);
  await closed;
}

async function background() {
  const tabs = await browser.tabs.query({ active: true, currentWindow: true });
  const sourceTabId = tabs[0].id;

  const sourceTabFrames = await browser.webNavigation.getAllFrames({
    tabId: sourceTabId,
  });

  browser.webNavigation.onCreatedNavigationTarget.addListener(msg => {
    browser.test.sendMessage("webNavOnCreated", msg);
  });

  browser.webNavigation.onCompleted.addListener(async msg => {
    // NOTE: checking the url is currently necessary because of Bug 1252129
    // ( Filter out webNavigation events related to new window initialization phase).
    if (msg.tabId !== sourceTabId && msg.url !== "about:blank") {
      await browser.tabs.remove(msg.tabId);
      browser.test.sendMessage("webNavOnCompleted", msg);
    }
  });

  browser.tabs.onCreated.addListener(tab => {
    browser.test.sendMessage("tabsOnCreated", tab.id);
  });

  browser.test.sendMessage("expectedSourceTab", {
    sourceTabId,
    sourceTabFrames,
  });
}

add_task(async function test_on_created_navigation_target_from_context_menu() {
  const tab = await BrowserTestUtils.openNewForegroundTab(
    gBrowser,
    SOURCE_PAGE
  );

  const extension = ExtensionTestUtils.loadExtension({
    background,
    manifest: {
      permissions: ["webNavigation"],
    },
  });

  await extension.startup();

  const expectedSourceTab = await extension.awaitMessage("expectedSourceTab");

  info("Open link in a new tab from the context menu");

  await runCreatedNavigationTargetTest({
    extension,
    async openNavTarget() {
      await clickContextMenuItem({
        pageElementSelector: "#test-create-new-tab-from-context-menu",
        contextMenuItemLabel: "Open Link in New Tab",
      });
    },
    expectedWebNavProps: {
      sourceTabId: expectedSourceTab.sourceTabId,
      sourceFrameId: 0,
      url: `${OPENED_PAGE}#new-tab-from-context-menu`,
    },
  });

  info("Open link in a new window from the context menu");

  await runCreatedNavigationTargetTest({
    extension,
    async openNavTarget() {
      await clickContextMenuItem({
        pageElementSelector: "#test-create-new-window-from-context-menu",
        contextMenuItemLabel: "Open Link in New Window",
      });
    },
    expectedWebNavProps: {
      sourceTabId: expectedSourceTab.sourceTabId,
      sourceFrameId: 0,
      url: `${OPENED_PAGE}#new-window-from-context-menu`,
    },
  });

  BrowserTestUtils.removeTab(tab);

  await extension.unload();
});

add_task(
  async function test_on_created_navigation_target_from_context_menu_subframe() {
    const tab = await BrowserTestUtils.openNewForegroundTab(
      gBrowser,
      SOURCE_PAGE
    );

    const extension = ExtensionTestUtils.loadExtension({
      background,
      manifest: {
        permissions: ["webNavigation"],
      },
    });

    await extension.startup();

    const expectedSourceTab = await extension.awaitMessage("expectedSourceTab");

    info("Open a subframe link in a new tab from the context menu");

    await runCreatedNavigationTargetTest({
      extension,
      async openNavTarget() {
        await clickContextMenuItem({
          pageElementSelector:
            "#test-create-new-tab-from-context-menu-subframe",
          contextMenuItemLabel: "Open Link in New Tab",
          frameIndex: 0,
        });
      },
      expectedWebNavProps: {
        sourceTabId: expectedSourceTab.sourceTabId,
        sourceFrameId: expectedSourceTab.sourceTabFrames[1].frameId,
        url: `${OPENED_PAGE}#new-tab-from-context-menu-subframe`,
      },
    });

    info("Open a subframe link in a new window from the context menu");

    await runCreatedNavigationTargetTest({
      extension,
      async openNavTarget() {
        await clickContextMenuItem({
          pageElementSelector:
            "#test-create-new-window-from-context-menu-subframe",
          contextMenuItemLabel: "Open Link in New Window",
          frameIndex: 0,
        });
      },
      expectedWebNavProps: {
        sourceTabId: expectedSourceTab.sourceTabId,
        sourceFrameId: expectedSourceTab.sourceTabFrames[1].frameId,
        url: `${OPENED_PAGE}#new-window-from-context-menu-subframe`,
      },
    });

    BrowserTestUtils.removeTab(tab);

    await extension.unload();
  }
);