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

// Opens two tabs at the start of the tab strip and focuses the second tab.
// Then an extension menu is registered for the "tab" context and a menu is
// opened on the first tab and the extension menu item is clicked.
// This triggers the onTabMenuClicked handler.
async function openTwoTabsAndOpenTabMenu(onTabMenuClicked) {
  const PAGE_URL =
    "http://mochi.test:8888/browser/browser/components/extensions/test/browser/context.html";
  const OTHER_URL =
    "http://127.0.0.1:8888/browser/browser/components/extensions/test/browser/context.html";

  let tab1 = await BrowserTestUtils.openNewForegroundTab(gBrowser, PAGE_URL);
  let tab2 = await BrowserTestUtils.openNewForegroundTab(gBrowser, OTHER_URL);
  // Move the first tab to the start so that it can be found by the .tabbrowser-tab selector below.
  gBrowser.moveTabTo(tab1, 0);
  gBrowser.moveTabTo(tab2, 1);

  async function background(onTabMenuClicked) {
    browser.menus.onClicked.addListener(async (info, tab) => {
      await onTabMenuClicked(info, tab);
      browser.test.sendMessage("onCommand_on_tab_click");
    });

    browser.menus.create(
      {
        title: "menu item on tab",
        contexts: ["tab"],
      },
      () => {
        browser.test.sendMessage("ready");
      }
    );
  }
  let extension = ExtensionTestUtils.loadExtension({
    manifest: {
      permissions: ["menus", "activeTab"],
    },
    background: `(${background})(${onTabMenuClicked})`,
  });

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

  // Focus a selected tab to to make tabbrowser.js to load localization files,
  // and thereby initialize document.l10n property.
  gBrowser.selectedTab.focus();

  // The .tabbrowser-tab selector matches the first tab (tab1).
  let menu = await openChromeContextMenu(
    "tabContextMenu",
    ".tabbrowser-tab",
    window
  );
  let menuItem = menu.getElementsByAttribute("label", "menu item on tab")[0];
  await closeTabContextMenu(menuItem);
  await extension.awaitMessage("onCommand_on_tab_click");

  await extension.unload();

  BrowserTestUtils.removeTab(tab1);
  BrowserTestUtils.removeTab(tab2);
}

add_task(async function activeTabForTabMenu() {
  await openTwoTabsAndOpenTabMenu(async function onTabMenuClicked(info, tab) {
    browser.test.assertEq(0, tab.index, "Expected a menu on the first tab.");

    try {
      let [actualUrl] = await browser.tabs.executeScript(tab.id, {
        code: "document.URL",
      });
      browser.test.assertEq(
        tab.url,
        actualUrl,
        "Content script to execute in the first tab"
      );
      // (the activeTab permission should have been granted to the first tab.)
    } catch (e) {
      browser.test.fail(
        `Unexpected error in executeScript: ${e} :: ${e.stack}`
      );
    }
  });
});

add_task(async function noActiveTabForCurrentTab() {
  await openTwoTabsAndOpenTabMenu(async function onTabMenuClicked(info, tab) {
    const PAGE_URL =
      "http://mochi.test:8888/browser/browser/components/extensions/test/browser/context.html";
    browser.test.assertEq(0, tab.index, "Expected a menu on the first tab.");
    browser.test.assertEq(
      PAGE_URL,
      tab.url,
      "Expected tab.url to be available for the first tab"
    );

    let [tab2] = await browser.tabs.query({ windowId: tab.windowId, index: 1 });
    browser.test.assertTrue(tab2.active, "The second tab should be focused.");
    browser.test.assertEq(
      undefined,
      tab2.url,
      "Expected tab.url to be unavailable for the second tab."
    );

    await browser.test.assertRejects(
      browser.tabs.executeScript(tab2.id, { code: "document.URL" }),
      /Missing host permission for the tab/,
      "Content script should not run in the second tab"
    );
    // (The activeTab permission was granted to the first tab, not tab2.)
  });
});