michael@0: /* Any copyright is dedicated to the Public Domain. michael@0: http://creativecommons.org/publicdomain/zero/1.0/ */ michael@0: michael@0: function check_bookmark_keyword(aItemId, aKeyword) michael@0: { michael@0: let keyword = aKeyword ? aKeyword.toLowerCase() : null; michael@0: do_check_eq(PlacesUtils.bookmarks.getKeywordForBookmark(aItemId), michael@0: keyword); michael@0: } michael@0: michael@0: function check_uri_keyword(aURI, aKeyword) michael@0: { michael@0: let keyword = aKeyword ? aKeyword.toLowerCase() : null; michael@0: do_check_eq(PlacesUtils.bookmarks.getKeywordForURI(aURI), michael@0: keyword); michael@0: michael@0: if (aKeyword) { michael@0: // This API can't tell which uri the user wants, so it returns a random one. michael@0: let re = /http:\/\/test[0-9]\.mozilla\.org/; michael@0: let url = PlacesUtils.bookmarks.getURIForKeyword(aKeyword).spec; michael@0: do_check_true(re.test(url)); michael@0: // Check case insensitivity. michael@0: url = PlacesUtils.bookmarks.getURIForKeyword(aKeyword.toUpperCase()).spec michael@0: do_check_true(re.test(url)); michael@0: } michael@0: } michael@0: michael@0: function check_orphans() michael@0: { michael@0: stmt = DBConn().createStatement( michael@0: "SELECT id FROM moz_keywords k WHERE NOT EXISTS (" michael@0: + "SELECT id FROM moz_bookmarks WHERE keyword_id = k.id " michael@0: + ")" michael@0: ); michael@0: try { michael@0: do_check_false(stmt.executeStep()); michael@0: } finally { michael@0: stmt.finalize(); michael@0: } michael@0: michael@0: print("Check there are no orphan database entries"); michael@0: let stmt = DBConn().createStatement( michael@0: "SELECT b.id FROM moz_bookmarks b " michael@0: + "LEFT JOIN moz_keywords k ON b.keyword_id = k.id " michael@0: + "WHERE keyword_id NOTNULL AND k.id ISNULL" michael@0: ); michael@0: try { michael@0: do_check_false(stmt.executeStep()); michael@0: } finally { michael@0: stmt.finalize(); michael@0: } michael@0: } michael@0: michael@0: const URIS = [ michael@0: uri("http://test1.mozilla.org/"), michael@0: uri("http://test2.mozilla.org/"), michael@0: ]; michael@0: michael@0: add_test(function test_addBookmarkWithKeyword() michael@0: { michael@0: check_uri_keyword(URIS[0], null); michael@0: michael@0: let itemId = michael@0: PlacesUtils.bookmarks.insertBookmark(PlacesUtils.unfiledBookmarksFolderId, michael@0: URIS[0], michael@0: PlacesUtils.bookmarks.DEFAULT_INDEX, michael@0: "test"); michael@0: PlacesUtils.bookmarks.setKeywordForBookmark(itemId, "keyword"); michael@0: check_bookmark_keyword(itemId, "keyword"); michael@0: check_uri_keyword(URIS[0], "keyword"); michael@0: michael@0: promiseAsyncUpdates().then(function() { michael@0: check_orphans(); michael@0: run_next_test(); michael@0: }); michael@0: }); michael@0: michael@0: add_test(function test_addBookmarkToURIHavingKeyword() michael@0: { michael@0: let itemId = michael@0: PlacesUtils.bookmarks.insertBookmark(PlacesUtils.unfiledBookmarksFolderId, michael@0: URIS[0], michael@0: PlacesUtils.bookmarks.DEFAULT_INDEX, michael@0: "test"); michael@0: // The uri has a keyword, but this specific bookmark has not. michael@0: check_bookmark_keyword(itemId, null); michael@0: check_uri_keyword(URIS[0], "keyword"); michael@0: michael@0: promiseAsyncUpdates().then(function() { michael@0: check_orphans(); michael@0: run_next_test(); michael@0: }); michael@0: }); michael@0: michael@0: add_test(function test_addSameKeywordToOtherURI() michael@0: { michael@0: let itemId = michael@0: PlacesUtils.bookmarks.insertBookmark(PlacesUtils.unfiledBookmarksFolderId, michael@0: URIS[1], michael@0: PlacesUtils.bookmarks.DEFAULT_INDEX, michael@0: "test2"); michael@0: check_bookmark_keyword(itemId, null); michael@0: check_uri_keyword(URIS[1], null); michael@0: michael@0: PlacesUtils.bookmarks.setKeywordForBookmark(itemId, "kEyWoRd"); michael@0: check_bookmark_keyword(itemId, "kEyWoRd"); michael@0: check_uri_keyword(URIS[1], "kEyWoRd"); michael@0: michael@0: // Check case insensitivity. michael@0: check_uri_keyword(URIS[0], "kEyWoRd"); michael@0: check_bookmark_keyword(itemId, "keyword"); michael@0: check_uri_keyword(URIS[1], "keyword"); michael@0: check_uri_keyword(URIS[0], "keyword"); michael@0: michael@0: promiseAsyncUpdates().then(function() { michael@0: check_orphans(); michael@0: run_next_test(); michael@0: }); michael@0: }); michael@0: michael@0: add_test(function test_removeBookmarkWithKeyword() michael@0: { michael@0: let itemId = michael@0: PlacesUtils.bookmarks.insertBookmark(PlacesUtils.unfiledBookmarksFolderId, michael@0: URIS[1], michael@0: PlacesUtils.bookmarks.DEFAULT_INDEX, michael@0: "test"); michael@0: PlacesUtils.bookmarks.setKeywordForBookmark(itemId, "keyword"); michael@0: check_bookmark_keyword(itemId, "keyword"); michael@0: check_uri_keyword(URIS[1], "keyword"); michael@0: michael@0: // The keyword should not be removed from other bookmarks. michael@0: PlacesUtils.bookmarks.removeItem(itemId); michael@0: michael@0: check_uri_keyword(URIS[1], "keyword"); michael@0: check_uri_keyword(URIS[0], "keyword"); michael@0: michael@0: promiseAsyncUpdates().then(function() { michael@0: check_orphans(); michael@0: run_next_test(); michael@0: }); michael@0: }); michael@0: michael@0: add_test(function test_removeFolderWithKeywordedBookmarks() michael@0: { michael@0: // Keyword should be removed as well. michael@0: PlacesUtils.bookmarks.removeFolderChildren(PlacesUtils.unfiledBookmarksFolderId); michael@0: michael@0: check_uri_keyword(URIS[1], null); michael@0: check_uri_keyword(URIS[0], null); michael@0: michael@0: promiseAsyncUpdates().then(function() { michael@0: check_orphans(); michael@0: run_next_test(); michael@0: }); michael@0: }); michael@0: michael@0: function run_test() michael@0: { michael@0: run_next_test(); michael@0: }