browser_contextmenu_share_win.js (2054B)
1 /* Any copyright is dedicated to the Public Domain. 2 http://creativecommons.org/publicdomain/zero/1.0/ */ 3 4 "use strict"; 5 6 const { sinon } = ChromeUtils.importESModule( 7 "resource://testing-common/Sinon.sys.mjs" 8 ); 9 const BASE = getRootDirectory(gTestPath).replace( 10 "chrome://mochitests/content", 11 "https://example.com" 12 ); 13 const TEST_URL = BASE + "browser_contextmenu_shareurl.html"; 14 15 // Setup spies for observing function calls from WindowsUIUtils. 16 let shareUrlSpy = sinon.spy(); 17 18 SharingUtils.testOnlyMockUIUtils({ 19 shareUrl(url, title) { 20 shareUrlSpy(url, title); 21 }, 22 QueryInterface: ChromeUtils.generateQI([Ci.nsIWindowsUIUtils]), 23 }); 24 25 registerCleanupFunction(function () { 26 SharingUtils.testOnlyMockUIUtils(null); 27 }); 28 29 /** 30 * Test the "Share" item in the tab contextmenu on Windows. 31 */ 32 add_task(async function test_contextmenu_share_win() { 33 await BrowserTestUtils.withNewTab(TEST_URL, async () => { 34 await openTabContextMenu(gBrowser.selectedTab); 35 36 let contextMenu = document.getElementById("tabContextMenu"); 37 let contextMenuClosedPromise = BrowserTestUtils.waitForPopupEvent( 38 contextMenu, 39 "hidden" 40 ); 41 let itemCreated = contextMenu.querySelector(".share-tab-url-item"); 42 43 ok(itemCreated, "Got Share item on Windows 10"); 44 45 info("Test the correct URL is shared when Share is selected."); 46 EventUtils.synthesizeMouseAtCenter(itemCreated, {}); 47 await contextMenuClosedPromise; 48 49 ok(shareUrlSpy.calledOnce, "shareUrl called"); 50 let [url, title] = shareUrlSpy.getCall(0).args; 51 is(url, TEST_URL, "Shared correct URL"); 52 is(title, "Sharing URL", "Shared correct URL"); 53 }); 54 }); 55 56 /** 57 * Helper for opening the toolbar context menu. 58 */ 59 async function openTabContextMenu(tab) { 60 info("Opening tab context menu"); 61 let contextMenu = document.getElementById("tabContextMenu"); 62 let openTabContextMenuPromise = BrowserTestUtils.waitForPopupEvent( 63 contextMenu, 64 "shown" 65 ); 66 67 EventUtils.synthesizeMouseAtCenter(tab, { type: "contextmenu" }); 68 await openTabContextMenuPromise; 69 }