browser/components/places/tests/unit/test_browserGlue_smartBookmarks.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 /* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* vim:set ts=2 sw=2 sts=2 et: */
michael@0 3 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 4 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 6
michael@0 7 /**
michael@0 8 * Tests that nsBrowserGlue is correctly interpreting the preferences settable
michael@0 9 * by the user or by other components.
michael@0 10 */
michael@0 11
michael@0 12 const PREF_SMART_BOOKMARKS_VERSION = "browser.places.smartBookmarksVersion";
michael@0 13 const PREF_AUTO_EXPORT_HTML = "browser.bookmarks.autoExportHTML";
michael@0 14 const PREF_IMPORT_BOOKMARKS_HTML = "browser.places.importBookmarksHTML";
michael@0 15 const PREF_RESTORE_DEFAULT_BOOKMARKS = "browser.bookmarks.restore_default_bookmarks";
michael@0 16
michael@0 17 const SMART_BOOKMARKS_ANNO = "Places/SmartBookmark";
michael@0 18
michael@0 19 /**
michael@0 20 * Rebuilds smart bookmarks listening to console output to report any message or
michael@0 21 * exception generated when calling ensurePlacesDefaultQueriesInitialized().
michael@0 22 */
michael@0 23 function rebuildSmartBookmarks() {
michael@0 24 let consoleListener = {
michael@0 25 observe: function(aMsg) {
michael@0 26 print("Got console message: " + aMsg.message);
michael@0 27 },
michael@0 28
michael@0 29 QueryInterface: XPCOMUtils.generateQI([
michael@0 30 Ci.nsIConsoleListener
michael@0 31 ]),
michael@0 32 };
michael@0 33 Services.console.reset();
michael@0 34 Services.console.registerListener(consoleListener);
michael@0 35 Cc["@mozilla.org/browser/browserglue;1"].getService(Ci.nsIBrowserGlue)
michael@0 36 .ensurePlacesDefaultQueriesInitialized();
michael@0 37 Services.console.unregisterListener(consoleListener);
michael@0 38 }
michael@0 39
michael@0 40
michael@0 41 let tests = [];
michael@0 42 //------------------------------------------------------------------------------
michael@0 43
michael@0 44 tests.push({
michael@0 45 description: "All smart bookmarks are created if smart bookmarks version is 0.",
michael@0 46 exec: function() {
michael@0 47 // Sanity check: we should have default bookmark.
michael@0 48 do_check_neq(PlacesUtils.bookmarks.getIdForItemAt(PlacesUtils.toolbarFolderId, 0), -1);
michael@0 49 do_check_neq(PlacesUtils.bookmarks.getIdForItemAt(PlacesUtils.bookmarksMenuFolderId, 0), -1);
michael@0 50
michael@0 51 // Set preferences.
michael@0 52 Services.prefs.setIntPref(PREF_SMART_BOOKMARKS_VERSION, 0);
michael@0 53
michael@0 54 rebuildSmartBookmarks();
michael@0 55
michael@0 56 // Count items.
michael@0 57 do_check_eq(countFolderChildren(PlacesUtils.toolbarFolderId),
michael@0 58 SMART_BOOKMARKS_ON_TOOLBAR + DEFAULT_BOOKMARKS_ON_TOOLBAR);
michael@0 59 do_check_eq(countFolderChildren(PlacesUtils.bookmarksMenuFolderId),
michael@0 60 SMART_BOOKMARKS_ON_MENU + DEFAULT_BOOKMARKS_ON_MENU);
michael@0 61
michael@0 62 // Check version has been updated.
michael@0 63 do_check_eq(Services.prefs.getIntPref(PREF_SMART_BOOKMARKS_VERSION),
michael@0 64 SMART_BOOKMARKS_VERSION);
michael@0 65
michael@0 66 next_test();
michael@0 67 }
michael@0 68 });
michael@0 69
michael@0 70 //------------------------------------------------------------------------------
michael@0 71
michael@0 72 tests.push({
michael@0 73 description: "An existing smart bookmark is replaced when version changes.",
michael@0 74 exec: function() {
michael@0 75 // Sanity check: we have a smart bookmark on the toolbar.
michael@0 76 let itemId = PlacesUtils.bookmarks.getIdForItemAt(PlacesUtils.toolbarFolderId, 0);
michael@0 77 do_check_neq(itemId, -1);
michael@0 78 do_check_true(PlacesUtils.annotations.itemHasAnnotation(itemId, SMART_BOOKMARKS_ANNO));
michael@0 79 // Change its title.
michael@0 80 PlacesUtils.bookmarks.setItemTitle(itemId, "new title");
michael@0 81 do_check_eq(PlacesUtils.bookmarks.getItemTitle(itemId), "new title");
michael@0 82
michael@0 83 // Sanity check items.
michael@0 84 do_check_eq(countFolderChildren(PlacesUtils.toolbarFolderId),
michael@0 85 SMART_BOOKMARKS_ON_TOOLBAR + DEFAULT_BOOKMARKS_ON_TOOLBAR);
michael@0 86 do_check_eq(countFolderChildren(PlacesUtils.bookmarksMenuFolderId),
michael@0 87 SMART_BOOKMARKS_ON_MENU + DEFAULT_BOOKMARKS_ON_MENU);
michael@0 88
michael@0 89 // Set preferences.
michael@0 90 Services.prefs.setIntPref(PREF_SMART_BOOKMARKS_VERSION, 1);
michael@0 91
michael@0 92 rebuildSmartBookmarks();
michael@0 93
michael@0 94 // Count items.
michael@0 95 do_check_eq(countFolderChildren(PlacesUtils.toolbarFolderId),
michael@0 96 SMART_BOOKMARKS_ON_TOOLBAR + DEFAULT_BOOKMARKS_ON_TOOLBAR);
michael@0 97 do_check_eq(countFolderChildren(PlacesUtils.bookmarksMenuFolderId),
michael@0 98 SMART_BOOKMARKS_ON_MENU + DEFAULT_BOOKMARKS_ON_MENU);
michael@0 99
michael@0 100 // Check smart bookmark has been replaced, itemId has changed.
michael@0 101 itemId = PlacesUtils.bookmarks.getIdForItemAt(PlacesUtils.toolbarFolderId, 0);
michael@0 102 do_check_neq(itemId, -1);
michael@0 103 do_check_neq(PlacesUtils.bookmarks.getItemTitle(itemId), "new title");
michael@0 104 do_check_true(PlacesUtils.annotations.itemHasAnnotation(itemId, SMART_BOOKMARKS_ANNO));
michael@0 105
michael@0 106 // Check version has been updated.
michael@0 107 do_check_eq(Services.prefs.getIntPref(PREF_SMART_BOOKMARKS_VERSION),
michael@0 108 SMART_BOOKMARKS_VERSION);
michael@0 109
michael@0 110 next_test();
michael@0 111 }
michael@0 112 });
michael@0 113
michael@0 114 //------------------------------------------------------------------------------
michael@0 115
michael@0 116 tests.push({
michael@0 117 description: "bookmarks position is retained when version changes.",
michael@0 118 exec: function() {
michael@0 119 // Sanity check items.
michael@0 120 do_check_eq(countFolderChildren(PlacesUtils.toolbarFolderId),
michael@0 121 SMART_BOOKMARKS_ON_TOOLBAR + DEFAULT_BOOKMARKS_ON_TOOLBAR);
michael@0 122 do_check_eq(countFolderChildren(PlacesUtils.bookmarksMenuFolderId),
michael@0 123 SMART_BOOKMARKS_ON_MENU + DEFAULT_BOOKMARKS_ON_MENU);
michael@0 124
michael@0 125 let itemId = PlacesUtils.bookmarks.getIdForItemAt(PlacesUtils.bookmarksMenuFolderId, 0);
michael@0 126 do_check_true(PlacesUtils.annotations.itemHasAnnotation(itemId, SMART_BOOKMARKS_ANNO));
michael@0 127 let firstItemTitle = PlacesUtils.bookmarks.getItemTitle(itemId);
michael@0 128
michael@0 129 itemId = PlacesUtils.bookmarks.getIdForItemAt(PlacesUtils.bookmarksMenuFolderId, 1);
michael@0 130 do_check_true(PlacesUtils.annotations.itemHasAnnotation(itemId, SMART_BOOKMARKS_ANNO));
michael@0 131 let secondItemTitle = PlacesUtils.bookmarks.getItemTitle(itemId);
michael@0 132
michael@0 133 // Set preferences.
michael@0 134 Services.prefs.setIntPref(PREF_SMART_BOOKMARKS_VERSION, 1);
michael@0 135
michael@0 136 rebuildSmartBookmarks();
michael@0 137
michael@0 138 // Count items.
michael@0 139 do_check_eq(countFolderChildren(PlacesUtils.toolbarFolderId),
michael@0 140 SMART_BOOKMARKS_ON_TOOLBAR + DEFAULT_BOOKMARKS_ON_TOOLBAR);
michael@0 141 do_check_eq(countFolderChildren(PlacesUtils.bookmarksMenuFolderId),
michael@0 142 SMART_BOOKMARKS_ON_MENU + DEFAULT_BOOKMARKS_ON_MENU);
michael@0 143
michael@0 144 // Check smart bookmarks are still in correct position.
michael@0 145 itemId = PlacesUtils.bookmarks.getIdForItemAt(PlacesUtils.bookmarksMenuFolderId, 0);
michael@0 146 do_check_true(PlacesUtils.annotations.itemHasAnnotation(itemId, SMART_BOOKMARKS_ANNO));
michael@0 147 do_check_eq(PlacesUtils.bookmarks.getItemTitle(itemId), firstItemTitle);
michael@0 148
michael@0 149 itemId = PlacesUtils.bookmarks.getIdForItemAt(PlacesUtils.bookmarksMenuFolderId, 1);
michael@0 150 do_check_true(PlacesUtils.annotations.itemHasAnnotation(itemId, SMART_BOOKMARKS_ANNO));
michael@0 151 do_check_eq(PlacesUtils.bookmarks.getItemTitle(itemId), secondItemTitle);
michael@0 152
michael@0 153 // Check version has been updated.
michael@0 154 do_check_eq(Services.prefs.getIntPref(PREF_SMART_BOOKMARKS_VERSION),
michael@0 155 SMART_BOOKMARKS_VERSION);
michael@0 156
michael@0 157 next_test();
michael@0 158 }
michael@0 159 });
michael@0 160
michael@0 161 //------------------------------------------------------------------------------
michael@0 162
michael@0 163 tests.push({
michael@0 164 description: "moved bookmarks position is retained when version changes.",
michael@0 165 exec: function() {
michael@0 166 // Sanity check items.
michael@0 167 do_check_eq(countFolderChildren(PlacesUtils.toolbarFolderId),
michael@0 168 SMART_BOOKMARKS_ON_TOOLBAR + DEFAULT_BOOKMARKS_ON_TOOLBAR);
michael@0 169 do_check_eq(countFolderChildren(PlacesUtils.bookmarksMenuFolderId),
michael@0 170 SMART_BOOKMARKS_ON_MENU + DEFAULT_BOOKMARKS_ON_MENU);
michael@0 171
michael@0 172 let itemId1 = PlacesUtils.bookmarks.getIdForItemAt(PlacesUtils.bookmarksMenuFolderId, 0);
michael@0 173 do_check_true(PlacesUtils.annotations.itemHasAnnotation(itemId1, SMART_BOOKMARKS_ANNO));
michael@0 174 let firstItemTitle = PlacesUtils.bookmarks.getItemTitle(itemId1);
michael@0 175
michael@0 176 let itemId2 = PlacesUtils.bookmarks.getIdForItemAt(PlacesUtils.bookmarksMenuFolderId, 1);
michael@0 177 do_check_true(PlacesUtils.annotations.itemHasAnnotation(itemId2, SMART_BOOKMARKS_ANNO));
michael@0 178 let secondItemTitle = PlacesUtils.bookmarks.getItemTitle(itemId2);
michael@0 179
michael@0 180 // Move the first smart bookmark to the end of the menu.
michael@0 181 PlacesUtils.bookmarks.moveItem(itemId1, PlacesUtils.bookmarksMenuFolderId,
michael@0 182 PlacesUtils.bookmarks.DEFAULT_INDEX);
michael@0 183
michael@0 184 do_check_eq(itemId1, PlacesUtils.bookmarks.getIdForItemAt(PlacesUtils.bookmarksMenuFolderId,
michael@0 185 PlacesUtils.bookmarks.DEFAULT_INDEX));
michael@0 186
michael@0 187 // Set preferences.
michael@0 188 Services.prefs.setIntPref(PREF_SMART_BOOKMARKS_VERSION, 1);
michael@0 189
michael@0 190 rebuildSmartBookmarks();
michael@0 191
michael@0 192 // Count items.
michael@0 193 do_check_eq(countFolderChildren(PlacesUtils.toolbarFolderId),
michael@0 194 SMART_BOOKMARKS_ON_TOOLBAR + DEFAULT_BOOKMARKS_ON_TOOLBAR);
michael@0 195 do_check_eq(countFolderChildren(PlacesUtils.bookmarksMenuFolderId),
michael@0 196 SMART_BOOKMARKS_ON_MENU + DEFAULT_BOOKMARKS_ON_MENU);
michael@0 197
michael@0 198 // Check smart bookmarks are still in correct position.
michael@0 199 itemId2 = PlacesUtils.bookmarks.getIdForItemAt(PlacesUtils.bookmarksMenuFolderId, 0);
michael@0 200 do_check_true(PlacesUtils.annotations.itemHasAnnotation(itemId2, SMART_BOOKMARKS_ANNO));
michael@0 201 do_check_eq(PlacesUtils.bookmarks.getItemTitle(itemId2), secondItemTitle);
michael@0 202
michael@0 203 itemId1 = PlacesUtils.bookmarks.getIdForItemAt(PlacesUtils.bookmarksMenuFolderId,
michael@0 204 PlacesUtils.bookmarks.DEFAULT_INDEX);
michael@0 205 do_check_true(PlacesUtils.annotations.itemHasAnnotation(itemId1, SMART_BOOKMARKS_ANNO));
michael@0 206 do_check_eq(PlacesUtils.bookmarks.getItemTitle(itemId1), firstItemTitle);
michael@0 207
michael@0 208 // Move back the smart bookmark to the original position.
michael@0 209 PlacesUtils.bookmarks.moveItem(itemId1, PlacesUtils.bookmarksMenuFolderId, 1);
michael@0 210
michael@0 211 // Check version has been updated.
michael@0 212 do_check_eq(Services.prefs.getIntPref(PREF_SMART_BOOKMARKS_VERSION),
michael@0 213 SMART_BOOKMARKS_VERSION);
michael@0 214
michael@0 215 next_test();
michael@0 216 }
michael@0 217 });
michael@0 218
michael@0 219 //------------------------------------------------------------------------------
michael@0 220
michael@0 221 tests.push({
michael@0 222 description: "An explicitly removed smart bookmark should not be recreated.",
michael@0 223 exec: function() {
michael@0 224 // Remove toolbar's smart bookmarks
michael@0 225 PlacesUtils.bookmarks.removeItem(PlacesUtils.bookmarks.getIdForItemAt(PlacesUtils.toolbarFolderId, 0));
michael@0 226
michael@0 227 // Sanity check items.
michael@0 228 do_check_eq(countFolderChildren(PlacesUtils.toolbarFolderId),
michael@0 229 DEFAULT_BOOKMARKS_ON_TOOLBAR);
michael@0 230 do_check_eq(countFolderChildren(PlacesUtils.bookmarksMenuFolderId),
michael@0 231 SMART_BOOKMARKS_ON_MENU + DEFAULT_BOOKMARKS_ON_MENU);
michael@0 232
michael@0 233 // Set preferences.
michael@0 234 Services.prefs.setIntPref(PREF_SMART_BOOKMARKS_VERSION, 1);
michael@0 235
michael@0 236 rebuildSmartBookmarks();
michael@0 237
michael@0 238 // Count items.
michael@0 239 // We should not have recreated the smart bookmark on toolbar.
michael@0 240 do_check_eq(countFolderChildren(PlacesUtils.toolbarFolderId),
michael@0 241 DEFAULT_BOOKMARKS_ON_TOOLBAR);
michael@0 242 do_check_eq(countFolderChildren(PlacesUtils.bookmarksMenuFolderId),
michael@0 243 SMART_BOOKMARKS_ON_MENU + DEFAULT_BOOKMARKS_ON_MENU);
michael@0 244
michael@0 245 // Check version has been updated.
michael@0 246 do_check_eq(Services.prefs.getIntPref(PREF_SMART_BOOKMARKS_VERSION),
michael@0 247 SMART_BOOKMARKS_VERSION);
michael@0 248
michael@0 249 next_test();
michael@0 250 }
michael@0 251 });
michael@0 252
michael@0 253 //------------------------------------------------------------------------------
michael@0 254
michael@0 255 tests.push({
michael@0 256 description: "Even if a smart bookmark has been removed recreate it if version is 0.",
michael@0 257 exec: function() {
michael@0 258 // Sanity check items.
michael@0 259 do_check_eq(countFolderChildren(PlacesUtils.toolbarFolderId),
michael@0 260 DEFAULT_BOOKMARKS_ON_TOOLBAR);
michael@0 261 do_check_eq(countFolderChildren(PlacesUtils.bookmarksMenuFolderId),
michael@0 262 SMART_BOOKMARKS_ON_MENU + DEFAULT_BOOKMARKS_ON_MENU);
michael@0 263
michael@0 264 // Set preferences.
michael@0 265 Services.prefs.setIntPref(PREF_SMART_BOOKMARKS_VERSION, 0);
michael@0 266
michael@0 267 rebuildSmartBookmarks();
michael@0 268
michael@0 269 // Count items.
michael@0 270 // We should not have recreated the smart bookmark on toolbar.
michael@0 271 do_check_eq(countFolderChildren(PlacesUtils.toolbarFolderId),
michael@0 272 SMART_BOOKMARKS_ON_TOOLBAR + DEFAULT_BOOKMARKS_ON_TOOLBAR);
michael@0 273 do_check_eq(countFolderChildren(PlacesUtils.bookmarksMenuFolderId),
michael@0 274 SMART_BOOKMARKS_ON_MENU + DEFAULT_BOOKMARKS_ON_MENU);
michael@0 275
michael@0 276 // Check version has been updated.
michael@0 277 do_check_eq(Services.prefs.getIntPref(PREF_SMART_BOOKMARKS_VERSION),
michael@0 278 SMART_BOOKMARKS_VERSION);
michael@0 279
michael@0 280 next_test();
michael@0 281 }
michael@0 282 });
michael@0 283 //------------------------------------------------------------------------------
michael@0 284
michael@0 285 function countFolderChildren(aFolderItemId) {
michael@0 286 let rootNode = PlacesUtils.getFolderContents(aFolderItemId).root;
michael@0 287 let cc = rootNode.childCount;
michael@0 288 // Dump contents.
michael@0 289 for (let i = 0; i < cc ; i++) {
michael@0 290 let node = rootNode.getChild(i);
michael@0 291 let title = PlacesUtils.nodeIsSeparator(node) ? "---" : node.title;
michael@0 292 print("Found child(" + i + "): " + title);
michael@0 293 }
michael@0 294 rootNode.containerOpen = false;
michael@0 295 return cc;
michael@0 296 }
michael@0 297
michael@0 298 function next_test() {
michael@0 299 if (tests.length) {
michael@0 300 // Execute next test.
michael@0 301 let test = tests.shift();
michael@0 302 print("\nTEST: " + test.description);
michael@0 303 test.exec();
michael@0 304 }
michael@0 305 else {
michael@0 306 // Clean up database from all bookmarks.
michael@0 307 remove_all_bookmarks();
michael@0 308 do_test_finished();
michael@0 309 }
michael@0 310 }
michael@0 311
michael@0 312 function run_test() {
michael@0 313 do_test_pending();
michael@0 314
michael@0 315 remove_bookmarks_html();
michael@0 316 remove_all_JSON_backups();
michael@0 317
michael@0 318 // Initialize browserGlue, but remove it's listener to places-init-complete.
michael@0 319 let bg = Cc["@mozilla.org/browser/browserglue;1"].getService(Ci.nsIObserver);
michael@0 320 // Initialize Places.
michael@0 321 PlacesUtils.history;
michael@0 322 // Observes Places initialisation complete.
michael@0 323 Services.obs.addObserver(function waitPlaceInitComplete() {
michael@0 324 Services.obs.removeObserver(waitPlaceInitComplete, "places-browser-init-complete");
michael@0 325
michael@0 326 // Ensure preferences status.
michael@0 327 do_check_false(Services.prefs.getBoolPref(PREF_AUTO_EXPORT_HTML));
michael@0 328 do_check_false(Services.prefs.getBoolPref(PREF_RESTORE_DEFAULT_BOOKMARKS));
michael@0 329 try {
michael@0 330 do_check_false(Services.prefs.getBoolPref(PREF_IMPORT_BOOKMARKS_HTML));
michael@0 331 do_throw("importBookmarksHTML pref should not exist");
michael@0 332 }
michael@0 333 catch(ex) {}
michael@0 334
michael@0 335 waitForImportAndSmartBookmarks(next_test);
michael@0 336 }, "places-browser-init-complete", false);
michael@0 337
michael@0 338 // Usually places init would async notify to glue, but we want to avoid
michael@0 339 // randomness here, thus we fire the notification synchronously.
michael@0 340 bg.observe(null, "places-init-complete", null);
michael@0 341 }
michael@0 342
michael@0 343 function waitForImportAndSmartBookmarks(aCallback) {
michael@0 344 Services.obs.addObserver(function waitImport() {
michael@0 345 Services.obs.removeObserver(waitImport, "bookmarks-restore-success");
michael@0 346 // Delay to test eventual smart bookmarks creation.
michael@0 347 do_execute_soon(function () {
michael@0 348 promiseAsyncUpdates().then(aCallback);
michael@0 349 });
michael@0 350 }, "bookmarks-restore-success", false);
michael@0 351 }

mercurial