|
| 1 | +/* Any copyright is dedicated to the Public Domain. |
| 2 | + https://creativecommons.org/publicdomain/zero/1.0/ */ |
| 3 | + |
| 4 | +"use strict"; |
| 5 | + |
| 6 | +const TEST_URL = "https://example.com/"; |
| 7 | + |
| 8 | +function isInEssentialsContainer(tab) { |
| 9 | + return !!tab.parentElement.closest(".zen-essentials-container"); |
| 10 | +} |
| 11 | + |
| 12 | +add_task(async function test_Move_Essential_Tab_To_Window_Duplicates() { |
| 13 | + const tab = await BrowserTestUtils.openNewForegroundTab( |
| 14 | + gBrowser, |
| 15 | + TEST_URL, |
| 16 | + true |
| 17 | + ); |
| 18 | + |
| 19 | + gZenPinnedTabManager.addToEssentials(tab); |
| 20 | + ok(tab.hasAttribute("zen-essential"), "The original tab is essential"); |
| 21 | + ok(isInEssentialsContainer(tab), "The original tab is in Essentials"); |
| 22 | + |
| 23 | + const essentialCount = gBrowser._numZenEssentials; |
| 24 | + const newWindowPromise = BrowserTestUtils.waitForNewWindow(); |
| 25 | + const openedWindow = gBrowser.replaceTabWithWindow(tab); |
| 26 | + ok(openedWindow, "Moving an Essential tab opens a new window"); |
| 27 | + |
| 28 | + const newWindow = await newWindowPromise; |
| 29 | + await BrowserTestUtils.waitForCondition( |
| 30 | + () => newWindow.gBrowser.selectedBrowser.currentURI.spec == TEST_URL, |
| 31 | + "The duplicated tab loaded in the new window" |
| 32 | + ); |
| 33 | + |
| 34 | + const duplicatedTab = newWindow.gBrowser.selectedTab; |
| 35 | + ok( |
| 36 | + tab.hasAttribute("zen-essential"), |
| 37 | + "The original tab remains marked as essential" |
| 38 | + ); |
| 39 | + ok( |
| 40 | + isInEssentialsContainer(tab), |
| 41 | + "The original tab remains in the Essentials container" |
| 42 | + ); |
| 43 | + is( |
| 44 | + gBrowser._numZenEssentials, |
| 45 | + essentialCount, |
| 46 | + "The source window keeps the same number of Essentials" |
| 47 | + ); |
| 48 | + ok( |
| 49 | + !duplicatedTab.hasAttribute("zen-essential"), |
| 50 | + "The new window receives a regular tab copy" |
| 51 | + ); |
| 52 | + ok(!duplicatedTab.pinned, "The new window tab is not pinned"); |
| 53 | + is( |
| 54 | + duplicatedTab.linkedBrowser.currentURI.spec, |
| 55 | + TEST_URL, |
| 56 | + "The new window tab keeps the original URL" |
| 57 | + ); |
| 58 | + |
| 59 | + await BrowserTestUtils.closeWindow(newWindow); |
| 60 | + await BrowserTestUtils.removeTab(tab); |
| 61 | +}); |
| 62 | + |
| 63 | +add_task(async function test_Forced_Sync_Moves_Essential_Tab() { |
| 64 | + let newWindow; |
| 65 | + let duplicated = false; |
| 66 | + const tab = await BrowserTestUtils.openNewForegroundTab( |
| 67 | + gBrowser, |
| 68 | + TEST_URL, |
| 69 | + true |
| 70 | + ); |
| 71 | + |
| 72 | + gZenPinnedTabManager.addToEssentials(tab); |
| 73 | + ok(tab.hasAttribute("zen-essential"), "The original tab is essential"); |
| 74 | + |
| 75 | + const originalDuplicateTab = gBrowser.duplicateTab; |
| 76 | + gBrowser.duplicateTab = (...args) => { |
| 77 | + duplicated = true; |
| 78 | + return originalDuplicateTab.apply(gBrowser, args); |
| 79 | + }; |
| 80 | + |
| 81 | + try { |
| 82 | + const newWindowPromise = BrowserTestUtils.waitForNewWindow(); |
| 83 | + const openedWindow = gBrowser.replaceTabWithWindow( |
| 84 | + tab, |
| 85 | + {}, |
| 86 | + /* zenForceSync = */ true |
| 87 | + ); |
| 88 | + ok(openedWindow, "Forced sync opens a new window for the Essential tab"); |
| 89 | + |
| 90 | + newWindow = await newWindowPromise; |
| 91 | + ok(!duplicated, "Forced sync does not duplicate an Essential tab"); |
| 92 | + } finally { |
| 93 | + gBrowser.duplicateTab = originalDuplicateTab; |
| 94 | + |
| 95 | + if (newWindow && !newWindow.closed) { |
| 96 | + await BrowserTestUtils.closeWindow(newWindow); |
| 97 | + } |
| 98 | + for (const candidate of [...gBrowser.tabs]) { |
| 99 | + if ( |
| 100 | + candidate.linkedBrowser.currentURI.spec == TEST_URL && |
| 101 | + !candidate.closing |
| 102 | + ) { |
| 103 | + await BrowserTestUtils.removeTab(candidate); |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | +}); |
| 108 | + |
| 109 | +add_task(async function test_Move_Tab_Group_Label_To_Window_Still_Works() { |
| 110 | + let newWindow; |
| 111 | + const tab1 = await BrowserTestUtils.openNewForegroundTab( |
| 112 | + gBrowser, |
| 113 | + TEST_URL, |
| 114 | + true |
| 115 | + ); |
| 116 | + const tab2 = await BrowserTestUtils.openNewForegroundTab( |
| 117 | + gBrowser, |
| 118 | + "https://example.org/", |
| 119 | + true |
| 120 | + ); |
| 121 | + |
| 122 | + try { |
| 123 | + const group = gBrowser.addTabGroup([tab1, tab2], { insertBefore: tab1 }); |
| 124 | + |
| 125 | + const newWindowPromise = BrowserTestUtils.waitForNewWindow(); |
| 126 | + const openedWindow = gBrowser.replaceTabWithWindow(group.labelElement); |
| 127 | + ok(openedWindow, "Moving a tab group label opens a new window"); |
| 128 | + |
| 129 | + newWindow = await newWindowPromise; |
| 130 | + await BrowserTestUtils.waitForCondition( |
| 131 | + () => newWindow.gBrowser.tabs.length >= 2, |
| 132 | + "The grouped tabs moved to the new window" |
| 133 | + ); |
| 134 | + |
| 135 | + is( |
| 136 | + newWindow.gBrowser.tabs.length, |
| 137 | + 2, |
| 138 | + "The new window receives both grouped tabs" |
| 139 | + ); |
| 140 | + } finally { |
| 141 | + if (newWindow && !newWindow.closed) { |
| 142 | + await BrowserTestUtils.closeWindow(newWindow); |
| 143 | + } |
| 144 | + for (const tab of [tab1, tab2]) { |
| 145 | + if (tab.isConnected && !tab.closing) { |
| 146 | + await BrowserTestUtils.removeTab(tab); |
| 147 | + } |
| 148 | + } |
| 149 | + } |
| 150 | +}); |
0 commit comments