toolkit/components/places/tests/unit/test_mozIAsyncLivemarks.js

branch
TOR_BUG_3246
changeset 7
129ffea94266
equal deleted inserted replaced
-1:000000000000 0:270a69bec23f
1 /* Any copyright is dedicated to the Public Domain.
2 http://creativecommons.org/publicdomain/zero/1.0/ */
3
4 // Tests functionality of the mozIAsyncLivemarks interface.
5
6 const FEED_URI = NetUtil.newURI("http://feed.rss/");
7 const SITE_URI = NetUtil.newURI("http://site.org/");
8
9
10 add_task(function test_addLivemark_noArguments_throws()
11 {
12 try {
13 yield PlacesUtils.livemarks.addLivemark();
14 do_throw("Invoking addLivemark with no arguments should throw");
15 } catch (ex) {
16 // The error is actually generated by XPConnect.
17 do_check_eq(ex.result, Cr.NS_ERROR_XPC_NOT_ENOUGH_ARGS);
18 }
19 });
20
21 add_task(function test_addLivemark_emptyObject_throws()
22 {
23 try {
24 yield PlacesUtils.livemarks.addLivemark({});
25 do_throw("Invoking addLivemark with empty object should throw");
26 } catch (ex) {
27 do_check_eq(ex.result, Cr.NS_ERROR_INVALID_ARG);
28 }
29 });
30
31 add_task(function test_addLivemark_badParentId_throws()
32 {
33 try {
34 yield PlacesUtils.livemarks.addLivemark({ parentId: "test" });
35 do_throw("Invoking addLivemark with a bad parent id should throw");
36 } catch (ex) {
37 do_check_eq(ex.result, Cr.NS_ERROR_INVALID_ARG);
38 }
39 });
40
41 add_task(function test_addLivemark_invalidParentId_throws()
42 {
43 try {
44 yield PlacesUtils.livemarks.addLivemark({ parentId: -2 });
45 do_throw("Invoking addLivemark with an invalid parent id should throw");
46 } catch (ex) {
47 do_check_eq(ex.result, Cr.NS_ERROR_INVALID_ARG);
48 }
49 });
50
51 add_task(function test_addLivemark_noIndex_throws()
52 {
53 try {
54 yield PlacesUtils.livemarks.addLivemark({
55 parentId: PlacesUtils.unfiledBookmarksFolderId });
56 do_throw("Invoking addLivemark with no index should throw");
57 } catch (ex) {
58 do_check_eq(ex.result, Cr.NS_ERROR_INVALID_ARG);
59 }
60 });
61
62 add_task(function test_addLivemark_badIndex_throws()
63 {
64 try {
65 yield PlacesUtils.livemarks.addLivemark(
66 { parentId: PlacesUtils.unfiledBookmarksFolderId
67 , index: "test" });
68 do_throw("Invoking addLivemark with a bad index should throw");
69 } catch (ex) {
70 do_check_eq(ex.result, Cr.NS_ERROR_INVALID_ARG);
71 }
72 });
73
74 add_task(function test_addLivemark_invalidIndex_throws()
75 {
76 try {
77 yield PlacesUtils.livemarks.addLivemark(
78 { parentId: PlacesUtils.unfiledBookmarksFolderId
79 , index: -2
80 });
81 do_throw("Invoking addLivemark with an invalid index should throw");
82 } catch (ex) {
83 do_check_eq(ex.result, Cr.NS_ERROR_INVALID_ARG);
84 }
85 });
86
87 add_task(function test_addLivemark_noFeedURI_throws()
88 {
89 try {
90 yield PlacesUtils.livemarks.addLivemark(
91 { parentId: PlacesUtils.unfiledBookmarksFolderId
92 , index: PlacesUtils.bookmarks.DEFAULT_INDEX });
93 do_throw("Invoking addLivemark with no feedURI should throw");
94 } catch (ex) {
95 do_check_eq(ex.result, Cr.NS_ERROR_INVALID_ARG);
96 }
97 });
98
99 add_task(function test_addLivemark_badFeedURI_throws()
100 {
101 try {
102 yield PlacesUtils.livemarks.addLivemark(
103 { parentId: PlacesUtils.unfiledBookmarksFolderId
104 , index: PlacesUtils.bookmarks.DEFAULT_INDEX
105 , feedURI: "test" });
106 do_throw("Invoking addLivemark with a bad feedURI should throw");
107 } catch (ex) {
108 do_check_eq(ex.result, Cr.NS_ERROR_INVALID_ARG);
109 }
110 });
111
112 add_task(function test_addLivemark_badSiteURI_throws()
113 {
114 try {
115 yield PlacesUtils.livemarks.addLivemark(
116 { parentId: PlacesUtils.unfiledBookmarksFolderId
117 , index: PlacesUtils.bookmarks.DEFAULT_INDEX
118 , feedURI: FEED_URI
119 , siteURI: "test" });
120 do_throw("Invoking addLivemark with a bad siteURI should throw");
121 } catch (ex) {
122 do_check_eq(ex.result, Cr.NS_ERROR_INVALID_ARG);
123 }
124 });
125
126 add_task(function test_addLivemark_badGuid_throws()
127 {
128 try {
129 yield PlacesUtils.livemarks.addLivemark(
130 { parentId: PlacesUtils.unfiledBookmarksFolderId
131 , index: PlacesUtils.bookmarks.DEFAULT_INDEX
132 , feedURI: FEED_URI
133 , guid: "123456" });
134 do_throw("Invoking addLivemark with a bad guid should throw");
135 } catch (ex) {
136 do_check_eq(ex.result, Cr.NS_ERROR_INVALID_ARG);
137 }
138 });
139
140 add_task(function test_addLivemark_badCallback_throws()
141 {
142 try {
143 yield PlacesUtils.livemarks.addLivemark(
144 { parentId: PlacesUtils.unfiledBookmarksFolderId
145 , index: PlacesUtils.bookmarks.DEFAULT_INDEX
146 , feedURI: FEED_URI
147 }, "test");
148 do_throw("Invoking addLivemark with a bad callback should throw");
149 } catch (ex) {
150 // The error is actually generated by XPConnect.
151 do_check_eq(ex.result, Cr.NS_ERROR_XPC_BAD_CONVERT_JS);
152 }
153 });
154
155 add_task(function test_addLivemark_noCallback_succeeds()
156 {
157 let onItemAddedCalled = false;
158 PlacesUtils.bookmarks.addObserver({
159 __proto__: NavBookmarkObserver.prototype,
160 onItemAdded: function onItemAdded(aItemId, aParentId, aIndex, aItemType,
161 aURI, aTitle)
162 {
163 onItemAddedCalled = true;
164 PlacesUtils.bookmarks.removeObserver(this);
165 do_check_eq(aParentId, PlacesUtils.unfiledBookmarksFolderId);
166 do_check_eq(aIndex, 0);
167 do_check_eq(aItemType, Ci.nsINavBookmarksService.TYPE_FOLDER);
168 do_check_eq(aTitle, "test");
169 }
170 }, false);
171
172 yield PlacesUtils.livemarks.addLivemark(
173 { title: "test"
174 , parentId: PlacesUtils.unfiledBookmarksFolderId
175 , index: PlacesUtils.bookmarks.DEFAULT_INDEX
176 , feedURI: FEED_URI });
177 do_check_true(onItemAddedCalled);
178 });
179
180
181 add_task(function test_addLivemark_noSiteURI_succeeds()
182 {
183 let livemark = yield PlacesUtils.livemarks.addLivemark(
184 { title: "test"
185 , parentId: PlacesUtils.unfiledBookmarksFolderId
186 , index: PlacesUtils.bookmarks.DEFAULT_INDEX
187 , feedURI: FEED_URI
188 });
189 do_check_true(livemark.id > 0);
190 do_check_valid_places_guid(livemark.guid);
191 do_check_eq(livemark.title, "test");
192 do_check_eq(livemark.parentId, PlacesUtils.unfiledBookmarksFolderId);
193 do_check_eq(livemark.index, PlacesUtils.bookmarks.getItemIndex(livemark.id));
194 do_check_eq(livemark.lastModified, PlacesUtils.bookmarks.getItemLastModified(livemark.id));
195 do_check_true(livemark.feedURI.equals(FEED_URI));
196 do_check_eq(livemark.siteURI, null);
197 });
198
199 add_task(function test_addLivemark_succeeds()
200 {
201 let livemark = yield PlacesUtils.livemarks.addLivemark(
202 { title: "test"
203 , parentId: PlacesUtils.unfiledBookmarksFolderId
204 , index: PlacesUtils.bookmarks.DEFAULT_INDEX
205 , feedURI: FEED_URI
206 , siteURI: SITE_URI
207 });
208
209 do_check_true(livemark.id > 0);
210 do_check_valid_places_guid(livemark.guid);
211 do_check_eq(livemark.title, "test");
212 do_check_eq(livemark.parentId, PlacesUtils.unfiledBookmarksFolderId);
213 do_check_eq(livemark.index, PlacesUtils.bookmarks.getItemIndex(livemark.id));
214 do_check_eq(livemark.lastModified, PlacesUtils.bookmarks.getItemLastModified(livemark.id));
215 do_check_true(livemark.feedURI.equals(FEED_URI));
216 do_check_true(livemark.siteURI.equals(SITE_URI));
217 do_check_true(PlacesUtils.annotations
218 .itemHasAnnotation(livemark.id,
219 PlacesUtils.LMANNO_FEEDURI));
220 do_check_true(PlacesUtils.annotations
221 .itemHasAnnotation(livemark.id,
222 PlacesUtils.LMANNO_SITEURI));
223 });
224
225 add_task(function test_addLivemark_bogusid_succeeds()
226 {
227 let livemark = yield PlacesUtils.livemarks.addLivemark(
228 { id: 100 // Should be ignored.
229 , title: "test"
230 , parentId: PlacesUtils.unfiledBookmarksFolderId
231 , index: PlacesUtils.bookmarks.DEFAULT_INDEX
232 , feedURI: FEED_URI
233 , siteURI: SITE_URI
234 });
235 do_check_true(livemark.id > 0);
236 do_check_neq(livemark.id, 100);
237 });
238
239 add_task(function test_addLivemark_bogusParent_fails()
240 {
241 try {
242 yield PlacesUtils.livemarks.addLivemark(
243 { title: "test"
244 , parentId: 187
245 , index: PlacesUtils.bookmarks.DEFAULT_INDEX
246 , feedURI: FEED_URI
247 });
248 do_throw("Adding a livemark with a bogus parent should fail");
249 } catch(ex) {}
250 });
251
252 add_task(function test_addLivemark_intoLivemark_fails()
253 {
254 let livemark = yield PlacesUtils.livemarks.addLivemark(
255 { title: "test"
256 , parentId: PlacesUtils.unfiledBookmarksFolderId
257 , index: PlacesUtils.bookmarks.DEFAULT_INDEX
258 , feedURI: FEED_URI
259 });
260 do_check_true(Boolean(livemark));
261
262 try {
263 yield PlacesUtils.livemarks.addLivemark(
264 { title: "test"
265 , parentId: livemark.id
266 , index: PlacesUtils.bookmarks.DEFAULT_INDEX
267 , feedURI: FEED_URI
268 });
269 do_throw("Adding a livemark into a livemark should fail");
270 } catch(ex) {}
271 });
272
273 add_task(function test_addLivemark_forceGuid_succeeds()
274 {
275 let checkLivemark = aLivemark => {
276 do_check_eq(aLivemark.guid, "1234567890AB");
277 do_check_guid_for_bookmark(aLivemark.id, "1234567890AB");
278 };
279
280 let livemark = yield PlacesUtils.livemarks.addLivemark(
281 { title: "test"
282 , parentId: PlacesUtils.unfiledBookmarksFolderId
283 , index: PlacesUtils.bookmarks.DEFAULT_INDEX
284 , feedURI: FEED_URI
285 , guid: "1234567890AB"
286 });
287 checkLivemark(livemark);
288 });
289
290 add_task(function test_addLivemark_lastModified_succeeds()
291 {
292 let now = Date.now() * 1000;
293 let livemark = yield PlacesUtils.livemarks.addLivemark(
294 { title: "test"
295 , parentId: PlacesUtils.unfiledBookmarksFolderId
296 , index: PlacesUtils.bookmarks.DEFAULT_INDEX
297 , feedURI: FEED_URI
298 , lastModified: now
299 });
300 do_check_eq(livemark.lastModified, now);
301 });
302
303 add_task(function test_removeLivemark_emptyObject_throws()
304 {
305 try {
306 yield PlacesUtils.livemarks.removeLivemark({});
307 do_throw("Invoking removeLivemark with empty object should throw");
308 } catch (ex) {
309 do_check_eq(ex.result, Cr.NS_ERROR_INVALID_ARG);
310 }
311 });
312
313 add_task(function test_removeLivemark_noValidId_throws()
314 {
315 try {
316 yield PlacesUtils.livemarks.removeLivemark({ id: -10, guid: "test"});
317 do_throw("Invoking removeLivemark with no valid id should throw");
318 } catch (ex) {
319 do_check_eq(ex.result, Cr.NS_ERROR_INVALID_ARG);
320 }
321 });
322
323 add_task(function test_removeLivemark_nonExistent_fails()
324 {
325 try {
326 yield PlacesUtils.livemarks.removeLivemark({ id: 1337 });
327 do_throw("Removing a non-existent livemark should fail");
328 }
329 catch(ex) {
330 }
331 });
332
333 add_task(function test_removeLivemark_guid_succeeds()
334 {
335 let livemark = yield PlacesUtils.livemarks.addLivemark(
336 { title: "test"
337 , parentId: PlacesUtils.unfiledBookmarksFolderId
338 , index: PlacesUtils.bookmarks.DEFAULT_INDEX
339 , feedURI: FEED_URI
340 , guid: "234567890ABC"
341 });
342
343
344 do_check_eq(livemark.guid, "234567890ABC");
345
346 yield PlacesUtils.livemarks.removeLivemark({
347 id: 789, guid: "234567890ABC"
348 });
349
350 do_check_eq(PlacesUtils.bookmarks.getItemIndex(livemark.id), -1);
351 });
352
353 add_task(function test_removeLivemark_id_succeeds()
354 {
355 let livemark = yield PlacesUtils.livemarks.addLivemark(
356 { title: "test"
357 , parentId: PlacesUtils.unfiledBookmarksFolderId
358 , index: PlacesUtils.bookmarks.DEFAULT_INDEX
359 , feedURI: FEED_URI
360 });
361
362 yield PlacesUtils.livemarks.removeLivemark({ id: livemark.id });
363 do_check_eq(PlacesUtils.bookmarks.getItemIndex(livemark.id), -1);
364 });
365
366 add_task(function test_getLivemark_emptyObject_throws()
367 {
368 try {
369 yield PlacesUtils.livemarks.getLivemark({});
370 do_throw("Invoking getLivemark with empty object should throw");
371 } catch (ex) {
372 do_check_eq(ex.result, Cr.NS_ERROR_INVALID_ARG);
373 }
374 });
375
376 add_task(function test_getLivemark_noValidId_throws()
377 {
378 try {
379 yield PlacesUtils.livemarks.getLivemark({ id: -10, guid: "test"});
380 do_throw("Invoking getLivemark with no valid id should throw");
381 } catch (ex) {
382 do_check_eq(ex.result, Cr.NS_ERROR_INVALID_ARG);
383 }
384 });
385
386 add_task(function test_getLivemark_nonExistentId_fails()
387 {
388 try {
389 yield PlacesUtils.livemarks.getLivemark({ id: 1234 });
390 do_throw("getLivemark for a non existent id should fail");
391 }
392 catch(ex) {}
393 });
394
395 add_task(function test_getLivemark_nonExistentGUID_fails()
396 {
397 try {
398 yield PlacesUtils.livemarks.getLivemark({ guid: "34567890ABCD" });
399 do_throw("getLivemark for a non-existent guid should fail");
400 }
401 catch(ex) {}
402 });
403
404 add_task(function test_getLivemark_guid_succeeds()
405 {
406 yield PlacesUtils.livemarks.addLivemark(
407 { title: "test"
408 , parentId: PlacesUtils.unfiledBookmarksFolderId
409 , index: PlacesUtils.bookmarks.DEFAULT_INDEX
410 , feedURI: FEED_URI
411 , guid: "34567890ABCD" });
412
413 // invalid id to check the guid wins.
414 let livemark =
415 yield PlacesUtils.livemarks.getLivemark({ id: 789, guid: "34567890ABCD" });
416
417 do_check_eq(livemark.title, "test");
418 do_check_eq(livemark.parentId, PlacesUtils.unfiledBookmarksFolderId);
419 do_check_eq(livemark.index, PlacesUtils.bookmarks.getItemIndex(livemark.id));
420 do_check_true(livemark.feedURI.equals(FEED_URI));
421 do_check_eq(livemark.siteURI, null);
422 do_check_eq(livemark.guid, "34567890ABCD");
423 });
424
425 add_task(function test_getLivemark_id_succeeds()
426 {
427 let livemark = yield PlacesUtils.livemarks.addLivemark(
428 { title: "test"
429 , parentId: PlacesUtils.unfiledBookmarksFolderId
430 , index: PlacesUtils.bookmarks.DEFAULT_INDEX
431 , feedURI: FEED_URI
432 });
433
434 livemark = yield PlacesUtils.livemarks.getLivemark({ id: livemark.id });
435
436 do_check_eq(livemark.title, "test");
437 do_check_eq(livemark.parentId, PlacesUtils.unfiledBookmarksFolderId);
438 do_check_eq(livemark.index, PlacesUtils.bookmarks.getItemIndex(livemark.id));
439 do_check_true(livemark.feedURI.equals(FEED_URI));
440 do_check_eq(livemark.siteURI, null);
441 do_check_guid_for_bookmark(livemark.id, livemark.guid);
442 });
443
444 add_task(function test_getLivemark_removeItem_contention()
445 {
446 PlacesUtils.livemarks.addLivemark({ title: "test"
447 , parentId: PlacesUtils.unfiledBookmarksFolderId
448 , index: PlacesUtils.bookmarks.DEFAULT_INDEX
449 , feedURI: FEED_URI
450 });
451 PlacesUtils.bookmarks.removeFolderChildren(PlacesUtils.unfiledBookmarksFolderId);
452 PlacesUtils.livemarks.addLivemark({ title: "test"
453 , parentId: PlacesUtils.unfiledBookmarksFolderId
454 , index: PlacesUtils.bookmarks.DEFAULT_INDEX
455 , feedURI: FEED_URI
456 });
457 let id = PlacesUtils.bookmarks.getIdForItemAt(PlacesUtils.unfiledBookmarksFolderId,
458 PlacesUtils.bookmarks.DEFAULT_INDEX);
459
460 let livemark = yield PlacesUtils.livemarks.getLivemark({ id: id });
461
462 do_check_eq(livemark.title, "test");
463 do_check_eq(livemark.parentId, PlacesUtils.unfiledBookmarksFolderId);
464 do_check_eq(livemark.index, PlacesUtils.bookmarks.getItemIndex(livemark.id));
465 do_check_true(livemark.feedURI.equals(FEED_URI));
466 do_check_eq(livemark.siteURI, null);
467 do_check_guid_for_bookmark(livemark.id, livemark.guid);
468 });
469
470 add_task(function test_title_change()
471 {
472 let livemark = yield PlacesUtils.livemarks.addLivemark(
473 { title: "test"
474 , parentId: PlacesUtils.unfiledBookmarksFolderId
475 , index: PlacesUtils.bookmarks.DEFAULT_INDEX
476 , feedURI: FEED_URI });
477
478 PlacesUtils.bookmarks.setItemTitle(livemark.id, "test2");
479 do_check_eq(livemark.title, "test2");
480 });
481
482 add_task(function test_livemark_move()
483 {
484 let livemark = yield PlacesUtils.livemarks.addLivemark(
485 { title: "test"
486 , parentId: PlacesUtils.unfiledBookmarksFolderId
487 , index: PlacesUtils.bookmarks.DEFAULT_INDEX
488 , feedURI: FEED_URI } );
489
490 PlacesUtils.bookmarks.moveItem(livemark.id,
491 PlacesUtils.toolbarFolderId,
492 PlacesUtils.bookmarks.DEFAULT_INDEX);
493 do_check_eq(livemark.parentId, PlacesUtils.toolbarFolderId);
494 do_check_eq(livemark.index, PlacesUtils.bookmarks.getItemIndex(livemark.id));
495 });
496
497 function run_test() {
498 run_next_test();
499 }

mercurial