summaryrefslogtreecommitdiffstats
path: root/browser/components/places/tests/browser/browser_bookmarks_sidebar_search.js
blob: a425a9b0311a84ead7cc98f5be2558479052f715 (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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/**
 *  Test searching for bookmarks (by title and by tag) from the Bookmarks sidebar.
 */
"use strict";

let sidebar = document.getElementById("sidebar");

const TEST_URI = "http://example.com/";
const BOOKMARKS_COUNT = 4;
const TEST_PARENT_FOLDER = "testParentFolder";
const TEST_SIF_URL = "http://testsif.example.com/";
const TEST_SIF_TITLE = "TestSIF";
const TEST_NEW_TITLE = "NewTestSIF";

function assertBookmarks(searchValue) {
  let found = 0;

  let searchBox = sidebar.contentDocument.getElementById("search-box");

  ok(searchBox, "search box is in context");

  searchBox.value = searchValue;
  searchBox.doCommand();

  let tree = sidebar.contentDocument.getElementById("bookmarks-view");

  for (let i = 0; i < tree.view.rowCount; i++) {
    let cellText = tree.view.getCellText(i, tree.columns.getColumnAt(0));

    if (cellText.includes("example page")) {
      found++;
    }
  }

  info("Reset the search");
  searchBox.value = "";
  searchBox.doCommand();

  is(found, BOOKMARKS_COUNT, "found expected site");
}

async function showInFolder(aSearchStr, aParentFolderGuid) {
  let searchBox = sidebar.contentDocument.getElementById("search-box");

  searchBox.value = aSearchStr;
  searchBox.doCommand();

  let tree = sidebar.contentDocument.getElementById("bookmarks-view");
  let theNode = tree.view._getNodeForRow(0);
  let bookmarkGuid = theNode.bookmarkGuid;

  Assert.equal(theNode.uri, TEST_SIF_URL, "Found expected bookmark");

  info("Running Show in Folder command");
  tree.selectNode(theNode);
  tree.controller.doCommand("placesCmd_showInFolder");

  let treeNode = tree.selectedNode;
  Assert.equal(
    treeNode.parent.bookmarkGuid,
    aParentFolderGuid,
    "Containing folder node is correct"
  );
  Assert.equal(
    treeNode.bookmarkGuid,
    bookmarkGuid,
    "The searched bookmark guid matches selected node"
  );
  Assert.equal(
    treeNode.uri,
    TEST_SIF_URL,
    "The searched bookmark URL matches selected node"
  );

  info("Check the title will be applied the item when changing it");
  await PlacesUtils.bookmarks.update({
    guid: theNode.bookmarkGuid,
    title: TEST_NEW_TITLE,
  });
  Assert.equal(
    treeNode.title,
    TEST_NEW_TITLE,
    "New title is applied to the node"
  );
}

add_task(async function testTree() {
  // Add bookmarks and tags.
  for (let i = 0; i < BOOKMARKS_COUNT; i++) {
    let url = Services.io.newURI(TEST_URI + i);

    await PlacesUtils.bookmarks.insert({
      url,
      title: "example page " + i,
      parentGuid: PlacesUtils.bookmarks.toolbarGuid,
    });
    PlacesUtils.tagging.tagURI(url, ["test"]);
  }

  await withSidebarTree("bookmarks", function () {
    // Search a bookmark by its title.
    assertBookmarks("example.com");
    // Search a bookmark by its tag.
    assertBookmarks("test");
  });

  // Cleanup before testing Show in Folder.
  await PlacesUtils.bookmarks.eraseEverything();
});

add_task(async function testShowInFolder() {
  // Now test Show in Folder
  let parentFolder = await PlacesUtils.bookmarks.insert({
    parentGuid: PlacesUtils.bookmarks.unfiledGuid,
    type: PlacesUtils.bookmarks.TYPE_FOLDER,
    title: TEST_PARENT_FOLDER,
  });

  await PlacesUtils.bookmarks.insert({
    parentGuid: parentFolder.guid,
    title: TEST_SIF_TITLE,
    url: TEST_SIF_URL,
  });

  await withSidebarTree("bookmarks", async function () {
    await showInFolder(TEST_SIF_TITLE, parentFolder.guid);
  });

  // Cleanup
  await PlacesUtils.bookmarks.eraseEverything();
});

add_task(async function testRenameOnQueryResult() {
  await PlacesUtils.bookmarks.insert({
    parentGuid: PlacesUtils.bookmarks.toolbarGuid,
    title: TEST_SIF_TITLE,
    url: TEST_SIF_URL,
  });

  await withSidebarTree("bookmarks", async function () {
    const searchBox = sidebar.contentDocument.getElementById("search-box");

    searchBox.value = TEST_SIF_TITLE;
    searchBox.doCommand();

    const tree = sidebar.contentDocument.getElementById("bookmarks-view");
    const theNode = tree.view._getNodeForRow(0);

    info("Check the found bookmark");
    Assert.equal(theNode.uri, TEST_SIF_URL, "URI of bookmark found is correct");
    Assert.equal(
      theNode.title,
      TEST_SIF_TITLE,
      "Title of bookmark found is correct"
    );

    info("Check the title will be applied the item when changing it");
    await PlacesUtils.bookmarks.update({
      guid: theNode.bookmarkGuid,
      title: TEST_NEW_TITLE,
    });

    // As the query result is refreshed once then the node also is regenerated,
    // need to get the result node from the tree again.
    Assert.equal(
      tree.view._getNodeForRow(0).bookmarkGuid,
      theNode.bookmarkGuid,
      "GUID of node regenerated is correct"
    );
    Assert.equal(
      tree.view._getNodeForRow(0).uri,
      theNode.uri,
      "URI of node regenerated is correct"
    );
    Assert.equal(
      tree.view._getNodeForRow(0).parentGuid,
      theNode.parentGuid,
      "parentGuid of node regenerated is correct"
    );
    Assert.equal(
      tree.view._getNodeForRow(0).title,
      TEST_NEW_TITLE,
      "New title is applied to the node"
    );

    info("Check the new date will be applied the item when changing it");
    const now = new Date();
    await PlacesUtils.bookmarks.update({
      guid: theNode.bookmarkGuid,
      dateAdded: now,
      lastModified: now,
    });

    Assert.equal(
      tree.view._getNodeForRow(0).uri,
      theNode.uri,
      "URI of node regenerated is correct"
    );
    Assert.equal(
      tree.view._getNodeForRow(0).dateAdded,
      now.getTime() * 1000,
      "New dateAdded is applied to the node"
    );
    Assert.equal(
      tree.view._getNodeForRow(0).lastModified,
      now.getTime() * 1000,
      "New lastModified is applied to the node"
    );
  });

  // Cleanup before testing Show in Folder.
  await PlacesUtils.bookmarks.eraseEverything();
});