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: /** michael@0: * Tests that nsBrowserGlue is correctly interpreting the preferences settable michael@0: * by the user or by other components. michael@0: */ michael@0: michael@0: const PREF_SMART_BOOKMARKS_VERSION = "browser.places.smartBookmarksVersion"; michael@0: const PREF_AUTO_EXPORT_HTML = "browser.bookmarks.autoExportHTML"; michael@0: const PREF_IMPORT_BOOKMARKS_HTML = "browser.places.importBookmarksHTML"; michael@0: const PREF_RESTORE_DEFAULT_BOOKMARKS = "browser.bookmarks.restore_default_bookmarks"; michael@0: michael@0: const SMART_BOOKMARKS_ANNO = "Places/SmartBookmark"; michael@0: michael@0: /** michael@0: * Rebuilds smart bookmarks listening to console output to report any message or michael@0: * exception generated when calling ensurePlacesDefaultQueriesInitialized(). michael@0: */ michael@0: function rebuildSmartBookmarks() { michael@0: let consoleListener = { michael@0: observe: function(aMsg) { michael@0: print("Got console message: " + aMsg.message); michael@0: }, michael@0: michael@0: QueryInterface: XPCOMUtils.generateQI([ michael@0: Ci.nsIConsoleListener michael@0: ]), michael@0: }; michael@0: Services.console.reset(); michael@0: Services.console.registerListener(consoleListener); michael@0: Cc["@mozilla.org/browser/browserglue;1"].getService(Ci.nsIBrowserGlue) michael@0: .ensurePlacesDefaultQueriesInitialized(); michael@0: Services.console.unregisterListener(consoleListener); michael@0: } michael@0: michael@0: michael@0: let tests = []; michael@0: //------------------------------------------------------------------------------ michael@0: michael@0: tests.push({ michael@0: description: "All smart bookmarks are created if smart bookmarks version is 0.", michael@0: exec: function() { michael@0: // Sanity check: we should have default bookmark. michael@0: do_check_neq(PlacesUtils.bookmarks.getIdForItemAt(PlacesUtils.toolbarFolderId, 0), -1); michael@0: do_check_neq(PlacesUtils.bookmarks.getIdForItemAt(PlacesUtils.bookmarksMenuFolderId, 0), -1); michael@0: michael@0: // Set preferences. michael@0: Services.prefs.setIntPref(PREF_SMART_BOOKMARKS_VERSION, 0); michael@0: michael@0: rebuildSmartBookmarks(); michael@0: michael@0: // Count items. michael@0: do_check_eq(countFolderChildren(PlacesUtils.toolbarFolderId), michael@0: SMART_BOOKMARKS_ON_TOOLBAR + DEFAULT_BOOKMARKS_ON_TOOLBAR); michael@0: do_check_eq(countFolderChildren(PlacesUtils.bookmarksMenuFolderId), michael@0: SMART_BOOKMARKS_ON_MENU + DEFAULT_BOOKMARKS_ON_MENU); michael@0: michael@0: // Check version has been updated. michael@0: do_check_eq(Services.prefs.getIntPref(PREF_SMART_BOOKMARKS_VERSION), michael@0: SMART_BOOKMARKS_VERSION); michael@0: michael@0: next_test(); michael@0: } michael@0: }); michael@0: michael@0: //------------------------------------------------------------------------------ michael@0: michael@0: tests.push({ michael@0: description: "An existing smart bookmark is replaced when version changes.", michael@0: exec: function() { michael@0: // Sanity check: we have a smart bookmark on the toolbar. michael@0: let itemId = PlacesUtils.bookmarks.getIdForItemAt(PlacesUtils.toolbarFolderId, 0); michael@0: do_check_neq(itemId, -1); michael@0: do_check_true(PlacesUtils.annotations.itemHasAnnotation(itemId, SMART_BOOKMARKS_ANNO)); michael@0: // Change its title. michael@0: PlacesUtils.bookmarks.setItemTitle(itemId, "new title"); michael@0: do_check_eq(PlacesUtils.bookmarks.getItemTitle(itemId), "new title"); michael@0: michael@0: // Sanity check items. michael@0: do_check_eq(countFolderChildren(PlacesUtils.toolbarFolderId), michael@0: SMART_BOOKMARKS_ON_TOOLBAR + DEFAULT_BOOKMARKS_ON_TOOLBAR); michael@0: do_check_eq(countFolderChildren(PlacesUtils.bookmarksMenuFolderId), michael@0: SMART_BOOKMARKS_ON_MENU + DEFAULT_BOOKMARKS_ON_MENU); michael@0: michael@0: // Set preferences. michael@0: Services.prefs.setIntPref(PREF_SMART_BOOKMARKS_VERSION, 1); michael@0: michael@0: rebuildSmartBookmarks(); michael@0: michael@0: // Count items. michael@0: do_check_eq(countFolderChildren(PlacesUtils.toolbarFolderId), michael@0: SMART_BOOKMARKS_ON_TOOLBAR + DEFAULT_BOOKMARKS_ON_TOOLBAR); michael@0: do_check_eq(countFolderChildren(PlacesUtils.bookmarksMenuFolderId), michael@0: SMART_BOOKMARKS_ON_MENU + DEFAULT_BOOKMARKS_ON_MENU); michael@0: michael@0: // Check smart bookmark has been replaced, itemId has changed. michael@0: itemId = PlacesUtils.bookmarks.getIdForItemAt(PlacesUtils.toolbarFolderId, 0); michael@0: do_check_neq(itemId, -1); michael@0: do_check_neq(PlacesUtils.bookmarks.getItemTitle(itemId), "new title"); michael@0: do_check_true(PlacesUtils.annotations.itemHasAnnotation(itemId, SMART_BOOKMARKS_ANNO)); michael@0: michael@0: // Check version has been updated. michael@0: do_check_eq(Services.prefs.getIntPref(PREF_SMART_BOOKMARKS_VERSION), michael@0: SMART_BOOKMARKS_VERSION); michael@0: michael@0: next_test(); michael@0: } michael@0: }); michael@0: michael@0: //------------------------------------------------------------------------------ michael@0: michael@0: tests.push({ michael@0: description: "bookmarks position is retained when version changes.", michael@0: exec: function() { michael@0: // Sanity check items. michael@0: do_check_eq(countFolderChildren(PlacesUtils.toolbarFolderId), michael@0: SMART_BOOKMARKS_ON_TOOLBAR + DEFAULT_BOOKMARKS_ON_TOOLBAR); michael@0: do_check_eq(countFolderChildren(PlacesUtils.bookmarksMenuFolderId), michael@0: SMART_BOOKMARKS_ON_MENU + DEFAULT_BOOKMARKS_ON_MENU); michael@0: michael@0: let itemId = PlacesUtils.bookmarks.getIdForItemAt(PlacesUtils.bookmarksMenuFolderId, 0); michael@0: do_check_true(PlacesUtils.annotations.itemHasAnnotation(itemId, SMART_BOOKMARKS_ANNO)); michael@0: let firstItemTitle = PlacesUtils.bookmarks.getItemTitle(itemId); michael@0: michael@0: itemId = PlacesUtils.bookmarks.getIdForItemAt(PlacesUtils.bookmarksMenuFolderId, 1); michael@0: do_check_true(PlacesUtils.annotations.itemHasAnnotation(itemId, SMART_BOOKMARKS_ANNO)); michael@0: let secondItemTitle = PlacesUtils.bookmarks.getItemTitle(itemId); michael@0: michael@0: // Set preferences. michael@0: Services.prefs.setIntPref(PREF_SMART_BOOKMARKS_VERSION, 1); michael@0: michael@0: rebuildSmartBookmarks(); michael@0: michael@0: // Count items. michael@0: do_check_eq(countFolderChildren(PlacesUtils.toolbarFolderId), michael@0: SMART_BOOKMARKS_ON_TOOLBAR + DEFAULT_BOOKMARKS_ON_TOOLBAR); michael@0: do_check_eq(countFolderChildren(PlacesUtils.bookmarksMenuFolderId), michael@0: SMART_BOOKMARKS_ON_MENU + DEFAULT_BOOKMARKS_ON_MENU); michael@0: michael@0: // Check smart bookmarks are still in correct position. michael@0: itemId = PlacesUtils.bookmarks.getIdForItemAt(PlacesUtils.bookmarksMenuFolderId, 0); michael@0: do_check_true(PlacesUtils.annotations.itemHasAnnotation(itemId, SMART_BOOKMARKS_ANNO)); michael@0: do_check_eq(PlacesUtils.bookmarks.getItemTitle(itemId), firstItemTitle); michael@0: michael@0: itemId = PlacesUtils.bookmarks.getIdForItemAt(PlacesUtils.bookmarksMenuFolderId, 1); michael@0: do_check_true(PlacesUtils.annotations.itemHasAnnotation(itemId, SMART_BOOKMARKS_ANNO)); michael@0: do_check_eq(PlacesUtils.bookmarks.getItemTitle(itemId), secondItemTitle); michael@0: michael@0: // Check version has been updated. michael@0: do_check_eq(Services.prefs.getIntPref(PREF_SMART_BOOKMARKS_VERSION), michael@0: SMART_BOOKMARKS_VERSION); michael@0: michael@0: next_test(); michael@0: } michael@0: }); michael@0: michael@0: //------------------------------------------------------------------------------ michael@0: michael@0: tests.push({ michael@0: description: "moved bookmarks position is retained when version changes.", michael@0: exec: function() { michael@0: // Sanity check items. michael@0: do_check_eq(countFolderChildren(PlacesUtils.toolbarFolderId), michael@0: SMART_BOOKMARKS_ON_TOOLBAR + DEFAULT_BOOKMARKS_ON_TOOLBAR); michael@0: do_check_eq(countFolderChildren(PlacesUtils.bookmarksMenuFolderId), michael@0: SMART_BOOKMARKS_ON_MENU + DEFAULT_BOOKMARKS_ON_MENU); michael@0: michael@0: let itemId1 = PlacesUtils.bookmarks.getIdForItemAt(PlacesUtils.bookmarksMenuFolderId, 0); michael@0: do_check_true(PlacesUtils.annotations.itemHasAnnotation(itemId1, SMART_BOOKMARKS_ANNO)); michael@0: let firstItemTitle = PlacesUtils.bookmarks.getItemTitle(itemId1); michael@0: michael@0: let itemId2 = PlacesUtils.bookmarks.getIdForItemAt(PlacesUtils.bookmarksMenuFolderId, 1); michael@0: do_check_true(PlacesUtils.annotations.itemHasAnnotation(itemId2, SMART_BOOKMARKS_ANNO)); michael@0: let secondItemTitle = PlacesUtils.bookmarks.getItemTitle(itemId2); michael@0: michael@0: // Move the first smart bookmark to the end of the menu. michael@0: PlacesUtils.bookmarks.moveItem(itemId1, PlacesUtils.bookmarksMenuFolderId, michael@0: PlacesUtils.bookmarks.DEFAULT_INDEX); michael@0: michael@0: do_check_eq(itemId1, PlacesUtils.bookmarks.getIdForItemAt(PlacesUtils.bookmarksMenuFolderId, michael@0: PlacesUtils.bookmarks.DEFAULT_INDEX)); michael@0: michael@0: // Set preferences. michael@0: Services.prefs.setIntPref(PREF_SMART_BOOKMARKS_VERSION, 1); michael@0: michael@0: rebuildSmartBookmarks(); michael@0: michael@0: // Count items. michael@0: do_check_eq(countFolderChildren(PlacesUtils.toolbarFolderId), michael@0: SMART_BOOKMARKS_ON_TOOLBAR + DEFAULT_BOOKMARKS_ON_TOOLBAR); michael@0: do_check_eq(countFolderChildren(PlacesUtils.bookmarksMenuFolderId), michael@0: SMART_BOOKMARKS_ON_MENU + DEFAULT_BOOKMARKS_ON_MENU); michael@0: michael@0: // Check smart bookmarks are still in correct position. michael@0: itemId2 = PlacesUtils.bookmarks.getIdForItemAt(PlacesUtils.bookmarksMenuFolderId, 0); michael@0: do_check_true(PlacesUtils.annotations.itemHasAnnotation(itemId2, SMART_BOOKMARKS_ANNO)); michael@0: do_check_eq(PlacesUtils.bookmarks.getItemTitle(itemId2), secondItemTitle); michael@0: michael@0: itemId1 = PlacesUtils.bookmarks.getIdForItemAt(PlacesUtils.bookmarksMenuFolderId, michael@0: PlacesUtils.bookmarks.DEFAULT_INDEX); michael@0: do_check_true(PlacesUtils.annotations.itemHasAnnotation(itemId1, SMART_BOOKMARKS_ANNO)); michael@0: do_check_eq(PlacesUtils.bookmarks.getItemTitle(itemId1), firstItemTitle); michael@0: michael@0: // Move back the smart bookmark to the original position. michael@0: PlacesUtils.bookmarks.moveItem(itemId1, PlacesUtils.bookmarksMenuFolderId, 1); michael@0: michael@0: // Check version has been updated. michael@0: do_check_eq(Services.prefs.getIntPref(PREF_SMART_BOOKMARKS_VERSION), michael@0: SMART_BOOKMARKS_VERSION); michael@0: michael@0: next_test(); michael@0: } michael@0: }); michael@0: michael@0: //------------------------------------------------------------------------------ michael@0: michael@0: tests.push({ michael@0: description: "An explicitly removed smart bookmark should not be recreated.", michael@0: exec: function() { michael@0: // Remove toolbar's smart bookmarks michael@0: PlacesUtils.bookmarks.removeItem(PlacesUtils.bookmarks.getIdForItemAt(PlacesUtils.toolbarFolderId, 0)); michael@0: michael@0: // Sanity check items. michael@0: do_check_eq(countFolderChildren(PlacesUtils.toolbarFolderId), michael@0: DEFAULT_BOOKMARKS_ON_TOOLBAR); michael@0: do_check_eq(countFolderChildren(PlacesUtils.bookmarksMenuFolderId), michael@0: SMART_BOOKMARKS_ON_MENU + DEFAULT_BOOKMARKS_ON_MENU); michael@0: michael@0: // Set preferences. michael@0: Services.prefs.setIntPref(PREF_SMART_BOOKMARKS_VERSION, 1); michael@0: michael@0: rebuildSmartBookmarks(); michael@0: michael@0: // Count items. michael@0: // We should not have recreated the smart bookmark on toolbar. michael@0: do_check_eq(countFolderChildren(PlacesUtils.toolbarFolderId), michael@0: DEFAULT_BOOKMARKS_ON_TOOLBAR); michael@0: do_check_eq(countFolderChildren(PlacesUtils.bookmarksMenuFolderId), michael@0: SMART_BOOKMARKS_ON_MENU + DEFAULT_BOOKMARKS_ON_MENU); michael@0: michael@0: // Check version has been updated. michael@0: do_check_eq(Services.prefs.getIntPref(PREF_SMART_BOOKMARKS_VERSION), michael@0: SMART_BOOKMARKS_VERSION); michael@0: michael@0: next_test(); michael@0: } michael@0: }); michael@0: michael@0: //------------------------------------------------------------------------------ michael@0: michael@0: tests.push({ michael@0: description: "Even if a smart bookmark has been removed recreate it if version is 0.", michael@0: exec: function() { michael@0: // Sanity check items. michael@0: do_check_eq(countFolderChildren(PlacesUtils.toolbarFolderId), michael@0: DEFAULT_BOOKMARKS_ON_TOOLBAR); michael@0: do_check_eq(countFolderChildren(PlacesUtils.bookmarksMenuFolderId), michael@0: SMART_BOOKMARKS_ON_MENU + DEFAULT_BOOKMARKS_ON_MENU); michael@0: michael@0: // Set preferences. michael@0: Services.prefs.setIntPref(PREF_SMART_BOOKMARKS_VERSION, 0); michael@0: michael@0: rebuildSmartBookmarks(); michael@0: michael@0: // Count items. michael@0: // We should not have recreated the smart bookmark on toolbar. michael@0: do_check_eq(countFolderChildren(PlacesUtils.toolbarFolderId), michael@0: SMART_BOOKMARKS_ON_TOOLBAR + DEFAULT_BOOKMARKS_ON_TOOLBAR); michael@0: do_check_eq(countFolderChildren(PlacesUtils.bookmarksMenuFolderId), michael@0: SMART_BOOKMARKS_ON_MENU + DEFAULT_BOOKMARKS_ON_MENU); michael@0: michael@0: // Check version has been updated. michael@0: do_check_eq(Services.prefs.getIntPref(PREF_SMART_BOOKMARKS_VERSION), michael@0: SMART_BOOKMARKS_VERSION); michael@0: michael@0: next_test(); michael@0: } michael@0: }); michael@0: //------------------------------------------------------------------------------ michael@0: michael@0: function countFolderChildren(aFolderItemId) { michael@0: let rootNode = PlacesUtils.getFolderContents(aFolderItemId).root; michael@0: let cc = rootNode.childCount; michael@0: // Dump contents. michael@0: for (let i = 0; i < cc ; i++) { michael@0: let node = rootNode.getChild(i); michael@0: let title = PlacesUtils.nodeIsSeparator(node) ? "---" : node.title; michael@0: print("Found child(" + i + "): " + title); michael@0: } michael@0: rootNode.containerOpen = false; michael@0: return cc; michael@0: } michael@0: michael@0: function next_test() { michael@0: if (tests.length) { michael@0: // Execute next test. michael@0: let test = tests.shift(); michael@0: print("\nTEST: " + test.description); michael@0: test.exec(); michael@0: } michael@0: else { michael@0: // Clean up database from all bookmarks. michael@0: remove_all_bookmarks(); michael@0: do_test_finished(); michael@0: } michael@0: } michael@0: michael@0: function run_test() { michael@0: do_test_pending(); michael@0: michael@0: remove_bookmarks_html(); michael@0: remove_all_JSON_backups(); michael@0: michael@0: // Initialize browserGlue, but remove it's listener to places-init-complete. michael@0: let bg = Cc["@mozilla.org/browser/browserglue;1"].getService(Ci.nsIObserver); michael@0: // Initialize Places. michael@0: PlacesUtils.history; michael@0: // Observes Places initialisation complete. michael@0: Services.obs.addObserver(function waitPlaceInitComplete() { michael@0: Services.obs.removeObserver(waitPlaceInitComplete, "places-browser-init-complete"); michael@0: michael@0: // Ensure preferences status. michael@0: do_check_false(Services.prefs.getBoolPref(PREF_AUTO_EXPORT_HTML)); michael@0: do_check_false(Services.prefs.getBoolPref(PREF_RESTORE_DEFAULT_BOOKMARKS)); michael@0: try { michael@0: do_check_false(Services.prefs.getBoolPref(PREF_IMPORT_BOOKMARKS_HTML)); michael@0: do_throw("importBookmarksHTML pref should not exist"); michael@0: } michael@0: catch(ex) {} michael@0: michael@0: waitForImportAndSmartBookmarks(next_test); michael@0: }, "places-browser-init-complete", false); michael@0: michael@0: // Usually places init would async notify to glue, but we want to avoid michael@0: // randomness here, thus we fire the notification synchronously. michael@0: bg.observe(null, "places-init-complete", null); michael@0: } michael@0: michael@0: function waitForImportAndSmartBookmarks(aCallback) { michael@0: Services.obs.addObserver(function waitImport() { michael@0: Services.obs.removeObserver(waitImport, "bookmarks-restore-success"); michael@0: // Delay to test eventual smart bookmarks creation. michael@0: do_execute_soon(function () { michael@0: promiseAsyncUpdates().then(aCallback); michael@0: }); michael@0: }, "bookmarks-restore-success", false); michael@0: }