diff --git a/browser/components/nsBrowserGlue.js b/browser/components/nsBrowserGlue.js index c1b6aaadab1b..07cd11dc43ec 100644 --- a/browser/components/nsBrowserGlue.js +++ b/browser/components/nsBrowserGlue.js @@ -264,6 +264,9 @@ BrowserGlue.prototype = { else if (data == "force-distribution-customization") { this._distributionCustomizer.applyCustomizations(); } + else if (data == "force-places-init") { + this._initPlaces(); + } break; } }, diff --git a/browser/components/places/tests/unit/test_browserGlue_prefs.js b/browser/components/places/tests/unit/test_browserGlue_prefs.js index f899e8205080..18abaca2f525 100644 --- a/browser/components/places/tests/unit/test_browserGlue_prefs.js +++ b/browser/components/places/tests/unit/test_browserGlue_prefs.js @@ -1,287 +1,265 @@ -/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ -/* vim:set ts=2 sw=2 sts=2 et: */ -/* ***** BEGIN LICENSE BLOCK ***** - * Version: MPL 1.1/GPL 2.0/LGPL 2.1 - * - * The contents of this file are subject to the Mozilla Public License Version - * 1.1 (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * http://www.mozilla.org/MPL/ - * - * Software distributed under the License is distributed on an "AS IS" basis, - * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License - * for the specific language governing rights and limitations under the - * License. - * - * The Original Code is Places Unit Test code. - * - * The Initial Developer of the Original Code is Mozilla Foundation. - * Portions created by the Initial Developer are Copyright (C) 2009 - * the Initial Developer. All Rights Reserved. - * - * Contributor(s): - * Marco Bonardo - * - * Alternatively, the contents of this file may be used under the terms of - * either the GNU General Public License Version 2 or later (the "GPL"), or - * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), - * in which case the provisions of the GPL or the LGPL are applicable instead - * of those above. If you wish to allow use of your version of this file only - * under the terms of either the GPL or the LGPL, and not to allow others to - * use your version of this file under the terms of the MPL, indicate your - * decision by deleting the provisions above and replace them with the notice - * and other provisions required by the GPL or the LGPL. If you do not delete - * the provisions above, a recipient may use your version of this file under - * the terms of any one of the MPL, the GPL or the LGPL. - * - * ***** END LICENSE BLOCK ***** */ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ /** * Tests that nsBrowserGlue is correctly interpreting the preferences settable * by the user or by other components. */ -/** Bug 539067 - * Test is disabled due to random failures and timeouts, see run_test. - * This is commented out to avoid leaks. -// Initialize browserGlue. -let bg = Cc["@mozilla.org/browser/browserglue;1"]. - getService(Ci.nsIBrowserGlue); -*/ - -// Initialize Places through Bookmarks Service. -let bs = Cc["@mozilla.org/browser/nav-bookmarks-service;1"]. - getService(Ci.nsINavBookmarksService); - -// Get other services. -let ps = Cc["@mozilla.org/preferences-service;1"]. - getService(Ci.nsIPrefBranch); -let os = Cc["@mozilla.org/observer-service;1"]. - getService(Ci.nsIObserverService); - const PREF_IMPORT_BOOKMARKS_HTML = "browser.places.importBookmarksHTML"; const PREF_RESTORE_DEFAULT_BOOKMARKS = "browser.bookmarks.restore_default_bookmarks"; const PREF_SMART_BOOKMARKS_VERSION = "browser.places.smartBookmarksVersion"; const PREF_AUTO_EXPORT_HTML = "browser.bookmarks.autoExportHTML"; -let tests = []; -//------------------------------------------------------------------------------ +const TOPIC_BROWSERGLUE_TEST = "browser-glue-test"; +const TOPICDATA_FORCE_PLACES_INIT = "force-places-init"; -tests.push({ - description: "Import from bookmarks.html if importBookmarksHTML is true.", - exec: function() { +let bg = Cc["@mozilla.org/browser/browserglue;1"]. + getService(Ci.nsIBrowserGlue); + +let gTests = [ + + // This test must be the first one. + function test_checkPreferences() { + // Initialize Places through the History Service and check that a new + // database has been created. + do_check_eq(PlacesUtils.history.databaseStatus, + PlacesUtils.history.DATABASE_STATUS_CREATE); + + // Wait for Places init notification. + Services.obs.addObserver(function(aSubject, aTopic, aData) { + Services.obs.removeObserver(arguments.callee, + PlacesUtils.TOPIC_INIT_COMPLETE); + do_execute_soon(function () { + // Ensure preferences status. + do_check_false(Services.prefs.getBoolPref(PREF_AUTO_EXPORT_HTML)); + + try { + do_check_false(Services.prefs.getBoolPref(PREF_IMPORT_BOOKMARKS_HTML)); + do_throw("importBookmarksHTML pref should not exist"); + } + catch(ex) {} + + try { + do_check_false(Services.prefs.getBoolPref(PREF_RESTORE_DEFAULT_BOOKMARKS)); + do_throw("importBookmarksHTML pref should not exist"); + } + catch(ex) {} + + run_next_test(); + }); + }, PlacesUtils.TOPIC_INIT_COMPLETE, false); + }, + + function test_import() + { + do_log_info("Import from bookmarks.html if importBookmarksHTML is true."); + + remove_all_bookmarks(); // Sanity check: we should not have any bookmark on the toolbar. - do_check_eq(bs.getIdForItemAt(bs.toolbarFolder, 0), -1); + let itemId = + PlacesUtils.bookmarks.getIdForItemAt(PlacesUtils.toolbarFolderId, 0); + do_check_eq(itemId, -1); // Set preferences. - ps.setBoolPref(PREF_IMPORT_BOOKMARKS_HTML, true); + Services.prefs.setBoolPref(PREF_IMPORT_BOOKMARKS_HTML, true); // Force nsBrowserGlue::_initPlaces(). print("Simulate Places init"); bg.QueryInterface(Ci.nsIObserver).observe(null, - PlacesUtils.TOPIC_INIT_COMPLETE, - null); + TOPIC_BROWSERGLUE_TEST, + TOPICDATA_FORCE_PLACES_INIT); + // Check bookmarks.html has been imported, and a smart bookmark has been // created. - let itemId = bs.getIdForItemAt(bs.toolbarFolder, - SMART_BOOKMARKS_ON_TOOLBAR); - do_check_eq(bs.getItemTitle(itemId), "example"); + itemId = PlacesUtils.bookmarks.getIdForItemAt(PlacesUtils.toolbarFolderId, + SMART_BOOKMARKS_ON_TOOLBAR); + do_check_eq(PlacesUtils.bookmarks.getItemTitle(itemId), "example"); // Check preferences have been reverted. - do_check_false(ps.getBoolPref(PREF_IMPORT_BOOKMARKS_HTML)); + do_check_false(Services.prefs.getBoolPref(PREF_IMPORT_BOOKMARKS_HTML)); - next_test(); - } -}); + run_next_test(); + }, -//------------------------------------------------------------------------------ + function test_import_noSmartBookmarks() + { + do_log_info("import from bookmarks.html, but don't create smart bookmarks \ + if they are disabled"); -tests.push({ - description: "import from bookmarks.html, but don't create smart bookmarks if they are disabled", - exec: function() { + remove_all_bookmarks(); // Sanity check: we should not have any bookmark on the toolbar. - do_check_eq(bs.getIdForItemAt(bs.toolbarFolder, 0), -1); + let itemId = + PlacesUtils.bookmarks.getIdForItemAt(PlacesUtils.toolbarFolderId, 0); + do_check_eq(itemId, -1); // Set preferences. - ps.setIntPref(PREF_SMART_BOOKMARKS_VERSION, -1); - ps.setBoolPref(PREF_IMPORT_BOOKMARKS_HTML, true); + Services.prefs.setIntPref(PREF_SMART_BOOKMARKS_VERSION, -1); + Services.prefs.setBoolPref(PREF_IMPORT_BOOKMARKS_HTML, true); // Force nsBrowserGlue::_initPlaces(). print("Simulate Places init"); bg.QueryInterface(Ci.nsIObserver).observe(null, - PlacesUtils.TOPIC_INIT_COMPLETE, - null); + TOPIC_BROWSERGLUE_TEST, + TOPICDATA_FORCE_PLACES_INIT); + // Check bookmarks.html has been imported, but smart bookmarks have not // been created. - let itemId = bs.getIdForItemAt(bs.toolbarFolder, 0); - do_check_eq(bs.getItemTitle(itemId), "example"); + itemId = + PlacesUtils.bookmarks.getIdForItemAt(PlacesUtils.toolbarFolderId, 0); + do_check_eq(PlacesUtils.bookmarks.getItemTitle(itemId), "example"); // Check preferences have been reverted. - do_check_false(ps.getBoolPref(PREF_IMPORT_BOOKMARKS_HTML)); + do_check_false(Services.prefs.getBoolPref(PREF_IMPORT_BOOKMARKS_HTML)); - next_test(); - } -}); + run_next_test(); + }, -//------------------------------------------------------------------------------ + function test_import_autoExport_updatedSmartBookmarks() + { + do_log_info("Import from bookmarks.html, but don't create smart bookmarks \ + if autoExportHTML is true and they are at latest version"); -tests.push({ - description: "Import from bookmarks.html, but don't create smart bookmarks if autoExportHTML is true and they are at latest version", - exec: function() { + remove_all_bookmarks(); // Sanity check: we should not have any bookmark on the toolbar. - do_check_eq(bs.getIdForItemAt(bs.toolbarFolder, 0), -1); + let itemId = + PlacesUtils.bookmarks.getIdForItemAt(PlacesUtils.toolbarFolderId, 0); + do_check_eq(itemId, -1); + // Set preferences. - ps.setIntPref(PREF_SMART_BOOKMARKS_VERSION, 999); - ps.setBoolPref(PREF_AUTO_EXPORT_HTML, true); - ps.setBoolPref(PREF_IMPORT_BOOKMARKS_HTML, true); + Services.prefs.setIntPref(PREF_SMART_BOOKMARKS_VERSION, 999); + Services.prefs.setBoolPref(PREF_AUTO_EXPORT_HTML, true); + Services.prefs.setBoolPref(PREF_IMPORT_BOOKMARKS_HTML, true); // Force nsBrowserGlue::_initPlaces() print("Simulate Places init"); bg.QueryInterface(Ci.nsIObserver).observe(null, - PlacesUtils.TOPIC_INIT_COMPLETE, - null); + TOPIC_BROWSERGLUE_TEST, + TOPICDATA_FORCE_PLACES_INIT); + // Check bookmarks.html has been imported, but smart bookmarks have not // been created. - let itemId = bs.getIdForItemAt(bs.toolbarFolder, 0); - do_check_eq(bs.getItemTitle(itemId), "example"); - do_check_false(ps.getBoolPref(PREF_IMPORT_BOOKMARKS_HTML)); + itemId = + PlacesUtils.bookmarks.getIdForItemAt(PlacesUtils.toolbarFolderId, 0); + do_check_eq(PlacesUtils.bookmarks.getItemTitle(itemId), "example"); + do_check_false(Services.prefs.getBoolPref(PREF_IMPORT_BOOKMARKS_HTML)); // Check preferences have been reverted. - ps.setBoolPref(PREF_AUTO_EXPORT_HTML, false); + Services.prefs.setBoolPref(PREF_AUTO_EXPORT_HTML, false); - next_test(); - } -}); + run_next_test(); + }, -//------------------------------------------------------------------------------ + function test_import_autoExport_oldSmartBookmarks() + { + do_log_info("Import from bookmarks.html, and create smart bookmarks if \ + autoExportHTML is true and they are not at latest version."); -tests.push({ - description: "Import from bookmarks.html, and create smart bookmarks if autoExportHTML is true and they are not at latest version.", - exec: function() { + remove_all_bookmarks(); // Sanity check: we should not have any bookmark on the toolbar. - do_check_eq(bs.getIdForItemAt(bs.toolbarFolder, 0), -1); + let itemId = + PlacesUtils.bookmarks.getIdForItemAt(PlacesUtils.toolbarFolderId, 0); + do_check_eq(itemId, -1); + // Set preferences. - ps.setIntPref(PREF_SMART_BOOKMARKS_VERSION, 0); - ps.setBoolPref(PREF_AUTO_EXPORT_HTML, true); - ps.setBoolPref(PREF_IMPORT_BOOKMARKS_HTML, true); + Services.prefs.setIntPref(PREF_SMART_BOOKMARKS_VERSION, 0); + Services.prefs.setBoolPref(PREF_AUTO_EXPORT_HTML, true); + Services.prefs.setBoolPref(PREF_IMPORT_BOOKMARKS_HTML, true); // Force nsBrowserGlue::_initPlaces() print("Simulate Places init"); bg.QueryInterface(Ci.nsIObserver).observe(null, - PlacesUtils.TOPIC_INIT_COMPLETE, - null); + TOPIC_BROWSERGLUE_TEST, + TOPICDATA_FORCE_PLACES_INIT); + // Check bookmarks.html has been imported, but smart bookmarks have not // been created. - let itemId = bs.getIdForItemAt(bs.toolbarFolder, SMART_BOOKMARKS_ON_TOOLBAR); - do_check_eq(bs.getItemTitle(itemId), "example"); - do_check_false(ps.getBoolPref(PREF_IMPORT_BOOKMARKS_HTML)); + itemId = + PlacesUtils.bookmarks.getIdForItemAt(PlacesUtils.toolbarFolderId, + SMART_BOOKMARKS_ON_TOOLBAR); + do_check_eq(PlacesUtils.bookmarks.getItemTitle(itemId), "example"); + do_check_false(Services.prefs.getBoolPref(PREF_IMPORT_BOOKMARKS_HTML)); // Check preferences have been reverted. - ps.setBoolPref(PREF_AUTO_EXPORT_HTML, false); + Services.prefs.setBoolPref(PREF_AUTO_EXPORT_HTML, false); - next_test(); - } -}); + run_next_test(); + }, -//------------------------------------------------------------------------------ -tests.push({ - description: "restore from default bookmarks.html if restore_default_bookmarks is true.", - exec: function() { + function test_restore() + { + do_log_info("restore from default bookmarks.html if \ + restore_default_bookmarks is true."); + + remove_all_bookmarks(); // Sanity check: we should not have any bookmark on the toolbar. - do_check_eq(bs.getIdForItemAt(bs.toolbarFolder, 0), -1); + let itemId = + PlacesUtils.bookmarks.getIdForItemAt(PlacesUtils.toolbarFolderId, 0); + do_check_eq(itemId, -1); + // Set preferences. - ps.setBoolPref(PREF_RESTORE_DEFAULT_BOOKMARKS, true); + Services.prefs.setBoolPref(PREF_RESTORE_DEFAULT_BOOKMARKS, true); // Force nsBrowserGlue::_initPlaces() print("Simulate Places init"); bg.QueryInterface(Ci.nsIObserver).observe(null, - PlacesUtils.TOPIC_INIT_COMPLETE, - null); + TOPIC_BROWSERGLUE_TEST, + TOPICDATA_FORCE_PLACES_INIT); + // Check bookmarks.html has been restored. - let itemId = bs.getIdForItemAt(bs.toolbarFolder, SMART_BOOKMARKS_ON_TOOLBAR + 1); + itemId = + PlacesUtils.bookmarks.getIdForItemAt(PlacesUtils.toolbarFolderId, + SMART_BOOKMARKS_ON_TOOLBAR + 1); do_check_true(itemId > 0); // Check preferences have been reverted. - do_check_false(ps.getBoolPref(PREF_RESTORE_DEFAULT_BOOKMARKS)); + do_check_false(Services.prefs.getBoolPref(PREF_RESTORE_DEFAULT_BOOKMARKS)); - next_test(); - } -}); + run_next_test(); + }, -//------------------------------------------------------------------------------ + function test_restore_import() + { + do_log_info("setting both importBookmarksHTML and \ + restore_default_bookmarks should restore defaults."); -tests.push({ - description: "setting both importBookmarksHTML and restore_default_bookmarks should restore defaults.", - exec: function() { + remove_all_bookmarks(); // Sanity check: we should not have any bookmark on the toolbar. - do_check_eq(bs.getIdForItemAt(bs.toolbarFolder, 0), -1); + let itemId = + PlacesUtils.bookmarks.getIdForItemAt(PlacesUtils.toolbarFolderId, 0); + do_check_eq(itemId, -1); + // Set preferences. - ps.setBoolPref(PREF_IMPORT_BOOKMARKS_HTML, true); - ps.setBoolPref(PREF_RESTORE_DEFAULT_BOOKMARKS, true); + Services.prefs.setBoolPref(PREF_IMPORT_BOOKMARKS_HTML, true); + Services.prefs.setBoolPref(PREF_RESTORE_DEFAULT_BOOKMARKS, true); // Force nsBrowserGlue::_initPlaces() print("Simulate Places init"); bg.QueryInterface(Ci.nsIObserver).observe(null, - PlacesUtils.TOPIC_INIT_COMPLETE, - null); + TOPIC_BROWSERGLUE_TEST, + TOPICDATA_FORCE_PLACES_INIT); + // Check bookmarks.html has been restored. - let itemId = bs.getIdForItemAt(bs.toolbarFolder, SMART_BOOKMARKS_ON_TOOLBAR + 1); + itemId = + PlacesUtils.bookmarks.getIdForItemAt(PlacesUtils.toolbarFolderId, + SMART_BOOKMARKS_ON_TOOLBAR + 1); do_check_true(itemId > 0); // Check preferences have been reverted. - do_check_false(ps.getBoolPref(PREF_RESTORE_DEFAULT_BOOKMARKS)); - do_check_false(ps.getBoolPref(PREF_IMPORT_BOOKMARKS_HTML)); + do_check_false(Services.prefs.getBoolPref(PREF_RESTORE_DEFAULT_BOOKMARKS)); + do_check_false(Services.prefs.getBoolPref(PREF_IMPORT_BOOKMARKS_HTML)); - do_test_finished(); + run_next_test(); } -}); -//------------------------------------------------------------------------------ +]; -function finish_test() { - // Clean up database from all bookmarks. +do_register_cleanup(function () { remove_all_bookmarks(); remove_bookmarks_html(); remove_all_JSON_backups(); +}); - do_test_finished(); -} -var testIndex = 0; -function next_test() { - // Clean up database from all bookmarks. - remove_all_bookmarks(); - // nsBrowserGlue stops observing topics after first notification, - // so we add back the observer to test additional runs. - os.addObserver(bg.QueryInterface(Ci.nsIObserver), - PlacesUtils.TOPIC_INIT_COMPLETE, false); - os.addObserver(bg.QueryInterface(Ci.nsIObserver), - PlacesUtils.TOPIC_DATABASE_LOCKED, false); - // Execute next test. - let test = tests.shift(); - print("\nTEST " + (++testIndex) + ": " + test.description); - test.exec(); -} -function run_test() { - // Bug 539067: disabled due to random failures and timeouts. - return; - - do_test_pending(); - // Enqueue test, so it will consume the default places-init-complete - // notification created at Places init. - do_timeout(0, start_tests); -} - -function start_tests() { - // Clean up database from all bookmarks. - remove_all_bookmarks(); - - // Ensure preferences status. - do_check_false(ps.getBoolPref(PREF_AUTO_EXPORT_HTML)); - try { - do_check_false(ps.getBoolPref(PREF_IMPORT_BOOKMARKS_HTML)); - do_throw("importBookmarksHTML pref should not exist"); - } - catch(ex) {} - do_check_false(ps.getBoolPref(PREF_RESTORE_DEFAULT_BOOKMARKS)); - +function run_test() +{ // Create our bookmarks.html from bookmarks.glue.html. create_bookmarks_html("bookmarks.glue.html"); // Create our JSON backup from bookmarks.glue.json. create_JSON_backup("bookmarks.glue.json"); - // Kick-off tests. - next_test(); + + run_next_test(); }