michael@0: /* Any copyright is dedicated to the Public Domain. michael@0: http://creativecommons.org/publicdomain/zero/1.0/ */ michael@0: michael@0: // Tests functionality of the mozIAsyncLivemarks interface. michael@0: michael@0: const FEED_URI = NetUtil.newURI("http://feed.rss/"); michael@0: const SITE_URI = NetUtil.newURI("http://site.org/"); michael@0: michael@0: michael@0: add_task(function test_addLivemark_noArguments_throws() michael@0: { michael@0: try { michael@0: yield PlacesUtils.livemarks.addLivemark(); michael@0: do_throw("Invoking addLivemark with no arguments should throw"); michael@0: } catch (ex) { michael@0: // The error is actually generated by XPConnect. michael@0: do_check_eq(ex.result, Cr.NS_ERROR_XPC_NOT_ENOUGH_ARGS); michael@0: } michael@0: }); michael@0: michael@0: add_task(function test_addLivemark_emptyObject_throws() michael@0: { michael@0: try { michael@0: yield PlacesUtils.livemarks.addLivemark({}); michael@0: do_throw("Invoking addLivemark with empty object should throw"); michael@0: } catch (ex) { michael@0: do_check_eq(ex.result, Cr.NS_ERROR_INVALID_ARG); michael@0: } michael@0: }); michael@0: michael@0: add_task(function test_addLivemark_badParentId_throws() michael@0: { michael@0: try { michael@0: yield PlacesUtils.livemarks.addLivemark({ parentId: "test" }); michael@0: do_throw("Invoking addLivemark with a bad parent id should throw"); michael@0: } catch (ex) { michael@0: do_check_eq(ex.result, Cr.NS_ERROR_INVALID_ARG); michael@0: } michael@0: }); michael@0: michael@0: add_task(function test_addLivemark_invalidParentId_throws() michael@0: { michael@0: try { michael@0: yield PlacesUtils.livemarks.addLivemark({ parentId: -2 }); michael@0: do_throw("Invoking addLivemark with an invalid parent id should throw"); michael@0: } catch (ex) { michael@0: do_check_eq(ex.result, Cr.NS_ERROR_INVALID_ARG); michael@0: } michael@0: }); michael@0: michael@0: add_task(function test_addLivemark_noIndex_throws() michael@0: { michael@0: try { michael@0: yield PlacesUtils.livemarks.addLivemark({ michael@0: parentId: PlacesUtils.unfiledBookmarksFolderId }); michael@0: do_throw("Invoking addLivemark with no index should throw"); michael@0: } catch (ex) { michael@0: do_check_eq(ex.result, Cr.NS_ERROR_INVALID_ARG); michael@0: } michael@0: }); michael@0: michael@0: add_task(function test_addLivemark_badIndex_throws() michael@0: { michael@0: try { michael@0: yield PlacesUtils.livemarks.addLivemark( michael@0: { parentId: PlacesUtils.unfiledBookmarksFolderId michael@0: , index: "test" }); michael@0: do_throw("Invoking addLivemark with a bad index should throw"); michael@0: } catch (ex) { michael@0: do_check_eq(ex.result, Cr.NS_ERROR_INVALID_ARG); michael@0: } michael@0: }); michael@0: michael@0: add_task(function test_addLivemark_invalidIndex_throws() michael@0: { michael@0: try { michael@0: yield PlacesUtils.livemarks.addLivemark( michael@0: { parentId: PlacesUtils.unfiledBookmarksFolderId michael@0: , index: -2 michael@0: }); michael@0: do_throw("Invoking addLivemark with an invalid index should throw"); michael@0: } catch (ex) { michael@0: do_check_eq(ex.result, Cr.NS_ERROR_INVALID_ARG); michael@0: } michael@0: }); michael@0: michael@0: add_task(function test_addLivemark_noFeedURI_throws() michael@0: { michael@0: try { michael@0: yield PlacesUtils.livemarks.addLivemark( michael@0: { parentId: PlacesUtils.unfiledBookmarksFolderId michael@0: , index: PlacesUtils.bookmarks.DEFAULT_INDEX }); michael@0: do_throw("Invoking addLivemark with no feedURI should throw"); michael@0: } catch (ex) { michael@0: do_check_eq(ex.result, Cr.NS_ERROR_INVALID_ARG); michael@0: } michael@0: }); michael@0: michael@0: add_task(function test_addLivemark_badFeedURI_throws() michael@0: { michael@0: try { michael@0: yield PlacesUtils.livemarks.addLivemark( michael@0: { parentId: PlacesUtils.unfiledBookmarksFolderId michael@0: , index: PlacesUtils.bookmarks.DEFAULT_INDEX michael@0: , feedURI: "test" }); michael@0: do_throw("Invoking addLivemark with a bad feedURI should throw"); michael@0: } catch (ex) { michael@0: do_check_eq(ex.result, Cr.NS_ERROR_INVALID_ARG); michael@0: } michael@0: }); michael@0: michael@0: add_task(function test_addLivemark_badSiteURI_throws() michael@0: { michael@0: try { michael@0: yield PlacesUtils.livemarks.addLivemark( michael@0: { parentId: PlacesUtils.unfiledBookmarksFolderId michael@0: , index: PlacesUtils.bookmarks.DEFAULT_INDEX michael@0: , feedURI: FEED_URI michael@0: , siteURI: "test" }); michael@0: do_throw("Invoking addLivemark with a bad siteURI should throw"); michael@0: } catch (ex) { michael@0: do_check_eq(ex.result, Cr.NS_ERROR_INVALID_ARG); michael@0: } michael@0: }); michael@0: michael@0: add_task(function test_addLivemark_badGuid_throws() michael@0: { michael@0: try { michael@0: yield PlacesUtils.livemarks.addLivemark( michael@0: { parentId: PlacesUtils.unfiledBookmarksFolderId michael@0: , index: PlacesUtils.bookmarks.DEFAULT_INDEX michael@0: , feedURI: FEED_URI michael@0: , guid: "123456" }); michael@0: do_throw("Invoking addLivemark with a bad guid should throw"); michael@0: } catch (ex) { michael@0: do_check_eq(ex.result, Cr.NS_ERROR_INVALID_ARG); michael@0: } michael@0: }); michael@0: michael@0: add_task(function test_addLivemark_badCallback_throws() michael@0: { michael@0: try { michael@0: yield PlacesUtils.livemarks.addLivemark( michael@0: { parentId: PlacesUtils.unfiledBookmarksFolderId michael@0: , index: PlacesUtils.bookmarks.DEFAULT_INDEX michael@0: , feedURI: FEED_URI michael@0: }, "test"); michael@0: do_throw("Invoking addLivemark with a bad callback should throw"); michael@0: } catch (ex) { michael@0: // The error is actually generated by XPConnect. michael@0: do_check_eq(ex.result, Cr.NS_ERROR_XPC_BAD_CONVERT_JS); michael@0: } michael@0: }); michael@0: michael@0: add_task(function test_addLivemark_noCallback_succeeds() michael@0: { michael@0: let onItemAddedCalled = false; michael@0: PlacesUtils.bookmarks.addObserver({ michael@0: __proto__: NavBookmarkObserver.prototype, michael@0: onItemAdded: function onItemAdded(aItemId, aParentId, aIndex, aItemType, michael@0: aURI, aTitle) michael@0: { michael@0: onItemAddedCalled = true; michael@0: PlacesUtils.bookmarks.removeObserver(this); michael@0: do_check_eq(aParentId, PlacesUtils.unfiledBookmarksFolderId); michael@0: do_check_eq(aIndex, 0); michael@0: do_check_eq(aItemType, Ci.nsINavBookmarksService.TYPE_FOLDER); michael@0: do_check_eq(aTitle, "test"); michael@0: } michael@0: }, false); michael@0: michael@0: yield PlacesUtils.livemarks.addLivemark( michael@0: { title: "test" michael@0: , parentId: PlacesUtils.unfiledBookmarksFolderId michael@0: , index: PlacesUtils.bookmarks.DEFAULT_INDEX michael@0: , feedURI: FEED_URI }); michael@0: do_check_true(onItemAddedCalled); michael@0: }); michael@0: michael@0: michael@0: add_task(function test_addLivemark_noSiteURI_succeeds() michael@0: { michael@0: let livemark = yield PlacesUtils.livemarks.addLivemark( michael@0: { title: "test" michael@0: , parentId: PlacesUtils.unfiledBookmarksFolderId michael@0: , index: PlacesUtils.bookmarks.DEFAULT_INDEX michael@0: , feedURI: FEED_URI michael@0: }); michael@0: do_check_true(livemark.id > 0); michael@0: do_check_valid_places_guid(livemark.guid); michael@0: do_check_eq(livemark.title, "test"); michael@0: do_check_eq(livemark.parentId, PlacesUtils.unfiledBookmarksFolderId); michael@0: do_check_eq(livemark.index, PlacesUtils.bookmarks.getItemIndex(livemark.id)); michael@0: do_check_eq(livemark.lastModified, PlacesUtils.bookmarks.getItemLastModified(livemark.id)); michael@0: do_check_true(livemark.feedURI.equals(FEED_URI)); michael@0: do_check_eq(livemark.siteURI, null); michael@0: }); michael@0: michael@0: add_task(function test_addLivemark_succeeds() michael@0: { michael@0: let livemark = yield PlacesUtils.livemarks.addLivemark( michael@0: { title: "test" michael@0: , parentId: PlacesUtils.unfiledBookmarksFolderId michael@0: , index: PlacesUtils.bookmarks.DEFAULT_INDEX michael@0: , feedURI: FEED_URI michael@0: , siteURI: SITE_URI michael@0: }); michael@0: michael@0: do_check_true(livemark.id > 0); michael@0: do_check_valid_places_guid(livemark.guid); michael@0: do_check_eq(livemark.title, "test"); michael@0: do_check_eq(livemark.parentId, PlacesUtils.unfiledBookmarksFolderId); michael@0: do_check_eq(livemark.index, PlacesUtils.bookmarks.getItemIndex(livemark.id)); michael@0: do_check_eq(livemark.lastModified, PlacesUtils.bookmarks.getItemLastModified(livemark.id)); michael@0: do_check_true(livemark.feedURI.equals(FEED_URI)); michael@0: do_check_true(livemark.siteURI.equals(SITE_URI)); michael@0: do_check_true(PlacesUtils.annotations michael@0: .itemHasAnnotation(livemark.id, michael@0: PlacesUtils.LMANNO_FEEDURI)); michael@0: do_check_true(PlacesUtils.annotations michael@0: .itemHasAnnotation(livemark.id, michael@0: PlacesUtils.LMANNO_SITEURI)); michael@0: }); michael@0: michael@0: add_task(function test_addLivemark_bogusid_succeeds() michael@0: { michael@0: let livemark = yield PlacesUtils.livemarks.addLivemark( michael@0: { id: 100 // Should be ignored. michael@0: , title: "test" michael@0: , parentId: PlacesUtils.unfiledBookmarksFolderId michael@0: , index: PlacesUtils.bookmarks.DEFAULT_INDEX michael@0: , feedURI: FEED_URI michael@0: , siteURI: SITE_URI michael@0: }); michael@0: do_check_true(livemark.id > 0); michael@0: do_check_neq(livemark.id, 100); michael@0: }); michael@0: michael@0: add_task(function test_addLivemark_bogusParent_fails() michael@0: { michael@0: try { michael@0: yield PlacesUtils.livemarks.addLivemark( michael@0: { title: "test" michael@0: , parentId: 187 michael@0: , index: PlacesUtils.bookmarks.DEFAULT_INDEX michael@0: , feedURI: FEED_URI michael@0: }); michael@0: do_throw("Adding a livemark with a bogus parent should fail"); michael@0: } catch(ex) {} michael@0: }); michael@0: michael@0: add_task(function test_addLivemark_intoLivemark_fails() michael@0: { michael@0: let livemark = yield PlacesUtils.livemarks.addLivemark( michael@0: { title: "test" michael@0: , parentId: PlacesUtils.unfiledBookmarksFolderId michael@0: , index: PlacesUtils.bookmarks.DEFAULT_INDEX michael@0: , feedURI: FEED_URI michael@0: }); michael@0: do_check_true(Boolean(livemark)); michael@0: michael@0: try { michael@0: yield PlacesUtils.livemarks.addLivemark( michael@0: { title: "test" michael@0: , parentId: livemark.id michael@0: , index: PlacesUtils.bookmarks.DEFAULT_INDEX michael@0: , feedURI: FEED_URI michael@0: }); michael@0: do_throw("Adding a livemark into a livemark should fail"); michael@0: } catch(ex) {} michael@0: }); michael@0: michael@0: add_task(function test_addLivemark_forceGuid_succeeds() michael@0: { michael@0: let checkLivemark = aLivemark => { michael@0: do_check_eq(aLivemark.guid, "1234567890AB"); michael@0: do_check_guid_for_bookmark(aLivemark.id, "1234567890AB"); michael@0: }; michael@0: michael@0: let livemark = yield PlacesUtils.livemarks.addLivemark( michael@0: { title: "test" michael@0: , parentId: PlacesUtils.unfiledBookmarksFolderId michael@0: , index: PlacesUtils.bookmarks.DEFAULT_INDEX michael@0: , feedURI: FEED_URI michael@0: , guid: "1234567890AB" michael@0: }); michael@0: checkLivemark(livemark); michael@0: }); michael@0: michael@0: add_task(function test_addLivemark_lastModified_succeeds() michael@0: { michael@0: let now = Date.now() * 1000; michael@0: let livemark = yield PlacesUtils.livemarks.addLivemark( michael@0: { title: "test" michael@0: , parentId: PlacesUtils.unfiledBookmarksFolderId michael@0: , index: PlacesUtils.bookmarks.DEFAULT_INDEX michael@0: , feedURI: FEED_URI michael@0: , lastModified: now michael@0: }); michael@0: do_check_eq(livemark.lastModified, now); michael@0: }); michael@0: michael@0: add_task(function test_removeLivemark_emptyObject_throws() michael@0: { michael@0: try { michael@0: yield PlacesUtils.livemarks.removeLivemark({}); michael@0: do_throw("Invoking removeLivemark with empty object should throw"); michael@0: } catch (ex) { michael@0: do_check_eq(ex.result, Cr.NS_ERROR_INVALID_ARG); michael@0: } michael@0: }); michael@0: michael@0: add_task(function test_removeLivemark_noValidId_throws() michael@0: { michael@0: try { michael@0: yield PlacesUtils.livemarks.removeLivemark({ id: -10, guid: "test"}); michael@0: do_throw("Invoking removeLivemark with no valid id should throw"); michael@0: } catch (ex) { michael@0: do_check_eq(ex.result, Cr.NS_ERROR_INVALID_ARG); michael@0: } michael@0: }); michael@0: michael@0: add_task(function test_removeLivemark_nonExistent_fails() michael@0: { michael@0: try { michael@0: yield PlacesUtils.livemarks.removeLivemark({ id: 1337 }); michael@0: do_throw("Removing a non-existent livemark should fail"); michael@0: } michael@0: catch(ex) { michael@0: } michael@0: }); michael@0: michael@0: add_task(function test_removeLivemark_guid_succeeds() michael@0: { michael@0: let livemark = yield PlacesUtils.livemarks.addLivemark( michael@0: { title: "test" michael@0: , parentId: PlacesUtils.unfiledBookmarksFolderId michael@0: , index: PlacesUtils.bookmarks.DEFAULT_INDEX michael@0: , feedURI: FEED_URI michael@0: , guid: "234567890ABC" michael@0: }); michael@0: michael@0: michael@0: do_check_eq(livemark.guid, "234567890ABC"); michael@0: michael@0: yield PlacesUtils.livemarks.removeLivemark({ michael@0: id: 789, guid: "234567890ABC" michael@0: }); michael@0: michael@0: do_check_eq(PlacesUtils.bookmarks.getItemIndex(livemark.id), -1); michael@0: }); michael@0: michael@0: add_task(function test_removeLivemark_id_succeeds() michael@0: { michael@0: let livemark = yield PlacesUtils.livemarks.addLivemark( michael@0: { title: "test" michael@0: , parentId: PlacesUtils.unfiledBookmarksFolderId michael@0: , index: PlacesUtils.bookmarks.DEFAULT_INDEX michael@0: , feedURI: FEED_URI michael@0: }); michael@0: michael@0: yield PlacesUtils.livemarks.removeLivemark({ id: livemark.id }); michael@0: do_check_eq(PlacesUtils.bookmarks.getItemIndex(livemark.id), -1); michael@0: }); michael@0: michael@0: add_task(function test_getLivemark_emptyObject_throws() michael@0: { michael@0: try { michael@0: yield PlacesUtils.livemarks.getLivemark({}); michael@0: do_throw("Invoking getLivemark with empty object should throw"); michael@0: } catch (ex) { michael@0: do_check_eq(ex.result, Cr.NS_ERROR_INVALID_ARG); michael@0: } michael@0: }); michael@0: michael@0: add_task(function test_getLivemark_noValidId_throws() michael@0: { michael@0: try { michael@0: yield PlacesUtils.livemarks.getLivemark({ id: -10, guid: "test"}); michael@0: do_throw("Invoking getLivemark with no valid id should throw"); michael@0: } catch (ex) { michael@0: do_check_eq(ex.result, Cr.NS_ERROR_INVALID_ARG); michael@0: } michael@0: }); michael@0: michael@0: add_task(function test_getLivemark_nonExistentId_fails() michael@0: { michael@0: try { michael@0: yield PlacesUtils.livemarks.getLivemark({ id: 1234 }); michael@0: do_throw("getLivemark for a non existent id should fail"); michael@0: } michael@0: catch(ex) {} michael@0: }); michael@0: michael@0: add_task(function test_getLivemark_nonExistentGUID_fails() michael@0: { michael@0: try { michael@0: yield PlacesUtils.livemarks.getLivemark({ guid: "34567890ABCD" }); michael@0: do_throw("getLivemark for a non-existent guid should fail"); michael@0: } michael@0: catch(ex) {} michael@0: }); michael@0: michael@0: add_task(function test_getLivemark_guid_succeeds() michael@0: { michael@0: yield PlacesUtils.livemarks.addLivemark( michael@0: { title: "test" michael@0: , parentId: PlacesUtils.unfiledBookmarksFolderId michael@0: , index: PlacesUtils.bookmarks.DEFAULT_INDEX michael@0: , feedURI: FEED_URI michael@0: , guid: "34567890ABCD" }); michael@0: michael@0: // invalid id to check the guid wins. michael@0: let livemark = michael@0: yield PlacesUtils.livemarks.getLivemark({ id: 789, guid: "34567890ABCD" }); michael@0: michael@0: do_check_eq(livemark.title, "test"); michael@0: do_check_eq(livemark.parentId, PlacesUtils.unfiledBookmarksFolderId); michael@0: do_check_eq(livemark.index, PlacesUtils.bookmarks.getItemIndex(livemark.id)); michael@0: do_check_true(livemark.feedURI.equals(FEED_URI)); michael@0: do_check_eq(livemark.siteURI, null); michael@0: do_check_eq(livemark.guid, "34567890ABCD"); michael@0: }); michael@0: michael@0: add_task(function test_getLivemark_id_succeeds() michael@0: { michael@0: let livemark = yield PlacesUtils.livemarks.addLivemark( michael@0: { title: "test" michael@0: , parentId: PlacesUtils.unfiledBookmarksFolderId michael@0: , index: PlacesUtils.bookmarks.DEFAULT_INDEX michael@0: , feedURI: FEED_URI michael@0: }); michael@0: michael@0: livemark = yield PlacesUtils.livemarks.getLivemark({ id: livemark.id }); michael@0: michael@0: do_check_eq(livemark.title, "test"); michael@0: do_check_eq(livemark.parentId, PlacesUtils.unfiledBookmarksFolderId); michael@0: do_check_eq(livemark.index, PlacesUtils.bookmarks.getItemIndex(livemark.id)); michael@0: do_check_true(livemark.feedURI.equals(FEED_URI)); michael@0: do_check_eq(livemark.siteURI, null); michael@0: do_check_guid_for_bookmark(livemark.id, livemark.guid); michael@0: }); michael@0: michael@0: add_task(function test_getLivemark_removeItem_contention() michael@0: { michael@0: PlacesUtils.livemarks.addLivemark({ title: "test" michael@0: , parentId: PlacesUtils.unfiledBookmarksFolderId michael@0: , index: PlacesUtils.bookmarks.DEFAULT_INDEX michael@0: , feedURI: FEED_URI michael@0: }); michael@0: PlacesUtils.bookmarks.removeFolderChildren(PlacesUtils.unfiledBookmarksFolderId); michael@0: PlacesUtils.livemarks.addLivemark({ title: "test" michael@0: , parentId: PlacesUtils.unfiledBookmarksFolderId michael@0: , index: PlacesUtils.bookmarks.DEFAULT_INDEX michael@0: , feedURI: FEED_URI michael@0: }); michael@0: let id = PlacesUtils.bookmarks.getIdForItemAt(PlacesUtils.unfiledBookmarksFolderId, michael@0: PlacesUtils.bookmarks.DEFAULT_INDEX); michael@0: michael@0: let livemark = yield PlacesUtils.livemarks.getLivemark({ id: id }); michael@0: michael@0: do_check_eq(livemark.title, "test"); michael@0: do_check_eq(livemark.parentId, PlacesUtils.unfiledBookmarksFolderId); michael@0: do_check_eq(livemark.index, PlacesUtils.bookmarks.getItemIndex(livemark.id)); michael@0: do_check_true(livemark.feedURI.equals(FEED_URI)); michael@0: do_check_eq(livemark.siteURI, null); michael@0: do_check_guid_for_bookmark(livemark.id, livemark.guid); michael@0: }); michael@0: michael@0: add_task(function test_title_change() michael@0: { michael@0: let livemark = yield PlacesUtils.livemarks.addLivemark( michael@0: { title: "test" michael@0: , parentId: PlacesUtils.unfiledBookmarksFolderId michael@0: , index: PlacesUtils.bookmarks.DEFAULT_INDEX michael@0: , feedURI: FEED_URI }); michael@0: michael@0: PlacesUtils.bookmarks.setItemTitle(livemark.id, "test2"); michael@0: do_check_eq(livemark.title, "test2"); michael@0: }); michael@0: michael@0: add_task(function test_livemark_move() michael@0: { michael@0: let livemark = yield PlacesUtils.livemarks.addLivemark( michael@0: { title: "test" michael@0: , parentId: PlacesUtils.unfiledBookmarksFolderId michael@0: , index: PlacesUtils.bookmarks.DEFAULT_INDEX michael@0: , feedURI: FEED_URI } ); michael@0: michael@0: PlacesUtils.bookmarks.moveItem(livemark.id, michael@0: PlacesUtils.toolbarFolderId, michael@0: PlacesUtils.bookmarks.DEFAULT_INDEX); michael@0: do_check_eq(livemark.parentId, PlacesUtils.toolbarFolderId); michael@0: do_check_eq(livemark.index, PlacesUtils.bookmarks.getItemIndex(livemark.id)); michael@0: }); michael@0: michael@0: function run_test() { michael@0: run_next_test(); michael@0: }