services/sync/tests/unit/test_bookmark_engine.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://gre/modules/BookmarkJSONUtils.jsm");
michael@0 6 Cu.import("resource://services-common/async.js");
michael@0 7 Cu.import("resource://gre/modules/Log.jsm");
michael@0 8 Cu.import("resource://services-sync/engines.js");
michael@0 9 Cu.import("resource://services-sync/engines/bookmarks.js");
michael@0 10 Cu.import("resource://services-sync/service.js");
michael@0 11 Cu.import("resource://services-sync/util.js");
michael@0 12 Cu.import("resource://testing-common/services/sync/utils.js");
michael@0 13 Cu.import("resource://gre/modules/Promise.jsm");
michael@0 14
michael@0 15 Service.engineManager.register(BookmarksEngine);
michael@0 16
michael@0 17 add_test(function bad_record_allIDs() {
michael@0 18 let server = new SyncServer();
michael@0 19 server.start();
michael@0 20 let syncTesting = new SyncTestingInfrastructure(server.server);
michael@0 21
michael@0 22 _("Ensure that bad Places queries don't cause an error in getAllIDs.");
michael@0 23 let engine = new BookmarksEngine(Service);
michael@0 24 let store = engine._store;
michael@0 25 let badRecordID = PlacesUtils.bookmarks.insertBookmark(
michael@0 26 PlacesUtils.bookmarks.toolbarFolder,
michael@0 27 Utils.makeURI("place:folder=1138"),
michael@0 28 PlacesUtils.bookmarks.DEFAULT_INDEX,
michael@0 29 null);
michael@0 30
michael@0 31 do_check_true(badRecordID > 0);
michael@0 32 _("Record is " + badRecordID);
michael@0 33 _("Type: " + PlacesUtils.bookmarks.getItemType(badRecordID));
michael@0 34
michael@0 35 _("Fetching children.");
michael@0 36 store._getChildren("toolbar", {});
michael@0 37
michael@0 38 _("Fetching all IDs.");
michael@0 39 let all = store.getAllIDs();
michael@0 40
michael@0 41 _("All IDs: " + JSON.stringify(all));
michael@0 42 do_check_true("menu" in all);
michael@0 43 do_check_true("toolbar" in all);
michael@0 44
michael@0 45 _("Clean up.");
michael@0 46 PlacesUtils.bookmarks.removeItem(badRecordID);
michael@0 47 server.stop(run_next_test);
michael@0 48 });
michael@0 49
michael@0 50 add_test(function test_ID_caching() {
michael@0 51 let server = new SyncServer();
michael@0 52 server.start();
michael@0 53 let syncTesting = new SyncTestingInfrastructure(server.server);
michael@0 54
michael@0 55 _("Ensure that Places IDs are not cached.");
michael@0 56 let engine = new BookmarksEngine(Service);
michael@0 57 let store = engine._store;
michael@0 58 _("All IDs: " + JSON.stringify(store.getAllIDs()));
michael@0 59
michael@0 60 let mobileID = store.idForGUID("mobile");
michael@0 61 _("Change the GUID for that item, and drop the mobile anno.");
michael@0 62 store._setGUID(mobileID, "abcdefghijkl");
michael@0 63 PlacesUtils.annotations.removeItemAnnotation(mobileID, "mobile/bookmarksRoot");
michael@0 64
michael@0 65 let err;
michael@0 66 let newMobileID;
michael@0 67
michael@0 68 // With noCreate, we don't find an entry.
michael@0 69 try {
michael@0 70 newMobileID = store.idForGUID("mobile", true);
michael@0 71 _("New mobile ID: " + newMobileID);
michael@0 72 } catch (ex) {
michael@0 73 err = ex;
michael@0 74 _("Error: " + Utils.exceptionStr(err));
michael@0 75 }
michael@0 76
michael@0 77 do_check_true(!err);
michael@0 78
michael@0 79 // With !noCreate, lookup works, and it's different.
michael@0 80 newMobileID = store.idForGUID("mobile", false);
michael@0 81 _("New mobile ID: " + newMobileID);
michael@0 82 do_check_true(!!newMobileID);
michael@0 83 do_check_neq(newMobileID, mobileID);
michael@0 84
michael@0 85 // And it's repeatable, even with creation enabled.
michael@0 86 do_check_eq(newMobileID, store.idForGUID("mobile", false));
michael@0 87
michael@0 88 do_check_eq(store.GUIDForId(mobileID), "abcdefghijkl");
michael@0 89 server.stop(run_next_test);
michael@0 90 });
michael@0 91
michael@0 92 function serverForFoo(engine) {
michael@0 93 return serverForUsers({"foo": "password"}, {
michael@0 94 meta: {global: {engines: {bookmarks: {version: engine.version,
michael@0 95 syncID: engine.syncID}}}},
michael@0 96 bookmarks: {}
michael@0 97 });
michael@0 98 }
michael@0 99
michael@0 100 add_test(function test_processIncoming_error_orderChildren() {
michael@0 101 _("Ensure that _orderChildren() is called even when _processIncoming() throws an error.");
michael@0 102
michael@0 103 let engine = new BookmarksEngine(Service);
michael@0 104 let store = engine._store;
michael@0 105 let server = serverForFoo(engine);
michael@0 106 new SyncTestingInfrastructure(server.server);
michael@0 107
michael@0 108 let collection = server.user("foo").collection("bookmarks");
michael@0 109
michael@0 110 try {
michael@0 111
michael@0 112 let folder1_id = PlacesUtils.bookmarks.createFolder(
michael@0 113 PlacesUtils.bookmarks.toolbarFolder, "Folder 1", 0);
michael@0 114 let folder1_guid = store.GUIDForId(folder1_id);
michael@0 115
michael@0 116 let fxuri = Utils.makeURI("http://getfirefox.com/");
michael@0 117 let tburi = Utils.makeURI("http://getthunderbird.com/");
michael@0 118
michael@0 119 let bmk1_id = PlacesUtils.bookmarks.insertBookmark(
michael@0 120 folder1_id, fxuri, PlacesUtils.bookmarks.DEFAULT_INDEX, "Get Firefox!");
michael@0 121 let bmk1_guid = store.GUIDForId(bmk1_id);
michael@0 122 let bmk2_id = PlacesUtils.bookmarks.insertBookmark(
michael@0 123 folder1_id, tburi, PlacesUtils.bookmarks.DEFAULT_INDEX, "Get Thunderbird!");
michael@0 124 let bmk2_guid = store.GUIDForId(bmk2_id);
michael@0 125
michael@0 126 // Create a server record for folder1 where we flip the order of
michael@0 127 // the children.
michael@0 128 let folder1_payload = store.createRecord(folder1_guid).cleartext;
michael@0 129 folder1_payload.children.reverse();
michael@0 130 collection.insert(folder1_guid, encryptPayload(folder1_payload));
michael@0 131
michael@0 132 // Create a bogus record that when synced down will provoke a
michael@0 133 // network error which in turn provokes an exception in _processIncoming.
michael@0 134 const BOGUS_GUID = "zzzzzzzzzzzz";
michael@0 135 let bogus_record = collection.insert(BOGUS_GUID, "I'm a bogus record!");
michael@0 136 bogus_record.get = function get() {
michael@0 137 throw "Sync this!";
michael@0 138 };
michael@0 139
michael@0 140 // Make the 10 minutes old so it will only be synced in the toFetch phase.
michael@0 141 bogus_record.modified = Date.now() / 1000 - 60 * 10;
michael@0 142 engine.lastSync = Date.now() / 1000 - 60;
michael@0 143 engine.toFetch = [BOGUS_GUID];
michael@0 144
michael@0 145 let error;
michael@0 146 try {
michael@0 147 engine.sync();
michael@0 148 } catch(ex) {
michael@0 149 error = ex;
michael@0 150 }
michael@0 151 do_check_true(!!error);
michael@0 152
michael@0 153 // Verify that the bookmark order has been applied.
michael@0 154 let new_children = store.createRecord(folder1_guid).children;
michael@0 155 do_check_eq(new_children.length, 2);
michael@0 156 do_check_eq(new_children[0], folder1_payload.children[0]);
michael@0 157 do_check_eq(new_children[1], folder1_payload.children[1]);
michael@0 158
michael@0 159 do_check_eq(PlacesUtils.bookmarks.getItemIndex(bmk1_id), 1);
michael@0 160 do_check_eq(PlacesUtils.bookmarks.getItemIndex(bmk2_id), 0);
michael@0 161
michael@0 162 } finally {
michael@0 163 store.wipe();
michael@0 164 Svc.Prefs.resetBranch("");
michael@0 165 Service.recordManager.clearCache();
michael@0 166 server.stop(run_next_test);
michael@0 167 }
michael@0 168 });
michael@0 169
michael@0 170 add_task(function test_restorePromptsReupload() {
michael@0 171 _("Ensure that restoring from a backup will reupload all records.");
michael@0 172 let engine = new BookmarksEngine(Service);
michael@0 173 let store = engine._store;
michael@0 174 let server = serverForFoo(engine);
michael@0 175 new SyncTestingInfrastructure(server.server);
michael@0 176
michael@0 177 let collection = server.user("foo").collection("bookmarks");
michael@0 178
michael@0 179 Svc.Obs.notify("weave:engine:start-tracking"); // We skip usual startup...
michael@0 180
michael@0 181 try {
michael@0 182
michael@0 183 let folder1_id = PlacesUtils.bookmarks.createFolder(
michael@0 184 PlacesUtils.bookmarks.toolbarFolder, "Folder 1", 0);
michael@0 185 let folder1_guid = store.GUIDForId(folder1_id);
michael@0 186 _("Folder 1: " + folder1_id + ", " + folder1_guid);
michael@0 187
michael@0 188 let fxuri = Utils.makeURI("http://getfirefox.com/");
michael@0 189 let tburi = Utils.makeURI("http://getthunderbird.com/");
michael@0 190
michael@0 191 _("Create a single record.");
michael@0 192 let bmk1_id = PlacesUtils.bookmarks.insertBookmark(
michael@0 193 folder1_id, fxuri, PlacesUtils.bookmarks.DEFAULT_INDEX, "Get Firefox!");
michael@0 194 let bmk1_guid = store.GUIDForId(bmk1_id);
michael@0 195 _("Get Firefox!: " + bmk1_id + ", " + bmk1_guid);
michael@0 196
michael@0 197
michael@0 198 let dirSvc = Cc["@mozilla.org/file/directory_service;1"]
michael@0 199 .getService(Ci.nsIProperties);
michael@0 200
michael@0 201 let backupFile = dirSvc.get("TmpD", Ci.nsILocalFile);
michael@0 202
michael@0 203 _("Make a backup.");
michael@0 204 backupFile.append("t_b_e_" + Date.now() + ".json");
michael@0 205
michael@0 206 _("Backing up to file " + backupFile.path);
michael@0 207 backupFile.create(Ci.nsILocalFile.NORMAL_FILE_TYPE, 0600);
michael@0 208 yield BookmarkJSONUtils.exportToFile(backupFile);
michael@0 209
michael@0 210 _("Create a different record and sync.");
michael@0 211 let bmk2_id = PlacesUtils.bookmarks.insertBookmark(
michael@0 212 folder1_id, tburi, PlacesUtils.bookmarks.DEFAULT_INDEX, "Get Thunderbird!");
michael@0 213 let bmk2_guid = store.GUIDForId(bmk2_id);
michael@0 214 _("Get Thunderbird!: " + bmk2_id + ", " + bmk2_guid);
michael@0 215
michael@0 216 PlacesUtils.bookmarks.removeItem(bmk1_id);
michael@0 217
michael@0 218 let error;
michael@0 219 try {
michael@0 220 engine.sync();
michael@0 221 } catch(ex) {
michael@0 222 error = ex;
michael@0 223 _("Got error: " + Utils.exceptionStr(ex));
michael@0 224 }
michael@0 225 do_check_true(!error);
michael@0 226
michael@0 227 _("Verify that there's only one bookmark on the server, and it's Thunderbird.");
michael@0 228 // Of course, there's also the Bookmarks Toolbar and Bookmarks Menu...
michael@0 229 let wbos = collection.keys(function (id) {
michael@0 230 return ["menu", "toolbar", "mobile", folder1_guid].indexOf(id) == -1;
michael@0 231 });
michael@0 232 do_check_eq(wbos.length, 1);
michael@0 233 do_check_eq(wbos[0], bmk2_guid);
michael@0 234
michael@0 235 _("Now restore from a backup.");
michael@0 236 yield BookmarkJSONUtils.importFromFile(backupFile, true);
michael@0 237
michael@0 238 _("Ensure we have the bookmarks we expect locally.");
michael@0 239 let guids = store.getAllIDs();
michael@0 240 _("GUIDs: " + JSON.stringify(guids));
michael@0 241 let found = false;
michael@0 242 let count = 0;
michael@0 243 let newFX;
michael@0 244 for (let guid in guids) {
michael@0 245 count++;
michael@0 246 let id = store.idForGUID(guid, true);
michael@0 247 // Only one bookmark, so _all_ should be Firefox!
michael@0 248 if (PlacesUtils.bookmarks.getItemType(id) == PlacesUtils.bookmarks.TYPE_BOOKMARK) {
michael@0 249 let uri = PlacesUtils.bookmarks.getBookmarkURI(id);
michael@0 250 _("Found URI " + uri.spec + " for GUID " + guid);
michael@0 251 do_check_eq(uri.spec, fxuri.spec);
michael@0 252 newFX = guid; // Save the new GUID after restore.
michael@0 253 found = true; // Only runs if the above check passes.
michael@0 254 }
michael@0 255 }
michael@0 256 _("We found it: " + found);
michael@0 257 do_check_true(found);
michael@0 258
michael@0 259 _("Have the correct number of IDs locally, too.");
michael@0 260 do_check_eq(count, ["menu", "toolbar", folder1_id, bmk1_id].length);
michael@0 261
michael@0 262 _("Sync again. This'll wipe bookmarks from the server.");
michael@0 263 try {
michael@0 264 engine.sync();
michael@0 265 } catch(ex) {
michael@0 266 error = ex;
michael@0 267 _("Got error: " + Utils.exceptionStr(ex));
michael@0 268 }
michael@0 269 do_check_true(!error);
michael@0 270
michael@0 271 _("Verify that there's only one bookmark on the server, and it's Firefox.");
michael@0 272 // Of course, there's also the Bookmarks Toolbar and Bookmarks Menu...
michael@0 273 let payloads = server.user("foo").collection("bookmarks").payloads();
michael@0 274 let bookmarkWBOs = payloads.filter(function (wbo) {
michael@0 275 return wbo.type == "bookmark";
michael@0 276 });
michael@0 277 let folderWBOs = payloads.filter(function (wbo) {
michael@0 278 return ((wbo.type == "folder") &&
michael@0 279 (wbo.id != "menu") &&
michael@0 280 (wbo.id != "toolbar"));
michael@0 281 });
michael@0 282
michael@0 283 do_check_eq(bookmarkWBOs.length, 1);
michael@0 284 do_check_eq(bookmarkWBOs[0].id, newFX);
michael@0 285 do_check_eq(bookmarkWBOs[0].bmkUri, fxuri.spec);
michael@0 286 do_check_eq(bookmarkWBOs[0].title, "Get Firefox!");
michael@0 287
michael@0 288 _("Our old friend Folder 1 is still in play.");
michael@0 289 do_check_eq(folderWBOs.length, 1);
michael@0 290 do_check_eq(folderWBOs[0].title, "Folder 1");
michael@0 291
michael@0 292 } finally {
michael@0 293 store.wipe();
michael@0 294 Svc.Prefs.resetBranch("");
michael@0 295 Service.recordManager.clearCache();
michael@0 296 let deferred = Promise.defer();
michael@0 297 server.stop(deferred.resolve);
michael@0 298 yield deferred.promise;
michael@0 299 }
michael@0 300 });
michael@0 301
michael@0 302 function FakeRecord(constructor, r) {
michael@0 303 constructor.call(this, "bookmarks", r.id);
michael@0 304 for (let x in r) {
michael@0 305 this[x] = r[x];
michael@0 306 }
michael@0 307 }
michael@0 308
michael@0 309 // Bug 632287.
michael@0 310 add_test(function test_mismatched_types() {
michael@0 311 _("Ensure that handling a record that changes type causes deletion " +
michael@0 312 "then re-adding.");
michael@0 313
michael@0 314 let oldRecord = {
michael@0 315 "id": "l1nZZXfB8nC7",
michael@0 316 "type":"folder",
michael@0 317 "parentName":"Bookmarks Toolbar",
michael@0 318 "title":"Innerst i Sneglehode",
michael@0 319 "description":null,
michael@0 320 "parentid": "toolbar"
michael@0 321 };
michael@0 322
michael@0 323 let newRecord = {
michael@0 324 "id": "l1nZZXfB8nC7",
michael@0 325 "type":"livemark",
michael@0 326 "siteUri":"http://sneglehode.wordpress.com/",
michael@0 327 "feedUri":"http://sneglehode.wordpress.com/feed/",
michael@0 328 "parentName":"Bookmarks Toolbar",
michael@0 329 "title":"Innerst i Sneglehode",
michael@0 330 "description":null,
michael@0 331 "children":
michael@0 332 ["HCRq40Rnxhrd", "YeyWCV1RVsYw", "GCceVZMhvMbP", "sYi2hevdArlF",
michael@0 333 "vjbZlPlSyGY8", "UtjUhVyrpeG6", "rVq8WMG2wfZI", "Lx0tcy43ZKhZ",
michael@0 334 "oT74WwV8_j4P", "IztsItWVSo3-"],
michael@0 335 "parentid": "toolbar"
michael@0 336 };
michael@0 337
michael@0 338 let engine = new BookmarksEngine(Service);
michael@0 339 let store = engine._store;
michael@0 340 let server = serverForFoo(engine);
michael@0 341 new SyncTestingInfrastructure(server.server);
michael@0 342
michael@0 343 _("GUID: " + store.GUIDForId(6, true));
michael@0 344
michael@0 345 try {
michael@0 346 let bms = PlacesUtils.bookmarks;
michael@0 347 let oldR = new FakeRecord(BookmarkFolder, oldRecord);
michael@0 348 let newR = new FakeRecord(Livemark, newRecord);
michael@0 349 oldR._parent = PlacesUtils.bookmarks.toolbarFolder;
michael@0 350 newR._parent = PlacesUtils.bookmarks.toolbarFolder;
michael@0 351
michael@0 352 store.applyIncoming(oldR);
michael@0 353 _("Applied old. It's a folder.");
michael@0 354 let oldID = store.idForGUID(oldR.id);
michael@0 355 _("Old ID: " + oldID);
michael@0 356 do_check_eq(bms.getItemType(oldID), bms.TYPE_FOLDER);
michael@0 357 do_check_false(PlacesUtils.annotations
michael@0 358 .itemHasAnnotation(oldID, PlacesUtils.LMANNO_FEEDURI));
michael@0 359
michael@0 360 store.applyIncoming(newR);
michael@0 361 let newID = store.idForGUID(newR.id);
michael@0 362 _("New ID: " + newID);
michael@0 363
michael@0 364 _("Applied new. It's a livemark.");
michael@0 365 do_check_eq(bms.getItemType(newID), bms.TYPE_FOLDER);
michael@0 366 do_check_true(PlacesUtils.annotations
michael@0 367 .itemHasAnnotation(newID, PlacesUtils.LMANNO_FEEDURI));
michael@0 368
michael@0 369 } finally {
michael@0 370 store.wipe();
michael@0 371 Svc.Prefs.resetBranch("");
michael@0 372 Service.recordManager.clearCache();
michael@0 373 server.stop(run_next_test);
michael@0 374 }
michael@0 375 });
michael@0 376
michael@0 377 add_test(function test_bookmark_guidMap_fail() {
michael@0 378 _("Ensure that failures building the GUID map cause early death.");
michael@0 379
michael@0 380 let engine = new BookmarksEngine(Service);
michael@0 381 let store = engine._store;
michael@0 382
michael@0 383 let store = engine._store;
michael@0 384 let server = serverForFoo(engine);
michael@0 385 let coll = server.user("foo").collection("bookmarks");
michael@0 386 new SyncTestingInfrastructure(server.server);
michael@0 387
michael@0 388 // Add one item to the server.
michael@0 389 let itemID = PlacesUtils.bookmarks.createFolder(
michael@0 390 PlacesUtils.bookmarks.toolbarFolder, "Folder 1", 0);
michael@0 391 let itemGUID = store.GUIDForId(itemID);
michael@0 392 let itemPayload = store.createRecord(itemGUID).cleartext;
michael@0 393 coll.insert(itemGUID, encryptPayload(itemPayload));
michael@0 394
michael@0 395 engine.lastSync = 1; // So we don't back up.
michael@0 396
michael@0 397 // Make building the GUID map fail.
michael@0 398 store.getAllIDs = function () { throw "Nooo"; };
michael@0 399
michael@0 400 // Ensure that we throw when accessing _guidMap.
michael@0 401 engine._syncStartup();
michael@0 402 _("No error.");
michael@0 403 do_check_false(engine._guidMapFailed);
michael@0 404
michael@0 405 _("We get an error if building _guidMap fails in use.");
michael@0 406 let err;
michael@0 407 try {
michael@0 408 _(engine._guidMap);
michael@0 409 } catch (ex) {
michael@0 410 err = ex;
michael@0 411 }
michael@0 412 do_check_eq(err.code, Engine.prototype.eEngineAbortApplyIncoming);
michael@0 413 do_check_eq(err.cause, "Nooo");
michael@0 414
michael@0 415 _("We get an error and abort during processIncoming.");
michael@0 416 err = undefined;
michael@0 417 try {
michael@0 418 engine._processIncoming();
michael@0 419 } catch (ex) {
michael@0 420 err = ex;
michael@0 421 }
michael@0 422 do_check_eq(err, "Nooo");
michael@0 423
michael@0 424 server.stop(run_next_test);
michael@0 425 });
michael@0 426
michael@0 427 add_test(function test_bookmark_is_taggable() {
michael@0 428 let engine = new BookmarksEngine(Service);
michael@0 429 let store = engine._store;
michael@0 430
michael@0 431 do_check_true(store.isTaggable("bookmark"));
michael@0 432 do_check_true(store.isTaggable("microsummary"));
michael@0 433 do_check_true(store.isTaggable("query"));
michael@0 434 do_check_false(store.isTaggable("folder"));
michael@0 435 do_check_false(store.isTaggable("livemark"));
michael@0 436 do_check_false(store.isTaggable(null));
michael@0 437 do_check_false(store.isTaggable(undefined));
michael@0 438 do_check_false(store.isTaggable(""));
michael@0 439
michael@0 440 run_next_test();
michael@0 441 });
michael@0 442
michael@0 443 add_test(function test_bookmark_tag_but_no_uri() {
michael@0 444 _("Ensure that a bookmark record with tags, but no URI, doesn't throw an exception.");
michael@0 445
michael@0 446 let engine = new BookmarksEngine(Service);
michael@0 447 let store = engine._store;
michael@0 448
michael@0 449 // We're simply checking that no exception is thrown, so
michael@0 450 // no actual checks in this test.
michael@0 451
michael@0 452 store._tagURI(null, ["foo"]);
michael@0 453 store._tagURI(null, null);
michael@0 454 store._tagURI(Utils.makeURI("about:fake"), null);
michael@0 455
michael@0 456 let record = {
michael@0 457 _parent: PlacesUtils.bookmarks.toolbarFolder,
michael@0 458 id: Utils.makeGUID(),
michael@0 459 description: "",
michael@0 460 tags: ["foo"],
michael@0 461 title: "Taggy tag",
michael@0 462 type: "folder"
michael@0 463 };
michael@0 464
michael@0 465 // Because update() walks the cleartext.
michael@0 466 record.cleartext = record;
michael@0 467
michael@0 468 store.create(record);
michael@0 469 record.tags = ["bar"];
michael@0 470 store.update(record);
michael@0 471
michael@0 472 run_next_test();
michael@0 473 });
michael@0 474
michael@0 475 add_test(function test_misreconciled_root() {
michael@0 476 _("Ensure that we don't reconcile an arbitrary record with a root.");
michael@0 477
michael@0 478 let engine = new BookmarksEngine(Service);
michael@0 479 let store = engine._store;
michael@0 480 let server = serverForFoo(engine);
michael@0 481
michael@0 482 // Log real hard for this test.
michael@0 483 store._log.trace = store._log.debug;
michael@0 484 engine._log.trace = engine._log.debug;
michael@0 485
michael@0 486 engine._syncStartup();
michael@0 487
michael@0 488 // Let's find out where the toolbar is right now.
michael@0 489 let toolbarBefore = store.createRecord("toolbar", "bookmarks");
michael@0 490 let toolbarIDBefore = store.idForGUID("toolbar");
michael@0 491 do_check_neq(-1, toolbarIDBefore);
michael@0 492
michael@0 493 let parentGUIDBefore = toolbarBefore.parentid;
michael@0 494 let parentIDBefore = store.idForGUID(parentGUIDBefore);
michael@0 495 do_check_neq(-1, parentIDBefore);
michael@0 496 do_check_eq("string", typeof(parentGUIDBefore));
michael@0 497
michael@0 498 _("Current parent: " + parentGUIDBefore + " (" + parentIDBefore + ").");
michael@0 499
michael@0 500 let to_apply = {
michael@0 501 id: "zzzzzzzzzzzz",
michael@0 502 type: "folder",
michael@0 503 title: "Bookmarks Toolbar",
michael@0 504 description: "Now you're for it.",
michael@0 505 parentName: "",
michael@0 506 parentid: "mobile", // Why not?
michael@0 507 children: [],
michael@0 508 };
michael@0 509
michael@0 510 let rec = new FakeRecord(BookmarkFolder, to_apply);
michael@0 511 let encrypted = encryptPayload(rec.cleartext);
michael@0 512 encrypted.decrypt = function () {
michael@0 513 for (let x in rec) {
michael@0 514 encrypted[x] = rec[x];
michael@0 515 }
michael@0 516 };
michael@0 517
michael@0 518 _("Applying record.");
michael@0 519 engine._processIncoming({
michael@0 520 get: function () {
michael@0 521 this.recordHandler(encrypted);
michael@0 522 return {success: true}
michael@0 523 },
michael@0 524 });
michael@0 525
michael@0 526 // Ensure that afterwards, toolbar is still there.
michael@0 527 // As of 2012-12-05, this only passes because Places doesn't use "toolbar" as
michael@0 528 // the real GUID, instead using a generated one. Sync does the translation.
michael@0 529 let toolbarAfter = store.createRecord("toolbar", "bookmarks");
michael@0 530 let parentGUIDAfter = toolbarAfter.parentid;
michael@0 531 let parentIDAfter = store.idForGUID(parentGUIDAfter);
michael@0 532 do_check_eq(store.GUIDForId(toolbarIDBefore), "toolbar");
michael@0 533 do_check_eq(parentGUIDBefore, parentGUIDAfter);
michael@0 534 do_check_eq(parentIDBefore, parentIDAfter);
michael@0 535
michael@0 536 server.stop(run_next_test);
michael@0 537 });
michael@0 538
michael@0 539 function run_test() {
michael@0 540 initTestLogging("Trace");
michael@0 541 generateNewKeys(Service.collectionKeys);
michael@0 542 run_next_test();
michael@0 543 }

mercurial