summaryrefslogtreecommitdiffstats
path: root/browser/base/content/test/popups/browser_popupUI.js
blob: 27423b586802d85d7ad81ae2ed08b2bddba1f9b4 (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
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

"use strict";

add_task(async function toolbar_ui_visibility() {
  SpecialPowers.pushPrefEnv({ set: [["dom.disable_open_during_load", false]] });

  let popupOpened = BrowserTestUtils.waitForNewWindow({ url: "about:blank" });
  BrowserTestUtils.openNewForegroundTab(
    gBrowser,
    "data:text/html,<html><script>popup=open('about:blank','','width=300,height=200')</script>"
  );
  const win = await popupOpened;
  const doc = win.document;

  ok(win.gURLBar, "location bar exists in the popup");
  isnot(win.gURLBar.clientWidth, 0, "location bar is visible in the popup");
  ok(win.gURLBar.readOnly, "location bar is read-only in the popup");
  isnot(
    doc.getElementById("Browser:OpenLocation").getAttribute("disabled"),
    "true",
    "'open location' command is not disabled in the popup"
  );

  EventUtils.synthesizeKey("t", { accelKey: true }, win);
  is(
    win.gBrowser.browsers.length,
    1,
    "Accel+T doesn't open a new tab in the popup"
  );
  is(
    gBrowser.browsers.length,
    3,
    "Accel+T opened a new tab in the parent window"
  );
  gBrowser.removeCurrentTab();

  EventUtils.synthesizeKey("w", { accelKey: true }, win);
  ok(win.closed, "Accel+W closes the popup");

  if (!win.closed) {
    win.close();
  }
  gBrowser.removeCurrentTab();
});

add_task(async function titlebar_buttons_visibility() {
  if (!navigator.platform.startsWith("Win")) {
    ok(true, "Testing only on Windows");
    return;
  }

  const BUTTONS_MAY_VISIBLE = true;
  const BUTTONS_NEVER_VISIBLE = false;

  // Always open a new window.
  // With default behavior, it opens a new tab, that doesn't affect button
  // visibility at all.
  Services.prefs.setIntPref("browser.link.open_newwindow", 2);

  const drawInTitlebarValues = [
    [1, BUTTONS_MAY_VISIBLE],
    [0, BUTTONS_NEVER_VISIBLE],
  ];
  const windowFeaturesValues = [
    // Opens a popup
    ["width=300,height=100", BUTTONS_NEVER_VISIBLE],
    ["toolbar", BUTTONS_NEVER_VISIBLE],
    ["menubar", BUTTONS_NEVER_VISIBLE],
    ["menubar,toolbar", BUTTONS_NEVER_VISIBLE],

    // Opens a new window
    ["", BUTTONS_MAY_VISIBLE],
  ];
  const menuBarShownValues = [true, false];

  for (const [drawInTitlebar, drawInTitlebarButtons] of drawInTitlebarValues) {
    Services.prefs.setIntPref("browser.tabs.inTitlebar", drawInTitlebar);

    for (const [
      windowFeatures,
      windowFeaturesButtons,
    ] of windowFeaturesValues) {
      for (const menuBarShown of menuBarShownValues) {
        CustomizableUI.setToolbarVisibility("toolbar-menubar", menuBarShown);

        const popupPromise = BrowserTestUtils.waitForNewWindow("about:blank");
        BrowserTestUtils.openNewForegroundTab(
          gBrowser,
          `data:text/html;charset=UTF-8,<html><script>window.open("about:blank","","${windowFeatures}")</script>`
        );
        const popupWin = await popupPromise;

        const menubar = popupWin.document.querySelector("#toolbar-menubar");
        const menubarIsShown =
          menubar.getAttribute("autohide") != "true" ||
          menubar.getAttribute("inactive") != "true";
        const buttonsInMenubar = menubar.querySelector(
          ".titlebar-buttonbox-container"
        );
        const buttonsInMenubarShown =
          menubarIsShown &&
          popupWin.getComputedStyle(buttonsInMenubar).display != "none";

        const buttonsInTabbar = popupWin.document.querySelector(
          "#TabsToolbar .titlebar-buttonbox-container"
        );
        const buttonsInTabbarShown =
          popupWin.getComputedStyle(buttonsInTabbar).display != "none";

        const params = `drawInTitlebar=${drawInTitlebar}, windowFeatures=${windowFeatures}, menuBarShown=${menuBarShown}`;
        if (
          drawInTitlebarButtons == BUTTONS_MAY_VISIBLE &&
          windowFeaturesButtons == BUTTONS_MAY_VISIBLE
        ) {
          ok(
            buttonsInMenubarShown || buttonsInTabbarShown,
            `Titlebar buttons should be visible: ${params}`
          );
        } else {
          ok(
            !buttonsInMenubarShown,
            `Titlebar buttons should not be visible: ${params}`
          );
          ok(
            !buttonsInTabbarShown,
            `Titlebar buttons should not be visible: ${params}`
          );
        }

        const closedPopupPromise = BrowserTestUtils.windowClosed(popupWin);
        popupWin.close();
        await closedPopupPromise;
        gBrowser.removeCurrentTab();
      }
    }
  }

  CustomizableUI.setToolbarVisibility("toolbar-menubar", false);
  Services.prefs.clearUserPref("browser.tabs.inTitlebar");
  Services.prefs.clearUserPref("browser.link.open_newwindow");
});

// Test only `visibility` rule here, to verify bug 1636229 fix.
// Other styles and ancestors can be different for each OS.
function isVisible(element) {
  const style = element.ownerGlobal.getComputedStyle(element);
  return style.visibility == "visible";
}

async function testTabBarVisibility() {
  SpecialPowers.pushPrefEnv({ set: [["dom.disable_open_during_load", false]] });

  const popupOpened = BrowserTestUtils.waitForNewWindow({ url: "about:blank" });
  BrowserTestUtils.openNewForegroundTab(
    gBrowser,
    "data:text/html,<html><script>popup=open('about:blank','','width=300,height=200')</script>"
  );
  const win = await popupOpened;
  const doc = win.document;

  ok(
    !isVisible(doc.getElementById("TabsToolbar")),
    "tabbar should be hidden for popup"
  );

  const closedPopupPromise = BrowserTestUtils.windowClosed(win);
  win.close();
  await closedPopupPromise;

  gBrowser.removeCurrentTab();
}

add_task(async function tabbar_visibility() {
  await testTabBarVisibility();
});

add_task(async function tabbar_visibility_with_theme() {
  const extension = ExtensionTestUtils.loadExtension({
    manifest: {
      theme: {},
    },
  });

  await extension.startup();

  await testTabBarVisibility();

  await extension.unload();
});