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

"use strict";

/**
 * Tests that recently added bookmarks can be opened.
 */

const BASE_URL =
  "http://example.org/browser/browser/components/places/tests/browser/";
const bookmarkItems = [
  {
    url: `${BASE_URL}bookmark_dummy_1.html`,
    title: "Custom Title 1",
  },
  {
    url: `${BASE_URL}bookmark_dummy_2.html`,
    title: "Custom Title 2",
  },
];
let openedTabs = [];

async function openBookmarksPanelInLibraryToolbarButton() {
  let libraryBtn = document.getElementById("library-button");
  libraryBtn.click();
  let libView = document.getElementById("appMenu-libraryView");
  let viewShownPromise = BrowserTestUtils.waitForEvent(libView, "ViewShown");
  await viewShownPromise;

  let bookmarksButton;
  await TestUtils.waitForCondition(() => {
    bookmarksButton = document.getElementById(
      "appMenu-library-bookmarks-button"
    );
    return bookmarksButton;
  }, "Should have the library bookmarks button");
  bookmarksButton.click();

  let BookmarksView = document.getElementById("PanelUI-bookmarks");
  let viewRecentPromise = BrowserTestUtils.waitForEvent(
    BookmarksView,
    "ViewShown"
  );
  await viewRecentPromise;
}

async function openBookmarkedItemInNewTab(itemFromMenu) {
  let placesContext = document.getElementById("placesContext");
  let openContextMenuPromise = BrowserTestUtils.waitForEvent(
    placesContext,
    "popupshown"
  );
  EventUtils.synthesizeMouseAtCenter(itemFromMenu, {
    button: 2,
    type: "contextmenu",
  });
  await openContextMenuPromise;
  info("Opened context menu");

  let tabCreatedPromise = BrowserTestUtils.waitForNewTab(gBrowser, null, true);

  let openInNewTabOption = document.getElementById("placesContext_open:newtab");
  placesContext.activateItem(openInNewTabOption);
  info("Click open in new tab");

  let lastOpenedTab = await tabCreatedPromise;
  Assert.equal(
    lastOpenedTab.linkedBrowser.currentURI.spec,
    itemFromMenu._placesNode.uri,
    "Should have opened the correct URI"
  );
  openedTabs.push(lastOpenedTab);
}

async function closeLibraryMenu() {
  let libView = document.getElementById("appMenu-libraryView");
  let viewHiddenPromise = BrowserTestUtils.waitForEvent(libView, "ViewHiding");
  EventUtils.synthesizeKey("KEY_Escape", {}, window);
  await viewHiddenPromise;
}

async function closeTabs() {
  for (var i = 0; i < openedTabs.length; i++) {
    await gBrowser.removeTab(openedTabs[i]);
  }
  Assert.equal(gBrowser.tabs.length, 1, "Should close all opened tabs");
}

async function getRecentlyBookmarkedItems() {
  let historyMenu = document.getElementById("panelMenu_bookmarksMenu");
  let items = historyMenu.querySelectorAll("toolbarbutton");
  Assert.ok(items, "Recently bookmarked items should exists");

  await TestUtils.waitForCondition(
    () => items[0].attributes !== "undefined",
    "Custom bookmark exists"
  );

  if (items) {
    Assert.equal(
      items[0]._placesNode.uri,
      bookmarkItems[1].url,
      "Should match the expected url"
    );
    Assert.equal(
      items[0].getAttribute("label"),
      bookmarkItems[1].title,
      "Should be the expected title"
    );
    Assert.equal(
      items[1]._placesNode.uri,
      bookmarkItems[0].url,
      "Should match the expected url"
    );
    Assert.equal(
      items[1].getAttribute("label"),
      bookmarkItems[0].title,
      "Should be the expected title"
    );
  }
  return Array.from(items).slice(0, 2);
}

add_setup(async function () {
  let libraryButton = CustomizableUI.getPlacementOfWidget("library-button");
  if (!libraryButton) {
    CustomizableUI.addWidgetToArea(
      "library-button",
      CustomizableUI.AREA_NAVBAR
    );
  }
  await PlacesUtils.bookmarks.insertTree({
    guid: PlacesUtils.bookmarks.menuGuid,
    children: bookmarkItems,
  });

  registerCleanupFunction(async () => {
    await PlacesUtils.bookmarks.eraseEverything();
    CustomizableUI.reset();
  });
});

add_task(async function test_recently_added() {
  await openBookmarksPanelInLibraryToolbarButton();

  let historyItems = await getRecentlyBookmarkedItems();

  for (let item of historyItems) {
    await openBookmarkedItemInNewTab(item);
  }

  await closeLibraryMenu();

  registerCleanupFunction(async () => {
    await closeTabs();
  });
});