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

"use strict";

const bookmarksInfo = [
  {
    title: "firefox",
    // eslint-disable-next-line @microsoft/sdl/no-insecure-url
    url: "http://example.com",
  },
  {
    title: "rules",
    // eslint-disable-next-line @microsoft/sdl/no-insecure-url
    url: "http://example.com/2",
  },
  {
    title: "yo",
    // eslint-disable-next-line @microsoft/sdl/no-insecure-url
    url: "http://example.com/2",
  },
];

async function emptyToolbarMessageVisible(visible, win = window) {
  info("Empty toolbar message should be " + (visible ? "visible" : "hidden"));
  let emptyMessage = win.document.getElementById("personal-toolbar-empty");
  await BrowserTestUtils.waitForMutationCondition(
    emptyMessage,
    { attributes: true, attributeFilter: ["hidden"] },
    () => emptyMessage.hidden != visible
  );
}

add_setup(async function () {
  await SpecialPowers.pushPrefEnv({
    // Ensure we can wait for about:newtab to load.
    set: [["browser.newtab.preload", false]],
  });
  // Move all existing bookmarks in the Bookmarks Toolbar and
  // Other Bookmarks to the Bookmarks Menu so they don't affect
  // the visibility of the Bookmarks Toolbar. Restore them at
  // the end of the test.
  let Bookmarks = PlacesUtils.bookmarks;
  let toolbarBookmarks = [];
  let unfiledBookmarks = [];
  let guidBookmarkTuples = [
    [Bookmarks.toolbarGuid, toolbarBookmarks],
    [Bookmarks.unfiledGuid, unfiledBookmarks],
  ];
  for (let [parentGuid, arr] of guidBookmarkTuples) {
    await Bookmarks.fetch({ parentGuid }, bookmark => arr.push(bookmark));
  }
  await Promise.all(
    [...toolbarBookmarks, ...unfiledBookmarks].map(async bookmark => {
      bookmark.parentGuid = Bookmarks.menuGuid;
      return Bookmarks.update(bookmark);
    })
  );
  registerCleanupFunction(async () => {
    for (let [parentGuid, arr] of guidBookmarkTuples) {
      await Promise.all(
        arr.map(async bookmark => {
          bookmark.parentGuid = parentGuid;
          return Bookmarks.update(bookmark);
        })
      );
    }
  });
});

add_task(async function bookmarks_toolbar_not_shown_when_empty() {
  let bookmarks = await PlacesUtils.bookmarks.insertTree({
    guid: PlacesUtils.bookmarks.toolbarGuid,
    children: bookmarksInfo,
  });
  let exampleTab = await BrowserTestUtils.openNewForegroundTab({
    gBrowser,
    opening: "https://example.com",
  });
  let newtab = await BrowserTestUtils.openNewForegroundTab({
    gBrowser,
    opening: "about:newtab",
  });
  let emptyMessage = document.getElementById("personal-toolbar-empty");

  // 1: Test that the toolbar is shown in a newly opened foreground about:newtab
  let placesItems = document.getElementById("PlacesToolbarItems");
  let promiseBookmarksOnToolbar = BrowserTestUtils.waitForMutationCondition(
    placesItems,
    { childList: true },
    () => placesItems.childNodes.length
  );
  await waitForBookmarksToolbarVisibility({
    visible: true,
    message: "Toolbar should be visible on newtab",
  });
  await promiseBookmarksOnToolbar;
  await emptyToolbarMessageVisible(false);

  // 2: Toolbar should get hidden when switching tab to example.com
  let promiseToolbar = waitForBookmarksToolbarVisibility({
    visible: false,
    message: "Toolbar should be hidden on example.com",
  });
  await BrowserTestUtils.switchTab(gBrowser, exampleTab);
  await promiseToolbar;

  // 3: Remove all children of the Bookmarks Toolbar and confirm that
  // the toolbar should not become visible when switching to newtab
  CustomizableUI.addWidgetToArea(
    "personal-bookmarks",
    CustomizableUI.AREA_TABSTRIP
  );
  CustomizableUI.removeWidgetFromArea("import-button");
  await BrowserTestUtils.switchTab(gBrowser, newtab);
  await waitForBookmarksToolbarVisibility({
    visible: true,
    message: "Toolbar is visible when there are no items in the toolbar area",
  });
  await emptyToolbarMessageVisible(true);

  // Click the link and check we open the library:
  let winPromise = BrowserTestUtils.domWindowOpenedAndLoaded();
  EventUtils.synthesizeMouseAtCenter(
    emptyMessage.querySelector(".text-link"),
    {}
  );
  let libraryWin = await winPromise;
  is(
    libraryWin.document.location.href,
    "chrome://browser/content/places/places.xhtml",
    "Should have opened library."
  );
  await BrowserTestUtils.closeWindow(libraryWin);

  // 4: Put personal-bookmarks back in the toolbar and confirm the toolbar is visible now
  CustomizableUI.addWidgetToArea(
    "personal-bookmarks",
    CustomizableUI.AREA_BOOKMARKS
  );
  await BrowserTestUtils.switchTab(gBrowser, exampleTab);
  await BrowserTestUtils.switchTab(gBrowser, newtab);
  promiseBookmarksOnToolbar = BrowserTestUtils.waitForMutationCondition(
    placesItems,
    { childList: true },
    () => placesItems.childNodes.length
  );
  await waitForBookmarksToolbarVisibility({
    visible: true,
    message: "Toolbar should be visible with Bookmarks Toolbar Items restored",
  });
  await promiseBookmarksOnToolbar;
  await emptyToolbarMessageVisible(false);

  // 5: Remove all the bookmarks in the toolbar and confirm that the toolbar
  // is hidden on the New Tab now
  await PlacesUtils.bookmarks.remove(bookmarks);
  await BrowserTestUtils.switchTab(gBrowser, exampleTab);
  await BrowserTestUtils.switchTab(gBrowser, newtab);
  await waitForBookmarksToolbarVisibility({
    visible: true,
    message:
      "Toolbar is visible when there are no items or nested bookmarks in the toolbar area",
  });
  await emptyToolbarMessageVisible(true);

  // 6: Add a toolbarbutton and make sure that the toolbar appears when the button is visible
  CustomizableUI.addWidgetToArea(
    "characterencoding-button",
    CustomizableUI.AREA_BOOKMARKS
  );
  await BrowserTestUtils.switchTab(gBrowser, exampleTab);
  await BrowserTestUtils.switchTab(gBrowser, newtab);
  await waitForBookmarksToolbarVisibility({
    visible: true,
    message: "Toolbar is visible when there is a visible button in the toolbar",
  });
  await emptyToolbarMessageVisible(false);

  await BrowserTestUtils.removeTab(newtab);
  await BrowserTestUtils.removeTab(exampleTab);
  CustomizableUI.reset();
});