test_initialize_vertical_tabs.py (10358B)
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.by import By 6 from marionette_harness import MarionetteTestCase 7 from mozfile import json 8 9 vertical_parent_id = "vertical-tabs" 10 horizontal_parent_id = "TabsToolbar-customization-target" 11 snapshot_pref = "browser.uiCustomization.horizontalTabstrip" 12 customization_pref = "browser.uiCustomization.state" 13 14 15 class TestInitializeVerticalTabs(MarionetteTestCase): 16 def setUp(self): 17 super().setUp() 18 self.marionette.set_context("chrome") 19 20 def tearDown(self): 21 try: 22 # Make sure subsequent tests get a clean profile 23 self.marionette.restart(in_app=False, clean=True) 24 finally: 25 super().tearDown() 26 27 def restart_with_prefs(self, prefs): 28 # We need to quit the browser and restart with the prefs already set 29 # in order to examine the startup behavior 30 for name, value in prefs.items(): 31 if value is None: 32 self.marionette.clear_pref(name) 33 else: 34 self.marionette.set_pref(name, value) 35 self.marionette.restart(clean=False, in_app=True) 36 self.marionette.set_context("chrome") 37 38 def get_area_widgets(self, area): 39 return self.marionette.execute_script( 40 f"return CustomizableUI.getWidgetIdsInArea(CustomizableUI.{area})" 41 ) 42 43 def check_tabs_toolbar_visibilities(self, orientation="vertical"): 44 self.marionette.set_context("chrome") 45 h_tabstoolbar = self.marionette.find_element(By.ID, "TabsToolbar") 46 v_tabstoolbar = self.marionette.find_element(By.ID, "vertical-tabs") 47 48 h_collapsed = self.marionette.execute_script( 49 "return document.getElementById(CustomizableUI.AREA_TABSTRIP).getAttribute('collapsed')" 50 ) 51 v_collapsed = self.marionette.execute_script( 52 "return document.getElementById(CustomizableUI.AREA_VERTICAL_TABSTRIP).getAttribute('collapsed')" 53 ) 54 55 if orientation == "vertical": 56 self.assertEqual( 57 h_collapsed, 58 "", 59 "Horizontal tab strip has expected collapsed attribute value", 60 ) 61 self.assertEqual( 62 v_collapsed, 63 None, 64 "Vertical tab strip has expected collapsed attribute value", 65 ) 66 67 self.assertFalse( 68 h_tabstoolbar.is_displayed(), "Horizontal tab strip is not displayed" 69 ) 70 71 self.assertTrue( 72 v_tabstoolbar.is_displayed(), "Vertical tab strip is displayed" 73 ) 74 self.assertTrue( 75 v_tabstoolbar.rect["width"] > 0, "Vertical tab strip has > 0 width" 76 ) 77 else: 78 self.assertEqual( 79 v_collapsed, 80 "", 81 "Vertical tab strip has expected collapsed attribute value", 82 ) 83 84 self.assertTrue( 85 h_tabstoolbar.is_displayed(), "Horizontal tab strip is displayed" 86 ) 87 self.assertTrue( 88 h_tabstoolbar.rect["height"] > 0, "Horizontal tab strip has > 0 height" 89 ) 90 91 self.assertFalse( 92 v_tabstoolbar.is_displayed(), "Vertical tab strip is not displayed" 93 ) 94 self.assertEqual( 95 v_tabstoolbar.rect["width"], 0, "Vertical tab strip has 0 width" 96 ) 97 98 def test_vertical_widgets_in_area(self): 99 # A clean startup in verticalTabs mode; we should get all the defaults 100 self.restart_with_prefs({ 101 "sidebar.revamp": True, 102 "sidebar.verticalTabs": True, 103 customization_pref: None, 104 snapshot_pref: None, 105 }) 106 horiz_tab_ids = self.get_area_widgets("AREA_TABSTRIP") 107 vertical_tab_ids = self.get_area_widgets("AREA_VERTICAL_TABSTRIP") 108 109 self.assertEqual( 110 len(horiz_tab_ids), 111 0, 112 msg="The horizontal tabstrip area is empty", 113 ) 114 self.assertEqual( 115 len(vertical_tab_ids), 116 1, 117 msg="The vertical tabstrip area has a single widget in it", 118 ) 119 120 self.check_tabs_toolbar_visibilities("vertical") 121 122 # Check we're able to recover if we initialize with vertical tabs enabled 123 # and no saved pref for the horizontal tab strip placements 124 self.marionette.set_pref("sidebar.verticalTabs", False) 125 126 horiz_tab_ids = self.get_area_widgets("AREA_TABSTRIP") 127 vertical_tab_ids = self.get_area_widgets("AREA_VERTICAL_TABSTRIP") 128 129 self.check_tabs_toolbar_visibilities("horizontal") 130 131 # Make sure we ended up with sensible defaults 132 self.assertEqual( 133 horiz_tab_ids, 134 [ 135 "firefox-view-button", 136 "tabbrowser-tabs", 137 "new-tab-button", 138 "alltabs-button", 139 ], 140 msg="The tabstrip was populated with the expected defaults", 141 ) 142 143 self.assertEqual( 144 len(vertical_tab_ids), 145 0, 146 msg="The vertical tabstrip area was emptied", 147 ) 148 149 def test_restore_tabstrip_customizations(self): 150 fixture_prefs = { 151 "sidebar.revamp": True, 152 "sidebar.verticalTabs": False, 153 } 154 self.restart_with_prefs({ 155 **fixture_prefs, 156 customization_pref: None, 157 snapshot_pref: None, 158 }) 159 160 # Add a widget at the start of the horizontal tabstrip 161 # This is synchronous and should result in updating the UI and the saved state in uiCustomization pref 162 self.marionette.execute_script( 163 "CustomizableUI.addWidgetToArea('panic-button', CustomizableUI.AREA_TABSTRIP, 0)" 164 ) 165 166 saved_state = json.loads(self.marionette.get_pref(customization_pref)) 167 horiz_tab_ids = self.get_area_widgets("AREA_TABSTRIP") 168 self.assertTrue( 169 "panic-button" in horiz_tab_ids, "The widget we added is in the tabstrip" 170 ) 171 172 self.assertTrue( 173 "panic-button" in saved_state["placements"]["TabsToolbar"], 174 "The widget we added is included in the saved customization state", 175 ) 176 177 # Restart with vertical tabs enabled, leaving the uiCustomizations prefs as-is 178 # We want to ensure initialization puts us in a good state without needing user 179 # input to trigger the orientation change 180 fixture_prefs["sidebar.verticalTabs"] = True 181 self.restart_with_prefs(fixture_prefs) 182 183 saved_state = json.loads(self.marionette.get_pref(customization_pref)) 184 185 self.check_tabs_toolbar_visibilities("vertical") 186 187 horiz_tab_ids = self.get_area_widgets("AREA_TABSTRIP") 188 nav_bar_ids = self.get_area_widgets("AREA_NAVBAR") 189 190 self.assertEqual( 191 len(horiz_tab_ids), 192 0, 193 msg="The horizontal tabstrip area is empty", 194 ) 195 self.assertTrue( 196 "panic-button" in nav_bar_ids, "The widget we added has moved to the navbar" 197 ) 198 199 # Restart with horizontal tabs enabled. We want to ensure customizing the 200 # panic-button into the tabstrip is correctly restored at initialization, 201 # without needing user-input to trigger the orientation change 202 fixture_prefs["sidebar.verticalTabs"] = False 203 self.restart_with_prefs(fixture_prefs) 204 205 self.check_tabs_toolbar_visibilities("horizontal") 206 207 horiz_tab_ids = self.get_area_widgets("AREA_TABSTRIP") 208 209 self.assertEqual( 210 horiz_tab_ids[0], 211 "panic-button", 212 msg="The customization was preserved after restarting in horizontal tabs mode", 213 ) 214 215 def test_preserve_visibility_pref_after_restart(self): 216 fixture_prefs = { 217 "sidebar.revamp": True, 218 "sidebar.verticalTabs": True, 219 "sidebar.visibility": "hide-sidebar", 220 } 221 self.restart_with_prefs({ 222 **fixture_prefs, 223 customization_pref: None, 224 snapshot_pref: None, 225 }) 226 227 pref_value = self.marionette.execute_script( 228 """ 229 return Services.prefs.getStringPref("sidebar.visibility", null); 230 """ 231 ) 232 self.assertEqual(pref_value, "hide-sidebar") 233 234 # Restart with no user visibility pref. We should get the default for vertical tabs 235 # which is always-show 236 fixture_prefs["sidebar.visibility"] = None 237 self.restart_with_prefs(fixture_prefs) 238 239 pref_value = self.marionette.execute_script( 240 """ 241 return Services.prefs.getStringPref("sidebar.visibility", null); 242 """ 243 ) 244 self.assertEqual(pref_value, "always-show") 245 246 # Restart with vertical tabs disabled. We should get the default for horizontal tabs 247 # which is hide-sidebar 248 249 fixture_prefs["sidebar.visibility"] = None 250 fixture_prefs["sidebar.verticalTabs"] = False 251 self.restart_with_prefs(fixture_prefs) 252 253 pref_value = self.marionette.execute_script( 254 """ 255 return Services.prefs.getStringPref("sidebar.visibility", null); 256 """ 257 ) 258 self.assertEqual(pref_value, "hide-sidebar") 259 260 def test_hide_drag_to_pin_promo_if_horizontal_tabs_pinned(self): 261 # Pin a tab using the horizontal tabstrip. 262 self.restart_with_prefs({ 263 "sidebar.revamp": False, 264 "sidebar.verticalTabs": False, 265 }) 266 self.marionette.execute_async_script( 267 """ 268 let resolve = arguments[0]; 269 let tab = gBrowser.selectedTab; 270 tab.addEventListener("TabPinned", resolve, { once: true }); 271 gBrowser.pinTab(tab); 272 """ 273 ) 274 275 # Switch to vertical tabs. 276 self.marionette.execute_script( 277 """ 278 Services.prefs.setBoolPref("sidebar.verticalTabs", true); 279 """ 280 ) 281 282 promo_card = self.marionette.find_element(By.ID, "drag-to-pin-promo-card") 283 self.assertFalse( 284 promo_card.is_displayed(), "Drag-to-pin promo card is not displayed." 285 )