summaryrefslogtreecommitdiffstats
path: root/browser/components/extensions/test/browser/browser_ext_menus.js
blob: 76ac3cf045cafe0b48e387ba27b2fed090f7f83f (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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
"use strict";

const PAGE =
  "http://mochi.test:8888/browser/browser/components/extensions/test/browser/context.html";

add_task(async function test_permissions() {
  function background() {
    browser.test.sendMessage("apis", {
      menus: typeof browser.menus,
      contextMenus: typeof browser.contextMenus,
      menusInternal: typeof browser.menusInternal,
    });
  }

  const first = ExtensionTestUtils.loadExtension({
    manifest: { permissions: ["menus"] },
    background,
  });
  const second = ExtensionTestUtils.loadExtension({
    manifest: { permissions: ["contextMenus"] },
    background,
  });

  await first.startup();
  await second.startup();

  const apis1 = await first.awaitMessage("apis");
  const apis2 = await second.awaitMessage("apis");

  is(apis1.menus, "object", "browser.menus available with 'menus' permission");
  is(
    apis1.contextMenus,
    "undefined",
    "browser.contextMenus unavailable with  'menus' permission"
  );
  is(
    apis1.menusInternal,
    "undefined",
    "browser.menusInternal is never available"
  );

  is(
    apis2.menus,
    "undefined",
    "browser.menus unavailable with 'contextMenus' permission"
  );
  is(
    apis2.contextMenus,
    "object",
    "browser.contextMenus unavailable with  'contextMenus' permission"
  );
  is(
    apis2.menusInternal,
    "undefined",
    "browser.menusInternal is never available"
  );

  await first.unload();
  await second.unload();
});

add_task(async function test_actionContextMenus() {
  const manifest = {
    page_action: {},
    browser_action: {
      default_area: "navbar",
    },
    permissions: ["menus"],
  };

  async function background() {
    const contexts = ["page_action", "browser_action"];

    const parentId = browser.menus.create({ contexts, title: "parent" });
    browser.menus.create({ parentId, title: "click A" });
    browser.menus.create({ parentId, title: "click B" });

    for (let i = 1; i < 9; i++) {
      browser.menus.create({ contexts, id: `${i}`, title: `click ${i}` });
    }

    browser.menus.onClicked.addListener((info, tab) => {
      browser.test.sendMessage("click", { info, tab });
    });

    const [tab] = await browser.tabs.query({ active: true });
    await browser.pageAction.show(tab.id);
    browser.test.sendMessage("ready", tab.id);
  }

  const extension = ExtensionTestUtils.loadExtension({ manifest, background });
  const tab = await BrowserTestUtils.openNewForegroundTab(
    gBrowser,
    "http://example.com/"
  );

  await extension.startup();
  const tabId = await extension.awaitMessage("ready");

  for (const kind of ["page", "browser"]) {
    const menu = await openActionContextMenu(extension, kind);
    const [submenu, second, , , , last, separator] = menu.children;

    is(submenu.tagName, "menu", "Correct submenu type");
    is(submenu.label, "parent", "Correct submenu title");

    const popup = await openSubmenu(submenu);
    is(popup, submenu.menupopup, "Correct submenu opened");
    is(popup.children.length, 2, "Correct number of submenu items");

    let idPrefix = `${makeWidgetId(extension.id)}-menuitem-_`;

    is(second.tagName, "menuitem", "Second menu item type is correct");
    is(second.label, "click 1", "Second menu item title is correct");
    is(second.id, `${idPrefix}1`, "Second menu item id is correct");

    is(last.tagName, "menu", "Last menu item type is correct");
    is(last.label, "Generated extension", "Last menu item title is correct");
    is(
      last.getAttribute("ext-type"),
      "top-level-menu",
      "Last menu ext-type is correct"
    );
    is(separator.tagName, "menuseparator", "Separator after last menu item");

    // Verify that menu items exceeding ACTION_MENU_TOP_LEVEL_LIMIT are moved into a submenu.
    let overflowPopup = await openSubmenu(last);
    is(
      overflowPopup.children.length,
      4,
      "Excess items should be moved into a submenu"
    );
    is(
      overflowPopup.firstElementChild.id,
      `${idPrefix}5`,
      "First submenu item ID is correct"
    );
    is(
      overflowPopup.lastElementChild.id,
      `${idPrefix}8`,
      "Last submenu item ID is correct"
    );

    await closeActionContextMenu(overflowPopup.firstElementChild, kind);
    const { info, tab } = await extension.awaitMessage("click");
    is(info.pageUrl, "http://example.com/", "Click info pageUrl is correct");
    is(tab.id, tabId, "Click event tab ID is correct");
  }

  BrowserTestUtils.removeTab(tab);
  await extension.unload();
});

add_task(async function test_bookmarkContextMenu() {
  const ext = ExtensionTestUtils.loadExtension({
    manifest: {
      permissions: ["menus", "bookmarks"],
    },
    background() {
      browser.menus.onShown.addListener(() => {
        browser.test.sendMessage("hello");
      });
      browser.menus.create({ title: "blarg", contexts: ["bookmark"] }, () => {
        browser.test.sendMessage("ready");
      });
    },
  });

  await ext.startup();
  await ext.awaitMessage("ready");
  await toggleBookmarksToolbar(true);

  let menu = await openChromeContextMenu(
    "placesContext",
    "#PlacesToolbarItems .bookmark-item"
  );
  let children = Array.from(menu.children);
  let item = children[children.length - 1];
  is(item.label, "blarg", "Menu item label is correct");
  await ext.awaitMessage("hello"); // onShown listener fired

  closeChromeContextMenu("placesContext", item);
  await ext.unload();
  await toggleBookmarksToolbar(false);
});

add_task(async function test_tabContextMenu() {
  const first = ExtensionTestUtils.loadExtension({
    manifest: {
      permissions: ["menus"],
    },
    async background() {
      browser.menus.create({
        id: "alpha-beta-parent",
        title: "alpha-beta parent",
        contexts: ["tab"],
      });

      browser.menus.create({ parentId: "alpha-beta-parent", title: "alpha" });
      browser.menus.create({ parentId: "alpha-beta-parent", title: "beta" });

      browser.menus.create({ title: "dummy", contexts: ["page"] });

      browser.menus.onClicked.addListener((info, tab) => {
        browser.test.sendMessage("click", { info, tab });
      });

      const [tab] = await browser.tabs.query({ active: true });
      browser.test.sendMessage("ready", tab.id);
    },
  });

  const second = ExtensionTestUtils.loadExtension({
    manifest: {
      permissions: ["menus"],
    },
    background() {
      browser.menus.create({
        title: "invisible",
        contexts: ["tab"],
        documentUrlPatterns: ["http://does/not/match"],
      });
      browser.menus.create(
        {
          title: "gamma",
          contexts: ["tab"],
          documentUrlPatterns: ["http://example.com/"],
        },
        () => {
          browser.test.sendMessage("ready");
        }
      );
    },
  });

  const tab = await BrowserTestUtils.openNewForegroundTab(
    gBrowser,
    "http://example.com/"
  );
  await first.startup();
  await second.startup();

  const tabId = await first.awaitMessage("ready");
  await second.awaitMessage("ready");

  const menu = await openTabContextMenu();
  const [separator, submenu, gamma] = Array.from(menu.children).slice(-3);
  is(
    separator.tagName,
    "menuseparator",
    "Separator before first extension item"
  );

  is(submenu.tagName, "menu", "Correct submenu type");
  is(submenu.label, "alpha-beta parent", "Correct submenu title");

  isnot(
    gamma.label,
    "dummy",
    "`page` context menu item should not appear here"
  );

  is(gamma.tagName, "menuitem", "Third menu item type is correct");
  is(gamma.label, "gamma", "Third menu item label is correct");

  const popup = await openSubmenu(submenu);
  is(popup, submenu.menupopup, "Correct submenu opened");
  is(popup.children.length, 2, "Correct number of submenu items");

  const [alpha, beta] = popup.children;
  is(alpha.tagName, "menuitem", "First menu item type is correct");
  is(alpha.label, "alpha", "First menu item label is correct");
  is(beta.tagName, "menuitem", "Second menu item type is correct");
  is(beta.label, "beta", "Second menu item label is correct");

  await closeTabContextMenu(beta);
  const click = await first.awaitMessage("click");
  is(
    click.info.pageUrl,
    "http://example.com/",
    "Click info pageUrl is correct"
  );
  is(click.tab.id, tabId, "Click event tab ID is correct");
  is(click.info.frameId, undefined, "no frameId on chrome");

  BrowserTestUtils.removeTab(tab);
  await first.unload();
  await second.unload();
});

add_task(async function test_onclick_frameid() {
  const manifest = {
    permissions: ["menus"],
  };

  function background() {
    function onclick(info) {
      browser.test.sendMessage("click", info);
    }
    browser.menus.create(
      { contexts: ["frame", "page"], title: "modify", onclick },
      () => {
        browser.test.sendMessage("ready");
      }
    );
  }

  const extension = ExtensionTestUtils.loadExtension({ manifest, background });
  const tab = await BrowserTestUtils.openNewForegroundTab(gBrowser, PAGE);

  await extension.startup();
  await extension.awaitMessage("ready");

  async function click(menu) {
    const items = menu.getElementsByAttribute("label", "modify");
    is(items.length, 1, "found menu item");
    await closeExtensionContextMenu(items[0]);
    return extension.awaitMessage("click");
  }

  let info = await click(await openContextMenu("body"));
  is(info.frameId, 0, "top level click");
  info = await click(await openContextMenuInFrame());
  isnot(info.frameId, undefined, "frame click, frameId is not undefined");
  isnot(info.frameId, 0, "frame click, frameId probably okay");

  BrowserTestUtils.removeTab(tab);
  await extension.unload();
});

add_task(async function test_multiple_contexts_init() {
  const manifest = {
    permissions: ["menus"],
  };

  function background() {
    browser.menus.create({ id: "parent", title: "parent" }, () => {
      browser.tabs.create({ url: "tab.html", active: false });
    });
  }

  const files = {
    "tab.html":
      "<!DOCTYPE html><meta charset=utf-8><script src=tab.js></script>",
    "tab.js": function () {
      browser.menus.onClicked.addListener(info => {
        browser.test.sendMessage("click", info);
      });
      browser.menus.create(
        { parentId: "parent", id: "child", title: "child" },
        () => {
          browser.test.sendMessage("ready");
        }
      );
    },
  };

  const tab = await BrowserTestUtils.openNewForegroundTab(gBrowser, PAGE);
  const extension = ExtensionTestUtils.loadExtension({
    manifest,
    background,
    files,
  });

  await extension.startup();
  await extension.awaitMessage("ready");

  const menu = await openContextMenu();
  const items = menu.getElementsByAttribute("label", "parent");

  is(items.length, 1, "Found parent menu item");
  is(items[0].tagName, "menu", "And it has children");

  const popup = await openSubmenu(items[0]);
  is(popup.firstElementChild.label, "child", "Correct child menu item");
  await closeExtensionContextMenu(popup.firstElementChild);

  const info = await extension.awaitMessage("click");
  is(info.menuItemId, "child", "onClicked the correct item");

  BrowserTestUtils.removeTab(tab);
  await extension.unload();
});

add_task(async function test_tools_menu() {
  const first = ExtensionTestUtils.loadExtension({
    manifest: {
      permissions: ["menus"],
    },
    background() {
      browser.menus.create({ title: "alpha", contexts: ["tools_menu"] });
      browser.menus.create({ title: "beta", contexts: ["tools_menu"] }, () => {
        browser.test.sendMessage("ready");
      });
    },
  });

  const second = ExtensionTestUtils.loadExtension({
    manifest: {
      permissions: ["menus"],
    },
    async background() {
      browser.menus.create({ title: "gamma", contexts: ["tools_menu"] });
      browser.menus.onClicked.addListener((info, tab) => {
        browser.test.sendMessage("click", { info, tab });
      });

      const [tab] = await browser.tabs.query({ active: true });
      browser.test.sendMessage("ready", tab.id);
    },
  });

  const tab = await BrowserTestUtils.openNewForegroundTab(
    gBrowser,
    "http://example.com/"
  );
  await first.startup();
  await second.startup();

  await first.awaitMessage("ready");
  const tabId = await second.awaitMessage("ready");
  const menu = await openToolsMenu();

  const [separator, submenu, gamma] = Array.from(menu.children).slice(-3);
  is(
    separator.tagName,
    "menuseparator",
    "Separator before first extension item"
  );

  is(submenu.tagName, "menu", "Correct submenu type");
  is(
    submenu.getAttribute("label"),
    "Generated extension",
    "Correct submenu title"
  );
  is(submenu.menupopup.children.length, 2, "Correct number of submenu items");

  is(gamma.tagName, "menuitem", "Third menu item type is correct");
  is(gamma.getAttribute("label"), "gamma", "Third menu item label is correct");

  closeToolsMenu(gamma);

  const click = await second.awaitMessage("click");
  is(
    click.info.pageUrl,
    "http://example.com/",
    "Click info pageUrl is correct"
  );
  is(click.tab.id, tabId, "Click event tab ID is correct");

  BrowserTestUtils.removeTab(tab);
  await first.unload();
  await second.unload();
});