summaryrefslogtreecommitdiffstats
path: root/dom/tests/browser/browser_test_toolbars_visibility.js
blob: f313f1549fa6da4b6fe123fbee79fcd0472cc7d0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
// Tests that toolbars have proper visibility when opening a new window
// in either content or chrome context.

const ROOT = "http://www.example.com/browser/dom/tests/browser/";
const CONTENT_PAGE = ROOT + "test_new_window_from_content_child.html";
const TARGET_PAGE = ROOT + "dummy.html";

/**
 * This function retrieves the visibility state of the toolbars of a
 * window within the content context.
 *
 * @param aBrowser (<xul:browser>)
 *        The browser to query for toolbar visibility states
 * @returns Promise
 *        A promise that resolves when the toolbar state is retrieved
 *        within the content context, which value is an object that holds
 *        the visibility state of the toolbars
 */
function getToolbarsFromBrowserContent(aBrowser) {
  return SpecialPowers.spawn(aBrowser, [], async function () {
    // This is still chrome context.
    // Inject a script that runs on content context, and gather the result.

    let script = content.document.createElement("script");
    script.textContent = `
let bars = [
  "toolbar",
  "menubar",
  "personalbar",
  "statusbar",
  "scrollbars",
  "locationbar",
];

for (let bar of bars) {
  let node = document.createElement("span");
  node.id = bar;
  node.textContent = window[bar].visible;
  document.body.appendChild(node);
}
`;
    content.document.body.appendChild(script);

    let result = {};

    let bars = [
      "toolbar",
      "menubar",
      "personalbar",
      "statusbar",
      "scrollbars",
      "locationbar",
    ];

    for (let bar of bars) {
      let node = content.document.getElementById(bar);
      let value = node.textContent;
      if (value !== "true" && value !== "false") {
        throw new Error("bar visibility isn't set");
      }
      result[bar] = value === "true";
      node.remove();
    }

    return result;
  });
}

/**
 * This function retrieves the visibility state of the toolbars of a
 * window within the chrome context.
 *
 * @param win
 *        the chrome privileged window
 * @returns object
 *        an object that holds the visibility state of the toolbars
 */
function getToolbarsFromWindowChrome(win) {
  return {
    toolbar: win.toolbar.visible,
    menubar: win.menubar.visible,
    personalbar: win.personalbar.visible,
    statusbar: win.statusbar.visible,
    scrollbars: win.scrollbars.visible,
    locationbar: win.locationbar.visible,
  };
}

/**
 * Tests toolbar visibility when opening a window with default parameters.
 *
 * @param toolbars
 *        the visibility state of the toolbar elements
 */
function testDefaultToolbars(toolbars) {
  ok(
    toolbars.locationbar,
    "locationbar should be visible on default window.open()"
  );
  ok(toolbars.menubar, "menubar be visible on default window.open()");
  ok(
    toolbars.personalbar,
    "personalbar should be visible on default window.open()"
  );
  ok(
    toolbars.statusbar,
    "statusbar should be visible on default window.open()"
  );
  ok(
    toolbars.scrollbars,
    "scrollbars should be visible on default window.open()"
  );
  ok(toolbars.toolbar, "toolbar should be visible on default window.open()");
}

/**
 * Tests toolbar visibility when opening a popup window on the content context,
 * and reading the value from context context.
 *
 * @param toolbars
 *        the visibility state of the toolbar elements
 */
function testNonDefaultContentToolbarsFromContent(toolbars) {
  // Accessing BarProp.visible from content context should return false for
  // popup, regardless of the each feature value in window.open parameter.
  ok(!toolbars.locationbar, "locationbar.visible should be false for popup");
  ok(!toolbars.menubar, "menubar.visible should be false for popup");
  ok(!toolbars.personalbar, "personalbar.visible should be false for popup");
  ok(!toolbars.statusbar, "statusbar.visible should be false for popup");
  ok(!toolbars.scrollbars, "scrollbars.visible should be false for popup");
  ok(!toolbars.toolbar, "toolbar.visible should be false for popup");
}

/**
 * Tests toolbar visibility when opening a popup window on the content context,
 * and reading the value from chrome context.
 *
 * @param toolbars
 *        the visibility state of the toolbar elements
 */
function testNonDefaultContentToolbarsFromChrome(toolbars) {
  // Accessing BarProp.visible from chrome context should return the
  // actual visibility.

  // Locationbar should always be visible on content context
  ok(
    toolbars.locationbar,
    "locationbar should be visible even with location=no"
  );
  ok(!toolbars.menubar, "menubar shouldn't be visible when menubar=no");
  ok(
    !toolbars.personalbar,
    "personalbar shouldn't be visible when personalbar=no"
  );
  // statusbar will report visible=true even when it's hidden because of bug#55820
  todo(!toolbars.statusbar, "statusbar shouldn't be visible when status=no");
  ok(
    toolbars.scrollbars,
    "scrollbars should be visible even with scrollbars=no"
  );
  ok(!toolbars.toolbar, "toolbar shouldn't be visible when toolbar=no");
}

/**
 * Tests toolbar visibility when opening a window with non default parameters
 * on the chrome context.
 *
 * @param toolbars
 *        the visibility state of the toolbar elements
 */
function testNonDefaultChromeToolbars(toolbars) {
  // None of the toolbars should be visible if hidden with chrome privileges
  ok(
    !toolbars.locationbar,
    "locationbar should not be visible with location=no"
  );
  ok(!toolbars.menubar, "menubar should not be visible with menubar=no");
  ok(
    !toolbars.personalbar,
    "personalbar should not be visible with personalbar=no"
  );
  ok(!toolbars.statusbar, "statusbar should not be visible with status=no");
  ok(
    toolbars.scrollbars,
    "scrollbars should be visible even with scrollbars=no"
  );
  ok(!toolbars.toolbar, "toolbar should not be visible with toolbar=no");
}

/**
 * Ensure that toolbars of a window opened in the content context have the
 * correct visibility.
 *
 * A window opened with default parameters should have all toolbars visible.
 *
 * A window opened with "location=no, personalbar=no, toolbar=no, scrollbars=no,
 * menubar=no, status=no", should only have location visible.
 */
add_task(async function () {
  await BrowserTestUtils.withNewTab(
    {
      gBrowser,
      url: CONTENT_PAGE,
    },
    async function (browser) {
      // First, call the default window.open() which will open a new tab
      let newTabPromise = BrowserTestUtils.waitForNewTab(gBrowser);
      await BrowserTestUtils.synthesizeMouseAtCenter(
        "#winOpenDefault",
        {},
        browser
      );
      let tab = await newTabPromise;

      // Check that all toolbars are visible
      let toolbars = await getToolbarsFromBrowserContent(
        gBrowser.selectedBrowser
      );
      testDefaultToolbars(toolbars);

      // Cleanup
      BrowserTestUtils.removeTab(tab);

      // Now let's open a window with toolbars hidden
      let winPromise = BrowserTestUtils.waitForNewWindow({ url: TARGET_PAGE });
      await BrowserTestUtils.synthesizeMouseAtCenter(
        "#winOpenNonDefault",
        {},
        browser
      );
      let popupWindow = await winPromise;

      let popupBrowser = popupWindow.gBrowser.selectedBrowser;

      // Test toolbars visibility value from content.
      let popupToolbars = await getToolbarsFromBrowserContent(popupBrowser);
      testNonDefaultContentToolbarsFromContent(popupToolbars);

      // Test toolbars visibility value from chrome.
      let chromeToolbars = getToolbarsFromWindowChrome(popupWindow);
      testNonDefaultContentToolbarsFromChrome(chromeToolbars);

      // Close the new window
      await BrowserTestUtils.closeWindow(popupWindow);
    }
  );
});

/**
 * Ensure that toolbars of a window opened to about:blank in the content context
 * have the correct visibility.
 *
 * A window opened with "location=no, personalbar=no, toolbar=no, scrollbars=no,
 * menubar=no, status=no", should only have location visible.
 */
add_task(async function () {
  await BrowserTestUtils.withNewTab(
    {
      gBrowser,
      url: CONTENT_PAGE,
    },
    async function (browser) {
      // Open a blank window with toolbars hidden
      let winPromise = BrowserTestUtils.waitForNewWindow();
      await BrowserTestUtils.synthesizeMouseAtCenter(
        "#winOpenNoURLNonDefault",
        {},
        browser
      );
      let popupWindow = await winPromise;

      // No need to wait for this window to load, since it's loading about:blank
      let popupBrowser = popupWindow.gBrowser.selectedBrowser;
      let popupToolbars = await getToolbarsFromBrowserContent(popupBrowser);
      testNonDefaultContentToolbarsFromContent(popupToolbars);

      let chromeToolbars = getToolbarsFromWindowChrome(popupWindow);
      testNonDefaultContentToolbarsFromChrome(chromeToolbars);

      // Close the new window
      await BrowserTestUtils.closeWindow(popupWindow);
    }
  );
});

/**
 * Ensure that toolbars of a window opened in the chrome context have the
 * correct visibility.
 *
 * A window opened with default parameters should have all toolbars visible.
 *
 * A window opened with "location=no, personalbar=no, toolbar=no, scrollbars=no,
 * menubar=no, status=no", should not have any toolbar visible.
 */
add_task(async function () {
  // First open a default window from this chrome context
  let defaultWindowPromise = BrowserTestUtils.waitForNewWindow({
    url: TARGET_PAGE,
  });
  window.open(TARGET_PAGE, "_blank", "noopener");
  let defaultWindow = await defaultWindowPromise;

  // Check that all toolbars are visible
  let toolbars = getToolbarsFromWindowChrome(defaultWindow);
  testDefaultToolbars(toolbars);

  // Now lets open a window with toolbars hidden from this chrome context
  let features =
    "location=no, personalbar=no, toolbar=no, scrollbars=no, menubar=no, status=no, noopener";
  let popupWindowPromise = BrowserTestUtils.waitForNewWindow({
    url: TARGET_PAGE,
  });
  window.open(TARGET_PAGE, "_blank", features);
  let popupWindow = await popupWindowPromise;

  // Test none of the tooolbars are visible
  let hiddenToolbars = getToolbarsFromWindowChrome(popupWindow);
  testNonDefaultChromeToolbars(hiddenToolbars);

  // Cleanup
  await BrowserTestUtils.closeWindow(defaultWindow);
  await BrowserTestUtils.closeWindow(popupWindow);
});