browser/components/places/tests/unit/test_browserGlue_prefs.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* Any copyright is dedicated to the Public Domain.
michael@0 2 http://creativecommons.org/publicdomain/zero/1.0/ */
michael@0 3
michael@0 4 /**
michael@0 5 * Tests that nsBrowserGlue is correctly interpreting the preferences settable
michael@0 6 * by the user or by other components.
michael@0 7 */
michael@0 8
michael@0 9 const PREF_IMPORT_BOOKMARKS_HTML = "browser.places.importBookmarksHTML";
michael@0 10 const PREF_RESTORE_DEFAULT_BOOKMARKS = "browser.bookmarks.restore_default_bookmarks";
michael@0 11 const PREF_SMART_BOOKMARKS_VERSION = "browser.places.smartBookmarksVersion";
michael@0 12 const PREF_AUTO_EXPORT_HTML = "browser.bookmarks.autoExportHTML";
michael@0 13
michael@0 14 const TOPIC_BROWSERGLUE_TEST = "browser-glue-test";
michael@0 15 const TOPICDATA_FORCE_PLACES_INIT = "force-places-init";
michael@0 16
michael@0 17 let bg = Cc["@mozilla.org/browser/browserglue;1"].
michael@0 18 getService(Ci.nsIBrowserGlue);
michael@0 19
michael@0 20 function waitForImportAndSmartBookmarks(aCallback) {
michael@0 21 Services.obs.addObserver(function waitImport() {
michael@0 22 Services.obs.removeObserver(waitImport, "bookmarks-restore-success");
michael@0 23 // Delay to test eventual smart bookmarks creation.
michael@0 24 do_execute_soon(function () {
michael@0 25 promiseAsyncUpdates().then(aCallback);
michael@0 26 });
michael@0 27 }, "bookmarks-restore-success", false);
michael@0 28 }
michael@0 29
michael@0 30 [
michael@0 31
michael@0 32 // This test must be the first one.
michael@0 33 function test_checkPreferences() {
michael@0 34 // Initialize Places through the History Service and check that a new
michael@0 35 // database has been created.
michael@0 36 do_check_eq(PlacesUtils.history.databaseStatus,
michael@0 37 PlacesUtils.history.DATABASE_STATUS_CREATE);
michael@0 38
michael@0 39 // Wait for Places init notification.
michael@0 40 Services.obs.addObserver(function(aSubject, aTopic, aData) {
michael@0 41 Services.obs.removeObserver(arguments.callee,
michael@0 42 "places-browser-init-complete");
michael@0 43 do_execute_soon(function () {
michael@0 44 // Ensure preferences status.
michael@0 45 do_check_false(Services.prefs.getBoolPref(PREF_AUTO_EXPORT_HTML));
michael@0 46
michael@0 47 try {
michael@0 48 do_check_false(Services.prefs.getBoolPref(PREF_IMPORT_BOOKMARKS_HTML));
michael@0 49 do_throw("importBookmarksHTML pref should not exist");
michael@0 50 }
michael@0 51 catch(ex) {}
michael@0 52
michael@0 53 try {
michael@0 54 do_check_false(Services.prefs.getBoolPref(PREF_RESTORE_DEFAULT_BOOKMARKS));
michael@0 55 do_throw("importBookmarksHTML pref should not exist");
michael@0 56 }
michael@0 57 catch(ex) {}
michael@0 58
michael@0 59 run_next_test();
michael@0 60 });
michael@0 61 }, "places-browser-init-complete", false);
michael@0 62 },
michael@0 63
michael@0 64 function test_import()
michael@0 65 {
michael@0 66 do_log_info("Import from bookmarks.html if importBookmarksHTML is true.");
michael@0 67
michael@0 68 remove_all_bookmarks();
michael@0 69 // Sanity check: we should not have any bookmark on the toolbar.
michael@0 70 let itemId =
michael@0 71 PlacesUtils.bookmarks.getIdForItemAt(PlacesUtils.toolbarFolderId, 0);
michael@0 72 do_check_eq(itemId, -1);
michael@0 73
michael@0 74 // Set preferences.
michael@0 75 Services.prefs.setBoolPref(PREF_IMPORT_BOOKMARKS_HTML, true);
michael@0 76
michael@0 77 waitForImportAndSmartBookmarks(function () {
michael@0 78 // Check bookmarks.html has been imported, and a smart bookmark has been
michael@0 79 // created.
michael@0 80 itemId = PlacesUtils.bookmarks.getIdForItemAt(PlacesUtils.toolbarFolderId,
michael@0 81 SMART_BOOKMARKS_ON_TOOLBAR);
michael@0 82 do_check_eq(PlacesUtils.bookmarks.getItemTitle(itemId), "example");
michael@0 83 // Check preferences have been reverted.
michael@0 84 do_check_false(Services.prefs.getBoolPref(PREF_IMPORT_BOOKMARKS_HTML));
michael@0 85
michael@0 86 run_next_test();
michael@0 87 });
michael@0 88 // Force nsBrowserGlue::_initPlaces().
michael@0 89 do_log_info("Simulate Places init");
michael@0 90 bg.QueryInterface(Ci.nsIObserver).observe(null,
michael@0 91 TOPIC_BROWSERGLUE_TEST,
michael@0 92 TOPICDATA_FORCE_PLACES_INIT);
michael@0 93 },
michael@0 94
michael@0 95 function test_import_noSmartBookmarks()
michael@0 96 {
michael@0 97 do_log_info("import from bookmarks.html, but don't create smart bookmarks \
michael@0 98 if they are disabled");
michael@0 99
michael@0 100 remove_all_bookmarks();
michael@0 101 // Sanity check: we should not have any bookmark on the toolbar.
michael@0 102 let itemId =
michael@0 103 PlacesUtils.bookmarks.getIdForItemAt(PlacesUtils.toolbarFolderId, 0);
michael@0 104 do_check_eq(itemId, -1);
michael@0 105
michael@0 106 // Set preferences.
michael@0 107 Services.prefs.setIntPref(PREF_SMART_BOOKMARKS_VERSION, -1);
michael@0 108 Services.prefs.setBoolPref(PREF_IMPORT_BOOKMARKS_HTML, true);
michael@0 109
michael@0 110 waitForImportAndSmartBookmarks(function () {
michael@0 111 // Check bookmarks.html has been imported, but smart bookmarks have not
michael@0 112 // been created.
michael@0 113 itemId =
michael@0 114 PlacesUtils.bookmarks.getIdForItemAt(PlacesUtils.toolbarFolderId, 0);
michael@0 115 do_check_eq(PlacesUtils.bookmarks.getItemTitle(itemId), "example");
michael@0 116 // Check preferences have been reverted.
michael@0 117 do_check_false(Services.prefs.getBoolPref(PREF_IMPORT_BOOKMARKS_HTML));
michael@0 118
michael@0 119 run_next_test();
michael@0 120 });
michael@0 121 // Force nsBrowserGlue::_initPlaces().
michael@0 122 do_log_info("Simulate Places init");
michael@0 123 bg.QueryInterface(Ci.nsIObserver).observe(null,
michael@0 124 TOPIC_BROWSERGLUE_TEST,
michael@0 125 TOPICDATA_FORCE_PLACES_INIT);
michael@0 126 },
michael@0 127
michael@0 128 function test_import_autoExport_updatedSmartBookmarks()
michael@0 129 {
michael@0 130 do_log_info("Import from bookmarks.html, but don't create smart bookmarks \
michael@0 131 if autoExportHTML is true and they are at latest version");
michael@0 132
michael@0 133 remove_all_bookmarks();
michael@0 134 // Sanity check: we should not have any bookmark on the toolbar.
michael@0 135 let itemId =
michael@0 136 PlacesUtils.bookmarks.getIdForItemAt(PlacesUtils.toolbarFolderId, 0);
michael@0 137 do_check_eq(itemId, -1);
michael@0 138
michael@0 139 // Set preferences.
michael@0 140 Services.prefs.setIntPref(PREF_SMART_BOOKMARKS_VERSION, 999);
michael@0 141 Services.prefs.setBoolPref(PREF_AUTO_EXPORT_HTML, true);
michael@0 142 Services.prefs.setBoolPref(PREF_IMPORT_BOOKMARKS_HTML, true);
michael@0 143
michael@0 144 waitForImportAndSmartBookmarks(function () {
michael@0 145 // Check bookmarks.html has been imported, but smart bookmarks have not
michael@0 146 // been created.
michael@0 147 itemId =
michael@0 148 PlacesUtils.bookmarks.getIdForItemAt(PlacesUtils.toolbarFolderId, 0);
michael@0 149 do_check_eq(PlacesUtils.bookmarks.getItemTitle(itemId), "example");
michael@0 150 do_check_false(Services.prefs.getBoolPref(PREF_IMPORT_BOOKMARKS_HTML));
michael@0 151 // Check preferences have been reverted.
michael@0 152 Services.prefs.setBoolPref(PREF_AUTO_EXPORT_HTML, false);
michael@0 153
michael@0 154 run_next_test();
michael@0 155 });
michael@0 156 // Force nsBrowserGlue::_initPlaces()
michael@0 157 do_log_info("Simulate Places init");
michael@0 158 bg.QueryInterface(Ci.nsIObserver).observe(null,
michael@0 159 TOPIC_BROWSERGLUE_TEST,
michael@0 160 TOPICDATA_FORCE_PLACES_INIT);
michael@0 161 },
michael@0 162
michael@0 163 function test_import_autoExport_oldSmartBookmarks()
michael@0 164 {
michael@0 165 do_log_info("Import from bookmarks.html, and create smart bookmarks if \
michael@0 166 autoExportHTML is true and they are not at latest version.");
michael@0 167
michael@0 168 remove_all_bookmarks();
michael@0 169 // Sanity check: we should not have any bookmark on the toolbar.
michael@0 170 let itemId =
michael@0 171 PlacesUtils.bookmarks.getIdForItemAt(PlacesUtils.toolbarFolderId, 0);
michael@0 172 do_check_eq(itemId, -1);
michael@0 173
michael@0 174 // Set preferences.
michael@0 175 Services.prefs.setIntPref(PREF_SMART_BOOKMARKS_VERSION, 0);
michael@0 176 Services.prefs.setBoolPref(PREF_AUTO_EXPORT_HTML, true);
michael@0 177 Services.prefs.setBoolPref(PREF_IMPORT_BOOKMARKS_HTML, true);
michael@0 178
michael@0 179 waitForImportAndSmartBookmarks(function () {
michael@0 180 // Check bookmarks.html has been imported, but smart bookmarks have not
michael@0 181 // been created.
michael@0 182 itemId =
michael@0 183 PlacesUtils.bookmarks.getIdForItemAt(PlacesUtils.toolbarFolderId,
michael@0 184 SMART_BOOKMARKS_ON_TOOLBAR);
michael@0 185 do_check_eq(PlacesUtils.bookmarks.getItemTitle(itemId), "example");
michael@0 186 do_check_false(Services.prefs.getBoolPref(PREF_IMPORT_BOOKMARKS_HTML));
michael@0 187 // Check preferences have been reverted.
michael@0 188 Services.prefs.setBoolPref(PREF_AUTO_EXPORT_HTML, false);
michael@0 189
michael@0 190 run_next_test();
michael@0 191 });
michael@0 192 // Force nsBrowserGlue::_initPlaces()
michael@0 193 do_log_info("Simulate Places init");
michael@0 194 bg.QueryInterface(Ci.nsIObserver).observe(null,
michael@0 195 TOPIC_BROWSERGLUE_TEST,
michael@0 196 TOPICDATA_FORCE_PLACES_INIT);
michael@0 197 },
michael@0 198
michael@0 199 function test_restore()
michael@0 200 {
michael@0 201 do_log_info("restore from default bookmarks.html if \
michael@0 202 restore_default_bookmarks is true.");
michael@0 203
michael@0 204 remove_all_bookmarks();
michael@0 205 // Sanity check: we should not have any bookmark on the toolbar.
michael@0 206 let itemId =
michael@0 207 PlacesUtils.bookmarks.getIdForItemAt(PlacesUtils.toolbarFolderId, 0);
michael@0 208 do_check_eq(itemId, -1);
michael@0 209
michael@0 210 // Set preferences.
michael@0 211 Services.prefs.setBoolPref(PREF_RESTORE_DEFAULT_BOOKMARKS, true);
michael@0 212
michael@0 213 waitForImportAndSmartBookmarks(function () {
michael@0 214 // Check bookmarks.html has been restored.
michael@0 215 itemId =
michael@0 216 PlacesUtils.bookmarks.getIdForItemAt(PlacesUtils.toolbarFolderId,
michael@0 217 SMART_BOOKMARKS_ON_TOOLBAR);
michael@0 218 do_check_true(itemId > 0);
michael@0 219 // Check preferences have been reverted.
michael@0 220 do_check_false(Services.prefs.getBoolPref(PREF_RESTORE_DEFAULT_BOOKMARKS));
michael@0 221
michael@0 222 run_next_test();
michael@0 223 });
michael@0 224 // Force nsBrowserGlue::_initPlaces()
michael@0 225 do_log_info("Simulate Places init");
michael@0 226 bg.QueryInterface(Ci.nsIObserver).observe(null,
michael@0 227 TOPIC_BROWSERGLUE_TEST,
michael@0 228 TOPICDATA_FORCE_PLACES_INIT);
michael@0 229
michael@0 230 },
michael@0 231
michael@0 232 function test_restore_import()
michael@0 233 {
michael@0 234 do_log_info("setting both importBookmarksHTML and \
michael@0 235 restore_default_bookmarks should restore defaults.");
michael@0 236
michael@0 237 remove_all_bookmarks();
michael@0 238 // Sanity check: we should not have any bookmark on the toolbar.
michael@0 239 let itemId =
michael@0 240 PlacesUtils.bookmarks.getIdForItemAt(PlacesUtils.toolbarFolderId, 0);
michael@0 241 do_check_eq(itemId, -1);
michael@0 242
michael@0 243 // Set preferences.
michael@0 244 Services.prefs.setBoolPref(PREF_IMPORT_BOOKMARKS_HTML, true);
michael@0 245 Services.prefs.setBoolPref(PREF_RESTORE_DEFAULT_BOOKMARKS, true);
michael@0 246
michael@0 247 waitForImportAndSmartBookmarks(function () {
michael@0 248 // Check bookmarks.html has been restored.
michael@0 249 itemId =
michael@0 250 PlacesUtils.bookmarks.getIdForItemAt(PlacesUtils.toolbarFolderId,
michael@0 251 SMART_BOOKMARKS_ON_TOOLBAR);
michael@0 252 do_check_true(itemId > 0);
michael@0 253 // Check preferences have been reverted.
michael@0 254 do_check_false(Services.prefs.getBoolPref(PREF_RESTORE_DEFAULT_BOOKMARKS));
michael@0 255 do_check_false(Services.prefs.getBoolPref(PREF_IMPORT_BOOKMARKS_HTML));
michael@0 256
michael@0 257 run_next_test();
michael@0 258 });
michael@0 259 // Force nsBrowserGlue::_initPlaces()
michael@0 260 do_log_info("Simulate Places init");
michael@0 261 bg.QueryInterface(Ci.nsIObserver).observe(null,
michael@0 262 TOPIC_BROWSERGLUE_TEST,
michael@0 263 TOPICDATA_FORCE_PLACES_INIT);
michael@0 264 }
michael@0 265
michael@0 266 ].forEach(add_test);
michael@0 267
michael@0 268 do_register_cleanup(function () {
michael@0 269 remove_all_bookmarks();
michael@0 270 remove_bookmarks_html();
michael@0 271 remove_all_JSON_backups();
michael@0 272 });
michael@0 273
michael@0 274 function run_test()
michael@0 275 {
michael@0 276 // Create our bookmarks.html from bookmarks.glue.html.
michael@0 277 create_bookmarks_html("bookmarks.glue.html");
michael@0 278 remove_all_JSON_backups();
michael@0 279 // Create our JSON backup from bookmarks.glue.json.
michael@0 280 create_JSON_backup("bookmarks.glue.json");
michael@0 281
michael@0 282 run_next_test();
michael@0 283 }

mercurial