/* Any copyright is dedicated to the Public Domain. http://creativecommons.org/publicdomain/zero/1.0/ */ "use strict"; const TEST_URL = "data:text/html,

hello " + "world " + "link


" + "" + "" + "" + ""; 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( "resource://testing-common/AsyncSpellCheckTestHelper.sys.mjs" ); 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(); });