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

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/browser/components/places/tests/unit/test_browserGlue_smartBookmarks.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,351 @@
     1.4 +/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* vim:set ts=2 sw=2 sts=2 et: */
     1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.9 +
    1.10 +/**
    1.11 + * Tests that nsBrowserGlue is correctly interpreting the preferences settable
    1.12 + * by the user or by other components.
    1.13 + */
    1.14 +
    1.15 +const PREF_SMART_BOOKMARKS_VERSION = "browser.places.smartBookmarksVersion";
    1.16 +const PREF_AUTO_EXPORT_HTML = "browser.bookmarks.autoExportHTML";
    1.17 +const PREF_IMPORT_BOOKMARKS_HTML = "browser.places.importBookmarksHTML";
    1.18 +const PREF_RESTORE_DEFAULT_BOOKMARKS = "browser.bookmarks.restore_default_bookmarks";
    1.19 +
    1.20 +const SMART_BOOKMARKS_ANNO = "Places/SmartBookmark";
    1.21 +
    1.22 +/**
    1.23 + * Rebuilds smart bookmarks listening to console output to report any message or
    1.24 + * exception generated when calling ensurePlacesDefaultQueriesInitialized().
    1.25 + */
    1.26 +function rebuildSmartBookmarks() {
    1.27 +  let consoleListener = {
    1.28 +    observe: function(aMsg) {
    1.29 +      print("Got console message: " + aMsg.message);
    1.30 +    },
    1.31 +
    1.32 +    QueryInterface: XPCOMUtils.generateQI([
    1.33 +      Ci.nsIConsoleListener
    1.34 +    ]),
    1.35 +  };
    1.36 +  Services.console.reset();
    1.37 +  Services.console.registerListener(consoleListener);
    1.38 +  Cc["@mozilla.org/browser/browserglue;1"].getService(Ci.nsIBrowserGlue)
    1.39 +                                          .ensurePlacesDefaultQueriesInitialized();
    1.40 +  Services.console.unregisterListener(consoleListener);
    1.41 +}
    1.42 +
    1.43 +
    1.44 +let tests = [];
    1.45 +//------------------------------------------------------------------------------
    1.46 +
    1.47 +tests.push({
    1.48 +  description: "All smart bookmarks are created if smart bookmarks version is 0.",
    1.49 +  exec: function() {
    1.50 +    // Sanity check: we should have default bookmark.
    1.51 +    do_check_neq(PlacesUtils.bookmarks.getIdForItemAt(PlacesUtils.toolbarFolderId, 0), -1);
    1.52 +    do_check_neq(PlacesUtils.bookmarks.getIdForItemAt(PlacesUtils.bookmarksMenuFolderId, 0), -1);
    1.53 +
    1.54 +    // Set preferences.
    1.55 +    Services.prefs.setIntPref(PREF_SMART_BOOKMARKS_VERSION, 0);
    1.56 +
    1.57 +    rebuildSmartBookmarks();
    1.58 +
    1.59 +    // Count items.
    1.60 +    do_check_eq(countFolderChildren(PlacesUtils.toolbarFolderId),
    1.61 +                SMART_BOOKMARKS_ON_TOOLBAR + DEFAULT_BOOKMARKS_ON_TOOLBAR);
    1.62 +    do_check_eq(countFolderChildren(PlacesUtils.bookmarksMenuFolderId),
    1.63 +                SMART_BOOKMARKS_ON_MENU + DEFAULT_BOOKMARKS_ON_MENU);
    1.64 +
    1.65 +    // Check version has been updated.
    1.66 +    do_check_eq(Services.prefs.getIntPref(PREF_SMART_BOOKMARKS_VERSION),
    1.67 +                SMART_BOOKMARKS_VERSION);
    1.68 +
    1.69 +    next_test();
    1.70 +  }
    1.71 +});
    1.72 +
    1.73 +//------------------------------------------------------------------------------
    1.74 +
    1.75 +tests.push({
    1.76 +  description: "An existing smart bookmark is replaced when version changes.",
    1.77 +  exec: function() {
    1.78 +    // Sanity check: we have a smart bookmark on the toolbar.
    1.79 +    let itemId = PlacesUtils.bookmarks.getIdForItemAt(PlacesUtils.toolbarFolderId, 0);
    1.80 +    do_check_neq(itemId, -1);
    1.81 +    do_check_true(PlacesUtils.annotations.itemHasAnnotation(itemId, SMART_BOOKMARKS_ANNO));
    1.82 +    // Change its title.
    1.83 +    PlacesUtils.bookmarks.setItemTitle(itemId, "new title");
    1.84 +    do_check_eq(PlacesUtils.bookmarks.getItemTitle(itemId), "new title");
    1.85 +
    1.86 +    // Sanity check items.
    1.87 +    do_check_eq(countFolderChildren(PlacesUtils.toolbarFolderId),
    1.88 +                SMART_BOOKMARKS_ON_TOOLBAR + DEFAULT_BOOKMARKS_ON_TOOLBAR);
    1.89 +    do_check_eq(countFolderChildren(PlacesUtils.bookmarksMenuFolderId),
    1.90 +                SMART_BOOKMARKS_ON_MENU + DEFAULT_BOOKMARKS_ON_MENU);
    1.91 +
    1.92 +    // Set preferences.
    1.93 +    Services.prefs.setIntPref(PREF_SMART_BOOKMARKS_VERSION, 1);
    1.94 +
    1.95 +    rebuildSmartBookmarks();
    1.96 +
    1.97 +    // Count items.
    1.98 +    do_check_eq(countFolderChildren(PlacesUtils.toolbarFolderId),
    1.99 +                SMART_BOOKMARKS_ON_TOOLBAR + DEFAULT_BOOKMARKS_ON_TOOLBAR);
   1.100 +    do_check_eq(countFolderChildren(PlacesUtils.bookmarksMenuFolderId),
   1.101 +                SMART_BOOKMARKS_ON_MENU + DEFAULT_BOOKMARKS_ON_MENU);
   1.102 +
   1.103 +    // Check smart bookmark has been replaced, itemId has changed.
   1.104 +    itemId = PlacesUtils.bookmarks.getIdForItemAt(PlacesUtils.toolbarFolderId, 0);
   1.105 +    do_check_neq(itemId, -1);
   1.106 +    do_check_neq(PlacesUtils.bookmarks.getItemTitle(itemId), "new title");
   1.107 +    do_check_true(PlacesUtils.annotations.itemHasAnnotation(itemId, SMART_BOOKMARKS_ANNO));
   1.108 +
   1.109 +    // Check version has been updated.
   1.110 +    do_check_eq(Services.prefs.getIntPref(PREF_SMART_BOOKMARKS_VERSION),
   1.111 +                SMART_BOOKMARKS_VERSION);
   1.112 +
   1.113 +    next_test();
   1.114 +  }
   1.115 +});
   1.116 +
   1.117 +//------------------------------------------------------------------------------
   1.118 +
   1.119 +tests.push({
   1.120 +  description: "bookmarks position is retained when version changes.",
   1.121 +  exec: function() {
   1.122 +    // Sanity check items.
   1.123 +    do_check_eq(countFolderChildren(PlacesUtils.toolbarFolderId),
   1.124 +                SMART_BOOKMARKS_ON_TOOLBAR + DEFAULT_BOOKMARKS_ON_TOOLBAR);
   1.125 +    do_check_eq(countFolderChildren(PlacesUtils.bookmarksMenuFolderId),
   1.126 +                SMART_BOOKMARKS_ON_MENU + DEFAULT_BOOKMARKS_ON_MENU);
   1.127 +
   1.128 +    let itemId = PlacesUtils.bookmarks.getIdForItemAt(PlacesUtils.bookmarksMenuFolderId, 0);
   1.129 +    do_check_true(PlacesUtils.annotations.itemHasAnnotation(itemId, SMART_BOOKMARKS_ANNO));
   1.130 +    let firstItemTitle = PlacesUtils.bookmarks.getItemTitle(itemId);
   1.131 +
   1.132 +    itemId = PlacesUtils.bookmarks.getIdForItemAt(PlacesUtils.bookmarksMenuFolderId, 1);
   1.133 +    do_check_true(PlacesUtils.annotations.itemHasAnnotation(itemId, SMART_BOOKMARKS_ANNO));
   1.134 +    let secondItemTitle = PlacesUtils.bookmarks.getItemTitle(itemId);
   1.135 +
   1.136 +    // Set preferences.
   1.137 +    Services.prefs.setIntPref(PREF_SMART_BOOKMARKS_VERSION, 1);
   1.138 +
   1.139 +    rebuildSmartBookmarks();
   1.140 +
   1.141 +    // Count items.
   1.142 +    do_check_eq(countFolderChildren(PlacesUtils.toolbarFolderId),
   1.143 +                SMART_BOOKMARKS_ON_TOOLBAR + DEFAULT_BOOKMARKS_ON_TOOLBAR);
   1.144 +    do_check_eq(countFolderChildren(PlacesUtils.bookmarksMenuFolderId),
   1.145 +                SMART_BOOKMARKS_ON_MENU + DEFAULT_BOOKMARKS_ON_MENU);
   1.146 +
   1.147 +    // Check smart bookmarks are still in correct position.
   1.148 +    itemId = PlacesUtils.bookmarks.getIdForItemAt(PlacesUtils.bookmarksMenuFolderId, 0);
   1.149 +    do_check_true(PlacesUtils.annotations.itemHasAnnotation(itemId, SMART_BOOKMARKS_ANNO));
   1.150 +    do_check_eq(PlacesUtils.bookmarks.getItemTitle(itemId), firstItemTitle);
   1.151 +
   1.152 +    itemId = PlacesUtils.bookmarks.getIdForItemAt(PlacesUtils.bookmarksMenuFolderId, 1);
   1.153 +    do_check_true(PlacesUtils.annotations.itemHasAnnotation(itemId, SMART_BOOKMARKS_ANNO));
   1.154 +    do_check_eq(PlacesUtils.bookmarks.getItemTitle(itemId), secondItemTitle);
   1.155 +
   1.156 +    // Check version has been updated.
   1.157 +    do_check_eq(Services.prefs.getIntPref(PREF_SMART_BOOKMARKS_VERSION),
   1.158 +                SMART_BOOKMARKS_VERSION);
   1.159 +
   1.160 +    next_test();
   1.161 +  }
   1.162 +});
   1.163 +
   1.164 +//------------------------------------------------------------------------------
   1.165 +
   1.166 +tests.push({
   1.167 +  description: "moved bookmarks position is retained when version changes.",
   1.168 +  exec: function() {
   1.169 +    // Sanity check items.
   1.170 +    do_check_eq(countFolderChildren(PlacesUtils.toolbarFolderId),
   1.171 +                SMART_BOOKMARKS_ON_TOOLBAR + DEFAULT_BOOKMARKS_ON_TOOLBAR);
   1.172 +    do_check_eq(countFolderChildren(PlacesUtils.bookmarksMenuFolderId),
   1.173 +                SMART_BOOKMARKS_ON_MENU + DEFAULT_BOOKMARKS_ON_MENU);
   1.174 +
   1.175 +    let itemId1 = PlacesUtils.bookmarks.getIdForItemAt(PlacesUtils.bookmarksMenuFolderId, 0);
   1.176 +    do_check_true(PlacesUtils.annotations.itemHasAnnotation(itemId1, SMART_BOOKMARKS_ANNO));
   1.177 +    let firstItemTitle = PlacesUtils.bookmarks.getItemTitle(itemId1);
   1.178 +
   1.179 +    let itemId2 = PlacesUtils.bookmarks.getIdForItemAt(PlacesUtils.bookmarksMenuFolderId, 1);
   1.180 +    do_check_true(PlacesUtils.annotations.itemHasAnnotation(itemId2, SMART_BOOKMARKS_ANNO));
   1.181 +    let secondItemTitle = PlacesUtils.bookmarks.getItemTitle(itemId2);
   1.182 +
   1.183 +    // Move the first smart bookmark to the end of the menu.
   1.184 +    PlacesUtils.bookmarks.moveItem(itemId1, PlacesUtils.bookmarksMenuFolderId,
   1.185 +                                   PlacesUtils.bookmarks.DEFAULT_INDEX);
   1.186 +
   1.187 +    do_check_eq(itemId1, PlacesUtils.bookmarks.getIdForItemAt(PlacesUtils.bookmarksMenuFolderId,
   1.188 +                                                   PlacesUtils.bookmarks.DEFAULT_INDEX));
   1.189 +
   1.190 +    // Set preferences.
   1.191 +    Services.prefs.setIntPref(PREF_SMART_BOOKMARKS_VERSION, 1);
   1.192 +
   1.193 +    rebuildSmartBookmarks();
   1.194 +
   1.195 +    // Count items.
   1.196 +    do_check_eq(countFolderChildren(PlacesUtils.toolbarFolderId),
   1.197 +                SMART_BOOKMARKS_ON_TOOLBAR + DEFAULT_BOOKMARKS_ON_TOOLBAR);
   1.198 +    do_check_eq(countFolderChildren(PlacesUtils.bookmarksMenuFolderId),
   1.199 +                SMART_BOOKMARKS_ON_MENU + DEFAULT_BOOKMARKS_ON_MENU);
   1.200 +
   1.201 +    // Check smart bookmarks are still in correct position.
   1.202 +    itemId2 = PlacesUtils.bookmarks.getIdForItemAt(PlacesUtils.bookmarksMenuFolderId, 0);
   1.203 +    do_check_true(PlacesUtils.annotations.itemHasAnnotation(itemId2, SMART_BOOKMARKS_ANNO));
   1.204 +    do_check_eq(PlacesUtils.bookmarks.getItemTitle(itemId2), secondItemTitle);
   1.205 +
   1.206 +    itemId1 = PlacesUtils.bookmarks.getIdForItemAt(PlacesUtils.bookmarksMenuFolderId,
   1.207 +                                                   PlacesUtils.bookmarks.DEFAULT_INDEX);
   1.208 +    do_check_true(PlacesUtils.annotations.itemHasAnnotation(itemId1, SMART_BOOKMARKS_ANNO));
   1.209 +    do_check_eq(PlacesUtils.bookmarks.getItemTitle(itemId1), firstItemTitle);
   1.210 +
   1.211 +    // Move back the smart bookmark to the original position.
   1.212 +    PlacesUtils.bookmarks.moveItem(itemId1, PlacesUtils.bookmarksMenuFolderId, 1);
   1.213 +
   1.214 +    // Check version has been updated.
   1.215 +    do_check_eq(Services.prefs.getIntPref(PREF_SMART_BOOKMARKS_VERSION),
   1.216 +                SMART_BOOKMARKS_VERSION);
   1.217 +
   1.218 +    next_test();
   1.219 +  }
   1.220 +});
   1.221 +
   1.222 +//------------------------------------------------------------------------------
   1.223 +
   1.224 +tests.push({
   1.225 +  description: "An explicitly removed smart bookmark should not be recreated.",
   1.226 +  exec: function() {   
   1.227 +    // Remove toolbar's smart bookmarks
   1.228 +    PlacesUtils.bookmarks.removeItem(PlacesUtils.bookmarks.getIdForItemAt(PlacesUtils.toolbarFolderId, 0));
   1.229 +
   1.230 +    // Sanity check items.
   1.231 +    do_check_eq(countFolderChildren(PlacesUtils.toolbarFolderId),
   1.232 +                DEFAULT_BOOKMARKS_ON_TOOLBAR);
   1.233 +    do_check_eq(countFolderChildren(PlacesUtils.bookmarksMenuFolderId),
   1.234 +                SMART_BOOKMARKS_ON_MENU + DEFAULT_BOOKMARKS_ON_MENU);
   1.235 +
   1.236 +    // Set preferences.
   1.237 +    Services.prefs.setIntPref(PREF_SMART_BOOKMARKS_VERSION, 1);
   1.238 +
   1.239 +    rebuildSmartBookmarks();
   1.240 +
   1.241 +    // Count items.
   1.242 +    // We should not have recreated the smart bookmark on toolbar.
   1.243 +    do_check_eq(countFolderChildren(PlacesUtils.toolbarFolderId),
   1.244 +                DEFAULT_BOOKMARKS_ON_TOOLBAR);
   1.245 +    do_check_eq(countFolderChildren(PlacesUtils.bookmarksMenuFolderId),
   1.246 +                SMART_BOOKMARKS_ON_MENU + DEFAULT_BOOKMARKS_ON_MENU);
   1.247 +
   1.248 +    // Check version has been updated.
   1.249 +    do_check_eq(Services.prefs.getIntPref(PREF_SMART_BOOKMARKS_VERSION),
   1.250 +                SMART_BOOKMARKS_VERSION);
   1.251 +
   1.252 +    next_test();
   1.253 +  }
   1.254 +});
   1.255 +
   1.256 +//------------------------------------------------------------------------------
   1.257 +
   1.258 +tests.push({
   1.259 +  description: "Even if a smart bookmark has been removed recreate it if version is 0.",
   1.260 +  exec: function() {
   1.261 +    // Sanity check items.
   1.262 +    do_check_eq(countFolderChildren(PlacesUtils.toolbarFolderId),
   1.263 +                DEFAULT_BOOKMARKS_ON_TOOLBAR);
   1.264 +    do_check_eq(countFolderChildren(PlacesUtils.bookmarksMenuFolderId),
   1.265 +                SMART_BOOKMARKS_ON_MENU + DEFAULT_BOOKMARKS_ON_MENU);
   1.266 +
   1.267 +    // Set preferences.
   1.268 +    Services.prefs.setIntPref(PREF_SMART_BOOKMARKS_VERSION, 0);
   1.269 +
   1.270 +    rebuildSmartBookmarks();
   1.271 +
   1.272 +    // Count items.
   1.273 +    // We should not have recreated the smart bookmark on toolbar.
   1.274 +    do_check_eq(countFolderChildren(PlacesUtils.toolbarFolderId),
   1.275 +                SMART_BOOKMARKS_ON_TOOLBAR + DEFAULT_BOOKMARKS_ON_TOOLBAR);
   1.276 +    do_check_eq(countFolderChildren(PlacesUtils.bookmarksMenuFolderId),
   1.277 +                SMART_BOOKMARKS_ON_MENU + DEFAULT_BOOKMARKS_ON_MENU);
   1.278 +
   1.279 +    // Check version has been updated.
   1.280 +    do_check_eq(Services.prefs.getIntPref(PREF_SMART_BOOKMARKS_VERSION),
   1.281 +                SMART_BOOKMARKS_VERSION);
   1.282 +
   1.283 +    next_test();
   1.284 +  }
   1.285 +});
   1.286 +//------------------------------------------------------------------------------
   1.287 +
   1.288 +function countFolderChildren(aFolderItemId) {
   1.289 +  let rootNode = PlacesUtils.getFolderContents(aFolderItemId).root;
   1.290 +  let cc = rootNode.childCount;
   1.291 +  // Dump contents.
   1.292 +  for (let i = 0; i < cc ; i++) {
   1.293 +    let node = rootNode.getChild(i);
   1.294 +    let title = PlacesUtils.nodeIsSeparator(node) ? "---" : node.title;
   1.295 +    print("Found child(" + i + "): " + title);
   1.296 +  }
   1.297 +  rootNode.containerOpen = false;
   1.298 +  return cc;
   1.299 +}
   1.300 +
   1.301 +function next_test() {
   1.302 +  if (tests.length) {
   1.303 +    // Execute next test.
   1.304 +    let test = tests.shift();
   1.305 +    print("\nTEST: " + test.description);
   1.306 +    test.exec();
   1.307 +  }
   1.308 +  else {
   1.309 +    // Clean up database from all bookmarks.
   1.310 +    remove_all_bookmarks();
   1.311 +    do_test_finished();
   1.312 +  }
   1.313 +}
   1.314 +
   1.315 +function run_test() {
   1.316 +  do_test_pending();
   1.317 +
   1.318 +  remove_bookmarks_html();
   1.319 +  remove_all_JSON_backups();
   1.320 +
   1.321 +  // Initialize browserGlue, but remove it's listener to places-init-complete.
   1.322 +  let bg = Cc["@mozilla.org/browser/browserglue;1"].getService(Ci.nsIObserver);
   1.323 +  // Initialize Places.
   1.324 +  PlacesUtils.history;
   1.325 +  // Observes Places initialisation complete.
   1.326 +  Services.obs.addObserver(function waitPlaceInitComplete() {
   1.327 +    Services.obs.removeObserver(waitPlaceInitComplete, "places-browser-init-complete");
   1.328 +
   1.329 +    // Ensure preferences status.
   1.330 +    do_check_false(Services.prefs.getBoolPref(PREF_AUTO_EXPORT_HTML));
   1.331 +    do_check_false(Services.prefs.getBoolPref(PREF_RESTORE_DEFAULT_BOOKMARKS));
   1.332 +    try {
   1.333 +      do_check_false(Services.prefs.getBoolPref(PREF_IMPORT_BOOKMARKS_HTML));
   1.334 +      do_throw("importBookmarksHTML pref should not exist");
   1.335 +    }
   1.336 +    catch(ex) {}
   1.337 +
   1.338 +    waitForImportAndSmartBookmarks(next_test);
   1.339 +  }, "places-browser-init-complete", false);
   1.340 +
   1.341 +  // Usually places init would async notify to glue, but we want to avoid
   1.342 +  // randomness here, thus we fire the notification synchronously.
   1.343 +  bg.observe(null, "places-init-complete", null);
   1.344 +}
   1.345 +
   1.346 +function waitForImportAndSmartBookmarks(aCallback) {
   1.347 +  Services.obs.addObserver(function waitImport() {
   1.348 +    Services.obs.removeObserver(waitImport, "bookmarks-restore-success");
   1.349 +    // Delay to test eventual smart bookmarks creation.
   1.350 +    do_execute_soon(function () {
   1.351 +      promiseAsyncUpdates().then(aCallback);
   1.352 +    });
   1.353 +  }, "bookmarks-restore-success", false);
   1.354 +}

mercurial