summaryrefslogtreecommitdiffstats
path: root/comm/mail/components/extensions/test/browser/head_menus.js
blob: 346c4ca044b0aa6d69711d97aa23d24a9c9d4ea0 (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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

/* globals synthesizeMouseAtCenterAndRetry, awaitBrowserLoaded */

"use strict";

const { ExtensionPermissions } = ChromeUtils.importESModule(
  "resource://gre/modules/ExtensionPermissions.sys.mjs"
);

const { mailTestUtils } = ChromeUtils.import(
  "resource://testing-common/mailnews/MailTestUtils.jsm"
);

const treeClick = mailTestUtils.treeClick.bind(null, EventUtils, window);

var URL_BASE =
  "http://mochi.test:8888/browser/comm/mail/components/extensions/test/browser/data";

/**
 * Left-click on something and wait for the context menu to appear.
 * For elements in the parent process only.
 *
 * @param {Element} menu - The <menu> that should appear.
 * @param {Element} element - The element to be clicked on.
 * @returns {Promise} A promise that resolves when the menu appears.
 */
function leftClick(menu, element) {
  let shownPromise = BrowserTestUtils.waitForEvent(menu, "popupshown");
  EventUtils.synthesizeMouseAtCenter(element, {}, element.ownerGlobal);
  return shownPromise;
}
/**
 * Right-click on something and wait for the context menu to appear.
 * For elements in the parent process only.
 *
 * @param {Element} menu - The <menu> that should appear.
 * @param {Element} element - The element to be clicked on.
 * @returns {Promise} A promise that resolves when the menu appears.
 */
function rightClick(menu, element) {
  let shownPromise = BrowserTestUtils.waitForEvent(menu, "popupshown");
  EventUtils.synthesizeMouseAtCenter(
    element,
    { type: "contextmenu" },
    element.ownerGlobal
  );
  return shownPromise;
}

/**
 * Right-click on something in a content document and wait for the context
 * menu to appear.
 *
 * @param {Element} menu - The <menu> that should appear.
 * @param {string} selector - CSS selector of the element to be clicked on.
 * @param {Element} browser - <browser> containing the element.
 * @returns {Promise} A promise that resolves when the menu appears.
 */
async function rightClickOnContent(menu, selector, browser) {
  let shownPromise = BrowserTestUtils.waitForEvent(menu, "popupshown");
  await synthesizeMouseAtCenterAndRetry(
    selector,
    { type: "contextmenu" },
    browser
  );
  return shownPromise;
}

/**
 * Check the parameters of a browser.onShown event was fired.
 *
 * @see mail/components/extensions/schemas/menus.json
 *
 * @param extension
 * @param {object} expectedInfo
 * @param {Array?} expectedInfo.menuIds
 * @param {Array?} expectedInfo.contexts
 * @param {Array?} expectedInfo.attachments
 * @param {object?} expectedInfo.displayedFolder
 * @param {object?} expectedInfo.selectedFolder
 * @param {Array?} expectedInfo.selectedMessages
 * @param {RegExp?} expectedInfo.pageUrl
 * @param {string?} expectedInfo.selectionText
 * @param {object} expectedTab
 * @param {boolean} expectedTab.active
 * @param {integer} expectedTab.index
 * @param {boolean} expectedTab.mailTab
 */
async function checkShownEvent(extension, expectedInfo, expectedTab) {
  let [info, tab] = await extension.awaitMessage("onShown");
  Assert.deepEqual(info.menuIds, expectedInfo.menuIds);
  Assert.deepEqual(info.contexts, expectedInfo.contexts);

  Assert.equal(
    !!info.attachments,
    !!expectedInfo.attachments,
    "attachments in info"
  );
  if (expectedInfo.attachments) {
    Assert.equal(info.attachments.length, expectedInfo.attachments.length);
    for (let i = 0; i < expectedInfo.attachments.length; i++) {
      Assert.equal(info.attachments[i].name, expectedInfo.attachments[i].name);
      Assert.equal(info.attachments[i].size, expectedInfo.attachments[i].size);
    }
  }

  for (let infoKey of ["displayedFolder", "selectedFolder"]) {
    Assert.equal(
      !!info[infoKey],
      !!expectedInfo[infoKey],
      `${infoKey} in info`
    );
    if (expectedInfo[infoKey]) {
      Assert.equal(info[infoKey].accountId, expectedInfo[infoKey].accountId);
      Assert.equal(info[infoKey].path, expectedInfo[infoKey].path);
      Assert.ok(Array.isArray(info[infoKey].subFolders));
    }
  }

  Assert.equal(
    !!info.selectedMessages,
    !!expectedInfo.selectedMessages,
    "selectedMessages in info"
  );
  if (expectedInfo.selectedMessages) {
    Assert.equal(info.selectedMessages.id, null);
    Assert.equal(
      info.selectedMessages.messages.length,
      expectedInfo.selectedMessages.messages.length
    );
    for (let i = 0; i < expectedInfo.selectedMessages.messages.length; i++) {
      Assert.equal(
        info.selectedMessages.messages[i].subject,
        expectedInfo.selectedMessages.messages[i].subject
      );
    }
  }

  Assert.equal(!!info.pageUrl, !!expectedInfo.pageUrl, "pageUrl in info");
  if (expectedInfo.pageUrl) {
    if (typeof expectedInfo.pageUrl == "string") {
      Assert.equal(info.pageUrl, expectedInfo.pageUrl);
    } else {
      Assert.ok(info.pageUrl.match(expectedInfo.pageUrl));
    }
  }

  Assert.equal(
    !!info.selectionText,
    !!expectedInfo.selectionText,
    "selectionText in info"
  );
  if (expectedInfo.selectionText) {
    Assert.equal(info.selectionText, expectedInfo.selectionText);
  }

  Assert.equal(tab.active, expectedTab.active, "tab is active");
  Assert.equal(tab.index, expectedTab.index, "tab index");
  Assert.equal(tab.mailTab, expectedTab.mailTab, "tab is mailTab");
}

/**
 * Check the parameters of a browser.onClicked event was fired.
 *
 * @see mail/components/extensions/schemas/menus.json
 *
 * @param extension
 * @param {object} expectedInfo
 * @param {string?} expectedInfo.selectionText
 * @param {string?} expectedInfo.linkText
 * @param {RegExp?} expectedInfo.pageUrl
 * @param {RegExp?} expectedInfo.linkUrl
 * @param {RegExp?} expectedInfo.srcUrl
 * @param {object} expectedTab
 * @param {boolean} expectedTab.active
 * @param {integer} expectedTab.index
 * @param {boolean} expectedTab.mailTab
 */
async function checkClickedEvent(extension, expectedInfo, expectedTab) {
  let [info, tab] = await extension.awaitMessage("onClicked");

  Assert.equal(info.selectionText, expectedInfo.selectionText, "selectionText");
  Assert.equal(info.linkText, expectedInfo.linkText, "linkText");
  if (expectedInfo.menuItemId) {
    Assert.equal(info.menuItemId, expectedInfo.menuItemId, "menuItemId");
  }

  for (let infoKey of ["pageUrl", "linkUrl", "srcUrl"]) {
    Assert.equal(
      !!info[infoKey],
      !!expectedInfo[infoKey],
      `${infoKey} in info`
    );
    if (expectedInfo[infoKey]) {
      if (typeof expectedInfo[infoKey] == "string") {
        Assert.equal(info[infoKey], expectedInfo[infoKey]);
      } else {
        Assert.ok(info[infoKey].match(expectedInfo[infoKey]));
      }
    }
  }

  Assert.equal(tab.active, expectedTab.active, "tab is active");
  Assert.equal(tab.index, expectedTab.index, "tab index");
  Assert.equal(tab.mailTab, expectedTab.mailTab, "tab is mailTab");
}

async function getMenuExtension(manifest) {
  let details = {
    files: {
      "background.js": async () => {
        let contexts = [
          "audio",
          "compose_action",
          "compose_action_menu",
          "message_display_action",
          "message_display_action_menu",
          "editable",
          "frame",
          "image",
          "link",
          "page",
          "password",
          "selection",
          "tab",
          "video",
          "message_list",
          "folder_pane",
          "compose_attachments",
          "compose_body",
          "tools_menu",
        ];
        if (browser.runtime.getManifest().manifest_version > 2) {
          contexts.push("action", "action_menu");
        } else {
          contexts.push("browser_action", "browser_action_menu");
        }

        for (let context of contexts) {
          browser.menus.create({
            id: context,
            title: context,
            contexts: [context],
          });
        }

        browser.menus.onShown.addListener((...args) => {
          browser.test.sendMessage("onShown", args);
        });

        browser.menus.onClicked.addListener((...args) => {
          browser.test.sendMessage("onClicked", args);
        });
        browser.test.sendMessage("menus-created");
      },
    },
    manifest: {
      browser_specific_settings: {
        gecko: {
          id: "menus@mochi.test",
        },
      },
      background: { scripts: ["background.js"] },
      ...manifest,
    },
    useAddonManager: "temporary",
  };

  if (!details.manifest.permissions) {
    details.manifest.permissions = [];
  }
  details.manifest.permissions.push("menus");
  console.log(JSON.stringify(details, 2));
  let extension = ExtensionTestUtils.loadExtension(details);
  if (details.manifest.host_permissions) {
    // MV3 has to manually grant the requested permission.
    await ExtensionPermissions.add("menus@mochi.test", {
      permissions: [],
      origins: details.manifest.host_permissions,
    });
  }
  return extension;
}

async function subtest_content(
  extension,
  extensionHasPermission,
  browser,
  pageUrl,
  tab
) {
  await awaitBrowserLoaded(browser, url => url != "about:blank");

  let menuId = browser.getAttribute("context");
  let ownerDocument;
  if (browser.ownerGlobal.parent.location.href == "about:3pane") {
    ownerDocument = browser.ownerGlobal.parent.document;
  } else if (menuId == "browserContext") {
    ownerDocument = browser.ownerGlobal.top.document;
  } else {
    ownerDocument = browser.ownerDocument;
  }
  let menu = ownerDocument.getElementById(menuId);

  await synthesizeMouseAtCenterAndRetry("body", {}, browser);

  info("Test a part of the page with no content.");

  await rightClickOnContent(menu, "body", browser);
  Assert.ok(menu.querySelector("#menus_mochi_test-menuitem-_page"));
  let hiddenPromise = BrowserTestUtils.waitForEvent(menu, "popuphidden");
  menu.hidePopup();
  await hiddenPromise;
  // Sometimes, the popup will open then instantly disappear. It seems to
  // still be hiding after the previous appearance. If we wait a little bit,
  // this doesn't happen.
  // eslint-disable-next-line mozilla/no-arbitrary-setTimeout
  await new Promise(r => setTimeout(r, 250));

  await checkShownEvent(
    extension,
    {
      menuIds: ["page"],
      contexts: ["page", "all"],
      pageUrl: extensionHasPermission ? pageUrl : undefined,
    },
    tab
  );

  info("Test selection.");

  await SpecialPowers.spawn(browser, [], () => {
    let text = content.document.querySelector("p");
    content.getSelection().selectAllChildren(text);
  });
  await rightClickOnContent(menu, "p", browser);
  Assert.ok(menu.querySelector("#menus_mochi_test-menuitem-_selection"));
  await checkShownEvent(
    extension,
    {
      pageUrl: extensionHasPermission ? pageUrl : undefined,
      selectionText: extensionHasPermission ? "This is text." : undefined,
      menuIds: ["selection"],
      contexts: ["selection", "all"],
    },
    tab
  );

  hiddenPromise = BrowserTestUtils.waitForEvent(menu, "popuphidden");
  let clickedPromise = checkClickedEvent(
    extension,
    {
      pageUrl,
      selectionText: "This is text.",
    },
    tab
  );
  menu.activateItem(
    menu.querySelector("#menus_mochi_test-menuitem-_selection")
  );
  await clickedPromise;
  await hiddenPromise;

  // Sometimes, the popup will open then instantly disappear. It seems to
  // still be hiding after the previous appearance. If we wait a little bit,
  // this doesn't happen.
  // eslint-disable-next-line mozilla/no-arbitrary-setTimeout
  await new Promise(r => setTimeout(r, 250));

  await synthesizeMouseAtCenterAndRetry("body", {}, browser); // Select nothing.

  info("Test link.");

  await rightClickOnContent(menu, "a", browser);
  Assert.ok(menu.querySelector("#menus_mochi_test-menuitem-_link"));
  await checkShownEvent(
    extension,
    {
      pageUrl: extensionHasPermission ? pageUrl : undefined,
      menuIds: ["link"],
      contexts: ["link", "all"],
    },
    tab
  );

  hiddenPromise = BrowserTestUtils.waitForEvent(menu, "popuphidden");
  clickedPromise = checkClickedEvent(
    extension,
    {
      pageUrl,
      linkUrl: "http://mochi.test:8888/",
      linkText: "This is a link with text.",
    },
    tab
  );
  menu.activateItem(menu.querySelector("#menus_mochi_test-menuitem-_link"));
  await clickedPromise;
  await hiddenPromise;
  // Sometimes, the popup will open then instantly disappear. It seems to
  // still be hiding after the previous appearance. If we wait a little bit,
  // this doesn't happen.
  // eslint-disable-next-line mozilla/no-arbitrary-setTimeout
  await new Promise(r => setTimeout(r, 250));

  info("Test image.");

  await rightClickOnContent(menu, "img", browser);
  Assert.ok(menu.querySelector("#menus_mochi_test-menuitem-_image"));
  await checkShownEvent(
    extension,
    {
      pageUrl: extensionHasPermission ? pageUrl : undefined,
      menuIds: ["image"],
      contexts: ["image", "all"],
    },
    tab
  );

  hiddenPromise = BrowserTestUtils.waitForEvent(menu, "popuphidden");
  clickedPromise = checkClickedEvent(
    extension,
    {
      pageUrl,
      srcUrl: `${URL_BASE}/tb-logo.png`,
    },
    tab
  );
  menu.activateItem(menu.querySelector("#menus_mochi_test-menuitem-_image"));
  await clickedPromise;
  await hiddenPromise;
  // Sometimes, the popup will open then instantly disappear. It seems to
  // still be hiding after the previous appearance. If we wait a little bit,
  // this doesn't happen.
  // eslint-disable-next-line mozilla/no-arbitrary-setTimeout
  await new Promise(r => setTimeout(r, 250));
}

async function openExtensionSubMenu(menu) {
  // The extension submenu ends with a number, which increases over time, but it
  // does not have a underscore.
  let submenu;
  for (let item of menu.querySelectorAll("[id^=menus_mochi_test-menuitem-]")) {
    if (!item.id.includes("-_")) {
      submenu = item;
      break;
    }
  }
  Assert.ok(submenu, `Found submenu: ${submenu.id}`);

  // Open submenu.
  let submenuPromise = BrowserTestUtils.waitForEvent(menu, "popupshown");
  submenu.openMenu(true);
  await submenuPromise;

  return submenu;
}

async function subtest_compose_body(
  extension,
  extensionHasPermission,
  browser,
  pageUrl,
  tab
) {
  await awaitBrowserLoaded(browser, url => url != "about:blank");

  let ownerDocument = browser.ownerDocument;
  let menu = ownerDocument.getElementById(browser.getAttribute("context"));

  await synthesizeMouseAtCenterAndRetry("body", {}, browser);

  info("Test a part of the page with no content.");
  {
    await rightClickOnContent(menu, "body", browser);
    Assert.ok(menu.querySelector(`#menus_mochi_test-menuitem-_compose_body`));
    Assert.ok(menu.querySelector(`#menus_mochi_test-menuitem-_editable`));
    let hiddenPromise = BrowserTestUtils.waitForEvent(menu, "popuphidden");
    menu.hidePopup();
    await hiddenPromise;
    // Sometimes, the popup will open then instantly disappear. It seems to
    // still be hiding after the previous appearance. If we wait a little bit,
    // this doesn't happen.
    // eslint-disable-next-line mozilla/no-arbitrary-setTimeout
    await new Promise(r => setTimeout(r, 250));

    await checkShownEvent(
      extension,
      {
        menuIds: ["editable", "compose_body"],
        contexts: ["editable", "compose_body", "all"],
        pageUrl: extensionHasPermission ? pageUrl : undefined,
      },
      tab
    );
  }

  info("Test selection.");
  {
    await SpecialPowers.spawn(browser, [], () => {
      let text = content.document.querySelector("p");
      content.getSelection().selectAllChildren(text);
    });

    await rightClickOnContent(menu, "p", browser);
    let submenu = await openExtensionSubMenu(menu);

    await checkShownEvent(
      extension,
      {
        pageUrl: extensionHasPermission ? pageUrl : undefined,
        selectionText: extensionHasPermission ? "This is text." : undefined,
        menuIds: ["editable", "selection", "compose_body"],
        contexts: ["editable", "selection", "compose_body", "all"],
      },
      tab
    );
    Assert.ok(submenu.querySelector("#menus_mochi_test-menuitem-_selection"));
    Assert.ok(
      submenu.querySelector("#menus_mochi_test-menuitem-_compose_body")
    );
    Assert.ok(submenu.querySelector("#menus_mochi_test-menuitem-_editable"));

    let hiddenPromise = BrowserTestUtils.waitForEvent(submenu, "popuphidden");
    let clickedPromise = checkClickedEvent(
      extension,
      {
        pageUrl,
        selectionText: "This is text.",
      },
      tab
    );
    menu.activateItem(
      submenu.querySelector("#menus_mochi_test-menuitem-_selection")
    );
    await clickedPromise;
    await hiddenPromise;

    // Sometimes, the popup will open then instantly disappear. It seems to
    // still be hiding after the previous appearance. If we wait a little bit,
    // this doesn't happen.
    // eslint-disable-next-line mozilla/no-arbitrary-setTimeout
    await new Promise(r => setTimeout(r, 250));
    await synthesizeMouseAtCenterAndRetry("body", {}, browser); // Select nothing.
  }

  info("Test link.");
  {
    await rightClickOnContent(menu, "a", browser);
    let submenu = await openExtensionSubMenu(menu);

    await checkShownEvent(
      extension,
      {
        pageUrl: extensionHasPermission ? pageUrl : undefined,
        menuIds: ["editable", "link", "compose_body"],
        contexts: ["editable", "link", "compose_body", "all"],
      },
      tab
    );
    Assert.ok(submenu.querySelector("#menus_mochi_test-menuitem-_link"));
    Assert.ok(submenu.querySelector("#menus_mochi_test-menuitem-_editable"));
    Assert.ok(
      submenu.querySelector("#menus_mochi_test-menuitem-_compose_body")
    );

    let hiddenPromise = BrowserTestUtils.waitForEvent(submenu, "popuphidden");
    let clickedPromise = checkClickedEvent(
      extension,
      {
        pageUrl,
        linkUrl: "http://mochi.test:8888/",
        linkText: "This is a link with text.",
      },
      tab
    );
    menu.activateItem(
      submenu.querySelector("#menus_mochi_test-menuitem-_link")
    );
    await clickedPromise;
    await hiddenPromise;

    // Sometimes, the popup will open then instantly disappear. It seems to
    // still be hiding after the previous appearance. If we wait a little bit,
    // this doesn't happen.
    // eslint-disable-next-line mozilla/no-arbitrary-setTimeout
    await new Promise(r => setTimeout(r, 250));
    await synthesizeMouseAtCenterAndRetry("body", {}, browser); // Select nothing.
  }

  info("Test image.");
  {
    await rightClickOnContent(menu, "img", browser);
    let submenu = await openExtensionSubMenu(menu);

    await checkShownEvent(
      extension,
      {
        pageUrl: extensionHasPermission ? pageUrl : undefined,
        menuIds: ["editable", "image", "compose_body"],
        contexts: ["editable", "image", "compose_body", "all"],
      },
      tab
    );
    Assert.ok(submenu.querySelector("#menus_mochi_test-menuitem-_image"));
    Assert.ok(submenu.querySelector("#menus_mochi_test-menuitem-_editable"));
    Assert.ok(
      submenu.querySelector("#menus_mochi_test-menuitem-_compose_body")
    );

    let hiddenPromise = BrowserTestUtils.waitForEvent(menu, "popuphidden");
    let clickedPromise = checkClickedEvent(
      extension,
      {
        pageUrl,
        srcUrl: `${URL_BASE}/tb-logo.png`,
      },
      tab
    );
    menu.activateItem(
      submenu.querySelector("#menus_mochi_test-menuitem-_image")
    );
    await clickedPromise;
    await hiddenPromise;

    // Sometimes, the popup will open then instantly disappear. It seems to
    // still be hiding after the previous appearance. If we wait a little bit,
    // this doesn't happen.
    // eslint-disable-next-line mozilla/no-arbitrary-setTimeout
    await new Promise(r => setTimeout(r, 250));
    await synthesizeMouseAtCenterAndRetry("body", {}, browser); // Select nothing.
  }
}

// Test UI elements which have been made accessible for the menus API.
// Assumed to be run after subtest_content, so we know everything has finished
// loading.
async function subtest_element(
  extension,
  extensionHasPermission,
  element,
  pageUrl,
  tab
) {
  for (let selectedTest of [false, true]) {
    element.focus();
    if (selectedTest) {
      element.value = "This is selected text.";
      element.select();
    } else {
      element.value = "";
    }

    let event = await rightClick(element.ownerGlobal, element);
    let menu = event.target;
    let trigger = menu.triggerNode;
    let menuitem = menu.querySelector("#menus_mochi_test-menuitem-_editable");
    Assert.equal(
      element.id,
      trigger.id,
      "Contextmenu of correct element has been triggered."
    );
    Assert.equal(
      menuitem.id,
      "menus_mochi_test-menuitem-_editable",
      "Contextmenu includes menu."
    );

    await checkShownEvent(
      extension,
      {
        menuIds: selectedTest ? ["editable", "selection"] : ["editable"],
        contexts: selectedTest
          ? ["editable", "selection", "all"]
          : ["editable", "all"],
        pageUrl: extensionHasPermission ? pageUrl : undefined,
        selectionText:
          extensionHasPermission && selectedTest
            ? "This is selected text."
            : undefined,
      },
      tab
    );

    // With text being selected, there will be two "context" entries in an
    // extension submenu. Open the submenu.
    let submenu = null;
    if (selectedTest) {
      for (let foundMenu of menu.querySelectorAll(
        "[id^='menus_mochi_test-menuitem-']"
      )) {
        if (!foundMenu.id.startsWith("menus_mochi_test-menuitem-_")) {
          submenu = foundMenu;
        }
      }
      Assert.ok(submenu, "Submenu found.");
      let submenuPromise = BrowserTestUtils.waitForEvent(
        element.ownerGlobal,
        "popupshown"
      );
      submenu.openMenu(true);
      await submenuPromise;
    }

    let hiddenPromise = BrowserTestUtils.waitForEvent(
      element.ownerGlobal,
      "popuphidden"
    );
    let clickedPromise = checkClickedEvent(
      extension,
      {
        pageUrl,
        selectionText: selectedTest ? "This is selected text." : undefined,
      },
      tab
    );
    if (submenu) {
      submenu.menupopup.activateItem(menuitem);
    } else {
      menu.activateItem(menuitem);
    }
    await clickedPromise;
    await hiddenPromise;

    // Sometimes, the popup will open then instantly disappear. It seems to
    // still be hiding after the previous appearance. If we wait a little bit,
    // this doesn't happen.
    // eslint-disable-next-line mozilla/no-arbitrary-setTimeout
    await new Promise(r => setTimeout(r, 250));
  }
}