Commit ed77853f authored by Jared Wein's avatar Jared Wein
Browse files

Bug 1673402 - Don't show the bookmarks toolbar on the new tab page if there are no contents. r=Gijs

parent 4d900d1a
Loading
Loading
Loading
Loading
+20 −0
Original line number Diff line number Diff line
@@ -1610,6 +1610,26 @@ var BookmarkingUI = {
    return menu;
  },

  bookmarksToolbarHasVisibleChildren() {
    let bookmarksToolbarWidgets = CustomizableUI.getWidgetsInArea(
      CustomizableUI.AREA_BOOKMARKS
    );

    const BOOKMARKS_TOOLBAR_ITEMS_ID = "personal-bookmarks";
    if (
      bookmarksToolbarWidgets.find(w => w.id == BOOKMARKS_TOOLBAR_ITEMS_ID) &&
      PlacesUtils.getChildCountForFolder(PlacesUtils.bookmarks.toolbarGuid)
    ) {
      return true;
    }

    // The bookmarks items may not have any children, but if there are
    // other widgets present then treat them as visible.
    return bookmarksToolbarWidgets.some(
      w => w.id != BOOKMARKS_TOOLBAR_ITEMS_ID
    );
  },

  attachPlacesView(event, node) {
    // If the view is already there, bail out early.
    if (node.parentNode._placesView) {
+3 −0
Original line number Diff line number Diff line
@@ -6621,6 +6621,9 @@ function setToolbarVisibility(
          currentURI: gBrowser.currentURI,
          isNullPrincipal: gBrowser.contentPrincipal.isNullPrincipal,
        });
        // If there is nothing visible in the toolbar, then don't show
        // it on the New Tab page.
        isVisible &&= BookmarkingUI.bookmarksToolbarHasVisibleChildren();
        break;
    }
  }
+1 −0
Original line number Diff line number Diff line
@@ -35,6 +35,7 @@ support-files =
  iframe_page_xfo.html
  xfo_iframe.sjs
[browser_aboutNewTab_bookmarksToolbar.js]
[browser_aboutNewTab_bookmarksToolbarEmpty.js]
[browser_aboutNewTab_bookmarksToolbarPrefs.js]
[browser_aboutNewTab_defaultBrowserNotification.js]
skip-if = debug || asan || ccov # Default browser checks are skipped on debug builds, bug 1660723
+134 −0
Original line number Diff line number Diff line
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

const bookmarksInfo = [
  {
    title: "firefox",
    url: "http://example.com",
  },
  {
    title: "rules",
    url: "http://example.com/2",
  },
  {
    title: "yo",
    url: "http://example.com/2",
  },
];

add_task(async function setup() {
  // 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_shown_on_newtab() {
  for (let featureEnabled of [true, false]) {
    info(
      "Testing with the feature " + (featureEnabled ? "enabled" : "disabled")
    );
    await SpecialPowers.pushPrefEnv({
      set: [["browser.toolbars.bookmarks.2h2020", featureEnabled]],
    });
    let bookmarks = await PlacesUtils.bookmarks.insertTree({
      guid: PlacesUtils.bookmarks.toolbarGuid,
      children: bookmarksInfo,
    });
    let example = await BrowserTestUtils.openNewForegroundTab({
      gBrowser,
      opening: "https://example.com",
    });
    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
    if (featureEnabled) {
      await waitForBookmarksToolbarVisibility({
        visible: true,
        message: "Toolbar should be visible on newtab if enabled",
      });
    }

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

    // 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: false,
      message:
        "Toolbar is not visible when there are no items in the toolbar area",
    });

    // 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, example);
    await BrowserTestUtils.switchTab(gBrowser, newtab);
    if (featureEnabled) {
      await waitForBookmarksToolbarVisibility({
        visible: true,
        message:
          "Toolbar should be visible with Bookmarks Toolbar Items restored",
      });
    }

    // 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, example);
    await BrowserTestUtils.switchTab(gBrowser, newtab);
    await waitForBookmarksToolbarVisibility({
      visible: false,
      message:
        "Toolbar is not visible when there are no items or nested bookmarks in the toolbar area",
    });

    await BrowserTestUtils.removeTab(newtab);
    await BrowserTestUtils.removeTab(example);
  }
});
+3 −1
Original line number Diff line number Diff line
@@ -241,10 +241,12 @@ async function waitForBookmarksToolbarVisibility({
  visible,
  message,
}) {
  return TestUtils.waitForCondition(() => {
  let result = await TestUtils.waitForCondition(() => {
    let toolbar = win.gNavToolbox.querySelector("#PersonalToolbar");
    return visible ? !toolbar.collapsed : toolbar.collapsed;
  }, message || "waiting for toolbar to become " + (visible ? "visible" : "hidden"));
  ok(result, message);
  return result;
}

function isBookmarksToolbarVisible(win = window) {