browser_contextmenu_linkopen.js (3271B)
1 /* Any copyright is dedicated to the Public Domain. 2 http://creativecommons.org/publicdomain/zero/1.0/ */ 3 4 "use strict"; 5 6 const TEST_LINK = "https://example.com/"; 7 const RESOURCE_LINK = 8 getRootDirectory(gTestPath).replace( 9 "chrome://mochitests/content", 10 "https://example.com" 11 ) + "test_contextmenu_links.html"; 12 13 async function activateContextAndWaitFor(selector, where) { 14 info("Starting test for " + where); 15 let contextMenuItem = "openlink"; 16 let openPromise; 17 let closeMethod; 18 switch (where) { 19 case "tab": 20 contextMenuItem += "intab"; 21 openPromise = BrowserTestUtils.waitForNewTab(gBrowser, TEST_LINK, false); 22 closeMethod = async tab => BrowserTestUtils.removeTab(tab); 23 break; 24 case "privatewindow": 25 contextMenuItem += "private"; 26 openPromise = BrowserTestUtils.waitForNewWindow({ url: TEST_LINK }).then( 27 win => { 28 ok( 29 PrivateBrowsingUtils.isWindowPrivate(win), 30 "Should have opened a private window." 31 ); 32 return win; 33 } 34 ); 35 closeMethod = async win => BrowserTestUtils.closeWindow(win); 36 break; 37 case "window": 38 // No contextMenuItem suffix for normal new windows; 39 openPromise = BrowserTestUtils.waitForNewWindow({ url: TEST_LINK }).then( 40 win => { 41 ok( 42 !PrivateBrowsingUtils.isWindowPrivate(win), 43 "Should have opened a normal window." 44 ); 45 return win; 46 } 47 ); 48 closeMethod = async win => BrowserTestUtils.closeWindow(win); 49 break; 50 } 51 let contextMenu = document.getElementById("contentAreaContextMenu"); 52 is(contextMenu.state, "closed", "checking if popup is closed"); 53 let awaitPopupShown = BrowserTestUtils.waitForEvent( 54 contextMenu, 55 "popupshown" 56 ); 57 await BrowserTestUtils.synthesizeMouse( 58 selector, 59 0, 60 0, 61 { 62 type: "contextmenu", 63 button: 2, 64 centered: true, 65 }, 66 gBrowser.selectedBrowser 67 ); 68 await awaitPopupShown; 69 info("Popup Shown"); 70 let awaitPopupHidden = BrowserTestUtils.waitForEvent( 71 contextMenu, 72 "popuphidden" 73 ); 74 let domItem = contextMenu.querySelector("#context-" + contextMenuItem); 75 info("Going to click item " + domItem.id); 76 ok( 77 BrowserTestUtils.isVisible(domItem), 78 "DOM context menu item " + where + " should be visible" 79 ); 80 ok( 81 !domItem.disabled, 82 "DOM context menu item " + where + " shouldn't be disabled" 83 ); 84 contextMenu.activateItem(domItem); 85 await awaitPopupHidden; 86 87 info("Waiting for the link to open"); 88 let openedThing = await openPromise; 89 info("Waiting for the opened window/tab to close"); 90 await closeMethod(openedThing); 91 } 92 93 add_setup(async function () { 94 await SpecialPowers.pushPrefEnv({ 95 set: [["test.wait300msAfterTabSwitch", true]], 96 }); 97 }); 98 99 add_task(async function test_select_text_link() { 100 let testTab = await BrowserTestUtils.openNewForegroundTab( 101 gBrowser, 102 RESOURCE_LINK 103 ); 104 for (let elementID of [ 105 "test-link", 106 "test-image-link", 107 "svg-with-link", 108 "svg-with-relative-link", 109 ]) { 110 for (let where of ["tab", "window", "privatewindow"]) { 111 await activateContextAndWaitFor("#" + elementID, where); 112 } 113 } 114 BrowserTestUtils.removeTab(testTab); 115 });