michael@0: /* Any copyright is dedicated to the Public Domain. michael@0: http://creativecommons.org/publicdomain/zero/1.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_IMPORT_BOOKMARKS_HTML = "browser.places.importBookmarksHTML"; michael@0: const PREF_RESTORE_DEFAULT_BOOKMARKS = "browser.bookmarks.restore_default_bookmarks"; michael@0: const PREF_SMART_BOOKMARKS_VERSION = "browser.places.smartBookmarksVersion"; michael@0: const PREF_AUTO_EXPORT_HTML = "browser.bookmarks.autoExportHTML"; michael@0: michael@0: const TOPIC_BROWSERGLUE_TEST = "browser-glue-test"; michael@0: const TOPICDATA_FORCE_PLACES_INIT = "force-places-init"; michael@0: michael@0: let bg = Cc["@mozilla.org/browser/browserglue;1"]. michael@0: getService(Ci.nsIBrowserGlue); 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: } michael@0: michael@0: [ michael@0: michael@0: // This test must be the first one. michael@0: function test_checkPreferences() { michael@0: // Initialize Places through the History Service and check that a new michael@0: // database has been created. michael@0: do_check_eq(PlacesUtils.history.databaseStatus, michael@0: PlacesUtils.history.DATABASE_STATUS_CREATE); michael@0: michael@0: // Wait for Places init notification. michael@0: Services.obs.addObserver(function(aSubject, aTopic, aData) { michael@0: Services.obs.removeObserver(arguments.callee, michael@0: "places-browser-init-complete"); michael@0: do_execute_soon(function () { michael@0: // Ensure preferences status. michael@0: do_check_false(Services.prefs.getBoolPref(PREF_AUTO_EXPORT_HTML)); michael@0: 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: try { michael@0: do_check_false(Services.prefs.getBoolPref(PREF_RESTORE_DEFAULT_BOOKMARKS)); michael@0: do_throw("importBookmarksHTML pref should not exist"); michael@0: } michael@0: catch(ex) {} michael@0: michael@0: run_next_test(); michael@0: }); michael@0: }, "places-browser-init-complete", false); michael@0: }, michael@0: michael@0: function test_import() michael@0: { michael@0: do_log_info("Import from bookmarks.html if importBookmarksHTML is true."); michael@0: michael@0: remove_all_bookmarks(); michael@0: // Sanity check: we should not have any bookmark on the toolbar. michael@0: let itemId = michael@0: PlacesUtils.bookmarks.getIdForItemAt(PlacesUtils.toolbarFolderId, 0); michael@0: do_check_eq(itemId, -1); michael@0: michael@0: // Set preferences. michael@0: Services.prefs.setBoolPref(PREF_IMPORT_BOOKMARKS_HTML, true); michael@0: michael@0: waitForImportAndSmartBookmarks(function () { michael@0: // Check bookmarks.html has been imported, and a smart bookmark has been michael@0: // created. michael@0: itemId = PlacesUtils.bookmarks.getIdForItemAt(PlacesUtils.toolbarFolderId, michael@0: SMART_BOOKMARKS_ON_TOOLBAR); michael@0: do_check_eq(PlacesUtils.bookmarks.getItemTitle(itemId), "example"); michael@0: // Check preferences have been reverted. michael@0: do_check_false(Services.prefs.getBoolPref(PREF_IMPORT_BOOKMARKS_HTML)); michael@0: michael@0: run_next_test(); michael@0: }); michael@0: // Force nsBrowserGlue::_initPlaces(). michael@0: do_log_info("Simulate Places init"); michael@0: bg.QueryInterface(Ci.nsIObserver).observe(null, michael@0: TOPIC_BROWSERGLUE_TEST, michael@0: TOPICDATA_FORCE_PLACES_INIT); michael@0: }, michael@0: michael@0: function test_import_noSmartBookmarks() michael@0: { michael@0: do_log_info("import from bookmarks.html, but don't create smart bookmarks \ michael@0: if they are disabled"); michael@0: michael@0: remove_all_bookmarks(); michael@0: // Sanity check: we should not have any bookmark on the toolbar. michael@0: let itemId = michael@0: PlacesUtils.bookmarks.getIdForItemAt(PlacesUtils.toolbarFolderId, 0); michael@0: do_check_eq(itemId, -1); michael@0: michael@0: // Set preferences. michael@0: Services.prefs.setIntPref(PREF_SMART_BOOKMARKS_VERSION, -1); michael@0: Services.prefs.setBoolPref(PREF_IMPORT_BOOKMARKS_HTML, true); michael@0: michael@0: waitForImportAndSmartBookmarks(function () { michael@0: // Check bookmarks.html has been imported, but smart bookmarks have not michael@0: // been created. michael@0: itemId = michael@0: PlacesUtils.bookmarks.getIdForItemAt(PlacesUtils.toolbarFolderId, 0); michael@0: do_check_eq(PlacesUtils.bookmarks.getItemTitle(itemId), "example"); michael@0: // Check preferences have been reverted. michael@0: do_check_false(Services.prefs.getBoolPref(PREF_IMPORT_BOOKMARKS_HTML)); michael@0: michael@0: run_next_test(); michael@0: }); michael@0: // Force nsBrowserGlue::_initPlaces(). michael@0: do_log_info("Simulate Places init"); michael@0: bg.QueryInterface(Ci.nsIObserver).observe(null, michael@0: TOPIC_BROWSERGLUE_TEST, michael@0: TOPICDATA_FORCE_PLACES_INIT); michael@0: }, michael@0: michael@0: function test_import_autoExport_updatedSmartBookmarks() michael@0: { michael@0: do_log_info("Import from bookmarks.html, but don't create smart bookmarks \ michael@0: if autoExportHTML is true and they are at latest version"); michael@0: michael@0: remove_all_bookmarks(); michael@0: // Sanity check: we should not have any bookmark on the toolbar. michael@0: let itemId = michael@0: PlacesUtils.bookmarks.getIdForItemAt(PlacesUtils.toolbarFolderId, 0); michael@0: do_check_eq(itemId, -1); michael@0: michael@0: // Set preferences. michael@0: Services.prefs.setIntPref(PREF_SMART_BOOKMARKS_VERSION, 999); michael@0: Services.prefs.setBoolPref(PREF_AUTO_EXPORT_HTML, true); michael@0: Services.prefs.setBoolPref(PREF_IMPORT_BOOKMARKS_HTML, true); michael@0: michael@0: waitForImportAndSmartBookmarks(function () { michael@0: // Check bookmarks.html has been imported, but smart bookmarks have not michael@0: // been created. michael@0: itemId = michael@0: PlacesUtils.bookmarks.getIdForItemAt(PlacesUtils.toolbarFolderId, 0); michael@0: do_check_eq(PlacesUtils.bookmarks.getItemTitle(itemId), "example"); michael@0: do_check_false(Services.prefs.getBoolPref(PREF_IMPORT_BOOKMARKS_HTML)); michael@0: // Check preferences have been reverted. michael@0: Services.prefs.setBoolPref(PREF_AUTO_EXPORT_HTML, false); michael@0: michael@0: run_next_test(); michael@0: }); michael@0: // Force nsBrowserGlue::_initPlaces() michael@0: do_log_info("Simulate Places init"); michael@0: bg.QueryInterface(Ci.nsIObserver).observe(null, michael@0: TOPIC_BROWSERGLUE_TEST, michael@0: TOPICDATA_FORCE_PLACES_INIT); michael@0: }, michael@0: michael@0: function test_import_autoExport_oldSmartBookmarks() michael@0: { michael@0: do_log_info("Import from bookmarks.html, and create smart bookmarks if \ michael@0: autoExportHTML is true and they are not at latest version."); michael@0: michael@0: remove_all_bookmarks(); michael@0: // Sanity check: we should not have any bookmark on the toolbar. michael@0: let itemId = michael@0: PlacesUtils.bookmarks.getIdForItemAt(PlacesUtils.toolbarFolderId, 0); michael@0: do_check_eq(itemId, -1); michael@0: michael@0: // Set preferences. michael@0: Services.prefs.setIntPref(PREF_SMART_BOOKMARKS_VERSION, 0); michael@0: Services.prefs.setBoolPref(PREF_AUTO_EXPORT_HTML, true); michael@0: Services.prefs.setBoolPref(PREF_IMPORT_BOOKMARKS_HTML, true); michael@0: michael@0: waitForImportAndSmartBookmarks(function () { michael@0: // Check bookmarks.html has been imported, but smart bookmarks have not michael@0: // been created. michael@0: itemId = michael@0: PlacesUtils.bookmarks.getIdForItemAt(PlacesUtils.toolbarFolderId, michael@0: SMART_BOOKMARKS_ON_TOOLBAR); michael@0: do_check_eq(PlacesUtils.bookmarks.getItemTitle(itemId), "example"); michael@0: do_check_false(Services.prefs.getBoolPref(PREF_IMPORT_BOOKMARKS_HTML)); michael@0: // Check preferences have been reverted. michael@0: Services.prefs.setBoolPref(PREF_AUTO_EXPORT_HTML, false); michael@0: michael@0: run_next_test(); michael@0: }); michael@0: // Force nsBrowserGlue::_initPlaces() michael@0: do_log_info("Simulate Places init"); michael@0: bg.QueryInterface(Ci.nsIObserver).observe(null, michael@0: TOPIC_BROWSERGLUE_TEST, michael@0: TOPICDATA_FORCE_PLACES_INIT); michael@0: }, michael@0: michael@0: function test_restore() michael@0: { michael@0: do_log_info("restore from default bookmarks.html if \ michael@0: restore_default_bookmarks is true."); michael@0: michael@0: remove_all_bookmarks(); michael@0: // Sanity check: we should not have any bookmark on the toolbar. michael@0: let itemId = michael@0: PlacesUtils.bookmarks.getIdForItemAt(PlacesUtils.toolbarFolderId, 0); michael@0: do_check_eq(itemId, -1); michael@0: michael@0: // Set preferences. michael@0: Services.prefs.setBoolPref(PREF_RESTORE_DEFAULT_BOOKMARKS, true); michael@0: michael@0: waitForImportAndSmartBookmarks(function () { michael@0: // Check bookmarks.html has been restored. michael@0: itemId = michael@0: PlacesUtils.bookmarks.getIdForItemAt(PlacesUtils.toolbarFolderId, michael@0: SMART_BOOKMARKS_ON_TOOLBAR); michael@0: do_check_true(itemId > 0); michael@0: // Check preferences have been reverted. michael@0: do_check_false(Services.prefs.getBoolPref(PREF_RESTORE_DEFAULT_BOOKMARKS)); michael@0: michael@0: run_next_test(); michael@0: }); michael@0: // Force nsBrowserGlue::_initPlaces() michael@0: do_log_info("Simulate Places init"); michael@0: bg.QueryInterface(Ci.nsIObserver).observe(null, michael@0: TOPIC_BROWSERGLUE_TEST, michael@0: TOPICDATA_FORCE_PLACES_INIT); michael@0: michael@0: }, michael@0: michael@0: function test_restore_import() michael@0: { michael@0: do_log_info("setting both importBookmarksHTML and \ michael@0: restore_default_bookmarks should restore defaults."); michael@0: michael@0: remove_all_bookmarks(); michael@0: // Sanity check: we should not have any bookmark on the toolbar. michael@0: let itemId = michael@0: PlacesUtils.bookmarks.getIdForItemAt(PlacesUtils.toolbarFolderId, 0); michael@0: do_check_eq(itemId, -1); michael@0: michael@0: // Set preferences. michael@0: Services.prefs.setBoolPref(PREF_IMPORT_BOOKMARKS_HTML, true); michael@0: Services.prefs.setBoolPref(PREF_RESTORE_DEFAULT_BOOKMARKS, true); michael@0: michael@0: waitForImportAndSmartBookmarks(function () { michael@0: // Check bookmarks.html has been restored. michael@0: itemId = michael@0: PlacesUtils.bookmarks.getIdForItemAt(PlacesUtils.toolbarFolderId, michael@0: SMART_BOOKMARKS_ON_TOOLBAR); michael@0: do_check_true(itemId > 0); michael@0: // Check preferences have been reverted. michael@0: do_check_false(Services.prefs.getBoolPref(PREF_RESTORE_DEFAULT_BOOKMARKS)); michael@0: do_check_false(Services.prefs.getBoolPref(PREF_IMPORT_BOOKMARKS_HTML)); michael@0: michael@0: run_next_test(); michael@0: }); michael@0: // Force nsBrowserGlue::_initPlaces() michael@0: do_log_info("Simulate Places init"); michael@0: bg.QueryInterface(Ci.nsIObserver).observe(null, michael@0: TOPIC_BROWSERGLUE_TEST, michael@0: TOPICDATA_FORCE_PLACES_INIT); michael@0: } michael@0: michael@0: ].forEach(add_test); michael@0: michael@0: do_register_cleanup(function () { michael@0: remove_all_bookmarks(); michael@0: remove_bookmarks_html(); michael@0: remove_all_JSON_backups(); michael@0: }); michael@0: michael@0: function run_test() michael@0: { michael@0: // Create our bookmarks.html from bookmarks.glue.html. michael@0: create_bookmarks_html("bookmarks.glue.html"); michael@0: remove_all_JSON_backups(); michael@0: // Create our JSON backup from bookmarks.glue.json. michael@0: create_JSON_backup("bookmarks.glue.json"); michael@0: michael@0: run_next_test(); michael@0: }