services/sync/tests/unit/test_bookmark_store.js

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

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 Cu.import("resource://gre/modules/PlacesUtils.jsm");
michael@0 5 Cu.import("resource://services-sync/engines.js");
michael@0 6 Cu.import("resource://services-sync/engines/bookmarks.js");
michael@0 7 Cu.import("resource://services-sync/service.js");
michael@0 8 Cu.import("resource://services-sync/util.js");
michael@0 9
michael@0 10 const PARENT_ANNO = "sync/parent";
michael@0 11
michael@0 12 Service.engineManager.register(BookmarksEngine);
michael@0 13
michael@0 14 let engine = Service.engineManager.get("bookmarks");
michael@0 15 let store = engine._store;
michael@0 16 let tracker = engine._tracker;
michael@0 17
michael@0 18 // Don't write some persistence files asynchronously.
michael@0 19 tracker.persistChangedIDs = false;
michael@0 20
michael@0 21 let fxuri = Utils.makeURI("http://getfirefox.com/");
michael@0 22 let tburi = Utils.makeURI("http://getthunderbird.com/");
michael@0 23
michael@0 24 add_test(function test_ignore_specials() {
michael@0 25 _("Ensure that we can't delete bookmark roots.");
michael@0 26
michael@0 27 // Belt...
michael@0 28 let record = new BookmarkFolder("bookmarks", "toolbar", "folder");
michael@0 29 record.deleted = true;
michael@0 30 do_check_neq(null, store.idForGUID("toolbar"));
michael@0 31
michael@0 32 store.applyIncoming(record);
michael@0 33
michael@0 34 // Ensure that the toolbar exists.
michael@0 35 do_check_neq(null, store.idForGUID("toolbar"));
michael@0 36
michael@0 37 // This will fail painfully in getItemType if the deletion worked.
michael@0 38 engine._buildGUIDMap();
michael@0 39
michael@0 40 // Braces...
michael@0 41 store.remove(record);
michael@0 42 do_check_neq(null, store.idForGUID("toolbar"));
michael@0 43 engine._buildGUIDMap();
michael@0 44
michael@0 45 store.wipe();
michael@0 46 run_next_test();
michael@0 47 });
michael@0 48
michael@0 49 add_test(function test_bookmark_create() {
michael@0 50 try {
michael@0 51 _("Ensure the record isn't present yet.");
michael@0 52 let ids = PlacesUtils.bookmarks.getBookmarkIdsForURI(fxuri, {});
michael@0 53 do_check_eq(ids.length, 0);
michael@0 54
michael@0 55 _("Let's create a new record.");
michael@0 56 let fxrecord = new Bookmark("bookmarks", "get-firefox1");
michael@0 57 fxrecord.bmkUri = fxuri.spec;
michael@0 58 fxrecord.description = "Firefox is awesome.";
michael@0 59 fxrecord.title = "Get Firefox!";
michael@0 60 fxrecord.tags = ["firefox", "awesome", "browser"];
michael@0 61 fxrecord.keyword = "awesome";
michael@0 62 fxrecord.loadInSidebar = false;
michael@0 63 fxrecord.parentName = "Bookmarks Toolbar";
michael@0 64 fxrecord.parentid = "toolbar";
michael@0 65 store.applyIncoming(fxrecord);
michael@0 66
michael@0 67 _("Verify it has been created correctly.");
michael@0 68 let id = store.idForGUID(fxrecord.id);
michael@0 69 do_check_eq(store.GUIDForId(id), fxrecord.id);
michael@0 70 do_check_eq(PlacesUtils.bookmarks.getItemType(id),
michael@0 71 PlacesUtils.bookmarks.TYPE_BOOKMARK);
michael@0 72 do_check_true(PlacesUtils.bookmarks.getBookmarkURI(id).equals(fxuri));
michael@0 73 do_check_eq(PlacesUtils.bookmarks.getItemTitle(id), fxrecord.title);
michael@0 74 do_check_eq(PlacesUtils.annotations.getItemAnnotation(id, "bookmarkProperties/description"),
michael@0 75 fxrecord.description);
michael@0 76 do_check_eq(PlacesUtils.bookmarks.getFolderIdForItem(id),
michael@0 77 PlacesUtils.bookmarks.toolbarFolder);
michael@0 78 do_check_eq(PlacesUtils.bookmarks.getKeywordForBookmark(id), fxrecord.keyword);
michael@0 79
michael@0 80 _("Have the store create a new record object. Verify that it has the same data.");
michael@0 81 let newrecord = store.createRecord(fxrecord.id);
michael@0 82 do_check_true(newrecord instanceof Bookmark);
michael@0 83 for each (let property in ["type", "bmkUri", "description", "title",
michael@0 84 "keyword", "parentName", "parentid"]) {
michael@0 85 do_check_eq(newrecord[property], fxrecord[property]);
michael@0 86 }
michael@0 87 do_check_true(Utils.deepEquals(newrecord.tags.sort(),
michael@0 88 fxrecord.tags.sort()));
michael@0 89
michael@0 90 _("The calculated sort index is based on frecency data.");
michael@0 91 do_check_true(newrecord.sortindex >= 150);
michael@0 92
michael@0 93 _("Create a record with some values missing.");
michael@0 94 let tbrecord = new Bookmark("bookmarks", "thunderbird1");
michael@0 95 tbrecord.bmkUri = tburi.spec;
michael@0 96 tbrecord.parentName = "Bookmarks Toolbar";
michael@0 97 tbrecord.parentid = "toolbar";
michael@0 98 store.applyIncoming(tbrecord);
michael@0 99
michael@0 100 _("Verify it has been created correctly.");
michael@0 101 id = store.idForGUID(tbrecord.id);
michael@0 102 do_check_eq(store.GUIDForId(id), tbrecord.id);
michael@0 103 do_check_eq(PlacesUtils.bookmarks.getItemType(id),
michael@0 104 PlacesUtils.bookmarks.TYPE_BOOKMARK);
michael@0 105 do_check_true(PlacesUtils.bookmarks.getBookmarkURI(id).equals(tburi));
michael@0 106 do_check_eq(PlacesUtils.bookmarks.getItemTitle(id), null);
michael@0 107 let error;
michael@0 108 try {
michael@0 109 PlacesUtils.annotations.getItemAnnotation(id, "bookmarkProperties/description");
michael@0 110 } catch(ex) {
michael@0 111 error = ex;
michael@0 112 }
michael@0 113 do_check_eq(error.result, Cr.NS_ERROR_NOT_AVAILABLE);
michael@0 114 do_check_eq(PlacesUtils.bookmarks.getFolderIdForItem(id),
michael@0 115 PlacesUtils.bookmarks.toolbarFolder);
michael@0 116 do_check_eq(PlacesUtils.bookmarks.getKeywordForBookmark(id), null);
michael@0 117 } finally {
michael@0 118 _("Clean up.");
michael@0 119 store.wipe();
michael@0 120 run_next_test();
michael@0 121 }
michael@0 122 });
michael@0 123
michael@0 124 add_test(function test_bookmark_update() {
michael@0 125 try {
michael@0 126 _("Create a bookmark whose values we'll change.");
michael@0 127 let bmk1_id = PlacesUtils.bookmarks.insertBookmark(
michael@0 128 PlacesUtils.bookmarks.toolbarFolder, fxuri,
michael@0 129 PlacesUtils.bookmarks.DEFAULT_INDEX,
michael@0 130 "Get Firefox!");
michael@0 131 PlacesUtils.annotations.setItemAnnotation(
michael@0 132 bmk1_id, "bookmarkProperties/description", "Firefox is awesome.", 0,
michael@0 133 PlacesUtils.annotations.EXPIRE_NEVER);
michael@0 134 PlacesUtils.bookmarks.setKeywordForBookmark(bmk1_id, "firefox");
michael@0 135 let bmk1_guid = store.GUIDForId(bmk1_id);
michael@0 136
michael@0 137 _("Update the record with some null values.");
michael@0 138 let record = store.createRecord(bmk1_guid);
michael@0 139 record.title = null;
michael@0 140 record.description = null;
michael@0 141 record.keyword = null;
michael@0 142 record.tags = null;
michael@0 143 store.applyIncoming(record);
michael@0 144
michael@0 145 _("Verify that the values have been cleared.");
michael@0 146 do_check_throws(function () {
michael@0 147 PlacesUtils.annotations.getItemAnnotation(
michael@0 148 bmk1_id, "bookmarkProperties/description");
michael@0 149 }, Cr.NS_ERROR_NOT_AVAILABLE);
michael@0 150 do_check_eq(PlacesUtils.bookmarks.getItemTitle(bmk1_id), null);
michael@0 151 do_check_eq(PlacesUtils.bookmarks.getKeywordForBookmark(bmk1_id), null);
michael@0 152 } finally {
michael@0 153 _("Clean up.");
michael@0 154 store.wipe();
michael@0 155 run_next_test();
michael@0 156 }
michael@0 157 });
michael@0 158
michael@0 159 add_test(function test_bookmark_createRecord() {
michael@0 160 try {
michael@0 161 _("Create a bookmark without a description or title.");
michael@0 162 let bmk1_id = PlacesUtils.bookmarks.insertBookmark(
michael@0 163 PlacesUtils.bookmarks.toolbarFolder, fxuri,
michael@0 164 PlacesUtils.bookmarks.DEFAULT_INDEX, null);
michael@0 165 let bmk1_guid = store.GUIDForId(bmk1_id);
michael@0 166
michael@0 167 _("Verify that the record is created accordingly.");
michael@0 168 let record = store.createRecord(bmk1_guid);
michael@0 169 do_check_eq(record.title, null);
michael@0 170 do_check_eq(record.description, null);
michael@0 171 do_check_eq(record.keyword, null);
michael@0 172
michael@0 173 } finally {
michael@0 174 _("Clean up.");
michael@0 175 store.wipe();
michael@0 176 run_next_test();
michael@0 177 }
michael@0 178 });
michael@0 179
michael@0 180 add_test(function test_folder_create() {
michael@0 181 try {
michael@0 182 _("Create a folder.");
michael@0 183 let folder = new BookmarkFolder("bookmarks", "testfolder-1");
michael@0 184 folder.parentName = "Bookmarks Toolbar";
michael@0 185 folder.parentid = "toolbar";
michael@0 186 folder.title = "Test Folder";
michael@0 187 store.applyIncoming(folder);
michael@0 188
michael@0 189 _("Verify it has been created correctly.");
michael@0 190 let id = store.idForGUID(folder.id);
michael@0 191 do_check_eq(PlacesUtils.bookmarks.getItemType(id),
michael@0 192 PlacesUtils.bookmarks.TYPE_FOLDER);
michael@0 193 do_check_eq(PlacesUtils.bookmarks.getItemTitle(id), folder.title);
michael@0 194 do_check_eq(PlacesUtils.bookmarks.getFolderIdForItem(id),
michael@0 195 PlacesUtils.bookmarks.toolbarFolder);
michael@0 196
michael@0 197 _("Have the store create a new record object. Verify that it has the same data.");
michael@0 198 let newrecord = store.createRecord(folder.id);
michael@0 199 do_check_true(newrecord instanceof BookmarkFolder);
michael@0 200 for each (let property in ["title", "parentName", "parentid"])
michael@0 201 do_check_eq(newrecord[property], folder[property]);
michael@0 202
michael@0 203 _("Folders have high sort index to ensure they're synced first.");
michael@0 204 do_check_eq(newrecord.sortindex, 1000000);
michael@0 205 } finally {
michael@0 206 _("Clean up.");
michael@0 207 store.wipe();
michael@0 208 run_next_test();
michael@0 209 }
michael@0 210 });
michael@0 211
michael@0 212 add_test(function test_folder_createRecord() {
michael@0 213 try {
michael@0 214 _("Create a folder.");
michael@0 215 let folder1_id = PlacesUtils.bookmarks.createFolder(
michael@0 216 PlacesUtils.bookmarks.toolbarFolder, "Folder1", 0);
michael@0 217 let folder1_guid = store.GUIDForId(folder1_id);
michael@0 218
michael@0 219 _("Create two bookmarks in that folder without assigning them GUIDs.");
michael@0 220 let bmk1_id = PlacesUtils.bookmarks.insertBookmark(
michael@0 221 folder1_id, fxuri, PlacesUtils.bookmarks.DEFAULT_INDEX, "Get Firefox!");
michael@0 222 let bmk2_id = PlacesUtils.bookmarks.insertBookmark(
michael@0 223 folder1_id, tburi, PlacesUtils.bookmarks.DEFAULT_INDEX, "Get Thunderbird!");
michael@0 224
michael@0 225 _("Create a record for the folder and verify basic properties.");
michael@0 226 let record = store.createRecord(folder1_guid);
michael@0 227 do_check_true(record instanceof BookmarkFolder);
michael@0 228 do_check_eq(record.title, "Folder1");
michael@0 229 do_check_eq(record.parentid, "toolbar");
michael@0 230 do_check_eq(record.parentName, "Bookmarks Toolbar");
michael@0 231
michael@0 232 _("Verify the folder's children. Ensures that the bookmarks were given GUIDs.");
michael@0 233 let bmk1_guid = store.GUIDForId(bmk1_id);
michael@0 234 let bmk2_guid = store.GUIDForId(bmk2_id);
michael@0 235 do_check_eq(record.children.length, 2);
michael@0 236 do_check_eq(record.children[0], bmk1_guid);
michael@0 237 do_check_eq(record.children[1], bmk2_guid);
michael@0 238
michael@0 239 } finally {
michael@0 240 _("Clean up.");
michael@0 241 store.wipe();
michael@0 242 run_next_test();
michael@0 243 }
michael@0 244 });
michael@0 245
michael@0 246 add_test(function test_deleted() {
michael@0 247 try {
michael@0 248 _("Create a bookmark that will be deleted.");
michael@0 249 let bmk1_id = PlacesUtils.bookmarks.insertBookmark(
michael@0 250 PlacesUtils.bookmarks.toolbarFolder, fxuri,
michael@0 251 PlacesUtils.bookmarks.DEFAULT_INDEX, "Get Firefox!");
michael@0 252 let bmk1_guid = store.GUIDForId(bmk1_id);
michael@0 253
michael@0 254 _("Delete the bookmark through the store.");
michael@0 255 let record = new PlacesItem("bookmarks", bmk1_guid);
michael@0 256 record.deleted = true;
michael@0 257 store.applyIncoming(record);
michael@0 258
michael@0 259 _("Ensure it has been deleted.");
michael@0 260 let error;
michael@0 261 try {
michael@0 262 PlacesUtils.bookmarks.getBookmarkURI(bmk1_id);
michael@0 263 } catch(ex) {
michael@0 264 error = ex;
michael@0 265 }
michael@0 266 do_check_eq(error.result, Cr.NS_ERROR_ILLEGAL_VALUE);
michael@0 267
michael@0 268 let newrec = store.createRecord(bmk1_guid);
michael@0 269 do_check_eq(newrec.deleted, true);
michael@0 270
michael@0 271 } finally {
michael@0 272 _("Clean up.");
michael@0 273 store.wipe();
michael@0 274 run_next_test();
michael@0 275 }
michael@0 276 });
michael@0 277
michael@0 278 add_test(function test_move_folder() {
michael@0 279 try {
michael@0 280 _("Create two folders and a bookmark in one of them.");
michael@0 281 let folder1_id = PlacesUtils.bookmarks.createFolder(
michael@0 282 PlacesUtils.bookmarks.toolbarFolder, "Folder1", 0);
michael@0 283 let folder1_guid = store.GUIDForId(folder1_id);
michael@0 284 let folder2_id = PlacesUtils.bookmarks.createFolder(
michael@0 285 PlacesUtils.bookmarks.toolbarFolder, "Folder2", 0);
michael@0 286 let folder2_guid = store.GUIDForId(folder2_id);
michael@0 287 let bmk_id = PlacesUtils.bookmarks.insertBookmark(
michael@0 288 folder1_id, fxuri, PlacesUtils.bookmarks.DEFAULT_INDEX, "Get Firefox!");
michael@0 289 let bmk_guid = store.GUIDForId(bmk_id);
michael@0 290
michael@0 291 _("Get a record, reparent it and apply it to the store.");
michael@0 292 let record = store.createRecord(bmk_guid);
michael@0 293 do_check_eq(record.parentid, folder1_guid);
michael@0 294 record.parentid = folder2_guid;
michael@0 295 store.applyIncoming(record);
michael@0 296
michael@0 297 _("Verify the new parent.");
michael@0 298 let new_folder_id = PlacesUtils.bookmarks.getFolderIdForItem(bmk_id);
michael@0 299 do_check_eq(store.GUIDForId(new_folder_id), folder2_guid);
michael@0 300 } finally {
michael@0 301 _("Clean up.");
michael@0 302 store.wipe();
michael@0 303 run_next_test();
michael@0 304 }
michael@0 305 });
michael@0 306
michael@0 307 add_test(function test_move_order() {
michael@0 308 // Make sure the tracker is turned on.
michael@0 309 Svc.Obs.notify("weave:engine:start-tracking");
michael@0 310 try {
michael@0 311 _("Create two bookmarks");
michael@0 312 let bmk1_id = PlacesUtils.bookmarks.insertBookmark(
michael@0 313 PlacesUtils.bookmarks.toolbarFolder, fxuri,
michael@0 314 PlacesUtils.bookmarks.DEFAULT_INDEX, "Get Firefox!");
michael@0 315 let bmk1_guid = store.GUIDForId(bmk1_id);
michael@0 316 let bmk2_id = PlacesUtils.bookmarks.insertBookmark(
michael@0 317 PlacesUtils.bookmarks.toolbarFolder, tburi,
michael@0 318 PlacesUtils.bookmarks.DEFAULT_INDEX, "Get Thunderbird!");
michael@0 319 let bmk2_guid = store.GUIDForId(bmk2_id);
michael@0 320
michael@0 321 _("Verify order.");
michael@0 322 do_check_eq(PlacesUtils.bookmarks.getItemIndex(bmk1_id), 0);
michael@0 323 do_check_eq(PlacesUtils.bookmarks.getItemIndex(bmk2_id), 1);
michael@0 324 let toolbar = store.createRecord("toolbar");
michael@0 325 do_check_eq(toolbar.children.length, 2);
michael@0 326 do_check_eq(toolbar.children[0], bmk1_guid);
michael@0 327 do_check_eq(toolbar.children[1], bmk2_guid);
michael@0 328
michael@0 329 _("Move bookmarks around.");
michael@0 330 store._childrenToOrder = {};
michael@0 331 toolbar.children = [bmk2_guid, bmk1_guid];
michael@0 332 store.applyIncoming(toolbar);
michael@0 333 // Bookmarks engine does this at the end of _processIncoming
michael@0 334 tracker.ignoreAll = true;
michael@0 335 store._orderChildren();
michael@0 336 tracker.ignoreAll = false;
michael@0 337 delete store._childrenToOrder;
michael@0 338
michael@0 339 _("Verify new order.");
michael@0 340 do_check_eq(PlacesUtils.bookmarks.getItemIndex(bmk2_id), 0);
michael@0 341 do_check_eq(PlacesUtils.bookmarks.getItemIndex(bmk1_id), 1);
michael@0 342
michael@0 343 } finally {
michael@0 344 Svc.Obs.notify("weave:engine:stop-tracking");
michael@0 345 _("Clean up.");
michael@0 346 store.wipe();
michael@0 347 run_next_test();
michael@0 348 }
michael@0 349 });
michael@0 350
michael@0 351 add_test(function test_orphan() {
michael@0 352 try {
michael@0 353
michael@0 354 _("Add a new bookmark locally.");
michael@0 355 let bmk1_id = PlacesUtils.bookmarks.insertBookmark(
michael@0 356 PlacesUtils.bookmarks.toolbarFolder, fxuri,
michael@0 357 PlacesUtils.bookmarks.DEFAULT_INDEX, "Get Firefox!");
michael@0 358 let bmk1_guid = store.GUIDForId(bmk1_id);
michael@0 359 do_check_eq(PlacesUtils.bookmarks.getFolderIdForItem(bmk1_id),
michael@0 360 PlacesUtils.bookmarks.toolbarFolder);
michael@0 361 let error;
michael@0 362 try {
michael@0 363 PlacesUtils.annotations.getItemAnnotation(bmk1_id, PARENT_ANNO);
michael@0 364 } catch(ex) {
michael@0 365 error = ex;
michael@0 366 }
michael@0 367 do_check_eq(error.result, Cr.NS_ERROR_NOT_AVAILABLE);
michael@0 368
michael@0 369 _("Apply a server record that is the same but refers to non-existent folder.");
michael@0 370 let record = store.createRecord(bmk1_guid);
michael@0 371 record.parentid = "non-existent";
michael@0 372 store.applyIncoming(record);
michael@0 373
michael@0 374 _("Verify that bookmark has been flagged as orphan, has not moved.");
michael@0 375 do_check_eq(PlacesUtils.bookmarks.getFolderIdForItem(bmk1_id),
michael@0 376 PlacesUtils.bookmarks.toolbarFolder);
michael@0 377 do_check_eq(PlacesUtils.annotations.getItemAnnotation(bmk1_id, PARENT_ANNO),
michael@0 378 "non-existent");
michael@0 379
michael@0 380 } finally {
michael@0 381 _("Clean up.");
michael@0 382 store.wipe();
michael@0 383 run_next_test();
michael@0 384 }
michael@0 385 });
michael@0 386
michael@0 387 add_test(function test_reparentOrphans() {
michael@0 388 try {
michael@0 389 let folder1_id = PlacesUtils.bookmarks.createFolder(
michael@0 390 PlacesUtils.bookmarks.toolbarFolder, "Folder1", 0);
michael@0 391 let folder1_guid = store.GUIDForId(folder1_id);
michael@0 392
michael@0 393 _("Create a bogus orphan record and write the record back to the store to trigger _reparentOrphans.");
michael@0 394 PlacesUtils.annotations.setItemAnnotation(
michael@0 395 folder1_id, PARENT_ANNO, folder1_guid, 0,
michael@0 396 PlacesUtils.annotations.EXPIRE_NEVER);
michael@0 397 let record = store.createRecord(folder1_guid);
michael@0 398 record.title = "New title for Folder 1";
michael@0 399 store._childrenToOrder = {};
michael@0 400 store.applyIncoming(record);
michael@0 401
michael@0 402 _("Verify that is has been marked as an orphan even though it couldn't be moved into itself.");
michael@0 403 do_check_eq(PlacesUtils.annotations.getItemAnnotation(folder1_id, PARENT_ANNO),
michael@0 404 folder1_guid);
michael@0 405
michael@0 406 } finally {
michael@0 407 _("Clean up.");
michael@0 408 store.wipe();
michael@0 409 run_next_test();
michael@0 410 }
michael@0 411 });
michael@0 412
michael@0 413 // Tests Bug 806460, in which query records arrive with empty folder
michael@0 414 // names and missing bookmark URIs.
michael@0 415 add_test(function test_empty_query_doesnt_die() {
michael@0 416 let record = new BookmarkQuery("bookmarks", "8xoDGqKrXf1P");
michael@0 417 record.folderName = "";
michael@0 418 record.queryId = "";
michael@0 419 record.parentName = "Toolbar";
michael@0 420 record.parentid = "toolbar";
michael@0 421
michael@0 422 // These should not throw.
michael@0 423 store.applyIncoming(record);
michael@0 424
michael@0 425 delete record.folderName;
michael@0 426 store.applyIncoming(record);
michael@0 427
michael@0 428 run_next_test();
michael@0 429 });
michael@0 430
michael@0 431 function run_test() {
michael@0 432 initTestLogging('Trace');
michael@0 433 run_next_test();
michael@0 434 }

mercurial