browser_test_toolbars_visibility.js (10470B)
1 // Tests that toolbars have proper visibility when opening a new window 2 // in either content or chrome context. 3 4 const ROOT = "http://www.example.com/browser/dom/tests/browser/"; 5 const CONTENT_PAGE = ROOT + "test_new_window_from_content_child.html"; 6 const TARGET_PAGE = ROOT + "dummy.html"; 7 8 /** 9 * This function retrieves the visibility state of the toolbars of a 10 * window within the content context. 11 * 12 * @param aBrowser (<xul:browser>) 13 * The browser to query for toolbar visibility states 14 * @returns Promise 15 * A promise that resolves when the toolbar state is retrieved 16 * within the content context, which value is an object that holds 17 * the visibility state of the toolbars 18 */ 19 function getToolbarsFromBrowserContent(aBrowser) { 20 return SpecialPowers.spawn(aBrowser, [], async function () { 21 // This is still chrome context. 22 // Inject a script that runs on content context, and gather the result. 23 24 let script = content.document.createElement("script"); 25 script.textContent = ` 26 let bars = [ 27 "toolbar", 28 "menubar", 29 "personalbar", 30 "statusbar", 31 "scrollbars", 32 "locationbar", 33 ]; 34 35 for (let bar of bars) { 36 let node = document.createElement("span"); 37 node.id = bar; 38 node.textContent = window[bar].visible; 39 document.body.appendChild(node); 40 } 41 `; 42 content.document.body.appendChild(script); 43 44 let result = {}; 45 46 let bars = [ 47 "toolbar", 48 "menubar", 49 "personalbar", 50 "statusbar", 51 "scrollbars", 52 "locationbar", 53 ]; 54 55 for (let bar of bars) { 56 let node = content.document.getElementById(bar); 57 let value = node.textContent; 58 if (value !== "true" && value !== "false") { 59 throw new Error("bar visibility isn't set"); 60 } 61 result[bar] = value === "true"; 62 node.remove(); 63 } 64 65 return result; 66 }); 67 } 68 69 /** 70 * This function retrieves the visibility state of the toolbars of a 71 * window within the chrome context. 72 * 73 * @param win 74 * the chrome privileged window 75 * @returns object 76 * an object that holds the visibility state of the toolbars 77 */ 78 function getToolbarsFromWindowChrome(win) { 79 return { 80 toolbar: win.toolbar.visible, 81 menubar: win.menubar.visible, 82 personalbar: win.personalbar.visible, 83 statusbar: win.statusbar.visible, 84 scrollbars: win.scrollbars.visible, 85 locationbar: win.locationbar.visible, 86 }; 87 } 88 89 /** 90 * Tests toolbar visibility when opening a window with default parameters. 91 * 92 * @param toolbars 93 * the visibility state of the toolbar elements 94 */ 95 function testDefaultToolbars(toolbars) { 96 ok( 97 toolbars.locationbar, 98 "locationbar should be visible on default window.open()" 99 ); 100 ok(toolbars.menubar, "menubar be visible on default window.open()"); 101 ok( 102 toolbars.personalbar, 103 "personalbar should be visible on default window.open()" 104 ); 105 ok( 106 toolbars.statusbar, 107 "statusbar should be visible on default window.open()" 108 ); 109 ok( 110 toolbars.scrollbars, 111 "scrollbars should be visible on default window.open()" 112 ); 113 ok(toolbars.toolbar, "toolbar should be visible on default window.open()"); 114 } 115 116 /** 117 * Tests toolbar visibility when opening a popup window on the content context, 118 * and reading the value from context context. 119 * 120 * @param toolbars 121 * the visibility state of the toolbar elements 122 */ 123 function testNonDefaultContentToolbarsFromContent(toolbars) { 124 // Accessing BarProp.visible from content context should return false for 125 // popup, regardless of the each feature value in window.open parameter. 126 ok(!toolbars.locationbar, "locationbar.visible should be false for popup"); 127 ok(!toolbars.menubar, "menubar.visible should be false for popup"); 128 ok(!toolbars.personalbar, "personalbar.visible should be false for popup"); 129 ok(!toolbars.statusbar, "statusbar.visible should be false for popup"); 130 ok(!toolbars.scrollbars, "scrollbars.visible should be false for popup"); 131 ok(!toolbars.toolbar, "toolbar.visible should be false for popup"); 132 } 133 134 /** 135 * Tests toolbar visibility when opening a popup window on the content context, 136 * and reading the value from chrome context. 137 * 138 * @param toolbars 139 * the visibility state of the toolbar elements 140 */ 141 function testNonDefaultContentToolbarsFromChrome(toolbars) { 142 // Accessing BarProp.visible from chrome context should return the 143 // actual visibility. 144 145 // Locationbar should always be visible on content context 146 ok( 147 toolbars.locationbar, 148 "locationbar should be visible even with location=no" 149 ); 150 ok(!toolbars.menubar, "menubar shouldn't be visible when menubar=no"); 151 ok( 152 !toolbars.personalbar, 153 "personalbar shouldn't be visible when personalbar=no" 154 ); 155 // statusbar will report visible=true even when it's hidden because of bug#55820 156 todo(!toolbars.statusbar, "statusbar shouldn't be visible when status=no"); 157 ok( 158 toolbars.scrollbars, 159 "scrollbars should be visible even with scrollbars=no" 160 ); 161 ok(!toolbars.toolbar, "toolbar shouldn't be visible when toolbar=no"); 162 } 163 164 /** 165 * Tests toolbar visibility when opening a window with non default parameters 166 * on the chrome context. 167 * 168 * @param toolbars 169 * the visibility state of the toolbar elements 170 */ 171 function testNonDefaultChromeToolbars(toolbars) { 172 // None of the toolbars should be visible if hidden with chrome privileges 173 ok( 174 !toolbars.locationbar, 175 "locationbar should not be visible with location=no" 176 ); 177 ok(!toolbars.menubar, "menubar should not be visible with menubar=no"); 178 ok( 179 !toolbars.personalbar, 180 "personalbar should not be visible with personalbar=no" 181 ); 182 ok(!toolbars.statusbar, "statusbar should not be visible with status=no"); 183 ok( 184 toolbars.scrollbars, 185 "scrollbars should be visible even with scrollbars=no" 186 ); 187 ok(!toolbars.toolbar, "toolbar should not be visible with toolbar=no"); 188 } 189 190 add_setup(async function () { 191 await SpecialPowers.pushPrefEnv({ 192 set: [["test.wait300msAfterTabSwitch", true]], 193 }); 194 }); 195 196 /** 197 * Ensure that toolbars of a window opened in the content context have the 198 * correct visibility. 199 * 200 * A window opened with default parameters should have all toolbars visible. 201 * 202 * A window opened with "location=no, personalbar=no, toolbar=no, scrollbars=no, 203 * menubar=no, status=no", should only have location visible. 204 */ 205 add_task(async function () { 206 await BrowserTestUtils.withNewTab( 207 { 208 gBrowser, 209 url: CONTENT_PAGE, 210 }, 211 async function (browser) { 212 // First, call the default window.open() which will open a new tab 213 let newTabPromise = BrowserTestUtils.waitForNewTab(gBrowser); 214 await BrowserTestUtils.synthesizeMouseAtCenter( 215 "#winOpenDefault", 216 {}, 217 browser 218 ); 219 let tab = await newTabPromise; 220 221 // Check that all toolbars are visible 222 let toolbars = await getToolbarsFromBrowserContent( 223 gBrowser.selectedBrowser 224 ); 225 testDefaultToolbars(toolbars); 226 227 // Cleanup 228 BrowserTestUtils.removeTab(tab); 229 230 // Now let's open a window with toolbars hidden 231 let winPromise = BrowserTestUtils.waitForNewWindow({ url: TARGET_PAGE }); 232 await BrowserTestUtils.synthesizeMouseAtCenter( 233 "#winOpenNonDefault", 234 {}, 235 browser 236 ); 237 let popupWindow = await winPromise; 238 239 let popupBrowser = popupWindow.gBrowser.selectedBrowser; 240 241 // Test toolbars visibility value from content. 242 let popupToolbars = await getToolbarsFromBrowserContent(popupBrowser); 243 testNonDefaultContentToolbarsFromContent(popupToolbars); 244 245 // Test toolbars visibility value from chrome. 246 let chromeToolbars = getToolbarsFromWindowChrome(popupWindow); 247 testNonDefaultContentToolbarsFromChrome(chromeToolbars); 248 249 // Close the new window 250 await BrowserTestUtils.closeWindow(popupWindow); 251 } 252 ); 253 }); 254 255 /** 256 * Ensure that toolbars of a window opened to about:blank in the content context 257 * have the correct visibility. 258 * 259 * A window opened with "location=no, personalbar=no, toolbar=no, scrollbars=no, 260 * menubar=no, status=no", should only have location visible. 261 */ 262 add_task(async function () { 263 await BrowserTestUtils.withNewTab( 264 { 265 gBrowser, 266 url: CONTENT_PAGE, 267 }, 268 async function (browser) { 269 // Open a blank window with toolbars hidden 270 let winPromise = BrowserTestUtils.waitForNewWindow(); 271 await BrowserTestUtils.synthesizeMouseAtCenter( 272 "#winOpenNoURLNonDefault", 273 {}, 274 browser 275 ); 276 let popupWindow = await winPromise; 277 278 // No need to wait for this window to load, since it's loading about:blank 279 let popupBrowser = popupWindow.gBrowser.selectedBrowser; 280 let popupToolbars = await getToolbarsFromBrowserContent(popupBrowser); 281 testNonDefaultContentToolbarsFromContent(popupToolbars); 282 283 let chromeToolbars = getToolbarsFromWindowChrome(popupWindow); 284 testNonDefaultContentToolbarsFromChrome(chromeToolbars); 285 286 // Close the new window 287 await BrowserTestUtils.closeWindow(popupWindow); 288 } 289 ); 290 }); 291 292 /** 293 * Ensure that toolbars of a window opened in the chrome context have the 294 * correct visibility. 295 * 296 * A window opened with default parameters should have all toolbars visible. 297 * 298 * A window opened with "location=no, personalbar=no, toolbar=no, scrollbars=no, 299 * menubar=no, status=no", should not have any toolbar visible. 300 */ 301 add_task(async function () { 302 // First open a default window from this chrome context 303 let defaultWindowPromise = BrowserTestUtils.waitForNewWindow({ 304 url: TARGET_PAGE, 305 }); 306 window.open(TARGET_PAGE, "_blank", "noopener"); 307 let defaultWindow = await defaultWindowPromise; 308 309 // Check that all toolbars are visible 310 let toolbars = getToolbarsFromWindowChrome(defaultWindow); 311 testDefaultToolbars(toolbars); 312 313 // Now lets open a window with toolbars hidden from this chrome context 314 let features = 315 "location=no, personalbar=no, toolbar=no, scrollbars=no, menubar=no, status=no, noopener"; 316 let popupWindowPromise = BrowserTestUtils.waitForNewWindow({ 317 url: TARGET_PAGE, 318 }); 319 window.open(TARGET_PAGE, "_blank", features); 320 let popupWindow = await popupWindowPromise; 321 322 // Test none of the tooolbars are visible 323 let hiddenToolbars = getToolbarsFromWindowChrome(popupWindow); 324 testNonDefaultChromeToolbars(hiddenToolbars); 325 326 // Cleanup 327 await BrowserTestUtils.closeWindow(defaultWindow); 328 await BrowserTestUtils.closeWindow(popupWindow); 329 });