1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/toolkit/components/places/tests/unit/test_placesTxn.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,945 @@ 1.4 +/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* vim:set ts=2 sw=2 sts=2 et: */ 1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.9 + 1.10 +let bmsvc = PlacesUtils.bookmarks; 1.11 +let tagssvc = PlacesUtils.tagging; 1.12 +let annosvc = PlacesUtils.annotations; 1.13 +let txnManager = PlacesUtils.transactionManager; 1.14 +const DESCRIPTION_ANNO = "bookmarkProperties/description"; 1.15 + 1.16 +// create and add bookmarks observer 1.17 +let observer = { 1.18 + 1.19 + onBeginUpdateBatch: function() { 1.20 + this._beginUpdateBatch = true; 1.21 + }, 1.22 + _beginUpdateBatch: false, 1.23 + 1.24 + onEndUpdateBatch: function() { 1.25 + this._endUpdateBatch = true; 1.26 + }, 1.27 + _endUpdateBatch: false, 1.28 + 1.29 + onItemAdded: function(id, folder, index, itemType, uri) { 1.30 + this._itemAddedId = id; 1.31 + this._itemAddedParent = folder; 1.32 + this._itemAddedIndex = index; 1.33 + this._itemAddedType = itemType; 1.34 + }, 1.35 + _itemAddedId: null, 1.36 + _itemAddedParent: null, 1.37 + _itemAddedIndex: null, 1.38 + _itemAddedType: null, 1.39 + 1.40 + onItemRemoved: function(id, folder, index, itemType) { 1.41 + this._itemRemovedId = id; 1.42 + this._itemRemovedFolder = folder; 1.43 + this._itemRemovedIndex = index; 1.44 + }, 1.45 + _itemRemovedId: null, 1.46 + _itemRemovedFolder: null, 1.47 + _itemRemovedIndex: null, 1.48 + 1.49 + onItemChanged: function(id, property, isAnnotationProperty, newValue, 1.50 + lastModified, itemType) { 1.51 + // The transaction manager is being rewritten in bug 891303, so just 1.52 + // skip checking this for now. 1.53 + if (property == "tags") 1.54 + return; 1.55 + this._itemChangedId = id; 1.56 + this._itemChangedProperty = property; 1.57 + this._itemChanged_isAnnotationProperty = isAnnotationProperty; 1.58 + this._itemChangedValue = newValue; 1.59 + }, 1.60 + _itemChangedId: null, 1.61 + _itemChangedProperty: null, 1.62 + _itemChanged_isAnnotationProperty: null, 1.63 + _itemChangedValue: null, 1.64 + 1.65 + onItemVisited: function(id, visitID, time) { 1.66 + this._itemVisitedId = id; 1.67 + this._itemVisitedVistId = visitID; 1.68 + this._itemVisitedTime = time; 1.69 + }, 1.70 + _itemVisitedId: null, 1.71 + _itemVisitedVistId: null, 1.72 + _itemVisitedTime: null, 1.73 + 1.74 + onItemMoved: function(id, oldParent, oldIndex, newParent, newIndex, 1.75 + itemType) { 1.76 + this._itemMovedId = id; 1.77 + this._itemMovedOldParent = oldParent; 1.78 + this._itemMovedOldIndex = oldIndex; 1.79 + this._itemMovedNewParent = newParent; 1.80 + this._itemMovedNewIndex = newIndex; 1.81 + }, 1.82 + _itemMovedId: null, 1.83 + _itemMovedOldParent: null, 1.84 + _itemMovedOldIndex: null, 1.85 + _itemMovedNewParent: null, 1.86 + _itemMovedNewIndex: null, 1.87 + 1.88 + QueryInterface: function(iid) { 1.89 + if (iid.equals(Ci.nsINavBookmarkObserver) || 1.90 + iid.equals(Ci.nsISupports)) { 1.91 + return this; 1.92 + } 1.93 + throw Cr.NS_ERROR_NO_INTERFACE; 1.94 + } 1.95 +}; 1.96 + 1.97 +// index at which items should begin 1.98 +let bmStartIndex = 0; 1.99 + 1.100 +// get bookmarks root id 1.101 +let root = PlacesUtils.bookmarksMenuFolderId; 1.102 + 1.103 +function run_test() { 1.104 + bmsvc.addObserver(observer, false); 1.105 + do_register_cleanup(function () { 1.106 + bmsvc.removeObserver(observer); 1.107 + }); 1.108 + 1.109 + run_next_test(); 1.110 +} 1.111 + 1.112 +add_test(function test_create_folder_with_description() { 1.113 + const TEST_FOLDERNAME = "Test creating a folder with a description"; 1.114 + const TEST_DESCRIPTION = "this is my test description"; 1.115 + 1.116 + let annos = [{ name: DESCRIPTION_ANNO, 1.117 + type: annosvc.TYPE_STRING, 1.118 + flags: 0, 1.119 + value: TEST_DESCRIPTION, 1.120 + expires: annosvc.EXPIRE_NEVER }]; 1.121 + let txn = new PlacesCreateFolderTransaction(TEST_FOLDERNAME, root, bmStartIndex, annos); 1.122 + txnManager.doTransaction(txn); 1.123 + 1.124 + // This checks that calling undoTransaction on an "empty batch" doesn't 1.125 + // undo the previous transaction (getItemTitle will fail) 1.126 + txnManager.beginBatch(null); 1.127 + txnManager.endBatch(false); 1.128 + txnManager.undoTransaction(); 1.129 + 1.130 + let folderId = observer._itemAddedId; 1.131 + do_check_eq(bmsvc.getItemTitle(folderId), TEST_FOLDERNAME); 1.132 + do_check_eq(observer._itemAddedIndex, bmStartIndex); 1.133 + do_check_eq(observer._itemAddedParent, root); 1.134 + do_check_eq(observer._itemAddedId, folderId); 1.135 + do_check_eq(TEST_DESCRIPTION, annosvc.getItemAnnotation(folderId, DESCRIPTION_ANNO)); 1.136 + 1.137 + txn.undoTransaction(); 1.138 + do_check_eq(observer._itemRemovedId, folderId); 1.139 + do_check_eq(observer._itemRemovedFolder, root); 1.140 + do_check_eq(observer._itemRemovedIndex, bmStartIndex); 1.141 + 1.142 + txn.redoTransaction(); 1.143 + do_check_eq(observer._itemAddedIndex, bmStartIndex); 1.144 + do_check_eq(observer._itemAddedParent, root); 1.145 + do_check_eq(observer._itemAddedId, folderId); 1.146 + 1.147 + txn.undoTransaction(); 1.148 + do_check_eq(observer._itemRemovedId, folderId); 1.149 + do_check_eq(observer._itemRemovedFolder, root); 1.150 + do_check_eq(observer._itemRemovedIndex, bmStartIndex); 1.151 + 1.152 + run_next_test(); 1.153 +}); 1.154 + 1.155 +add_test(function test_create_item() { 1.156 + let testURI = NetUtil.newURI("http://test_create_item.com"); 1.157 + 1.158 + let txn = new PlacesCreateBookmarkTransaction(testURI, root, bmStartIndex, 1.159 + "Test creating an item"); 1.160 + 1.161 + txnManager.doTransaction(txn); 1.162 + let id = bmsvc.getBookmarkIdsForURI(testURI)[0]; 1.163 + do_check_eq(observer._itemAddedId, id); 1.164 + do_check_eq(observer._itemAddedIndex, bmStartIndex); 1.165 + do_check_true(bmsvc.isBookmarked(testURI)); 1.166 + 1.167 + txn.undoTransaction(); 1.168 + do_check_eq(observer._itemRemovedId, id); 1.169 + do_check_eq(observer._itemRemovedIndex, bmStartIndex); 1.170 + do_check_false(bmsvc.isBookmarked(testURI)); 1.171 + 1.172 + txn.redoTransaction(); 1.173 + do_check_true(bmsvc.isBookmarked(testURI)); 1.174 + let newId = bmsvc.getBookmarkIdsForURI(testURI)[0]; 1.175 + do_check_eq(observer._itemAddedIndex, bmStartIndex); 1.176 + do_check_eq(observer._itemAddedParent, root); 1.177 + do_check_eq(observer._itemAddedId, newId); 1.178 + 1.179 + txn.undoTransaction(); 1.180 + do_check_eq(observer._itemRemovedId, newId); 1.181 + do_check_eq(observer._itemRemovedFolder, root); 1.182 + do_check_eq(observer._itemRemovedIndex, bmStartIndex); 1.183 + 1.184 + run_next_test(); 1.185 +}); 1.186 + 1.187 +add_test(function test_create_item_to_folder() { 1.188 + const TEST_FOLDERNAME = "Test creating item to a folder"; 1.189 + let testURI = NetUtil.newURI("http://test_create_item_to_folder.com"); 1.190 + let folderId = bmsvc.createFolder(root, TEST_FOLDERNAME, bmsvc.DEFAULT_INDEX); 1.191 + 1.192 + let txn = new PlacesCreateBookmarkTransaction(testURI, folderId, bmStartIndex, 1.193 + "Test creating item"); 1.194 + txnManager.doTransaction(txn); 1.195 + let bkmId = bmsvc.getBookmarkIdsForURI(testURI)[0]; 1.196 + do_check_eq(observer._itemAddedId, bkmId); 1.197 + do_check_eq(observer._itemAddedIndex, bmStartIndex); 1.198 + do_check_true(bmsvc.isBookmarked(testURI)); 1.199 + 1.200 + txn.undoTransaction(); 1.201 + do_check_eq(observer._itemRemovedId, bkmId); 1.202 + do_check_eq(observer._itemRemovedIndex, bmStartIndex); 1.203 + 1.204 + txn.redoTransaction(); 1.205 + let newBkmId = bmsvc.getBookmarkIdsForURI(testURI)[0]; 1.206 + do_check_eq(observer._itemAddedIndex, bmStartIndex); 1.207 + do_check_eq(observer._itemAddedParent, folderId); 1.208 + do_check_eq(observer._itemAddedId, newBkmId); 1.209 + 1.210 + txn.undoTransaction(); 1.211 + do_check_eq(observer._itemRemovedId, newBkmId); 1.212 + do_check_eq(observer._itemRemovedFolder, folderId); 1.213 + do_check_eq(observer._itemRemovedIndex, bmStartIndex); 1.214 + 1.215 + run_next_test(); 1.216 +}); 1.217 + 1.218 +add_test(function test_move_items_to_folder() { 1.219 + let testFolderId = bmsvc.createFolder(root, "Test move items", bmsvc.DEFAULT_INDEX); 1.220 + let testURI = NetUtil.newURI("http://test_move_items.com"); 1.221 + let testBkmId = bmsvc.insertBookmark(testFolderId, testURI, bmsvc.DEFAULT_INDEX, "1: Test move items"); 1.222 + bmsvc.insertBookmark(testFolderId, testURI, bmsvc.DEFAULT_INDEX, "2: Test move items"); 1.223 + 1.224 + // Moving items between the same folder 1.225 + let sameTxn = new PlacesMoveItemTransaction(testBkmId, testFolderId, bmsvc.DEFAULT_INDEX); 1.226 + 1.227 + sameTxn.doTransaction(); 1.228 + do_check_eq(observer._itemMovedId, testBkmId); 1.229 + do_check_eq(observer._itemMovedOldParent, testFolderId); 1.230 + do_check_eq(observer._itemMovedOldIndex, 0); 1.231 + do_check_eq(observer._itemMovedNewParent, testFolderId); 1.232 + do_check_eq(observer._itemMovedNewIndex, 1); 1.233 + 1.234 + sameTxn.undoTransaction(); 1.235 + do_check_eq(observer._itemMovedId, testBkmId); 1.236 + do_check_eq(observer._itemMovedOldParent, testFolderId); 1.237 + do_check_eq(observer._itemMovedOldIndex, 1); 1.238 + do_check_eq(observer._itemMovedNewParent, testFolderId); 1.239 + do_check_eq(observer._itemMovedNewIndex, 0); 1.240 + 1.241 + sameTxn.redoTransaction(); 1.242 + do_check_eq(observer._itemMovedId, testBkmId); 1.243 + do_check_eq(observer._itemMovedOldParent, testFolderId); 1.244 + do_check_eq(observer._itemMovedOldIndex, 0); 1.245 + do_check_eq(observer._itemMovedNewParent, testFolderId); 1.246 + do_check_eq(observer._itemMovedNewIndex, 1); 1.247 + 1.248 + sameTxn.undoTransaction(); 1.249 + do_check_eq(observer._itemMovedId, testBkmId); 1.250 + do_check_eq(observer._itemMovedOldParent, testFolderId); 1.251 + do_check_eq(observer._itemMovedOldIndex, 1); 1.252 + do_check_eq(observer._itemMovedNewParent, testFolderId); 1.253 + do_check_eq(observer._itemMovedNewIndex, 0); 1.254 + 1.255 + // Moving items between different folders 1.256 + let folderId = bmsvc.createFolder(testFolderId, 1.257 + "Test move items between different folders", 1.258 + bmsvc.DEFAULT_INDEX); 1.259 + let diffTxn = new PlacesMoveItemTransaction(testBkmId, folderId, bmsvc.DEFAULT_INDEX); 1.260 + 1.261 + diffTxn.doTransaction(); 1.262 + do_check_eq(observer._itemMovedId, testBkmId); 1.263 + do_check_eq(observer._itemMovedOldParent, testFolderId); 1.264 + do_check_eq(observer._itemMovedOldIndex, 0); 1.265 + do_check_eq(observer._itemMovedNewParent, folderId); 1.266 + do_check_eq(observer._itemMovedNewIndex, 0); 1.267 + 1.268 + sameTxn.undoTransaction(); 1.269 + do_check_eq(observer._itemMovedId, testBkmId); 1.270 + do_check_eq(observer._itemMovedOldParent, folderId); 1.271 + do_check_eq(observer._itemMovedOldIndex, 0); 1.272 + do_check_eq(observer._itemMovedNewParent, testFolderId); 1.273 + do_check_eq(observer._itemMovedNewIndex, 0); 1.274 + 1.275 + diffTxn.redoTransaction(); 1.276 + do_check_eq(observer._itemMovedId, testBkmId); 1.277 + do_check_eq(observer._itemMovedOldParent, testFolderId); 1.278 + do_check_eq(observer._itemMovedOldIndex, 0); 1.279 + do_check_eq(observer._itemMovedNewParent, folderId); 1.280 + do_check_eq(observer._itemMovedNewIndex, 0); 1.281 + 1.282 + sameTxn.undoTransaction(); 1.283 + do_check_eq(observer._itemMovedId, testBkmId); 1.284 + do_check_eq(observer._itemMovedOldParent, folderId); 1.285 + do_check_eq(observer._itemMovedOldIndex, 0); 1.286 + do_check_eq(observer._itemMovedNewParent, testFolderId); 1.287 + do_check_eq(observer._itemMovedNewIndex, 0); 1.288 + 1.289 + run_next_test(); 1.290 +}); 1.291 + 1.292 +add_test(function test_remove_folder() { 1.293 + let testFolder = bmsvc.createFolder(root, "Test Removing a Folder", bmsvc.DEFAULT_INDEX); 1.294 + let folderId = bmsvc.createFolder(testFolder, "Removed Folder", bmsvc.DEFAULT_INDEX); 1.295 + 1.296 + let txn = new PlacesRemoveItemTransaction(folderId); 1.297 + 1.298 + txn.doTransaction(); 1.299 + do_check_eq(observer._itemRemovedId, folderId); 1.300 + do_check_eq(observer._itemRemovedFolder, testFolder); 1.301 + do_check_eq(observer._itemRemovedIndex, 0); 1.302 + 1.303 + txn.undoTransaction(); 1.304 + do_check_eq(observer._itemAddedId, folderId); 1.305 + do_check_eq(observer._itemAddedParent, testFolder); 1.306 + do_check_eq(observer._itemAddedIndex, 0); 1.307 + 1.308 + txn.redoTransaction(); 1.309 + do_check_eq(observer._itemRemovedId, folderId); 1.310 + do_check_eq(observer._itemRemovedFolder, testFolder); 1.311 + do_check_eq(observer._itemRemovedIndex, 0); 1.312 + 1.313 + txn.undoTransaction(); 1.314 + do_check_eq(observer._itemAddedId, folderId); 1.315 + do_check_eq(observer._itemAddedParent, testFolder); 1.316 + do_check_eq(observer._itemAddedIndex, 0); 1.317 + 1.318 + run_next_test(); 1.319 +}); 1.320 + 1.321 +add_test(function test_remove_item_with_keyword_and_tag() { 1.322 + // Notice in this case the tag persists since other bookmarks have same uri. 1.323 + let testFolder = bmsvc.createFolder(root, "Test removing an item with a keyword and a tag", 1.324 + bmsvc.DEFAULT_INDEX); 1.325 + 1.326 + const KEYWORD = "test: test removing an item with a keyword and a tag"; 1.327 + const TAG_NAME = "tag-test_remove_item_with_keyword_and_tag"; 1.328 + let testURI = NetUtil.newURI("http://test_remove_item_with_keyword_and_tag.com"); 1.329 + let testBkmId = bmsvc.insertBookmark(testFolder, testURI, bmsvc.DEFAULT_INDEX, "test-item1"); 1.330 + 1.331 + // create bookmark for not removing tag. 1.332 + bmsvc.insertBookmark(testFolder, testURI, bmsvc.DEFAULT_INDEX, "test-item2"); 1.333 + 1.334 + // set tag & keyword 1.335 + bmsvc.setKeywordForBookmark(testBkmId, KEYWORD); 1.336 + tagssvc.tagURI(testURI, [TAG_NAME]); 1.337 + 1.338 + let txn = new PlacesRemoveItemTransaction(testBkmId); 1.339 + 1.340 + txn.doTransaction(); 1.341 + do_check_eq(observer._itemRemovedId, testBkmId); 1.342 + do_check_eq(observer._itemRemovedFolder, testFolder); 1.343 + do_check_eq(observer._itemRemovedIndex, 0); 1.344 + do_check_eq(bmsvc.getKeywordForBookmark(testBkmId), null); 1.345 + do_check_eq(tagssvc.getTagsForURI(testURI)[0], TAG_NAME); 1.346 + 1.347 + txn.undoTransaction(); 1.348 + let newbkmk2Id = observer._itemAddedId; 1.349 + do_check_eq(observer._itemAddedParent, testFolder); 1.350 + do_check_eq(observer._itemAddedIndex, 0); 1.351 + do_check_eq(bmsvc.getKeywordForBookmark(newbkmk2Id), KEYWORD); 1.352 + do_check_eq(tagssvc.getTagsForURI(testURI)[0], TAG_NAME); 1.353 + 1.354 + txn.redoTransaction(); 1.355 + do_check_eq(observer._itemRemovedId, newbkmk2Id); 1.356 + do_check_eq(observer._itemRemovedFolder, testFolder); 1.357 + do_check_eq(observer._itemRemovedIndex, 0); 1.358 + do_check_eq(bmsvc.getKeywordForBookmark(newbkmk2Id), null); 1.359 + do_check_eq(tagssvc.getTagsForURI(testURI)[0], TAG_NAME); 1.360 + 1.361 + txn.undoTransaction(); 1.362 + do_check_eq(observer._itemAddedParent, testFolder); 1.363 + do_check_eq(observer._itemAddedIndex, 0); 1.364 + do_check_eq(tagssvc.getTagsForURI(testURI)[0], TAG_NAME); 1.365 + 1.366 + run_next_test(); 1.367 +}); 1.368 + 1.369 +add_test(function test_remove_item_with_tag() { 1.370 + const TAG_NAME = "tag-test_remove_item_with_tag"; 1.371 + let testURI = NetUtil.newURI("http://test_remove_item_with_tag.com/"); 1.372 + let itemId = bmsvc.insertBookmark(root, testURI, bmsvc.DEFAULT_INDEX, "Test removing an item with a tag"); 1.373 + tagssvc.tagURI(testURI, [TAG_NAME]); 1.374 + 1.375 + let txn = new PlacesRemoveItemTransaction(itemId); 1.376 + 1.377 + txn.doTransaction(); 1.378 + do_check_true(tagssvc.getTagsForURI(testURI).length == 0); 1.379 + 1.380 + txn.undoTransaction(); 1.381 + do_check_eq(tagssvc.getTagsForURI(testURI)[0], TAG_NAME); 1.382 + 1.383 + txn.redoTransaction(); 1.384 + do_check_true(tagssvc.getTagsForURI(testURI).length == 0); 1.385 + 1.386 + txn.undoTransaction(); 1.387 + do_check_eq(tagssvc.getTagsForURI(testURI)[0], TAG_NAME); 1.388 + 1.389 + txn.redoTransaction(); 1.390 + 1.391 + run_next_test(); 1.392 +}); 1.393 + 1.394 +add_test(function test_creating_separator() { 1.395 + let testFolder = bmsvc.createFolder(root, "Test creating a separator", bmsvc.DEFAULT_INDEX); 1.396 + 1.397 + let txn = new PlacesCreateSeparatorTransaction(testFolder, 0); 1.398 + txn.doTransaction(); 1.399 + 1.400 + let sepId = observer._itemAddedId; 1.401 + do_check_eq(observer._itemAddedIndex, 0); 1.402 + do_check_eq(observer._itemAddedParent, testFolder); 1.403 + 1.404 + txn.undoTransaction(); 1.405 + do_check_eq(observer._itemRemovedId, sepId); 1.406 + do_check_eq(observer._itemRemovedFolder, testFolder); 1.407 + do_check_eq(observer._itemRemovedIndex, 0); 1.408 + 1.409 + txn.redoTransaction(); 1.410 + let newSepId = observer._itemAddedId; 1.411 + do_check_eq(observer._itemAddedIndex, 0); 1.412 + do_check_eq(observer._itemAddedParent, testFolder); 1.413 + 1.414 + txn.undoTransaction(); 1.415 + do_check_eq(observer._itemRemovedId, newSepId); 1.416 + do_check_eq(observer._itemRemovedFolder, testFolder); 1.417 + do_check_eq(observer._itemRemovedIndex, 0); 1.418 + 1.419 + run_next_test(); 1.420 +}); 1.421 + 1.422 +add_test(function test_removing_separator() { 1.423 + let testFolder = bmsvc.createFolder(root, "Test removing a separator", bmsvc.DEFAULT_INDEX); 1.424 + 1.425 + let sepId = bmsvc.insertSeparator(testFolder, 0); 1.426 + let txn = new PlacesRemoveItemTransaction(sepId); 1.427 + 1.428 + txn.doTransaction(); 1.429 + do_check_eq(observer._itemRemovedId, sepId); 1.430 + do_check_eq(observer._itemRemovedFolder, testFolder); 1.431 + do_check_eq(observer._itemRemovedIndex, 0); 1.432 + 1.433 + txn.undoTransaction(); 1.434 + do_check_eq(observer._itemAddedId, sepId); //New separator created 1.435 + do_check_eq(observer._itemAddedParent, testFolder); 1.436 + do_check_eq(observer._itemAddedIndex, 0); 1.437 + 1.438 + txn.redoTransaction(); 1.439 + do_check_eq(observer._itemRemovedId, sepId); 1.440 + do_check_eq(observer._itemRemovedFolder, testFolder); 1.441 + do_check_eq(observer._itemRemovedIndex, 0); 1.442 + 1.443 + txn.undoTransaction(); 1.444 + do_check_eq(observer._itemAddedId, sepId); //New separator created 1.445 + do_check_eq(observer._itemAddedParent, testFolder); 1.446 + do_check_eq(observer._itemAddedIndex, 0); 1.447 + 1.448 + run_next_test(); 1.449 +}); 1.450 + 1.451 +add_test(function test_editing_item_title() { 1.452 + const TITLE = "Test editing item title"; 1.453 + const MOD_TITLE = "Mod: Test editing item title"; 1.454 + let testURI = NetUtil.newURI("http://www.test_editing_item_title.com"); 1.455 + let testBkmId = bmsvc.insertBookmark(root, testURI, bmsvc.DEFAULT_INDEX, TITLE); 1.456 + 1.457 + let txn = new PlacesEditItemTitleTransaction(testBkmId, MOD_TITLE); 1.458 + 1.459 + txn.doTransaction(); 1.460 + do_check_eq(observer._itemChangedId, testBkmId); 1.461 + do_check_eq(observer._itemChangedProperty, "title"); 1.462 + do_check_eq(observer._itemChangedValue, MOD_TITLE); 1.463 + 1.464 + txn.undoTransaction(); 1.465 + do_check_eq(observer._itemChangedId, testBkmId); 1.466 + do_check_eq(observer._itemChangedProperty, "title"); 1.467 + do_check_eq(observer._itemChangedValue, TITLE); 1.468 + 1.469 + txn.redoTransaction(); 1.470 + do_check_eq(observer._itemChangedId, testBkmId); 1.471 + do_check_eq(observer._itemChangedProperty, "title"); 1.472 + do_check_eq(observer._itemChangedValue, MOD_TITLE); 1.473 + 1.474 + txn.undoTransaction(); 1.475 + do_check_eq(observer._itemChangedId, testBkmId); 1.476 + do_check_eq(observer._itemChangedProperty, "title"); 1.477 + do_check_eq(observer._itemChangedValue, TITLE); 1.478 + 1.479 + run_next_test(); 1.480 +}); 1.481 + 1.482 +add_test(function test_editing_item_uri() { 1.483 + const OLD_TEST_URI = NetUtil.newURI("http://old.test_editing_item_uri.com/"); 1.484 + const NEW_TEST_URI = NetUtil.newURI("http://new.test_editing_item_uri.com/"); 1.485 + let testBkmId = bmsvc.insertBookmark(root, OLD_TEST_URI, bmsvc.DEFAULT_INDEX, 1.486 + "Test editing item title"); 1.487 + tagssvc.tagURI(OLD_TEST_URI, ["tag"]); 1.488 + 1.489 + let txn = new PlacesEditBookmarkURITransaction(testBkmId, NEW_TEST_URI); 1.490 + 1.491 + txn.doTransaction(); 1.492 + do_check_eq(observer._itemChangedId, testBkmId); 1.493 + do_check_eq(observer._itemChangedProperty, "uri"); 1.494 + do_check_eq(observer._itemChangedValue, NEW_TEST_URI.spec); 1.495 + do_check_eq(JSON.stringify(tagssvc.getTagsForURI(NEW_TEST_URI)), JSON.stringify(["tag"])); 1.496 + do_check_eq(JSON.stringify(tagssvc.getTagsForURI(OLD_TEST_URI)), JSON.stringify([])); 1.497 + 1.498 + txn.undoTransaction(); 1.499 + do_check_eq(observer._itemChangedId, testBkmId); 1.500 + do_check_eq(observer._itemChangedProperty, "uri"); 1.501 + do_check_eq(observer._itemChangedValue, OLD_TEST_URI.spec); 1.502 + do_check_eq(JSON.stringify(tagssvc.getTagsForURI(OLD_TEST_URI)), JSON.stringify(["tag"])); 1.503 + do_check_eq(JSON.stringify(tagssvc.getTagsForURI(NEW_TEST_URI)), JSON.stringify([])); 1.504 + 1.505 + txn.redoTransaction(); 1.506 + do_check_eq(observer._itemChangedId, testBkmId); 1.507 + do_check_eq(observer._itemChangedProperty, "uri"); 1.508 + do_check_eq(observer._itemChangedValue, NEW_TEST_URI.spec); 1.509 + do_check_eq(JSON.stringify(tagssvc.getTagsForURI(NEW_TEST_URI)), JSON.stringify(["tag"])); 1.510 + do_check_eq(JSON.stringify(tagssvc.getTagsForURI(OLD_TEST_URI)), JSON.stringify([])); 1.511 + 1.512 + txn.undoTransaction(); 1.513 + do_check_eq(observer._itemChangedId, testBkmId); 1.514 + do_check_eq(observer._itemChangedProperty, "uri"); 1.515 + do_check_eq(observer._itemChangedValue, OLD_TEST_URI.spec); 1.516 + do_check_eq(JSON.stringify(tagssvc.getTagsForURI(OLD_TEST_URI)), JSON.stringify(["tag"])); 1.517 + do_check_eq(JSON.stringify(tagssvc.getTagsForURI(NEW_TEST_URI)), JSON.stringify([])); 1.518 + 1.519 + run_next_test(); 1.520 +}); 1.521 + 1.522 +add_test(function test_edit_description_transaction() { 1.523 + let testURI = NetUtil.newURI("http://test_edit_description_transaction.com"); 1.524 + let testBkmId = bmsvc.insertBookmark(root, testURI, bmsvc.DEFAULT_INDEX, "Test edit description transaction"); 1.525 + 1.526 + let anno = { 1.527 + name: DESCRIPTION_ANNO, 1.528 + type: Ci.nsIAnnotationService.TYPE_STRING, 1.529 + flags: 0, 1.530 + value: "Test edit Description", 1.531 + expires: Ci.nsIAnnotationService.EXPIRE_NEVER, 1.532 + }; 1.533 + let txn = new PlacesSetItemAnnotationTransaction(testBkmId, anno); 1.534 + 1.535 + txn.doTransaction(); 1.536 + do_check_eq(observer._itemChangedId, testBkmId); 1.537 + do_check_eq(observer._itemChangedProperty, DESCRIPTION_ANNO); 1.538 + 1.539 + run_next_test(); 1.540 +}); 1.541 + 1.542 +add_test(function test_edit_keyword() { 1.543 + const KEYWORD = "keyword-test_edit_keyword"; 1.544 + 1.545 + let testURI = NetUtil.newURI("http://test_edit_keyword.com"); 1.546 + let testBkmId = bmsvc.insertBookmark(root, testURI, bmsvc.DEFAULT_INDEX, "Test edit keyword"); 1.547 + 1.548 + let txn = new PlacesEditBookmarkKeywordTransaction(testBkmId, KEYWORD); 1.549 + 1.550 + txn.doTransaction(); 1.551 + do_check_eq(observer._itemChangedId, testBkmId); 1.552 + do_check_eq(observer._itemChangedProperty, "keyword"); 1.553 + do_check_eq(observer._itemChangedValue, KEYWORD); 1.554 + 1.555 + txn.undoTransaction(); 1.556 + do_check_eq(observer._itemChangedId, testBkmId); 1.557 + do_check_eq(observer._itemChangedProperty, "keyword"); 1.558 + do_check_eq(observer._itemChangedValue, ""); 1.559 + 1.560 + run_next_test(); 1.561 +}); 1.562 + 1.563 +add_test(function test_LoadInSidebar_transaction() { 1.564 + let testURI = NetUtil.newURI("http://test_LoadInSidebar_transaction.com"); 1.565 + let testBkmId = bmsvc.insertBookmark(root, testURI, bmsvc.DEFAULT_INDEX, "Test LoadInSidebar transaction"); 1.566 + 1.567 + const LOAD_IN_SIDEBAR_ANNO = "bookmarkProperties/loadInSidebar"; 1.568 + let anno = { name: LOAD_IN_SIDEBAR_ANNO, 1.569 + type: Ci.nsIAnnotationService.TYPE_INT32, 1.570 + flags: 0, 1.571 + value: true, 1.572 + expires: Ci.nsIAnnotationService.EXPIRE_NEVER }; 1.573 + let txn = new PlacesSetItemAnnotationTransaction(testBkmId, anno); 1.574 + 1.575 + txn.doTransaction(); 1.576 + do_check_eq(observer._itemChangedId, testBkmId); 1.577 + do_check_eq(observer._itemChangedProperty, LOAD_IN_SIDEBAR_ANNO); 1.578 + do_check_eq(observer._itemChanged_isAnnotationProperty, true); 1.579 + 1.580 + txn.undoTransaction(); 1.581 + do_check_eq(observer._itemChangedId, testBkmId); 1.582 + do_check_eq(observer._itemChangedProperty, LOAD_IN_SIDEBAR_ANNO); 1.583 + do_check_eq(observer._itemChanged_isAnnotationProperty, true); 1.584 + 1.585 + run_next_test(); 1.586 +}); 1.587 + 1.588 +add_test(function test_generic_item_annotation() { 1.589 + let testURI = NetUtil.newURI("http://test_generic_item_annotation.com"); 1.590 + let testBkmId = bmsvc.insertBookmark(root, testURI, bmsvc.DEFAULT_INDEX, "Test generic item annotation"); 1.591 + 1.592 + let itemAnnoObj = { name: "testAnno/testInt", 1.593 + type: Ci.nsIAnnotationService.TYPE_INT32, 1.594 + flags: 0, 1.595 + value: 123, 1.596 + expires: Ci.nsIAnnotationService.EXPIRE_NEVER }; 1.597 + let txn = new PlacesSetItemAnnotationTransaction(testBkmId, itemAnnoObj); 1.598 + 1.599 + txn.doTransaction(); 1.600 + do_check_eq(observer._itemChangedId, testBkmId); 1.601 + do_check_eq(observer._itemChangedProperty, "testAnno/testInt"); 1.602 + do_check_eq(observer._itemChanged_isAnnotationProperty, true); 1.603 + 1.604 + txn.undoTransaction(); 1.605 + do_check_eq(observer._itemChangedId, testBkmId); 1.606 + do_check_eq(observer._itemChangedProperty, "testAnno/testInt"); 1.607 + do_check_eq(observer._itemChanged_isAnnotationProperty, true); 1.608 + 1.609 + txn.redoTransaction(); 1.610 + do_check_eq(observer._itemChangedId, testBkmId); 1.611 + do_check_eq(observer._itemChangedProperty, "testAnno/testInt"); 1.612 + do_check_eq(observer._itemChanged_isAnnotationProperty, true); 1.613 + 1.614 + run_next_test(); 1.615 +}); 1.616 + 1.617 +add_test(function test_editing_item_date_added() { 1.618 + let testURI = NetUtil.newURI("http://test_editing_item_date_added.com"); 1.619 + let testBkmId = bmsvc.insertBookmark(root, testURI, bmsvc.DEFAULT_INDEX, 1.620 + "Test editing item date added"); 1.621 + 1.622 + let oldAdded = bmsvc.getItemDateAdded(testBkmId); 1.623 + let newAdded = Date.now() + 1000; 1.624 + let txn = new PlacesEditItemDateAddedTransaction(testBkmId, newAdded); 1.625 + 1.626 + txn.doTransaction(); 1.627 + do_check_eq(newAdded, bmsvc.getItemDateAdded(testBkmId)); 1.628 + 1.629 + txn.undoTransaction(); 1.630 + do_check_eq(oldAdded, bmsvc.getItemDateAdded(testBkmId)); 1.631 + 1.632 + run_next_test(); 1.633 +}); 1.634 + 1.635 +add_test(function test_edit_item_last_modified() { 1.636 + let testURI = NetUtil.newURI("http://test_edit_item_last_modified.com"); 1.637 + let testBkmId = bmsvc.insertBookmark(root, testURI, bmsvc.DEFAULT_INDEX, 1.638 + "Test editing item last modified"); 1.639 + 1.640 + let oldModified = bmsvc.getItemLastModified(testBkmId); 1.641 + let newModified = Date.now() + 1000; 1.642 + let txn = new PlacesEditItemLastModifiedTransaction(testBkmId, newModified); 1.643 + 1.644 + txn.doTransaction(); 1.645 + do_check_eq(newModified, bmsvc.getItemLastModified(testBkmId)); 1.646 + 1.647 + txn.undoTransaction(); 1.648 + do_check_eq(oldModified, bmsvc.getItemLastModified(testBkmId)); 1.649 + 1.650 + run_next_test(); 1.651 +}); 1.652 + 1.653 +add_test(function test_generic_page_annotation() { 1.654 + const TEST_ANNO = "testAnno/testInt"; 1.655 + let testURI = NetUtil.newURI("http://www.mozilla.org/"); 1.656 + promiseAddVisits(testURI).then(function () { 1.657 + let pageAnnoObj = { name: TEST_ANNO, 1.658 + type: Ci.nsIAnnotationService.TYPE_INT32, 1.659 + flags: 0, 1.660 + value: 123, 1.661 + expires: Ci.nsIAnnotationService.EXPIRE_NEVER }; 1.662 + let txn = new PlacesSetPageAnnotationTransaction(testURI, pageAnnoObj); 1.663 + 1.664 + txn.doTransaction(); 1.665 + do_check_true(annosvc.pageHasAnnotation(testURI, TEST_ANNO)); 1.666 + 1.667 + txn.undoTransaction(); 1.668 + do_check_false(annosvc.pageHasAnnotation(testURI, TEST_ANNO)); 1.669 + 1.670 + txn.redoTransaction(); 1.671 + do_check_true(annosvc.pageHasAnnotation(testURI, TEST_ANNO)); 1.672 + 1.673 + run_next_test(); 1.674 + }); 1.675 +}); 1.676 + 1.677 +add_test(function test_sort_folder_by_name() { 1.678 + let testFolder = bmsvc.createFolder(root, "Test PlacesSortFolderByNameTransaction", 1.679 + bmsvc.DEFAULT_INDEX); 1.680 + let testURI = NetUtil.newURI("http://test_sort_folder_by_name.com"); 1.681 + 1.682 + bmsvc.insertBookmark(testFolder, testURI, bmsvc.DEFAULT_INDEX, "bookmark3"); 1.683 + bmsvc.insertBookmark(testFolder, testURI, bmsvc.DEFAULT_INDEX, "bookmark2"); 1.684 + bmsvc.insertBookmark(testFolder, testURI, bmsvc.DEFAULT_INDEX, "bookmark1"); 1.685 + 1.686 + let bkmIds = bmsvc.getBookmarkIdsForURI(testURI); 1.687 + bkmIds.sort(); 1.688 + 1.689 + let b1 = bkmIds[0]; 1.690 + let b2 = bkmIds[1]; 1.691 + let b3 = bkmIds[2]; 1.692 + 1.693 + do_check_eq(0, bmsvc.getItemIndex(b1)); 1.694 + do_check_eq(1, bmsvc.getItemIndex(b2)); 1.695 + do_check_eq(2, bmsvc.getItemIndex(b3)); 1.696 + 1.697 + let txn = new PlacesSortFolderByNameTransaction(testFolder); 1.698 + 1.699 + txn.doTransaction(); 1.700 + do_check_eq(2, bmsvc.getItemIndex(b1)); 1.701 + do_check_eq(1, bmsvc.getItemIndex(b2)); 1.702 + do_check_eq(0, bmsvc.getItemIndex(b3)); 1.703 + 1.704 + txn.undoTransaction(); 1.705 + do_check_eq(0, bmsvc.getItemIndex(b1)); 1.706 + do_check_eq(1, bmsvc.getItemIndex(b2)); 1.707 + do_check_eq(2, bmsvc.getItemIndex(b3)); 1.708 + 1.709 + txn.redoTransaction(); 1.710 + do_check_eq(2, bmsvc.getItemIndex(b1)); 1.711 + do_check_eq(1, bmsvc.getItemIndex(b2)); 1.712 + do_check_eq(0, bmsvc.getItemIndex(b3)); 1.713 + 1.714 + txn.undoTransaction(); 1.715 + do_check_eq(0, bmsvc.getItemIndex(b1)); 1.716 + do_check_eq(1, bmsvc.getItemIndex(b2)); 1.717 + do_check_eq(2, bmsvc.getItemIndex(b3)); 1.718 + 1.719 + run_next_test(); 1.720 +}); 1.721 + 1.722 +add_test(function test_edit_postData() { 1.723 + const POST_DATA_ANNO = "bookmarkProperties/POSTData"; 1.724 + let postData = "post-test_edit_postData"; 1.725 + let testURI = NetUtil.newURI("http://test_edit_postData.com"); 1.726 + let testBkmId = bmsvc.insertBookmark(root, testURI, bmsvc.DEFAULT_INDEX, "Test edit Post Data"); 1.727 + 1.728 + let txn = new PlacesEditBookmarkPostDataTransaction(testBkmId, postData); 1.729 + 1.730 + txn.doTransaction(); 1.731 + do_check_true(annosvc.itemHasAnnotation(testBkmId, POST_DATA_ANNO)); 1.732 + do_check_eq(annosvc.getItemAnnotation(testBkmId, POST_DATA_ANNO), postData); 1.733 + 1.734 + txn.undoTransaction(); 1.735 + do_check_false(annosvc.itemHasAnnotation(testBkmId, POST_DATA_ANNO)); 1.736 + 1.737 + run_next_test(); 1.738 +}); 1.739 + 1.740 +add_test(function test_tagURI_untagURI() { 1.741 + const TAG_1 = "tag-test_tagURI_untagURI-bar"; 1.742 + const TAG_2 = "tag-test_tagURI_untagURI-foo"; 1.743 + let tagURI = NetUtil.newURI("http://test_tagURI_untagURI.com"); 1.744 + 1.745 + // Test tagURI 1.746 + let tagTxn = new PlacesTagURITransaction(tagURI, [TAG_1, TAG_2]); 1.747 + 1.748 + tagTxn.doTransaction(); 1.749 + do_check_eq(JSON.stringify(tagssvc.getTagsForURI(tagURI)), JSON.stringify([TAG_1,TAG_2])); 1.750 + 1.751 + tagTxn.undoTransaction(); 1.752 + do_check_eq(tagssvc.getTagsForURI(tagURI).length, 0); 1.753 + 1.754 + tagTxn.redoTransaction(); 1.755 + do_check_eq(JSON.stringify(tagssvc.getTagsForURI(tagURI)), JSON.stringify([TAG_1,TAG_2])); 1.756 + 1.757 + // Test untagURI 1.758 + let untagTxn = new PlacesUntagURITransaction(tagURI, [TAG_1]); 1.759 + 1.760 + untagTxn.doTransaction(); 1.761 + do_check_eq(JSON.stringify(tagssvc.getTagsForURI(tagURI)), JSON.stringify([TAG_2])); 1.762 + 1.763 + untagTxn.undoTransaction(); 1.764 + do_check_eq(JSON.stringify(tagssvc.getTagsForURI(tagURI)), JSON.stringify([TAG_1,TAG_2])); 1.765 + 1.766 + untagTxn.redoTransaction(); 1.767 + do_check_eq(JSON.stringify(tagssvc.getTagsForURI(tagURI)), JSON.stringify([TAG_2])); 1.768 + 1.769 + run_next_test(); 1.770 +}); 1.771 + 1.772 +add_test(function test_aggregate_removeItem_Txn() { 1.773 + let testFolder = bmsvc.createFolder(root, "Test aggregate removeItem transaction", bmsvc.DEFAULT_INDEX); 1.774 + 1.775 + const TEST_URL = "http://test_aggregate_removeitem_txn.com/"; 1.776 + const FOLDERNAME = "Folder"; 1.777 + let testURI = NetUtil.newURI(TEST_URL); 1.778 + 1.779 + let bkmk1Id = bmsvc.insertBookmark(testFolder, testURI, 0, "Mozilla"); 1.780 + let bkmk2Id = bmsvc.insertSeparator(testFolder, 1); 1.781 + let bkmk3Id = bmsvc.createFolder(testFolder, FOLDERNAME, 2); 1.782 + 1.783 + let bkmk3_1Id = bmsvc.insertBookmark(bkmk3Id, testURI, 0, "Mozilla"); 1.784 + let bkmk3_2Id = bmsvc.insertSeparator(bkmk3Id, 1); 1.785 + let bkmk3_3Id = bmsvc.createFolder(bkmk3Id, FOLDERNAME, 2); 1.786 + 1.787 + let childTxn1 = new PlacesRemoveItemTransaction(bkmk1Id); 1.788 + let childTxn2 = new PlacesRemoveItemTransaction(bkmk2Id); 1.789 + let childTxn3 = new PlacesRemoveItemTransaction(bkmk3Id); 1.790 + let transactions = [childTxn1, childTxn2, childTxn3]; 1.791 + let txn = new PlacesAggregatedTransaction("RemoveItems", transactions); 1.792 + 1.793 + txn.doTransaction(); 1.794 + do_check_eq(bmsvc.getItemIndex(bkmk1Id), -1); 1.795 + do_check_eq(bmsvc.getItemIndex(bkmk2Id), -1); 1.796 + do_check_eq(bmsvc.getItemIndex(bkmk3Id), -1); 1.797 + do_check_eq(bmsvc.getItemIndex(bkmk3_1Id), -1); 1.798 + do_check_eq(bmsvc.getItemIndex(bkmk3_2Id), -1); 1.799 + do_check_eq(bmsvc.getItemIndex(bkmk3_3Id), -1); 1.800 + // Check last removed item id. 1.801 + do_check_eq(observer._itemRemovedId, bkmk3Id); 1.802 + 1.803 + txn.undoTransaction(); 1.804 + let newBkmk1Id = bmsvc.getIdForItemAt(testFolder, 0); 1.805 + let newBkmk2Id = bmsvc.getIdForItemAt(testFolder, 1); 1.806 + let newBkmk3Id = bmsvc.getIdForItemAt(testFolder, 2); 1.807 + let newBkmk3_1Id = bmsvc.getIdForItemAt(newBkmk3Id, 0); 1.808 + let newBkmk3_2Id = bmsvc.getIdForItemAt(newBkmk3Id, 1); 1.809 + let newBkmk3_3Id = bmsvc.getIdForItemAt(newBkmk3Id, 2); 1.810 + do_check_eq(bmsvc.getItemType(newBkmk1Id), bmsvc.TYPE_BOOKMARK); 1.811 + do_check_eq(bmsvc.getBookmarkURI(newBkmk1Id).spec, TEST_URL); 1.812 + do_check_eq(bmsvc.getItemType(newBkmk2Id), bmsvc.TYPE_SEPARATOR); 1.813 + do_check_eq(bmsvc.getItemType(newBkmk3Id), bmsvc.TYPE_FOLDER); 1.814 + do_check_eq(bmsvc.getItemTitle(newBkmk3Id), FOLDERNAME); 1.815 + do_check_eq(bmsvc.getFolderIdForItem(newBkmk3_1Id), newBkmk3Id); 1.816 + do_check_eq(bmsvc.getItemType(newBkmk3_1Id), bmsvc.TYPE_BOOKMARK); 1.817 + do_check_eq(bmsvc.getBookmarkURI(newBkmk3_1Id).spec, TEST_URL); 1.818 + do_check_eq(bmsvc.getFolderIdForItem(newBkmk3_2Id), newBkmk3Id); 1.819 + do_check_eq(bmsvc.getItemType(newBkmk3_2Id), bmsvc.TYPE_SEPARATOR); 1.820 + do_check_eq(bmsvc.getFolderIdForItem(newBkmk3_3Id), newBkmk3Id); 1.821 + do_check_eq(bmsvc.getItemType(newBkmk3_3Id), bmsvc.TYPE_FOLDER); 1.822 + do_check_eq(bmsvc.getItemTitle(newBkmk3_3Id), FOLDERNAME); 1.823 + // Check last added back item id. 1.824 + // Notice items are restored in reverse order. 1.825 + do_check_eq(observer._itemAddedId, newBkmk1Id); 1.826 + 1.827 + txn.redoTransaction(); 1.828 + do_check_eq(bmsvc.getItemIndex(newBkmk1Id), -1); 1.829 + do_check_eq(bmsvc.getItemIndex(newBkmk2Id), -1); 1.830 + do_check_eq(bmsvc.getItemIndex(newBkmk3Id), -1); 1.831 + do_check_eq(bmsvc.getItemIndex(newBkmk3_1Id), -1); 1.832 + do_check_eq(bmsvc.getItemIndex(newBkmk3_2Id), -1); 1.833 + do_check_eq(bmsvc.getItemIndex(newBkmk3_3Id), -1); 1.834 + // Check last removed item id. 1.835 + do_check_eq(observer._itemRemovedId, newBkmk3Id); 1.836 + 1.837 + txn.undoTransaction(); 1.838 + newBkmk1Id = bmsvc.getIdForItemAt(testFolder, 0); 1.839 + newBkmk2Id = bmsvc.getIdForItemAt(testFolder, 1); 1.840 + newBkmk3Id = bmsvc.getIdForItemAt(testFolder, 2); 1.841 + newBkmk3_1Id = bmsvc.getIdForItemAt(newBkmk3Id, 0); 1.842 + newBkmk3_2Id = bmsvc.getIdForItemAt(newBkmk3Id, 1); 1.843 + newBkmk3_3Id = bmsvc.getIdForItemAt(newBkmk3Id, 2); 1.844 + do_check_eq(bmsvc.getItemType(newBkmk1Id), bmsvc.TYPE_BOOKMARK); 1.845 + do_check_eq(bmsvc.getBookmarkURI(newBkmk1Id).spec, TEST_URL); 1.846 + do_check_eq(bmsvc.getItemType(newBkmk2Id), bmsvc.TYPE_SEPARATOR); 1.847 + do_check_eq(bmsvc.getItemType(newBkmk3Id), bmsvc.TYPE_FOLDER); 1.848 + do_check_eq(bmsvc.getItemTitle(newBkmk3Id), FOLDERNAME); 1.849 + do_check_eq(bmsvc.getFolderIdForItem(newBkmk3_1Id), newBkmk3Id); 1.850 + do_check_eq(bmsvc.getItemType(newBkmk3_1Id), bmsvc.TYPE_BOOKMARK); 1.851 + do_check_eq(bmsvc.getBookmarkURI(newBkmk3_1Id).spec, TEST_URL); 1.852 + do_check_eq(bmsvc.getFolderIdForItem(newBkmk3_2Id), newBkmk3Id); 1.853 + do_check_eq(bmsvc.getItemType(newBkmk3_2Id), bmsvc.TYPE_SEPARATOR); 1.854 + do_check_eq(bmsvc.getFolderIdForItem(newBkmk3_3Id), newBkmk3Id); 1.855 + do_check_eq(bmsvc.getItemType(newBkmk3_3Id), bmsvc.TYPE_FOLDER); 1.856 + do_check_eq(bmsvc.getItemTitle(newBkmk3_3Id), FOLDERNAME); 1.857 + // Check last added back item id. 1.858 + // Notice items are restored in reverse order. 1.859 + do_check_eq(observer._itemAddedId, newBkmk1Id); 1.860 + 1.861 + run_next_test(); 1.862 +}); 1.863 + 1.864 +add_test(function test_create_item_with_childTxn() { 1.865 + let testFolder = bmsvc.createFolder(root, "Test creating an item with childTxns", bmsvc.DEFAULT_INDEX); 1.866 + 1.867 + const BOOKMARK_TITLE = "parent item"; 1.868 + let testURI = NetUtil.newURI("http://test_create_item_with_childTxn.com"); 1.869 + let childTxns = []; 1.870 + let newDateAdded = Date.now() - 20000; 1.871 + let editDateAdddedTxn = new PlacesEditItemDateAddedTransaction(null, newDateAdded); 1.872 + childTxns.push(editDateAdddedTxn); 1.873 + 1.874 + let itemChildAnnoObj = { name: "testAnno/testInt", 1.875 + type: Ci.nsIAnnotationService.TYPE_INT32, 1.876 + flags: 0, 1.877 + value: 123, 1.878 + expires: Ci.nsIAnnotationService.EXPIRE_NEVER }; 1.879 + let annoTxn = new PlacesSetItemAnnotationTransaction(null, itemChildAnnoObj); 1.880 + childTxns.push(annoTxn); 1.881 + 1.882 + let itemWChildTxn = new PlacesCreateBookmarkTransaction(testURI, testFolder, bmStartIndex, 1.883 + BOOKMARK_TITLE, null, null, 1.884 + childTxns); 1.885 + try { 1.886 + txnManager.doTransaction(itemWChildTxn); 1.887 + let itemId = bmsvc.getBookmarkIdsForURI(testURI)[0]; 1.888 + do_check_eq(observer._itemAddedId, itemId); 1.889 + do_check_eq(newDateAdded, bmsvc.getItemDateAdded(itemId)); 1.890 + do_check_eq(observer._itemChangedProperty, "testAnno/testInt"); 1.891 + do_check_eq(observer._itemChanged_isAnnotationProperty, true); 1.892 + do_check_true(annosvc.itemHasAnnotation(itemId, itemChildAnnoObj.name)) 1.893 + do_check_eq(annosvc.getItemAnnotation(itemId, itemChildAnnoObj.name), itemChildAnnoObj.value); 1.894 + 1.895 + itemWChildTxn.undoTransaction(); 1.896 + do_check_eq(observer._itemRemovedId, itemId); 1.897 + 1.898 + itemWChildTxn.redoTransaction(); 1.899 + do_check_true(bmsvc.isBookmarked(testURI)); 1.900 + let newId = bmsvc.getBookmarkIdsForURI(testURI)[0]; 1.901 + do_check_eq(newDateAdded, bmsvc.getItemDateAdded(newId)); 1.902 + do_check_eq(observer._itemAddedId, newId); 1.903 + do_check_eq(observer._itemChangedProperty, "testAnno/testInt"); 1.904 + do_check_eq(observer._itemChanged_isAnnotationProperty, true); 1.905 + do_check_true(annosvc.itemHasAnnotation(newId, itemChildAnnoObj.name)) 1.906 + do_check_eq(annosvc.getItemAnnotation(newId, itemChildAnnoObj.name), itemChildAnnoObj.value); 1.907 + 1.908 + itemWChildTxn.undoTransaction(); 1.909 + do_check_eq(observer._itemRemovedId, newId); 1.910 + } 1.911 + catch (ex) { 1.912 + do_throw("Setting a child transaction in a createItem transaction did throw: " + ex); 1.913 + } 1.914 + 1.915 + run_next_test(); 1.916 +}); 1.917 + 1.918 +add_test(function test_create_folder_with_child_itemTxn() { 1.919 + let childURI = NetUtil.newURI("http://test_create_folder_with_child_itemTxn.com"); 1.920 + let childItemTxn = new PlacesCreateBookmarkTransaction(childURI, root, 1.921 + bmStartIndex, "childItem"); 1.922 + let txn = new PlacesCreateFolderTransaction("Test creating a folder with child itemTxns", 1.923 + root, bmStartIndex, null, [childItemTxn]); 1.924 + try { 1.925 + txnManager.doTransaction(txn); 1.926 + let childItemId = bmsvc.getBookmarkIdsForURI(childURI)[0]; 1.927 + do_check_eq(observer._itemAddedId, childItemId); 1.928 + do_check_eq(observer._itemAddedIndex, 0); 1.929 + do_check_true(bmsvc.isBookmarked(childURI)); 1.930 + 1.931 + txn.undoTransaction(); 1.932 + do_check_false(bmsvc.isBookmarked(childURI)); 1.933 + 1.934 + txn.redoTransaction(); 1.935 + let newchildItemId = bmsvc.getBookmarkIdsForURI(childURI)[0]; 1.936 + do_check_eq(observer._itemAddedIndex, 0); 1.937 + do_check_eq(observer._itemAddedId, newchildItemId); 1.938 + do_check_true(bmsvc.isBookmarked(childURI)); 1.939 + 1.940 + txn.undoTransaction(); 1.941 + do_check_false(bmsvc.isBookmarked(childURI)); 1.942 + } 1.943 + catch (ex) { 1.944 + do_throw("Setting a child item transaction in a createFolder transaction did throw: " + ex); 1.945 + } 1.946 + 1.947 + run_next_test(); 1.948 +});