1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/browser/components/places/tests/unit/test_PUIU_makeTransaction.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,351 @@ 1.4 +/* Any copyright is dedicated to the Public Domain. 1.5 + http://creativecommons.org/publicdomain/zero/1.0/ */ 1.6 + 1.7 +function waitForBookmarkNotification(aNotification, aCallback, aProperty) 1.8 +{ 1.9 + PlacesUtils.bookmarks.addObserver({ 1.10 + validate: function (aMethodName, aData) 1.11 + { 1.12 + if (aMethodName == aNotification && 1.13 + (!aProperty || aProperty == aData.property)) { 1.14 + PlacesUtils.bookmarks.removeObserver(this); 1.15 + aCallback(aData); 1.16 + } 1.17 + }, 1.18 + 1.19 + // nsINavBookmarkObserver 1.20 + QueryInterface: XPCOMUtils.generateQI([Ci.nsINavBookmarkObserver]), 1.21 + onBeginUpdateBatch: function onBeginUpdateBatch() 1.22 + this.validate(arguments.callee.name, arguments), 1.23 + onEndUpdateBatch: function onEndUpdateBatch() 1.24 + this.validate(arguments.callee.name, arguments), 1.25 + onItemAdded: function onItemAdded(aItemId, aParentId, aIndex, aItemType, 1.26 + aURI, aTitle) 1.27 + { 1.28 + return this.validate(arguments.callee.name, { id: aItemId, 1.29 + index: aIndex, 1.30 + type: aItemType, 1.31 + url: aURI ? aURI.spec : null, 1.32 + title: aTitle }); 1.33 + }, 1.34 + onItemRemoved: function onItemRemoved() 1.35 + this.validate(arguments.callee.name, arguments), 1.36 + onItemChanged: function onItemChanged(aItemId, aProperty, aIsAnno, 1.37 + aNewValue, aLastModified, aItemType) 1.38 + { 1.39 + return this.validate(arguments.callee.name, 1.40 + { id: aItemId, 1.41 + get index() PlacesUtils.bookmarks.getItemIndex(this.id), 1.42 + type: aItemType, 1.43 + property: aProperty, 1.44 + get url() aItemType == PlacesUtils.bookmarks.TYPE_BOOKMARK ? 1.45 + PlacesUtils.bookmarks.getBookmarkURI(this.id).spec : 1.46 + null, 1.47 + get title() PlacesUtils.bookmarks.getItemTitle(this.id), 1.48 + }); 1.49 + }, 1.50 + onItemVisited: function onItemVisited() 1.51 + this.validate(arguments.callee.name, arguments), 1.52 + onItemMoved: function onItemMoved(aItemId, aOldParentId, aOldIndex, 1.53 + aNewParentId, aNewIndex, aItemType) 1.54 + { 1.55 + this.validate(arguments.callee.name, { id: aItemId, 1.56 + index: aNewIndex, 1.57 + type: aItemType }); 1.58 + } 1.59 + }, false); 1.60 +} 1.61 + 1.62 +function wrapNodeByIdAndParent(aItemId, aParentId) 1.63 +{ 1.64 + let wrappedNode; 1.65 + let root = PlacesUtils.getFolderContents(aParentId, false, false).root; 1.66 + for (let i = 0; i < root.childCount; ++i) { 1.67 + let node = root.getChild(i); 1.68 + if (node.itemId == aItemId) { 1.69 + let type; 1.70 + if (PlacesUtils.nodeIsContainer(node)) { 1.71 + type = PlacesUtils.TYPE_X_MOZ_PLACE_CONTAINER; 1.72 + } 1.73 + else if (PlacesUtils.nodeIsURI(node)) { 1.74 + type = PlacesUtils.TYPE_X_MOZ_PLACE; 1.75 + } 1.76 + else if (PlacesUtils.nodeIsSeparator(node)) { 1.77 + type = PlacesUtils.TYPE_X_MOZ_PLACE_SEPARATOR; 1.78 + } 1.79 + else { 1.80 + do_throw("Unknown node type"); 1.81 + } 1.82 + wrappedNode = PlacesUtils.wrapNode(node, type); 1.83 + } 1.84 + } 1.85 + root.containerOpen = false; 1.86 + return JSON.parse(wrappedNode); 1.87 +} 1.88 + 1.89 +add_test(function test_text_paste() 1.90 +{ 1.91 + const TEST_URL = "http://places.moz.org/" 1.92 + const TEST_TITLE = "Places bookmark" 1.93 + 1.94 + waitForBookmarkNotification("onItemAdded", function(aData) 1.95 + { 1.96 + do_check_eq(aData.title, TEST_TITLE); 1.97 + do_check_eq(aData.url, TEST_URL); 1.98 + do_check_eq(aData.type, PlacesUtils.bookmarks.TYPE_BOOKMARK); 1.99 + do_check_eq(aData.index, 0); 1.100 + run_next_test(); 1.101 + }); 1.102 + 1.103 + let txn = PlacesUIUtils.makeTransaction( 1.104 + { title: TEST_TITLE, uri: TEST_URL }, 1.105 + PlacesUtils.TYPE_X_MOZ_URL, 1.106 + PlacesUtils.unfiledBookmarksFolderId, 1.107 + PlacesUtils.bookmarks.DEFAULT_INDEX, 1.108 + true // Unused for text. 1.109 + ); 1.110 + PlacesUtils.transactionManager.doTransaction(txn); 1.111 +}); 1.112 + 1.113 +add_test(function test_container() 1.114 +{ 1.115 + const TEST_TITLE = "Places folder" 1.116 + 1.117 + waitForBookmarkNotification("onItemChanged", function(aData) 1.118 + { 1.119 + do_check_eq(aData.title, TEST_TITLE); 1.120 + do_check_eq(aData.type, PlacesUtils.bookmarks.TYPE_FOLDER); 1.121 + do_check_eq(aData.index, 1); 1.122 + 1.123 + waitForBookmarkNotification("onItemAdded", function(aData) 1.124 + { 1.125 + do_check_eq(aData.title, TEST_TITLE); 1.126 + do_check_eq(aData.type, PlacesUtils.bookmarks.TYPE_FOLDER); 1.127 + do_check_eq(aData.index, 2); 1.128 + let id = aData.id; 1.129 + 1.130 + waitForBookmarkNotification("onItemMoved", function(aData) 1.131 + { 1.132 + do_check_eq(aData.id, id); 1.133 + do_check_eq(aData.type, PlacesUtils.bookmarks.TYPE_FOLDER); 1.134 + do_check_eq(aData.index, 1); 1.135 + 1.136 + run_next_test(); 1.137 + }); 1.138 + 1.139 + let txn = PlacesUIUtils.makeTransaction( 1.140 + wrapNodeByIdAndParent(aData.id, PlacesUtils.unfiledBookmarksFolderId), 1.141 + 0, // Unused for real nodes. 1.142 + PlacesUtils.unfiledBookmarksFolderId, 1.143 + 1, // Move to position 1. 1.144 + false 1.145 + ); 1.146 + PlacesUtils.transactionManager.doTransaction(txn); 1.147 + }); 1.148 + 1.149 + try { 1.150 + let txn = PlacesUIUtils.makeTransaction( 1.151 + wrapNodeByIdAndParent(aData.id, PlacesUtils.unfiledBookmarksFolderId), 1.152 + 0, // Unused for real nodes. 1.153 + PlacesUtils.unfiledBookmarksFolderId, 1.154 + PlacesUtils.bookmarks.DEFAULT_INDEX, 1.155 + true 1.156 + ); 1.157 + PlacesUtils.transactionManager.doTransaction(txn); 1.158 + } catch(ex) { 1.159 + do_throw(ex); 1.160 + } 1.161 + }, "random-anno"); 1.162 + 1.163 + let id = PlacesUtils.bookmarks.createFolder(PlacesUtils.unfiledBookmarksFolderId, 1.164 + TEST_TITLE, 1.165 + PlacesUtils.bookmarks.DEFAULT_INDEX); 1.166 + PlacesUtils.annotations.setItemAnnotation(id, PlacesUIUtils.DESCRIPTION_ANNO, 1.167 + "description", 0, 1.168 + PlacesUtils.annotations.EXPIRE_NEVER); 1.169 + PlacesUtils.annotations.setItemAnnotation(id, "random-anno", 1.170 + "random-value", 0, 1.171 + PlacesUtils.annotations.EXPIRE_NEVER); 1.172 +}); 1.173 + 1.174 + 1.175 +add_test(function test_separator() 1.176 +{ 1.177 + waitForBookmarkNotification("onItemChanged", function(aData) 1.178 + { 1.179 + do_check_eq(aData.type, PlacesUtils.bookmarks.TYPE_SEPARATOR); 1.180 + do_check_eq(aData.index, 3); 1.181 + 1.182 + waitForBookmarkNotification("onItemAdded", function(aData) 1.183 + { 1.184 + do_check_eq(aData.type, PlacesUtils.bookmarks.TYPE_SEPARATOR); 1.185 + do_check_eq(aData.index, 4); 1.186 + let id = aData.id; 1.187 + 1.188 + waitForBookmarkNotification("onItemMoved", function(aData) 1.189 + { 1.190 + do_check_eq(aData.id, id); 1.191 + do_check_eq(aData.type, PlacesUtils.bookmarks.TYPE_SEPARATOR); 1.192 + do_check_eq(aData.index, 1); 1.193 + 1.194 + run_next_test(); 1.195 + }); 1.196 + 1.197 + let txn = PlacesUIUtils.makeTransaction( 1.198 + wrapNodeByIdAndParent(aData.id, PlacesUtils.unfiledBookmarksFolderId), 1.199 + 0, // Unused for real nodes. 1.200 + PlacesUtils.unfiledBookmarksFolderId, 1.201 + 1, // Move to position 1. 1.202 + false 1.203 + ); 1.204 + PlacesUtils.transactionManager.doTransaction(txn); 1.205 + }); 1.206 + 1.207 + try { 1.208 + let txn = PlacesUIUtils.makeTransaction( 1.209 + wrapNodeByIdAndParent(aData.id, PlacesUtils.unfiledBookmarksFolderId), 1.210 + 0, // Unused for real nodes. 1.211 + PlacesUtils.unfiledBookmarksFolderId, 1.212 + PlacesUtils.bookmarks.DEFAULT_INDEX, 1.213 + true 1.214 + ); 1.215 + PlacesUtils.transactionManager.doTransaction(txn); 1.216 + } catch(ex) { 1.217 + do_throw(ex); 1.218 + } 1.219 + }, "random-anno"); 1.220 + 1.221 + let id = PlacesUtils.bookmarks.insertSeparator(PlacesUtils.unfiledBookmarksFolderId, 1.222 + PlacesUtils.bookmarks.DEFAULT_INDEX); 1.223 + PlacesUtils.annotations.setItemAnnotation(id, "random-anno", 1.224 + "random-value", 0, 1.225 + PlacesUtils.annotations.EXPIRE_NEVER); 1.226 +}); 1.227 + 1.228 +add_test(function test_bookmark() 1.229 +{ 1.230 + const TEST_URL = "http://places.moz.org/" 1.231 + const TEST_TITLE = "Places bookmark" 1.232 + 1.233 + waitForBookmarkNotification("onItemChanged", function(aData) 1.234 + { 1.235 + do_check_eq(aData.title, TEST_TITLE); 1.236 + do_check_eq(aData.url, TEST_URL); 1.237 + do_check_eq(aData.type, PlacesUtils.bookmarks.TYPE_BOOKMARK); 1.238 + do_check_eq(aData.index, 5); 1.239 + 1.240 + waitForBookmarkNotification("onItemAdded", function(aData) 1.241 + { 1.242 + do_check_eq(aData.title, TEST_TITLE); 1.243 + do_check_eq(aData.url, TEST_URL); 1.244 + do_check_eq(aData.type, PlacesUtils.bookmarks.TYPE_BOOKMARK); 1.245 + do_check_eq(aData.index, 6); 1.246 + let id = aData.id; 1.247 + 1.248 + waitForBookmarkNotification("onItemMoved", function(aData) 1.249 + { 1.250 + do_check_eq(aData.id, id); 1.251 + do_check_eq(aData.type, PlacesUtils.bookmarks.TYPE_BOOKMARK); 1.252 + do_check_eq(aData.index, 1); 1.253 + 1.254 + run_next_test(); 1.255 + }); 1.256 + 1.257 + let txn = PlacesUIUtils.makeTransaction( 1.258 + wrapNodeByIdAndParent(aData.id, PlacesUtils.unfiledBookmarksFolderId), 1.259 + 0, // Unused for real nodes. 1.260 + PlacesUtils.unfiledBookmarksFolderId, 1.261 + 1, // Move to position 1. 1.262 + false 1.263 + ); 1.264 + PlacesUtils.transactionManager.doTransaction(txn); 1.265 + }); 1.266 + 1.267 + try { 1.268 + let txn = PlacesUIUtils.makeTransaction( 1.269 + wrapNodeByIdAndParent(aData.id, PlacesUtils.unfiledBookmarksFolderId), 1.270 + 0, // Unused for real nodes. 1.271 + PlacesUtils.unfiledBookmarksFolderId, 1.272 + PlacesUtils.bookmarks.DEFAULT_INDEX, 1.273 + true 1.274 + ); 1.275 + PlacesUtils.transactionManager.doTransaction(txn); 1.276 + } catch(ex) { 1.277 + do_throw(ex); 1.278 + } 1.279 + }, "random-anno"); 1.280 + 1.281 + let id = PlacesUtils.bookmarks.insertBookmark(PlacesUtils.unfiledBookmarksFolderId, 1.282 + NetUtil.newURI(TEST_URL), 1.283 + PlacesUtils.bookmarks.DEFAULT_INDEX, 1.284 + TEST_TITLE); 1.285 + PlacesUtils.annotations.setItemAnnotation(id, PlacesUIUtils.DESCRIPTION_ANNO, 1.286 + "description", 0, 1.287 + PlacesUtils.annotations.EXPIRE_NEVER); 1.288 + PlacesUtils.annotations.setItemAnnotation(id, "random-anno", 1.289 + "random-value", 0, 1.290 + PlacesUtils.annotations.EXPIRE_NEVER); 1.291 +}); 1.292 + 1.293 +add_test(function test_visit() 1.294 +{ 1.295 + const TEST_URL = "http://places.moz.org/" 1.296 + const TEST_TITLE = "Places bookmark" 1.297 + 1.298 + waitForBookmarkNotification("onItemAdded", function(aData) 1.299 + { 1.300 + do_check_eq(aData.title, TEST_TITLE); 1.301 + do_check_eq(aData.url, TEST_URL); 1.302 + do_check_eq(aData.type, PlacesUtils.bookmarks.TYPE_BOOKMARK); 1.303 + do_check_eq(aData.index, 7); 1.304 + 1.305 + waitForBookmarkNotification("onItemAdded", function(aData) 1.306 + { 1.307 + do_check_eq(aData.title, TEST_TITLE); 1.308 + do_check_eq(aData.url, TEST_URL); 1.309 + do_check_eq(aData.type, PlacesUtils.bookmarks.TYPE_BOOKMARK); 1.310 + do_check_eq(aData.index, 8); 1.311 + run_next_test(); 1.312 + }); 1.313 + 1.314 + try { 1.315 + let node = wrapNodeByIdAndParent(aData.id, PlacesUtils.unfiledBookmarksFolderId); 1.316 + // Simulate a not-bookmarked node, will copy it to a new bookmark. 1.317 + node.id = -1; 1.318 + let txn = PlacesUIUtils.makeTransaction( 1.319 + node, 1.320 + 0, // Unused for real nodes. 1.321 + PlacesUtils.unfiledBookmarksFolderId, 1.322 + PlacesUtils.bookmarks.DEFAULT_INDEX, 1.323 + true 1.324 + ); 1.325 + PlacesUtils.transactionManager.doTransaction(txn); 1.326 + } catch(ex) { 1.327 + do_throw(ex); 1.328 + } 1.329 + }); 1.330 + 1.331 + PlacesUtils.bookmarks.insertBookmark(PlacesUtils.unfiledBookmarksFolderId, 1.332 + NetUtil.newURI(TEST_URL), 1.333 + PlacesUtils.bookmarks.DEFAULT_INDEX, 1.334 + TEST_TITLE); 1.335 +}); 1.336 + 1.337 +add_test(function check_annotations() { 1.338 + // As last step check how many items for each annotation exist. 1.339 + 1.340 + // Copies should retain the description annotation. 1.341 + let descriptions = 1.342 + PlacesUtils.annotations.getItemsWithAnnotation(PlacesUIUtils.DESCRIPTION_ANNO, {}); 1.343 + do_check_eq(descriptions.length, 4); 1.344 + 1.345 + // Only the original bookmarks should have this annotation. 1.346 + let others = PlacesUtils.annotations.getItemsWithAnnotation("random-anno", {}); 1.347 + do_check_eq(others.length, 3); 1.348 + run_next_test(); 1.349 +}); 1.350 + 1.351 +function run_test() 1.352 +{ 1.353 + run_next_test(); 1.354 +}