diff --git a/browser/components/places/tests/unit/test_browserGlue_smartBookmarks.js b/browser/components/places/tests/unit/test_browserGlue_smartBookmarks.js index c1e65d102857..d9ab4acf12b8 100644 --- a/browser/components/places/tests/unit/test_browserGlue_smartBookmarks.js +++ b/browser/components/places/tests/unit/test_browserGlue_smartBookmarks.js @@ -41,16 +41,16 @@ * by the user or by other components. */ -// Initialize browserGlue. -var bg = Cc["@mozilla.org/browser/browserglue;1"]. - getService(Ci.nsIBrowserGlue); - // Initialize Places. var hs = Cc["@mozilla.org/browser/nav-history-service;1"]. getService(Ci.nsINavHistoryService); var bs = Cc["@mozilla.org/browser/nav-bookmarks-service;1"]. getService(Ci.nsINavBookmarksService); +// Initialize browserGlue after Places. +var bg = Cc["@mozilla.org/browser/browserglue;1"]. + getService(Ci.nsIBrowserGlue); + // Get other services. var ps = Cc["@mozilla.org/preferences-service;1"]. getService(Ci.nsIPrefBranch); @@ -63,6 +63,7 @@ const PREF_SMART_BOOKMARKS_VERSION = "browser.places.smartBookmarksVersion"; const SMART_BOOKMARKS_ANNO = "Places/SmartBookmark"; const TOPIC_PLACES_INIT_COMPLETE = "places-init-complete"; +const TOPIC_PLACES_DATABASE_LOCKED = "places-database-locked"; var tests = []; @@ -71,23 +72,28 @@ var tests = []; tests.push({ description: "All smart bookmarks are created if smart bookmarks version is 0.", exec: function() { - // Sanity check: we should not have any bookmark on the toolbar. + // Sanity check: we should not have any bookmark. do_check_eq(bs.getIdForItemAt(bs.toolbarFolder, 0), -1); - // Sanity check: we should not have any bookmark on the menu. do_check_eq(bs.getIdForItemAt(bs.bookmarksMenuFolder, 0), -1); // Set preferences. ps.setIntPref(PREF_SMART_BOOKMARKS_VERSION, 0); - // Force nsBrowserGlue::_initPlaces(). - os.notifyObservers(null, TOPIC_PLACES_INIT_COMPLETE, null); - // Count items on toolbar. - do_check_eq(countFolderChildren(bs.toolbarFolder), SMART_BOOKMARKS_ON_TOOLBAR + DEFAULT_BOOKMARKS_ON_TOOLBAR); - // Count items on menu (+1 for the separator). - do_check_eq(countFolderChildren(bs.bookmarksMenuFolder), SMART_BOOKMARKS_ON_MENU + DEFAULT_BOOKMARKS_ON_MENU); + // Force nsBrowserGlue::_initPlaces(). + print("Simulate Places init"); + bg.QueryInterface(Ci.nsIObserver).observe(null, + TOPIC_PLACES_INIT_COMPLETE, + null); + + // Count items. + do_check_eq(countFolderChildren(bs.toolbarFolder), + SMART_BOOKMARKS_ON_TOOLBAR + DEFAULT_BOOKMARKS_ON_TOOLBAR); + do_check_eq(countFolderChildren(bs.bookmarksMenuFolder), + SMART_BOOKMARKS_ON_MENU + DEFAULT_BOOKMARKS_ON_MENU); // Check version has been updated. - do_check_eq(ps.getIntPref(PREF_SMART_BOOKMARKS_VERSION), SMART_BOOKMARKS_VERSION); + do_check_eq(ps.getIntPref(PREF_SMART_BOOKMARKS_VERSION), + SMART_BOOKMARKS_VERSION); next_test(); } @@ -102,29 +108,42 @@ tests.push({ var itemId = bs.getIdForItemAt(bs.toolbarFolder, 0); do_check_neq(itemId, -1); do_check_true(as.itemHasAnnotation(itemId, SMART_BOOKMARKS_ANNO)); - // Change its title. bs.setItemTitle(itemId, "new title"); do_check_eq(bs.getItemTitle(itemId), "new title"); + // Sanity check items. + dump_table("moz_bookmarks"); + dump_table("moz_items_annos"); + do_check_eq(countFolderChildren(bs.toolbarFolder), + SMART_BOOKMARKS_ON_TOOLBAR + DEFAULT_BOOKMARKS_ON_TOOLBAR); + do_check_eq(countFolderChildren(bs.bookmarksMenuFolder), + SMART_BOOKMARKS_ON_MENU + DEFAULT_BOOKMARKS_ON_MENU); + // Set preferences. ps.setIntPref(PREF_SMART_BOOKMARKS_VERSION, 1); + // Force nsBrowserGlue::_initPlaces(). - os.notifyObservers(null, TOPIC_PLACES_INIT_COMPLETE, null); + print("Simulate Places init"); + bg.QueryInterface(Ci.nsIObserver).observe(null, + TOPIC_PLACES_INIT_COMPLETE, + null); - // Count items on toolbar. - do_check_eq(countFolderChildren(bs.toolbarFolder), SMART_BOOKMARKS_ON_TOOLBAR + DEFAULT_BOOKMARKS_ON_TOOLBAR); - // Count items on menu (+1 for the separator). - do_check_eq(countFolderChildren(bs.bookmarksMenuFolder), SMART_BOOKMARKS_ON_MENU + DEFAULT_BOOKMARKS_ON_MENU); + // Count items. + do_check_eq(countFolderChildren(bs.toolbarFolder), + SMART_BOOKMARKS_ON_TOOLBAR + DEFAULT_BOOKMARKS_ON_TOOLBAR); + do_check_eq(countFolderChildren(bs.bookmarksMenuFolder), + SMART_BOOKMARKS_ON_MENU + DEFAULT_BOOKMARKS_ON_MENU); - // check smart bookmark has been replaced, itemId has changed. + // Check smart bookmark has been replaced, itemId has changed. itemId = bs.getIdForItemAt(bs.toolbarFolder, 0); do_check_neq(itemId, -1); do_check_neq(bs.getItemTitle(itemId), "new title"); do_check_true(as.itemHasAnnotation(itemId, SMART_BOOKMARKS_ANNO)); // Check version has been updated. - do_check_eq(ps.getIntPref(PREF_SMART_BOOKMARKS_VERSION), SMART_BOOKMARKS_VERSION); + do_check_eq(ps.getIntPref(PREF_SMART_BOOKMARKS_VERSION), + SMART_BOOKMARKS_VERSION); next_test(); } @@ -135,21 +154,36 @@ tests.push({ tests.push({ description: "An explicitly removed smart bookmark should not be recreated.", exec: function() { - // Set preferences. - ps.setIntPref(PREF_SMART_BOOKMARKS_VERSION, 1); // Remove toolbar's smart bookmarks bs.removeItem(bs.getIdForItemAt(bs.toolbarFolder, 0)); - // Force nsBrowserGlue::_initPlaces(). - os.notifyObservers(null, TOPIC_PLACES_INIT_COMPLETE, null); + // Sanity check items. + dump_table("moz_bookmarks"); + dump_table("moz_items_annos"); + do_check_eq(countFolderChildren(bs.toolbarFolder), + DEFAULT_BOOKMARKS_ON_TOOLBAR); + do_check_eq(countFolderChildren(bs.bookmarksMenuFolder), + SMART_BOOKMARKS_ON_MENU + DEFAULT_BOOKMARKS_ON_MENU); - // Count items on toolbar, we should not have recreated the smart bookmark. - do_check_eq(countFolderChildren(bs.toolbarFolder), DEFAULT_BOOKMARKS_ON_TOOLBAR); - // Count items on menu (+1 for the separator). - do_check_eq(countFolderChildren(bs.bookmarksMenuFolder), SMART_BOOKMARKS_ON_MENU + DEFAULT_BOOKMARKS_ON_MENU); + // Set preferences. + ps.setIntPref(PREF_SMART_BOOKMARKS_VERSION, 1); + + // Force nsBrowserGlue::_initPlaces(). + print("Simulate Places init"); + bg.QueryInterface(Ci.nsIObserver).observe(null, + TOPIC_PLACES_INIT_COMPLETE, + null); + + // Count items. + // We should not have recreated the smart bookmark on toolbar. + do_check_eq(countFolderChildren(bs.toolbarFolder), + DEFAULT_BOOKMARKS_ON_TOOLBAR); + do_check_eq(countFolderChildren(bs.bookmarksMenuFolder), + SMART_BOOKMARKS_ON_MENU + DEFAULT_BOOKMARKS_ON_MENU); // Check version has been updated. - do_check_eq(ps.getIntPref(PREF_SMART_BOOKMARKS_VERSION), SMART_BOOKMARKS_VERSION); + do_check_eq(ps.getIntPref(PREF_SMART_BOOKMARKS_VERSION), + SMART_BOOKMARKS_VERSION); next_test(); } @@ -159,20 +193,34 @@ tests.push({ tests.push({ description: "Even if a smart bookmark has been removed recreate it if version is 0.", - exec: function() { + exec: function() { + // Sanity check items. + dump_table("moz_bookmarks"); + dump_table("moz_items_annos"); + do_check_eq(countFolderChildren(bs.toolbarFolder), + DEFAULT_BOOKMARKS_ON_TOOLBAR); + do_check_eq(countFolderChildren(bs.bookmarksMenuFolder), + SMART_BOOKMARKS_ON_MENU + DEFAULT_BOOKMARKS_ON_MENU); + // Set preferences. ps.setIntPref(PREF_SMART_BOOKMARKS_VERSION, 0); // Force nsBrowserGlue::_initPlaces(). - os.notifyObservers(null, TOPIC_PLACES_INIT_COMPLETE, null); + print("Simulate Places init"); + bg.QueryInterface(Ci.nsIObserver).observe(null, + TOPIC_PLACES_INIT_COMPLETE, + null); - // Count items on toolbar, we should not have recreated the smart bookmark. - do_check_eq(countFolderChildren(bs.toolbarFolder), SMART_BOOKMARKS_ON_TOOLBAR + DEFAULT_BOOKMARKS_ON_TOOLBAR); - // Count items on menu (+1 for the separator). - do_check_eq(countFolderChildren(bs.bookmarksMenuFolder), SMART_BOOKMARKS_ON_MENU + DEFAULT_BOOKMARKS_ON_MENU); + // Count items. + // We should not have recreated the smart bookmark on toolbar. + do_check_eq(countFolderChildren(bs.toolbarFolder), + SMART_BOOKMARKS_ON_TOOLBAR + DEFAULT_BOOKMARKS_ON_TOOLBAR); + do_check_eq(countFolderChildren(bs.bookmarksMenuFolder), + SMART_BOOKMARKS_ON_MENU + DEFAULT_BOOKMARKS_ON_MENU); // Check version has been updated. - do_check_eq(ps.getIntPref(PREF_SMART_BOOKMARKS_VERSION), SMART_BOOKMARKS_VERSION); + do_check_eq(ps.getIntPref(PREF_SMART_BOOKMARKS_VERSION), + SMART_BOOKMARKS_VERSION); finish_test(); } @@ -204,10 +252,14 @@ function finish_test() { var testIndex = 0; function next_test() { - // nsBrowserGlue stops observing topics after first notification, - // so we add back the observer to test additional runs. - if (testIndex > 0) - os.addObserver(bg, TOPIC_PLACES_INIT_COMPLETE, false); + if (testIndex > 0) { + // nsBrowserGlue stops observing topics after each notification, + // so we add back the observers for additional tests. + os.addObserver(bg.QueryInterface(Ci.nsIObserver), + TOPIC_PLACES_INIT_COMPLETE, false); + os.addObserver(bg.QueryInterface(Ci.nsIObserver), + TOPIC_PLACES_DATABASE_LOCKED, false); + } // Execute next test. let test = tests.shift();