browser_aboutNewTab_bookmarksToolbar.js (11823B)
1 /* Any copyright is dedicated to the Public Domain. 2 * http://creativecommons.org/publicdomain/zero/1.0/ */ 3 4 "use strict"; 5 6 add_setup(async function () { 7 await SpecialPowers.pushPrefEnv({ 8 set: [["test.wait300msAfterTabSwitch", true]], 9 }); 10 }); 11 12 add_task(async function bookmarks_toolbar_shown_on_newtab() { 13 let newtab = await BrowserTestUtils.openNewForegroundTab({ 14 gBrowser, 15 opening: "about:newtab", 16 waitForLoad: false, 17 }); 18 19 // 1: Test that the toolbar is shown in a newly opened foreground about:newtab 20 await waitForBookmarksToolbarVisibility({ 21 visible: true, 22 message: "Toolbar should be visible on newtab", 23 }); 24 ok(isBookmarksToolbarVisible(), "Toolbar should be visible on newtab"); 25 26 // 2: Test that the toolbar is hidden when the browser is navigated away from newtab 27 BrowserTestUtils.startLoadingURIString( 28 newtab.linkedBrowser, 29 "https://example.com" 30 ); 31 await BrowserTestUtils.browserLoaded(newtab.linkedBrowser); 32 await waitForBookmarksToolbarVisibility({ 33 visible: false, 34 message: 35 "Toolbar should not be visible on newtab after example.com is loaded within", 36 }); 37 ok( 38 !isBookmarksToolbarVisible(), 39 "Toolbar should not be visible on newtab after example.com is loaded within" 40 ); 41 42 // 3: Re-load about:newtab in the browser for the following tests and confirm toolbar reappears 43 BrowserTestUtils.startLoadingURIString(newtab.linkedBrowser, "about:newtab"); 44 await BrowserTestUtils.browserLoaded(newtab.linkedBrowser); 45 await waitForBookmarksToolbarVisibility({ 46 visible: true, 47 message: "Toolbar should be visible on newtab", 48 }); 49 ok(isBookmarksToolbarVisible(), "Toolbar should be visible on newtab"); 50 51 // 4: Toolbar should get hidden when opening a new tab to example.com 52 let example = await BrowserTestUtils.openNewForegroundTab({ 53 gBrowser, 54 opening: "https://example.com", 55 }); 56 await waitForBookmarksToolbarVisibility({ 57 visible: false, 58 message: "Toolbar should be hidden on example.com", 59 }); 60 61 // 5: Toolbar should become visible when switching tabs to newtab 62 await BrowserTestUtils.switchTab(gBrowser, newtab); 63 await waitForBookmarksToolbarVisibility({ 64 visible: true, 65 message: "Toolbar is visible with switch to newtab", 66 }); 67 ok(isBookmarksToolbarVisible(), "Toolbar is visible with switch to newtab"); 68 69 // 6: Toolbar should become hidden when switching tabs to example.com 70 await BrowserTestUtils.switchTab(gBrowser, example); 71 await waitForBookmarksToolbarVisibility({ 72 visible: false, 73 message: "Toolbar is hidden with switch to example", 74 }); 75 76 // 7: Similar to #3 above, loading about:newtab in example should show toolbar 77 BrowserTestUtils.startLoadingURIString(example.linkedBrowser, "about:newtab"); 78 await BrowserTestUtils.browserLoaded(example.linkedBrowser); 79 await waitForBookmarksToolbarVisibility({ 80 visible: true, 81 message: "Toolbar is visible with newtab load", 82 }); 83 ok(isBookmarksToolbarVisible(), "Toolbar is visible with newtab load"); 84 85 // 8: Switching back and forth between two browsers showing about:newtab will still show the toolbar 86 await BrowserTestUtils.switchTab(gBrowser, newtab); 87 ok(isBookmarksToolbarVisible(), "Toolbar is visible with switch to newtab"); 88 await BrowserTestUtils.switchTab(gBrowser, example); 89 ok( 90 isBookmarksToolbarVisible(), 91 "Toolbar is visible with switch to example(newtab)" 92 ); 93 94 // 9: With custom newtab URL, toolbar isn't shown on about:newtab but is shown on custom URL 95 let oldNewTab = AboutNewTab.newTabURL; 96 AboutNewTab.newTabURL = "https://example.com/2"; 97 await BrowserTestUtils.switchTab(gBrowser, newtab); 98 await waitForBookmarksToolbarVisibility({ visible: false }); 99 ok(!isBookmarksToolbarVisible(), "Toolbar should hide with custom newtab"); 100 BrowserTestUtils.startLoadingURIString( 101 example.linkedBrowser, 102 AboutNewTab.newTabURL 103 ); 104 await BrowserTestUtils.browserLoaded(example.linkedBrowser); 105 await BrowserTestUtils.switchTab(gBrowser, example); 106 await waitForBookmarksToolbarVisibility({ visible: true }); 107 ok( 108 isBookmarksToolbarVisible(), 109 "Toolbar is visible with switch to custom newtab" 110 ); 111 112 await BrowserTestUtils.removeTab(newtab); 113 await BrowserTestUtils.removeTab(example); 114 AboutNewTab.newTabURL = oldNewTab; 115 }); 116 117 add_task(async function bookmarks_toolbar_open_persisted() { 118 let newtab = await BrowserTestUtils.openNewForegroundTab({ 119 gBrowser, 120 opening: "about:newtab", 121 waitForLoad: false, 122 }); 123 let example = await BrowserTestUtils.openNewForegroundTab({ 124 gBrowser, 125 opening: "https://example.com", 126 }); 127 let isToolbarPersistedOpen = () => 128 Services.prefs.getCharPref("browser.toolbars.bookmarks.visibility") == 129 "always"; 130 131 ok(!isBookmarksToolbarVisible(), "Toolbar is hidden"); 132 await BrowserTestUtils.switchTab(gBrowser, newtab); 133 await waitForBookmarksToolbarVisibility({ visible: true }); 134 ok(isBookmarksToolbarVisible(), "Toolbar is visible"); 135 await BrowserTestUtils.switchTab(gBrowser, example); 136 await waitForBookmarksToolbarVisibility({ visible: false }); 137 ok(!isBookmarksToolbarVisible(), "Toolbar is hidden"); 138 ok(!isToolbarPersistedOpen(), "Toolbar is not persisted open"); 139 140 let contextMenu = document.querySelector("#toolbar-context-menu"); 141 let popupShown = BrowserTestUtils.waitForEvent(contextMenu, "popupshown"); 142 let menuButton = document.getElementById("PanelUI-menu-button"); 143 EventUtils.synthesizeMouseAtCenter( 144 menuButton, 145 { type: "contextmenu" }, 146 window 147 ); 148 await popupShown; 149 let bookmarksToolbarMenu = document.querySelector("#toggle_PersonalToolbar"); 150 let subMenu = bookmarksToolbarMenu.querySelector("menupopup"); 151 popupShown = BrowserTestUtils.waitForEvent(subMenu, "popupshown"); 152 bookmarksToolbarMenu.openMenu(true); 153 await popupShown; 154 let alwaysMenuItem = document.querySelector( 155 'menuitem[data-visibility-enum="always"]' 156 ); 157 let neverMenuItem = document.querySelector( 158 'menuitem[data-visibility-enum="never"]' 159 ); 160 let newTabMenuItem = document.querySelector( 161 'menuitem[data-visibility-enum="newtab"]' 162 ); 163 ok(!alwaysMenuItem.hasAttribute("checked"), "Menuitem isn't checked"); 164 ok(!neverMenuItem.hasAttribute("checked"), "Menuitem isn't checked"); 165 ok(newTabMenuItem.hasAttribute("checked"), "Menuitem is checked"); 166 167 subMenu.activateItem(alwaysMenuItem); 168 169 await waitForBookmarksToolbarVisibility({ visible: true }); 170 popupShown = BrowserTestUtils.waitForEvent(contextMenu, "popupshown"); 171 EventUtils.synthesizeMouseAtCenter( 172 menuButton, 173 { type: "contextmenu" }, 174 window 175 ); 176 await popupShown; 177 bookmarksToolbarMenu = document.querySelector("#toggle_PersonalToolbar"); 178 subMenu = bookmarksToolbarMenu.querySelector("menupopup"); 179 popupShown = BrowserTestUtils.waitForEvent(subMenu, "popupshown"); 180 bookmarksToolbarMenu.openMenu(true); 181 await popupShown; 182 alwaysMenuItem = document.querySelector( 183 'menuitem[data-visibility-enum="always"]' 184 ); 185 neverMenuItem = document.querySelector( 186 'menuitem[data-visibility-enum="never"]' 187 ); 188 newTabMenuItem = document.querySelector( 189 'menuitem[data-visibility-enum="newtab"]' 190 ); 191 ok(alwaysMenuItem.hasAttribute("checked"), "Menuitem is checked"); 192 ok(!neverMenuItem.hasAttribute("checked"), "Menuitem isn't checked"); 193 ok(!newTabMenuItem.hasAttribute("checked"), "Menuitem isn't checked"); 194 contextMenu.hidePopup(); 195 ok(isBookmarksToolbarVisible(), "Toolbar is visible"); 196 ok(isToolbarPersistedOpen(), "Toolbar is persisted open"); 197 await BrowserTestUtils.switchTab(gBrowser, newtab); 198 ok(isBookmarksToolbarVisible(), "Toolbar is visible"); 199 await BrowserTestUtils.switchTab(gBrowser, example); 200 ok(isBookmarksToolbarVisible(), "Toolbar is visible"); 201 202 popupShown = BrowserTestUtils.waitForEvent(contextMenu, "popupshown"); 203 EventUtils.synthesizeMouseAtCenter( 204 menuButton, 205 { type: "contextmenu" }, 206 window 207 ); 208 await popupShown; 209 bookmarksToolbarMenu = document.querySelector("#toggle_PersonalToolbar"); 210 subMenu = bookmarksToolbarMenu.querySelector("menupopup"); 211 popupShown = BrowserTestUtils.waitForEvent(subMenu, "popupshown"); 212 bookmarksToolbarMenu.openMenu(true); 213 await popupShown; 214 alwaysMenuItem = document.querySelector( 215 'menuitem[data-visibility-enum="always"]' 216 ); 217 neverMenuItem = document.querySelector( 218 'menuitem[data-visibility-enum="never"]' 219 ); 220 newTabMenuItem = document.querySelector( 221 'menuitem[data-visibility-enum="newtab"]' 222 ); 223 ok(alwaysMenuItem.hasAttribute("checked"), "Menuitem is checked"); 224 ok(!neverMenuItem.hasAttribute("checked"), "Menuitem isn't checked"); 225 ok(!newTabMenuItem.hasAttribute("checked"), "Menuitem isn't checked"); 226 subMenu.activateItem(newTabMenuItem); 227 await waitForBookmarksToolbarVisibility({ 228 visible: false, 229 message: "Toolbar is hidden", 230 }); 231 await BrowserTestUtils.switchTab(gBrowser, newtab); 232 await waitForBookmarksToolbarVisibility({ 233 visible: true, 234 message: "Toolbar is visible", 235 }); 236 await BrowserTestUtils.switchTab(gBrowser, example); 237 await waitForBookmarksToolbarVisibility({ 238 visible: false, 239 message: "Toolbar is hidden", 240 }); 241 242 await BrowserTestUtils.removeTab(newtab); 243 await BrowserTestUtils.removeTab(example); 244 }); 245 246 add_task(async function test_with_newtabpage_disabled() { 247 await SpecialPowers.pushPrefEnv({ 248 set: [["browser.newtabpage.enabled", true]], 249 }); 250 251 let tabCount = gBrowser.tabs.length; 252 document.getElementById("cmd_newNavigatorTab").doCommand(); 253 // Can't use BrowserTestUtils.waitForNewTab since onLocationChange will not 254 // fire due to preloaded new tabs. 255 await TestUtils.waitForCondition(() => gBrowser.tabs.length == tabCount + 1); 256 let newtab = gBrowser.selectedTab; 257 is(newtab.linkedBrowser.currentURI.spec, "about:newtab", "newtab is loaded"); 258 await waitForBookmarksToolbarVisibility({ 259 visible: true, 260 message: "Toolbar is visible with NTP enabled", 261 }); 262 let firstid = await SpecialPowers.spawn(newtab.linkedBrowser, [], () => { 263 return content.document.body.firstElementChild?.id; 264 }); 265 is(firstid, "root", "new tab page contains content"); 266 await BrowserTestUtils.removeTab(newtab); 267 268 await SpecialPowers.pushPrefEnv({ 269 set: [["browser.newtabpage.enabled", false]], 270 }); 271 272 document.getElementById("cmd_newNavigatorTab").doCommand(); 273 await TestUtils.waitForCondition(() => gBrowser.tabs.length == tabCount + 1); 274 newtab = gBrowser.selectedTab; 275 276 await waitForBookmarksToolbarVisibility({ 277 visible: true, 278 message: "Toolbar is visible with NTP disabled", 279 }); 280 281 is( 282 newtab.linkedBrowser.currentURI.spec, 283 "about:newtab", 284 "blank new tab is loaded" 285 ); 286 firstid = await SpecialPowers.spawn(newtab.linkedBrowser, [], () => { 287 return content.document.body.firstElementChild; 288 }); 289 ok(!firstid, "blank new tab page contains no content"); 290 291 await BrowserTestUtils.removeTab(newtab); 292 293 await SpecialPowers.pushPrefEnv({ 294 set: [["browser.newtabpage.enabled", true]], 295 }); 296 }); 297 298 add_task(async function test_history_pushstate() { 299 await BrowserTestUtils.withNewTab("https://example.com/", async browser => { 300 await waitForBookmarksToolbarVisibility({ visible: false }); 301 ok(!isBookmarksToolbarVisible(), "Toolbar should be hidden"); 302 303 // Temporarily show the toolbar: 304 setToolbarVisibility( 305 document.querySelector("#PersonalToolbar"), 306 true, 307 false, 308 false 309 ); 310 ok(isBookmarksToolbarVisible(), "Toolbar should now be visible"); 311 312 // Now "navigate" 313 await SpecialPowers.spawn(browser, [], () => { 314 content.location.href += "#foo"; 315 }); 316 317 await TestUtils.waitForCondition( 318 () => gURLBar.value.endsWith("#foo"), 319 "URL bar should update" 320 ); 321 ok(isBookmarksToolbarVisible(), "Toolbar should still be visible"); 322 }); 323 });