summaryrefslogtreecommitdiffstats
path: root/browser/components/extensions/test/browser/browser_ext_optionsPage_popups.js
blob: 168a9c11b506f51f39606212f5171087b6f0c472 (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
238
239
240
241
242
243
244
245
246
247
248
249
/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
/* vim: set sts=2 sw=2 et tw=80: */
"use strict";

async function openContextMenuInOptionsPage(optionsBrowser) {
  let contentAreaContextMenu = document.getElementById(
    "contentAreaContextMenu"
  );
  let popupShownPromise = BrowserTestUtils.waitForEvent(
    contentAreaContextMenu,
    "popupshown"
  );

  info("Trigger context menu in the extension options page");

  // Instead of BrowserTestUtils.synthesizeMouseAtCenter, we are dispatching a contextmenu
  // event directly on the target element to prevent intermittent failures on debug builds
  // (especially linux32-debug), see Bug 1519808 for a rationale.
  SpecialPowers.spawn(optionsBrowser, [], () => {
    let el = content.document.querySelector("a");
    el.dispatchEvent(
      new content.MouseEvent("contextmenu", {
        bubbles: true,
        cancelable: true,
        view: el.ownerGlobal,
      })
    );
  });

  info("Wait the context menu to be shown");
  await popupShownPromise;

  return contentAreaContextMenu;
}

async function contextMenuClosed(contextMenu) {
  info("Wait context menu popup to be closed");
  await closeContextMenu(contextMenu);
  is(contextMenu.state, "closed", "The context menu popup has been closed");
}

add_task(async function test_tab_options_popups() {
  async function backgroundScript() {
    browser.menus.onShown.addListener(info => {
      browser.test.sendMessage("extension-menus-onShown", info);
    });

    await browser.menus.create({
      id: "sidebaronly",
      title: "sidebaronly",
      viewTypes: ["sidebar"],
    });
    await browser.menus.create({
      id: "tabonly",
      title: "tabonly",
      viewTypes: ["tab"],
    });
    await browser.menus.create({ id: "anypage", title: "anypage" });

    browser.runtime.openOptionsPage();
  }

  function optionsScript() {
    browser.test.sendMessage("options-page:loaded", document.documentURI);
  }

  let extension = ExtensionTestUtils.loadExtension({
    useAddonManager: "temporary",

    manifest: {
      permissions: ["tabs", "menus"],
      options_ui: {
        page: "options.html",
      },
    },
    files: {
      "options.html": `<!DOCTYPE html>
        <html>
          <head>
            <meta charset="utf-8">
            <script src="options.js" type="text/javascript"></script>
          </head>
          <body style="height: 100px;">
            <h1>Extensions Options</h1>
            <a href="http://mochi.test:8888/">options page link</a>
          </body>
        </html>`,
      "options.js": optionsScript,
    },
    background: backgroundScript,
  });

  const aboutAddonsTab = await BrowserTestUtils.openNewForegroundTab(
    gBrowser,
    "about:addons"
  );

  await extension.startup();

  const pageUrl = await extension.awaitMessage("options-page:loaded");

  const optionsBrowser = getInlineOptionsBrowser(gBrowser.selectedBrowser);

  const contentAreaContextMenu = await openContextMenuInOptionsPage(
    optionsBrowser
  );

  let contextMenuItemIds = [
    "context-openlinkintab",
    "context-openlinkprivate",
    "context-copylink",
  ];

  // Test that the "open link in container" menu is available if the containers are enabled
  // (which is the default on Nightly, but not on Beta).
  if (Services.prefs.getBoolPref("privacy.userContext.enabled")) {
    contextMenuItemIds.push("context-openlinkinusercontext-menu");
  }

  for (const itemID of contextMenuItemIds) {
    const item = contentAreaContextMenu.querySelector(`#${itemID}`);

    ok(!item.hidden, `${itemID} should not be hidden`);
    ok(!item.disabled, `${itemID} should not be disabled`);
  }

  const menuDetails = await extension.awaitMessage("extension-menus-onShown");

  isnot(
    menuDetails.targetElementId,
    undefined,
    "Got a targetElementId in the menu details"
  );
  delete menuDetails.targetElementId;

  Assert.deepEqual(
    menuDetails,
    {
      menuIds: ["anypage"],
      contexts: ["link", "all"],
      viewType: undefined,
      frameId: 0,
      editable: false,
      linkText: "options page link",
      linkUrl: "http://mochi.test:8888/",
      pageUrl,
    },
    "Got the expected menu details from menus.onShown"
  );

  await contextMenuClosed(contentAreaContextMenu);

  BrowserTestUtils.removeTab(aboutAddonsTab);

  await extension.unload();
});

add_task(async function overrideContext_in_options_page() {
  function optionsScript() {
    document.addEventListener(
      "contextmenu",
      () => {
        browser.menus.overrideContext({});
        browser.test.sendMessage("contextmenu-overridden");
      },
      { once: true }
    );
    browser.test.sendMessage("options-page:loaded");
  }

  let extension = ExtensionTestUtils.loadExtension({
    useAddonManager: "temporary",
    manifest: {
      permissions: ["tabs", "menus", "menus.overrideContext"],
      options_ui: {
        page: "options.html",
      },
    },
    files: {
      "options.html": `<!DOCTYPE html>
        <html>
          <head>
            <meta charset="utf-8">
            <script src="options.js" type="text/javascript"></script>
          </head>
          <body style="height: 100px;">
            <h1>Extensions Options</h1>
            <a href="http://mochi.test:8888/">options page link</a>
          </body>
        </html>`,
      "options.js": optionsScript,
    },
    async background() {
      // Expected to match and be shown.
      await new Promise(resolve => {
        browser.menus.create({ id: "bg_1_1", title: "bg_1_1" });
        browser.menus.create({ id: "bg_1_2", title: "bg_1_2" });
        // Expected to not match and be hidden.
        browser.menus.create(
          {
            id: "bg_1_3",
            title: "bg_1_3",
            targetUrlPatterns: ["*://nomatch/*"],
          },
          // menus.create returns a number and gets a callback, the order
          // is deterministic and so we just need to wait for the last one.
          resolve
        );
      });
      browser.runtime.openOptionsPage();
    },
  });

  const aboutAddonsTab = await BrowserTestUtils.openNewForegroundTab(
    gBrowser,
    "about:addons"
  );

  await extension.startup();
  await extension.awaitMessage("options-page:loaded");

  const optionsBrowser = getInlineOptionsBrowser(gBrowser.selectedBrowser);
  const contentAreaContextMenu = await openContextMenuInOptionsPage(
    optionsBrowser
  );

  await extension.awaitMessage("contextmenu-overridden");

  const allVisibleMenuItems = Array.from(contentAreaContextMenu.children)
    .filter(elem => {
      return !elem.hidden;
    })
    .map(elem => elem.id);

  Assert.deepEqual(
    allVisibleMenuItems,
    [
      `${makeWidgetId(extension.id)}-menuitem-_bg_1_1`,
      `${makeWidgetId(extension.id)}-menuitem-_bg_1_2`,
    ],
    "Expected only extension menu items"
  );

  await contextMenuClosed(contentAreaContextMenu);

  BrowserTestUtils.removeTab(aboutAddonsTab);

  await extension.unload();
});