browser/components/places/tests/unit/test_browserGlue_migrate.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_migrate.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,88 @@
     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 does not overwrite bookmarks imported from the
     1.9 + * migrators.  They usually run before nsBrowserGlue, so if we find any
    1.10 + * bookmark on init, we should not try to import.
    1.11 + */
    1.12 +
    1.13 +const PREF_SMART_BOOKMARKS_VERSION = "browser.places.smartBookmarksVersion";
    1.14 +
    1.15 +function run_test() {
    1.16 +  do_test_pending();
    1.17 +
    1.18 +  // Create our bookmarks.html copying bookmarks.glue.html to the profile
    1.19 +  // folder.  It should be ignored.
    1.20 +  create_bookmarks_html("bookmarks.glue.html");
    1.21 +
    1.22 +  // Remove current database file.
    1.23 +  let db = gProfD.clone();
    1.24 +  db.append("places.sqlite");
    1.25 +  if (db.exists()) {
    1.26 +    db.remove(false);
    1.27 +    do_check_false(db.exists());
    1.28 +  }
    1.29 +
    1.30 +  // Initialize Places through the History Service and check that a new
    1.31 +  // database has been created.
    1.32 +  do_check_eq(PlacesUtils.history.databaseStatus,
    1.33 +              PlacesUtils.history.DATABASE_STATUS_CREATE);
    1.34 +
    1.35 +  // A migrator would run before nsBrowserGlue Places initialization, so mimic
    1.36 +  // that behavior adding a bookmark and notifying the migration.
    1.37 +  let bg = Cc["@mozilla.org/browser/browserglue;1"].
    1.38 +           getService(Ci.nsIObserver);
    1.39 +
    1.40 +  bg.observe(null, "initial-migration-will-import-default-bookmarks", null);
    1.41 +
    1.42 +  PlacesUtils.bookmarks.insertBookmark(PlacesUtils.bookmarks.bookmarksMenuFolder, uri("http://mozilla.org/"),
    1.43 +                    PlacesUtils.bookmarks.DEFAULT_INDEX, "migrated");
    1.44 +
    1.45 +  let bookmarksObserver = {
    1.46 +    onBeginUpdateBatch: function() {},
    1.47 +    onEndUpdateBatch: function() {
    1.48 +      // Check if the currently finished batch created the smart bookmarks.
    1.49 +      let itemId =
    1.50 +        PlacesUtils.bookmarks.getIdForItemAt(PlacesUtils.toolbarFolderId, 0);
    1.51 +      do_check_neq(itemId, -1);
    1.52 +      if (PlacesUtils.annotations
    1.53 +                     .itemHasAnnotation(itemId, "Places/SmartBookmark")) {
    1.54 +        do_execute_soon(onSmartBookmarksCreation);
    1.55 +      }
    1.56 +    },
    1.57 +    onItemAdded: function() {},
    1.58 +    onItemRemoved: function(id, folder, index, itemType) {},
    1.59 +    onItemChanged: function() {},
    1.60 +    onItemVisited: function(id, visitID, time) {},
    1.61 +    onItemMoved: function() {},
    1.62 +    QueryInterface: XPCOMUtils.generateQI([Ci.nsINavBookmarkObserver])
    1.63 +  };
    1.64 +  // The test will continue once import has finished and smart bookmarks
    1.65 +  // have been created.
    1.66 +  PlacesUtils.bookmarks.addObserver(bookmarksObserver, false);
    1.67 +
    1.68 +  bg.observe(null, "initial-migration-did-import-default-bookmarks", null);
    1.69 +}
    1.70 +
    1.71 +function onSmartBookmarksCreation() {
    1.72 +  // Check the created bookmarks still exist.
    1.73 +  let itemId =
    1.74 +    PlacesUtils.bookmarks.getIdForItemAt(PlacesUtils.bookmarksMenuFolderId,
    1.75 +                                         SMART_BOOKMARKS_ON_MENU);
    1.76 +  do_check_eq(PlacesUtils.bookmarks.getItemTitle(itemId), "migrated");
    1.77 +
    1.78 +  // Check that we have not imported any new bookmark.
    1.79 +  itemId =
    1.80 +    PlacesUtils.bookmarks.getIdForItemAt(PlacesUtils.bookmarksMenuFolderId,
    1.81 +                                         SMART_BOOKMARKS_ON_MENU + 1)
    1.82 +  do_check_eq(itemId, -1);
    1.83 +  itemId =
    1.84 +    PlacesUtils.bookmarks.getIdForItemAt(PlacesUtils.toolbarFolderId,
    1.85 +                                         SMART_BOOKMARKS_ON_MENU)
    1.86 +  do_check_eq(itemId, -1);
    1.87 +
    1.88 +  remove_bookmarks_html();
    1.89 +
    1.90 +  do_test_finished();
    1.91 +}

mercurial