toolkit/components/places/tests/unit/test_placesTxn.js

changeset 0
6474c204b198
equal deleted inserted replaced
-1:000000000000 0:adb992b413db
1 /* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim:set ts=2 sw=2 sts=2 et: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7 let bmsvc = PlacesUtils.bookmarks;
8 let tagssvc = PlacesUtils.tagging;
9 let annosvc = PlacesUtils.annotations;
10 let txnManager = PlacesUtils.transactionManager;
11 const DESCRIPTION_ANNO = "bookmarkProperties/description";
12
13 // create and add bookmarks observer
14 let observer = {
15
16 onBeginUpdateBatch: function() {
17 this._beginUpdateBatch = true;
18 },
19 _beginUpdateBatch: false,
20
21 onEndUpdateBatch: function() {
22 this._endUpdateBatch = true;
23 },
24 _endUpdateBatch: false,
25
26 onItemAdded: function(id, folder, index, itemType, uri) {
27 this._itemAddedId = id;
28 this._itemAddedParent = folder;
29 this._itemAddedIndex = index;
30 this._itemAddedType = itemType;
31 },
32 _itemAddedId: null,
33 _itemAddedParent: null,
34 _itemAddedIndex: null,
35 _itemAddedType: null,
36
37 onItemRemoved: function(id, folder, index, itemType) {
38 this._itemRemovedId = id;
39 this._itemRemovedFolder = folder;
40 this._itemRemovedIndex = index;
41 },
42 _itemRemovedId: null,
43 _itemRemovedFolder: null,
44 _itemRemovedIndex: null,
45
46 onItemChanged: function(id, property, isAnnotationProperty, newValue,
47 lastModified, itemType) {
48 // The transaction manager is being rewritten in bug 891303, so just
49 // skip checking this for now.
50 if (property == "tags")
51 return;
52 this._itemChangedId = id;
53 this._itemChangedProperty = property;
54 this._itemChanged_isAnnotationProperty = isAnnotationProperty;
55 this._itemChangedValue = newValue;
56 },
57 _itemChangedId: null,
58 _itemChangedProperty: null,
59 _itemChanged_isAnnotationProperty: null,
60 _itemChangedValue: null,
61
62 onItemVisited: function(id, visitID, time) {
63 this._itemVisitedId = id;
64 this._itemVisitedVistId = visitID;
65 this._itemVisitedTime = time;
66 },
67 _itemVisitedId: null,
68 _itemVisitedVistId: null,
69 _itemVisitedTime: null,
70
71 onItemMoved: function(id, oldParent, oldIndex, newParent, newIndex,
72 itemType) {
73 this._itemMovedId = id;
74 this._itemMovedOldParent = oldParent;
75 this._itemMovedOldIndex = oldIndex;
76 this._itemMovedNewParent = newParent;
77 this._itemMovedNewIndex = newIndex;
78 },
79 _itemMovedId: null,
80 _itemMovedOldParent: null,
81 _itemMovedOldIndex: null,
82 _itemMovedNewParent: null,
83 _itemMovedNewIndex: null,
84
85 QueryInterface: function(iid) {
86 if (iid.equals(Ci.nsINavBookmarkObserver) ||
87 iid.equals(Ci.nsISupports)) {
88 return this;
89 }
90 throw Cr.NS_ERROR_NO_INTERFACE;
91 }
92 };
93
94 // index at which items should begin
95 let bmStartIndex = 0;
96
97 // get bookmarks root id
98 let root = PlacesUtils.bookmarksMenuFolderId;
99
100 function run_test() {
101 bmsvc.addObserver(observer, false);
102 do_register_cleanup(function () {
103 bmsvc.removeObserver(observer);
104 });
105
106 run_next_test();
107 }
108
109 add_test(function test_create_folder_with_description() {
110 const TEST_FOLDERNAME = "Test creating a folder with a description";
111 const TEST_DESCRIPTION = "this is my test description";
112
113 let annos = [{ name: DESCRIPTION_ANNO,
114 type: annosvc.TYPE_STRING,
115 flags: 0,
116 value: TEST_DESCRIPTION,
117 expires: annosvc.EXPIRE_NEVER }];
118 let txn = new PlacesCreateFolderTransaction(TEST_FOLDERNAME, root, bmStartIndex, annos);
119 txnManager.doTransaction(txn);
120
121 // This checks that calling undoTransaction on an "empty batch" doesn't
122 // undo the previous transaction (getItemTitle will fail)
123 txnManager.beginBatch(null);
124 txnManager.endBatch(false);
125 txnManager.undoTransaction();
126
127 let folderId = observer._itemAddedId;
128 do_check_eq(bmsvc.getItemTitle(folderId), TEST_FOLDERNAME);
129 do_check_eq(observer._itemAddedIndex, bmStartIndex);
130 do_check_eq(observer._itemAddedParent, root);
131 do_check_eq(observer._itemAddedId, folderId);
132 do_check_eq(TEST_DESCRIPTION, annosvc.getItemAnnotation(folderId, DESCRIPTION_ANNO));
133
134 txn.undoTransaction();
135 do_check_eq(observer._itemRemovedId, folderId);
136 do_check_eq(observer._itemRemovedFolder, root);
137 do_check_eq(observer._itemRemovedIndex, bmStartIndex);
138
139 txn.redoTransaction();
140 do_check_eq(observer._itemAddedIndex, bmStartIndex);
141 do_check_eq(observer._itemAddedParent, root);
142 do_check_eq(observer._itemAddedId, folderId);
143
144 txn.undoTransaction();
145 do_check_eq(observer._itemRemovedId, folderId);
146 do_check_eq(observer._itemRemovedFolder, root);
147 do_check_eq(observer._itemRemovedIndex, bmStartIndex);
148
149 run_next_test();
150 });
151
152 add_test(function test_create_item() {
153 let testURI = NetUtil.newURI("http://test_create_item.com");
154
155 let txn = new PlacesCreateBookmarkTransaction(testURI, root, bmStartIndex,
156 "Test creating an item");
157
158 txnManager.doTransaction(txn);
159 let id = bmsvc.getBookmarkIdsForURI(testURI)[0];
160 do_check_eq(observer._itemAddedId, id);
161 do_check_eq(observer._itemAddedIndex, bmStartIndex);
162 do_check_true(bmsvc.isBookmarked(testURI));
163
164 txn.undoTransaction();
165 do_check_eq(observer._itemRemovedId, id);
166 do_check_eq(observer._itemRemovedIndex, bmStartIndex);
167 do_check_false(bmsvc.isBookmarked(testURI));
168
169 txn.redoTransaction();
170 do_check_true(bmsvc.isBookmarked(testURI));
171 let newId = bmsvc.getBookmarkIdsForURI(testURI)[0];
172 do_check_eq(observer._itemAddedIndex, bmStartIndex);
173 do_check_eq(observer._itemAddedParent, root);
174 do_check_eq(observer._itemAddedId, newId);
175
176 txn.undoTransaction();
177 do_check_eq(observer._itemRemovedId, newId);
178 do_check_eq(observer._itemRemovedFolder, root);
179 do_check_eq(observer._itemRemovedIndex, bmStartIndex);
180
181 run_next_test();
182 });
183
184 add_test(function test_create_item_to_folder() {
185 const TEST_FOLDERNAME = "Test creating item to a folder";
186 let testURI = NetUtil.newURI("http://test_create_item_to_folder.com");
187 let folderId = bmsvc.createFolder(root, TEST_FOLDERNAME, bmsvc.DEFAULT_INDEX);
188
189 let txn = new PlacesCreateBookmarkTransaction(testURI, folderId, bmStartIndex,
190 "Test creating item");
191 txnManager.doTransaction(txn);
192 let bkmId = bmsvc.getBookmarkIdsForURI(testURI)[0];
193 do_check_eq(observer._itemAddedId, bkmId);
194 do_check_eq(observer._itemAddedIndex, bmStartIndex);
195 do_check_true(bmsvc.isBookmarked(testURI));
196
197 txn.undoTransaction();
198 do_check_eq(observer._itemRemovedId, bkmId);
199 do_check_eq(observer._itemRemovedIndex, bmStartIndex);
200
201 txn.redoTransaction();
202 let newBkmId = bmsvc.getBookmarkIdsForURI(testURI)[0];
203 do_check_eq(observer._itemAddedIndex, bmStartIndex);
204 do_check_eq(observer._itemAddedParent, folderId);
205 do_check_eq(observer._itemAddedId, newBkmId);
206
207 txn.undoTransaction();
208 do_check_eq(observer._itemRemovedId, newBkmId);
209 do_check_eq(observer._itemRemovedFolder, folderId);
210 do_check_eq(observer._itemRemovedIndex, bmStartIndex);
211
212 run_next_test();
213 });
214
215 add_test(function test_move_items_to_folder() {
216 let testFolderId = bmsvc.createFolder(root, "Test move items", bmsvc.DEFAULT_INDEX);
217 let testURI = NetUtil.newURI("http://test_move_items.com");
218 let testBkmId = bmsvc.insertBookmark(testFolderId, testURI, bmsvc.DEFAULT_INDEX, "1: Test move items");
219 bmsvc.insertBookmark(testFolderId, testURI, bmsvc.DEFAULT_INDEX, "2: Test move items");
220
221 // Moving items between the same folder
222 let sameTxn = new PlacesMoveItemTransaction(testBkmId, testFolderId, bmsvc.DEFAULT_INDEX);
223
224 sameTxn.doTransaction();
225 do_check_eq(observer._itemMovedId, testBkmId);
226 do_check_eq(observer._itemMovedOldParent, testFolderId);
227 do_check_eq(observer._itemMovedOldIndex, 0);
228 do_check_eq(observer._itemMovedNewParent, testFolderId);
229 do_check_eq(observer._itemMovedNewIndex, 1);
230
231 sameTxn.undoTransaction();
232 do_check_eq(observer._itemMovedId, testBkmId);
233 do_check_eq(observer._itemMovedOldParent, testFolderId);
234 do_check_eq(observer._itemMovedOldIndex, 1);
235 do_check_eq(observer._itemMovedNewParent, testFolderId);
236 do_check_eq(observer._itemMovedNewIndex, 0);
237
238 sameTxn.redoTransaction();
239 do_check_eq(observer._itemMovedId, testBkmId);
240 do_check_eq(observer._itemMovedOldParent, testFolderId);
241 do_check_eq(observer._itemMovedOldIndex, 0);
242 do_check_eq(observer._itemMovedNewParent, testFolderId);
243 do_check_eq(observer._itemMovedNewIndex, 1);
244
245 sameTxn.undoTransaction();
246 do_check_eq(observer._itemMovedId, testBkmId);
247 do_check_eq(observer._itemMovedOldParent, testFolderId);
248 do_check_eq(observer._itemMovedOldIndex, 1);
249 do_check_eq(observer._itemMovedNewParent, testFolderId);
250 do_check_eq(observer._itemMovedNewIndex, 0);
251
252 // Moving items between different folders
253 let folderId = bmsvc.createFolder(testFolderId,
254 "Test move items between different folders",
255 bmsvc.DEFAULT_INDEX);
256 let diffTxn = new PlacesMoveItemTransaction(testBkmId, folderId, bmsvc.DEFAULT_INDEX);
257
258 diffTxn.doTransaction();
259 do_check_eq(observer._itemMovedId, testBkmId);
260 do_check_eq(observer._itemMovedOldParent, testFolderId);
261 do_check_eq(observer._itemMovedOldIndex, 0);
262 do_check_eq(observer._itemMovedNewParent, folderId);
263 do_check_eq(observer._itemMovedNewIndex, 0);
264
265 sameTxn.undoTransaction();
266 do_check_eq(observer._itemMovedId, testBkmId);
267 do_check_eq(observer._itemMovedOldParent, folderId);
268 do_check_eq(observer._itemMovedOldIndex, 0);
269 do_check_eq(observer._itemMovedNewParent, testFolderId);
270 do_check_eq(observer._itemMovedNewIndex, 0);
271
272 diffTxn.redoTransaction();
273 do_check_eq(observer._itemMovedId, testBkmId);
274 do_check_eq(observer._itemMovedOldParent, testFolderId);
275 do_check_eq(observer._itemMovedOldIndex, 0);
276 do_check_eq(observer._itemMovedNewParent, folderId);
277 do_check_eq(observer._itemMovedNewIndex, 0);
278
279 sameTxn.undoTransaction();
280 do_check_eq(observer._itemMovedId, testBkmId);
281 do_check_eq(observer._itemMovedOldParent, folderId);
282 do_check_eq(observer._itemMovedOldIndex, 0);
283 do_check_eq(observer._itemMovedNewParent, testFolderId);
284 do_check_eq(observer._itemMovedNewIndex, 0);
285
286 run_next_test();
287 });
288
289 add_test(function test_remove_folder() {
290 let testFolder = bmsvc.createFolder(root, "Test Removing a Folder", bmsvc.DEFAULT_INDEX);
291 let folderId = bmsvc.createFolder(testFolder, "Removed Folder", bmsvc.DEFAULT_INDEX);
292
293 let txn = new PlacesRemoveItemTransaction(folderId);
294
295 txn.doTransaction();
296 do_check_eq(observer._itemRemovedId, folderId);
297 do_check_eq(observer._itemRemovedFolder, testFolder);
298 do_check_eq(observer._itemRemovedIndex, 0);
299
300 txn.undoTransaction();
301 do_check_eq(observer._itemAddedId, folderId);
302 do_check_eq(observer._itemAddedParent, testFolder);
303 do_check_eq(observer._itemAddedIndex, 0);
304
305 txn.redoTransaction();
306 do_check_eq(observer._itemRemovedId, folderId);
307 do_check_eq(observer._itemRemovedFolder, testFolder);
308 do_check_eq(observer._itemRemovedIndex, 0);
309
310 txn.undoTransaction();
311 do_check_eq(observer._itemAddedId, folderId);
312 do_check_eq(observer._itemAddedParent, testFolder);
313 do_check_eq(observer._itemAddedIndex, 0);
314
315 run_next_test();
316 });
317
318 add_test(function test_remove_item_with_keyword_and_tag() {
319 // Notice in this case the tag persists since other bookmarks have same uri.
320 let testFolder = bmsvc.createFolder(root, "Test removing an item with a keyword and a tag",
321 bmsvc.DEFAULT_INDEX);
322
323 const KEYWORD = "test: test removing an item with a keyword and a tag";
324 const TAG_NAME = "tag-test_remove_item_with_keyword_and_tag";
325 let testURI = NetUtil.newURI("http://test_remove_item_with_keyword_and_tag.com");
326 let testBkmId = bmsvc.insertBookmark(testFolder, testURI, bmsvc.DEFAULT_INDEX, "test-item1");
327
328 // create bookmark for not removing tag.
329 bmsvc.insertBookmark(testFolder, testURI, bmsvc.DEFAULT_INDEX, "test-item2");
330
331 // set tag & keyword
332 bmsvc.setKeywordForBookmark(testBkmId, KEYWORD);
333 tagssvc.tagURI(testURI, [TAG_NAME]);
334
335 let txn = new PlacesRemoveItemTransaction(testBkmId);
336
337 txn.doTransaction();
338 do_check_eq(observer._itemRemovedId, testBkmId);
339 do_check_eq(observer._itemRemovedFolder, testFolder);
340 do_check_eq(observer._itemRemovedIndex, 0);
341 do_check_eq(bmsvc.getKeywordForBookmark(testBkmId), null);
342 do_check_eq(tagssvc.getTagsForURI(testURI)[0], TAG_NAME);
343
344 txn.undoTransaction();
345 let newbkmk2Id = observer._itemAddedId;
346 do_check_eq(observer._itemAddedParent, testFolder);
347 do_check_eq(observer._itemAddedIndex, 0);
348 do_check_eq(bmsvc.getKeywordForBookmark(newbkmk2Id), KEYWORD);
349 do_check_eq(tagssvc.getTagsForURI(testURI)[0], TAG_NAME);
350
351 txn.redoTransaction();
352 do_check_eq(observer._itemRemovedId, newbkmk2Id);
353 do_check_eq(observer._itemRemovedFolder, testFolder);
354 do_check_eq(observer._itemRemovedIndex, 0);
355 do_check_eq(bmsvc.getKeywordForBookmark(newbkmk2Id), null);
356 do_check_eq(tagssvc.getTagsForURI(testURI)[0], TAG_NAME);
357
358 txn.undoTransaction();
359 do_check_eq(observer._itemAddedParent, testFolder);
360 do_check_eq(observer._itemAddedIndex, 0);
361 do_check_eq(tagssvc.getTagsForURI(testURI)[0], TAG_NAME);
362
363 run_next_test();
364 });
365
366 add_test(function test_remove_item_with_tag() {
367 const TAG_NAME = "tag-test_remove_item_with_tag";
368 let testURI = NetUtil.newURI("http://test_remove_item_with_tag.com/");
369 let itemId = bmsvc.insertBookmark(root, testURI, bmsvc.DEFAULT_INDEX, "Test removing an item with a tag");
370 tagssvc.tagURI(testURI, [TAG_NAME]);
371
372 let txn = new PlacesRemoveItemTransaction(itemId);
373
374 txn.doTransaction();
375 do_check_true(tagssvc.getTagsForURI(testURI).length == 0);
376
377 txn.undoTransaction();
378 do_check_eq(tagssvc.getTagsForURI(testURI)[0], TAG_NAME);
379
380 txn.redoTransaction();
381 do_check_true(tagssvc.getTagsForURI(testURI).length == 0);
382
383 txn.undoTransaction();
384 do_check_eq(tagssvc.getTagsForURI(testURI)[0], TAG_NAME);
385
386 txn.redoTransaction();
387
388 run_next_test();
389 });
390
391 add_test(function test_creating_separator() {
392 let testFolder = bmsvc.createFolder(root, "Test creating a separator", bmsvc.DEFAULT_INDEX);
393
394 let txn = new PlacesCreateSeparatorTransaction(testFolder, 0);
395 txn.doTransaction();
396
397 let sepId = observer._itemAddedId;
398 do_check_eq(observer._itemAddedIndex, 0);
399 do_check_eq(observer._itemAddedParent, testFolder);
400
401 txn.undoTransaction();
402 do_check_eq(observer._itemRemovedId, sepId);
403 do_check_eq(observer._itemRemovedFolder, testFolder);
404 do_check_eq(observer._itemRemovedIndex, 0);
405
406 txn.redoTransaction();
407 let newSepId = observer._itemAddedId;
408 do_check_eq(observer._itemAddedIndex, 0);
409 do_check_eq(observer._itemAddedParent, testFolder);
410
411 txn.undoTransaction();
412 do_check_eq(observer._itemRemovedId, newSepId);
413 do_check_eq(observer._itemRemovedFolder, testFolder);
414 do_check_eq(observer._itemRemovedIndex, 0);
415
416 run_next_test();
417 });
418
419 add_test(function test_removing_separator() {
420 let testFolder = bmsvc.createFolder(root, "Test removing a separator", bmsvc.DEFAULT_INDEX);
421
422 let sepId = bmsvc.insertSeparator(testFolder, 0);
423 let txn = new PlacesRemoveItemTransaction(sepId);
424
425 txn.doTransaction();
426 do_check_eq(observer._itemRemovedId, sepId);
427 do_check_eq(observer._itemRemovedFolder, testFolder);
428 do_check_eq(observer._itemRemovedIndex, 0);
429
430 txn.undoTransaction();
431 do_check_eq(observer._itemAddedId, sepId); //New separator created
432 do_check_eq(observer._itemAddedParent, testFolder);
433 do_check_eq(observer._itemAddedIndex, 0);
434
435 txn.redoTransaction();
436 do_check_eq(observer._itemRemovedId, sepId);
437 do_check_eq(observer._itemRemovedFolder, testFolder);
438 do_check_eq(observer._itemRemovedIndex, 0);
439
440 txn.undoTransaction();
441 do_check_eq(observer._itemAddedId, sepId); //New separator created
442 do_check_eq(observer._itemAddedParent, testFolder);
443 do_check_eq(observer._itemAddedIndex, 0);
444
445 run_next_test();
446 });
447
448 add_test(function test_editing_item_title() {
449 const TITLE = "Test editing item title";
450 const MOD_TITLE = "Mod: Test editing item title";
451 let testURI = NetUtil.newURI("http://www.test_editing_item_title.com");
452 let testBkmId = bmsvc.insertBookmark(root, testURI, bmsvc.DEFAULT_INDEX, TITLE);
453
454 let txn = new PlacesEditItemTitleTransaction(testBkmId, MOD_TITLE);
455
456 txn.doTransaction();
457 do_check_eq(observer._itemChangedId, testBkmId);
458 do_check_eq(observer._itemChangedProperty, "title");
459 do_check_eq(observer._itemChangedValue, MOD_TITLE);
460
461 txn.undoTransaction();
462 do_check_eq(observer._itemChangedId, testBkmId);
463 do_check_eq(observer._itemChangedProperty, "title");
464 do_check_eq(observer._itemChangedValue, TITLE);
465
466 txn.redoTransaction();
467 do_check_eq(observer._itemChangedId, testBkmId);
468 do_check_eq(observer._itemChangedProperty, "title");
469 do_check_eq(observer._itemChangedValue, MOD_TITLE);
470
471 txn.undoTransaction();
472 do_check_eq(observer._itemChangedId, testBkmId);
473 do_check_eq(observer._itemChangedProperty, "title");
474 do_check_eq(observer._itemChangedValue, TITLE);
475
476 run_next_test();
477 });
478
479 add_test(function test_editing_item_uri() {
480 const OLD_TEST_URI = NetUtil.newURI("http://old.test_editing_item_uri.com/");
481 const NEW_TEST_URI = NetUtil.newURI("http://new.test_editing_item_uri.com/");
482 let testBkmId = bmsvc.insertBookmark(root, OLD_TEST_URI, bmsvc.DEFAULT_INDEX,
483 "Test editing item title");
484 tagssvc.tagURI(OLD_TEST_URI, ["tag"]);
485
486 let txn = new PlacesEditBookmarkURITransaction(testBkmId, NEW_TEST_URI);
487
488 txn.doTransaction();
489 do_check_eq(observer._itemChangedId, testBkmId);
490 do_check_eq(observer._itemChangedProperty, "uri");
491 do_check_eq(observer._itemChangedValue, NEW_TEST_URI.spec);
492 do_check_eq(JSON.stringify(tagssvc.getTagsForURI(NEW_TEST_URI)), JSON.stringify(["tag"]));
493 do_check_eq(JSON.stringify(tagssvc.getTagsForURI(OLD_TEST_URI)), JSON.stringify([]));
494
495 txn.undoTransaction();
496 do_check_eq(observer._itemChangedId, testBkmId);
497 do_check_eq(observer._itemChangedProperty, "uri");
498 do_check_eq(observer._itemChangedValue, OLD_TEST_URI.spec);
499 do_check_eq(JSON.stringify(tagssvc.getTagsForURI(OLD_TEST_URI)), JSON.stringify(["tag"]));
500 do_check_eq(JSON.stringify(tagssvc.getTagsForURI(NEW_TEST_URI)), JSON.stringify([]));
501
502 txn.redoTransaction();
503 do_check_eq(observer._itemChangedId, testBkmId);
504 do_check_eq(observer._itemChangedProperty, "uri");
505 do_check_eq(observer._itemChangedValue, NEW_TEST_URI.spec);
506 do_check_eq(JSON.stringify(tagssvc.getTagsForURI(NEW_TEST_URI)), JSON.stringify(["tag"]));
507 do_check_eq(JSON.stringify(tagssvc.getTagsForURI(OLD_TEST_URI)), JSON.stringify([]));
508
509 txn.undoTransaction();
510 do_check_eq(observer._itemChangedId, testBkmId);
511 do_check_eq(observer._itemChangedProperty, "uri");
512 do_check_eq(observer._itemChangedValue, OLD_TEST_URI.spec);
513 do_check_eq(JSON.stringify(tagssvc.getTagsForURI(OLD_TEST_URI)), JSON.stringify(["tag"]));
514 do_check_eq(JSON.stringify(tagssvc.getTagsForURI(NEW_TEST_URI)), JSON.stringify([]));
515
516 run_next_test();
517 });
518
519 add_test(function test_edit_description_transaction() {
520 let testURI = NetUtil.newURI("http://test_edit_description_transaction.com");
521 let testBkmId = bmsvc.insertBookmark(root, testURI, bmsvc.DEFAULT_INDEX, "Test edit description transaction");
522
523 let anno = {
524 name: DESCRIPTION_ANNO,
525 type: Ci.nsIAnnotationService.TYPE_STRING,
526 flags: 0,
527 value: "Test edit Description",
528 expires: Ci.nsIAnnotationService.EXPIRE_NEVER,
529 };
530 let txn = new PlacesSetItemAnnotationTransaction(testBkmId, anno);
531
532 txn.doTransaction();
533 do_check_eq(observer._itemChangedId, testBkmId);
534 do_check_eq(observer._itemChangedProperty, DESCRIPTION_ANNO);
535
536 run_next_test();
537 });
538
539 add_test(function test_edit_keyword() {
540 const KEYWORD = "keyword-test_edit_keyword";
541
542 let testURI = NetUtil.newURI("http://test_edit_keyword.com");
543 let testBkmId = bmsvc.insertBookmark(root, testURI, bmsvc.DEFAULT_INDEX, "Test edit keyword");
544
545 let txn = new PlacesEditBookmarkKeywordTransaction(testBkmId, KEYWORD);
546
547 txn.doTransaction();
548 do_check_eq(observer._itemChangedId, testBkmId);
549 do_check_eq(observer._itemChangedProperty, "keyword");
550 do_check_eq(observer._itemChangedValue, KEYWORD);
551
552 txn.undoTransaction();
553 do_check_eq(observer._itemChangedId, testBkmId);
554 do_check_eq(observer._itemChangedProperty, "keyword");
555 do_check_eq(observer._itemChangedValue, "");
556
557 run_next_test();
558 });
559
560 add_test(function test_LoadInSidebar_transaction() {
561 let testURI = NetUtil.newURI("http://test_LoadInSidebar_transaction.com");
562 let testBkmId = bmsvc.insertBookmark(root, testURI, bmsvc.DEFAULT_INDEX, "Test LoadInSidebar transaction");
563
564 const LOAD_IN_SIDEBAR_ANNO = "bookmarkProperties/loadInSidebar";
565 let anno = { name: LOAD_IN_SIDEBAR_ANNO,
566 type: Ci.nsIAnnotationService.TYPE_INT32,
567 flags: 0,
568 value: true,
569 expires: Ci.nsIAnnotationService.EXPIRE_NEVER };
570 let txn = new PlacesSetItemAnnotationTransaction(testBkmId, anno);
571
572 txn.doTransaction();
573 do_check_eq(observer._itemChangedId, testBkmId);
574 do_check_eq(observer._itemChangedProperty, LOAD_IN_SIDEBAR_ANNO);
575 do_check_eq(observer._itemChanged_isAnnotationProperty, true);
576
577 txn.undoTransaction();
578 do_check_eq(observer._itemChangedId, testBkmId);
579 do_check_eq(observer._itemChangedProperty, LOAD_IN_SIDEBAR_ANNO);
580 do_check_eq(observer._itemChanged_isAnnotationProperty, true);
581
582 run_next_test();
583 });
584
585 add_test(function test_generic_item_annotation() {
586 let testURI = NetUtil.newURI("http://test_generic_item_annotation.com");
587 let testBkmId = bmsvc.insertBookmark(root, testURI, bmsvc.DEFAULT_INDEX, "Test generic item annotation");
588
589 let itemAnnoObj = { name: "testAnno/testInt",
590 type: Ci.nsIAnnotationService.TYPE_INT32,
591 flags: 0,
592 value: 123,
593 expires: Ci.nsIAnnotationService.EXPIRE_NEVER };
594 let txn = new PlacesSetItemAnnotationTransaction(testBkmId, itemAnnoObj);
595
596 txn.doTransaction();
597 do_check_eq(observer._itemChangedId, testBkmId);
598 do_check_eq(observer._itemChangedProperty, "testAnno/testInt");
599 do_check_eq(observer._itemChanged_isAnnotationProperty, true);
600
601 txn.undoTransaction();
602 do_check_eq(observer._itemChangedId, testBkmId);
603 do_check_eq(observer._itemChangedProperty, "testAnno/testInt");
604 do_check_eq(observer._itemChanged_isAnnotationProperty, true);
605
606 txn.redoTransaction();
607 do_check_eq(observer._itemChangedId, testBkmId);
608 do_check_eq(observer._itemChangedProperty, "testAnno/testInt");
609 do_check_eq(observer._itemChanged_isAnnotationProperty, true);
610
611 run_next_test();
612 });
613
614 add_test(function test_editing_item_date_added() {
615 let testURI = NetUtil.newURI("http://test_editing_item_date_added.com");
616 let testBkmId = bmsvc.insertBookmark(root, testURI, bmsvc.DEFAULT_INDEX,
617 "Test editing item date added");
618
619 let oldAdded = bmsvc.getItemDateAdded(testBkmId);
620 let newAdded = Date.now() + 1000;
621 let txn = new PlacesEditItemDateAddedTransaction(testBkmId, newAdded);
622
623 txn.doTransaction();
624 do_check_eq(newAdded, bmsvc.getItemDateAdded(testBkmId));
625
626 txn.undoTransaction();
627 do_check_eq(oldAdded, bmsvc.getItemDateAdded(testBkmId));
628
629 run_next_test();
630 });
631
632 add_test(function test_edit_item_last_modified() {
633 let testURI = NetUtil.newURI("http://test_edit_item_last_modified.com");
634 let testBkmId = bmsvc.insertBookmark(root, testURI, bmsvc.DEFAULT_INDEX,
635 "Test editing item last modified");
636
637 let oldModified = bmsvc.getItemLastModified(testBkmId);
638 let newModified = Date.now() + 1000;
639 let txn = new PlacesEditItemLastModifiedTransaction(testBkmId, newModified);
640
641 txn.doTransaction();
642 do_check_eq(newModified, bmsvc.getItemLastModified(testBkmId));
643
644 txn.undoTransaction();
645 do_check_eq(oldModified, bmsvc.getItemLastModified(testBkmId));
646
647 run_next_test();
648 });
649
650 add_test(function test_generic_page_annotation() {
651 const TEST_ANNO = "testAnno/testInt";
652 let testURI = NetUtil.newURI("http://www.mozilla.org/");
653 promiseAddVisits(testURI).then(function () {
654 let pageAnnoObj = { name: TEST_ANNO,
655 type: Ci.nsIAnnotationService.TYPE_INT32,
656 flags: 0,
657 value: 123,
658 expires: Ci.nsIAnnotationService.EXPIRE_NEVER };
659 let txn = new PlacesSetPageAnnotationTransaction(testURI, pageAnnoObj);
660
661 txn.doTransaction();
662 do_check_true(annosvc.pageHasAnnotation(testURI, TEST_ANNO));
663
664 txn.undoTransaction();
665 do_check_false(annosvc.pageHasAnnotation(testURI, TEST_ANNO));
666
667 txn.redoTransaction();
668 do_check_true(annosvc.pageHasAnnotation(testURI, TEST_ANNO));
669
670 run_next_test();
671 });
672 });
673
674 add_test(function test_sort_folder_by_name() {
675 let testFolder = bmsvc.createFolder(root, "Test PlacesSortFolderByNameTransaction",
676 bmsvc.DEFAULT_INDEX);
677 let testURI = NetUtil.newURI("http://test_sort_folder_by_name.com");
678
679 bmsvc.insertBookmark(testFolder, testURI, bmsvc.DEFAULT_INDEX, "bookmark3");
680 bmsvc.insertBookmark(testFolder, testURI, bmsvc.DEFAULT_INDEX, "bookmark2");
681 bmsvc.insertBookmark(testFolder, testURI, bmsvc.DEFAULT_INDEX, "bookmark1");
682
683 let bkmIds = bmsvc.getBookmarkIdsForURI(testURI);
684 bkmIds.sort();
685
686 let b1 = bkmIds[0];
687 let b2 = bkmIds[1];
688 let b3 = bkmIds[2];
689
690 do_check_eq(0, bmsvc.getItemIndex(b1));
691 do_check_eq(1, bmsvc.getItemIndex(b2));
692 do_check_eq(2, bmsvc.getItemIndex(b3));
693
694 let txn = new PlacesSortFolderByNameTransaction(testFolder);
695
696 txn.doTransaction();
697 do_check_eq(2, bmsvc.getItemIndex(b1));
698 do_check_eq(1, bmsvc.getItemIndex(b2));
699 do_check_eq(0, bmsvc.getItemIndex(b3));
700
701 txn.undoTransaction();
702 do_check_eq(0, bmsvc.getItemIndex(b1));
703 do_check_eq(1, bmsvc.getItemIndex(b2));
704 do_check_eq(2, bmsvc.getItemIndex(b3));
705
706 txn.redoTransaction();
707 do_check_eq(2, bmsvc.getItemIndex(b1));
708 do_check_eq(1, bmsvc.getItemIndex(b2));
709 do_check_eq(0, bmsvc.getItemIndex(b3));
710
711 txn.undoTransaction();
712 do_check_eq(0, bmsvc.getItemIndex(b1));
713 do_check_eq(1, bmsvc.getItemIndex(b2));
714 do_check_eq(2, bmsvc.getItemIndex(b3));
715
716 run_next_test();
717 });
718
719 add_test(function test_edit_postData() {
720 const POST_DATA_ANNO = "bookmarkProperties/POSTData";
721 let postData = "post-test_edit_postData";
722 let testURI = NetUtil.newURI("http://test_edit_postData.com");
723 let testBkmId = bmsvc.insertBookmark(root, testURI, bmsvc.DEFAULT_INDEX, "Test edit Post Data");
724
725 let txn = new PlacesEditBookmarkPostDataTransaction(testBkmId, postData);
726
727 txn.doTransaction();
728 do_check_true(annosvc.itemHasAnnotation(testBkmId, POST_DATA_ANNO));
729 do_check_eq(annosvc.getItemAnnotation(testBkmId, POST_DATA_ANNO), postData);
730
731 txn.undoTransaction();
732 do_check_false(annosvc.itemHasAnnotation(testBkmId, POST_DATA_ANNO));
733
734 run_next_test();
735 });
736
737 add_test(function test_tagURI_untagURI() {
738 const TAG_1 = "tag-test_tagURI_untagURI-bar";
739 const TAG_2 = "tag-test_tagURI_untagURI-foo";
740 let tagURI = NetUtil.newURI("http://test_tagURI_untagURI.com");
741
742 // Test tagURI
743 let tagTxn = new PlacesTagURITransaction(tagURI, [TAG_1, TAG_2]);
744
745 tagTxn.doTransaction();
746 do_check_eq(JSON.stringify(tagssvc.getTagsForURI(tagURI)), JSON.stringify([TAG_1,TAG_2]));
747
748 tagTxn.undoTransaction();
749 do_check_eq(tagssvc.getTagsForURI(tagURI).length, 0);
750
751 tagTxn.redoTransaction();
752 do_check_eq(JSON.stringify(tagssvc.getTagsForURI(tagURI)), JSON.stringify([TAG_1,TAG_2]));
753
754 // Test untagURI
755 let untagTxn = new PlacesUntagURITransaction(tagURI, [TAG_1]);
756
757 untagTxn.doTransaction();
758 do_check_eq(JSON.stringify(tagssvc.getTagsForURI(tagURI)), JSON.stringify([TAG_2]));
759
760 untagTxn.undoTransaction();
761 do_check_eq(JSON.stringify(tagssvc.getTagsForURI(tagURI)), JSON.stringify([TAG_1,TAG_2]));
762
763 untagTxn.redoTransaction();
764 do_check_eq(JSON.stringify(tagssvc.getTagsForURI(tagURI)), JSON.stringify([TAG_2]));
765
766 run_next_test();
767 });
768
769 add_test(function test_aggregate_removeItem_Txn() {
770 let testFolder = bmsvc.createFolder(root, "Test aggregate removeItem transaction", bmsvc.DEFAULT_INDEX);
771
772 const TEST_URL = "http://test_aggregate_removeitem_txn.com/";
773 const FOLDERNAME = "Folder";
774 let testURI = NetUtil.newURI(TEST_URL);
775
776 let bkmk1Id = bmsvc.insertBookmark(testFolder, testURI, 0, "Mozilla");
777 let bkmk2Id = bmsvc.insertSeparator(testFolder, 1);
778 let bkmk3Id = bmsvc.createFolder(testFolder, FOLDERNAME, 2);
779
780 let bkmk3_1Id = bmsvc.insertBookmark(bkmk3Id, testURI, 0, "Mozilla");
781 let bkmk3_2Id = bmsvc.insertSeparator(bkmk3Id, 1);
782 let bkmk3_3Id = bmsvc.createFolder(bkmk3Id, FOLDERNAME, 2);
783
784 let childTxn1 = new PlacesRemoveItemTransaction(bkmk1Id);
785 let childTxn2 = new PlacesRemoveItemTransaction(bkmk2Id);
786 let childTxn3 = new PlacesRemoveItemTransaction(bkmk3Id);
787 let transactions = [childTxn1, childTxn2, childTxn3];
788 let txn = new PlacesAggregatedTransaction("RemoveItems", transactions);
789
790 txn.doTransaction();
791 do_check_eq(bmsvc.getItemIndex(bkmk1Id), -1);
792 do_check_eq(bmsvc.getItemIndex(bkmk2Id), -1);
793 do_check_eq(bmsvc.getItemIndex(bkmk3Id), -1);
794 do_check_eq(bmsvc.getItemIndex(bkmk3_1Id), -1);
795 do_check_eq(bmsvc.getItemIndex(bkmk3_2Id), -1);
796 do_check_eq(bmsvc.getItemIndex(bkmk3_3Id), -1);
797 // Check last removed item id.
798 do_check_eq(observer._itemRemovedId, bkmk3Id);
799
800 txn.undoTransaction();
801 let newBkmk1Id = bmsvc.getIdForItemAt(testFolder, 0);
802 let newBkmk2Id = bmsvc.getIdForItemAt(testFolder, 1);
803 let newBkmk3Id = bmsvc.getIdForItemAt(testFolder, 2);
804 let newBkmk3_1Id = bmsvc.getIdForItemAt(newBkmk3Id, 0);
805 let newBkmk3_2Id = bmsvc.getIdForItemAt(newBkmk3Id, 1);
806 let newBkmk3_3Id = bmsvc.getIdForItemAt(newBkmk3Id, 2);
807 do_check_eq(bmsvc.getItemType(newBkmk1Id), bmsvc.TYPE_BOOKMARK);
808 do_check_eq(bmsvc.getBookmarkURI(newBkmk1Id).spec, TEST_URL);
809 do_check_eq(bmsvc.getItemType(newBkmk2Id), bmsvc.TYPE_SEPARATOR);
810 do_check_eq(bmsvc.getItemType(newBkmk3Id), bmsvc.TYPE_FOLDER);
811 do_check_eq(bmsvc.getItemTitle(newBkmk3Id), FOLDERNAME);
812 do_check_eq(bmsvc.getFolderIdForItem(newBkmk3_1Id), newBkmk3Id);
813 do_check_eq(bmsvc.getItemType(newBkmk3_1Id), bmsvc.TYPE_BOOKMARK);
814 do_check_eq(bmsvc.getBookmarkURI(newBkmk3_1Id).spec, TEST_URL);
815 do_check_eq(bmsvc.getFolderIdForItem(newBkmk3_2Id), newBkmk3Id);
816 do_check_eq(bmsvc.getItemType(newBkmk3_2Id), bmsvc.TYPE_SEPARATOR);
817 do_check_eq(bmsvc.getFolderIdForItem(newBkmk3_3Id), newBkmk3Id);
818 do_check_eq(bmsvc.getItemType(newBkmk3_3Id), bmsvc.TYPE_FOLDER);
819 do_check_eq(bmsvc.getItemTitle(newBkmk3_3Id), FOLDERNAME);
820 // Check last added back item id.
821 // Notice items are restored in reverse order.
822 do_check_eq(observer._itemAddedId, newBkmk1Id);
823
824 txn.redoTransaction();
825 do_check_eq(bmsvc.getItemIndex(newBkmk1Id), -1);
826 do_check_eq(bmsvc.getItemIndex(newBkmk2Id), -1);
827 do_check_eq(bmsvc.getItemIndex(newBkmk3Id), -1);
828 do_check_eq(bmsvc.getItemIndex(newBkmk3_1Id), -1);
829 do_check_eq(bmsvc.getItemIndex(newBkmk3_2Id), -1);
830 do_check_eq(bmsvc.getItemIndex(newBkmk3_3Id), -1);
831 // Check last removed item id.
832 do_check_eq(observer._itemRemovedId, newBkmk3Id);
833
834 txn.undoTransaction();
835 newBkmk1Id = bmsvc.getIdForItemAt(testFolder, 0);
836 newBkmk2Id = bmsvc.getIdForItemAt(testFolder, 1);
837 newBkmk3Id = bmsvc.getIdForItemAt(testFolder, 2);
838 newBkmk3_1Id = bmsvc.getIdForItemAt(newBkmk3Id, 0);
839 newBkmk3_2Id = bmsvc.getIdForItemAt(newBkmk3Id, 1);
840 newBkmk3_3Id = bmsvc.getIdForItemAt(newBkmk3Id, 2);
841 do_check_eq(bmsvc.getItemType(newBkmk1Id), bmsvc.TYPE_BOOKMARK);
842 do_check_eq(bmsvc.getBookmarkURI(newBkmk1Id).spec, TEST_URL);
843 do_check_eq(bmsvc.getItemType(newBkmk2Id), bmsvc.TYPE_SEPARATOR);
844 do_check_eq(bmsvc.getItemType(newBkmk3Id), bmsvc.TYPE_FOLDER);
845 do_check_eq(bmsvc.getItemTitle(newBkmk3Id), FOLDERNAME);
846 do_check_eq(bmsvc.getFolderIdForItem(newBkmk3_1Id), newBkmk3Id);
847 do_check_eq(bmsvc.getItemType(newBkmk3_1Id), bmsvc.TYPE_BOOKMARK);
848 do_check_eq(bmsvc.getBookmarkURI(newBkmk3_1Id).spec, TEST_URL);
849 do_check_eq(bmsvc.getFolderIdForItem(newBkmk3_2Id), newBkmk3Id);
850 do_check_eq(bmsvc.getItemType(newBkmk3_2Id), bmsvc.TYPE_SEPARATOR);
851 do_check_eq(bmsvc.getFolderIdForItem(newBkmk3_3Id), newBkmk3Id);
852 do_check_eq(bmsvc.getItemType(newBkmk3_3Id), bmsvc.TYPE_FOLDER);
853 do_check_eq(bmsvc.getItemTitle(newBkmk3_3Id), FOLDERNAME);
854 // Check last added back item id.
855 // Notice items are restored in reverse order.
856 do_check_eq(observer._itemAddedId, newBkmk1Id);
857
858 run_next_test();
859 });
860
861 add_test(function test_create_item_with_childTxn() {
862 let testFolder = bmsvc.createFolder(root, "Test creating an item with childTxns", bmsvc.DEFAULT_INDEX);
863
864 const BOOKMARK_TITLE = "parent item";
865 let testURI = NetUtil.newURI("http://test_create_item_with_childTxn.com");
866 let childTxns = [];
867 let newDateAdded = Date.now() - 20000;
868 let editDateAdddedTxn = new PlacesEditItemDateAddedTransaction(null, newDateAdded);
869 childTxns.push(editDateAdddedTxn);
870
871 let itemChildAnnoObj = { name: "testAnno/testInt",
872 type: Ci.nsIAnnotationService.TYPE_INT32,
873 flags: 0,
874 value: 123,
875 expires: Ci.nsIAnnotationService.EXPIRE_NEVER };
876 let annoTxn = new PlacesSetItemAnnotationTransaction(null, itemChildAnnoObj);
877 childTxns.push(annoTxn);
878
879 let itemWChildTxn = new PlacesCreateBookmarkTransaction(testURI, testFolder, bmStartIndex,
880 BOOKMARK_TITLE, null, null,
881 childTxns);
882 try {
883 txnManager.doTransaction(itemWChildTxn);
884 let itemId = bmsvc.getBookmarkIdsForURI(testURI)[0];
885 do_check_eq(observer._itemAddedId, itemId);
886 do_check_eq(newDateAdded, bmsvc.getItemDateAdded(itemId));
887 do_check_eq(observer._itemChangedProperty, "testAnno/testInt");
888 do_check_eq(observer._itemChanged_isAnnotationProperty, true);
889 do_check_true(annosvc.itemHasAnnotation(itemId, itemChildAnnoObj.name))
890 do_check_eq(annosvc.getItemAnnotation(itemId, itemChildAnnoObj.name), itemChildAnnoObj.value);
891
892 itemWChildTxn.undoTransaction();
893 do_check_eq(observer._itemRemovedId, itemId);
894
895 itemWChildTxn.redoTransaction();
896 do_check_true(bmsvc.isBookmarked(testURI));
897 let newId = bmsvc.getBookmarkIdsForURI(testURI)[0];
898 do_check_eq(newDateAdded, bmsvc.getItemDateAdded(newId));
899 do_check_eq(observer._itemAddedId, newId);
900 do_check_eq(observer._itemChangedProperty, "testAnno/testInt");
901 do_check_eq(observer._itemChanged_isAnnotationProperty, true);
902 do_check_true(annosvc.itemHasAnnotation(newId, itemChildAnnoObj.name))
903 do_check_eq(annosvc.getItemAnnotation(newId, itemChildAnnoObj.name), itemChildAnnoObj.value);
904
905 itemWChildTxn.undoTransaction();
906 do_check_eq(observer._itemRemovedId, newId);
907 }
908 catch (ex) {
909 do_throw("Setting a child transaction in a createItem transaction did throw: " + ex);
910 }
911
912 run_next_test();
913 });
914
915 add_test(function test_create_folder_with_child_itemTxn() {
916 let childURI = NetUtil.newURI("http://test_create_folder_with_child_itemTxn.com");
917 let childItemTxn = new PlacesCreateBookmarkTransaction(childURI, root,
918 bmStartIndex, "childItem");
919 let txn = new PlacesCreateFolderTransaction("Test creating a folder with child itemTxns",
920 root, bmStartIndex, null, [childItemTxn]);
921 try {
922 txnManager.doTransaction(txn);
923 let childItemId = bmsvc.getBookmarkIdsForURI(childURI)[0];
924 do_check_eq(observer._itemAddedId, childItemId);
925 do_check_eq(observer._itemAddedIndex, 0);
926 do_check_true(bmsvc.isBookmarked(childURI));
927
928 txn.undoTransaction();
929 do_check_false(bmsvc.isBookmarked(childURI));
930
931 txn.redoTransaction();
932 let newchildItemId = bmsvc.getBookmarkIdsForURI(childURI)[0];
933 do_check_eq(observer._itemAddedIndex, 0);
934 do_check_eq(observer._itemAddedId, newchildItemId);
935 do_check_true(bmsvc.isBookmarked(childURI));
936
937 txn.undoTransaction();
938 do_check_false(bmsvc.isBookmarked(childURI));
939 }
940 catch (ex) {
941 do_throw("Setting a child item transaction in a createFolder transaction did throw: " + ex);
942 }
943
944 run_next_test();
945 });

mercurial