test_default_launcher_visible.py (9815B)
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 from marionette_driver import Wait 6 from marionette_driver.by import By 7 from marionette_harness import MarionetteTestCase 8 9 initial_prefs = { 10 "sidebar.revamp": False, 11 # Set browser restore previous session pref 12 # we'll need to examine behavior using restored sidebar properties 13 "browser.startup.page": 3, 14 } 15 16 17 class TestDefaultLauncherVisible(MarionetteTestCase): 18 def setUp(self): 19 MarionetteTestCase.setUp(self) 20 21 self.marionette.set_context("chrome") 22 23 def tearDown(self): 24 try: 25 # Make sure subsequent tests get a clean profile 26 self.marionette.restart(in_app=False, clean=True) 27 finally: 28 super().tearDown() 29 30 def _close_last_tab(self): 31 # "self.marionette.close" cannot be used because it doesn't 32 # allow closing the very last tab. 33 self.marionette.execute_script("window.close()") 34 35 def restart_with_prefs(self, prefs): 36 # set the prefs then restart the browser 37 self.marionette.enforce_gecko_prefs(prefs) 38 39 # Restore the context as used before the restart 40 self.marionette.set_context("chrome") 41 42 self.wait_for_sidebar_initialized() 43 44 def is_launcher_visible(self): 45 hidden = self.marionette.execute_script( 46 """ 47 const window = BrowserWindowTracker.getTopWindow(); 48 return window.SidebarController.sidebarContainer.hidden; 49 """ 50 ) 51 return not hidden 52 53 def is_button_visible(self): 54 visible = self.marionette.execute_script( 55 """ 56 const window = BrowserWindowTracker.getTopWindow(); 57 const placement = window.CustomizableUI.getPlacementOfWidget('sidebar-button'); 58 if (!placement) { 59 return false; 60 } 61 const node = window.document.getElementById("sidebar-button"); 62 return node && !node.hidden; 63 """ 64 ) 65 return visible 66 67 def click_toolbar_button(self): 68 # Click the button to show the launcher 69 self.marionette.execute_script( 70 """ 71 const window = BrowserWindowTracker.getTopWindow(); 72 return window.document.getElementById("sidebar-button").click() 73 """ 74 ) 75 76 def wait_for_sidebar_initialized(self): 77 self.marionette.set_context("chrome") 78 self.marionette.execute_async_script( 79 """ 80 let resolve = arguments[0]; 81 let { BrowserInitState } = ChromeUtils.importESModule("resource:///modules/BrowserGlue.sys.mjs"); 82 83 (async () => { 84 await BrowserInitState.startupIdleTaskPromise; 85 const win = BrowserWindowTracker.getTopWindow(); 86 await win.SidebarController.promiseInitialized; 87 })().then(resolve); 88 """ 89 ) 90 91 def test_first_use_default_visible_pref_false(self): 92 # We test with the default pre-148 pref values, then flip sidebar.revamp to true, 93 # for a profile that has never enabled or seen the sidebar launcher. 94 # We want to ensure the sidebar state is correctly persisted and restored 95 96 self.wait_for_sidebar_initialized() 97 98 self.assertFalse( 99 self.is_launcher_visible(), 100 "Sidebar launcher is hidden", 101 ) 102 self.assertFalse( 103 self.is_button_visible(), 104 "Sidebar toolbar button is hidden", 105 ) 106 107 # Mimic an update which enables sidebar.revamp for the first time 108 self.restart_with_prefs({ 109 "sidebar.revamp": True, 110 "browser.startup.page": 3, 111 }) 112 113 self.assertTrue( 114 self.is_button_visible(), 115 "Sidebar button should be visible", 116 ) 117 118 self.assertFalse( 119 self.is_launcher_visible(), 120 "Sidebar launcher is expected to be initially hidden when starting with sidebar.revamp", 121 ) 122 123 # Click the button and verify that sticks 124 self.click_toolbar_button() 125 126 Wait(self.marionette).until( 127 lambda _: self.is_launcher_visible(), 128 message="Sidebar launcher should now be visible", 129 ) 130 self.marionette.restart() 131 self.marionette.set_context("chrome") 132 self.wait_for_sidebar_initialized() 133 134 self.assertTrue( 135 self.is_launcher_visible(), 136 "Sidebar launcher remains visible because user showed it in the resumed session", 137 ) 138 139 def test_new_sidebar_enabled_via_settings(self): 140 self.wait_for_sidebar_initialized() 141 self.assertFalse( 142 self.is_launcher_visible(), 143 "Sidebar launcher is not visible", 144 ) 145 self.assertFalse( 146 self.is_button_visible(), 147 "Sidebar toolbar button is not visible", 148 ) 149 150 # Navigate to about:preferences and enable the new sidebar 151 self.marionette.set_context("content") 152 self.marionette.navigate("about:preferences") 153 154 self.marionette.find_element(By.ID, "browserLayoutShowSidebar").click() 155 156 self.marionette.set_context("chrome") 157 self.assertTrue( 158 self.marionette.get_pref("sidebar.revamp"), 159 "The sidebar.revamp pref should now be true", 160 ) 161 162 # We expect that to add the button to the toolbar 163 Wait(self.marionette).until( 164 lambda _: self.is_button_visible(), 165 message="The toolbar button is visible", 166 ) 167 168 # In this scenario, even when the defaultLauncherVisible is False, the launcher 169 # should have been shown 170 self.assertTrue( 171 self.is_launcher_visible(), 172 "The launcher is shown when revamp is enabled by the user", 173 ) 174 175 # And it should stay visible on restart 176 self.marionette.restart() 177 self.marionette.set_context("chrome") 178 self.wait_for_sidebar_initialized() 179 180 self.assertTrue( 181 self.marionette.get_pref("sidebar.revamp"), 182 "The sidebar.revamp pref should still be true", 183 ) 184 185 self.assertTrue( 186 self.is_launcher_visible(), 187 "Sidebar launcher should still be shown after restart", 188 ) 189 190 def test_new_sidebar_enabled_at_runtime_via_nimbus(self): 191 self.wait_for_sidebar_initialized() 192 self.assertFalse( 193 self.is_launcher_visible(), 194 "Sidebar launcher is not visible", 195 ) 196 self.assertFalse( 197 self.is_button_visible(), 198 "Sidebar toolbar button is not visible", 199 ) 200 201 # stub the getVariable function to return false so sidebar code thinks 202 # we're enrolled in an experiment 203 self.marionette.execute_script( 204 """ 205 const window = BrowserWindowTracker.getTopWindow(); 206 window.NimbusFeatures.sidebar.getVariable = () => false; 207 """ 208 ) 209 210 showLauncherOnEnabled = self.marionette.execute_script( 211 """ 212 const window = BrowserWindowTracker.getTopWindow(); 213 return window.SidebarController.SidebarManager.showLauncherOnEnabled; 214 """ 215 ) 216 self.assertFalse( 217 showLauncherOnEnabled, 218 "showLauncherOnEnabled should be false when with the mocked NimbusFeatures getVariable", 219 ) 220 221 # This mocks the enrollment in which Nimbus sets the following prefs 222 self.marionette.set_prefs({ 223 "sidebar.revamp": True, 224 "sidebar.revamp.defaultLauncherVisible": False, 225 }) 226 227 # We expect enabling the pref to add the button to the toolbar 228 Wait(self.marionette).until( 229 lambda _: self.is_button_visible(), 230 message="The toolbar button is visible", 231 ) 232 233 # In this scenario, we expect the launcher visibility to be determined by the nimbus variable 234 self.assertFalse( 235 self.is_launcher_visible(), 236 "The launcher is hidden when revamp is not initiated by the user", 237 ) 238 239 # And it should stay hidden on restart 240 self.marionette.restart() 241 self.marionette.set_context("chrome") 242 self.wait_for_sidebar_initialized() 243 244 self.assertFalse( 245 self.is_launcher_visible(), 246 "The launcher is remains hidden after a restart", 247 ) 248 249 def test_vertical_tabs_default_hidden(self): 250 # Verify that starting with verticalTabs enabled and default visibility false results in a visible 251 # launcher with the vertical tabstrip 252 self.restart_with_prefs({ 253 "sidebar.revamp": True, 254 "sidebar.verticalTabs": True, 255 "sidebar.visibility": "always-show", 256 }) 257 258 Wait(self.marionette).until( 259 lambda _: self.is_launcher_visible(), 260 message="Sidebar launcher should be initially visible", 261 ) 262 tabsWidth = self.marionette.execute_script( 263 """ 264 const window = BrowserWindowTracker.getTopWindow(); 265 return document.getElementById("vertical-tabs").getBoundingClientRect().width; 266 """ 267 ) 268 self.assertGreater(tabsWidth, 0, "#vertical-tabs element has width") 269 270 # switch to 'hide-sidebar' visibility mode and confirm the launcher becomes hidden 271 self.marionette.set_pref("sidebar.visibility", "hide-sidebar") 272 Wait(self.marionette).until( 273 lambda _: not self.is_launcher_visible(), 274 message="Sidebar launcher should become hidden when hide-sidebar visibility is set and defaultLauncherVisible2 is false", 275 )