|
| 1 | +/* Any copyright is dedicated to the Public Domain. |
| 2 | + https://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 { ObjectUtils } = ChromeUtils.importESModule( |
| 10 | + "resource://gre/modules/ObjectUtils.sys.mjs" |
| 11 | +); |
| 12 | + |
| 13 | +const EXAMPLE_URL = "https://example.com/"; |
| 14 | + |
| 15 | +async function addTab(url = EXAMPLE_URL) { |
| 16 | + const tab = BrowserTestUtils.addTab(gBrowser, url, { |
| 17 | + skipAnimation: true, |
| 18 | + }); |
| 19 | + const browser = gBrowser.getBrowserForTab(tab); |
| 20 | + await BrowserTestUtils.browserLoaded(browser); |
| 21 | + return tab; |
| 22 | +} |
| 23 | + |
| 24 | +async function openContextMenu(tab) { |
| 25 | + let contextMenu = document.getElementById("tabContextMenu"); |
| 26 | + let openTabContextMenuPromise = BrowserTestUtils.waitForPopupEvent( |
| 27 | + contextMenu, |
| 28 | + "shown" |
| 29 | + ); |
| 30 | + |
| 31 | + await sendAndWaitForMouseEvent(tab, { type: "contextmenu" }); |
| 32 | + await openTabContextMenuPromise; |
| 33 | + return contextMenu; |
| 34 | +} |
| 35 | + |
| 36 | +async function openMoveTabOptionsMenuPopup(contextMenu) { |
| 37 | + let moveTabMenuItem = contextMenu.querySelector("#context_moveTabOptions"); |
| 38 | + let subMenu = contextMenu.querySelector("#moveTabOptionsMenu"); |
| 39 | + let popupShown = BrowserTestUtils.waitForEvent(subMenu, "popupshown"); |
| 40 | + |
| 41 | + if (AppConstants.platform === "macosx") { |
| 42 | + moveTabMenuItem.openMenu(true); |
| 43 | + } else { |
| 44 | + await sendAndWaitForMouseEvent(moveTabMenuItem); |
| 45 | + } |
| 46 | + |
| 47 | + await popupShown; |
| 48 | + |
| 49 | + const separator = subMenu.querySelector("#moveTabSeparator"); |
| 50 | + await TestUtils.waitForCondition( |
| 51 | + () => |
| 52 | + BrowserTestUtils.isVisible(separator) && |
| 53 | + BrowserTestUtils.isVisible(separator.nextElementSibling) |
| 54 | + ); |
| 55 | + |
| 56 | + return subMenu; |
| 57 | +} |
| 58 | + |
| 59 | +async function clickMoveToProfileMenuItem(subMenu) { |
| 60 | + let profileMenuItem = subMenu.querySelector(":scope > menuitem[profileid]"); |
| 61 | + if (AppConstants.platform === "macosx") { |
| 62 | + subMenu.activateItem(profileMenuItem); |
| 63 | + } else { |
| 64 | + await sendAndWaitForMouseEvent(profileMenuItem); |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +async function sendAndWaitForMouseEvent(target, options = {}) { |
| 69 | + let promise = BrowserTestUtils.waitForEvent(target, options.type ?? "click"); |
| 70 | + EventUtils.synthesizeMouseAtCenter(target, options); |
| 71 | + return promise; |
| 72 | +} |
| 73 | + |
| 74 | +const execProcess = sinon.fake(); |
| 75 | +const sendCommandLine = sinon.fake.throws(Cr.NS_ERROR_NOT_AVAILABLE); |
| 76 | +sinon.replace( |
| 77 | + SelectableProfileService, |
| 78 | + "sendCommandLine", |
| 79 | + (path, args, raise) => sendCommandLine(path, [...args], raise) |
| 80 | +); |
| 81 | +sinon.replace(SelectableProfileService, "execProcess", execProcess); |
| 82 | + |
| 83 | +registerCleanupFunction(() => { |
| 84 | + sinon.restore(); |
| 85 | +}); |
| 86 | + |
| 87 | +let lastCommandLineCallCount = 1; |
| 88 | +async function assertCommandLineExists(expected) { |
| 89 | + await TestUtils.waitForCondition( |
| 90 | + () => sendCommandLine.callCount > lastCommandLineCallCount, |
| 91 | + "Waiting for notify task to complete" |
| 92 | + ); |
| 93 | + |
| 94 | + let allCommandLineCalls = sendCommandLine.getCalls(); |
| 95 | + |
| 96 | + lastCommandLineCallCount++; |
| 97 | + |
| 98 | + let expectedCount = allCommandLineCalls.reduce((count, call) => { |
| 99 | + if (ObjectUtils.deepEqual(call.args, expected)) { |
| 100 | + return count + 1; |
| 101 | + } |
| 102 | + |
| 103 | + return count; |
| 104 | + }, 0); |
| 105 | + |
| 106 | + Assert.equal(expectedCount, 1, "Found expected args"); |
| 107 | + Assert.deepEqual( |
| 108 | + allCommandLineCalls.find(call => ObjectUtils.deepEqual(call.args, expected)) |
| 109 | + .args, |
| 110 | + expected, |
| 111 | + "Expected sendCommandLine arguments to open tab in profile" |
| 112 | + ); |
| 113 | +} |
| 114 | + |
| 115 | +add_task(async function test_moveSelectedTab() { |
| 116 | + await initGroupDatabase(); |
| 117 | + |
| 118 | + const allProfiles = await SelectableProfileService.getAllProfiles(); |
| 119 | + let otherProfile; |
| 120 | + if (allProfiles.length < 2) { |
| 121 | + otherProfile = await SelectableProfileService.createNewProfile(false); |
| 122 | + } else { |
| 123 | + otherProfile = allProfiles.find( |
| 124 | + p => p.id !== SelectableProfileService.currentProfile.id |
| 125 | + ); |
| 126 | + } |
| 127 | + |
| 128 | + let tab2 = await addTab(EXAMPLE_URL + "2"); |
| 129 | + |
| 130 | + gBrowser.selectedTab = tab2; |
| 131 | + |
| 132 | + let contextMenu = await openContextMenu(tab2); |
| 133 | + let subMenu = await openMoveTabOptionsMenuPopup(contextMenu); |
| 134 | + await clickMoveToProfileMenuItem(subMenu); |
| 135 | + |
| 136 | + let expectedArgs = ["-new-tab", EXAMPLE_URL + "2"]; |
| 137 | + |
| 138 | + await assertCommandLineExists([otherProfile.path, expectedArgs, true]); |
| 139 | + |
| 140 | + gBrowser.removeTab(tab2); |
| 141 | +}); |
| 142 | + |
| 143 | +add_task(async function test_moveNonSelectedTab() { |
| 144 | + await initGroupDatabase(); |
| 145 | + |
| 146 | + const allProfiles = await SelectableProfileService.getAllProfiles(); |
| 147 | + let otherProfile; |
| 148 | + if (allProfiles.length < 2) { |
| 149 | + otherProfile = await SelectableProfileService.createNewProfile(false); |
| 150 | + } else { |
| 151 | + otherProfile = allProfiles.find( |
| 152 | + p => p.id !== SelectableProfileService.currentProfile.id |
| 153 | + ); |
| 154 | + } |
| 155 | + |
| 156 | + let tab2 = await addTab(EXAMPLE_URL + "2"); |
| 157 | + let tab3 = await addTab(EXAMPLE_URL + "3"); |
| 158 | + |
| 159 | + gBrowser.selectedTab = tab2; |
| 160 | + |
| 161 | + let contextMenu = await openContextMenu(tab3); |
| 162 | + let subMenu = await openMoveTabOptionsMenuPopup(contextMenu); |
| 163 | + await clickMoveToProfileMenuItem(subMenu); |
| 164 | + |
| 165 | + let expectedArgs = ["-new-tab", EXAMPLE_URL + "3"]; |
| 166 | + |
| 167 | + await assertCommandLineExists([otherProfile.path, expectedArgs, true]); |
| 168 | + |
| 169 | + gBrowser.removeTabs([tab2, tab3]); |
| 170 | +}); |
| 171 | + |
| 172 | +add_task(async function test_moveMultipleSelectedTabs() { |
| 173 | + await initGroupDatabase(); |
| 174 | + |
| 175 | + const allProfiles = await SelectableProfileService.getAllProfiles(); |
| 176 | + let otherProfile; |
| 177 | + if (allProfiles.length < 2) { |
| 178 | + otherProfile = await SelectableProfileService.createNewProfile(false); |
| 179 | + } else { |
| 180 | + otherProfile = allProfiles.find( |
| 181 | + p => p.id !== SelectableProfileService.currentProfile.id |
| 182 | + ); |
| 183 | + } |
| 184 | + |
| 185 | + let tab2 = await addTab(EXAMPLE_URL + "2"); |
| 186 | + let tab3 = await addTab(EXAMPLE_URL + "3"); |
| 187 | + let tab4 = await addTab(EXAMPLE_URL + "4"); |
| 188 | + |
| 189 | + gBrowser.selectedTab = tab2; |
| 190 | + |
| 191 | + await sendAndWaitForMouseEvent(tab2); |
| 192 | + if (AppConstants.platform === "macosx") { |
| 193 | + await sendAndWaitForMouseEvent(tab3, { metaKey: true }); |
| 194 | + await sendAndWaitForMouseEvent(tab4, { metaKey: true }); |
| 195 | + } else { |
| 196 | + await sendAndWaitForMouseEvent(tab3, { ctrlKey: true }); |
| 197 | + await sendAndWaitForMouseEvent(tab4, { ctrlKey: true }); |
| 198 | + } |
| 199 | + |
| 200 | + Assert.ok(tab2.multiselected, "Tab2 is multiselected"); |
| 201 | + Assert.ok(tab3.multiselected, "Tab3 is multiselected"); |
| 202 | + Assert.ok(tab4.multiselected, "Tab4 is multiselected"); |
| 203 | + |
| 204 | + let contextMenu = await openContextMenu(tab4); |
| 205 | + let subMenu = await openMoveTabOptionsMenuPopup(contextMenu); |
| 206 | + await clickMoveToProfileMenuItem(subMenu); |
| 207 | + |
| 208 | + let expectedArgs = [ |
| 209 | + "-new-tab", |
| 210 | + EXAMPLE_URL + "2", |
| 211 | + "-new-tab", |
| 212 | + EXAMPLE_URL + "3", |
| 213 | + "-new-tab", |
| 214 | + EXAMPLE_URL + "4", |
| 215 | + ]; |
| 216 | + |
| 217 | + await assertCommandLineExists([otherProfile.path, expectedArgs, true]); |
| 218 | + |
| 219 | + gBrowser.removeTabs([tab2, tab3, tab4]); |
| 220 | +}); |
0 commit comments