summaryrefslogtreecommitdiffstats
path: root/browser/components/extensions/test/browser/browser_ext_commands_execute_browser_action.js
blob: 526dfbbeebf653b5c22cd538f98d58e6456b6ac5 (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
/* -*- 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]],
  });
});

async function testExecuteBrowserActionWithOptions(options = {}) {
  // Make sure the mouse isn't hovering over the browserAction widget.
  EventUtils.synthesizeMouseAtCenter(
    gURLBar.textbox,
    { type: "mouseover" },
    window
  );

  let extensionOptions = {};

  let browser_action =
    options.manifest_version > 2 ? "action" : "browser_action";
  let browser_action_key = options.manifest_version > 2 ? "a" : "j";

  // We accept any command in the manifest, so here we add commands for
  // both V2 and V3, but only the command that matches the manifest version
  // should ever work.
  extensionOptions.manifest = {
    manifest_version: options.manifest_version || 2,
    commands: {
      _execute_browser_action: {
        suggested_key: {
          default: "Alt+Shift+J",
        },
      },
      _execute_action: {
        suggested_key: {
          default: "Alt+Shift+A",
        },
      },
    },
    [browser_action]: {
      browser_style: true,
    },
  };

  if (options.withPopup) {
    extensionOptions.manifest[browser_action].default_popup = "popup.html";

    extensionOptions.files = {
      "popup.html": `
        <!DOCTYPE html>
        <html>
          <head>
            <meta charset="utf-8">
            <script src="popup.js"></script>
          </head>
        </html>
      `,

      "popup.js": function () {
        browser.runtime.sendMessage("from-browser-action-popup");
      },
    };
  }

  extensionOptions.background = () => {
    let manifest = browser.runtime.getManifest();
    let { manifest_version } = manifest;
    const action = manifest_version < 3 ? "browserAction" : "action";

    browser.test.onMessage.addListener((message, options) => {
      browser.commands.onCommand.addListener(commandName => {
        if (
          ["_execute_browser_action", "_execute_action"].includes(commandName)
        ) {
          browser.test.assertEq(
            commandName,
            options.expectedCommand,
            `The onCommand listener fired for ${commandName}.`
          );
          browser[action].openPopup();
        }
      });

      if (!options.expectedCommand) {
        browser[action].onClicked.addListener(() => {
          if (options.withPopup) {
            browser.test.fail(
              "The onClick listener should never fire if the browserAction has a popup."
            );
            browser.test.notifyFail("execute-browser-action-on-clicked-fired");
          } else {
            browser.test.notifyPass("execute-browser-action-on-clicked-fired");
          }
        });
      }

      browser.runtime.onMessage.addListener(msg => {
        if (msg == "from-browser-action-popup") {
          browser.test.notifyPass("execute-browser-action-popup-opened");
        }
      });

      browser.test.sendMessage("send-keys");
    });
  };

  let extension = ExtensionTestUtils.loadExtension(extensionOptions);

  extension.onMessage("send-keys", () => {
    EventUtils.synthesizeKey(browser_action_key, {
      altKey: true,
      shiftKey: true,
    });
  });

  await extension.startup();

  await SimpleTest.promiseFocus(window);

  if (options.inArea) {
    let widget = getBrowserActionWidget(extension);
    CustomizableUI.addWidgetToArea(widget.id, options.inArea);
  }

  extension.sendMessage("options", options);

  if (options.withPopup) {
    await extension.awaitFinish("execute-browser-action-popup-opened");

    if (!getBrowserActionPopup(extension)) {
      await awaitExtensionPanel(extension);
    }
    await closeBrowserAction(extension);
  } else {
    await extension.awaitFinish("execute-browser-action-on-clicked-fired");
  }
  await extension.unload();
}

add_task(async function test_execute_browser_action_with_popup() {
  await testExecuteBrowserActionWithOptions({
    withPopup: true,
  });
});

add_task(async function test_execute_browser_action_without_popup() {
  await testExecuteBrowserActionWithOptions();
});

add_task(async function test_execute_browser_action_command() {
  await testExecuteBrowserActionWithOptions({
    withPopup: true,
    expectedCommand: "_execute_browser_action",
  });
});

add_task(async function test_execute_action_with_popup() {
  await testExecuteBrowserActionWithOptions({
    withPopup: true,
    manifest_version: 3,
  });
});

add_task(async function test_execute_action_without_popup() {
  await testExecuteBrowserActionWithOptions({
    manifest_version: 3,
  });
});

add_task(async function test_execute_action_command() {
  await testExecuteBrowserActionWithOptions({
    withPopup: true,
    expectedCommand: "_execute_action",
  });
});

add_task(
  async function test_execute_browser_action_in_hamburger_menu_with_popup() {
    await testExecuteBrowserActionWithOptions({
      withPopup: true,
      inArea: getCustomizableUIPanelID(),
    });
  }
);

add_task(
  async function test_execute_browser_action_in_hamburger_menu_without_popup() {
    await testExecuteBrowserActionWithOptions({
      inArea: getCustomizableUIPanelID(),
    });
  }
);