Wed, 31 Dec 2014 06:55:50 +0100
Added tag UPSTREAM_283F7C6 for changeset ca08bd8f51b2
michael@0 | 1 | /* Any copyright is dedicated to the Public Domain. |
michael@0 | 2 | http://creativecommons.org/publicdomain/zero/1.0/ */ |
michael@0 | 3 | |
michael@0 | 4 | function waitForBookmarkNotification(aNotification, aCallback, aProperty) |
michael@0 | 5 | { |
michael@0 | 6 | PlacesUtils.bookmarks.addObserver({ |
michael@0 | 7 | validate: function (aMethodName, aData) |
michael@0 | 8 | { |
michael@0 | 9 | if (aMethodName == aNotification && |
michael@0 | 10 | (!aProperty || aProperty == aData.property)) { |
michael@0 | 11 | PlacesUtils.bookmarks.removeObserver(this); |
michael@0 | 12 | aCallback(aData); |
michael@0 | 13 | } |
michael@0 | 14 | }, |
michael@0 | 15 | |
michael@0 | 16 | // nsINavBookmarkObserver |
michael@0 | 17 | QueryInterface: XPCOMUtils.generateQI([Ci.nsINavBookmarkObserver]), |
michael@0 | 18 | onBeginUpdateBatch: function onBeginUpdateBatch() |
michael@0 | 19 | this.validate(arguments.callee.name, arguments), |
michael@0 | 20 | onEndUpdateBatch: function onEndUpdateBatch() |
michael@0 | 21 | this.validate(arguments.callee.name, arguments), |
michael@0 | 22 | onItemAdded: function onItemAdded(aItemId, aParentId, aIndex, aItemType, |
michael@0 | 23 | aURI, aTitle) |
michael@0 | 24 | { |
michael@0 | 25 | return this.validate(arguments.callee.name, { id: aItemId, |
michael@0 | 26 | index: aIndex, |
michael@0 | 27 | type: aItemType, |
michael@0 | 28 | url: aURI ? aURI.spec : null, |
michael@0 | 29 | title: aTitle }); |
michael@0 | 30 | }, |
michael@0 | 31 | onItemRemoved: function onItemRemoved() |
michael@0 | 32 | this.validate(arguments.callee.name, arguments), |
michael@0 | 33 | onItemChanged: function onItemChanged(aItemId, aProperty, aIsAnno, |
michael@0 | 34 | aNewValue, aLastModified, aItemType) |
michael@0 | 35 | { |
michael@0 | 36 | return this.validate(arguments.callee.name, |
michael@0 | 37 | { id: aItemId, |
michael@0 | 38 | get index() PlacesUtils.bookmarks.getItemIndex(this.id), |
michael@0 | 39 | type: aItemType, |
michael@0 | 40 | property: aProperty, |
michael@0 | 41 | get url() aItemType == PlacesUtils.bookmarks.TYPE_BOOKMARK ? |
michael@0 | 42 | PlacesUtils.bookmarks.getBookmarkURI(this.id).spec : |
michael@0 | 43 | null, |
michael@0 | 44 | get title() PlacesUtils.bookmarks.getItemTitle(this.id), |
michael@0 | 45 | }); |
michael@0 | 46 | }, |
michael@0 | 47 | onItemVisited: function onItemVisited() |
michael@0 | 48 | this.validate(arguments.callee.name, arguments), |
michael@0 | 49 | onItemMoved: function onItemMoved(aItemId, aOldParentId, aOldIndex, |
michael@0 | 50 | aNewParentId, aNewIndex, aItemType) |
michael@0 | 51 | { |
michael@0 | 52 | this.validate(arguments.callee.name, { id: aItemId, |
michael@0 | 53 | index: aNewIndex, |
michael@0 | 54 | type: aItemType }); |
michael@0 | 55 | } |
michael@0 | 56 | }, false); |
michael@0 | 57 | } |
michael@0 | 58 | |
michael@0 | 59 | function wrapNodeByIdAndParent(aItemId, aParentId) |
michael@0 | 60 | { |
michael@0 | 61 | let wrappedNode; |
michael@0 | 62 | let root = PlacesUtils.getFolderContents(aParentId, false, false).root; |
michael@0 | 63 | for (let i = 0; i < root.childCount; ++i) { |
michael@0 | 64 | let node = root.getChild(i); |
michael@0 | 65 | if (node.itemId == aItemId) { |
michael@0 | 66 | let type; |
michael@0 | 67 | if (PlacesUtils.nodeIsContainer(node)) { |
michael@0 | 68 | type = PlacesUtils.TYPE_X_MOZ_PLACE_CONTAINER; |
michael@0 | 69 | } |
michael@0 | 70 | else if (PlacesUtils.nodeIsURI(node)) { |
michael@0 | 71 | type = PlacesUtils.TYPE_X_MOZ_PLACE; |
michael@0 | 72 | } |
michael@0 | 73 | else if (PlacesUtils.nodeIsSeparator(node)) { |
michael@0 | 74 | type = PlacesUtils.TYPE_X_MOZ_PLACE_SEPARATOR; |
michael@0 | 75 | } |
michael@0 | 76 | else { |
michael@0 | 77 | do_throw("Unknown node type"); |
michael@0 | 78 | } |
michael@0 | 79 | wrappedNode = PlacesUtils.wrapNode(node, type); |
michael@0 | 80 | } |
michael@0 | 81 | } |
michael@0 | 82 | root.containerOpen = false; |
michael@0 | 83 | return JSON.parse(wrappedNode); |
michael@0 | 84 | } |
michael@0 | 85 | |
michael@0 | 86 | add_test(function test_text_paste() |
michael@0 | 87 | { |
michael@0 | 88 | const TEST_URL = "http://places.moz.org/" |
michael@0 | 89 | const TEST_TITLE = "Places bookmark" |
michael@0 | 90 | |
michael@0 | 91 | waitForBookmarkNotification("onItemAdded", function(aData) |
michael@0 | 92 | { |
michael@0 | 93 | do_check_eq(aData.title, TEST_TITLE); |
michael@0 | 94 | do_check_eq(aData.url, TEST_URL); |
michael@0 | 95 | do_check_eq(aData.type, PlacesUtils.bookmarks.TYPE_BOOKMARK); |
michael@0 | 96 | do_check_eq(aData.index, 0); |
michael@0 | 97 | run_next_test(); |
michael@0 | 98 | }); |
michael@0 | 99 | |
michael@0 | 100 | let txn = PlacesUIUtils.makeTransaction( |
michael@0 | 101 | { title: TEST_TITLE, uri: TEST_URL }, |
michael@0 | 102 | PlacesUtils.TYPE_X_MOZ_URL, |
michael@0 | 103 | PlacesUtils.unfiledBookmarksFolderId, |
michael@0 | 104 | PlacesUtils.bookmarks.DEFAULT_INDEX, |
michael@0 | 105 | true // Unused for text. |
michael@0 | 106 | ); |
michael@0 | 107 | PlacesUtils.transactionManager.doTransaction(txn); |
michael@0 | 108 | }); |
michael@0 | 109 | |
michael@0 | 110 | add_test(function test_container() |
michael@0 | 111 | { |
michael@0 | 112 | const TEST_TITLE = "Places folder" |
michael@0 | 113 | |
michael@0 | 114 | waitForBookmarkNotification("onItemChanged", function(aData) |
michael@0 | 115 | { |
michael@0 | 116 | do_check_eq(aData.title, TEST_TITLE); |
michael@0 | 117 | do_check_eq(aData.type, PlacesUtils.bookmarks.TYPE_FOLDER); |
michael@0 | 118 | do_check_eq(aData.index, 1); |
michael@0 | 119 | |
michael@0 | 120 | waitForBookmarkNotification("onItemAdded", function(aData) |
michael@0 | 121 | { |
michael@0 | 122 | do_check_eq(aData.title, TEST_TITLE); |
michael@0 | 123 | do_check_eq(aData.type, PlacesUtils.bookmarks.TYPE_FOLDER); |
michael@0 | 124 | do_check_eq(aData.index, 2); |
michael@0 | 125 | let id = aData.id; |
michael@0 | 126 | |
michael@0 | 127 | waitForBookmarkNotification("onItemMoved", function(aData) |
michael@0 | 128 | { |
michael@0 | 129 | do_check_eq(aData.id, id); |
michael@0 | 130 | do_check_eq(aData.type, PlacesUtils.bookmarks.TYPE_FOLDER); |
michael@0 | 131 | do_check_eq(aData.index, 1); |
michael@0 | 132 | |
michael@0 | 133 | run_next_test(); |
michael@0 | 134 | }); |
michael@0 | 135 | |
michael@0 | 136 | let txn = PlacesUIUtils.makeTransaction( |
michael@0 | 137 | wrapNodeByIdAndParent(aData.id, PlacesUtils.unfiledBookmarksFolderId), |
michael@0 | 138 | 0, // Unused for real nodes. |
michael@0 | 139 | PlacesUtils.unfiledBookmarksFolderId, |
michael@0 | 140 | 1, // Move to position 1. |
michael@0 | 141 | false |
michael@0 | 142 | ); |
michael@0 | 143 | PlacesUtils.transactionManager.doTransaction(txn); |
michael@0 | 144 | }); |
michael@0 | 145 | |
michael@0 | 146 | try { |
michael@0 | 147 | let txn = PlacesUIUtils.makeTransaction( |
michael@0 | 148 | wrapNodeByIdAndParent(aData.id, PlacesUtils.unfiledBookmarksFolderId), |
michael@0 | 149 | 0, // Unused for real nodes. |
michael@0 | 150 | PlacesUtils.unfiledBookmarksFolderId, |
michael@0 | 151 | PlacesUtils.bookmarks.DEFAULT_INDEX, |
michael@0 | 152 | true |
michael@0 | 153 | ); |
michael@0 | 154 | PlacesUtils.transactionManager.doTransaction(txn); |
michael@0 | 155 | } catch(ex) { |
michael@0 | 156 | do_throw(ex); |
michael@0 | 157 | } |
michael@0 | 158 | }, "random-anno"); |
michael@0 | 159 | |
michael@0 | 160 | let id = PlacesUtils.bookmarks.createFolder(PlacesUtils.unfiledBookmarksFolderId, |
michael@0 | 161 | TEST_TITLE, |
michael@0 | 162 | PlacesUtils.bookmarks.DEFAULT_INDEX); |
michael@0 | 163 | PlacesUtils.annotations.setItemAnnotation(id, PlacesUIUtils.DESCRIPTION_ANNO, |
michael@0 | 164 | "description", 0, |
michael@0 | 165 | PlacesUtils.annotations.EXPIRE_NEVER); |
michael@0 | 166 | PlacesUtils.annotations.setItemAnnotation(id, "random-anno", |
michael@0 | 167 | "random-value", 0, |
michael@0 | 168 | PlacesUtils.annotations.EXPIRE_NEVER); |
michael@0 | 169 | }); |
michael@0 | 170 | |
michael@0 | 171 | |
michael@0 | 172 | add_test(function test_separator() |
michael@0 | 173 | { |
michael@0 | 174 | waitForBookmarkNotification("onItemChanged", function(aData) |
michael@0 | 175 | { |
michael@0 | 176 | do_check_eq(aData.type, PlacesUtils.bookmarks.TYPE_SEPARATOR); |
michael@0 | 177 | do_check_eq(aData.index, 3); |
michael@0 | 178 | |
michael@0 | 179 | waitForBookmarkNotification("onItemAdded", function(aData) |
michael@0 | 180 | { |
michael@0 | 181 | do_check_eq(aData.type, PlacesUtils.bookmarks.TYPE_SEPARATOR); |
michael@0 | 182 | do_check_eq(aData.index, 4); |
michael@0 | 183 | let id = aData.id; |
michael@0 | 184 | |
michael@0 | 185 | waitForBookmarkNotification("onItemMoved", function(aData) |
michael@0 | 186 | { |
michael@0 | 187 | do_check_eq(aData.id, id); |
michael@0 | 188 | do_check_eq(aData.type, PlacesUtils.bookmarks.TYPE_SEPARATOR); |
michael@0 | 189 | do_check_eq(aData.index, 1); |
michael@0 | 190 | |
michael@0 | 191 | run_next_test(); |
michael@0 | 192 | }); |
michael@0 | 193 | |
michael@0 | 194 | let txn = PlacesUIUtils.makeTransaction( |
michael@0 | 195 | wrapNodeByIdAndParent(aData.id, PlacesUtils.unfiledBookmarksFolderId), |
michael@0 | 196 | 0, // Unused for real nodes. |
michael@0 | 197 | PlacesUtils.unfiledBookmarksFolderId, |
michael@0 | 198 | 1, // Move to position 1. |
michael@0 | 199 | false |
michael@0 | 200 | ); |
michael@0 | 201 | PlacesUtils.transactionManager.doTransaction(txn); |
michael@0 | 202 | }); |
michael@0 | 203 | |
michael@0 | 204 | try { |
michael@0 | 205 | let txn = PlacesUIUtils.makeTransaction( |
michael@0 | 206 | wrapNodeByIdAndParent(aData.id, PlacesUtils.unfiledBookmarksFolderId), |
michael@0 | 207 | 0, // Unused for real nodes. |
michael@0 | 208 | PlacesUtils.unfiledBookmarksFolderId, |
michael@0 | 209 | PlacesUtils.bookmarks.DEFAULT_INDEX, |
michael@0 | 210 | true |
michael@0 | 211 | ); |
michael@0 | 212 | PlacesUtils.transactionManager.doTransaction(txn); |
michael@0 | 213 | } catch(ex) { |
michael@0 | 214 | do_throw(ex); |
michael@0 | 215 | } |
michael@0 | 216 | }, "random-anno"); |
michael@0 | 217 | |
michael@0 | 218 | let id = PlacesUtils.bookmarks.insertSeparator(PlacesUtils.unfiledBookmarksFolderId, |
michael@0 | 219 | PlacesUtils.bookmarks.DEFAULT_INDEX); |
michael@0 | 220 | PlacesUtils.annotations.setItemAnnotation(id, "random-anno", |
michael@0 | 221 | "random-value", 0, |
michael@0 | 222 | PlacesUtils.annotations.EXPIRE_NEVER); |
michael@0 | 223 | }); |
michael@0 | 224 | |
michael@0 | 225 | add_test(function test_bookmark() |
michael@0 | 226 | { |
michael@0 | 227 | const TEST_URL = "http://places.moz.org/" |
michael@0 | 228 | const TEST_TITLE = "Places bookmark" |
michael@0 | 229 | |
michael@0 | 230 | waitForBookmarkNotification("onItemChanged", function(aData) |
michael@0 | 231 | { |
michael@0 | 232 | do_check_eq(aData.title, TEST_TITLE); |
michael@0 | 233 | do_check_eq(aData.url, TEST_URL); |
michael@0 | 234 | do_check_eq(aData.type, PlacesUtils.bookmarks.TYPE_BOOKMARK); |
michael@0 | 235 | do_check_eq(aData.index, 5); |
michael@0 | 236 | |
michael@0 | 237 | waitForBookmarkNotification("onItemAdded", function(aData) |
michael@0 | 238 | { |
michael@0 | 239 | do_check_eq(aData.title, TEST_TITLE); |
michael@0 | 240 | do_check_eq(aData.url, TEST_URL); |
michael@0 | 241 | do_check_eq(aData.type, PlacesUtils.bookmarks.TYPE_BOOKMARK); |
michael@0 | 242 | do_check_eq(aData.index, 6); |
michael@0 | 243 | let id = aData.id; |
michael@0 | 244 | |
michael@0 | 245 | waitForBookmarkNotification("onItemMoved", function(aData) |
michael@0 | 246 | { |
michael@0 | 247 | do_check_eq(aData.id, id); |
michael@0 | 248 | do_check_eq(aData.type, PlacesUtils.bookmarks.TYPE_BOOKMARK); |
michael@0 | 249 | do_check_eq(aData.index, 1); |
michael@0 | 250 | |
michael@0 | 251 | run_next_test(); |
michael@0 | 252 | }); |
michael@0 | 253 | |
michael@0 | 254 | let txn = PlacesUIUtils.makeTransaction( |
michael@0 | 255 | wrapNodeByIdAndParent(aData.id, PlacesUtils.unfiledBookmarksFolderId), |
michael@0 | 256 | 0, // Unused for real nodes. |
michael@0 | 257 | PlacesUtils.unfiledBookmarksFolderId, |
michael@0 | 258 | 1, // Move to position 1. |
michael@0 | 259 | false |
michael@0 | 260 | ); |
michael@0 | 261 | PlacesUtils.transactionManager.doTransaction(txn); |
michael@0 | 262 | }); |
michael@0 | 263 | |
michael@0 | 264 | try { |
michael@0 | 265 | let txn = PlacesUIUtils.makeTransaction( |
michael@0 | 266 | wrapNodeByIdAndParent(aData.id, PlacesUtils.unfiledBookmarksFolderId), |
michael@0 | 267 | 0, // Unused for real nodes. |
michael@0 | 268 | PlacesUtils.unfiledBookmarksFolderId, |
michael@0 | 269 | PlacesUtils.bookmarks.DEFAULT_INDEX, |
michael@0 | 270 | true |
michael@0 | 271 | ); |
michael@0 | 272 | PlacesUtils.transactionManager.doTransaction(txn); |
michael@0 | 273 | } catch(ex) { |
michael@0 | 274 | do_throw(ex); |
michael@0 | 275 | } |
michael@0 | 276 | }, "random-anno"); |
michael@0 | 277 | |
michael@0 | 278 | let id = PlacesUtils.bookmarks.insertBookmark(PlacesUtils.unfiledBookmarksFolderId, |
michael@0 | 279 | NetUtil.newURI(TEST_URL), |
michael@0 | 280 | PlacesUtils.bookmarks.DEFAULT_INDEX, |
michael@0 | 281 | TEST_TITLE); |
michael@0 | 282 | PlacesUtils.annotations.setItemAnnotation(id, PlacesUIUtils.DESCRIPTION_ANNO, |
michael@0 | 283 | "description", 0, |
michael@0 | 284 | PlacesUtils.annotations.EXPIRE_NEVER); |
michael@0 | 285 | PlacesUtils.annotations.setItemAnnotation(id, "random-anno", |
michael@0 | 286 | "random-value", 0, |
michael@0 | 287 | PlacesUtils.annotations.EXPIRE_NEVER); |
michael@0 | 288 | }); |
michael@0 | 289 | |
michael@0 | 290 | add_test(function test_visit() |
michael@0 | 291 | { |
michael@0 | 292 | const TEST_URL = "http://places.moz.org/" |
michael@0 | 293 | const TEST_TITLE = "Places bookmark" |
michael@0 | 294 | |
michael@0 | 295 | waitForBookmarkNotification("onItemAdded", function(aData) |
michael@0 | 296 | { |
michael@0 | 297 | do_check_eq(aData.title, TEST_TITLE); |
michael@0 | 298 | do_check_eq(aData.url, TEST_URL); |
michael@0 | 299 | do_check_eq(aData.type, PlacesUtils.bookmarks.TYPE_BOOKMARK); |
michael@0 | 300 | do_check_eq(aData.index, 7); |
michael@0 | 301 | |
michael@0 | 302 | waitForBookmarkNotification("onItemAdded", function(aData) |
michael@0 | 303 | { |
michael@0 | 304 | do_check_eq(aData.title, TEST_TITLE); |
michael@0 | 305 | do_check_eq(aData.url, TEST_URL); |
michael@0 | 306 | do_check_eq(aData.type, PlacesUtils.bookmarks.TYPE_BOOKMARK); |
michael@0 | 307 | do_check_eq(aData.index, 8); |
michael@0 | 308 | run_next_test(); |
michael@0 | 309 | }); |
michael@0 | 310 | |
michael@0 | 311 | try { |
michael@0 | 312 | let node = wrapNodeByIdAndParent(aData.id, PlacesUtils.unfiledBookmarksFolderId); |
michael@0 | 313 | // Simulate a not-bookmarked node, will copy it to a new bookmark. |
michael@0 | 314 | node.id = -1; |
michael@0 | 315 | let txn = PlacesUIUtils.makeTransaction( |
michael@0 | 316 | node, |
michael@0 | 317 | 0, // Unused for real nodes. |
michael@0 | 318 | PlacesUtils.unfiledBookmarksFolderId, |
michael@0 | 319 | PlacesUtils.bookmarks.DEFAULT_INDEX, |
michael@0 | 320 | true |
michael@0 | 321 | ); |
michael@0 | 322 | PlacesUtils.transactionManager.doTransaction(txn); |
michael@0 | 323 | } catch(ex) { |
michael@0 | 324 | do_throw(ex); |
michael@0 | 325 | } |
michael@0 | 326 | }); |
michael@0 | 327 | |
michael@0 | 328 | PlacesUtils.bookmarks.insertBookmark(PlacesUtils.unfiledBookmarksFolderId, |
michael@0 | 329 | NetUtil.newURI(TEST_URL), |
michael@0 | 330 | PlacesUtils.bookmarks.DEFAULT_INDEX, |
michael@0 | 331 | TEST_TITLE); |
michael@0 | 332 | }); |
michael@0 | 333 | |
michael@0 | 334 | add_test(function check_annotations() { |
michael@0 | 335 | // As last step check how many items for each annotation exist. |
michael@0 | 336 | |
michael@0 | 337 | // Copies should retain the description annotation. |
michael@0 | 338 | let descriptions = |
michael@0 | 339 | PlacesUtils.annotations.getItemsWithAnnotation(PlacesUIUtils.DESCRIPTION_ANNO, {}); |
michael@0 | 340 | do_check_eq(descriptions.length, 4); |
michael@0 | 341 | |
michael@0 | 342 | // Only the original bookmarks should have this annotation. |
michael@0 | 343 | let others = PlacesUtils.annotations.getItemsWithAnnotation("random-anno", {}); |
michael@0 | 344 | do_check_eq(others.length, 3); |
michael@0 | 345 | run_next_test(); |
michael@0 | 346 | }); |
michael@0 | 347 | |
michael@0 | 348 | function run_test() |
michael@0 | 349 | { |
michael@0 | 350 | run_next_test(); |
michael@0 | 351 | } |