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

add_task(async function testTabSwitchActionContext() {
  await SpecialPowers.pushPrefEnv({
    set: [["extensions.manifestV3.enabled", true]],
  });
});

add_task(async function test_actions_context_menu() {
  function background() {
    browser.contextMenus.create({
      title: "open_browser_action",
      contexts: ["all"],
      command: "_execute_browser_action",
    });
    browser.contextMenus.create({
      title: "open_page_action",
      contexts: ["all"],
      command: "_execute_page_action",
    });
    browser.contextMenus.create({
      title: "open_sidebar_action",
      contexts: ["all"],
      command: "_execute_sidebar_action",
    });
    browser.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
      browser.pageAction.show(tabId);
    });
    browser.contextMenus.onClicked.addListener(() => {
      browser.test.fail(`menu onClicked should not have been received`);
    });
    browser.test.sendMessage("ready");
  }

  function testScript() {
    window.onload = () => {
      browser.test.sendMessage("test-opened", true);
    };
  }

  let extension = ExtensionTestUtils.loadExtension({
    manifest: {
      name: "contextMenus commands",
      permissions: ["contextMenus", "activeTab", "tabs"],
      browser_action: {
        default_title: "Test BrowserAction",
        default_popup: "test.html",
        browser_style: true,
      },
      page_action: {
        default_title: "Test PageAction",
        default_popup: "test.html",
        browser_style: true,
      },
      sidebar_action: {
        default_title: "Test Sidebar",
        default_panel: "test.html",
      },
    },
    background,
    files: {
      "test.html": `<!DOCTYPE html><meta charset="utf-8"><script src="test.js"></script>`,
      "test.js": testScript,
    },
  });

  async function testContext(id) {
    const menu = await openExtensionContextMenu();
    const items = menu.getElementsByAttribute("label", id);
    is(items.length, 1, `exactly one menu item found`);
    await closeExtensionContextMenu(items[0]);
    return extension.awaitMessage("test-opened");
  }

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

  // open a page so page action works
  const PAGE =
    "http://mochi.test:8888/browser/browser/components/extensions/test/browser/context.html?test=commands";
  const tab = await BrowserTestUtils.openNewForegroundTab(gBrowser, PAGE);

  ok(
    await testContext("open_sidebar_action"),
    "_execute_sidebar_action worked"
  );
  ok(
    await testContext("open_browser_action"),
    "_execute_browser_action worked"
  );
  ok(await testContext("open_page_action"), "_execute_page_action worked");

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

add_task(async function test_v3_action_context_menu() {
  let extension = ExtensionTestUtils.loadExtension({
    manifest: {
      name: "contextMenus commands",
      manifest_version: 3,
      permissions: ["contextMenus"],
      action: {
        default_title: "Test Action",
        default_popup: "test.html",
        // TODO bug 1830712: Remove this. Probably not even needed for the test.
        browser_style: true,
      },
    },
    background() {
      browser.contextMenus.onClicked.addListener(() => {
        browser.test.fail(`menu onClicked should not have been received`);
      });

      browser.contextMenus.create(
        {
          id: "open_action",
          title: "open_action",
          contexts: ["all"],
          command: "_execute_action",
        },
        () => {
          browser.test.sendMessage("ready");
        }
      );
    },
    files: {
      "test.html": `<!DOCTYPE html><meta charset="utf-8"><script src="test.js"></script>`,
      "test.js": () => {
        window.onload = () => {
          browser.test.sendMessage("test-opened", true);
        };
      },
    },
  });

  async function testContext(id) {
    const menu = await openContextMenu();
    const items = menu.getElementsByAttribute("label", id);
    is(items.length, 1, `exactly one menu item found`);
    await closeExtensionContextMenu(items[0]);
    return extension.awaitMessage("test-opened");
  }

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

  const PAGE =
    "http://mochi.test:8888/browser/browser/components/extensions/test/browser/context.html?test=commands";
  const tab = await BrowserTestUtils.openNewForegroundTab(gBrowser, PAGE);

  ok(await testContext("open_action"), "_execute_action worked");

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