browser_sidebar_toggle.js (4118B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 5 "use strict"; 6 7 registerCleanupFunction(async function () { 8 await resetCustomization(); 9 10 // Ensure sidebar is hidden after each test: 11 if (!document.getElementById("sidebar-box").hidden) { 12 SidebarController.hide(); 13 } 14 }); 15 16 async function ensureSidebarLauncherIsVisible() { 17 await TestUtils.waitForTick(); 18 // Show the sidebar launcher if its hidden 19 if (SidebarController.sidebarContainer.hidden) { 20 document.getElementById("sidebar-button").doCommand(); 21 } 22 await TestUtils.waitForTick(); 23 Assert.ok( 24 BrowserTestUtils.isVisible(SidebarController.sidebarMain), 25 "Sidebar launcher is visible" 26 ); 27 } 28 29 var showSidebar = async function (win = window) { 30 let button = win.document.getElementById("sidebar-button"); 31 let sidebarFocusedPromise = BrowserTestUtils.waitForEvent( 32 win.document, 33 "SidebarFocused" 34 ); 35 EventUtils.synthesizeMouseAtCenter(button, {}, win); 36 await sidebarFocusedPromise; 37 ok(win.SidebarController.isOpen, "Sidebar is opened"); 38 ok(button.hasAttribute("checked"), "Toolbar button is checked"); 39 }; 40 41 var hideSidebar = async function (win = window) { 42 let button = win.document.getElementById("sidebar-button"); 43 let box = win.document.getElementById("sidebar-box"); 44 45 EventUtils.synthesizeMouseAtCenter(button, {}, win); 46 await BrowserTestUtils.waitForMutationCondition( 47 box, 48 { attributes: true, attributeFilter: ["hidden"] }, 49 () => box.hidden 50 ); 51 ok(!win.SidebarController.isOpen, "Sidebar is closed"); 52 ok(!button.hasAttribute("checked"), "Toolbar button isn't checked"); 53 }; 54 55 // Check the sidebar widget shows the default items 56 add_task(async function () { 57 let sidebarRevampEnabled = Services.prefs.getBoolPref( 58 "sidebar.revamp", 59 false 60 ); 61 info(`sidebarRevampEnabled: ${sidebarRevampEnabled}`); 62 if (!sidebarRevampEnabled) { 63 CustomizableUI.addWidgetToArea("sidebar-button", "nav-bar"); 64 65 await showSidebar(); 66 is( 67 SidebarController.currentID, 68 "viewBookmarksSidebar", 69 "Default sidebar selected" 70 ); 71 await SidebarController.show("viewHistorySidebar"); 72 73 await hideSidebar(); 74 await showSidebar(); 75 is( 76 SidebarController.currentID, 77 "viewHistorySidebar", 78 "Selected sidebar remembered" 79 ); 80 81 await hideSidebar(); 82 } else { 83 const sidebar = document.querySelector("sidebar-main"); 84 await ensureSidebarLauncherIsVisible(); 85 for (const [index, toolButton] of sidebar.toolButtons.entries()) { 86 await SidebarController.toggle(toolButton.getAttribute("view")); 87 is( 88 SidebarController.currentID, 89 toolButton.getAttribute("view"), 90 `${toolButton.getAttribute("view")} sidebar selected` 91 ); 92 if (index < sidebar.toolButtons.length - 1) { 93 SidebarController.toggle(toolButton.getAttribute("view")); 94 } 95 } 96 } 97 let otherWin = await BrowserTestUtils.openNewBrowserWindow(); 98 info("Waiting for the sidebar to initialize in the new browser window"); 99 await BrowserTestUtils.waitForCondition( 100 () => otherWin.SidebarController.uiStateInitialized, 101 "The uiStateInitialized is true in the new window" 102 ); 103 if (!sidebarRevampEnabled) { 104 ok( 105 !otherWin.SidebarController.isOpen, 106 "The sidebar panel isn't open in the new window" 107 ); 108 await showSidebar(otherWin); 109 is( 110 otherWin.SidebarController.currentID, 111 "viewHistorySidebar", 112 "Selected sidebar remembered across windows" 113 ); 114 await hideSidebar(otherWin); 115 } else { 116 let otherSidebar = otherWin.document.querySelector("sidebar-main"); 117 let lastTool = 118 otherSidebar.toolButtons[otherSidebar.toolButtons.length - 1]; 119 is( 120 otherWin.SidebarController.currentID, 121 lastTool.getAttribute("view"), 122 "Selected sidebar remembered across windows" 123 ); 124 otherWin.SidebarController.toggle(lastTool.getAttribute("view")); 125 } 126 127 await BrowserTestUtils.closeWindow(otherWin); 128 });