Source code
Revision control
Copy as Markdown
Other Tools
Test Info: Warnings
-
CI results over the last 21 days
- This test runs only with pattern: os == 'mac'
- Manifest: browser/base/content/test/browser-contextMenu/browser.toml
/* Any copyright is dedicated to the Public Domain.
"use strict";
const TEST_URL =
"data:text/html,<p style='font-size: 40px'><span id='hello'>hello</span> " +
"link</a></p><input id='misspelled' spellcheck='true' " +
"value='prodkjfgigrty' style='font-size: 40px'><br>" +
"<img id='image' width='40' height='40' src='data:image/png;base64," +
"iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkYAAAAAYAAjCB0C8AAAAASUVORK5CYII='>" +
"<canvas id='canvas' width='40' height='40'></canvas>" +
"<video id='video' width='40' height='40'></video>" +
"<audio id='audio' controls></audio>";
async function openContextMenuAt(browser, selector) {
let contextMenu = document.getElementById("contentAreaContextMenu");
let shown = BrowserTestUtils.waitForEvent(contextMenu, "popupshown");
await BrowserTestUtils.synthesizeMouseAtCenter(
selector,
{ type: "contextmenu", button: 2 },
browser
);
await shown;
return contextMenu;
}
async function closeContextMenu(contextMenu) {
let hidden = BrowserTestUtils.waitForEvent(contextMenu, "popuphidden");
contextMenu.hidePopup();
await hidden;
}
function visibleItems(contextMenu) {
return [...contextMenu.children].filter(
item => !item.hidden && !item.hasAttribute("generateditemid")
);
}
async function selectContents(browser, selector) {
await SpecialPowers.spawn(browser, [selector], contentSelector => {
content
.getSelection()
.selectAllChildren(content.document.querySelector(contentSelector));
});
}
async function clearSelection(browser) {
await SpecialPowers.spawn(browser, [], () => {
content.getSelection().removeAllRanges();
});
}
add_setup(async function () {
await SpecialPowers.pushPrefEnv({
set: [
["browser.menu.lookUp.enabled", true],
["ui.mouse.right_click.select_under_cursor", false],
],
});
});
add_task(async function test_hidden_without_selection() {
await BrowserTestUtils.withNewTab(TEST_URL, async browser => {
let contextMenu = await openContextMenuAt(browser, "#hello");
ok(
document.getElementById("context-look-up").hidden,
"Look Up is hidden when nothing is selected"
);
await closeContextMenu(contextMenu);
});
});
add_task(async function test_hidden_when_pref_disabled() {
await SpecialPowers.pushPrefEnv({
set: [["browser.menu.lookUp.enabled", false]],
});
await BrowserTestUtils.withNewTab(TEST_URL, async browser => {
await selectContents(browser, "#hello");
let contextMenu = await openContextMenuAt(browser, "#hello");
ok(
document.getElementById("context-look-up").hidden,
"Look Up is hidden when the pref is disabled"
);
await closeContextMenu(contextMenu);
});
await SpecialPowers.popPrefEnv();
});
add_task(async function test_first_item_with_selection() {
await BrowserTestUtils.withNewTab(TEST_URL, async browser => {
await selectContents(browser, "#hello");
let contextMenu = await openContextMenuAt(browser, "#hello");
let item = document.getElementById("context-look-up");
ok(!item.hidden, "Look Up is shown with a selection");
Assert.deepEqual(
document.l10n.getAttributes(item).args,
{ text: "hello" },
"Look Up uses the selected text"
);
let items = visibleItems(contextMenu);
is(items[0], item, "Look Up is the first item");
is(
items[1].id,
"context-sep-look-up",
"Look Up is followed by a separator"
);
await closeContextMenu(contextMenu);
});
});
add_task(async function test_hidden_on_link() {
await BrowserTestUtils.withNewTab(TEST_URL, async browser => {
await selectContents(browser, "#link");
let contextMenu = await openContextMenuAt(browser, "#link");
ok(
document.getElementById("context-look-up").hidden,
"Look Up is hidden on a link"
);
await closeContextMenu(contextMenu);
await clearSelection(browser);
});
});
add_task(async function test_hidden_on_image_and_media() {
await BrowserTestUtils.withNewTab(TEST_URL, async browser => {
await selectContents(browser, "#hello");
for (let selector of ["#image", "#canvas", "#video", "#audio"]) {
let contextMenu = await openContextMenuAt(browser, selector);
ok(
document.getElementById("context-look-up").hidden,
`Look Up is hidden on ${selector} while text is selected`
);
await closeContextMenu(contextMenu);
}
});
});
add_task(async function test_below_spelling_suggestions() {
await BrowserTestUtils.withNewTab(TEST_URL, async browser => {
await SpecialPowers.spawn(browser, [], async () => {
let { onSpellCheck } = ChromeUtils.importESModule(
);
let input = content.document.getElementById("misspelled");
input.focus();
input.select();
await new Promise(resolve => onSpellCheck(input, resolve));
});
let contextMenu = await openContextMenuAt(browser, "#misspelled");
let item = document.getElementById("context-look-up");
ok(!item.hidden, "Look Up is shown on a selected misspelled word");
let ids = visibleItems(contextMenu).map(i => i.id);
let lookUpIndex = ids.indexOf("context-look-up");
is(
ids[lookUpIndex - 1],
"spell-suggestions-separator",
"Look Up comes right after the spelling block"
);
Assert.less(
ids.indexOf("spell-add-to-dictionary"),
lookUpIndex,
"Spelling items come before Look Up"
);
await closeContextMenu(contextMenu);
});
});
add_task(async function test_shown_outside_selection() {
await BrowserTestUtils.withNewTab(TEST_URL, async browser => {
await selectContents(browser, "#hello");
let contextMenu = await openContextMenuAt(browser, "#world");
let item = document.getElementById("context-look-up");
ok(
!item.hidden,
"Look Up is shown when right clicking outside the selection"
);
Assert.deepEqual(
document.l10n.getAttributes(item).args,
{ text: "hello" },
"Look Up uses the selected text, not the word under the pointer"
);
await closeContextMenu(contextMenu);
});
});
add_task(async function test_shown_with_right_click_select() {
await SpecialPowers.pushPrefEnv({
set: [["ui.mouse.right_click.select_under_cursor", true]],
});
await BrowserTestUtils.withNewTab(TEST_URL, async browser => {
let contextMenu = await openContextMenuAt(browser, "#hello");
let item = document.getElementById("context-look-up");
ok(!item.hidden, "Look Up is shown for the word selected by right click");
Assert.deepEqual(
document.l10n.getAttributes(item).args,
{ text: "hello" },
"Look Up uses the word selected by right click"
);
await closeContextMenu(contextMenu);
});
await SpecialPowers.popPrefEnv();
});