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
|
/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
/* vim:set ts=2 sw=2 sts=2 et: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
var bmsvc = PlacesUtils.bookmarks;
var tagssvc = PlacesUtils.tagging;
var annosvc = PlacesUtils.annotations;
var txnManager = PlacesUtils.transactionManager;
const DESCRIPTION_ANNO = "bookmarkProperties/description";
function* promiseKeyword(keyword, href, postData) {
while (true) {
let entry = yield PlacesUtils.keywords.fetch(keyword);
if (href == null && !entry)
break;
if (entry && entry.url.href == href && entry.postData == postData) {
break;
}
yield new Promise(resolve => do_timeout(100, resolve));
}
}
// create and add bookmarks observer
var observer = {
onBeginUpdateBatch: function() {
this._beginUpdateBatch = true;
},
_beginUpdateBatch: false,
onEndUpdateBatch: function() {
this._endUpdateBatch = true;
},
_endUpdateBatch: false,
onItemAdded: function(id, folder, index, itemType, uri) {
this._itemAddedId = id;
this._itemAddedParent = folder;
this._itemAddedIndex = index;
this._itemAddedType = itemType;
},
_itemAddedId: null,
_itemAddedParent: null,
_itemAddedIndex: null,
_itemAddedType: null,
onItemRemoved: function(id, folder, index, itemType) {
this._itemRemovedId = id;
this._itemRemovedFolder = folder;
this._itemRemovedIndex = index;
},
_itemRemovedId: null,
_itemRemovedFolder: null,
_itemRemovedIndex: null,
onItemChanged: function(id, property, isAnnotationProperty, newValue,
lastModified, itemType) {
// The transaction manager is being rewritten in bug 891303, so just
// skip checking this for now.
if (property == "tags")
return;
this._itemChangedId = id;
this._itemChangedProperty = property;
this._itemChanged_isAnnotationProperty = isAnnotationProperty;
this._itemChangedValue = newValue;
},
_itemChangedId: null,
_itemChangedProperty: null,
_itemChanged_isAnnotationProperty: null,
_itemChangedValue: null,
onItemVisited: function(id, visitID, time) {
this._itemVisitedId = id;
this._itemVisitedVistId = visitID;
this._itemVisitedTime = time;
},
_itemVisitedId: null,
_itemVisitedVistId: null,
_itemVisitedTime: null,
onItemMoved: function(id, oldParent, oldIndex, newParent, newIndex,
itemType) {
this._itemMovedId = id;
this._itemMovedOldParent = oldParent;
this._itemMovedOldIndex = oldIndex;
this._itemMovedNewParent = newParent;
this._itemMovedNewIndex = newIndex;
},
_itemMovedId: null,
_itemMovedOldParent: null,
_itemMovedOldIndex: null,
_itemMovedNewParent: null,
_itemMovedNewIndex: null,
QueryInterface: function(iid) {
if (iid.equals(Ci.nsINavBookmarkObserver) ||
iid.equals(Ci.nsISupports)) {
return this;
}
throw Cr.NS_ERROR_NO_INTERFACE;
}
};
// index at which items should begin
var bmStartIndex = 0;
// get bookmarks root id
var root = PlacesUtils.bookmarksMenuFolderId;
add_task(function* init() {
bmsvc.addObserver(observer, false);
do_register_cleanup(function () {
bmsvc.removeObserver(observer);
});
});
add_task(function* test_create_folder_with_description() {
const TEST_FOLDERNAME = "Test creating a folder with a description";
const TEST_DESCRIPTION = "this is my test description";
let annos = [{ name: DESCRIPTION_ANNO,
type: annosvc.TYPE_STRING,
flags: 0,
value: TEST_DESCRIPTION,
expires: annosvc.EXPIRE_NEVER }];
let txn = new PlacesCreateFolderTransaction(TEST_FOLDERNAME, root, bmStartIndex, annos);
txnManager.doTransaction(txn);
// This checks that calling undoTransaction on an "empty batch" doesn't
// undo the previous transaction (getItemTitle will fail)
txnManager.beginBatch(null);
txnManager.endBatch(false);
txnManager.undoTransaction();
let folderId = observer._itemAddedId;
do_check_eq(bmsvc.getItemTitle(folderId), TEST_FOLDERNAME);
do_check_eq(observer._itemAddedIndex, bmStartIndex);
do_check_eq(observer._itemAddedParent, root);
do_check_eq(observer._itemAddedId, folderId);
do_check_eq(TEST_DESCRIPTION, annosvc.getItemAnnotation(folderId, DESCRIPTION_ANNO));
txn.undoTransaction();
do_check_eq(observer._itemRemovedId, folderId);
do_check_eq(observer._itemRemovedFolder, root);
do_check_eq(observer._itemRemovedIndex, bmStartIndex);
txn.redoTransaction();
do_check_eq(observer._itemAddedIndex, bmStartIndex);
do_check_eq(observer._itemAddedParent, root);
do_check_eq(observer._itemAddedId, folderId);
txn.undoTransaction();
do_check_eq(observer._itemRemovedId, folderId);
do_check_eq(observer._itemRemovedFolder, root);
do_check_eq(observer._itemRemovedIndex, bmStartIndex);
});
add_task(function* test_create_item() {
let testURI = NetUtil.newURI("http://test_create_item.com");
let txn = new PlacesCreateBookmarkTransaction(testURI, root, bmStartIndex,
"Test creating an item");
txnManager.doTransaction(txn);
let id = bmsvc.getBookmarkIdsForURI(testURI)[0];
do_check_eq(observer._itemAddedId, id);
do_check_eq(observer._itemAddedIndex, bmStartIndex);
do_check_true(bmsvc.isBookmarked(testURI));
txn.undoTransaction();
do_check_eq(observer._itemRemovedId, id);
do_check_eq(observer._itemRemovedIndex, bmStartIndex);
do_check_false(bmsvc.isBookmarked(testURI));
txn.redoTransaction();
do_check_true(bmsvc.isBookmarked(testURI));
let newId = bmsvc.getBookmarkIdsForURI(testURI)[0];
do_check_eq(observer._itemAddedIndex, bmStartIndex);
do_check_eq(observer._itemAddedParent, root);
do_check_eq(observer._itemAddedId, newId);
txn.undoTransaction();
do_check_eq(observer._itemRemovedId, newId);
do_check_eq(observer._itemRemovedFolder, root);
do_check_eq(observer._itemRemovedIndex, bmStartIndex);
});
add_task(function* test_create_item_to_folder() {
const TEST_FOLDERNAME = "Test creating item to a folder";
let testURI = NetUtil.newURI("http://test_create_item_to_folder.com");
let folderId = bmsvc.createFolder(root, TEST_FOLDERNAME, bmsvc.DEFAULT_INDEX);
let txn = new PlacesCreateBookmarkTransaction(testURI, folderId, bmStartIndex,
"Test creating item");
txnManager.doTransaction(txn);
let bkmId = bmsvc.getBookmarkIdsForURI(testURI)[0];
do_check_eq(observer._itemAddedId, bkmId);
do_check_eq(observer._itemAddedIndex, bmStartIndex);
do_check_true(bmsvc.isBookmarked(testURI));
txn.undoTransaction();
do_check_eq(observer._itemRemovedId, bkmId);
do_check_eq(observer._itemRemovedIndex, bmStartIndex);
txn.redoTransaction();
let newBkmId = bmsvc.getBookmarkIdsForURI(testURI)[0];
do_check_eq(observer._itemAddedIndex, bmStartIndex);
do_check_eq(observer._itemAddedParent, folderId);
do_check_eq(observer._itemAddedId, newBkmId);
txn.undoTransaction();
do_check_eq(observer._itemRemovedId, newBkmId);
do_check_eq(observer._itemRemovedFolder, folderId);
do_check_eq(observer._itemRemovedIndex, bmStartIndex);
});
add_task(function* test_move_items_to_folder() {
let testFolderId = bmsvc.createFolder(root, "Test move items", bmsvc.DEFAULT_INDEX);
let testURI = NetUtil.newURI("http://test_move_items.com");
let testBkmId = bmsvc.insertBookmark(testFolderId, testURI, bmsvc.DEFAULT_INDEX, "1: Test move items");
bmsvc.insertBookmark(testFolderId, testURI, bmsvc.DEFAULT_INDEX, "2: Test move items");
// Moving items between the same folder
let sameTxn = new PlacesMoveItemTransaction(testBkmId, testFolderId, bmsvc.DEFAULT_INDEX);
sameTxn.doTransaction();
do_check_eq(observer._itemMovedId, testBkmId);
do_check_eq(observer._itemMovedOldParent, testFolderId);
do_check_eq(observer._itemMovedOldIndex, 0);
do_check_eq(observer._itemMovedNewParent, testFolderId);
do_check_eq(observer._itemMovedNewIndex, 1);
sameTxn.undoTransaction();
do_check_eq(observer._itemMovedId, testBkmId);
do_check_eq(observer._itemMovedOldParent, testFolderId);
do_check_eq(observer._itemMovedOldIndex, 1);
do_check_eq(observer._itemMovedNewParent, testFolderId);
do_check_eq(observer._itemMovedNewIndex, 0);
sameTxn.redoTransaction();
do_check_eq(observer._itemMovedId, testBkmId);
do_check_eq(observer._itemMovedOldParent, testFolderId);
do_check_eq(observer._itemMovedOldIndex, 0);
do_check_eq(observer._itemMovedNewParent, testFolderId);
do_check_eq(observer._itemMovedNewIndex, 1);
sameTxn.undoTransaction();
do_check_eq(observer._itemMovedId, testBkmId);
do_check_eq(observer._itemMovedOldParent, testFolderId);
do_check_eq(observer._itemMovedOldIndex, 1);
do_check_eq(observer._itemMovedNewParent, testFolderId);
do_check_eq(observer._itemMovedNewIndex, 0);
// Moving items between different folders
let folderId = bmsvc.createFolder(testFolderId,
"Test move items between different folders",
bmsvc.DEFAULT_INDEX);
let diffTxn = new PlacesMoveItemTransaction(testBkmId, folderId, bmsvc.DEFAULT_INDEX);
diffTxn.doTransaction();
do_check_eq(observer._itemMovedId, testBkmId);
do_check_eq(observer._itemMovedOldParent, testFolderId);
do_check_eq(observer._itemMovedOldIndex, 0);
do_check_eq(observer._itemMovedNewParent, folderId);
do_check_eq(observer._itemMovedNewIndex, 0);
sameTxn.undoTransaction();
do_check_eq(observer._itemMovedId, testBkmId);
do_check_eq(observer._itemMovedOldParent, folderId);
do_check_eq(observer._itemMovedOldIndex, 0);
do_check_eq(observer._itemMovedNewParent, testFolderId);
do_check_eq(observer._itemMovedNewIndex, 0);
diffTxn.redoTransaction();
do_check_eq(observer._itemMovedId, testBkmId);
do_check_eq(observer._itemMovedOldParent, testFolderId);
do_check_eq(observer._itemMovedOldIndex, 0);
do_check_eq(observer._itemMovedNewParent, folderId);
do_check_eq(observer._itemMovedNewIndex, 0);
sameTxn.undoTransaction();
do_check_eq(observer._itemMovedId, testBkmId);
do_check_eq(observer._itemMovedOldParent, folderId);
do_check_eq(observer._itemMovedOldIndex, 0);
do_check_eq(observer._itemMovedNewParent, testFolderId);
do_check_eq(observer._itemMovedNewIndex, 0);
});
add_task(function* test_remove_folder() {
let testFolder = bmsvc.createFolder(root, "Test Removing a Folder", bmsvc.DEFAULT_INDEX);
let folderId = bmsvc.createFolder(testFolder, "Removed Folder", bmsvc.DEFAULT_INDEX);
let txn = new PlacesRemoveItemTransaction(folderId);
txn.doTransaction();
do_check_eq(observer._itemRemovedId, folderId);
do_check_eq(observer._itemRemovedFolder, testFolder);
do_check_eq(observer._itemRemovedIndex, 0);
txn.undoTransaction();
do_check_eq(observer._itemAddedId, folderId);
do_check_eq(observer._itemAddedParent, testFolder);
do_check_eq(observer._itemAddedIndex, 0);
txn.redoTransaction();
do_check_eq(observer._itemRemovedId, folderId);
do_check_eq(observer._itemRemovedFolder, testFolder);
do_check_eq(observer._itemRemovedIndex, 0);
txn.undoTransaction();
do_check_eq(observer._itemAddedId, folderId);
do_check_eq(observer._itemAddedParent, testFolder);
do_check_eq(observer._itemAddedIndex, 0);
});
add_task(function* test_remove_item_with_tag() {
// Notice in this case the tag persists since other bookmarks have same uri.
let testFolder = bmsvc.createFolder(root, "Test removing an item with a tag",
bmsvc.DEFAULT_INDEX);
const TAG_NAME = "tag-test_remove_item_with_tag";
let testURI = NetUtil.newURI("http://test_remove_item_with_tag.com");
let testBkmId = bmsvc.insertBookmark(testFolder, testURI, bmsvc.DEFAULT_INDEX, "test-item1");
// create bookmark for not removing tag.
bmsvc.insertBookmark(testFolder, testURI, bmsvc.DEFAULT_INDEX, "test-item2");
// set tag
tagssvc.tagURI(testURI, [TAG_NAME]);
let txn = new PlacesRemoveItemTransaction(testBkmId);
txn.doTransaction();
do_check_eq(observer._itemRemovedId, testBkmId);
do_check_eq(observer._itemRemovedFolder, testFolder);
do_check_eq(observer._itemRemovedIndex, 0);
do_check_eq(tagssvc.getTagsForURI(testURI), TAG_NAME);
txn.undoTransaction();
let newbkmk2Id = observer._itemAddedId;
do_check_eq(observer._itemAddedParent, testFolder);
do_check_eq(observer._itemAddedIndex, 0);
do_check_eq(tagssvc.getTagsForURI(testURI)[0], TAG_NAME);
txn.redoTransaction();
do_check_eq(observer._itemRemovedId, newbkmk2Id);
do_check_eq(observer._itemRemovedFolder, testFolder);
do_check_eq(observer._itemRemovedIndex, 0);
do_check_eq(tagssvc.getTagsForURI(testURI)[0], TAG_NAME);
txn.undoTransaction();
do_check_eq(observer._itemAddedParent, testFolder);
do_check_eq(observer._itemAddedIndex, 0);
do_check_eq(tagssvc.getTagsForURI(testURI)[0], TAG_NAME);
});
add_task(function* test_remove_item_with_keyword() {
// Notice in this case the tag persists since other bookmarks have same uri.
let testFolder = bmsvc.createFolder(root, "Test removing an item with a keyword",
bmsvc.DEFAULT_INDEX);
const KEYWORD = "test: test removing an item with a keyword";
let testURI = NetUtil.newURI("http://test_remove_item_with_keyword.com");
let testBkmId = bmsvc.insertBookmark(testFolder, testURI, bmsvc.DEFAULT_INDEX, "test-item1");
// set keyword
yield PlacesUtils.keywords.insert({ url: testURI.spec, keyword: KEYWORD});
let txn = new PlacesRemoveItemTransaction(testBkmId);
txn.doTransaction();
do_check_eq(observer._itemRemovedId, testBkmId);
do_check_eq(observer._itemRemovedFolder, testFolder);
do_check_eq(observer._itemRemovedIndex, 0);
yield promiseKeyword(KEYWORD, null);
txn.undoTransaction();
let newbkmk2Id = observer._itemAddedId;
do_check_eq(observer._itemAddedParent, testFolder);
do_check_eq(observer._itemAddedIndex, 0);
yield promiseKeyword(KEYWORD, testURI.spec);
txn.redoTransaction();
do_check_eq(observer._itemRemovedId, newbkmk2Id);
do_check_eq(observer._itemRemovedFolder, testFolder);
do_check_eq(observer._itemRemovedIndex, 0);
yield promiseKeyword(KEYWORD, null);
txn.undoTransaction();
do_check_eq(observer._itemAddedParent, testFolder);
do_check_eq(observer._itemAddedIndex, 0);
});
add_task(function* test_creating_separator() {
let testFolder = bmsvc.createFolder(root, "Test creating a separator", bmsvc.DEFAULT_INDEX);
let txn = new PlacesCreateSeparatorTransaction(testFolder, 0);
txn.doTransaction();
let sepId = observer._itemAddedId;
do_check_eq(observer._itemAddedIndex, 0);
do_check_eq(observer._itemAddedParent, testFolder);
txn.undoTransaction();
do_check_eq(observer._itemRemovedId, sepId);
do_check_eq(observer._itemRemovedFolder, testFolder);
do_check_eq(observer._itemRemovedIndex, 0);
txn.redoTransaction();
let newSepId = observer._itemAddedId;
do_check_eq(observer._itemAddedIndex, 0);
do_check_eq(observer._itemAddedParent, testFolder);
txn.undoTransaction();
do_check_eq(observer._itemRemovedId, newSepId);
do_check_eq(observer._itemRemovedFolder, testFolder);
do_check_eq(observer._itemRemovedIndex, 0);
});
add_task(function* test_removing_separator() {
let testFolder = bmsvc.createFolder(root, "Test removing a separator", bmsvc.DEFAULT_INDEX);
let sepId = bmsvc.insertSeparator(testFolder, 0);
let txn = new PlacesRemoveItemTransaction(sepId);
txn.doTransaction();
do_check_eq(observer._itemRemovedId, sepId);
do_check_eq(observer._itemRemovedFolder, testFolder);
do_check_eq(observer._itemRemovedIndex, 0);
txn.undoTransaction();
do_check_eq(observer._itemAddedId, sepId); // New separator created
do_check_eq(observer._itemAddedParent, testFolder);
do_check_eq(observer._itemAddedIndex, 0);
txn.redoTransaction();
do_check_eq(observer._itemRemovedId, sepId);
do_check_eq(observer._itemRemovedFolder, testFolder);
do_check_eq(observer._itemRemovedIndex, 0);
txn.undoTransaction();
do_check_eq(observer._itemAddedId, sepId); // New separator created
do_check_eq(observer._itemAddedParent, testFolder);
do_check_eq(observer._itemAddedIndex, 0);
});
add_task(function* test_editing_item_title() {
const TITLE = "Test editing item title";
const MOD_TITLE = "Mod: Test editing item title";
let testURI = NetUtil.newURI("http://www.test_editing_item_title.com");
let testBkmId = bmsvc.insertBookmark(root, testURI, bmsvc.DEFAULT_INDEX, TITLE);
let txn = new PlacesEditItemTitleTransaction(testBkmId, MOD_TITLE);
txn.doTransaction();
do_check_eq(observer._itemChangedId, testBkmId);
do_check_eq(observer._itemChangedProperty, "title");
do_check_eq(observer._itemChangedValue, MOD_TITLE);
txn.undoTransaction();
do_check_eq(observer._itemChangedId, testBkmId);
do_check_eq(observer._itemChangedProperty, "title");
do_check_eq(observer._itemChangedValue, TITLE);
txn.redoTransaction();
do_check_eq(observer._itemChangedId, testBkmId);
do_check_eq(observer._itemChangedProperty, "title");
do_check_eq(observer._itemChangedValue, MOD_TITLE);
txn.undoTransaction();
do_check_eq(observer._itemChangedId, testBkmId);
do_check_eq(observer._itemChangedProperty, "title");
do_check_eq(observer._itemChangedValue, TITLE);
});
add_task(function* test_editing_item_uri() {
const OLD_TEST_URI = NetUtil.newURI("http://old.test_editing_item_uri.com/");
const NEW_TEST_URI = NetUtil.newURI("http://new.test_editing_item_uri.com/");
let testBkmId = bmsvc.insertBookmark(root, OLD_TEST_URI, bmsvc.DEFAULT_INDEX,
"Test editing item title");
tagssvc.tagURI(OLD_TEST_URI, ["tag"]);
let txn = new PlacesEditBookmarkURITransaction(testBkmId, NEW_TEST_URI);
txn.doTransaction();
do_check_eq(observer._itemChangedId, testBkmId);
do_check_eq(observer._itemChangedProperty, "uri");
do_check_eq(observer._itemChangedValue, NEW_TEST_URI.spec);
do_check_eq(JSON.stringify(tagssvc.getTagsForURI(NEW_TEST_URI)), JSON.stringify(["tag"]));
do_check_eq(JSON.stringify(tagssvc.getTagsForURI(OLD_TEST_URI)), JSON.stringify([]));
txn.undoTransaction();
do_check_eq(observer._itemChangedId, testBkmId);
do_check_eq(observer._itemChangedProperty, "uri");
do_check_eq(observer._itemChangedValue, OLD_TEST_URI.spec);
do_check_eq(JSON.stringify(tagssvc.getTagsForURI(OLD_TEST_URI)), JSON.stringify(["tag"]));
do_check_eq(JSON.stringify(tagssvc.getTagsForURI(NEW_TEST_URI)), JSON.stringify([]));
txn.redoTransaction();
do_check_eq(observer._itemChangedId, testBkmId);
do_check_eq(observer._itemChangedProperty, "uri");
do_check_eq(observer._itemChangedValue, NEW_TEST_URI.spec);
do_check_eq(JSON.stringify(tagssvc.getTagsForURI(NEW_TEST_URI)), JSON.stringify(["tag"]));
do_check_eq(JSON.stringify(tagssvc.getTagsForURI(OLD_TEST_URI)), JSON.stringify([]));
txn.undoTransaction();
do_check_eq(observer._itemChangedId, testBkmId);
do_check_eq(observer._itemChangedProperty, "uri");
do_check_eq(observer._itemChangedValue, OLD_TEST_URI.spec);
do_check_eq(JSON.stringify(tagssvc.getTagsForURI(OLD_TEST_URI)), JSON.stringify(["tag"]));
do_check_eq(JSON.stringify(tagssvc.getTagsForURI(NEW_TEST_URI)), JSON.stringify([]));
});
add_task(function* test_edit_description_transaction() {
let testURI = NetUtil.newURI("http://test_edit_description_transaction.com");
let testBkmId = bmsvc.insertBookmark(root, testURI, bmsvc.DEFAULT_INDEX, "Test edit description transaction");
let anno = {
name: DESCRIPTION_ANNO,
type: Ci.nsIAnnotationService.TYPE_STRING,
flags: 0,
value: "Test edit Description",
expires: Ci.nsIAnnotationService.EXPIRE_NEVER,
};
let txn = new PlacesSetItemAnnotationTransaction(testBkmId, anno);
txn.doTransaction();
do_check_eq(observer._itemChangedId, testBkmId);
do_check_eq(observer._itemChangedProperty, DESCRIPTION_ANNO);
});
add_task(function* test_edit_keyword() {
const KEYWORD = "keyword-test_edit_keyword";
let testURI = NetUtil.newURI("http://test_edit_keyword.com");
let testBkmId = bmsvc.insertBookmark(root, testURI, bmsvc.DEFAULT_INDEX, "Test edit keyword");
let txn = new PlacesEditBookmarkKeywordTransaction(testBkmId, KEYWORD, "postData");
txn.doTransaction();
do_check_eq(observer._itemChangedId, testBkmId);
do_check_eq(observer._itemChangedProperty, "keyword");
do_check_eq(observer._itemChangedValue, KEYWORD);
do_check_eq(PlacesUtils.getPostDataForBookmark(testBkmId), "postData");
txn.undoTransaction();
do_check_eq(observer._itemChangedId, testBkmId);
do_check_eq(observer._itemChangedProperty, "keyword");
do_check_eq(observer._itemChangedValue, "");
do_check_eq(PlacesUtils.getPostDataForBookmark(testBkmId), null);
});
add_task(function* test_edit_specific_keyword() {
const KEYWORD = "keyword-test_edit_keyword2";
let testURI = NetUtil.newURI("http://test_edit_keyword2.com");
let testBkmId = bmsvc.insertBookmark(root, testURI, bmsvc.DEFAULT_INDEX, "Test edit keyword");
// Add multiple keyword to this uri.
yield PlacesUtils.keywords.insert({ keyword: "kw1", url: testURI.spec, postData: "postData1" });
yield PlacesUtils.keywords.insert({keyword: "kw2", url: testURI.spec, postData: "postData2" });
// Try to change only kw2.
let txn = new PlacesEditBookmarkKeywordTransaction(testBkmId, KEYWORD, "postData2", "kw2");
txn.doTransaction();
do_check_eq(observer._itemChangedId, testBkmId);
do_check_eq(observer._itemChangedProperty, "keyword");
do_check_eq(observer._itemChangedValue, KEYWORD);
let entry = yield PlacesUtils.keywords.fetch("kw1");
Assert.equal(entry.url.href, testURI.spec);
Assert.equal(entry.postData, "postData1");
yield promiseKeyword(KEYWORD, testURI.spec, "postData2");
yield promiseKeyword("kw2", null);
txn.undoTransaction();
do_check_eq(observer._itemChangedId, testBkmId);
do_check_eq(observer._itemChangedProperty, "keyword");
do_check_eq(observer._itemChangedValue, "kw2");
do_check_eq(PlacesUtils.getPostDataForBookmark(testBkmId), "postData1");
entry = yield PlacesUtils.keywords.fetch("kw1");
Assert.equal(entry.url.href, testURI.spec);
Assert.equal(entry.postData, "postData1");
yield promiseKeyword("kw2", testURI.spec, "postData2");
yield promiseKeyword("keyword", null);
});
add_task(function* test_LoadInSidebar_transaction() {
let testURI = NetUtil.newURI("http://test_LoadInSidebar_transaction.com");
let testBkmId = bmsvc.insertBookmark(root, testURI, bmsvc.DEFAULT_INDEX, "Test LoadInSidebar transaction");
const LOAD_IN_SIDEBAR_ANNO = "bookmarkProperties/loadInSidebar";
let anno = { name: LOAD_IN_SIDEBAR_ANNO,
type: Ci.nsIAnnotationService.TYPE_INT32,
flags: 0,
value: true,
expires: Ci.nsIAnnotationService.EXPIRE_NEVER };
let txn = new PlacesSetItemAnnotationTransaction(testBkmId, anno);
txn.doTransaction();
do_check_eq(observer._itemChangedId, testBkmId);
do_check_eq(observer._itemChangedProperty, LOAD_IN_SIDEBAR_ANNO);
do_check_eq(observer._itemChanged_isAnnotationProperty, true);
txn.undoTransaction();
do_check_eq(observer._itemChangedId, testBkmId);
do_check_eq(observer._itemChangedProperty, LOAD_IN_SIDEBAR_ANNO);
do_check_eq(observer._itemChanged_isAnnotationProperty, true);
});
add_task(function* test_generic_item_annotation() {
let testURI = NetUtil.newURI("http://test_generic_item_annotation.com");
let testBkmId = bmsvc.insertBookmark(root, testURI, bmsvc.DEFAULT_INDEX, "Test generic item annotation");
let itemAnnoObj = { name: "testAnno/testInt",
type: Ci.nsIAnnotationService.TYPE_INT32,
flags: 0,
value: 123,
expires: Ci.nsIAnnotationService.EXPIRE_NEVER };
let txn = new PlacesSetItemAnnotationTransaction(testBkmId, itemAnnoObj);
txn.doTransaction();
do_check_eq(observer._itemChangedId, testBkmId);
do_check_eq(observer._itemChangedProperty, "testAnno/testInt");
do_check_eq(observer._itemChanged_isAnnotationProperty, true);
txn.undoTransaction();
do_check_eq(observer._itemChangedId, testBkmId);
do_check_eq(observer._itemChangedProperty, "testAnno/testInt");
do_check_eq(observer._itemChanged_isAnnotationProperty, true);
txn.redoTransaction();
do_check_eq(observer._itemChangedId, testBkmId);
do_check_eq(observer._itemChangedProperty, "testAnno/testInt");
do_check_eq(observer._itemChanged_isAnnotationProperty, true);
});
add_task(function* test_editing_item_date_added() {
let testURI = NetUtil.newURI("http://test_editing_item_date_added.com");
let testBkmId = bmsvc.insertBookmark(root, testURI, bmsvc.DEFAULT_INDEX,
"Test editing item date added");
let oldAdded = bmsvc.getItemDateAdded(testBkmId);
let newAdded = Date.now() * 1000 + 1000;
let txn = new PlacesEditItemDateAddedTransaction(testBkmId, newAdded);
txn.doTransaction();
do_check_eq(newAdded, bmsvc.getItemDateAdded(testBkmId));
txn.undoTransaction();
do_check_eq(oldAdded, bmsvc.getItemDateAdded(testBkmId));
});
add_task(function* test_edit_item_last_modified() {
let testURI = NetUtil.newURI("http://test_edit_item_last_modified.com");
let testBkmId = bmsvc.insertBookmark(root, testURI, bmsvc.DEFAULT_INDEX,
"Test editing item last modified");
let oldModified = bmsvc.getItemLastModified(testBkmId);
let newModified = Date.now() * 1000 + 1000;
let txn = new PlacesEditItemLastModifiedTransaction(testBkmId, newModified);
txn.doTransaction();
do_check_eq(newModified, bmsvc.getItemLastModified(testBkmId));
txn.undoTransaction();
do_check_eq(oldModified, bmsvc.getItemLastModified(testBkmId));
});
add_task(function* test_generic_page_annotation() {
const TEST_ANNO = "testAnno/testInt";
let testURI = NetUtil.newURI("http://www.mozilla.org/");
PlacesTestUtils.addVisits(testURI).then(function () {
let pageAnnoObj = { name: TEST_ANNO,
type: Ci.nsIAnnotationService.TYPE_INT32,
flags: 0,
value: 123,
expires: Ci.nsIAnnotationService.EXPIRE_NEVER };
let txn = new PlacesSetPageAnnotationTransaction(testURI, pageAnnoObj);
txn.doTransaction();
do_check_true(annosvc.pageHasAnnotation(testURI, TEST_ANNO));
txn.undoTransaction();
do_check_false(annosvc.pageHasAnnotation(testURI, TEST_ANNO));
txn.redoTransaction();
do_check_true(annosvc.pageHasAnnotation(testURI, TEST_ANNO));
});
});
add_task(function* test_sort_folder_by_name() {
let testFolder = bmsvc.createFolder(root, "Test PlacesSortFolderByNameTransaction",
bmsvc.DEFAULT_INDEX);
let testURI = NetUtil.newURI("http://test_sort_folder_by_name.com");
bmsvc.insertBookmark(testFolder, testURI, bmsvc.DEFAULT_INDEX, "bookmark3");
bmsvc.insertBookmark(testFolder, testURI, bmsvc.DEFAULT_INDEX, "bookmark2");
bmsvc.insertBookmark(testFolder, testURI, bmsvc.DEFAULT_INDEX, "bookmark1");
let bkmIds = bmsvc.getBookmarkIdsForURI(testURI);
bkmIds.sort();
let b1 = bkmIds[0];
let b2 = bkmIds[1];
let b3 = bkmIds[2];
do_check_eq(0, bmsvc.getItemIndex(b1));
do_check_eq(1, bmsvc.getItemIndex(b2));
do_check_eq(2, bmsvc.getItemIndex(b3));
let txn = new PlacesSortFolderByNameTransaction(testFolder);
txn.doTransaction();
do_check_eq(2, bmsvc.getItemIndex(b1));
do_check_eq(1, bmsvc.getItemIndex(b2));
do_check_eq(0, bmsvc.getItemIndex(b3));
txn.undoTransaction();
do_check_eq(0, bmsvc.getItemIndex(b1));
do_check_eq(1, bmsvc.getItemIndex(b2));
do_check_eq(2, bmsvc.getItemIndex(b3));
txn.redoTransaction();
do_check_eq(2, bmsvc.getItemIndex(b1));
do_check_eq(1, bmsvc.getItemIndex(b2));
do_check_eq(0, bmsvc.getItemIndex(b3));
txn.undoTransaction();
do_check_eq(0, bmsvc.getItemIndex(b1));
do_check_eq(1, bmsvc.getItemIndex(b2));
do_check_eq(2, bmsvc.getItemIndex(b3));
});
add_task(function* test_tagURI_untagURI() {
const TAG_1 = "tag-test_tagURI_untagURI-bar";
const TAG_2 = "tag-test_tagURI_untagURI-foo";
let tagURI = NetUtil.newURI("http://test_tagURI_untagURI.com");
// Test tagURI
let tagTxn = new PlacesTagURITransaction(tagURI, [TAG_1, TAG_2]);
tagTxn.doTransaction();
do_check_eq(JSON.stringify(tagssvc.getTagsForURI(tagURI)), JSON.stringify([TAG_1, TAG_2]));
tagTxn.undoTransaction();
do_check_eq(tagssvc.getTagsForURI(tagURI).length, 0);
tagTxn.redoTransaction();
do_check_eq(JSON.stringify(tagssvc.getTagsForURI(tagURI)), JSON.stringify([TAG_1, TAG_2]));
// Test untagURI
let untagTxn = new PlacesUntagURITransaction(tagURI, [TAG_1]);
untagTxn.doTransaction();
do_check_eq(JSON.stringify(tagssvc.getTagsForURI(tagURI)), JSON.stringify([TAG_2]));
untagTxn.undoTransaction();
do_check_eq(JSON.stringify(tagssvc.getTagsForURI(tagURI)), JSON.stringify([TAG_1, TAG_2]));
untagTxn.redoTransaction();
do_check_eq(JSON.stringify(tagssvc.getTagsForURI(tagURI)), JSON.stringify([TAG_2]));
});
add_task(function* test_aggregate_removeItem_Txn() {
let testFolder = bmsvc.createFolder(root, "Test aggregate removeItem transaction", bmsvc.DEFAULT_INDEX);
const TEST_URL = "http://test_aggregate_removeitem_txn.com/";
const FOLDERNAME = "Folder";
let testURI = NetUtil.newURI(TEST_URL);
let bkmk1Id = bmsvc.insertBookmark(testFolder, testURI, 0, "Mozilla");
let bkmk2Id = bmsvc.insertSeparator(testFolder, 1);
let bkmk3Id = bmsvc.createFolder(testFolder, FOLDERNAME, 2);
let bkmk3_1Id = bmsvc.insertBookmark(bkmk3Id, testURI, 0, "Mozilla");
let bkmk3_2Id = bmsvc.insertSeparator(bkmk3Id, 1);
let bkmk3_3Id = bmsvc.createFolder(bkmk3Id, FOLDERNAME, 2);
let childTxn1 = new PlacesRemoveItemTransaction(bkmk1Id);
let childTxn2 = new PlacesRemoveItemTransaction(bkmk2Id);
let childTxn3 = new PlacesRemoveItemTransaction(bkmk3Id);
let transactions = [childTxn1, childTxn2, childTxn3];
let txn = new PlacesAggregatedTransaction("RemoveItems", transactions);
txn.doTransaction();
do_check_eq(bmsvc.getItemIndex(bkmk1Id), -1);
do_check_eq(bmsvc.getItemIndex(bkmk2Id), -1);
do_check_eq(bmsvc.getItemIndex(bkmk3Id), -1);
do_check_eq(bmsvc.getItemIndex(bkmk3_1Id), -1);
do_check_eq(bmsvc.getItemIndex(bkmk3_2Id), -1);
do_check_eq(bmsvc.getItemIndex(bkmk3_3Id), -1);
// Check last removed item id.
do_check_eq(observer._itemRemovedId, bkmk3Id);
txn.undoTransaction();
let newBkmk1Id = bmsvc.getIdForItemAt(testFolder, 0);
let newBkmk2Id = bmsvc.getIdForItemAt(testFolder, 1);
let newBkmk3Id = bmsvc.getIdForItemAt(testFolder, 2);
let newBkmk3_1Id = bmsvc.getIdForItemAt(newBkmk3Id, 0);
let newBkmk3_2Id = bmsvc.getIdForItemAt(newBkmk3Id, 1);
let newBkmk3_3Id = bmsvc.getIdForItemAt(newBkmk3Id, 2);
do_check_eq(bmsvc.getItemType(newBkmk1Id), bmsvc.TYPE_BOOKMARK);
do_check_eq(bmsvc.getBookmarkURI(newBkmk1Id).spec, TEST_URL);
do_check_eq(bmsvc.getItemType(newBkmk2Id), bmsvc.TYPE_SEPARATOR);
do_check_eq(bmsvc.getItemType(newBkmk3Id), bmsvc.TYPE_FOLDER);
do_check_eq(bmsvc.getItemTitle(newBkmk3Id), FOLDERNAME);
do_check_eq(bmsvc.getFolderIdForItem(newBkmk3_1Id), newBkmk3Id);
do_check_eq(bmsvc.getItemType(newBkmk3_1Id), bmsvc.TYPE_BOOKMARK);
do_check_eq(bmsvc.getBookmarkURI(newBkmk3_1Id).spec, TEST_URL);
do_check_eq(bmsvc.getFolderIdForItem(newBkmk3_2Id), newBkmk3Id);
do_check_eq(bmsvc.getItemType(newBkmk3_2Id), bmsvc.TYPE_SEPARATOR);
do_check_eq(bmsvc.getFolderIdForItem(newBkmk3_3Id), newBkmk3Id);
do_check_eq(bmsvc.getItemType(newBkmk3_3Id), bmsvc.TYPE_FOLDER);
do_check_eq(bmsvc.getItemTitle(newBkmk3_3Id), FOLDERNAME);
// Check last added back item id.
// Notice items are restored in reverse order.
do_check_eq(observer._itemAddedId, newBkmk1Id);
txn.redoTransaction();
do_check_eq(bmsvc.getItemIndex(newBkmk1Id), -1);
do_check_eq(bmsvc.getItemIndex(newBkmk2Id), -1);
do_check_eq(bmsvc.getItemIndex(newBkmk3Id), -1);
do_check_eq(bmsvc.getItemIndex(newBkmk3_1Id), -1);
do_check_eq(bmsvc.getItemIndex(newBkmk3_2Id), -1);
do_check_eq(bmsvc.getItemIndex(newBkmk3_3Id), -1);
// Check last removed item id.
do_check_eq(observer._itemRemovedId, newBkmk3Id);
txn.undoTransaction();
newBkmk1Id = bmsvc.getIdForItemAt(testFolder, 0);
newBkmk2Id = bmsvc.getIdForItemAt(testFolder, 1);
newBkmk3Id = bmsvc.getIdForItemAt(testFolder, 2);
newBkmk3_1Id = bmsvc.getIdForItemAt(newBkmk3Id, 0);
newBkmk3_2Id = bmsvc.getIdForItemAt(newBkmk3Id, 1);
newBkmk3_3Id = bmsvc.getIdForItemAt(newBkmk3Id, 2);
do_check_eq(bmsvc.getItemType(newBkmk1Id), bmsvc.TYPE_BOOKMARK);
do_check_eq(bmsvc.getBookmarkURI(newBkmk1Id).spec, TEST_URL);
do_check_eq(bmsvc.getItemType(newBkmk2Id), bmsvc.TYPE_SEPARATOR);
do_check_eq(bmsvc.getItemType(newBkmk3Id), bmsvc.TYPE_FOLDER);
do_check_eq(bmsvc.getItemTitle(newBkmk3Id), FOLDERNAME);
do_check_eq(bmsvc.getFolderIdForItem(newBkmk3_1Id), newBkmk3Id);
do_check_eq(bmsvc.getItemType(newBkmk3_1Id), bmsvc.TYPE_BOOKMARK);
do_check_eq(bmsvc.getBookmarkURI(newBkmk3_1Id).spec, TEST_URL);
do_check_eq(bmsvc.getFolderIdForItem(newBkmk3_2Id), newBkmk3Id);
do_check_eq(bmsvc.getItemType(newBkmk3_2Id), bmsvc.TYPE_SEPARATOR);
do_check_eq(bmsvc.getFolderIdForItem(newBkmk3_3Id), newBkmk3Id);
do_check_eq(bmsvc.getItemType(newBkmk3_3Id), bmsvc.TYPE_FOLDER);
do_check_eq(bmsvc.getItemTitle(newBkmk3_3Id), FOLDERNAME);
// Check last added back item id.
// Notice items are restored in reverse order.
do_check_eq(observer._itemAddedId, newBkmk1Id);
});
add_task(function* test_create_item_with_childTxn() {
let testFolder = bmsvc.createFolder(root, "Test creating an item with childTxns", bmsvc.DEFAULT_INDEX);
const BOOKMARK_TITLE = "parent item";
let testURI = NetUtil.newURI("http://test_create_item_with_childTxn.com");
let childTxns = [];
let newDateAdded = Date.now() * 1000 - 20000;
let editDateAdddedTxn = new PlacesEditItemDateAddedTransaction(null, newDateAdded);
childTxns.push(editDateAdddedTxn);
let itemChildAnnoObj = { name: "testAnno/testInt",
type: Ci.nsIAnnotationService.TYPE_INT32,
flags: 0,
value: 123,
expires: Ci.nsIAnnotationService.EXPIRE_NEVER };
let annoTxn = new PlacesSetItemAnnotationTransaction(null, itemChildAnnoObj);
childTxns.push(annoTxn);
let itemWChildTxn = new PlacesCreateBookmarkTransaction(testURI, testFolder, bmStartIndex,
BOOKMARK_TITLE, null, null,
childTxns);
try {
txnManager.doTransaction(itemWChildTxn);
let itemId = bmsvc.getBookmarkIdsForURI(testURI)[0];
do_check_eq(observer._itemAddedId, itemId);
do_check_eq(newDateAdded, bmsvc.getItemDateAdded(itemId));
do_check_eq(observer._itemChangedProperty, "testAnno/testInt");
do_check_eq(observer._itemChanged_isAnnotationProperty, true);
do_check_true(annosvc.itemHasAnnotation(itemId, itemChildAnnoObj.name))
do_check_eq(annosvc.getItemAnnotation(itemId, itemChildAnnoObj.name), itemChildAnnoObj.value);
itemWChildTxn.undoTransaction();
do_check_eq(observer._itemRemovedId, itemId);
itemWChildTxn.redoTransaction();
do_check_true(bmsvc.isBookmarked(testURI));
let newId = bmsvc.getBookmarkIdsForURI(testURI)[0];
do_check_eq(newDateAdded, bmsvc.getItemDateAdded(newId));
do_check_eq(observer._itemAddedId, newId);
do_check_eq(observer._itemChangedProperty, "testAnno/testInt");
do_check_eq(observer._itemChanged_isAnnotationProperty, true);
do_check_true(annosvc.itemHasAnnotation(newId, itemChildAnnoObj.name))
do_check_eq(annosvc.getItemAnnotation(newId, itemChildAnnoObj.name), itemChildAnnoObj.value);
itemWChildTxn.undoTransaction();
do_check_eq(observer._itemRemovedId, newId);
}
catch (ex) {
do_throw("Setting a child transaction in a createItem transaction did throw: " + ex);
}
});
add_task(function* test_create_folder_with_child_itemTxn() {
let childURI = NetUtil.newURI("http://test_create_folder_with_child_itemTxn.com");
let childItemTxn = new PlacesCreateBookmarkTransaction(childURI, root,
bmStartIndex, "childItem");
let txn = new PlacesCreateFolderTransaction("Test creating a folder with child itemTxns",
root, bmStartIndex, null, [childItemTxn]);
try {
txnManager.doTransaction(txn);
let childItemId = bmsvc.getBookmarkIdsForURI(childURI)[0];
do_check_eq(observer._itemAddedId, childItemId);
do_check_eq(observer._itemAddedIndex, 0);
do_check_true(bmsvc.isBookmarked(childURI));
txn.undoTransaction();
do_check_false(bmsvc.isBookmarked(childURI));
txn.redoTransaction();
let newchildItemId = bmsvc.getBookmarkIdsForURI(childURI)[0];
do_check_eq(observer._itemAddedIndex, 0);
do_check_eq(observer._itemAddedId, newchildItemId);
do_check_true(bmsvc.isBookmarked(childURI));
txn.undoTransaction();
do_check_false(bmsvc.isBookmarked(childURI));
}
catch (ex) {
do_throw("Setting a child item transaction in a createFolder transaction did throw: " + ex);
}
});
|