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

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

// Loaded both as a background script and a tab page.
function testScript() {
  let page = location.pathname.includes("tab.html") ? "tab" : "background";
  let clickCounts = {
    old: 0,
    new: 0,
  };
  browser.contextMenus.onClicked.addListener(() => {
    // Async to give other onclick handlers a chance to fire.
    setTimeout(() => {
      browser.test.sendMessage("onClicked-fired", page);
    });
  });
  browser.test.onMessage.addListener((toPage, msg) => {
    if (toPage !== page) {
      return;
    }
    browser.test.log(`Received ${msg} for ${toPage}`);
    if (msg == "get-click-counts") {
      browser.test.sendMessage("click-counts", clickCounts);
    } else if (msg == "clear-click-counts") {
      clickCounts.old = clickCounts.new = 0;
      browser.test.sendMessage("next");
    } else if (msg == "create-with-onclick") {
      browser.contextMenus.create(
        {
          id: "iden",
          title: "tifier",
          onclick() {
            ++clickCounts.old;
            browser.test.log(
              `onclick fired for original onclick property in ${page}`
            );
          },
        },
        () => browser.test.sendMessage("next")
      );
    } else if (msg == "create-without-onclick") {
      browser.contextMenus.create(
        {
          id: "iden",
          title: "tifier",
        },
        () => browser.test.sendMessage("next")
      );
    } else if (msg == "update-without-onclick") {
      browser.contextMenus.update(
        "iden",
        {
          enabled: true, // Already enabled, so this does nothing.
        },
        () => browser.test.sendMessage("next")
      );
    } else if (msg == "update-with-onclick") {
      browser.contextMenus.update(
        "iden",
        {
          onclick() {
            ++clickCounts.new;
            browser.test.log(
              `onclick fired for updated onclick property in ${page}`
            );
          },
        },
        () => browser.test.sendMessage("next")
      );
    } else if (msg == "remove") {
      browser.contextMenus.remove("iden", () =>
        browser.test.sendMessage("next")
      );
    } else if (msg == "removeAll") {
      browser.contextMenus.removeAll(() => browser.test.sendMessage("next"));
    }
  });

  if (page == "background") {
    browser.test.log("Opening tab.html");
    browser.tabs.create({
      url: "tab.html",
      active: false, // To not interfere with the context menu tests.
    });
  } else {
    // Sanity check - the pages must be in the same process.
    let pages = browser.extension.getViews();
    browser.test.assertTrue(
      pages.includes(window),
      "Expected this tab to be an extension view"
    );
    pages = pages.filter(w => w !== window);
    browser.test.assertEq(
      pages[0],
      browser.extension.getBackgroundPage(),
      "Expected the other page to be a background page"
    );
    browser.test.sendMessage("tab.html ready");
  }
}

add_task(async function () {
  let tab1 = await BrowserTestUtils.openNewForegroundTab(gBrowser, PAGE);

  gBrowser.selectedTab = tab1;

  let extension = ExtensionTestUtils.loadExtension({
    manifest: {
      permissions: ["contextMenus"],
    },
    background: testScript,
    files: {
      "tab.html": `<!DOCTYPE html><meta charset="utf-8"><script src="tab.js"></script>`,
      "tab.js": testScript,
    },
  });
  await extension.startup();
  await extension.awaitMessage("tab.html ready");

  async function clickContextMenu() {
    // Using openContextMenu instead of openExtensionContextMenu because the
    // test extension has only one context menu item.
    let extensionMenuRoot = await openContextMenu();
    let items = extensionMenuRoot.getElementsByAttribute("label", "tifier");
    is(items.length, 1, "Expected one context menu item");
    await closeExtensionContextMenu(items[0]);
    // One of them is "tab", the other is "background".
    info(`onClicked from: ${await extension.awaitMessage("onClicked-fired")}`);
    info(`onClicked from: ${await extension.awaitMessage("onClicked-fired")}`);
  }

  function getCounts(page) {
    extension.sendMessage(page, "get-click-counts");
    return extension.awaitMessage("click-counts");
  }
  async function resetCounts() {
    extension.sendMessage("tab", "clear-click-counts");
    extension.sendMessage("background", "clear-click-counts");
    await extension.awaitMessage("next");
    await extension.awaitMessage("next");
  }

  // During this test, at most one "onclick" attribute is expected at any time.
  for (let pageOne of ["background", "tab"]) {
    for (let pageTwo of ["background", "tab"]) {
      info(`Testing with menu created by ${pageOne} and updated by ${pageTwo}`);
      extension.sendMessage(pageOne, "create-with-onclick");
      await extension.awaitMessage("next");

      // Test that update without onclick attribute does not clear the existing
      // onclick handler.
      extension.sendMessage(pageTwo, "update-without-onclick");
      await extension.awaitMessage("next");
      await clickContextMenu();
      let clickCounts = await getCounts(pageOne);
      is(
        clickCounts.old,
        1,
        `Original onclick should still be present in ${pageOne}`
      );
      is(clickCounts.new, 0, `Not expecting any new handlers in ${pageOne}`);
      if (pageOne !== pageTwo) {
        clickCounts = await getCounts(pageTwo);
        is(clickCounts.old, 0, `Not expecting any handlers in ${pageTwo}`);
        is(clickCounts.new, 0, `Not expecting any new handlers in ${pageTwo}`);
      }
      await resetCounts();

      // Test that update with onclick handler in a different page clears the
      // existing handler and activates the new onclick handler.
      extension.sendMessage(pageTwo, "update-with-onclick");
      await extension.awaitMessage("next");
      await clickContextMenu();
      clickCounts = await getCounts(pageOne);
      is(clickCounts.old, 0, `Original onclick should be gone from ${pageOne}`);
      if (pageOne !== pageTwo) {
        is(
          clickCounts.new,
          0,
          `Still not expecting new handlers in ${pageOne}`
        );
      }
      clickCounts = await getCounts(pageTwo);
      if (pageOne !== pageTwo) {
        is(clickCounts.old, 0, `Not expecting an old onclick in ${pageTwo}`);
      }
      is(clickCounts.new, 1, `New onclick should be triggered in ${pageTwo}`);
      await resetCounts();

      // Test that updating the handler (different again from the last `update`
      // call, but the same as the `create` call) clears the existing handler
      // and activates the new onclick handler.
      extension.sendMessage(pageOne, "update-with-onclick");
      await extension.awaitMessage("next");
      await clickContextMenu();
      clickCounts = await getCounts(pageOne);
      is(clickCounts.new, 1, `onclick should be triggered in ${pageOne}`);
      if (pageOne !== pageTwo) {
        clickCounts = await getCounts(pageTwo);
        is(clickCounts.new, 0, `onclick should be gone from ${pageTwo}`);
      }
      await resetCounts();

      // Test that removing the context menu and recreating it with the same ID
      // (in a different context) does not leave behind any onclick handlers.
      extension.sendMessage(pageTwo, "remove");
      await extension.awaitMessage("next");
      extension.sendMessage(pageTwo, "create-without-onclick");
      await extension.awaitMessage("next");
      await clickContextMenu();
      clickCounts = await getCounts(pageOne);
      is(clickCounts.new, 0, `Did not expect any click handlers in ${pageOne}`);
      if (pageOne !== pageTwo) {
        clickCounts = await getCounts(pageTwo);
        is(
          clickCounts.new,
          0,
          `Did not expect any click handlers in ${pageTwo}`
        );
      }
      await resetCounts();

      // Remove context menu for the next iteration of the test. And just to get
      // more coverage, let's use removeAll instead of remove.
      extension.sendMessage(pageOne, "removeAll");
      await extension.awaitMessage("next");
    }
  }
  await extension.unload();
  BrowserTestUtils.removeTab(tab1);
});

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

  function background() {
    function onclick(info) {
      browser.test.sendMessage("click", info);
    }
    browser.contextMenus.create(
      { contexts: ["all"], 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(modifiers = {}) {
    const menu = await openContextMenu();
    const items = menu.getElementsByAttribute("label", "modify");
    is(items.length, 1, "Got exactly one context menu item");
    await closeExtensionContextMenu(items[0], modifiers);
    return extension.awaitMessage("click");
  }

  const plain = await click();
  is(plain.modifiers.length, 0, "modifiers array empty with a plain click");

  const shift = await click({ shiftKey: true });
  is(shift.modifiers.join(), "Shift", "Correct modifier: Shift");

  const ctrl = await click({ ctrlKey: true });
  if (AppConstants.platform !== "macosx") {
    is(ctrl.modifiers.join(), "Ctrl", "Correct modifier: Ctrl");
  } else {
    is(
      ctrl.modifiers.sort().join(),
      "Ctrl,MacCtrl",
      "Correct modifier: Ctrl (and MacCtrl)"
    );

    const meta = await click({ metaKey: true });
    is(meta.modifiers.join(), "Command", "Correct modifier: Command");
  }

  const altShift = await click({ altKey: true, shiftKey: true });
  is(
    altShift.modifiers.sort().join(),
    "Alt,Shift",
    "Correct modifiers: Shift+Alt"
  );

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