1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447
|
/**
* Tracks a set of bookmark guids and their syncChangeCounter field and
* provides a simple way for the test to check the correct fields had the
* counter incremented.
*/
class CounterTracker {
constructor() {
this.tracked = new Map();
}
async _getCounter(guid) {
let fields = await PlacesTestUtils.fetchBookmarkSyncFields(guid);
if (!fields.length) {
throw new Error(`Item ${guid} does not exist`);
}
return fields[0].syncChangeCounter;
}
// Call this after creating a new bookmark.
async track(guid, name, expectedInitial = 1) {
if (this.tracked.has(guid)) {
throw new Error(`Already tracking item ${guid}`);
}
let initial = await this._getCounter(guid);
Assert.equal(
initial,
expectedInitial,
`Initial value of item '${name}' is correct`
);
this.tracked.set(guid, { name, value: expectedInitial });
}
// Call this to check *only* the specified IDs had a change increment, and
// that none of the other "tracked" ones did.
async check(...expectedToIncrement) {
info(`Checking counter for items ${JSON.stringify(expectedToIncrement)}`);
for (let [guid, entry] of this.tracked) {
let { name, value } = entry;
let newValue = await this._getCounter(guid);
let desc = `record '${name}' (guid=${guid})`;
if (expectedToIncrement.includes(guid)) {
// Note we don't check specifically for +1, as some changes will
// increment the counter by more than 1 (which is OK).
Assert.greater(
newValue,
value,
`${desc} was expected to increment - was ${value}, now ${newValue}`
);
this.tracked.set(guid, { name, value: newValue });
} else {
Assert.equal(newValue, value, `${desc} was NOT expected to increment`);
}
}
}
}
async function checkSyncFields(guid, expected) {
let results = await PlacesTestUtils.fetchBookmarkSyncFields(guid);
if (!results.length) {
throw new Error(`Missing sync fields for ${guid}`);
}
for (let name in expected) {
let expectedValue = expected[name];
Assert.equal(
results[0][name],
expectedValue,
`field ${name} matches item ${guid}`
);
}
}
/**
* Common test cases for sync field changes.
*/
class TestCases {
async run() {
info("Test 1: inserts, updates, tags, and keywords");
try {
await this.testChanges();
} finally {
info("Reset sync fields after test 1");
await PlacesTestUtils.markBookmarksAsSynced();
}
if ("moveItem" in this && "reorder" in this) {
info("Test 2: reparenting");
try {
await this.testReparenting();
} finally {
info("Reset sync fields after test 2");
await PlacesTestUtils.markBookmarksAsSynced();
}
}
if ("insertSeparator" in this) {
info("Test 3: separators");
try {
await this.testSeparators();
} finally {
info("Reset sync fields after test 3");
await PlacesTestUtils.markBookmarksAsSynced();
}
}
}
async testChanges() {
let testUri = NetUtil.newURI("http://test.mozilla.org");
let guid = await this.insertBookmark(
PlacesUtils.bookmarks.unfiledGuid,
testUri,
PlacesUtils.bookmarks.DEFAULT_INDEX,
"bookmark title"
);
info(`Inserted bookmark ${guid}`);
await checkSyncFields(guid, {
syncStatus: PlacesUtils.bookmarks.SYNC_STATUS.NEW,
syncChangeCounter: 1,
});
// Pretend Sync just did whatever it does
await PlacesTestUtils.setBookmarkSyncFields({
guid,
syncStatus: PlacesUtils.bookmarks.SYNC_STATUS.NORMAL,
});
info(`Updated sync status of ${guid}`);
await checkSyncFields(guid, {
syncStatus: PlacesUtils.bookmarks.SYNC_STATUS.NORMAL,
syncChangeCounter: 1,
});
// update it - it should increment the change counter
await this.setTitle(guid, "new title");
info(`Changed title of ${guid}`);
await checkSyncFields(guid, {
syncStatus: PlacesUtils.bookmarks.SYNC_STATUS.NORMAL,
syncChangeCounter: 2,
});
// Tagging a bookmark should update its change counter.
await this.tagURI(testUri, ["test-tag"]);
info(`Tagged bookmark ${guid}`);
await checkSyncFields(guid, { syncChangeCounter: 3 });
if ("setKeyword" in this) {
await this.setKeyword(guid, "keyword");
info(`Set keyword for bookmark ${guid}`);
await checkSyncFields(guid, { syncChangeCounter: 4 });
}
if ("removeKeyword" in this) {
await this.removeKeyword(guid, "keyword");
info(`Removed keyword from bookmark ${guid}`);
await checkSyncFields(guid, { syncChangeCounter: 5 });
}
}
async testSeparators() {
let insertSyncedBookmark = uri => {
return this.insertBookmark(
PlacesUtils.bookmarks.unfiledGuid,
NetUtil.newURI(uri),
PlacesUtils.bookmarks.DEFAULT_INDEX,
"A bookmark name"
);
};
await insertSyncedBookmark("http://foo.bar");
let secondBmk = await insertSyncedBookmark("http://bar.foo");
let sepGuid = await this.insertSeparator(
PlacesUtils.bookmarks.unfiledGuid,
PlacesUtils.bookmarks.DEFAULT_INDEX
);
await insertSyncedBookmark("http://barbar.foo");
info("Move a bookmark around the separator");
await this.moveItem(secondBmk, PlacesUtils.bookmarks.unfiledGuid, 4);
await checkSyncFields(sepGuid, { syncChangeCounter: 2 });
info("Move a separator around directly");
await this.moveItem(sepGuid, PlacesUtils.bookmarks.unfiledGuid, 0);
await checkSyncFields(sepGuid, { syncChangeCounter: 3 });
}
async testReparenting() {
let counterTracker = new CounterTracker();
let folder1 = await this.createFolder(
PlacesUtils.bookmarks.unfiledGuid,
"folder1",
PlacesUtils.bookmarks.DEFAULT_INDEX
);
info(`Created the first folder, guid is ${folder1}`);
// New folder should have a change recorded.
await counterTracker.track(folder1, "folder 1");
// Put a new bookmark in the folder.
let testUri = NetUtil.newURI("http://test2.mozilla.org");
let child1 = await this.insertBookmark(
folder1,
testUri,
PlacesUtils.bookmarks.DEFAULT_INDEX,
"bookmark 1"
);
info(`Created a new bookmark into ${folder1}, guid is ${child1}`);
// both the folder and the child should have a change recorded.
await counterTracker.track(child1, "child 1");
await counterTracker.check(folder1);
// A new child in the folder at index 0 - even though the existing child
// was bumped down the list, it should *not* have a change recorded.
let child2 = await this.insertBookmark(folder1, testUri, 0, "bookmark 2");
info(
`Created a second new bookmark into folder ${folder1}, guid is ${child2}`
);
await counterTracker.track(child2, "child 2");
await counterTracker.check(folder1);
// Move the items within the same folder - this should result in just a
// change for the parent, but for neither of the children.
// child0 is currently at index 0, so move child1 there.
await this.moveItem(child1, folder1, 0);
await counterTracker.check(folder1);
// Another folder to play with.
let folder2 = await this.createFolder(
PlacesUtils.bookmarks.unfiledGuid,
"folder2",
PlacesUtils.bookmarks.DEFAULT_INDEX
);
info(`Created a second new folder, guid is ${folder2}`);
await counterTracker.track(folder2, "folder 2");
// nothing else has changed.
await counterTracker.check();
// Move one of the children to the new folder.
info(
`Moving bookmark ${child2} from folder ${folder1} to folder ${folder2}`
);
await this.moveItem(child2, folder2, PlacesUtils.bookmarks.DEFAULT_INDEX);
// child1 should have no change, everything should have a new change.
await counterTracker.check(folder1, folder2, child2);
// Move the new folder to another root.
await this.moveItem(
folder2,
PlacesUtils.bookmarks.toolbarGuid,
PlacesUtils.bookmarks.DEFAULT_INDEX
);
info(`Moving folder ${folder2} to toolbar`);
await counterTracker.check(
folder2,
PlacesUtils.bookmarks.toolbarGuid,
PlacesUtils.bookmarks.unfiledGuid
);
let child3 = await this.insertBookmark(folder2, testUri, 0, "bookmark 3");
info(`Prepended child ${child3} to folder ${folder2}`);
await counterTracker.check(folder2, child3);
// Reordering should only track the parent.
await this.reorder(folder2, [child2, child3]);
info(`Reorder children of ${folder2}`);
await counterTracker.check(folder2);
// All fields still have a syncStatus of SYNC_STATUS_NEW - so deleting them
// should *not* cause any deleted items to be written.
await this.removeItem(folder1);
Assert.equal((await PlacesTestUtils.fetchSyncTombstones()).length, 0);
// Set folder2 and child2 to have a state of SYNC_STATUS_NORMAL and deleting
// them will cause both GUIDs to be written to moz_bookmarks_deleted.
await PlacesTestUtils.setBookmarkSyncFields({
guid: folder2,
syncStatus: PlacesUtils.bookmarks.SYNC_STATUS.NORMAL,
});
await PlacesTestUtils.setBookmarkSyncFields({
guid: child2,
syncStatus: PlacesUtils.bookmarks.SYNC_STATUS.NORMAL,
});
await this.removeItem(folder2);
let tombstones = await PlacesTestUtils.fetchSyncTombstones();
let tombstoneGuids = sortBy(tombstones, "guid").map(({ guid }) => guid);
Assert.equal(tombstoneGuids.length, 2);
Assert.deepEqual(tombstoneGuids, [folder2, child2].sort(compareAscending));
}
}
/**
* Exercises the legacy, synchronous `nsINavBookmarksService` calls implemented
* in C++.
*/
class SyncTestCases extends TestCases {
async createFolder(parentGuid, title, index) {
let parentId = await PlacesTestUtils.promiseItemId(parentGuid);
let id = PlacesUtils.bookmarks.createFolder(parentId, title, index);
return PlacesTestUtils.promiseItemGuid(id);
}
async insertBookmark(parentGuid, uri, index, title) {
let parentId = await PlacesTestUtils.promiseItemId(parentGuid);
let id = PlacesUtils.bookmarks.insertBookmark(parentId, uri, index, title);
return PlacesTestUtils.promiseItemGuid(id);
}
async removeItem(guid) {
let id = await PlacesTestUtils.promiseItemId(guid);
PlacesUtils.bookmarks.removeItem(id);
}
async setTitle(guid, title) {
let id = await PlacesTestUtils.promiseItemId(guid);
PlacesUtils.bookmarks.setItemTitle(id, title);
}
async tagURI(uri, tags) {
PlacesUtils.tagging.tagURI(uri, tags);
}
}
async function findTagFolder(tag) {
let db = await PlacesUtils.promiseDBConnection();
let results = await db.executeCached(
`
SELECT guid
FROM moz_bookmarks
WHERE type = :type AND
parent = :tagsFolderId AND
title = :tag`,
{
type: PlacesUtils.bookmarks.TYPE_FOLDER,
tagsFolderId: PlacesUtils.tagsFolderId,
tag,
}
);
return results.length ? results[0].getResultByName("guid") : null;
}
/**
* Exercises the new, async calls implemented in `Bookmarks.sys.mjs`.
*/
class AsyncTestCases extends TestCases {
async createFolder(parentGuid, title, index) {
let item = await PlacesUtils.bookmarks.insert({
type: PlacesUtils.bookmarks.TYPE_FOLDER,
parentGuid,
title,
index,
});
return item.guid;
}
async insertBookmark(parentGuid, uri, index, title) {
let item = await PlacesUtils.bookmarks.insert({
parentGuid,
url: uri,
index,
title,
});
return item.guid;
}
async insertSeparator(parentGuid, index) {
let item = await PlacesUtils.bookmarks.insert({
type: PlacesUtils.bookmarks.TYPE_SEPARATOR,
parentGuid,
index,
});
return item.guid;
}
async moveItem(guid, newParentGuid, index) {
await PlacesUtils.bookmarks.update({
guid,
parentGuid: newParentGuid,
index,
});
}
async removeItem(guid) {
await PlacesUtils.bookmarks.remove(guid);
}
async setTitle(guid, title) {
await PlacesUtils.bookmarks.update({ guid, title });
}
async setKeyword(guid, keyword) {
let item = await PlacesUtils.bookmarks.fetch(guid);
if (!item) {
throw new Error(
`Cannot set keyword ${keyword} on nonexistent bookmark ${guid}`
);
}
await PlacesUtils.keywords.insert({ keyword, url: item.url });
}
async removeKeyword(guid, keyword) {
let item = await PlacesUtils.bookmarks.fetch(guid);
if (!item) {
throw new Error(
`Cannot remove keyword ${keyword} from nonexistent bookmark ${guid}`
);
}
let entry = await PlacesUtils.keywords.fetch({ keyword, url: item.url });
if (!entry) {
throw new Error(`Keyword ${keyword} not set on bookmark ${guid}`);
}
await PlacesUtils.keywords.remove(entry);
}
// There's no async API for tags, but the `PlacesUtils.bookmarks` methods are
// tag-aware, and should bump the change counters for tagged bookmarks when
// called directly.
async tagURI(uri, tags) {
for (let tag of tags) {
let tagFolderGuid = await findTagFolder(tag);
if (!tagFolderGuid) {
let tagFolder = await PlacesUtils.bookmarks.insert({
type: PlacesUtils.bookmarks.TYPE_FOLDER,
parentGuid: PlacesUtils.bookmarks.tagsGuid,
title: tag,
});
tagFolderGuid = tagFolder.guid;
}
await PlacesUtils.bookmarks.insert({
url: uri,
parentGuid: tagFolderGuid,
});
}
}
async reorder(parentGuid, childGuids) {
await PlacesUtils.bookmarks.reorder(parentGuid, childGuids);
}
}
add_task(async function test_sync_api() {
let tests = new SyncTestCases();
await tests.run();
});
add_task(async function test_async_api() {
let tests = new AsyncTestCases();
await tests.run();
});
|