browser_contextmenu_share_macosx.js (5817B)
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 // eslint-disable-next-line @microsoft/sdl/no-insecure-url 12 "http://example.com" 13 ); 14 const TEST_URL = BASE + "browser_contextmenu_shareurl.html"; 15 16 let mockShareData = [ 17 { 18 name: "Test", 19 menuItemTitle: "Sharing Service Test", 20 image: 21 "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAIAAAD8GO2jAAAAKE" + 22 "lEQVR42u3NQQ0AAAgEoNP+nTWFDzcoQE1udQQCgUAgEAgEAsGTYAGjxAE/G/Q2tQAAAABJRU5ErkJggg==", 23 }, 24 ]; 25 26 // Setup spies for observing function calls from MacSharingService 27 let shareUrlSpy = sinon.spy(); 28 let openSharingPreferencesSpy = sinon.spy(); 29 let getSharingProvidersSpy = sinon.spy(); 30 31 let { MockRegistrar } = ChromeUtils.importESModule( 32 "resource://testing-common/MockRegistrar.sys.mjs" 33 ); 34 let mockMacSharingService = MockRegistrar.register( 35 "@mozilla.org/widget/macsharingservice;1", 36 { 37 getSharingProviders(url) { 38 getSharingProvidersSpy(url); 39 return mockShareData; 40 }, 41 shareUrl(name, url, title) { 42 shareUrlSpy(name, url, title); 43 }, 44 openSharingPreferences() { 45 openSharingPreferencesSpy(); 46 }, 47 QueryInterface: ChromeUtils.generateQI([Ci.nsIMacSharingService]), 48 } 49 ); 50 51 registerCleanupFunction(function () { 52 MockRegistrar.unregister(mockMacSharingService); 53 }); 54 55 /** 56 * Test the "Share" item menus in the tab contextmenu on MacOSX. 57 */ 58 add_task(async function test_contextmenu_share_macosx() { 59 await BrowserTestUtils.withNewTab(TEST_URL, async () => { 60 let contextMenu = await openTabContextMenu(gBrowser.selectedTab); 61 await BrowserTestUtils.waitForMutationCondition( 62 contextMenu, 63 { childList: true }, 64 () => contextMenu.querySelector(".share-tab-url-item") 65 ); 66 ok(true, "Got Share item"); 67 68 await openMenuPopup(contextMenu); 69 ok(getSharingProvidersSpy.calledOnce, "getSharingProviders called"); 70 71 info( 72 "Check we have copy link, a service and one extra menu item for the More... button" 73 ); 74 let popup = contextMenu.querySelector(".share-tab-url-item").menupopup; 75 let items = Array.from(popup.querySelectorAll("menuitem")); 76 is(items.length, 3, "There should be 3 sharing services."); 77 78 info("Click on the sharing service"); 79 let menuPopupClosedPromised = BrowserTestUtils.waitForPopupEvent( 80 contextMenu, 81 "hidden" 82 ); 83 let shareButton = items.find( 84 t => t.label == mockShareData[0].menuItemTitle 85 ); 86 ok( 87 shareButton, 88 "Share button's label should match the service's menu item title. " 89 ); 90 is( 91 shareButton?.getAttribute("share-name"), 92 mockShareData[0].name, 93 "Share button's share-name value should match the service's name. " 94 ); 95 96 popup.activateItem(shareButton); 97 await menuPopupClosedPromised; 98 99 ok(shareUrlSpy.calledOnce, "shareUrl called"); 100 101 info("Check the correct data was shared."); 102 let [name, url, title] = shareUrlSpy.getCall(0).args; 103 is(name, mockShareData[0].name, "Shared correct service name"); 104 is(url, TEST_URL, "Shared correct URL"); 105 is(title, "Sharing URL", "Shared the correct title."); 106 107 info("Test the copy link button"); 108 contextMenu = await openTabContextMenu(gBrowser.selectedTab); 109 await openMenuPopup(contextMenu); 110 // Since the tab context menu was collapsed previously, the popup needs to get the 111 // providers again. 112 ok(getSharingProvidersSpy.calledTwice, "getSharingProviders called again"); 113 popup = contextMenu.querySelector(".share-tab-url-item").menupopup; 114 items = Array.from(popup.querySelectorAll("menuitem")); 115 is(items.length, 3, "There should be 3 sharing services."); 116 info("Click on the Copy Link item"); 117 let copyLinkItem = items.find( 118 item => item.dataset.l10nId == "menu-share-copy-link" 119 ); 120 menuPopupClosedPromised = BrowserTestUtils.waitForPopupEvent( 121 contextMenu, 122 "hidden" 123 ); 124 await SimpleTest.promiseClipboardChange(TEST_URL, () => 125 popup.activateItem(copyLinkItem) 126 ); 127 await menuPopupClosedPromised; 128 129 info("Test the More... item"); 130 contextMenu = await openTabContextMenu(gBrowser.selectedTab); 131 await openMenuPopup(contextMenu); 132 // Since the tab context menu was collapsed previously, the popup needs to get the 133 // providers again. 134 is(getSharingProvidersSpy.callCount, 3, "getSharingProviders called again"); 135 popup = contextMenu.querySelector(".share-tab-url-item").menupopup; 136 items = popup.querySelectorAll("menuitem"); 137 is(items.length, 3, "There should be 3 sharing services."); 138 139 info("Click on the More item"); 140 let moreMenuitem = items[2]; 141 menuPopupClosedPromised = BrowserTestUtils.waitForPopupEvent( 142 contextMenu, 143 "hidden" 144 ); 145 popup.activateItem(moreMenuitem); 146 await menuPopupClosedPromised; 147 ok(openSharingPreferencesSpy.calledOnce, "openSharingPreferences called"); 148 }); 149 }); 150 151 /** 152 * Helper for opening the toolbar context menu. 153 */ 154 async function openTabContextMenu(tab) { 155 info("Opening tab context menu"); 156 let contextMenu = document.getElementById("tabContextMenu"); 157 let openTabContextMenuPromise = BrowserTestUtils.waitForPopupEvent( 158 contextMenu, 159 "shown" 160 ); 161 162 EventUtils.synthesizeMouseAtCenter(tab, { type: "contextmenu" }); 163 await openTabContextMenuPromise; 164 return contextMenu; 165 } 166 167 async function openMenuPopup(contextMenu) { 168 info("Opening Share menu popup."); 169 let shareItem = contextMenu.querySelector(".share-tab-url-item"); 170 shareItem.openMenu(true); 171 await BrowserTestUtils.waitForPopupEvent(shareItem.menupopup, "shown"); 172 }