summaryrefslogtreecommitdiffstats
path: root/browser/base/content/test/about/browser_aboutNewTab_bookmarksToolbar.js
blob: c566276d9f6272ef05bd24cbf195ec1c3d822192 (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
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

add_task(async function bookmarks_toolbar_shown_on_newtab() {
  let newtab = await BrowserTestUtils.openNewForegroundTab({
    gBrowser,
    opening: "about:newtab",
    waitForLoad: false,
  });

  // 1: Test that the toolbar is shown in a newly opened foreground about:newtab
  await waitForBookmarksToolbarVisibility({
    visible: true,
    message: "Toolbar should be visible on newtab",
  });
  ok(isBookmarksToolbarVisible(), "Toolbar should be visible on newtab");

  // 2: Test that the toolbar is hidden when the browser is navigated away from newtab
  BrowserTestUtils.loadURIString(newtab.linkedBrowser, "https://example.com");
  await BrowserTestUtils.browserLoaded(newtab.linkedBrowser);
  await waitForBookmarksToolbarVisibility({
    visible: false,
    message:
      "Toolbar should not be visible on newtab after example.com is loaded within",
  });
  ok(
    !isBookmarksToolbarVisible(),
    "Toolbar should not be visible on newtab after example.com is loaded within"
  );

  // 3: Re-load about:newtab in the browser for the following tests and confirm toolbar reappears
  BrowserTestUtils.loadURIString(newtab.linkedBrowser, "about:newtab");
  await BrowserTestUtils.browserLoaded(newtab.linkedBrowser);
  await waitForBookmarksToolbarVisibility({
    visible: true,
    message: "Toolbar should be visible on newtab",
  });
  ok(isBookmarksToolbarVisible(), "Toolbar should be visible on newtab");

  // 4: Toolbar should get hidden when opening a new tab to example.com
  let example = await BrowserTestUtils.openNewForegroundTab({
    gBrowser,
    opening: "https://example.com",
  });
  await waitForBookmarksToolbarVisibility({
    visible: false,
    message: "Toolbar should be hidden on example.com",
  });

  // 5: Toolbar should become visible when switching tabs to newtab
  await BrowserTestUtils.switchTab(gBrowser, newtab);
  await waitForBookmarksToolbarVisibility({
    visible: true,
    message: "Toolbar is visible with switch to newtab",
  });
  ok(isBookmarksToolbarVisible(), "Toolbar is visible with switch to newtab");

  // 6: Toolbar should become hidden when switching tabs to example.com
  await BrowserTestUtils.switchTab(gBrowser, example);
  await waitForBookmarksToolbarVisibility({
    visible: false,
    message: "Toolbar is hidden with switch to example",
  });

  // 7: Similar to #3 above, loading about:newtab in example should show toolbar
  BrowserTestUtils.loadURIString(example.linkedBrowser, "about:newtab");
  await BrowserTestUtils.browserLoaded(example.linkedBrowser);
  await waitForBookmarksToolbarVisibility({
    visible: true,
    message: "Toolbar is visible with newtab load",
  });
  ok(isBookmarksToolbarVisible(), "Toolbar is visible with newtab load");

  // 8: Switching back and forth between two browsers showing about:newtab will still show the toolbar
  await BrowserTestUtils.switchTab(gBrowser, newtab);
  ok(isBookmarksToolbarVisible(), "Toolbar is visible with switch to newtab");
  await BrowserTestUtils.switchTab(gBrowser, example);
  ok(
    isBookmarksToolbarVisible(),
    "Toolbar is visible with switch to example(newtab)"
  );

  // 9: With custom newtab URL, toolbar isn't shown on about:newtab but is shown on custom URL
  let oldNewTab = AboutNewTab.newTabURL;
  AboutNewTab.newTabURL = "https://example.com/2";
  await BrowserTestUtils.switchTab(gBrowser, newtab);
  await waitForBookmarksToolbarVisibility({ visible: false });
  ok(!isBookmarksToolbarVisible(), "Toolbar should hide with custom newtab");
  BrowserTestUtils.loadURIString(example.linkedBrowser, AboutNewTab.newTabURL);
  await BrowserTestUtils.browserLoaded(example.linkedBrowser);
  await BrowserTestUtils.switchTab(gBrowser, example);
  await waitForBookmarksToolbarVisibility({ visible: true });
  ok(
    isBookmarksToolbarVisible(),
    "Toolbar is visible with switch to custom newtab"
  );

  await BrowserTestUtils.removeTab(newtab);
  await BrowserTestUtils.removeTab(example);
  AboutNewTab.newTabURL = oldNewTab;
});

add_task(async function bookmarks_toolbar_open_persisted() {
  let newtab = await BrowserTestUtils.openNewForegroundTab({
    gBrowser,
    opening: "about:newtab",
    waitForLoad: false,
  });
  let example = await BrowserTestUtils.openNewForegroundTab({
    gBrowser,
    opening: "https://example.com",
  });
  let isToolbarPersistedOpen = () =>
    Services.prefs.getCharPref("browser.toolbars.bookmarks.visibility") ==
    "always";

  ok(!isBookmarksToolbarVisible(), "Toolbar is hidden");
  await BrowserTestUtils.switchTab(gBrowser, newtab);
  await waitForBookmarksToolbarVisibility({ visible: true });
  ok(isBookmarksToolbarVisible(), "Toolbar is visible");
  await BrowserTestUtils.switchTab(gBrowser, example);
  await waitForBookmarksToolbarVisibility({ visible: false });
  ok(!isBookmarksToolbarVisible(), "Toolbar is hidden");
  ok(!isToolbarPersistedOpen(), "Toolbar is not persisted open");

  let contextMenu = document.querySelector("#toolbar-context-menu");
  let popupShown = BrowserTestUtils.waitForEvent(contextMenu, "popupshown");
  let menuButton = document.getElementById("PanelUI-menu-button");
  EventUtils.synthesizeMouseAtCenter(
    menuButton,
    { type: "contextmenu" },
    window
  );
  await popupShown;
  let bookmarksToolbarMenu = document.querySelector("#toggle_PersonalToolbar");
  let subMenu = bookmarksToolbarMenu.querySelector("menupopup");
  popupShown = BrowserTestUtils.waitForEvent(subMenu, "popupshown");
  bookmarksToolbarMenu.openMenu(true);
  await popupShown;
  let alwaysMenuItem = document.querySelector(
    'menuitem[data-visibility-enum="always"]'
  );
  let neverMenuItem = document.querySelector(
    'menuitem[data-visibility-enum="never"]'
  );
  let newTabMenuItem = document.querySelector(
    'menuitem[data-visibility-enum="newtab"]'
  );
  is(alwaysMenuItem.getAttribute("checked"), "false", "Menuitem isn't checked");
  is(neverMenuItem.getAttribute("checked"), "false", "Menuitem isn't checked");
  is(newTabMenuItem.getAttribute("checked"), "true", "Menuitem is checked");

  subMenu.activateItem(alwaysMenuItem);

  await waitForBookmarksToolbarVisibility({ visible: true });
  popupShown = BrowserTestUtils.waitForEvent(contextMenu, "popupshown");
  EventUtils.synthesizeMouseAtCenter(
    menuButton,
    { type: "contextmenu" },
    window
  );
  await popupShown;
  bookmarksToolbarMenu = document.querySelector("#toggle_PersonalToolbar");
  subMenu = bookmarksToolbarMenu.querySelector("menupopup");
  popupShown = BrowserTestUtils.waitForEvent(subMenu, "popupshown");
  bookmarksToolbarMenu.openMenu(true);
  await popupShown;
  alwaysMenuItem = document.querySelector(
    'menuitem[data-visibility-enum="always"]'
  );
  neverMenuItem = document.querySelector(
    'menuitem[data-visibility-enum="never"]'
  );
  newTabMenuItem = document.querySelector(
    'menuitem[data-visibility-enum="newtab"]'
  );
  is(alwaysMenuItem.getAttribute("checked"), "true", "Menuitem is checked");
  is(neverMenuItem.getAttribute("checked"), "false", "Menuitem isn't checked");
  is(newTabMenuItem.getAttribute("checked"), "false", "Menuitem isn't checked");
  contextMenu.hidePopup();
  ok(isBookmarksToolbarVisible(), "Toolbar is visible");
  ok(isToolbarPersistedOpen(), "Toolbar is persisted open");
  await BrowserTestUtils.switchTab(gBrowser, newtab);
  ok(isBookmarksToolbarVisible(), "Toolbar is visible");
  await BrowserTestUtils.switchTab(gBrowser, example);
  ok(isBookmarksToolbarVisible(), "Toolbar is visible");

  popupShown = BrowserTestUtils.waitForEvent(contextMenu, "popupshown");
  EventUtils.synthesizeMouseAtCenter(
    menuButton,
    { type: "contextmenu" },
    window
  );
  await popupShown;
  bookmarksToolbarMenu = document.querySelector("#toggle_PersonalToolbar");
  subMenu = bookmarksToolbarMenu.querySelector("menupopup");
  popupShown = BrowserTestUtils.waitForEvent(subMenu, "popupshown");
  bookmarksToolbarMenu.openMenu(true);
  await popupShown;
  alwaysMenuItem = document.querySelector(
    'menuitem[data-visibility-enum="always"]'
  );
  neverMenuItem = document.querySelector(
    'menuitem[data-visibility-enum="never"]'
  );
  newTabMenuItem = document.querySelector(
    'menuitem[data-visibility-enum="newtab"]'
  );
  is(alwaysMenuItem.getAttribute("checked"), "true", "Menuitem is checked");
  is(neverMenuItem.getAttribute("checked"), "false", "Menuitem isn't checked");
  is(newTabMenuItem.getAttribute("checked"), "false", "Menuitem isn't checked");
  subMenu.activateItem(newTabMenuItem);
  await waitForBookmarksToolbarVisibility({
    visible: false,
    message: "Toolbar is hidden",
  });
  await BrowserTestUtils.switchTab(gBrowser, newtab);
  await waitForBookmarksToolbarVisibility({
    visible: true,
    message: "Toolbar is visible",
  });
  await BrowserTestUtils.switchTab(gBrowser, example);
  await waitForBookmarksToolbarVisibility({
    visible: false,
    message: "Toolbar is hidden",
  });

  await BrowserTestUtils.removeTab(newtab);
  await BrowserTestUtils.removeTab(example);
});

add_task(async function test_with_newtabpage_disabled() {
  await SpecialPowers.pushPrefEnv({
    set: [["browser.newtabpage.enabled", true]],
  });

  let tabCount = gBrowser.tabs.length;
  document.getElementById("cmd_newNavigatorTab").doCommand();
  // Can't use BrowserTestUtils.waitForNewTab since onLocationChange will not
  // fire due to preloaded new tabs.
  await TestUtils.waitForCondition(() => gBrowser.tabs.length == tabCount + 1);
  let newtab = gBrowser.selectedTab;
  is(newtab.linkedBrowser.currentURI.spec, "about:newtab", "newtab is loaded");
  await waitForBookmarksToolbarVisibility({
    visible: true,
    message: "Toolbar is visible with NTP enabled",
  });
  let firstid = await SpecialPowers.spawn(newtab.linkedBrowser, [], () => {
    return content.document.body.firstElementChild?.id;
  });
  is(firstid, "root", "new tab page contains content");
  await BrowserTestUtils.removeTab(newtab);

  await SpecialPowers.pushPrefEnv({
    set: [["browser.newtabpage.enabled", false]],
  });

  document.getElementById("cmd_newNavigatorTab").doCommand();
  await TestUtils.waitForCondition(() => gBrowser.tabs.length == tabCount + 1);
  newtab = gBrowser.selectedTab;

  await waitForBookmarksToolbarVisibility({
    visible: true,
    message: "Toolbar is visible with NTP disabled",
  });

  is(
    newtab.linkedBrowser.currentURI.spec,
    "about:newtab",
    "blank new tab is loaded"
  );
  firstid = await SpecialPowers.spawn(newtab.linkedBrowser, [], () => {
    return content.document.body.firstElementChild;
  });
  ok(!firstid, "blank new tab page contains no content");

  await BrowserTestUtils.removeTab(newtab);

  await SpecialPowers.pushPrefEnv({
    set: [["browser.newtabpage.enabled", true]],
  });
});

add_task(async function test_history_pushstate() {
  await BrowserTestUtils.withNewTab("https://example.com/", async browser => {
    await waitForBookmarksToolbarVisibility({ visible: false });
    ok(!isBookmarksToolbarVisible(), "Toolbar should be hidden");

    // Temporarily show the toolbar:
    setToolbarVisibility(
      document.querySelector("#PersonalToolbar"),
      true,
      false,
      false
    );
    ok(isBookmarksToolbarVisible(), "Toolbar should now be visible");

    // Now "navigate"
    await SpecialPowers.spawn(browser, [], () => {
      content.location.href += "#foo";
    });

    await TestUtils.waitForCondition(
      () => gURLBar.value.endsWith("#foo"),
      "URL bar should update"
    );
    ok(isBookmarksToolbarVisible(), "Toolbar should still be visible");
  });
});