summaryrefslogtreecommitdiffstats
path: root/toolkit/components/places/tests/bookmarks/test_bookmarks_search.js
blob: 02f7c5460a393477870592143605825badd140b4 (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
add_task(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(function* search_bookmark() {
  yield PlacesUtils.bookmarks.eraseEverything();

  let bm1 = yield PlacesUtils.bookmarks.insert({ parentGuid: PlacesUtils.bookmarks.unfiledGuid,
                                                 url: "http://example.com/",
                                                 title: "a bookmark" });
  let bm2 = yield PlacesUtils.bookmarks.insert({ parentGuid: PlacesUtils.bookmarks.unfiledGuid,
                                                 url: "http://example.org/",
                                                 title: "another bookmark" });
  let bm3 = yield PlacesUtils.bookmarks.insert({ parentGuid: PlacesUtils.bookmarks.menuGuid,
                                                 url: "http://menu.org/",
                                                 title: "an on-menu bookmark" });
  let bm4 = yield 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 = yield PlacesUtils.bookmarks.search("example.com");
  Assert.equal(results.length, 1);
  checkBookmarkObject(results[0]);
  Assert.deepEqual(bm1, results[0]);

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

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

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

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

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

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

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

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

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

add_task(function* search_bookmark_by_url() {
  let bm1 = yield PlacesUtils.bookmarks.insert({ parentGuid: PlacesUtils.bookmarks.unfiledGuid,
                                                 url: "http://example.com/",
                                                 title: "a bookmark" });
  let bm2 = yield PlacesUtils.bookmarks.insert({ parentGuid: PlacesUtils.bookmarks.unfiledGuid,
                                                 url: "http://example.org/path",
                                                 title: "another bookmark" });
  let bm3 = yield 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 = yield PlacesUtils.bookmarks.search({url: "http://example.com/"});
  Assert.equal(results.length, 1);
  checkBookmarkObject(results[0]);
  Assert.deepEqual(bm1, results[0]);

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

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

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

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

add_task(function* search_bookmark_by_title() {
  let bm1 = yield PlacesUtils.bookmarks.insert({ parentGuid: PlacesUtils.bookmarks.unfiledGuid,
                                                 url: "http://example.com/",
                                                 title: "a bookmark" });
  let bm2 = yield PlacesUtils.bookmarks.insert({ parentGuid: PlacesUtils.bookmarks.unfiledGuid,
                                                 url: "http://example.org/path",
                                                 title: "another bookmark" });
  let bm3 = yield 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 = yield PlacesUtils.bookmarks.search({title: "a bookmark"});
  Assert.equal(results.length, 1);
  checkBookmarkObject(results[0]);
  Assert.deepEqual(bm1, results[0]);

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

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

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

add_task(function* search_bookmark_combinations() {
  let bm1 = yield PlacesUtils.bookmarks.insert({ parentGuid: PlacesUtils.bookmarks.unfiledGuid,
                                                 url: "http://example.com/",
                                                 title: "a bookmark" });
  let bm2 = yield PlacesUtils.bookmarks.insert({ parentGuid: PlacesUtils.bookmarks.unfiledGuid,
                                                 url: "http://example.org/path",
                                                 title: "another bookmark" });
  let bm3 = yield 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 = yield 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 = yield 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 = yield 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 = yield 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 = yield PlacesUtils.bookmarks.search({query: "nonexistent", url: "http://example.net/"});
  Assert.equal(results.length, 0);

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

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

  // also finds folders
  let results = yield 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 = yield 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);

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