michael@0: /* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* vim:set ts=2 sw=2 sts=2 et: */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: const bmsvc = PlacesUtils.bookmarks; michael@0: const histsvc = PlacesUtils.history; michael@0: michael@0: function run_test() { michael@0: run_next_test(); michael@0: } michael@0: michael@0: add_task(function test_addBookmarksAndCheckGuids() { michael@0: let folder = bmsvc.createFolder(bmsvc.placesRoot, "test folder", bmsvc.DEFAULT_INDEX); michael@0: let b1 = bmsvc.insertBookmark(folder, uri("http://test1.com/"), michael@0: bmsvc.DEFAULT_INDEX, "1 title"); michael@0: let b2 = bmsvc.insertBookmark(folder, uri("http://test2.com/"), michael@0: bmsvc.DEFAULT_INDEX, "2 title"); michael@0: let b3 = bmsvc.insertBookmark(folder, uri("http://test3.com/"), michael@0: bmsvc.DEFAULT_INDEX, "3 title"); michael@0: let s1 = bmsvc.insertSeparator(folder, bmsvc.DEFAULT_INDEX); michael@0: let f1 = bmsvc.createFolder(folder, "test folder 2", bmsvc.DEFAULT_INDEX); michael@0: michael@0: let root = PlacesUtils.getFolderContents(folder).root; michael@0: do_check_eq(root.childCount, 5); michael@0: michael@0: // check bookmark guids michael@0: let bookmarkGuidZero = root.getChild(0).bookmarkGuid; michael@0: do_check_eq(bookmarkGuidZero.length, 12); michael@0: // bookmarks have bookmark guids michael@0: do_check_eq(root.getChild(1).bookmarkGuid.length, 12); michael@0: do_check_eq(root.getChild(2).bookmarkGuid.length, 12); michael@0: // separator has bookmark guid michael@0: do_check_eq(root.getChild(3).bookmarkGuid.length, 12); michael@0: // folder has bookmark guid michael@0: do_check_eq(root.getChild(4).bookmarkGuid.length, 12); michael@0: // all bookmark guids are different. michael@0: do_check_neq(bookmarkGuidZero, root.getChild(1).bookmarkGuid); michael@0: do_check_neq(root.getChild(1).bookmarkGuid, root.getChild(2).bookmarkGuid); michael@0: do_check_neq(root.getChild(2).bookmarkGuid, root.getChild(3).bookmarkGuid); michael@0: do_check_neq(root.getChild(3).bookmarkGuid, root.getChild(4).bookmarkGuid); michael@0: michael@0: // check page guids michael@0: let pageGuidZero = root.getChild(0).pageGuid; michael@0: do_check_eq(pageGuidZero.length, 12); michael@0: // bookmarks have page guids michael@0: do_check_eq(root.getChild(1).pageGuid.length, 12); michael@0: do_check_eq(root.getChild(2).pageGuid.length, 12); michael@0: // folder and separator don't have page guids michael@0: do_check_eq(root.getChild(3).pageGuid, ""); michael@0: do_check_eq(root.getChild(4).pageGuid, ""); michael@0: michael@0: do_check_neq(pageGuidZero, root.getChild(1).pageGuid); michael@0: do_check_neq(root.getChild(1).pageGuid, root.getChild(2).pageGuid); michael@0: michael@0: root.containerOpen = false; michael@0: michael@0: remove_all_bookmarks(); michael@0: }); michael@0: michael@0: add_task(function test_updateBookmarksAndCheckGuids() { michael@0: let folder = bmsvc.createFolder(bmsvc.placesRoot, "test folder", bmsvc.DEFAULT_INDEX); michael@0: let b1 = bmsvc.insertBookmark(folder, uri("http://test1.com/"), michael@0: bmsvc.DEFAULT_INDEX, "1 title"); michael@0: let f1 = bmsvc.createFolder(folder, "test folder 2", bmsvc.DEFAULT_INDEX); michael@0: michael@0: let root = PlacesUtils.getFolderContents(folder).root; michael@0: do_check_eq(root.childCount, 2); michael@0: michael@0: // ensure the bookmark and page guids remain the same after modifing other property. michael@0: let bookmarkGuidZero = root.getChild(0).bookmarkGuid; michael@0: let pageGuidZero = root.getChild(0).pageGuid; michael@0: bmsvc.setItemTitle(b1, "1 title mod"); michael@0: do_check_eq(root.getChild(0).title, "1 title mod"); michael@0: do_check_eq(root.getChild(0).bookmarkGuid, bookmarkGuidZero); michael@0: do_check_eq(root.getChild(0).pageGuid, pageGuidZero); michael@0: michael@0: let bookmarkGuidOne = root.getChild(1).bookmarkGuid; michael@0: let pageGuidOne = root.getChild(1).pageGuid; michael@0: bmsvc.setItemTitle(f1, "test foolder 234"); michael@0: do_check_eq(root.getChild(1).title, "test foolder 234"); michael@0: do_check_eq(root.getChild(1).bookmarkGuid, bookmarkGuidOne); michael@0: do_check_eq(root.getChild(1).pageGuid, pageGuidOne); michael@0: michael@0: root.containerOpen = false; michael@0: michael@0: remove_all_bookmarks(); michael@0: }); michael@0: michael@0: add_task(function test_addVisitAndCheckGuid() { michael@0: // add a visit and test page guid and non-existing bookmark guids. michael@0: let now = Date.now() * 1000; michael@0: let sourceURI = uri("http://test4.com/"); michael@0: yield promiseAddVisits({ uri: sourceURI }); michael@0: do_check_eq(bmsvc.getBookmarkedURIFor(sourceURI), null); michael@0: michael@0: let options = histsvc.getNewQueryOptions(); michael@0: let query = histsvc.getNewQuery(); michael@0: query.uri = sourceURI; michael@0: let root = histsvc.executeQuery(query, options).root; michael@0: root.containerOpen = true; michael@0: do_check_eq(root.childCount, 1); michael@0: michael@0: do_check_valid_places_guid(root.getChild(0).pageGuid); michael@0: do_check_eq(root.getChild(0).bookmarkGuid, ""); michael@0: root.containerOpen = false; michael@0: michael@0: yield promiseClearHistory(); michael@0: }); michael@0: michael@0: add_task(function test_addItemsWithInvalidGUIDsFails() { michael@0: const INVALID_GUID = "XYZ"; michael@0: try { michael@0: bmsvc.createFolder(bmsvc.placesRoot, "XYZ folder", michael@0: bmsvc.DEFAULT_INDEX, INVALID_GUID); michael@0: do_throw("Adding a folder with an invalid guid should fail"); michael@0: } michael@0: catch(ex) { } michael@0: michael@0: let folder = bmsvc.createFolder(bmsvc.placesRoot, "test folder", michael@0: bmsvc.DEFAULT_INDEX); michael@0: try { michael@0: bmsvc.insertBookmark(folder, uri("http://test.tld"), bmsvc.DEFAULT_INDEX, michael@0: "title", INVALID_GUID); michael@0: do_throw("Adding a bookmark with an invalid guid should fail"); michael@0: } michael@0: catch(ex) { } michael@0: michael@0: try { michael@0: bmsvc.insertSeparator(folder, bmsvc.DEFAULT_INDEX, INVALID_GUID); michael@0: do_throw("Adding a separator with an invalid guid should fail"); michael@0: } michael@0: catch(ex) { } michael@0: michael@0: remove_all_bookmarks(); michael@0: }); michael@0: michael@0: add_task(function test_addItemsWithGUIDs() { michael@0: const FOLDER_GUID = "FOLDER--GUID"; michael@0: const BOOKMARK_GUID = "BM------GUID"; michael@0: const SEPARATOR_GUID = "SEP-----GUID"; michael@0: michael@0: let folder = bmsvc.createFolder(bmsvc.placesRoot, "test folder", michael@0: bmsvc.DEFAULT_INDEX, FOLDER_GUID); michael@0: bmsvc.insertBookmark(folder, uri("http://test1.com/"), bmsvc.DEFAULT_INDEX, michael@0: "1 title", BOOKMARK_GUID); michael@0: bmsvc.insertSeparator(folder, bmsvc.DEFAULT_INDEX, SEPARATOR_GUID); michael@0: michael@0: let root = PlacesUtils.getFolderContents(folder).root; michael@0: do_check_eq(root.childCount, 2); michael@0: do_check_eq(root.bookmarkGuid, FOLDER_GUID); michael@0: do_check_eq(root.getChild(0).bookmarkGuid, BOOKMARK_GUID); michael@0: do_check_eq(root.getChild(1).bookmarkGuid, SEPARATOR_GUID); michael@0: michael@0: root.containerOpen = false; michael@0: remove_all_bookmarks(); michael@0: }); michael@0: michael@0: add_task(function test_emptyGUIDIgnored() { michael@0: let folder = bmsvc.createFolder(bmsvc.placesRoot, "test folder", michael@0: bmsvc.DEFAULT_INDEX, ""); michael@0: do_check_valid_places_guid(PlacesUtils.getFolderContents(folder) michael@0: .root.bookmarkGuid); michael@0: remove_all_bookmarks(); michael@0: }); michael@0: michael@0: add_task(function test_usingSameGUIDFails() { michael@0: const GUID = "XYZXYZXYZXYZ"; michael@0: bmsvc.createFolder(bmsvc.placesRoot, "test folder", michael@0: bmsvc.DEFAULT_INDEX, GUID); michael@0: try { michael@0: bmsvc.createFolder(bmsvc.placesRoot, "test folder 2", michael@0: bmsvc.DEFAULT_INDEX, GUID); michael@0: do_throw("Using the same guid twice should fail"); michael@0: } michael@0: catch(ex) { } michael@0: michael@0: remove_all_bookmarks(); michael@0: });