1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/browser/components/places/tests/unit/test_browserGlue_prefs.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,283 @@ 1.4 +/* Any copyright is dedicated to the Public Domain. 1.5 + http://creativecommons.org/publicdomain/zero/1.0/ */ 1.6 + 1.7 +/** 1.8 + * Tests that nsBrowserGlue is correctly interpreting the preferences settable 1.9 + * by the user or by other components. 1.10 + */ 1.11 + 1.12 +const PREF_IMPORT_BOOKMARKS_HTML = "browser.places.importBookmarksHTML"; 1.13 +const PREF_RESTORE_DEFAULT_BOOKMARKS = "browser.bookmarks.restore_default_bookmarks"; 1.14 +const PREF_SMART_BOOKMARKS_VERSION = "browser.places.smartBookmarksVersion"; 1.15 +const PREF_AUTO_EXPORT_HTML = "browser.bookmarks.autoExportHTML"; 1.16 + 1.17 +const TOPIC_BROWSERGLUE_TEST = "browser-glue-test"; 1.18 +const TOPICDATA_FORCE_PLACES_INIT = "force-places-init"; 1.19 + 1.20 +let bg = Cc["@mozilla.org/browser/browserglue;1"]. 1.21 + getService(Ci.nsIBrowserGlue); 1.22 + 1.23 +function waitForImportAndSmartBookmarks(aCallback) { 1.24 + Services.obs.addObserver(function waitImport() { 1.25 + Services.obs.removeObserver(waitImport, "bookmarks-restore-success"); 1.26 + // Delay to test eventual smart bookmarks creation. 1.27 + do_execute_soon(function () { 1.28 + promiseAsyncUpdates().then(aCallback); 1.29 + }); 1.30 + }, "bookmarks-restore-success", false); 1.31 +} 1.32 + 1.33 +[ 1.34 + 1.35 + // This test must be the first one. 1.36 + function test_checkPreferences() { 1.37 + // Initialize Places through the History Service and check that a new 1.38 + // database has been created. 1.39 + do_check_eq(PlacesUtils.history.databaseStatus, 1.40 + PlacesUtils.history.DATABASE_STATUS_CREATE); 1.41 + 1.42 + // Wait for Places init notification. 1.43 + Services.obs.addObserver(function(aSubject, aTopic, aData) { 1.44 + Services.obs.removeObserver(arguments.callee, 1.45 + "places-browser-init-complete"); 1.46 + do_execute_soon(function () { 1.47 + // Ensure preferences status. 1.48 + do_check_false(Services.prefs.getBoolPref(PREF_AUTO_EXPORT_HTML)); 1.49 + 1.50 + try { 1.51 + do_check_false(Services.prefs.getBoolPref(PREF_IMPORT_BOOKMARKS_HTML)); 1.52 + do_throw("importBookmarksHTML pref should not exist"); 1.53 + } 1.54 + catch(ex) {} 1.55 + 1.56 + try { 1.57 + do_check_false(Services.prefs.getBoolPref(PREF_RESTORE_DEFAULT_BOOKMARKS)); 1.58 + do_throw("importBookmarksHTML pref should not exist"); 1.59 + } 1.60 + catch(ex) {} 1.61 + 1.62 + run_next_test(); 1.63 + }); 1.64 + }, "places-browser-init-complete", false); 1.65 + }, 1.66 + 1.67 + function test_import() 1.68 + { 1.69 + do_log_info("Import from bookmarks.html if importBookmarksHTML is true."); 1.70 + 1.71 + remove_all_bookmarks(); 1.72 + // Sanity check: we should not have any bookmark on the toolbar. 1.73 + let itemId = 1.74 + PlacesUtils.bookmarks.getIdForItemAt(PlacesUtils.toolbarFolderId, 0); 1.75 + do_check_eq(itemId, -1); 1.76 + 1.77 + // Set preferences. 1.78 + Services.prefs.setBoolPref(PREF_IMPORT_BOOKMARKS_HTML, true); 1.79 + 1.80 + waitForImportAndSmartBookmarks(function () { 1.81 + // Check bookmarks.html has been imported, and a smart bookmark has been 1.82 + // created. 1.83 + itemId = PlacesUtils.bookmarks.getIdForItemAt(PlacesUtils.toolbarFolderId, 1.84 + SMART_BOOKMARKS_ON_TOOLBAR); 1.85 + do_check_eq(PlacesUtils.bookmarks.getItemTitle(itemId), "example"); 1.86 + // Check preferences have been reverted. 1.87 + do_check_false(Services.prefs.getBoolPref(PREF_IMPORT_BOOKMARKS_HTML)); 1.88 + 1.89 + run_next_test(); 1.90 + }); 1.91 + // Force nsBrowserGlue::_initPlaces(). 1.92 + do_log_info("Simulate Places init"); 1.93 + bg.QueryInterface(Ci.nsIObserver).observe(null, 1.94 + TOPIC_BROWSERGLUE_TEST, 1.95 + TOPICDATA_FORCE_PLACES_INIT); 1.96 + }, 1.97 + 1.98 + function test_import_noSmartBookmarks() 1.99 + { 1.100 + do_log_info("import from bookmarks.html, but don't create smart bookmarks \ 1.101 + if they are disabled"); 1.102 + 1.103 + remove_all_bookmarks(); 1.104 + // Sanity check: we should not have any bookmark on the toolbar. 1.105 + let itemId = 1.106 + PlacesUtils.bookmarks.getIdForItemAt(PlacesUtils.toolbarFolderId, 0); 1.107 + do_check_eq(itemId, -1); 1.108 + 1.109 + // Set preferences. 1.110 + Services.prefs.setIntPref(PREF_SMART_BOOKMARKS_VERSION, -1); 1.111 + Services.prefs.setBoolPref(PREF_IMPORT_BOOKMARKS_HTML, true); 1.112 + 1.113 + waitForImportAndSmartBookmarks(function () { 1.114 + // Check bookmarks.html has been imported, but smart bookmarks have not 1.115 + // been created. 1.116 + itemId = 1.117 + PlacesUtils.bookmarks.getIdForItemAt(PlacesUtils.toolbarFolderId, 0); 1.118 + do_check_eq(PlacesUtils.bookmarks.getItemTitle(itemId), "example"); 1.119 + // Check preferences have been reverted. 1.120 + do_check_false(Services.prefs.getBoolPref(PREF_IMPORT_BOOKMARKS_HTML)); 1.121 + 1.122 + run_next_test(); 1.123 + }); 1.124 + // Force nsBrowserGlue::_initPlaces(). 1.125 + do_log_info("Simulate Places init"); 1.126 + bg.QueryInterface(Ci.nsIObserver).observe(null, 1.127 + TOPIC_BROWSERGLUE_TEST, 1.128 + TOPICDATA_FORCE_PLACES_INIT); 1.129 + }, 1.130 + 1.131 + function test_import_autoExport_updatedSmartBookmarks() 1.132 + { 1.133 + do_log_info("Import from bookmarks.html, but don't create smart bookmarks \ 1.134 + if autoExportHTML is true and they are at latest version"); 1.135 + 1.136 + remove_all_bookmarks(); 1.137 + // Sanity check: we should not have any bookmark on the toolbar. 1.138 + let itemId = 1.139 + PlacesUtils.bookmarks.getIdForItemAt(PlacesUtils.toolbarFolderId, 0); 1.140 + do_check_eq(itemId, -1); 1.141 + 1.142 + // Set preferences. 1.143 + Services.prefs.setIntPref(PREF_SMART_BOOKMARKS_VERSION, 999); 1.144 + Services.prefs.setBoolPref(PREF_AUTO_EXPORT_HTML, true); 1.145 + Services.prefs.setBoolPref(PREF_IMPORT_BOOKMARKS_HTML, true); 1.146 + 1.147 + waitForImportAndSmartBookmarks(function () { 1.148 + // Check bookmarks.html has been imported, but smart bookmarks have not 1.149 + // been created. 1.150 + itemId = 1.151 + PlacesUtils.bookmarks.getIdForItemAt(PlacesUtils.toolbarFolderId, 0); 1.152 + do_check_eq(PlacesUtils.bookmarks.getItemTitle(itemId), "example"); 1.153 + do_check_false(Services.prefs.getBoolPref(PREF_IMPORT_BOOKMARKS_HTML)); 1.154 + // Check preferences have been reverted. 1.155 + Services.prefs.setBoolPref(PREF_AUTO_EXPORT_HTML, false); 1.156 + 1.157 + run_next_test(); 1.158 + }); 1.159 + // Force nsBrowserGlue::_initPlaces() 1.160 + do_log_info("Simulate Places init"); 1.161 + bg.QueryInterface(Ci.nsIObserver).observe(null, 1.162 + TOPIC_BROWSERGLUE_TEST, 1.163 + TOPICDATA_FORCE_PLACES_INIT); 1.164 + }, 1.165 + 1.166 + function test_import_autoExport_oldSmartBookmarks() 1.167 + { 1.168 + do_log_info("Import from bookmarks.html, and create smart bookmarks if \ 1.169 + autoExportHTML is true and they are not at latest version."); 1.170 + 1.171 + remove_all_bookmarks(); 1.172 + // Sanity check: we should not have any bookmark on the toolbar. 1.173 + let itemId = 1.174 + PlacesUtils.bookmarks.getIdForItemAt(PlacesUtils.toolbarFolderId, 0); 1.175 + do_check_eq(itemId, -1); 1.176 + 1.177 + // Set preferences. 1.178 + Services.prefs.setIntPref(PREF_SMART_BOOKMARKS_VERSION, 0); 1.179 + Services.prefs.setBoolPref(PREF_AUTO_EXPORT_HTML, true); 1.180 + Services.prefs.setBoolPref(PREF_IMPORT_BOOKMARKS_HTML, true); 1.181 + 1.182 + waitForImportAndSmartBookmarks(function () { 1.183 + // Check bookmarks.html has been imported, but smart bookmarks have not 1.184 + // been created. 1.185 + itemId = 1.186 + PlacesUtils.bookmarks.getIdForItemAt(PlacesUtils.toolbarFolderId, 1.187 + SMART_BOOKMARKS_ON_TOOLBAR); 1.188 + do_check_eq(PlacesUtils.bookmarks.getItemTitle(itemId), "example"); 1.189 + do_check_false(Services.prefs.getBoolPref(PREF_IMPORT_BOOKMARKS_HTML)); 1.190 + // Check preferences have been reverted. 1.191 + Services.prefs.setBoolPref(PREF_AUTO_EXPORT_HTML, false); 1.192 + 1.193 + run_next_test(); 1.194 + }); 1.195 + // Force nsBrowserGlue::_initPlaces() 1.196 + do_log_info("Simulate Places init"); 1.197 + bg.QueryInterface(Ci.nsIObserver).observe(null, 1.198 + TOPIC_BROWSERGLUE_TEST, 1.199 + TOPICDATA_FORCE_PLACES_INIT); 1.200 + }, 1.201 + 1.202 + function test_restore() 1.203 + { 1.204 + do_log_info("restore from default bookmarks.html if \ 1.205 + restore_default_bookmarks is true."); 1.206 + 1.207 + remove_all_bookmarks(); 1.208 + // Sanity check: we should not have any bookmark on the toolbar. 1.209 + let itemId = 1.210 + PlacesUtils.bookmarks.getIdForItemAt(PlacesUtils.toolbarFolderId, 0); 1.211 + do_check_eq(itemId, -1); 1.212 + 1.213 + // Set preferences. 1.214 + Services.prefs.setBoolPref(PREF_RESTORE_DEFAULT_BOOKMARKS, true); 1.215 + 1.216 + waitForImportAndSmartBookmarks(function () { 1.217 + // Check bookmarks.html has been restored. 1.218 + itemId = 1.219 + PlacesUtils.bookmarks.getIdForItemAt(PlacesUtils.toolbarFolderId, 1.220 + SMART_BOOKMARKS_ON_TOOLBAR); 1.221 + do_check_true(itemId > 0); 1.222 + // Check preferences have been reverted. 1.223 + do_check_false(Services.prefs.getBoolPref(PREF_RESTORE_DEFAULT_BOOKMARKS)); 1.224 + 1.225 + run_next_test(); 1.226 + }); 1.227 + // Force nsBrowserGlue::_initPlaces() 1.228 + do_log_info("Simulate Places init"); 1.229 + bg.QueryInterface(Ci.nsIObserver).observe(null, 1.230 + TOPIC_BROWSERGLUE_TEST, 1.231 + TOPICDATA_FORCE_PLACES_INIT); 1.232 + 1.233 + }, 1.234 + 1.235 + function test_restore_import() 1.236 + { 1.237 + do_log_info("setting both importBookmarksHTML and \ 1.238 + restore_default_bookmarks should restore defaults."); 1.239 + 1.240 + remove_all_bookmarks(); 1.241 + // Sanity check: we should not have any bookmark on the toolbar. 1.242 + let itemId = 1.243 + PlacesUtils.bookmarks.getIdForItemAt(PlacesUtils.toolbarFolderId, 0); 1.244 + do_check_eq(itemId, -1); 1.245 + 1.246 + // Set preferences. 1.247 + Services.prefs.setBoolPref(PREF_IMPORT_BOOKMARKS_HTML, true); 1.248 + Services.prefs.setBoolPref(PREF_RESTORE_DEFAULT_BOOKMARKS, true); 1.249 + 1.250 + waitForImportAndSmartBookmarks(function () { 1.251 + // Check bookmarks.html has been restored. 1.252 + itemId = 1.253 + PlacesUtils.bookmarks.getIdForItemAt(PlacesUtils.toolbarFolderId, 1.254 + SMART_BOOKMARKS_ON_TOOLBAR); 1.255 + do_check_true(itemId > 0); 1.256 + // Check preferences have been reverted. 1.257 + do_check_false(Services.prefs.getBoolPref(PREF_RESTORE_DEFAULT_BOOKMARKS)); 1.258 + do_check_false(Services.prefs.getBoolPref(PREF_IMPORT_BOOKMARKS_HTML)); 1.259 + 1.260 + run_next_test(); 1.261 + }); 1.262 + // Force nsBrowserGlue::_initPlaces() 1.263 + do_log_info("Simulate Places init"); 1.264 + bg.QueryInterface(Ci.nsIObserver).observe(null, 1.265 + TOPIC_BROWSERGLUE_TEST, 1.266 + TOPICDATA_FORCE_PLACES_INIT); 1.267 + } 1.268 + 1.269 +].forEach(add_test); 1.270 + 1.271 +do_register_cleanup(function () { 1.272 + remove_all_bookmarks(); 1.273 + remove_bookmarks_html(); 1.274 + remove_all_JSON_backups(); 1.275 +}); 1.276 + 1.277 +function run_test() 1.278 +{ 1.279 + // Create our bookmarks.html from bookmarks.glue.html. 1.280 + create_bookmarks_html("bookmarks.glue.html"); 1.281 + remove_all_JSON_backups(); 1.282 + // Create our JSON backup from bookmarks.glue.json. 1.283 + create_JSON_backup("bookmarks.glue.json"); 1.284 + 1.285 + run_next_test(); 1.286 +}