services/sync/tests/unit/test_bookmark_store.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/services/sync/tests/unit/test_bookmark_store.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,434 @@
     1.4 +/* Any copyright is dedicated to the Public Domain.
     1.5 +   http://creativecommons.org/publicdomain/zero/1.0/ */
     1.6 +
     1.7 +Cu.import("resource://gre/modules/PlacesUtils.jsm");
     1.8 +Cu.import("resource://services-sync/engines.js");
     1.9 +Cu.import("resource://services-sync/engines/bookmarks.js");
    1.10 +Cu.import("resource://services-sync/service.js");
    1.11 +Cu.import("resource://services-sync/util.js");
    1.12 +
    1.13 +const PARENT_ANNO = "sync/parent";
    1.14 +
    1.15 +Service.engineManager.register(BookmarksEngine);
    1.16 +
    1.17 +let engine = Service.engineManager.get("bookmarks");
    1.18 +let store = engine._store;
    1.19 +let tracker = engine._tracker;
    1.20 +
    1.21 +// Don't write some persistence files asynchronously.
    1.22 +tracker.persistChangedIDs = false;
    1.23 +
    1.24 +let fxuri = Utils.makeURI("http://getfirefox.com/");
    1.25 +let tburi = Utils.makeURI("http://getthunderbird.com/");
    1.26 +
    1.27 +add_test(function test_ignore_specials() {
    1.28 +  _("Ensure that we can't delete bookmark roots.");
    1.29 +
    1.30 +  // Belt...
    1.31 +  let record = new BookmarkFolder("bookmarks", "toolbar", "folder");
    1.32 +  record.deleted = true;
    1.33 +  do_check_neq(null, store.idForGUID("toolbar"));
    1.34 +
    1.35 +  store.applyIncoming(record);
    1.36 +
    1.37 +  // Ensure that the toolbar exists.
    1.38 +  do_check_neq(null, store.idForGUID("toolbar"));
    1.39 +
    1.40 +  // This will fail painfully in getItemType if the deletion worked.
    1.41 +  engine._buildGUIDMap();
    1.42 +
    1.43 +  // Braces...
    1.44 +  store.remove(record);
    1.45 +  do_check_neq(null, store.idForGUID("toolbar"));
    1.46 +  engine._buildGUIDMap();
    1.47 +
    1.48 +  store.wipe();
    1.49 +  run_next_test();
    1.50 +});
    1.51 +
    1.52 +add_test(function test_bookmark_create() {
    1.53 +  try {
    1.54 +    _("Ensure the record isn't present yet.");
    1.55 +    let ids = PlacesUtils.bookmarks.getBookmarkIdsForURI(fxuri, {});
    1.56 +    do_check_eq(ids.length, 0);
    1.57 +
    1.58 +    _("Let's create a new record.");
    1.59 +    let fxrecord = new Bookmark("bookmarks", "get-firefox1");
    1.60 +    fxrecord.bmkUri        = fxuri.spec;
    1.61 +    fxrecord.description   = "Firefox is awesome.";
    1.62 +    fxrecord.title         = "Get Firefox!";
    1.63 +    fxrecord.tags          = ["firefox", "awesome", "browser"];
    1.64 +    fxrecord.keyword       = "awesome";
    1.65 +    fxrecord.loadInSidebar = false;
    1.66 +    fxrecord.parentName    = "Bookmarks Toolbar";
    1.67 +    fxrecord.parentid      = "toolbar";
    1.68 +    store.applyIncoming(fxrecord);
    1.69 +
    1.70 +    _("Verify it has been created correctly.");
    1.71 +    let id = store.idForGUID(fxrecord.id);
    1.72 +    do_check_eq(store.GUIDForId(id), fxrecord.id);
    1.73 +    do_check_eq(PlacesUtils.bookmarks.getItemType(id),
    1.74 +                PlacesUtils.bookmarks.TYPE_BOOKMARK);
    1.75 +    do_check_true(PlacesUtils.bookmarks.getBookmarkURI(id).equals(fxuri));
    1.76 +    do_check_eq(PlacesUtils.bookmarks.getItemTitle(id), fxrecord.title);
    1.77 +    do_check_eq(PlacesUtils.annotations.getItemAnnotation(id, "bookmarkProperties/description"),
    1.78 +                fxrecord.description);
    1.79 +    do_check_eq(PlacesUtils.bookmarks.getFolderIdForItem(id),
    1.80 +                PlacesUtils.bookmarks.toolbarFolder);
    1.81 +    do_check_eq(PlacesUtils.bookmarks.getKeywordForBookmark(id), fxrecord.keyword);
    1.82 +
    1.83 +    _("Have the store create a new record object. Verify that it has the same data.");
    1.84 +    let newrecord = store.createRecord(fxrecord.id);
    1.85 +    do_check_true(newrecord instanceof Bookmark);
    1.86 +    for each (let property in ["type", "bmkUri", "description", "title",
    1.87 +                               "keyword", "parentName", "parentid"]) {
    1.88 +      do_check_eq(newrecord[property], fxrecord[property]);
    1.89 +    }
    1.90 +    do_check_true(Utils.deepEquals(newrecord.tags.sort(),
    1.91 +                                   fxrecord.tags.sort()));
    1.92 +
    1.93 +    _("The calculated sort index is based on frecency data.");
    1.94 +    do_check_true(newrecord.sortindex >= 150);
    1.95 +
    1.96 +    _("Create a record with some values missing.");
    1.97 +    let tbrecord = new Bookmark("bookmarks", "thunderbird1");
    1.98 +    tbrecord.bmkUri        = tburi.spec;
    1.99 +    tbrecord.parentName    = "Bookmarks Toolbar";
   1.100 +    tbrecord.parentid      = "toolbar";
   1.101 +    store.applyIncoming(tbrecord);
   1.102 +
   1.103 +    _("Verify it has been created correctly.");
   1.104 +    id = store.idForGUID(tbrecord.id);
   1.105 +    do_check_eq(store.GUIDForId(id), tbrecord.id);
   1.106 +    do_check_eq(PlacesUtils.bookmarks.getItemType(id),
   1.107 +                PlacesUtils.bookmarks.TYPE_BOOKMARK);
   1.108 +    do_check_true(PlacesUtils.bookmarks.getBookmarkURI(id).equals(tburi));
   1.109 +    do_check_eq(PlacesUtils.bookmarks.getItemTitle(id), null);
   1.110 +    let error;
   1.111 +    try {
   1.112 +      PlacesUtils.annotations.getItemAnnotation(id, "bookmarkProperties/description");
   1.113 +    } catch(ex) {
   1.114 +      error = ex;
   1.115 +    }
   1.116 +    do_check_eq(error.result, Cr.NS_ERROR_NOT_AVAILABLE);
   1.117 +    do_check_eq(PlacesUtils.bookmarks.getFolderIdForItem(id),
   1.118 +                PlacesUtils.bookmarks.toolbarFolder);
   1.119 +    do_check_eq(PlacesUtils.bookmarks.getKeywordForBookmark(id), null);
   1.120 +  } finally {
   1.121 +    _("Clean up.");
   1.122 +    store.wipe();
   1.123 +    run_next_test();
   1.124 +  }
   1.125 +});
   1.126 +
   1.127 +add_test(function test_bookmark_update() {
   1.128 +  try {
   1.129 +    _("Create a bookmark whose values we'll change.");
   1.130 +    let bmk1_id = PlacesUtils.bookmarks.insertBookmark(
   1.131 +      PlacesUtils.bookmarks.toolbarFolder, fxuri,
   1.132 +      PlacesUtils.bookmarks.DEFAULT_INDEX,
   1.133 +      "Get Firefox!");
   1.134 +    PlacesUtils.annotations.setItemAnnotation(
   1.135 +      bmk1_id, "bookmarkProperties/description", "Firefox is awesome.", 0,
   1.136 +      PlacesUtils.annotations.EXPIRE_NEVER);
   1.137 +    PlacesUtils.bookmarks.setKeywordForBookmark(bmk1_id, "firefox");
   1.138 +    let bmk1_guid = store.GUIDForId(bmk1_id);
   1.139 +
   1.140 +    _("Update the record with some null values.");
   1.141 +    let record = store.createRecord(bmk1_guid);
   1.142 +    record.title = null;
   1.143 +    record.description = null;
   1.144 +    record.keyword = null;
   1.145 +    record.tags = null;
   1.146 +    store.applyIncoming(record);
   1.147 +
   1.148 +    _("Verify that the values have been cleared.");
   1.149 +    do_check_throws(function () {
   1.150 +      PlacesUtils.annotations.getItemAnnotation(
   1.151 +        bmk1_id, "bookmarkProperties/description");
   1.152 +    }, Cr.NS_ERROR_NOT_AVAILABLE);
   1.153 +    do_check_eq(PlacesUtils.bookmarks.getItemTitle(bmk1_id), null);
   1.154 +    do_check_eq(PlacesUtils.bookmarks.getKeywordForBookmark(bmk1_id), null);
   1.155 +  } finally {
   1.156 +    _("Clean up.");
   1.157 +    store.wipe();
   1.158 +    run_next_test();
   1.159 +  }
   1.160 +});
   1.161 +
   1.162 +add_test(function test_bookmark_createRecord() {
   1.163 +  try {
   1.164 +    _("Create a bookmark without a description or title.");
   1.165 +    let bmk1_id = PlacesUtils.bookmarks.insertBookmark(
   1.166 +      PlacesUtils.bookmarks.toolbarFolder, fxuri,
   1.167 +      PlacesUtils.bookmarks.DEFAULT_INDEX, null);
   1.168 +    let bmk1_guid = store.GUIDForId(bmk1_id);
   1.169 +
   1.170 +    _("Verify that the record is created accordingly.");
   1.171 +    let record = store.createRecord(bmk1_guid);
   1.172 +    do_check_eq(record.title, null);
   1.173 +    do_check_eq(record.description, null);
   1.174 +    do_check_eq(record.keyword, null);
   1.175 +
   1.176 +  } finally {
   1.177 +    _("Clean up.");
   1.178 +    store.wipe();
   1.179 +    run_next_test();
   1.180 +  }
   1.181 +});
   1.182 +
   1.183 +add_test(function test_folder_create() {
   1.184 +  try {
   1.185 +    _("Create a folder.");
   1.186 +    let folder = new BookmarkFolder("bookmarks", "testfolder-1");
   1.187 +    folder.parentName = "Bookmarks Toolbar";
   1.188 +    folder.parentid   = "toolbar";
   1.189 +    folder.title      = "Test Folder";
   1.190 +    store.applyIncoming(folder);
   1.191 +
   1.192 +    _("Verify it has been created correctly.");
   1.193 +    let id = store.idForGUID(folder.id);
   1.194 +    do_check_eq(PlacesUtils.bookmarks.getItemType(id),
   1.195 +                PlacesUtils.bookmarks.TYPE_FOLDER);
   1.196 +    do_check_eq(PlacesUtils.bookmarks.getItemTitle(id), folder.title);
   1.197 +    do_check_eq(PlacesUtils.bookmarks.getFolderIdForItem(id),
   1.198 +                PlacesUtils.bookmarks.toolbarFolder);
   1.199 +
   1.200 +    _("Have the store create a new record object. Verify that it has the same data.");
   1.201 +    let newrecord = store.createRecord(folder.id);
   1.202 +    do_check_true(newrecord instanceof BookmarkFolder);
   1.203 +    for each (let property in ["title", "parentName", "parentid"])
   1.204 +      do_check_eq(newrecord[property], folder[property]);
   1.205 +
   1.206 +    _("Folders have high sort index to ensure they're synced first.");
   1.207 +    do_check_eq(newrecord.sortindex, 1000000);
   1.208 +  } finally {
   1.209 +    _("Clean up.");
   1.210 +    store.wipe();
   1.211 +    run_next_test();
   1.212 +  }
   1.213 +});
   1.214 +
   1.215 +add_test(function test_folder_createRecord() {
   1.216 +  try {
   1.217 +    _("Create a folder.");
   1.218 +    let folder1_id = PlacesUtils.bookmarks.createFolder(
   1.219 +      PlacesUtils.bookmarks.toolbarFolder, "Folder1", 0);
   1.220 +    let folder1_guid = store.GUIDForId(folder1_id);
   1.221 +
   1.222 +    _("Create two bookmarks in that folder without assigning them GUIDs.");
   1.223 +    let bmk1_id = PlacesUtils.bookmarks.insertBookmark(
   1.224 +      folder1_id, fxuri, PlacesUtils.bookmarks.DEFAULT_INDEX, "Get Firefox!");
   1.225 +    let bmk2_id = PlacesUtils.bookmarks.insertBookmark(
   1.226 +      folder1_id, tburi, PlacesUtils.bookmarks.DEFAULT_INDEX, "Get Thunderbird!");
   1.227 +
   1.228 +    _("Create a record for the folder and verify basic properties.");
   1.229 +    let record = store.createRecord(folder1_guid);
   1.230 +    do_check_true(record instanceof BookmarkFolder);
   1.231 +    do_check_eq(record.title, "Folder1");
   1.232 +    do_check_eq(record.parentid, "toolbar");
   1.233 +    do_check_eq(record.parentName, "Bookmarks Toolbar");
   1.234 +
   1.235 +    _("Verify the folder's children. Ensures that the bookmarks were given GUIDs.");
   1.236 +    let bmk1_guid = store.GUIDForId(bmk1_id);
   1.237 +    let bmk2_guid = store.GUIDForId(bmk2_id);
   1.238 +    do_check_eq(record.children.length, 2);
   1.239 +    do_check_eq(record.children[0], bmk1_guid);
   1.240 +    do_check_eq(record.children[1], bmk2_guid);
   1.241 +
   1.242 +  } finally {
   1.243 +    _("Clean up.");
   1.244 +    store.wipe();
   1.245 +    run_next_test();
   1.246 +  }
   1.247 +});
   1.248 +
   1.249 +add_test(function test_deleted() {
   1.250 +  try {
   1.251 +    _("Create a bookmark that will be deleted.");
   1.252 +    let bmk1_id = PlacesUtils.bookmarks.insertBookmark(
   1.253 +      PlacesUtils.bookmarks.toolbarFolder, fxuri,
   1.254 +      PlacesUtils.bookmarks.DEFAULT_INDEX, "Get Firefox!");
   1.255 +    let bmk1_guid = store.GUIDForId(bmk1_id);
   1.256 +
   1.257 +    _("Delete the bookmark through the store.");
   1.258 +    let record = new PlacesItem("bookmarks", bmk1_guid);
   1.259 +    record.deleted = true;
   1.260 +    store.applyIncoming(record);
   1.261 +
   1.262 +    _("Ensure it has been deleted.");
   1.263 +    let error;
   1.264 +    try {
   1.265 +      PlacesUtils.bookmarks.getBookmarkURI(bmk1_id);
   1.266 +    } catch(ex) {
   1.267 +      error = ex;
   1.268 +    }
   1.269 +    do_check_eq(error.result, Cr.NS_ERROR_ILLEGAL_VALUE);
   1.270 +
   1.271 +    let newrec = store.createRecord(bmk1_guid);
   1.272 +    do_check_eq(newrec.deleted, true);
   1.273 +
   1.274 +  } finally {
   1.275 +    _("Clean up.");
   1.276 +    store.wipe();
   1.277 +    run_next_test();
   1.278 +  }
   1.279 +});
   1.280 +
   1.281 +add_test(function test_move_folder() {
   1.282 +  try {
   1.283 +    _("Create two folders and a bookmark in one of them.");
   1.284 +    let folder1_id = PlacesUtils.bookmarks.createFolder(
   1.285 +      PlacesUtils.bookmarks.toolbarFolder, "Folder1", 0);
   1.286 +    let folder1_guid = store.GUIDForId(folder1_id);
   1.287 +    let folder2_id = PlacesUtils.bookmarks.createFolder(
   1.288 +      PlacesUtils.bookmarks.toolbarFolder, "Folder2", 0);
   1.289 +    let folder2_guid = store.GUIDForId(folder2_id);
   1.290 +    let bmk_id = PlacesUtils.bookmarks.insertBookmark(
   1.291 +      folder1_id, fxuri, PlacesUtils.bookmarks.DEFAULT_INDEX, "Get Firefox!");
   1.292 +    let bmk_guid = store.GUIDForId(bmk_id);
   1.293 +
   1.294 +    _("Get a record, reparent it and apply it to the store.");
   1.295 +    let record = store.createRecord(bmk_guid);
   1.296 +    do_check_eq(record.parentid, folder1_guid);
   1.297 +    record.parentid = folder2_guid;
   1.298 +    store.applyIncoming(record);
   1.299 +
   1.300 +    _("Verify the new parent.");
   1.301 +    let new_folder_id = PlacesUtils.bookmarks.getFolderIdForItem(bmk_id);
   1.302 +    do_check_eq(store.GUIDForId(new_folder_id), folder2_guid);
   1.303 +  } finally {
   1.304 +    _("Clean up.");
   1.305 +    store.wipe();
   1.306 +    run_next_test();
   1.307 +  }
   1.308 +});
   1.309 +
   1.310 +add_test(function test_move_order() {
   1.311 +  // Make sure the tracker is turned on.
   1.312 +  Svc.Obs.notify("weave:engine:start-tracking");
   1.313 +  try {
   1.314 +    _("Create two bookmarks");
   1.315 +    let bmk1_id = PlacesUtils.bookmarks.insertBookmark(
   1.316 +      PlacesUtils.bookmarks.toolbarFolder, fxuri,
   1.317 +      PlacesUtils.bookmarks.DEFAULT_INDEX, "Get Firefox!");
   1.318 +    let bmk1_guid = store.GUIDForId(bmk1_id);
   1.319 +    let bmk2_id = PlacesUtils.bookmarks.insertBookmark(
   1.320 +      PlacesUtils.bookmarks.toolbarFolder, tburi,
   1.321 +      PlacesUtils.bookmarks.DEFAULT_INDEX, "Get Thunderbird!");
   1.322 +    let bmk2_guid = store.GUIDForId(bmk2_id);
   1.323 +
   1.324 +    _("Verify order.");
   1.325 +    do_check_eq(PlacesUtils.bookmarks.getItemIndex(bmk1_id), 0);
   1.326 +    do_check_eq(PlacesUtils.bookmarks.getItemIndex(bmk2_id), 1);
   1.327 +    let toolbar = store.createRecord("toolbar");
   1.328 +    do_check_eq(toolbar.children.length, 2);
   1.329 +    do_check_eq(toolbar.children[0], bmk1_guid);
   1.330 +    do_check_eq(toolbar.children[1], bmk2_guid);
   1.331 +
   1.332 +    _("Move bookmarks around.");
   1.333 +    store._childrenToOrder = {};
   1.334 +    toolbar.children = [bmk2_guid, bmk1_guid];
   1.335 +    store.applyIncoming(toolbar);
   1.336 +    // Bookmarks engine does this at the end of _processIncoming
   1.337 +    tracker.ignoreAll = true;
   1.338 +    store._orderChildren();
   1.339 +    tracker.ignoreAll = false;
   1.340 +    delete store._childrenToOrder;
   1.341 +
   1.342 +    _("Verify new order.");
   1.343 +    do_check_eq(PlacesUtils.bookmarks.getItemIndex(bmk2_id), 0);
   1.344 +    do_check_eq(PlacesUtils.bookmarks.getItemIndex(bmk1_id), 1);
   1.345 +
   1.346 +  } finally {
   1.347 +    Svc.Obs.notify("weave:engine:stop-tracking");
   1.348 +    _("Clean up.");
   1.349 +    store.wipe();
   1.350 +    run_next_test();
   1.351 +  }
   1.352 +});
   1.353 +
   1.354 +add_test(function test_orphan() {
   1.355 +  try {
   1.356 +
   1.357 +    _("Add a new bookmark locally.");
   1.358 +    let bmk1_id = PlacesUtils.bookmarks.insertBookmark(
   1.359 +      PlacesUtils.bookmarks.toolbarFolder, fxuri,
   1.360 +      PlacesUtils.bookmarks.DEFAULT_INDEX, "Get Firefox!");
   1.361 +    let bmk1_guid = store.GUIDForId(bmk1_id);
   1.362 +    do_check_eq(PlacesUtils.bookmarks.getFolderIdForItem(bmk1_id),
   1.363 +                PlacesUtils.bookmarks.toolbarFolder);
   1.364 +    let error;
   1.365 +    try {
   1.366 +      PlacesUtils.annotations.getItemAnnotation(bmk1_id, PARENT_ANNO);
   1.367 +    } catch(ex) {
   1.368 +      error = ex;
   1.369 +    }
   1.370 +    do_check_eq(error.result, Cr.NS_ERROR_NOT_AVAILABLE);
   1.371 +
   1.372 +    _("Apply a server record that is the same but refers to non-existent folder.");
   1.373 +    let record = store.createRecord(bmk1_guid);
   1.374 +    record.parentid = "non-existent";
   1.375 +    store.applyIncoming(record);
   1.376 +
   1.377 +    _("Verify that bookmark has been flagged as orphan, has not moved.");
   1.378 +    do_check_eq(PlacesUtils.bookmarks.getFolderIdForItem(bmk1_id),
   1.379 +                PlacesUtils.bookmarks.toolbarFolder);
   1.380 +    do_check_eq(PlacesUtils.annotations.getItemAnnotation(bmk1_id, PARENT_ANNO),
   1.381 +                "non-existent");
   1.382 +
   1.383 +  } finally {
   1.384 +    _("Clean up.");
   1.385 +    store.wipe();
   1.386 +    run_next_test();
   1.387 +  }
   1.388 +});
   1.389 +
   1.390 +add_test(function test_reparentOrphans() {
   1.391 +  try {
   1.392 +    let folder1_id = PlacesUtils.bookmarks.createFolder(
   1.393 +      PlacesUtils.bookmarks.toolbarFolder, "Folder1", 0);
   1.394 +    let folder1_guid = store.GUIDForId(folder1_id);
   1.395 +
   1.396 +    _("Create a bogus orphan record and write the record back to the store to trigger _reparentOrphans.");
   1.397 +    PlacesUtils.annotations.setItemAnnotation(
   1.398 +      folder1_id, PARENT_ANNO, folder1_guid, 0,
   1.399 +      PlacesUtils.annotations.EXPIRE_NEVER);
   1.400 +    let record = store.createRecord(folder1_guid);
   1.401 +    record.title = "New title for Folder 1";
   1.402 +    store._childrenToOrder = {};
   1.403 +    store.applyIncoming(record);
   1.404 +
   1.405 +    _("Verify that is has been marked as an orphan even though it couldn't be moved into itself.");
   1.406 +    do_check_eq(PlacesUtils.annotations.getItemAnnotation(folder1_id, PARENT_ANNO),
   1.407 +                folder1_guid);
   1.408 +
   1.409 +  } finally {
   1.410 +    _("Clean up.");
   1.411 +    store.wipe();
   1.412 +    run_next_test();
   1.413 +  }
   1.414 +});
   1.415 +
   1.416 +// Tests Bug 806460, in which query records arrive with empty folder
   1.417 +// names and missing bookmark URIs.
   1.418 +add_test(function test_empty_query_doesnt_die() {
   1.419 +  let record = new BookmarkQuery("bookmarks", "8xoDGqKrXf1P");
   1.420 +  record.folderName    = "";
   1.421 +  record.queryId       = "";
   1.422 +  record.parentName    = "Toolbar";
   1.423 +  record.parentid      = "toolbar";
   1.424 +
   1.425 +  // These should not throw.
   1.426 +  store.applyIncoming(record);
   1.427 +
   1.428 +  delete record.folderName;
   1.429 +  store.applyIncoming(record);
   1.430 +  
   1.431 +  run_next_test();
   1.432 +});
   1.433 +
   1.434 +function run_test() {
   1.435 +  initTestLogging('Trace');
   1.436 +  run_next_test();
   1.437 +}

mercurial