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: // Get bookmarks service michael@0: try { michael@0: var bmsvc = Cc["@mozilla.org/browser/nav-bookmarks-service;1"]. michael@0: getService(Ci.nsINavBookmarksService); michael@0: } michael@0: catch(ex) { michael@0: do_throw("Could not get bookmarks service\n"); michael@0: } michael@0: michael@0: // Get database connection michael@0: try { michael@0: var histsvc = Cc["@mozilla.org/browser/nav-history-service;1"]. michael@0: getService(Ci.nsINavHistoryService); michael@0: var mDBConn = histsvc.QueryInterface(Ci.nsPIPlacesDatabase).DBConnection; michael@0: } michael@0: catch(ex) { michael@0: do_throw("Could not get database connection\n"); michael@0: } michael@0: michael@0: add_test(function test_keywordRemovedOnUniqueItemRemoval() { michael@0: var bookmarkedURI = uri("http://foo.bar"); michael@0: var keyword = "testkeyword"; michael@0: michael@0: // TEST 1 michael@0: // 1. add a bookmark michael@0: // 2. add a keyword to it michael@0: // 3. remove bookmark michael@0: // 4. check that keyword has gone michael@0: var bookmarkId = bmsvc.insertBookmark(bmsvc.bookmarksMenuFolder, michael@0: bookmarkedURI, michael@0: bmsvc.DEFAULT_INDEX, michael@0: "A bookmark"); michael@0: bmsvc.setKeywordForBookmark(bookmarkId, keyword); michael@0: // remove bookmark michael@0: bmsvc.removeItem(bookmarkId); michael@0: michael@0: promiseAsyncUpdates().then(function() { michael@0: // Check that keyword has been removed from the database. michael@0: // The removal is asynchronous. michael@0: var sql = "SELECT id FROM moz_keywords WHERE keyword = ?1"; michael@0: var stmt = mDBConn.createStatement(sql); michael@0: stmt.bindByIndex(0, keyword); michael@0: do_check_false(stmt.executeStep()); michael@0: stmt.finalize(); michael@0: michael@0: run_next_test(); michael@0: }); michael@0: }); michael@0: michael@0: add_test(function test_keywordNotRemovedOnNonUniqueItemRemoval() { michael@0: var bookmarkedURI = uri("http://foo.bar"); michael@0: var keyword = "testkeyword"; michael@0: michael@0: // TEST 2 michael@0: // 1. add 2 bookmarks michael@0: // 2. add the same keyword to them michael@0: // 3. remove first bookmark michael@0: // 4. check that keyword is still there michael@0: var bookmarkId1 = bmsvc.insertBookmark(bmsvc.bookmarksMenuFolder, michael@0: bookmarkedURI, michael@0: bmsvc.DEFAULT_INDEX, michael@0: "A bookmark"); michael@0: bmsvc.setKeywordForBookmark(bookmarkId1, keyword); michael@0: michael@0: var bookmarkId2 = bmsvc.insertBookmark(bmsvc.toolbarFolder, michael@0: bookmarkedURI, michael@0: bmsvc.DEFAULT_INDEX, michael@0: keyword); michael@0: bmsvc.setKeywordForBookmark(bookmarkId2, keyword); michael@0: michael@0: // remove first bookmark michael@0: bmsvc.removeItem(bookmarkId1); michael@0: michael@0: promiseAsyncUpdates().then(function() { michael@0: // check that keyword is still there michael@0: var sql = "SELECT id FROM moz_keywords WHERE keyword = ?1"; michael@0: var stmt = mDBConn.createStatement(sql); michael@0: stmt.bindByIndex(0, keyword); michael@0: do_check_true(stmt.executeStep()); michael@0: stmt.finalize(); michael@0: michael@0: run_next_test(); michael@0: }); michael@0: }); michael@0: michael@0: function run_test() { michael@0: run_next_test(); michael@0: }