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

async function test_clickData({ manifest_version, persistent }) {
  const action = manifest_version < 3 ? "browser_action" : "action";
  const background = { scripts: ["background.js"] };

  if (persistent != null) {
    background.persistent = persistent;
  }

  let extension = ExtensionTestUtils.loadExtension({
    manifest: {
      manifest_version,
      [action]: {},
      background,
    },

    files: {
      "background.js": function backgroundScript() {
        function onClicked(tab, info) {
          let button = info.button;
          let modifiers = info.modifiers;
          browser.test.sendMessage("onClick", { button, modifiers });
        }

        const apiNS =
          browser.runtime.getManifest().manifest_version >= 3
            ? "action"
            : "browserAction";

        browser[apiNS].onClicked.addListener(onClicked);
        browser.test.sendMessage("ready");
      },
    },
  });

  const map = {
    shiftKey: "Shift",
    altKey: "Alt",
    metaKey: "Command",
    ctrlKey: "Ctrl",
  };

  function assertSingleModifier(info, modifier, area) {
    if (modifier === "ctrlKey" && AppConstants.platform === "macosx") {
      is(
        info.modifiers.length,
        2,
        `MacCtrl modifier with control click on Mac`
      );
      is(
        info.modifiers[1],
        "MacCtrl",
        `MacCtrl modifier with control click on Mac`
      );
    } else {
      is(
        info.modifiers.length,
        1,
        `No unnecessary modifiers for exactly one key on event`
      );
    }

    is(info.modifiers[0], map[modifier], `Correct modifier on ${area} click`);
  }

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

  if (manifest_version >= 3 || persistent === false) {
    // NOTE: even in MV3 where the API namespace is technically "action",
    // the event listeners will be persisted into the startup data
    // with "browserAction" as the module, because that is the name
    // of the module shared by both MV2 browserAction and MV3 action APIs.
    assertPersistentListeners(extension, "browserAction", "onClicked", {
      primed: false,
    });
    await extension.terminateBackground();
    assertPersistentListeners(extension, "browserAction", "onClicked", {
      primed: true,
    });
  }

  for (let area of [CustomizableUI.AREA_NAVBAR, getCustomizableUIPanelID()]) {
    let widget = getBrowserActionWidget(extension);
    CustomizableUI.addWidgetToArea(widget.id, area);

    for (let modifier of Object.keys(map)) {
      for (let i = 0; i < 2; i++) {
        info(`test click with button ${i} modifier ${modifier}`);
        let clickEventData = { button: i };
        clickEventData[modifier] = true;
        await clickBrowserAction(extension, window, clickEventData);
        let details = await extension.awaitMessage("onClick");

        is(details.button, i, `Correct button in ${area} click`);
        assertSingleModifier(details, modifier, area);
      }

      info(`test keypress with modifier ${modifier}`);
      let keypressEventData = {};
      keypressEventData[modifier] = true;
      await triggerBrowserActionWithKeyboard(extension, " ", keypressEventData);
      let details = await extension.awaitMessage("onClick");

      is(details.button, 0, `Key command emulates left click`);
      assertSingleModifier(details, modifier, area);
    }
  }

  if (manifest_version >= 3 || persistent === false) {
    // The background event page is expected to have been
    // spawned again to handle the action onClicked event.
    await extension.awaitMessage("ready");
  }

  await extension.unload();
}

async function test_clickData_reset({ manifest_version }) {
  const action = manifest_version < 3 ? "browser_action" : "action";
  const browser_action_command =
    manifest_version < 3 ? "_execute_browser_action" : "_execute_action";
  const browser_action_key = "j";
  let extension = ExtensionTestUtils.loadExtension({
    manifest: {
      manifest_version,
      [action]: {
        default_area: "navbar",
      },
      page_action: {},
      commands: {
        [browser_action_command]: {
          suggested_key: {
            default: "Alt+Shift+J",
          },
        },
      },
    },

    async background() {
      function onBrowserActionClicked(tab, info) {
        browser.test.sendMessage("onClick", info);
      }

      function onPageActionClicked(tab, info) {
        browser.test.sendMessage("open-popup");
      }

      const { manifest_version } = browser.runtime.getManifest();
      const apiNS = manifest_version >= 3 ? "action" : "browserAction";

      browser[apiNS].onClicked.addListener(onBrowserActionClicked);

      // pageAction should only be available in MV2 extensions.
      if (manifest_version < 3) {
        browser.pageAction.onClicked.addListener(onPageActionClicked);

        let [tab] = await browser.tabs.query({
          active: true,
          currentWindow: true,
        });
        await browser.pageAction.show(tab.id);
      }

      browser.test.sendMessage("ready");
    },
  });

  // Pollute the state of the browserAction's lastClickInfo
  async function clickBrowserActionWithModifiers() {
    await clickBrowserAction(extension, window, { button: 1, shiftKey: true });
    let info = await extension.awaitMessage("onClick");
    is(info.button, 1, "Got expected ClickData button details");
    is(info.modifiers[0], "Shift", "Got expected ClickData modifiers details");
  }

  function assertInfoReset(info) {
    is(info.button, 0, `ClickData button reset properly`);
    is(info.modifiers.length, 0, `ClickData modifiers reset properly`);
  }

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

  if (manifest_version >= 3) {
    // NOTE: even in MV3 where the API namespace is technically "action",
    // the event listeners will be persisted into the startup data
    // with "browserAction" as the module, because that is the name
    // of the module shared by both MV2 browserAction and MV3 action APIs.
    assertPersistentListeners(extension, "browserAction", "onClicked", {
      primed: false,
    });
    await extension.terminateBackground();
    assertPersistentListeners(extension, "browserAction", "onClicked", {
      primed: true,
    });
  }

  await clickBrowserActionWithModifiers();

  if (manifest_version >= 3) {
    // The background event page is expected to have been
    // spawned again to handle the action onClicked event.
    await extension.awaitMessage("ready");
  } else {
    extension.onMessage("open-popup", () => {
      EventUtils.synthesizeKey(browser_action_key, {
        altKey: true,
        shiftKey: true,
      });
    });

    // pageAction should only be available in MV2 extensions.
    await clickPageAction(extension);

    // NOTE: the pageAction event listener then triggers browserAction.onClicked
    assertInfoReset(await extension.awaitMessage("onClick"));
  }

  await clickBrowserActionWithModifiers();

  await triggerBrowserActionWithKeyboard(extension, " ");
  assertInfoReset(await extension.awaitMessage("onClick"));

  await clickBrowserActionWithModifiers();

  await triggerBrowserActionWithKeyboard(extension, " ");
  assertInfoReset(await extension.awaitMessage("onClick"));

  await extension.unload();
}

add_task(function test_clickData_MV2() {
  return test_clickData({ manifest_version: 2 });
});

add_task(async function test_clickData_MV2_eventpage() {
  await SpecialPowers.pushPrefEnv({
    set: [["extensions.eventPages.enabled", true]],
  });
  await test_clickData({
    manifest_version: 2,
    persistent: false,
  });
  await SpecialPowers.popPrefEnv();
});

add_task(async function test_clickData_MV3() {
  await SpecialPowers.pushPrefEnv({
    set: [["extensions.manifestV3.enabled", true]],
  });
  await test_clickData({ manifest_version: 3 });
  await SpecialPowers.popPrefEnv();
});

add_task(function test_clickData_reset_MV2() {
  return test_clickData_reset({ manifest_version: 2 });
});

add_task(async function test_clickData_reset_MV3() {
  await SpecialPowers.pushPrefEnv({
    set: [["extensions.manifestV3.enabled", true]],
  });
  await test_clickData_reset({ manifest_version: 3 });
  await SpecialPowers.popPrefEnv();
});