summaryrefslogtreecommitdiffstats
path: root/toolkit/components/places/tests/bookmarks/test_bookmarks_search.js
blob: 2b34b2c8f987fa5d4195791ca82f5b7becfce0a6 (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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
add_task(async function invalid_input_throws() {
  Assert.throws(
    () => PlacesUtils.bookmarks.search(),
    /Query object is required/
  );
  Assert.throws(
    () => PlacesUtils.bookmarks.search(null),
    /Query object is required/
  );
  Assert.throws(
    () => PlacesUtils.bookmarks.search({ title: 50 }),
    /Title option must be a string/
  );
  Assert.throws(
    () => PlacesUtils.bookmarks.search({ url: { url: "wombat" } }),
    /Url option must be a string or a URL object/
  );
  Assert.throws(
    () => PlacesUtils.bookmarks.search(50),
    /Query must be an object or a string/
  );
  Assert.throws(
    () => PlacesUtils.bookmarks.search(true),
    /Query must be an object or a string/
  );
});

add_task(async function search_bookmark() {
  await PlacesUtils.bookmarks.eraseEverything();

  let bm1 = await PlacesUtils.bookmarks.insert({
    parentGuid: PlacesUtils.bookmarks.unfiledGuid,
    url: "http://example.com/",
    title: "a bookmark",
  });
  let bm2 = await PlacesUtils.bookmarks.insert({
    parentGuid: PlacesUtils.bookmarks.unfiledGuid,
    url: "http://example.org/",
    title: "another bookmark",
  });
  let bm3 = await PlacesUtils.bookmarks.insert({
    parentGuid: PlacesUtils.bookmarks.menuGuid,
    url: "http://menu.org/",
    title: "an on-menu bookmark",
  });
  let bm4 = await PlacesUtils.bookmarks.insert({
    parentGuid: PlacesUtils.bookmarks.toolbarGuid,
    url: "http://toolbar.org/",
    title: "an on-toolbar bookmark",
  });
  checkBookmarkObject(bm1);
  checkBookmarkObject(bm2);
  checkBookmarkObject(bm3);
  checkBookmarkObject(bm4);

  // finds a result by query
  let results = await PlacesUtils.bookmarks.search("example.com");
  Assert.equal(results.length, 1);
  checkBookmarkObject(results[0]);
  Assert.deepEqual(bm1, results[0]);

  // finds multiple results
  results = await PlacesUtils.bookmarks.search("example");
  Assert.equal(results.length, 2);
  checkBookmarkObject(results[0]);
  checkBookmarkObject(results[1]);

  // finds menu bookmarks
  results = await PlacesUtils.bookmarks.search("an on-menu bookmark");
  Assert.equal(results.length, 1);
  checkBookmarkObject(results[0]);
  Assert.deepEqual(bm3, results[0]);

  // finds toolbar bookmarks
  results = await PlacesUtils.bookmarks.search("an on-toolbar bookmark");
  Assert.equal(results.length, 1);
  checkBookmarkObject(results[0]);
  Assert.deepEqual(bm4, results[0]);

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

add_task(async function search_bookmark_by_query_object() {
  let bm1 = await PlacesUtils.bookmarks.insert({
    parentGuid: PlacesUtils.bookmarks.unfiledGuid,
    url: "http://example.com/",
    title: "a bookmark",
  });
  let bm2 = await PlacesUtils.bookmarks.insert({
    parentGuid: PlacesUtils.bookmarks.unfiledGuid,
    url: "http://example.org/",
    title: "another bookmark",
  });
  checkBookmarkObject(bm1);
  checkBookmarkObject(bm2);

  let results = await PlacesUtils.bookmarks.search({ query: "example.com" });
  Assert.equal(results.length, 1);
  checkBookmarkObject(results[0]);

  Assert.deepEqual(bm1, results[0]);

  results = await PlacesUtils.bookmarks.search({ query: "example" });
  Assert.equal(results.length, 2);
  checkBookmarkObject(results[0]);
  checkBookmarkObject(results[1]);

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

add_task(async function search_bookmark_by_url() {
  let bm1 = await PlacesUtils.bookmarks.insert({
    parentGuid: PlacesUtils.bookmarks.unfiledGuid,
    url: "http://example.com/",
    title: "a bookmark",
  });
  let bm2 = await PlacesUtils.bookmarks.insert({
    parentGuid: PlacesUtils.bookmarks.unfiledGuid,
    url: "http://example.org/path",
    title: "another bookmark",
  });
  let bm3 = await PlacesUtils.bookmarks.insert({
    parentGuid: PlacesUtils.bookmarks.unfiledGuid,
    url: "http://example.org/path",
    title: "third bookmark",
  });
  checkBookmarkObject(bm1);
  checkBookmarkObject(bm2);
  checkBookmarkObject(bm3);

  // finds the correct result by url
  let results = await PlacesUtils.bookmarks.search({
    url: "http://example.com/",
  });
  Assert.equal(results.length, 1);
  checkBookmarkObject(results[0]);
  Assert.deepEqual(bm1, results[0]);

  // normalizes the url
  results = await PlacesUtils.bookmarks.search({ url: "http:/example.com" });
  Assert.equal(results.length, 1);
  checkBookmarkObject(results[0]);
  Assert.deepEqual(bm1, results[0]);

  // returns multiple matches
  results = await PlacesUtils.bookmarks.search({
    url: "http://example.org/path",
  });
  Assert.equal(results.length, 2);

  // requires exact match
  results = await PlacesUtils.bookmarks.search({ url: "http://example.org/" });
  Assert.equal(results.length, 0);

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

add_task(async function search_bookmark_by_title() {
  let bm1 = await PlacesUtils.bookmarks.insert({
    parentGuid: PlacesUtils.bookmarks.unfiledGuid,
    url: "http://example.com/",
    title: "a bookmark",
  });
  let bm2 = await PlacesUtils.bookmarks.insert({
    parentGuid: PlacesUtils.bookmarks.unfiledGuid,
    url: "http://example.org/path",
    title: "another bookmark",
  });
  let bm3 = await PlacesUtils.bookmarks.insert({
    parentGuid: PlacesUtils.bookmarks.unfiledGuid,
    url: "http://example.net/",
    title: "another bookmark",
  });
  checkBookmarkObject(bm1);
  checkBookmarkObject(bm2);
  checkBookmarkObject(bm3);

  // finds the correct result by title
  let results = await PlacesUtils.bookmarks.search({ title: "a bookmark" });
  Assert.equal(results.length, 1);
  checkBookmarkObject(results[0]);
  Assert.deepEqual(bm1, results[0]);

  // returns multiple matches
  results = await PlacesUtils.bookmarks.search({ title: "another bookmark" });
  Assert.equal(results.length, 2);

  // requires exact match
  results = await PlacesUtils.bookmarks.search({ title: "bookmark" });
  Assert.equal(results.length, 0);

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

add_task(async function search_bookmark_combinations() {
  let bm1 = await PlacesUtils.bookmarks.insert({
    parentGuid: PlacesUtils.bookmarks.unfiledGuid,
    url: "http://example.com/",
    title: "a bookmark",
  });
  let bm2 = await PlacesUtils.bookmarks.insert({
    parentGuid: PlacesUtils.bookmarks.unfiledGuid,
    url: "http://example.org/path",
    title: "another bookmark",
  });
  let bm3 = await PlacesUtils.bookmarks.insert({
    parentGuid: PlacesUtils.bookmarks.unfiledGuid,
    url: "http://example.net/",
    title: "third bookmark",
  });
  checkBookmarkObject(bm1);
  checkBookmarkObject(bm2);
  checkBookmarkObject(bm3);

  // finds the correct result if title and url match
  let results = await PlacesUtils.bookmarks.search({
    url: "http://example.com/",
    title: "a bookmark",
  });
  Assert.equal(results.length, 1);
  checkBookmarkObject(results[0]);
  Assert.deepEqual(bm1, results[0]);

  // does not match if query is not matching but url and title match
  results = await PlacesUtils.bookmarks.search({
    url: "http://example.com/",
    title: "a bookmark",
    query: "nonexistent",
  });
  Assert.equal(results.length, 0);

  // does not match if one parameter is not matching
  results = await PlacesUtils.bookmarks.search({
    url: "http://what.ever",
    title: "a bookmark",
  });
  Assert.equal(results.length, 0);

  // query only matches if other fields match as well
  results = await PlacesUtils.bookmarks.search({
    query: "bookmark",
    url: "http://example.net/",
  });
  Assert.equal(results.length, 1);
  checkBookmarkObject(results[0]);
  Assert.deepEqual(bm3, results[0]);

  // non-matching query will also return no results
  results = await PlacesUtils.bookmarks.search({
    query: "nonexistent",
    url: "http://example.net/",
  });
  Assert.equal(results.length, 0);

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

add_task(async function search_folder() {
  let folder = await PlacesUtils.bookmarks.insert({
    parentGuid: PlacesUtils.bookmarks.menuGuid,
    type: PlacesUtils.bookmarks.TYPE_FOLDER,
    title: "a test folder",
  });
  let bm = await PlacesUtils.bookmarks.insert({
    parentGuid: folder.guid,
    url: "http://example.com/",
    title: "a bookmark",
  });
  checkBookmarkObject(folder);
  checkBookmarkObject(bm);

  // also finds folders
  let results = await PlacesUtils.bookmarks.search("a test folder");
  Assert.equal(results.length, 1);
  checkBookmarkObject(results[0]);
  Assert.equal(folder.title, results[0].title);
  Assert.equal(folder.type, results[0].type);
  Assert.equal(folder.parentGuid, results[0].parentGuid);

  // finds elements in folders
  results = await PlacesUtils.bookmarks.search("example.com");
  Assert.equal(results.length, 1);
  checkBookmarkObject(results[0]);
  Assert.deepEqual(bm, results[0]);
  Assert.equal(folder.guid, results[0].parentGuid);

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

add_task(async function search_includes_separators() {
  let bm1 = await PlacesUtils.bookmarks.insert({
    parentGuid: PlacesUtils.bookmarks.unfiledGuid,
    url: "http://example.com/",
    title: "a bookmark",
  });
  let bm2 = await PlacesUtils.bookmarks.insert({
    parentGuid: PlacesUtils.bookmarks.unfiledGuid,
    type: PlacesUtils.bookmarks.TYPE_SEPARATOR,
  });

  checkBookmarkObject(bm1);
  checkBookmarkObject(bm2);

  let results = await PlacesUtils.bookmarks.search({});
  Assert.ok(
    results.findIndex(bookmark => {
      return bookmark.guid == bm1.guid;
    }) > -1,
    "The bookmark was found in the results."
  );
  Assert.ok(
    results.findIndex(bookmark => {
      return bookmark.guid == bm2.guid;
    }) > -1,
    "The separator was included in the results."
  );

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

add_task(async function search_excludes_tags() {
  let bm1 = await PlacesUtils.bookmarks.insert({
    parentGuid: PlacesUtils.bookmarks.unfiledGuid,
    url: "http://example.com/",
    title: "a bookmark",
  });
  checkBookmarkObject(bm1);
  PlacesUtils.tagging.tagURI(bm1.url.URI, ["Test Tag"]);

  let results = await PlacesUtils.bookmarks.search("example.com");
  // If tags are not being excluded, this would return two results, one representing the tag.
  Assert.equal(1, results.length, "A single object was returned from search.");
  Assert.deepEqual(bm1, results[0], "The bookmark was returned.");

  results = await PlacesUtils.bookmarks.search("Test Tag");
  Assert.equal(0, results.length, "The tag folder was not returned.");

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