summaryrefslogtreecommitdiffstats
path: root/services/sync/tests/unit/test_bookmark_store.js
blob: 902206ba6c928274fd9a37dba13382d57972c761 (plain)
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
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
/* 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://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");

const PARENT_ANNO = "sync/parent";

Service.engineManager.register(BookmarksEngine);

var engine = Service.engineManager.get("bookmarks");
var store = engine._store;
var tracker = engine._tracker;

// Don't write some persistence files asynchronously.
tracker.persistChangedIDs = false;

var fxuri = Utils.makeURI("http://getfirefox.com/");
var tburi = Utils.makeURI("http://getthunderbird.com/");

add_task(function* test_ignore_specials() {
  _("Ensure that we can't delete bookmark roots.");

  // Belt...
  let record = new BookmarkFolder("bookmarks", "toolbar", "folder");
  record.deleted = true;
  do_check_neq(null, store.idForGUID("toolbar"));

  store.applyIncoming(record);
  yield store.deletePending();

  // Ensure that the toolbar exists.
  do_check_neq(null, store.idForGUID("toolbar"));

  // This will fail painfully in getItemType if the deletion worked.
  engine._buildGUIDMap();

  // Braces...
  store.remove(record);
  yield store.deletePending();
  do_check_neq(null, store.idForGUID("toolbar"));
  engine._buildGUIDMap();

  store.wipe();
});

add_test(function test_bookmark_create() {
  try {
    _("Ensure the record isn't present yet.");
    let ids = PlacesUtils.bookmarks.getBookmarkIdsForURI(fxuri, {});
    do_check_eq(ids.length, 0);

    _("Let's create a new record.");
    let fxrecord = new Bookmark("bookmarks", "get-firefox1");
    fxrecord.bmkUri        = fxuri.spec;
    fxrecord.description   = "Firefox is awesome.";
    fxrecord.title         = "Get Firefox!";
    fxrecord.tags          = ["firefox", "awesome", "browser"];
    fxrecord.keyword       = "awesome";
    fxrecord.loadInSidebar = false;
    fxrecord.parentName    = "Bookmarks Toolbar";
    fxrecord.parentid      = "toolbar";
    store.applyIncoming(fxrecord);

    _("Verify it has been created correctly.");
    let id = store.idForGUID(fxrecord.id);
    do_check_eq(store.GUIDForId(id), fxrecord.id);
    do_check_eq(PlacesUtils.bookmarks.getItemType(id),
                PlacesUtils.bookmarks.TYPE_BOOKMARK);
    do_check_true(PlacesUtils.bookmarks.getBookmarkURI(id).equals(fxuri));
    do_check_eq(PlacesUtils.bookmarks.getItemTitle(id), fxrecord.title);
    do_check_eq(PlacesUtils.annotations.getItemAnnotation(id, "bookmarkProperties/description"),
                fxrecord.description);
    do_check_eq(PlacesUtils.bookmarks.getFolderIdForItem(id),
                PlacesUtils.bookmarks.toolbarFolder);
    do_check_eq(PlacesUtils.bookmarks.getKeywordForBookmark(id), fxrecord.keyword);

    _("Have the store create a new record object. Verify that it has the same data.");
    let newrecord = store.createRecord(fxrecord.id);
    do_check_true(newrecord instanceof Bookmark);
    for (let property of ["type", "bmkUri", "description", "title",
                          "keyword", "parentName", "parentid"]) {
      do_check_eq(newrecord[property], fxrecord[property]);
    }
    do_check_true(Utils.deepEquals(newrecord.tags.sort(),
                                   fxrecord.tags.sort()));

    _("The calculated sort index is based on frecency data.");
    do_check_true(newrecord.sortindex >= 150);

    _("Create a record with some values missing.");
    let tbrecord = new Bookmark("bookmarks", "thunderbird1");
    tbrecord.bmkUri        = tburi.spec;
    tbrecord.parentName    = "Bookmarks Toolbar";
    tbrecord.parentid      = "toolbar";
    store.applyIncoming(tbrecord);

    _("Verify it has been created correctly.");
    id = store.idForGUID(tbrecord.id);
    do_check_eq(store.GUIDForId(id), tbrecord.id);
    do_check_eq(PlacesUtils.bookmarks.getItemType(id),
                PlacesUtils.bookmarks.TYPE_BOOKMARK);
    do_check_true(PlacesUtils.bookmarks.getBookmarkURI(id).equals(tburi));
    do_check_eq(PlacesUtils.bookmarks.getItemTitle(id), null);
    let error;
    try {
      PlacesUtils.annotations.getItemAnnotation(id, "bookmarkProperties/description");
    } catch(ex) {
      error = ex;
    }
    do_check_eq(error.result, Cr.NS_ERROR_NOT_AVAILABLE);
    do_check_eq(PlacesUtils.bookmarks.getFolderIdForItem(id),
                PlacesUtils.bookmarks.toolbarFolder);
    do_check_eq(PlacesUtils.bookmarks.getKeywordForBookmark(id), null);
  } finally {
    _("Clean up.");
    store.wipe();
    run_next_test();
  }
});

add_test(function test_bookmark_update() {
  try {
    _("Create a bookmark whose values we'll change.");
    let bmk1_id = PlacesUtils.bookmarks.insertBookmark(
      PlacesUtils.bookmarks.toolbarFolder, fxuri,
      PlacesUtils.bookmarks.DEFAULT_INDEX,
      "Get Firefox!");
    PlacesUtils.annotations.setItemAnnotation(
      bmk1_id, "bookmarkProperties/description", "Firefox is awesome.", 0,
      PlacesUtils.annotations.EXPIRE_NEVER);
    PlacesUtils.bookmarks.setKeywordForBookmark(bmk1_id, "firefox");
    let bmk1_guid = store.GUIDForId(bmk1_id);

    _("Update the record with some null values.");
    let record = store.createRecord(bmk1_guid);
    record.title = null;
    record.description = null;
    record.keyword = null;
    record.tags = null;
    store.applyIncoming(record);

    _("Verify that the values have been cleared.");
    do_check_throws(function () {
      PlacesUtils.annotations.getItemAnnotation(
        bmk1_id, "bookmarkProperties/description");
    }, Cr.NS_ERROR_NOT_AVAILABLE);
    do_check_eq(PlacesUtils.bookmarks.getItemTitle(bmk1_id), null);
    do_check_eq(PlacesUtils.bookmarks.getKeywordForBookmark(bmk1_id), null);
  } finally {
    _("Clean up.");
    store.wipe();
    run_next_test();
  }
});

add_test(function test_bookmark_createRecord() {
  try {
    _("Create a bookmark without a description or title.");
    let bmk1_id = PlacesUtils.bookmarks.insertBookmark(
      PlacesUtils.bookmarks.toolbarFolder, fxuri,
      PlacesUtils.bookmarks.DEFAULT_INDEX, null);
    let bmk1_guid = store.GUIDForId(bmk1_id);

    _("Verify that the record is created accordingly.");
    let record = store.createRecord(bmk1_guid);
    do_check_eq(record.title, "");
    do_check_eq(record.description, null);
    do_check_eq(record.keyword, null);

  } finally {
    _("Clean up.");
    store.wipe();
    run_next_test();
  }
});

add_test(function test_folder_create() {
  try {
    _("Create a folder.");
    let folder = new BookmarkFolder("bookmarks", "testfolder-1");
    folder.parentName = "Bookmarks Toolbar";
    folder.parentid   = "toolbar";
    folder.title      = "Test Folder";
    store.applyIncoming(folder);

    _("Verify it has been created correctly.");
    let id = store.idForGUID(folder.id);
    do_check_eq(PlacesUtils.bookmarks.getItemType(id),
                PlacesUtils.bookmarks.TYPE_FOLDER);
    do_check_eq(PlacesUtils.bookmarks.getItemTitle(id), folder.title);
    do_check_eq(PlacesUtils.bookmarks.getFolderIdForItem(id),
                PlacesUtils.bookmarks.toolbarFolder);

    _("Have the store create a new record object. Verify that it has the same data.");
    let newrecord = store.createRecord(folder.id);
    do_check_true(newrecord instanceof BookmarkFolder);
    for (let property of ["title", "parentName", "parentid"])
      do_check_eq(newrecord[property], folder[property]);

    _("Folders have high sort index to ensure they're synced first.");
    do_check_eq(newrecord.sortindex, 1000000);
  } finally {
    _("Clean up.");
    store.wipe();
    run_next_test();
  }
});

add_test(function test_folder_createRecord() {
  try {
    _("Create a folder.");
    let folder1_id = PlacesUtils.bookmarks.createFolder(
      PlacesUtils.bookmarks.toolbarFolder, "Folder1", 0);
    let folder1_guid = store.GUIDForId(folder1_id);

    _("Create two bookmarks in that folder without assigning them GUIDs.");
    let bmk1_id = PlacesUtils.bookmarks.insertBookmark(
      folder1_id, fxuri, PlacesUtils.bookmarks.DEFAULT_INDEX, "Get Firefox!");
    let bmk2_id = PlacesUtils.bookmarks.insertBookmark(
      folder1_id, tburi, PlacesUtils.bookmarks.DEFAULT_INDEX, "Get Thunderbird!");

    _("Create a record for the folder and verify basic properties.");
    let record = store.createRecord(folder1_guid);
    do_check_true(record instanceof BookmarkFolder);
    do_check_eq(record.title, "Folder1");
    do_check_eq(record.parentid, "toolbar");
    do_check_eq(record.parentName, "Bookmarks Toolbar");

    _("Verify the folder's children. Ensures that the bookmarks were given GUIDs.");
    let bmk1_guid = store.GUIDForId(bmk1_id);
    let bmk2_guid = store.GUIDForId(bmk2_id);
    do_check_eq(record.children.length, 2);
    do_check_eq(record.children[0], bmk1_guid);
    do_check_eq(record.children[1], bmk2_guid);

  } finally {
    _("Clean up.");
    store.wipe();
    run_next_test();
  }
});

add_task(function* test_deleted() {
  try {
    _("Create a bookmark that will be deleted.");
    let bmk1_id = PlacesUtils.bookmarks.insertBookmark(
      PlacesUtils.bookmarks.toolbarFolder, fxuri,
      PlacesUtils.bookmarks.DEFAULT_INDEX, "Get Firefox!");
    let bmk1_guid = store.GUIDForId(bmk1_id);

    _("Delete the bookmark through the store.");
    let record = new PlacesItem("bookmarks", bmk1_guid);
    record.deleted = true;
    store.applyIncoming(record);
    yield store.deletePending();
    _("Ensure it has been deleted.");
    let error;
    try {
      PlacesUtils.bookmarks.getBookmarkURI(bmk1_id);
    } catch(ex) {
      error = ex;
    }
    do_check_eq(error.result, Cr.NS_ERROR_ILLEGAL_VALUE);

    let newrec = store.createRecord(bmk1_guid);
    do_check_eq(newrec.deleted, true);

  } finally {
    _("Clean up.");
    store.wipe();
  }
});

add_test(function test_move_folder() {
  try {
    _("Create two folders and a bookmark in one of them.");
    let folder1_id = PlacesUtils.bookmarks.createFolder(
      PlacesUtils.bookmarks.toolbarFolder, "Folder1", 0);
    let folder1_guid = store.GUIDForId(folder1_id);
    let folder2_id = PlacesUtils.bookmarks.createFolder(
      PlacesUtils.bookmarks.toolbarFolder, "Folder2", 0);
    let folder2_guid = store.GUIDForId(folder2_id);
    let bmk_id = PlacesUtils.bookmarks.insertBookmark(
      folder1_id, fxuri, PlacesUtils.bookmarks.DEFAULT_INDEX, "Get Firefox!");
    let bmk_guid = store.GUIDForId(bmk_id);

    _("Get a record, reparent it and apply it to the store.");
    let record = store.createRecord(bmk_guid);
    do_check_eq(record.parentid, folder1_guid);
    record.parentid = folder2_guid;
    store.applyIncoming(record);

    _("Verify the new parent.");
    let new_folder_id = PlacesUtils.bookmarks.getFolderIdForItem(bmk_id);
    do_check_eq(store.GUIDForId(new_folder_id), folder2_guid);
  } finally {
    _("Clean up.");
    store.wipe();
    run_next_test();
  }
});

add_test(function test_move_order() {
  // Make sure the tracker is turned on.
  Svc.Obs.notify("weave:engine:start-tracking");
  try {
    _("Create two bookmarks");
    let bmk1_id = PlacesUtils.bookmarks.insertBookmark(
      PlacesUtils.bookmarks.toolbarFolder, fxuri,
      PlacesUtils.bookmarks.DEFAULT_INDEX, "Get Firefox!");
    let bmk1_guid = store.GUIDForId(bmk1_id);
    let bmk2_id = PlacesUtils.bookmarks.insertBookmark(
      PlacesUtils.bookmarks.toolbarFolder, tburi,
      PlacesUtils.bookmarks.DEFAULT_INDEX, "Get Thunderbird!");
    let bmk2_guid = store.GUIDForId(bmk2_id);

    _("Verify order.");
    do_check_eq(PlacesUtils.bookmarks.getItemIndex(bmk1_id), 0);
    do_check_eq(PlacesUtils.bookmarks.getItemIndex(bmk2_id), 1);
    let toolbar = store.createRecord("toolbar");
    do_check_eq(toolbar.children.length, 2);
    do_check_eq(toolbar.children[0], bmk1_guid);
    do_check_eq(toolbar.children[1], bmk2_guid);

    _("Move bookmarks around.");
    store._childrenToOrder = {};
    toolbar.children = [bmk2_guid, bmk1_guid];
    store.applyIncoming(toolbar);
    // Bookmarks engine does this at the end of _processIncoming
    tracker.ignoreAll = true;
    store._orderChildren();
    tracker.ignoreAll = false;
    delete store._childrenToOrder;

    _("Verify new order.");
    do_check_eq(PlacesUtils.bookmarks.getItemIndex(bmk2_id), 0);
    do_check_eq(PlacesUtils.bookmarks.getItemIndex(bmk1_id), 1);

  } finally {
    Svc.Obs.notify("weave:engine:stop-tracking");
    _("Clean up.");
    store.wipe();
    run_next_test();
  }
});

add_test(function test_orphan() {
  try {

    _("Add a new bookmark locally.");
    let bmk1_id = PlacesUtils.bookmarks.insertBookmark(
      PlacesUtils.bookmarks.toolbarFolder, fxuri,
      PlacesUtils.bookmarks.DEFAULT_INDEX, "Get Firefox!");
    let bmk1_guid = store.GUIDForId(bmk1_id);
    do_check_eq(PlacesUtils.bookmarks.getFolderIdForItem(bmk1_id),
                PlacesUtils.bookmarks.toolbarFolder);
    let error;
    try {
      PlacesUtils.annotations.getItemAnnotation(bmk1_id, PARENT_ANNO);
    } catch(ex) {
      error = ex;
    }
    do_check_eq(error.result, Cr.NS_ERROR_NOT_AVAILABLE);

    _("Apply a server record that is the same but refers to non-existent folder.");
    let record = store.createRecord(bmk1_guid);
    record.parentid = "non-existent";
    store.applyIncoming(record);

    _("Verify that bookmark has been flagged as orphan, has not moved.");
    do_check_eq(PlacesUtils.bookmarks.getFolderIdForItem(bmk1_id),
                PlacesUtils.bookmarks.toolbarFolder);
    do_check_eq(PlacesUtils.annotations.getItemAnnotation(bmk1_id, PARENT_ANNO),
                "non-existent");

  } finally {
    _("Clean up.");
    store.wipe();
    run_next_test();
  }
});

add_test(function test_reparentOrphans() {
  try {
    let folder1_id = PlacesUtils.bookmarks.createFolder(
      PlacesUtils.bookmarks.toolbarFolder, "Folder1", 0);
    let folder1_guid = store.GUIDForId(folder1_id);

    _("Create a bogus orphan record and write the record back to the store to trigger _reparentOrphans.");
    PlacesUtils.annotations.setItemAnnotation(
      folder1_id, PARENT_ANNO, folder1_guid, 0,
      PlacesUtils.annotations.EXPIRE_NEVER);
    let record = store.createRecord(folder1_guid);
    record.title = "New title for Folder 1";
    store._childrenToOrder = {};
    store.applyIncoming(record);

    _("Verify that is has been marked as an orphan even though it couldn't be moved into itself.");
    do_check_eq(PlacesUtils.annotations.getItemAnnotation(folder1_id, PARENT_ANNO),
                folder1_guid);

  } finally {
    _("Clean up.");
    store.wipe();
    run_next_test();
  }
});

// Tests Bug 806460, in which query records arrive with empty folder
// names and missing bookmark URIs.
add_test(function test_empty_query_doesnt_die() {
  let record = new BookmarkQuery("bookmarks", "8xoDGqKrXf1P");
  record.folderName    = "";
  record.queryId       = "";
  record.parentName    = "Toolbar";
  record.parentid      = "toolbar";

  // These should not throw.
  store.applyIncoming(record);

  delete record.folderName;
  store.applyIncoming(record);
  
  run_next_test();
});

function assertDeleted(id) {
  let error;
  try {
    PlacesUtils.bookmarks.getItemType(id);
  } catch (e) {
    error = e;
  }
  equal(error.result, Cr.NS_ERROR_ILLEGAL_VALUE)
}

add_task(function* test_delete_buffering() {
  store.wipe();
  try {
    _("Create a folder with two bookmarks.");
    let folder = new BookmarkFolder("bookmarks", "testfolder-1");
    folder.parentName = "Bookmarks Toolbar";
    folder.parentid = "toolbar";
    folder.title = "Test Folder";
    store.applyIncoming(folder);


    let fxRecord = new Bookmark("bookmarks", "get-firefox1");
    fxRecord.bmkUri        = fxuri.spec;
    fxRecord.title         = "Get Firefox!";
    fxRecord.parentName    = "Test Folder";
    fxRecord.parentid      = "testfolder-1";

    let tbRecord = new Bookmark("bookmarks", "get-tndrbrd1");
    tbRecord.bmkUri        = tburi.spec;
    tbRecord.title         = "Get Thunderbird!";
    tbRecord.parentName    = "Test Folder";
    tbRecord.parentid      = "testfolder-1";

    store.applyIncoming(fxRecord);
    store.applyIncoming(tbRecord);

    let folderId = store.idForGUID(folder.id);
    let fxRecordId = store.idForGUID(fxRecord.id);
    let tbRecordId = store.idForGUID(tbRecord.id);

    _("Check everything was created correctly.");

    equal(PlacesUtils.bookmarks.getItemType(fxRecordId),
          PlacesUtils.bookmarks.TYPE_BOOKMARK);
    equal(PlacesUtils.bookmarks.getItemType(tbRecordId),
          PlacesUtils.bookmarks.TYPE_BOOKMARK);
    equal(PlacesUtils.bookmarks.getItemType(folderId),
          PlacesUtils.bookmarks.TYPE_FOLDER);

    equal(PlacesUtils.bookmarks.getFolderIdForItem(fxRecordId), folderId);
    equal(PlacesUtils.bookmarks.getFolderIdForItem(tbRecordId), folderId);
    equal(PlacesUtils.bookmarks.getFolderIdForItem(folderId),
          PlacesUtils.bookmarks.toolbarFolder);

    _("Delete the folder and one bookmark.");

    let deleteFolder = new PlacesItem("bookmarks", "testfolder-1");
    deleteFolder.deleted = true;

    let deleteFxRecord = new PlacesItem("bookmarks", "get-firefox1");
    deleteFxRecord.deleted = true;

    store.applyIncoming(deleteFolder);
    store.applyIncoming(deleteFxRecord);

    _("Check that we haven't deleted them yet, but that the deletions are queued");
    // these will throw if we've deleted them
    equal(PlacesUtils.bookmarks.getItemType(fxRecordId),
           PlacesUtils.bookmarks.TYPE_BOOKMARK);

    equal(PlacesUtils.bookmarks.getItemType(folderId),
           PlacesUtils.bookmarks.TYPE_FOLDER);

    equal(PlacesUtils.bookmarks.getFolderIdForItem(fxRecordId), folderId);

    ok(store._foldersToDelete.has(folder.id));
    ok(store._atomsToDelete.has(fxRecord.id));
    ok(!store._atomsToDelete.has(tbRecord.id));

    _("Process pending deletions and ensure that the right things are deleted.");
    let updatedGuids = yield store.deletePending();

    deepEqual(updatedGuids.sort(), ["get-tndrbrd1", "toolbar"]);

    assertDeleted(fxRecordId);
    assertDeleted(folderId);

    ok(!store._foldersToDelete.has(folder.id));
    ok(!store._atomsToDelete.has(fxRecord.id));

    equal(PlacesUtils.bookmarks.getFolderIdForItem(tbRecordId),
          PlacesUtils.bookmarks.toolbarFolder);

  } finally {
    _("Clean up.");
    store.wipe();
  }
});


function run_test() {
  initTestLogging('Trace');
  run_next_test();
}