summaryrefslogtreecommitdiffstats
path: root/browser/components/places/tests/browser/browser_autoshow_bookmarks_toolbar.js
blob: c841eb276bab5ee86665011a0bf79c0765b39d17 (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
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

const LOCATION_PREF = "browser.bookmarks.defaultLocation";
const TOOLBAR_VISIBILITY_PREF = "browser.toolbars.bookmarks.visibility";
let bookmarkPanel;
let win;

add_setup(async function () {
  Services.prefs.clearUserPref(LOCATION_PREF);
  await PlacesUtils.bookmarks.eraseEverything();

  win = await BrowserTestUtils.openNewBrowserWindow();

  let oldTimeout = win.StarUI._autoCloseTimeout;
  // Make the timeout something big, so it doesn't interact badly with tests.
  win.StarUI._autoCloseTimeout = 6000000;

  win.StarUI._createPanelIfNeeded();
  bookmarkPanel = win.document.getElementById("editBookmarkPanel");
  bookmarkPanel.setAttribute("animate", false);

  registerCleanupFunction(async () => {
    bookmarkPanel = null;
    win.StarUI._autoCloseTimeout = oldTimeout;
    await BrowserTestUtils.closeWindow(win);
    win = null;
    await PlacesUtils.bookmarks.eraseEverything();
    Services.prefs.clearUserPref(LOCATION_PREF);
  });
});

/**
 * Helper to check we've shown the toolbar
 *
 * @param {object} options
 *   Options for the test
 * @param {boolean} options.showToolbar
 *   If the toolbar should be shown or not
 * @param {string} options.expectedFolder
 *   The expected folder to be shown
 * @param {string} options.reason
 *   The reason the toolbar should be shown
 */
async function checkResponse({ showToolbar, expectedFolder, reason }) {
  // Check folder.
  let menuList = win.document.getElementById("editBMPanel_folderMenuList");

  Assert.equal(
    menuList.label,
    PlacesUtils.getString(expectedFolder),
    `Should have ${expectedFolder} selected ${reason}.`
  );

  // Check toolbar:
  let toolbar = win.document.getElementById("PersonalToolbar");
  Assert.equal(
    !toolbar.collapsed,
    showToolbar,
    `Toolbar should be ${showToolbar ? "visible" : "hidden"} ${reason}.`
  );

  // Confirm and close the dialog.
  let hiddenPromise = promisePopupHidden(
    win.document.getElementById("editBookmarkPanel")
  );
  win.document.getElementById("editBookmarkPanelRemoveButton").click();
  await hiddenPromise;
}

/**
 * Test that if we create a bookmark on the toolbar, we show the
 * toolbar:
 */
add_task(async function test_new_on_toolbar() {
  await BrowserTestUtils.withNewTab(
    { gBrowser: win.gBrowser, url: "https://example.com/1" },
    async browser => {
      let toolbar = win.document.getElementById("PersonalToolbar");
      Assert.equal(
        toolbar.collapsed,
        true,
        "Bookmarks toolbar should start out collapsed."
      );
      let shownPromise = promisePopupShown(
        win.document.getElementById("editBookmarkPanel")
      );
      win.document.getElementById("Browser:AddBookmarkAs").doCommand();
      await shownPromise;
      await TestUtils.waitForCondition(
        () => !toolbar.collapsed,
        "Toolbar should be shown."
      );
      let expectedFolder = "BookmarksToolbarFolderTitle";
      let reason = "when creating a bookmark there";
      await checkResponse({ showToolbar: true, expectedFolder, reason });
    }
  );
});

/**
 * Test that if we create a bookmark on the toolbar, we do not
 * show the toolbar if toolbar should never be shown:
 */
add_task(async function test_new_on_toolbar_never_show_toolbar() {
  await SpecialPowers.pushPrefEnv({
    set: [[TOOLBAR_VISIBILITY_PREF, "never"]],
  });

  await BrowserTestUtils.withNewTab(
    { gBrowser: win.gBrowser, url: "https://example.com/1" },
    async browser => {
      let toolbar = win.document.getElementById("PersonalToolbar");
      Assert.equal(
        toolbar.collapsed,
        true,
        "Bookmarks toolbar should start out collapsed."
      );
      let shownPromise = promisePopupShown(
        win.document.getElementById("editBookmarkPanel")
      );
      win.document.getElementById("Browser:AddBookmarkAs").doCommand();
      await shownPromise;
      let expectedFolder = "BookmarksToolbarFolderTitle";
      let reason = "when the visibility pref is 'never'";
      await checkResponse({ showToolbar: false, expectedFolder, reason });
    }
  );

  await SpecialPowers.popPrefEnv();
});

/**
 * Test that if we edit an existing bookmark, we don't show the toolbar.
 */
add_task(async function test_existing_on_toolbar() {
  // Create the bookmark first:
  await PlacesUtils.bookmarks.insert({
    parentGuid: PlacesUtils.bookmarks.toolbarGuid,
    title: "Test for editing",
    url: "https://example.com/editing-test",
  });
  await BrowserTestUtils.withNewTab(
    { gBrowser: win.gBrowser, url: "https://example.com/editing-test" },
    async browser => {
      await TestUtils.waitForCondition(
        () => win.BookmarkingUI.status == BookmarkingUI.STATUS_STARRED,
        "Page should be starred."
      );

      let toolbar = win.document.getElementById("PersonalToolbar");
      Assert.equal(
        toolbar.collapsed,
        true,
        "Bookmarks toolbar should start out collapsed."
      );
      await clickBookmarkStar(win);
      let expectedFolder = "BookmarksToolbarFolderTitle";
      let reason = "when editing a bookmark there";
      await checkResponse({ showToolbar: false, expectedFolder, reason });
    }
  );
});