-
Notifications
You must be signed in to change notification settings - Fork 54
/
test_sync_utils.js
3878 lines (3530 loc) · 109 KB
/
test_sync_utils.js
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
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
ChromeUtils.defineModuleGetter(
this,
"Preferences",
"resource://gre/modules/Preferences.jsm"
);
const SYNC_PARENT_ANNO = "sync/parent";
var makeGuid = PlacesUtils.history.makeGuid;
function shuffle(array) {
let results = [];
for (let i = 0; i < array.length; ++i) {
let randomIndex = Math.floor(Math.random() * (i + 1));
results[i] = results[randomIndex];
results[randomIndex] = array[i];
}
return results;
}
async function assertTagForURLs(tag, urls, message) {
let taggedURLs = new Set();
await PlacesUtils.bookmarks.fetch({ tags: [tag] }, b =>
taggedURLs.add(b.url.href)
);
deepEqual(
Array.from(taggedURLs).sort(compareAscending),
urls.sort(compareAscending),
message
);
}
function assertURLHasTags(url, tags, message) {
let actualTags = PlacesUtils.tagging.getTagsForURI(uri(url));
deepEqual(actualTags.sort(compareAscending), tags, message);
}
var populateTree = async function populate(parentGuid, ...items) {
let guids = {};
for (let index = 0; index < items.length; index++) {
let item = items[index];
let guid = makeGuid();
switch (item.kind) {
case "bookmark":
case "query":
await PlacesUtils.bookmarks.insert({
type: PlacesUtils.bookmarks.TYPE_BOOKMARK,
url: item.url,
title: item.title,
parentGuid,
guid,
index,
});
break;
case "separator":
await PlacesUtils.bookmarks.insert({
type: PlacesUtils.bookmarks.TYPE_SEPARATOR,
parentGuid,
guid,
});
break;
case "folder":
await PlacesUtils.bookmarks.insert({
type: PlacesUtils.bookmarks.TYPE_FOLDER,
title: item.title,
parentGuid,
guid,
});
if (item.children) {
Object.assign(guids, await populate(guid, ...item.children));
}
break;
default:
throw new Error(`Unsupported item type: ${item.type}`);
}
guids[item.title] = guid;
}
return guids;
};
var recordIdToId = async function recordIdToId(recordId) {
let guid = await PlacesSyncUtils.bookmarks.recordIdToGuid(recordId);
return PlacesUtils.promiseItemId(guid);
};
var moveSyncedBookmarksToUnsyncedParent = async function() {
info("Insert synced bookmarks");
let syncedGuids = await populateTree(
PlacesUtils.bookmarks.menuGuid,
{
kind: "folder",
title: "folder",
children: [
{
kind: "bookmark",
title: "childBmk",
url: "https://example.org",
},
],
},
{
kind: "bookmark",
title: "topBmk",
url: "https://example.com",
}
);
// Pretend we've synced each bookmark at least once.
await PlacesTestUtils.setBookmarkSyncFields(
...Object.values(syncedGuids).map(guid => ({
guid,
syncStatus: PlacesUtils.bookmarks.SYNC_STATUS.NORMAL,
}))
);
info("Make new folder");
let unsyncedFolder = await PlacesUtils.bookmarks.insert({
type: PlacesUtils.bookmarks.TYPE_FOLDER,
parentGuid: PlacesUtils.bookmarks.menuGuid,
title: "unsyncedFolder",
});
info("Move synced bookmarks into unsynced new folder");
for (let guid of Object.values(syncedGuids)) {
await PlacesUtils.bookmarks.update({
guid,
parentGuid: unsyncedFolder.guid,
index: PlacesUtils.bookmarks.DEFAULT_INDEX,
});
}
return { syncedGuids, unsyncedFolder };
};
var setChangesSynced = async function(changes) {
for (let recordId in changes) {
changes[recordId].synced = true;
}
await PlacesSyncUtils.bookmarks.pushChanges(changes);
};
var ignoreChangedRoots = async function() {
let changes = await PlacesSyncUtils.bookmarks.pullChanges();
let expectedRoots = ["menu", "mobile", "toolbar", "unfiled"];
if (!ObjectUtils.deepEqual(Object.keys(changes).sort(), expectedRoots)) {
// Make sure the previous test cleaned up.
throw new Error(
`Unexpected changes at start of test: ${JSON.stringify(changes)}`
);
}
await setChangesSynced(changes);
};
add_task(async function test_fetchURLFrecency() {
// Add visits to the following URLs and then check if frecency for those URLs is not -1.
let arrayOfURLsToVisit = [
"https://www.mozilla.org/en-US/",
"http://getfirefox.com",
"http://getthunderbird.com",
];
for (let url of arrayOfURLsToVisit) {
await PlacesTestUtils.addVisits(url);
}
for (let url of arrayOfURLsToVisit) {
let frecency = await PlacesSyncUtils.history.fetchURLFrecency(url);
equal(typeof frecency, "number", "The frecency should be of type: number");
notEqual(
frecency,
-1,
"The frecency of this url should be different than -1"
);
}
// Do not add visits to the following URLs, and then check if frecency for those URLs is -1.
let arrayOfURLsNotVisited = ["https://bugzilla.org", "https://example.org"];
for (let url of arrayOfURLsNotVisited) {
let frecency = await PlacesSyncUtils.history.fetchURLFrecency(url);
equal(frecency, -1, "The frecency of this url should be -1");
}
// Remove the visits added during this test.
await PlacesUtils.history.clear();
});
add_task(async function test_determineNonSyncableGuids() {
// Add visits to the following URLs with different transition types.
let arrayOfVisits = [
{ uri: "https://www.mozilla.org/en-US/", transition: TRANSITION_TYPED },
{ uri: "http://getfirefox.com/", transition: TRANSITION_LINK },
{ uri: "http://getthunderbird.com/", transition: TRANSITION_FRAMED_LINK },
];
for (let visit of arrayOfVisits) {
await PlacesTestUtils.addVisits(visit);
}
// Fetch the guid for each visit.
let guids = [];
let dictURLGuid = {};
for (let visit of arrayOfVisits) {
let guid = await PlacesSyncUtils.history.fetchGuidForURL(visit.uri);
guids.push(guid);
dictURLGuid[visit.uri] = guid;
}
// Filter the visits.
let filteredGuids = await PlacesSyncUtils.history.determineNonSyncableGuids(
guids
);
// Check if the filtered visits are of type TRANSITION_FRAMED_LINK.
for (let visit of arrayOfVisits) {
if (visit.transition === TRANSITION_FRAMED_LINK) {
ok(
filteredGuids.includes(dictURLGuid[visit.uri]),
"This url should be one of the filtered guids."
);
} else {
ok(
!filteredGuids.includes(dictURLGuid[visit.uri]),
"This url should not be one of the filtered guids."
);
}
}
// Remove the visits added during this test.
await PlacesUtils.history.clear();
});
add_task(async function test_changeGuid() {
// Add some visits of the following URLs.
let arrayOfURLsToVisit = [
"https://www.mozilla.org/en-US/",
"http://getfirefox.com/",
"http://getthunderbird.com/",
];
for (let url of arrayOfURLsToVisit) {
await PlacesTestUtils.addVisits(url);
}
for (let url of arrayOfURLsToVisit) {
let originalGuid = await PlacesSyncUtils.history.fetchGuidForURL(url);
let newGuid = makeGuid();
// Change the original GUID for the new GUID.
await PlacesSyncUtils.history.changeGuid(url, newGuid);
// Fetch the GUID for this URL.
let newGuidFetched = await PlacesSyncUtils.history.fetchGuidForURL(url);
// Check that the URL has the new GUID as its GUID and not the original one.
equal(
newGuid,
newGuidFetched,
"These should be equal since we changed the guid for the visit."
);
notEqual(
originalGuid,
newGuidFetched,
"These should be different since we changed the guid for the visit."
);
}
// Remove the visits added during this test.
await PlacesUtils.history.clear();
});
add_task(async function test_fetchVisitsForURL() {
// Get the date for this moment and a date for a minute ago.
let now = new Date();
let aMinuteAgo = new Date(now.getTime() - 1 * 60000);
// Add some visits of the following URLs, specifying the transition and the visit date.
let arrayOfVisits = [
{
uri: "https://www.mozilla.org/en-US/",
transition: TRANSITION_TYPED,
visitDate: aMinuteAgo,
},
{
uri: "http://getfirefox.com/",
transition: TRANSITION_LINK,
visitDate: aMinuteAgo,
},
{
uri: "http://getthunderbird.com/",
transition: TRANSITION_LINK,
visitDate: aMinuteAgo,
},
];
for (let elem of arrayOfVisits) {
await PlacesTestUtils.addVisits(elem);
}
for (let elem of arrayOfVisits) {
// Fetch all the visits for this URL.
let visits = await PlacesSyncUtils.history.fetchVisitsForURL(elem.uri);
// Since the visit we added will be the last one in the collection of visits, we get the index of it.
let iLast = visits.length - 1;
// The date is saved in _micro_seconds, here we change it to milliseconds.
let dateInMilliseconds = visits[iLast].date * 0.001;
// Check that the info we provided for this URL is the same one retrieved.
equal(
dateInMilliseconds,
elem.visitDate.getTime(),
"The date we provided should be the same we retrieved."
);
equal(
visits[iLast].type,
elem.transition,
"The transition type we provided should be the same we retrieved."
);
}
// Remove the visits added during this test.
await PlacesUtils.history.clear();
});
add_task(async function test_fetchGuidForURL() {
// Add some visits of the following URLs.
let arrayOfURLsToVisit = [
"https://www.mozilla.org/en-US/",
"http://getfirefox.com/",
"http://getthunderbird.com/",
];
for (let url of arrayOfURLsToVisit) {
await PlacesTestUtils.addVisits(url);
}
// This tries to test fetchGuidForURL in two ways:
// 1- By fetching the GUID, and then using that GUID to retrieve the info of the visit.
// It then compares the URL with the URL that is on the visits info.
// 2- By creating a new GUID, changing the GUID for the visit, fetching the GUID and comparing them.
for (let url of arrayOfURLsToVisit) {
let guid = await PlacesSyncUtils.history.fetchGuidForURL(url);
let info = await PlacesSyncUtils.history.fetchURLInfoForGuid(guid);
let newGuid = makeGuid();
await PlacesSyncUtils.history.changeGuid(url, newGuid);
let newGuid2 = await PlacesSyncUtils.history.fetchGuidForURL(url);
equal(
url,
info.url,
"The url provided and the url retrieved should be the same."
);
equal(
newGuid,
newGuid2,
"The changed guid and the retrieved guid should be the same."
);
}
// Remove the visits added during this test.
await PlacesUtils.history.clear();
});
add_task(async function test_fetchURLInfoForGuid() {
// Add some visits of the following URLs. specifying the title.
let visits = [
{ uri: "https://www.mozilla.org/en-US/", title: "mozilla" },
{ uri: "http://getfirefox.com/", title: "firefox" },
{ uri: "http://getthunderbird.com/", title: "thunderbird" },
{ uri: "http://quantum.mozilla.com/", title: null },
];
for (let visit of visits) {
await PlacesTestUtils.addVisits(visit);
}
for (let visit of visits) {
let guid = await PlacesSyncUtils.history.fetchGuidForURL(visit.uri);
let info = await PlacesSyncUtils.history.fetchURLInfoForGuid(guid);
// Compare the info returned by fetchURLInfoForGuid,
// URL and title should match while frecency must be different than -1.
equal(
info.url,
visit.uri,
"The url provided should be the same as the url retrieved."
);
equal(
info.title,
visit.title || "",
"The title provided should be the same as the title retrieved."
);
notEqual(
info.frecency,
-1,
"The frecency of the visit should be different than -1."
);
}
// Create a "fake" GUID and check that the result of fetchURLInfoForGuid is null.
let guid = makeGuid();
let info = await PlacesSyncUtils.history.fetchURLInfoForGuid(guid);
equal(
info,
null,
"The information object of a non-existent guid should be null."
);
// Remove the visits added during this test.
await PlacesUtils.history.clear();
});
add_task(async function test_getAllURLs() {
// Add some visits of the following URLs.
let arrayOfURLsToVisit = [
"https://www.mozilla.org/en-US/",
"http://getfirefox.com/",
"http://getthunderbird.com/",
];
for (let url of arrayOfURLsToVisit) {
await PlacesTestUtils.addVisits(url);
}
// Get all URLs.
let allURLs = await PlacesSyncUtils.history.getAllURLs({
since: new Date(Date.now() - 2592000000),
limit: 5000,
});
// The amount of URLs must be the same in both collections.
equal(
allURLs.length,
arrayOfURLsToVisit.length,
"The amount of urls retrived should match the amount of urls provided."
);
// Check that the correct URLs were retrived.
for (let url of arrayOfURLsToVisit) {
ok(
allURLs.includes(url),
"The urls retrieved should match the ones used in this test."
);
}
// Remove the visits added during this test.
await PlacesUtils.history.clear();
});
add_task(async function test_order() {
info("Insert some bookmarks");
let guids = await populateTree(
PlacesUtils.bookmarks.menuGuid,
{
kind: "bookmark",
title: "childBmk",
url: "http://getfirefox.com",
},
{
kind: "bookmark",
title: "siblingBmk",
url: "http://getthunderbird.com",
},
{
kind: "folder",
title: "siblingFolder",
},
{
kind: "separator",
title: "siblingSep",
}
);
info("Reorder inserted bookmarks");
{
let order = [
guids.siblingFolder,
guids.siblingSep,
guids.childBmk,
guids.siblingBmk,
];
await PlacesSyncUtils.bookmarks.order(
PlacesUtils.bookmarks.menuGuid,
order
);
let childRecordIds = await PlacesSyncUtils.bookmarks.fetchChildRecordIds(
PlacesUtils.bookmarks.menuGuid
);
deepEqual(
childRecordIds,
order,
"New bookmarks should be reordered according to array"
);
}
info("Same order with unspecified children");
{
await PlacesSyncUtils.bookmarks.order(PlacesUtils.bookmarks.menuGuid, [
guids.siblingSep,
guids.siblingBmk,
]);
let childRecordIds = await PlacesSyncUtils.bookmarks.fetchChildRecordIds(
PlacesUtils.bookmarks.menuGuid
);
deepEqual(
childRecordIds,
[guids.siblingFolder, guids.siblingSep, guids.childBmk, guids.siblingBmk],
"Current order should be respected if possible"
);
}
info("New order with unspecified children");
{
await PlacesSyncUtils.bookmarks.order(PlacesUtils.bookmarks.menuGuid, [
guids.siblingBmk,
guids.siblingSep,
]);
let childRecordIds = await PlacesSyncUtils.bookmarks.fetchChildRecordIds(
PlacesUtils.bookmarks.menuGuid
);
deepEqual(
childRecordIds,
[guids.siblingBmk, guids.siblingSep, guids.siblingFolder, guids.childBmk],
"Unordered children should be moved to end if current order can't be respected"
);
}
info("Reorder with nonexistent children");
{
await PlacesSyncUtils.bookmarks.order(PlacesUtils.bookmarks.menuGuid, [
guids.childBmk,
makeGuid(),
guids.siblingBmk,
guids.siblingSep,
makeGuid(),
guids.siblingFolder,
makeGuid(),
]);
let childRecordIds = await PlacesSyncUtils.bookmarks.fetchChildRecordIds(
PlacesUtils.bookmarks.menuGuid
);
deepEqual(
childRecordIds,
[guids.childBmk, guids.siblingBmk, guids.siblingSep, guids.siblingFolder],
"Nonexistent children should be ignored"
);
}
await PlacesUtils.bookmarks.eraseEverything();
await PlacesSyncUtils.bookmarks.reset();
});
add_task(async function test_dedupe() {
await ignoreChangedRoots();
let parentFolder = await PlacesSyncUtils.bookmarks.insert({
kind: "folder",
recordId: makeGuid(),
parentRecordId: "menu",
});
let differentParentFolder = await PlacesSyncUtils.bookmarks.insert({
kind: "folder",
recordId: makeGuid(),
parentRecordId: "menu",
});
let mozBmk = await PlacesSyncUtils.bookmarks.insert({
kind: "bookmark",
recordId: makeGuid(),
parentRecordId: parentFolder.recordId,
url: "https://mozilla.org",
});
let fxBmk = await PlacesSyncUtils.bookmarks.insert({
kind: "bookmark",
recordId: makeGuid(),
parentRecordId: parentFolder.recordId,
url: "http://getfirefox.com",
});
let tbBmk = await PlacesSyncUtils.bookmarks.insert({
kind: "bookmark",
recordId: makeGuid(),
parentRecordId: parentFolder.recordId,
url: "http://getthunderbird.com",
});
await Assert.rejects(
PlacesSyncUtils.bookmarks.dedupe(makeGuid(), makeGuid(), makeGuid()),
/does not exist/,
"Should reject attempts to de-dupe nonexistent items"
);
await Assert.rejects(
PlacesSyncUtils.bookmarks.dedupe("menu", makeGuid(), "places"),
/Cannot de-dupe local root/,
"Should reject attempts to de-dupe local roots"
);
info("De-dupe with same remote parent");
{
let localId = await PlacesUtils.promiseItemId(mozBmk.recordId);
let newRemoteRecordId = makeGuid();
let changes = await PlacesSyncUtils.bookmarks.dedupe(
mozBmk.recordId,
newRemoteRecordId,
parentFolder.recordId
);
deepEqual(
Object.keys(changes).sort(),
[
parentFolder.recordId, // Parent.
mozBmk.recordId, // Tombstone for old sync ID.
].sort(),
"Should bump change counter of parent"
);
ok(
changes[mozBmk.recordId].tombstone,
"Should write tombstone for old local sync ID"
);
ok(
Object.values(changes).every(change => change.counter === 1),
"Change counter for every bookmark should be 1"
);
ok(
!(await PlacesUtils.bookmarks.fetch(mozBmk.recordId)),
"Bookmark with old local sync ID should not exist"
);
await Assert.rejects(
PlacesUtils.promiseItemId(mozBmk.recordId),
/no item found for the given GUID/,
"Should invalidate GUID cache entry for old local sync ID"
);
let newMozBmk = await PlacesUtils.bookmarks.fetch(newRemoteRecordId);
equal(
newMozBmk.guid,
newRemoteRecordId,
"Should change local sync ID to remote sync ID"
);
equal(
await PlacesUtils.promiseItemId(newRemoteRecordId),
localId,
"Should add new remote sync ID to GUID cache"
);
await setChangesSynced(changes);
}
info("De-dupe with different remote parent");
{
let localId = await PlacesUtils.promiseItemId(fxBmk.recordId);
let newRemoteRecordId = makeGuid();
let changes = await PlacesSyncUtils.bookmarks.dedupe(
fxBmk.recordId,
newRemoteRecordId,
differentParentFolder.recordId
);
deepEqual(
Object.keys(changes).sort(),
[
parentFolder.recordId, // Old local parent.
differentParentFolder.recordId, // New remote parent.
fxBmk.recordId, // Tombstone for old sync ID.
].sort(),
"Should bump change counter of old parent and new parent"
);
ok(
changes[fxBmk.recordId].tombstone,
"Should write tombstone for old local sync ID"
);
ok(
Object.values(changes).every(change => change.counter === 1),
"Change counter for every bookmark should be 1"
);
let newFxBmk = await PlacesUtils.bookmarks.fetch(newRemoteRecordId);
equal(
newFxBmk.parentGuid,
parentFolder.recordId,
"De-duping should not move bookmark to new parent"
);
equal(
await PlacesUtils.promiseItemId(newRemoteRecordId),
localId,
"De-duping with different remote parent should cache new sync ID"
);
await setChangesSynced(changes);
}
info("De-dupe with nonexistent remote parent");
{
let localId = await PlacesUtils.promiseItemId(tbBmk.recordId);
let newRemoteRecordId = makeGuid();
let remoteParentRecordId = makeGuid();
let changes = await PlacesSyncUtils.bookmarks.dedupe(
tbBmk.recordId,
newRemoteRecordId,
remoteParentRecordId
);
deepEqual(
Object.keys(changes).sort(),
[
parentFolder.recordId, // Old local parent.
tbBmk.recordId, // Tombstone for old sync ID.
].sort(),
"Should bump change counter of old parent"
);
ok(
changes[tbBmk.recordId].tombstone,
"Should write tombstone for old local sync ID"
);
ok(
Object.values(changes).every(change => change.counter === 1),
"Change counter for every bookmark should be 1"
);
equal(
await PlacesUtils.promiseItemId(newRemoteRecordId),
localId,
"De-duping with nonexistent remote parent should cache new sync ID"
);
await setChangesSynced(changes);
}
await PlacesUtils.bookmarks.eraseEverything();
await PlacesSyncUtils.bookmarks.reset();
});
add_task(async function test_order_roots() {
let oldOrder = await PlacesSyncUtils.bookmarks.fetchChildRecordIds(
PlacesUtils.bookmarks.rootGuid
);
await PlacesSyncUtils.bookmarks.order(
PlacesUtils.bookmarks.rootGuid,
shuffle(oldOrder)
);
let newOrder = await PlacesSyncUtils.bookmarks.fetchChildRecordIds(
PlacesUtils.bookmarks.rootGuid
);
deepEqual(oldOrder, newOrder, "Should ignore attempts to reorder roots");
await PlacesUtils.bookmarks.eraseEverything();
await PlacesSyncUtils.bookmarks.reset();
});
add_task(async function test_update_tags() {
info("Insert item without tags");
let item = await PlacesSyncUtils.bookmarks.insert({
kind: "bookmark",
url: "https://mozilla.org",
recordId: makeGuid(),
parentRecordId: "menu",
});
info("Add tags");
{
let updatedItem = await PlacesSyncUtils.bookmarks.update({
recordId: item.recordId,
tags: ["foo", "bar"],
});
deepEqual(updatedItem.tags, ["foo", "bar"], "Should return new tags");
assertURLHasTags(
"https://mozilla.org",
["bar", "foo"],
"Should set new tags for URL"
);
}
info("Add new tag, remove existing tag");
{
let updatedItem = await PlacesSyncUtils.bookmarks.update({
recordId: item.recordId,
tags: ["foo", "baz"],
});
deepEqual(updatedItem.tags, ["foo", "baz"], "Should return updated tags");
assertURLHasTags(
"https://mozilla.org",
["baz", "foo"],
"Should update tags for URL"
);
await assertTagForURLs("bar", [], "Should remove existing tag");
}
info("Tags with whitespace");
{
let updatedItem = await PlacesSyncUtils.bookmarks.update({
recordId: item.recordId,
tags: [" leading", "trailing ", " baz ", " "],
});
deepEqual(
updatedItem.tags,
["leading", "trailing", "baz"],
"Should return filtered tags"
);
assertURLHasTags(
"https://mozilla.org",
["baz", "leading", "trailing"],
"Should trim whitespace and filter blank tags"
);
}
info("Remove all tags");
{
let updatedItem = await PlacesSyncUtils.bookmarks.update({
recordId: item.recordId,
tags: null,
});
deepEqual(updatedItem.tags, [], "Should return empty tag array");
assertURLHasTags(
"https://mozilla.org",
[],
"Should remove all existing tags"
);
}
await PlacesUtils.bookmarks.eraseEverything();
await PlacesSyncUtils.bookmarks.reset();
});
add_task(async function test_pullChanges_tags() {
await ignoreChangedRoots();
info("Insert untagged items with same URL");
let firstItem = await PlacesSyncUtils.bookmarks.insert({
kind: "bookmark",
recordId: makeGuid(),
parentRecordId: "menu",
url: "https://example.org",
});
let secondItem = await PlacesSyncUtils.bookmarks.insert({
kind: "bookmark",
recordId: makeGuid(),
parentRecordId: "menu",
url: "https://example.org",
});
let untaggedItem = await PlacesSyncUtils.bookmarks.insert({
kind: "bookmark",
recordId: makeGuid(),
parentRecordId: "menu",
url: "https://bugzilla.org",
});
let taggedItem = await PlacesSyncUtils.bookmarks.insert({
kind: "bookmark",
recordId: makeGuid(),
parentRecordId: "menu",
url: "https://mozilla.org",
});
info("Create tag");
PlacesUtils.tagging.tagURI(uri("https://example.org"), ["taggy"]);
let tagBm = await PlacesUtils.bookmarks.fetch({
parentGuid: PlacesUtils.bookmarks.tagsGuid,
index: 0,
});
let tagFolderGuid = tagBm.guid;
let tagFolderId = await PlacesUtils.promiseItemId(tagFolderGuid);
info("Tagged bookmarks should be in changeset");
{
let changes = await PlacesSyncUtils.bookmarks.pullChanges();
deepEqual(
Object.keys(changes).sort(),
[firstItem.recordId, secondItem.recordId].sort(),
"Should include tagged bookmarks in changeset"
);
await setChangesSynced(changes);
}
info("Change tag case");
{
PlacesUtils.tagging.tagURI(uri("https://mozilla.org"), ["TaGgY"]);
let changes = await PlacesSyncUtils.bookmarks.pullChanges();
deepEqual(
Object.keys(changes).sort(),
[firstItem.recordId, secondItem.recordId, taggedItem.recordId].sort(),
"Should include tagged bookmarks after changing case"
);
await assertTagForURLs(
"TaGgY",
["https://example.org/", "https://mozilla.org/"],
"Should add tag for new URL"
);
await setChangesSynced(changes);
}
// These tests change a tag item directly, without going through the tagging
// service. This behavior isn't supported, but the tagging service registers
// an observer to handle these cases, so we make sure we handle them
// correctly.
info("Rename tag folder using Bookmarks.setItemTitle");
{
PlacesUtils.bookmarks.setItemTitle(tagFolderId, "sneaky");
deepEqual(
(await PlacesUtils.bookmarks.fetchTags()).map(t => t.name),
["sneaky"],
"Tagging service should update cache with new title"
);
let changes = await PlacesSyncUtils.bookmarks.pullChanges();
deepEqual(
Object.keys(changes).sort(),
[firstItem.recordId, secondItem.recordId].sort(),
"Should include tagged bookmarks after renaming tag folder"
);
await setChangesSynced(changes);
}
info("Rename tag folder using Bookmarks.update");
{
await PlacesUtils.bookmarks.update({
guid: tagFolderGuid,
title: "tricky",
});
deepEqual(
(await PlacesUtils.bookmarks.fetchTags()).map(t => t.name),
["tricky"],
"Tagging service should update cache after updating tag folder"
);
let changes = await PlacesSyncUtils.bookmarks.pullChanges();
deepEqual(
Object.keys(changes).sort(),
[firstItem.recordId, secondItem.recordId].sort(),
"Should include tagged bookmarks after updating tag folder"
);
await setChangesSynced(changes);
}
info("Change tag entry URL using Bookmarks.update");
{
let bm = await PlacesUtils.bookmarks.fetch({
parentGuid: tagFolderGuid,
index: 0,
});
bm.url = "https://bugzilla.org/";
await PlacesUtils.bookmarks.update(bm);
let changes = await PlacesSyncUtils.bookmarks.pullChanges();
deepEqual(
Object.keys(changes).sort(),
[firstItem.recordId, secondItem.recordId, untaggedItem.recordId].sort(),
"Should include tagged bookmarks after changing tag entry URI"
);
await assertTagForURLs(
"tricky",
["https://bugzilla.org/", "https://mozilla.org/"],
"Should remove tag entry for old URI"
);
await setChangesSynced(changes);
bm.url = "https://example.org/";
await PlacesUtils.bookmarks.update(bm);
changes = await PlacesSyncUtils.bookmarks.pullChanges();
deepEqual(
Object.keys(changes).sort(),
[firstItem.recordId, secondItem.recordId, untaggedItem.recordId].sort(),
"Should include tagged bookmarks after changing tag entry URL"
);
await assertTagForURLs(
"tricky",
["https://example.org/", "https://mozilla.org/"],
"Should remove tag entry for old URL"
);
await setChangesSynced(changes);
}
await PlacesUtils.bookmarks.eraseEverything();
await PlacesSyncUtils.bookmarks.reset();
});
add_task(async function test_update_keyword() {
info("Insert item without keyword");
let item = await PlacesSyncUtils.bookmarks.insert({
kind: "bookmark",
parentRecordId: "menu",
url: "https://mozilla.org",
recordId: makeGuid(),
});
info("Add item keyword");
{
let updatedItem = await PlacesSyncUtils.bookmarks.update({
recordId: item.recordId,
keyword: "moz",
});
equal(updatedItem.keyword, "moz", "Should return new keyword");
let entryByKeyword = await PlacesUtils.keywords.fetch("moz");
equal(
entryByKeyword.url.href,
"https://mozilla.org/",
"Should set new keyword for URL"
);
let entryByURL = await PlacesUtils.keywords.fetch({
url: "https://mozilla.org",
});
equal(
entryByURL.keyword,
"moz",
"Looking up URL should return new keyword"