Bug 1317667 - Use async bookmark methods in test_bookmark_duping. r=kitcambridge, a=test-only
authorEdouard Oger <eoger@fastmail.com>
Fri, 03 Feb 2017 13:52:39 -0500
changeset 378197 c6db1230ce238d787f7ba3ea1180d4f117276bd9
parent 378196 627c2701f5fc9b8e184977e70fb711d00c856eb7
child 378198 70fa5013cfb8859a90a4f226ee7763f20397ecd0
push id1419
push userjlund@mozilla.com
push dateMon, 10 Apr 2017 20:44:07 +0000
treeherdermozilla-release@5e6801b73ef6 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerskitcambridge, test-only
bugs1317667
milestone53.0a2
Bug 1317667 - Use async bookmark methods in test_bookmark_duping. r=kitcambridge, a=test-only MozReview-Commit-ID: CpvcDXKkfcl
services/sync/tests/unit/test_bookmark_duping.js
--- a/services/sync/tests/unit/test_bookmark_duping.js
+++ b/services/sync/tests/unit/test_bookmark_duping.js
@@ -1,12 +1,13 @@
 /* Any copyright is dedicated to the Public Domain.
    http://creativecommons.org/publicdomain/zero/1.0/ */
 
 Cu.import("resource://gre/modules/PlacesUtils.jsm");
+Cu.import("resource://gre/modules/PlacesSyncUtils.jsm");
 Cu.import("resource://services-common/async.js");
 Cu.import("resource://gre/modules/Log.jsm");
 Cu.import("resource://services-sync/engines.js");
 Cu.import("resource://services-sync/engines/bookmarks.js");
 Cu.import("resource://services-sync/service.js");
 Cu.import("resource://services-sync/util.js");
 Cu.import("resource://testing-common/services/sync/utils.js");
 Cu.import("resource://services-sync/bookmark_validator.js");
@@ -52,41 +53,39 @@ async function cleanup(server) {
   Services.prefs.setBoolPref("services.sync-testing.startOverKeepIdentity", true);
   let promiseStartOver = promiseOneObserver("weave:service:start-over:finish");
   Service.startOver();
   await promiseStartOver;
   await promiseStopServer(server);
   await bms.eraseEverything();
 }
 
-function getFolderChildrenIDs(folderId) {
-  let index = 0;
-  let result = [];
-  while (true) {
-    let childId = bms.getIdForItemAt(folderId, index);
-    if (childId == -1) {
-      break;
-    }
-    result.push(childId);
-    index++;
-  }
-  return result;
+async function syncIdToId(syncId) {
+  let guid = await PlacesSyncUtils.bookmarks.syncIdToGuid(syncId);
+  return PlacesUtils.promiseItemId(guid);
+}
+
+async function getFolderChildrenIDs(folderId) {
+  let folderSyncId = PlacesSyncUtils.bookmarks.guidToSyncId(await PlacesUtils.promiseItemGuid(folderId));
+  let syncIds = await PlacesSyncUtils.bookmarks.fetchChildSyncIds(folderSyncId);
+  return Promise.all(syncIds.map(async (syncId) => await syncIdToId(syncId)));
 }
 
-function createFolder(parentId, title) {
-  let id = bms.createFolder(parentId, title, 0);
-  let guid = store.GUIDForId(id);
-  return { id, guid };
+async function createFolder(parentId, title) {
+  let parentGuid = await PlacesUtils.promiseItemGuid(parentId);
+  let folder = await bms.insert({ type: bms.TYPE_FOLDER, parentGuid, title, index: 0 });
+  let id = await PlacesUtils.promiseItemId(folder.guid);
+  return { id, guid: folder.guid };
 }
 
-function createBookmark(parentId, url, title, index = bms.DEFAULT_INDEX) {
-  let uri = Utils.makeURI(url);
-  let id = bms.insertBookmark(parentId, uri, index, title)
-  let guid = store.GUIDForId(id);
-  return { id, guid };
+async function createBookmark(parentId, url, title, index = bms.DEFAULT_INDEX) {
+  let parentGuid = await PlacesUtils.promiseItemGuid(parentId);
+  let bookmark = await bms.insert({ parentGuid, url, index, title });
+  let id = await PlacesUtils.promiseItemId(bookmark.guid);
+  return { id, guid: bookmark.guid };
 }
 
 function getServerRecord(collection, id) {
   let wbo = collection.get({ full: true, ids: [id] });
   // Whew - lots of json strings inside strings.
   return JSON.parse(JSON.parse(JSON.parse(wbo).payload).ciphertext);
 }
 
@@ -135,24 +134,24 @@ async function validate(collection, expe
 
 add_task(async function test_dupe_bookmark() {
   _("Ensure that a bookmark we consider a dupe is handled correctly.");
 
   let { server, collection } = await this.setup();
 
   try {
     // The parent folder and one bookmark in it.
-    let {id: folder1_id, guid: folder1_guid } = createFolder(bms.toolbarFolder, "Folder 1");
-    let {guid: bmk1_guid} = createBookmark(folder1_id, "http://getfirefox.com/", "Get Firefox!");
+    let {id: folder1_id, guid: folder1_guid } = await createFolder(bms.toolbarFolder, "Folder 1");
+    let {guid: bmk1_guid} = await createBookmark(folder1_id, "http://getfirefox.com/", "Get Firefox!");
 
     engine.sync();
 
     // We've added the bookmark, its parent (folder1) plus "menu", "toolbar", "unfiled", and "mobile".
     equal(collection.count(), 6);
-    equal(getFolderChildrenIDs(folder1_id).length, 1);
+    equal((await getFolderChildrenIDs(folder1_id)).length, 1);
 
     // Now create a new incoming record that looks alot like a dupe.
     let newGUID = Utils.makeGUID();
     let to_apply = {
       id: newGUID,
       bmkUri: "http://getfirefox.com/",
       type: "bookmark",
       title: "Get Firefox!",
@@ -166,17 +165,17 @@ add_task(async function test_dupe_bookma
     engine.sync();
 
     // We should have logically deleted the dupe record.
     equal(collection.count(), 7);
     ok(getServerRecord(collection, bmk1_guid).deleted);
     // and physically removed from the local store.
     await promiseNoLocalItem(bmk1_guid);
     // Parent should still only have 1 item.
-    equal(getFolderChildrenIDs(folder1_id).length, 1);
+    equal((await getFolderChildrenIDs(folder1_id)).length, 1);
     // The parent record on the server should now reference the new GUID and not the old.
     let serverRecord = getServerRecord(collection, folder1_guid);
     ok(!serverRecord.children.includes(bmk1_guid));
     ok(serverRecord.children.includes(newGUID));
 
     // and a final sanity check - use the validator
     await validate(collection);
   } finally {
@@ -186,29 +185,29 @@ add_task(async function test_dupe_bookma
 
 add_task(async function test_dupe_reparented_bookmark() {
   _("Ensure that a bookmark we consider a dupe from a different parent is handled correctly");
 
   let { server, collection } = await this.setup();
 
   try {
     // The parent folder and one bookmark in it.
-    let {id: folder1_id, guid: folder1_guid } = createFolder(bms.toolbarFolder, "Folder 1");
-    let {guid: bmk1_guid} = createBookmark(folder1_id, "http://getfirefox.com/", "Get Firefox!");
+    let {id: folder1_id, guid: folder1_guid } = await createFolder(bms.toolbarFolder, "Folder 1");
+    let {guid: bmk1_guid} = await createBookmark(folder1_id, "http://getfirefox.com/", "Get Firefox!");
     // Another parent folder *with the same name*
-    let {id: folder2_id, guid: folder2_guid } = createFolder(bms.toolbarFolder, "Folder 1");
+    let {id: folder2_id, guid: folder2_guid } = await createFolder(bms.toolbarFolder, "Folder 1");
 
     do_print(`folder1_guid=${folder1_guid}, folder2_guid=${folder2_guid}, bmk1_guid=${bmk1_guid}`);
 
     engine.sync();
 
     // We've added the bookmark, 2 folders plus "menu", "toolbar", "unfiled", and "mobile".
     equal(collection.count(), 7);
-    equal(getFolderChildrenIDs(folder1_id).length, 1);
-    equal(getFolderChildrenIDs(folder2_id).length, 0);
+    equal((await getFolderChildrenIDs(folder1_id)).length, 1);
+    equal((await getFolderChildrenIDs(folder2_id)).length, 0);
 
     // Now create a new incoming record that looks alot like a dupe of the
     // item in folder1_guid, but with a record that points to folder2_guid.
     let newGUID = Utils.makeGUID();
     let to_apply = {
       id: newGUID,
       bmkUri: "http://getfirefox.com/",
       type: "bookmark",
@@ -224,19 +223,19 @@ add_task(async function test_dupe_repare
     engine.sync();
 
     // We should have logically deleted the dupe record.
     equal(collection.count(), 8);
     ok(getServerRecord(collection, bmk1_guid).deleted);
     // and physically removed from the local store.
     await promiseNoLocalItem(bmk1_guid);
     // The original folder no longer has the item
-    equal(getFolderChildrenIDs(folder1_id).length, 0);
+    equal((await getFolderChildrenIDs(folder1_id)).length, 0);
     // But the second dupe folder does.
-    equal(getFolderChildrenIDs(folder2_id).length, 1);
+    equal((await getFolderChildrenIDs(folder2_id)).length, 1);
 
     // The record for folder1 on the server should reference neither old or new GUIDs.
     let serverRecord1 = getServerRecord(collection, folder1_guid);
     ok(!serverRecord1.children.includes(bmk1_guid));
     ok(!serverRecord1.children.includes(newGUID));
 
     // The record for folder2 on the server should only reference the new new GUID.
     let serverRecord2 = getServerRecord(collection, folder2_guid);
@@ -252,29 +251,29 @@ add_task(async function test_dupe_repare
 
 add_task(async function test_dupe_reparented_locally_changed_bookmark() {
   _("Ensure that a bookmark with local changes we consider a dupe from a different parent is handled correctly");
 
   let { server, collection } = await this.setup();
 
   try {
     // The parent folder and one bookmark in it.
-    let {id: folder1_id, guid: folder1_guid } = createFolder(bms.toolbarFolder, "Folder 1");
-    let {guid: bmk1_guid} = createBookmark(folder1_id, "http://getfirefox.com/", "Get Firefox!");
+    let {id: folder1_id, guid: folder1_guid } = await createFolder(bms.toolbarFolder, "Folder 1");
+    let {guid: bmk1_guid} = await createBookmark(folder1_id, "http://getfirefox.com/", "Get Firefox!");
     // Another parent folder *with the same name*
-    let {id: folder2_id, guid: folder2_guid } = createFolder(bms.toolbarFolder, "Folder 1");
+    let {id: folder2_id, guid: folder2_guid } = await createFolder(bms.toolbarFolder, "Folder 1");
 
     do_print(`folder1_guid=${folder1_guid}, folder2_guid=${folder2_guid}, bmk1_guid=${bmk1_guid}`);
 
     engine.sync();
 
     // We've added the bookmark, 2 folders plus "menu", "toolbar", "unfiled", and "mobile".
     equal(collection.count(), 7);
-    equal(getFolderChildrenIDs(folder1_id).length, 1);
-    equal(getFolderChildrenIDs(folder2_id).length, 0);
+    equal((await getFolderChildrenIDs(folder1_id)).length, 1);
+    equal((await getFolderChildrenIDs(folder2_id)).length, 0);
 
     // Now create a new incoming record that looks alot like a dupe of the
     // item in folder1_guid, but with a record that points to folder2_guid.
     let newGUID = Utils.makeGUID();
     let to_apply = {
       id: newGUID,
       bmkUri: "http://getfirefox.com/",
       type: "bookmark",
@@ -300,19 +299,19 @@ add_task(async function test_dupe_repare
     engine.sync();
 
     // We should have logically deleted the dupe record.
     equal(collection.count(), 8);
     ok(getServerRecord(collection, bmk1_guid).deleted);
     // and physically removed from the local store.
     await promiseNoLocalItem(bmk1_guid);
     // The original folder still longer has the item
-    equal(getFolderChildrenIDs(folder1_id).length, 1);
+    equal((await getFolderChildrenIDs(folder1_id)).length, 1);
     // The second folder does not.
-    equal(getFolderChildrenIDs(folder2_id).length, 0);
+    equal((await getFolderChildrenIDs(folder2_id)).length, 0);
 
     // The record for folder1 on the server should reference only the GUID.
     let serverRecord1 = getServerRecord(collection, folder1_guid);
     ok(!serverRecord1.children.includes(bmk1_guid));
     ok(serverRecord1.children.includes(newGUID));
 
     // The record for folder2 on the server should reference nothing.
     let serverRecord2 = getServerRecord(collection, folder2_guid);
@@ -329,28 +328,28 @@ add_task(async function test_dupe_repare
 add_task(async function test_dupe_reparented_to_earlier_appearing_parent_bookmark() {
   _("Ensure that a bookmark we consider a dupe from a different parent that " +
     "appears in the same sync before the dupe item");
 
   let { server, collection } = await this.setup();
 
   try {
     // The parent folder and one bookmark in it.
-    let {id: folder1_id, guid: folder1_guid } = createFolder(bms.toolbarFolder, "Folder 1");
-    let {guid: bmk1_guid} = createBookmark(folder1_id, "http://getfirefox.com/", "Get Firefox!");
+    let {id: folder1_id, guid: folder1_guid } = await createFolder(bms.toolbarFolder, "Folder 1");
+    let {guid: bmk1_guid} = await createBookmark(folder1_id, "http://getfirefox.com/", "Get Firefox!");
     // One more folder we'll use later.
-    let {guid: folder2_guid} = createFolder(bms.toolbarFolder, "A second folder");
+    let {guid: folder2_guid} = await createFolder(bms.toolbarFolder, "A second folder");
 
     do_print(`folder1=${folder1_guid}, bmk1=${bmk1_guid} folder2=${folder2_guid}`);
 
     engine.sync();
 
     // We've added the bookmark, 2 folders plus "menu", "toolbar", "unfiled", and "mobile".
     equal(collection.count(), 7);
-    equal(getFolderChildrenIDs(folder1_id).length, 1);
+    equal((await getFolderChildrenIDs(folder1_id)).length, 1);
 
     let newGUID = Utils.makeGUID();
     let newParentGUID = Utils.makeGUID();
 
     // Have the new parent appear before the dupe item.
     collection.insert(newParentGUID, encryptPayload({
       id: newParentGUID,
       type: "folder",
@@ -386,48 +385,48 @@ add_task(async function test_dupe_repare
     }), Date.now() / 1000 + 10);
 
 
     _("Syncing so new records are processed.");
     engine.lastSync = engine.lastSync - 0.01;
     engine.sync();
 
     // Everything should be parented correctly.
-    equal(getFolderChildrenIDs(folder1_id).length, 0);
+    equal((await getFolderChildrenIDs(folder1_id)).length, 0);
     let newParentID = store.idForGUID(newParentGUID);
     let newID = store.idForGUID(newGUID);
-    deepEqual(getFolderChildrenIDs(newParentID), [newID]);
+    deepEqual(await getFolderChildrenIDs(newParentID), [newID]);
 
     // Make sure the validator thinks everything is hunky-dory.
     await validate(collection);
   } finally {
     await cleanup(server);
   }
 });
 
 add_task(async function test_dupe_reparented_to_later_appearing_parent_bookmark() {
   _("Ensure that a bookmark we consider a dupe from a different parent that " +
     "doesn't exist locally as we process the child, but does appear in the same sync");
 
   let { server, collection } = await this.setup();
 
   try {
     // The parent folder and one bookmark in it.
-    let {id: folder1_id, guid: folder1_guid } = createFolder(bms.toolbarFolder, "Folder 1");
-    let {guid: bmk1_guid} = createBookmark(folder1_id, "http://getfirefox.com/", "Get Firefox!");
+    let {id: folder1_id, guid: folder1_guid } = await createFolder(bms.toolbarFolder, "Folder 1");
+    let {guid: bmk1_guid} = await createBookmark(folder1_id, "http://getfirefox.com/", "Get Firefox!");
     // One more folder we'll use later.
-    let {guid: folder2_guid} = createFolder(bms.toolbarFolder, "A second folder");
+    let {guid: folder2_guid} = await createFolder(bms.toolbarFolder, "A second folder");
 
     do_print(`folder1=${folder1_guid}, bmk1=${bmk1_guid} folder2=${folder2_guid}`);
 
     engine.sync();
 
     // We've added the bookmark, 2 folders plus "menu", "toolbar", "unfiled", and "mobile".
     equal(collection.count(), 7);
-    equal(getFolderChildrenIDs(folder1_id).length, 1);
+    equal((await getFolderChildrenIDs(folder1_id)).length, 1);
 
     // Now create a new incoming record that looks alot like a dupe of the
     // item in folder1_guid, but with a record that points to a parent with the
     // same name, but a non-existing local ID.
     let newGUID = Utils.makeGUID();
     let newParentGUID = Utils.makeGUID();
 
     collection.insert(newGUID, encryptPayload({
@@ -463,48 +462,48 @@ add_task(async function test_dupe_repare
     }), Date.now() / 1000 + 10);
 
     _("Syncing so out-of-order records are processed.");
     engine.lastSync = engine.lastSync - 0.01;
     engine.sync();
 
     // The intended parent did end up existing, so it should be parented
     // correctly after de-duplication.
-    equal(getFolderChildrenIDs(folder1_id).length, 0);
+    equal((await getFolderChildrenIDs(folder1_id)).length, 0);
     let newParentID = store.idForGUID(newParentGUID);
     let newID = store.idForGUID(newGUID);
-    deepEqual(getFolderChildrenIDs(newParentID), [newID]);
+    deepEqual(await getFolderChildrenIDs(newParentID), [newID]);
 
     // Make sure the validator thinks everything is hunky-dory.
     await validate(collection);
   } finally {
     await cleanup(server);
   }
 });
 
 add_task(async function test_dupe_reparented_to_future_arriving_parent_bookmark() {
   _("Ensure that a bookmark we consider a dupe from a different parent that " +
     "doesn't exist locally and doesn't appear in this Sync is handled correctly");
 
   let { server, collection } = await this.setup();
 
   try {
     // The parent folder and one bookmark in it.
-    let {id: folder1_id, guid: folder1_guid } = createFolder(bms.toolbarFolder, "Folder 1");
-    let {guid: bmk1_guid} = createBookmark(folder1_id, "http://getfirefox.com/", "Get Firefox!");
+    let {id: folder1_id, guid: folder1_guid } = await createFolder(bms.toolbarFolder, "Folder 1");
+    let {guid: bmk1_guid} = await createBookmark(folder1_id, "http://getfirefox.com/", "Get Firefox!");
     // One more folder we'll use later.
-    let {guid: folder2_guid} = createFolder(bms.toolbarFolder, "A second folder");
+    let {guid: folder2_guid} = await createFolder(bms.toolbarFolder, "A second folder");
 
     do_print(`folder1=${folder1_guid}, bmk1=${bmk1_guid} folder2=${folder2_guid}`);
 
     engine.sync();
 
     // We've added the bookmark, 2 folders plus "menu", "toolbar", "unfiled", and "mobile".
     equal(collection.count(), 7);
-    equal(getFolderChildrenIDs(folder1_id).length, 1);
+    equal((await getFolderChildrenIDs(folder1_id)).length, 1);
 
     // Now create a new incoming record that looks alot like a dupe of the
     // item in folder1_guid, but with a record that points to a parent with the
     // same name, but a non-existing local ID.
     let newGUID = Utils.makeGUID();
     let newParentGUID = Utils.makeGUID();
 
     collection.insert(newGUID, encryptPayload({
@@ -522,17 +521,17 @@ add_task(async function test_dupe_repare
     engine.sync();
 
     // We should have logically deleted the dupe record.
     equal(collection.count(), 8);
     ok(getServerRecord(collection, bmk1_guid).deleted);
     // and physically removed from the local store.
     await promiseNoLocalItem(bmk1_guid);
     // The intended parent doesn't exist, so it remains in the original folder
-    equal(getFolderChildrenIDs(folder1_id).length, 1);
+    equal((await getFolderChildrenIDs(folder1_id)).length, 1);
 
     // The record for folder1 on the server should reference the new GUID.
     let serverRecord1 = getServerRecord(collection, folder1_guid);
     ok(!serverRecord1.children.includes(bmk1_guid));
     ok(serverRecord1.children.includes(newGUID));
 
     // As the incoming parent is missing the item should have been annotated
     // with that missing parent.
@@ -570,20 +569,20 @@ add_task(async function test_dupe_repare
       tags: [],
     }), Date.now() / 1000 + 10);
 
     _("Syncing so missing parent appears");
     engine.lastSync = engine.lastSync - 0.01;
     engine.sync();
 
     // The intended parent now does exist, so it should have been reparented.
-    equal(getFolderChildrenIDs(folder1_id).length, 0);
+    equal((await getFolderChildrenIDs(folder1_id)).length, 0);
     let newParentID = store.idForGUID(newParentGUID);
     let newID = store.idForGUID(newGUID);
-    deepEqual(getFolderChildrenIDs(newParentID), [newID]);
+    deepEqual(await getFolderChildrenIDs(newParentID), [newID]);
 
     // validation now has different errors :(
     expected = [
       // The validator reports multipleParents because:
       // * The incoming record newParentGUID still (and correctly) references
       //   newGUID as a child.
       // * Our original Folder1 was updated to include newGUID when it
       //   originally de-deuped and couldn't find the parent.
@@ -603,17 +602,17 @@ add_task(async function test_dupe_repare
 add_task(async function test_dupe_empty_folder() {
   _("Ensure that an empty folder we consider a dupe is handled correctly.");
   // Empty folders aren't particularly interesting in practice (as that seems
   // an edge-case) but duping folders with items is broken - bug 1293163.
   let { server, collection } = await this.setup();
 
   try {
     // The folder we will end up duping away.
-    let {guid: folder1_guid } = createFolder(bms.toolbarFolder, "Folder 1");
+    let {guid: folder1_guid } = await createFolder(bms.toolbarFolder, "Folder 1");
 
     engine.sync();
 
     // We've added 1 folder, "menu", "toolbar", "unfiled", and "mobile".
     equal(collection.count(), 5);
 
     // Now create new incoming records that looks alot like a dupe of "Folder 1".
     let newFolderGUID = Utils.makeGUID();