tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

browser_aboutNewTab_bookmarksToolbarEmpty.js (6289B)


      1 /* Any copyright is dedicated to the Public Domain.
      2 * http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 "use strict";
      5 
      6 const bookmarksInfo = [
      7  {
      8    title: "firefox",
      9    // eslint-disable-next-line @microsoft/sdl/no-insecure-url
     10    url: "http://example.com",
     11  },
     12  {
     13    title: "rules",
     14    // eslint-disable-next-line @microsoft/sdl/no-insecure-url
     15    url: "http://example.com/2",
     16  },
     17  {
     18    title: "yo",
     19    // eslint-disable-next-line @microsoft/sdl/no-insecure-url
     20    url: "http://example.com/2",
     21  },
     22 ];
     23 
     24 async function emptyToolbarMessageVisible(visible, win = window) {
     25  info("Empty toolbar message should be " + (visible ? "visible" : "hidden"));
     26  let emptyMessage = win.document.getElementById("personal-toolbar-empty");
     27  await BrowserTestUtils.waitForMutationCondition(
     28    emptyMessage,
     29    { attributes: true, attributeFilter: ["hidden"] },
     30    () => emptyMessage.hidden != visible
     31  );
     32 }
     33 
     34 add_setup(async function () {
     35  await SpecialPowers.pushPrefEnv({
     36    // Ensure we can wait for about:newtab to load.
     37    set: [
     38      ["browser.newtab.preload", false],
     39      ["test.wait300msAfterTabSwitch", true],
     40    ],
     41  });
     42  // Move all existing bookmarks in the Bookmarks Toolbar and
     43  // Other Bookmarks to the Bookmarks Menu so they don't affect
     44  // the visibility of the Bookmarks Toolbar. Restore them at
     45  // the end of the test.
     46  let Bookmarks = PlacesUtils.bookmarks;
     47  let toolbarBookmarks = [];
     48  let unfiledBookmarks = [];
     49  let guidBookmarkTuples = [
     50    [Bookmarks.toolbarGuid, toolbarBookmarks],
     51    [Bookmarks.unfiledGuid, unfiledBookmarks],
     52  ];
     53  for (let [parentGuid, arr] of guidBookmarkTuples) {
     54    await Bookmarks.fetch({ parentGuid }, bookmark => arr.push(bookmark));
     55  }
     56  await Promise.all(
     57    [...toolbarBookmarks, ...unfiledBookmarks].map(async bookmark => {
     58      bookmark.parentGuid = Bookmarks.menuGuid;
     59      return Bookmarks.update(bookmark);
     60    })
     61  );
     62  registerCleanupFunction(async () => {
     63    for (let [parentGuid, arr] of guidBookmarkTuples) {
     64      await Promise.all(
     65        arr.map(async bookmark => {
     66          bookmark.parentGuid = parentGuid;
     67          return Bookmarks.update(bookmark);
     68        })
     69      );
     70    }
     71  });
     72 });
     73 
     74 add_task(async function bookmarks_toolbar_not_shown_when_empty() {
     75  let bookmarks = await PlacesUtils.bookmarks.insertTree({
     76    guid: PlacesUtils.bookmarks.toolbarGuid,
     77    children: bookmarksInfo,
     78  });
     79  let exampleTab = await BrowserTestUtils.openNewForegroundTab({
     80    gBrowser,
     81    opening: "https://example.com",
     82  });
     83  let newtab = await BrowserTestUtils.openNewForegroundTab({
     84    gBrowser,
     85    opening: "about:newtab",
     86  });
     87  let emptyMessage = document.getElementById("personal-toolbar-empty");
     88 
     89  // 1: Test that the toolbar is shown in a newly opened foreground about:newtab
     90  let placesItems = document.getElementById("PlacesToolbarItems");
     91  let promiseBookmarksOnToolbar = BrowserTestUtils.waitForMutationCondition(
     92    placesItems,
     93    { childList: true },
     94    () => placesItems.childNodes.length
     95  );
     96  await waitForBookmarksToolbarVisibility({
     97    visible: true,
     98    message: "Toolbar should be visible on newtab",
     99  });
    100  await promiseBookmarksOnToolbar;
    101  await emptyToolbarMessageVisible(false);
    102 
    103  // 2: Toolbar should get hidden when switching tab to example.com
    104  let promiseToolbar = waitForBookmarksToolbarVisibility({
    105    visible: false,
    106    message: "Toolbar should be hidden on example.com",
    107  });
    108  await BrowserTestUtils.switchTab(gBrowser, exampleTab);
    109  await promiseToolbar;
    110 
    111  // 3: Remove all children of the Bookmarks Toolbar and confirm that
    112  // the toolbar should not become visible when switching to newtab
    113  CustomizableUI.addWidgetToArea(
    114    "personal-bookmarks",
    115    CustomizableUI.AREA_TABSTRIP
    116  );
    117  CustomizableUI.removeWidgetFromArea("import-button");
    118  await BrowserTestUtils.switchTab(gBrowser, newtab);
    119  await waitForBookmarksToolbarVisibility({
    120    visible: true,
    121    message: "Toolbar is visible when there are no items in the toolbar area",
    122  });
    123  await emptyToolbarMessageVisible(true);
    124 
    125  // Click the link and check we open the library:
    126  let winPromise = BrowserTestUtils.domWindowOpenedAndLoaded();
    127  EventUtils.synthesizeMouseAtCenter(
    128    emptyMessage.querySelector(".text-link"),
    129    {}
    130  );
    131  let libraryWin = await winPromise;
    132  is(
    133    libraryWin.document.location.href,
    134    "chrome://browser/content/places/places.xhtml",
    135    "Should have opened library."
    136  );
    137  await BrowserTestUtils.closeWindow(libraryWin);
    138 
    139  // 4: Put personal-bookmarks back in the toolbar and confirm the toolbar is visible now
    140  CustomizableUI.addWidgetToArea(
    141    "personal-bookmarks",
    142    CustomizableUI.AREA_BOOKMARKS
    143  );
    144  await BrowserTestUtils.switchTab(gBrowser, exampleTab);
    145  await BrowserTestUtils.switchTab(gBrowser, newtab);
    146  promiseBookmarksOnToolbar = BrowserTestUtils.waitForMutationCondition(
    147    placesItems,
    148    { childList: true },
    149    () => placesItems.childNodes.length
    150  );
    151  await waitForBookmarksToolbarVisibility({
    152    visible: true,
    153    message: "Toolbar should be visible with Bookmarks Toolbar Items restored",
    154  });
    155  await promiseBookmarksOnToolbar;
    156  await emptyToolbarMessageVisible(false);
    157 
    158  // 5: Remove all the bookmarks in the toolbar and confirm that the toolbar
    159  // is hidden on the New Tab now
    160  await PlacesUtils.bookmarks.remove(bookmarks);
    161  await BrowserTestUtils.switchTab(gBrowser, exampleTab);
    162  await BrowserTestUtils.switchTab(gBrowser, newtab);
    163  await waitForBookmarksToolbarVisibility({
    164    visible: true,
    165    message:
    166      "Toolbar is visible when there are no items or nested bookmarks in the toolbar area",
    167  });
    168  await emptyToolbarMessageVisible(true);
    169 
    170  // 6: Add a toolbarbutton and make sure that the toolbar appears when the button is visible
    171  CustomizableUI.addWidgetToArea(
    172    "characterencoding-button",
    173    CustomizableUI.AREA_BOOKMARKS
    174  );
    175  await BrowserTestUtils.switchTab(gBrowser, exampleTab);
    176  await BrowserTestUtils.switchTab(gBrowser, newtab);
    177  await waitForBookmarksToolbarVisibility({
    178    visible: true,
    179    message: "Toolbar is visible when there is a visible button in the toolbar",
    180  });
    181  await emptyToolbarMessageVisible(false);
    182 
    183  await BrowserTestUtils.removeTab(newtab);
    184  await BrowserTestUtils.removeTab(exampleTab);
    185  CustomizableUI.reset();
    186 });