test_placesTxn.js
Go to the documentation of this file.
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 /* ***** BEGIN LICENSE BLOCK *****
4  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5  *
6  * The contents of this file are subject to the Mozilla Public License Version
7  * 1.1 (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  * http://www.mozilla.org/MPL/
10  *
11  * Software distributed under the License is distributed on an "AS IS" basis,
12  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13  * for the specific language governing rights and limitations under the
14  * License.
15  *
16  * The Original Code is mozilla.org code.
17  *
18  * The Initial Developer of the Original Code is
19  * Mozilla Foundation.
20  * Portions created by the Initial Developer are Copyright (C) 2005
21  * the Initial Developer. All Rights Reserved.
22  *
23  * Contributor(s):
24  * Sungjoon Steve Won <stevewon@gmail.com> (Original Author)
25  * Asaf Romano <mano@mozilla.com>
26  *
27  * Alternatively, the contents of this file may be used under the terms of
28  * either the GNU General Public License Version 2 or later (the "GPL"), or
29  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
30  * in which case the provisions of the GPL or the LGPL are applicable instead
31  * of those above. If you wish to allow use of your version of this file only
32  * under the terms of either the GPL or the LGPL, and not to allow others to
33  * use your version of this file under the terms of the MPL, indicate your
34  * decision by deleting the provisions above and replace them with the notice
35  * and other provisions required by the GPL or the LGPL. If you do not delete
36  * the provisions above, a recipient may use your version of this file under
37  * the terms of any one of the MPL, the GPL or the LGPL.
38  *
39  * ***** END LICENSE BLOCK ***** */
40 
41 // Get bookmark service
42 try {
43  var bmsvc = Cc["@mozilla.org/browser/nav-bookmarks-service;1"].getService(Ci.nsINavBookmarksService);
44 } catch(ex) {
45  do_throw("Could not get nav-bookmarks-service\n");
46 }
47 
48 // Get livemark service
49 try {
50  var lmsvc = Cc["@mozilla.org/browser/livemark-service;2"].getService(Ci.nsILivemarkService);
51 } catch(ex) {
52  do_throw("Could not get livemark-service\n");
53 }
54 
55 // Get microsummary service
56 try {
57  var mss = Cc["@mozilla.org/microsummary/service;1"].getService(Ci.nsIMicrosummaryService);
58 } catch(ex) {
59  do_throw("Could not get microsummary-service\n");
60 }
61 
62 // Get Places Transaction Manager Service
63 try {
64  var ptSvc = Cc["@mozilla.org/browser/placesTransactionsService;1"].
65  getService(Ci.nsIPlacesTransactionsService);
66 } catch(ex) {
67  do_throw("Could not get Places Transactions Service\n");
68 }
69 
70 // Get tagging service
71 try {
72  var tagssvc = Cc["@mozilla.org/browser/tagging-service;1"].
73  getService(Ci.nsITaggingService);
74 } catch(ex) {
75  do_throw("Could not get tagging service\n");
76 }
77 
78 // Get annotations service
79 try {
80  var annosvc = Cc["@mozilla.org/browser/annotation-service;1"].
81  getService(Ci.nsIAnnotationService);
82 } catch(ex) {
83  do_throw("Could not get annotations service\n");
84 }
85 
86 // create and add bookmarks observer
87 var observer = {
88  onBeginUpdateBatch: function() {
89  this._beginUpdateBatch = true;
90  },
91  onEndUpdateBatch: function() {
92  this._endUpdateBatch = true;
93  },
94  onItemAdded: function(id, folder, index, itemType) {
95  this._itemAddedId = id;
96  this._itemAddedParent = folder;
97  this._itemAddedIndex = index;
98  this._itemAddedType = itemType;
99  },
100  onBeforeItemRemoved: function(id) {
101  },
102  onItemRemoved: function(id, folder, index, itemType) {
103  this._itemRemovedId = id;
104  this._itemRemovedFolder = folder;
105  this._itemRemovedIndex = index;
106  },
107  onItemChanged: function(id, property, isAnnotationProperty, newValue,
108  lastModified, itemType) {
109  this._itemChangedId = id;
110  this._itemChangedProperty = property;
111  this._itemChanged_isAnnotationProperty = isAnnotationProperty;
112  this._itemChangedValue = newValue;
113  },
114  onItemVisited: function(id, visitID, time) {
115  this._itemVisitedId = id;
116  this._itemVisitedVistId = visitID;
117  this._itemVisitedTime = time;
118  },
119  onItemMoved: function(id, oldParent, oldIndex, newParent, newIndex,
120  itemType) {
121  this._itemMovedId = id
122  this._itemMovedOldParent = oldParent;
123  this._itemMovedOldIndex = oldIndex;
124  this._itemMovedNewParent = newParent;
125  this._itemMovedNewIndex = newIndex;
126  },
127  QueryInterface: function(iid) {
128  if (iid.equals(Ci.nsINavBookmarkObserver) ||
129  iid.equals(Ci.nsISupports)) {
130  return this;
131  }
132  throw Cr.NS_ERROR_NO_INTERFACE;
133  }
134 };
135 bmsvc.addObserver(observer, false);
136 
137 // index at which items should begin
138 var bmStartIndex = 0;
139 
140 // main
141 function run_test() {
142  // get bookmarks root index
143  var root = bmsvc.bookmarksMenuFolder;
144 
145  //Test creating a folder with a description
146  const DESCRIPTION_ANNO = "bookmarkProperties/description";
147  const TEST_DESCRIPTION = "this is my test description";
148  var annos = [{ name: DESCRIPTION_ANNO,
149  type: annosvc.TYPE_STRING,
150  flags: 0,
151  value: TEST_DESCRIPTION,
152  expires: annosvc.EXPIRE_NEVER }];
153  var txn1 = ptSvc.createFolder("Testing folder", root, bmStartIndex, annos);
154  ptSvc.doTransaction(txn1);
155 
156  // This checks that calling undoTransaction on an "empty batch" doesn't
157  // undo the previous transaction (getItemTitle will fail)
158  ptSvc.beginBatch();
159  ptSvc.endBatch();
160  ptSvc.undoTransaction();
161 
162  var folderId = observer._itemAddedId;
163  do_check_eq(bmsvc.getItemTitle(folderId), "Testing folder");
164  do_check_eq(observer._itemAddedIndex, bmStartIndex);
165  do_check_eq(observer._itemAddedParent, root);
166  do_check_eq(observer._itemAddedId, folderId);
167  do_check_eq(TEST_DESCRIPTION,
168  annosvc.getItemAnnotation(folderId, DESCRIPTION_ANNO));
169 
170  txn1.undoTransaction();
171  do_check_eq(observer._itemRemovedId, folderId);
172  do_check_eq(observer._itemRemovedFolder, root);
173  do_check_eq(observer._itemRemovedIndex, bmStartIndex);
174  txn1.redoTransaction();
175  do_check_eq(observer._itemAddedIndex, bmStartIndex);
176  do_check_eq(observer._itemAddedParent, root);
177  do_check_eq(observer._itemAddedId, folderId);
178  txn1.undoTransaction();
179  do_check_eq(observer._itemRemovedId, folderId);
180  do_check_eq(observer._itemRemovedFolder, root);
181  do_check_eq(observer._itemRemovedIndex, bmStartIndex);
182 
183  // Test creating an item
184  // Create to Root
185  var txn2 = ptSvc.createItem(uri("http://www.example.com"), root, bmStartIndex, "Testing1");
186  ptSvc.doTransaction(txn2); // Also testing doTransaction
187  var b = (bmsvc.getBookmarkIdsForURI(uri("http://www.example.com"), {}))[0];
188  do_check_eq(observer._itemAddedId, b);
189  do_check_eq(observer._itemAddedIndex, bmStartIndex);
190  do_check_true(bmsvc.isBookmarked(uri("http://www.example.com")));
191  txn2.undoTransaction();
192  do_check_eq(observer._itemRemovedId, b);
193  do_check_eq(observer._itemRemovedIndex, bmStartIndex);
194  do_check_false(bmsvc.isBookmarked(uri("http://www.example.com")));
195  txn2.redoTransaction();
196  do_check_true(bmsvc.isBookmarked(uri("http://www.example.com")));
197  var newId = (bmsvc.getBookmarkIdsForURI(uri("http://www.example.com"), {}))[0];
198  do_check_eq(observer._itemAddedIndex, bmStartIndex);
199  do_check_eq(observer._itemAddedParent, root);
200  do_check_eq(observer._itemAddedId, newId);
201  txn2.undoTransaction();
202  do_check_eq(observer._itemRemovedId, newId);
203  do_check_eq(observer._itemRemovedFolder, root);
204  do_check_eq(observer._itemRemovedIndex, bmStartIndex);
205 
206  // Create item to a folder
207  var txn2a = ptSvc.createFolder("Folder", root, bmStartIndex);
208  ptSvc.doTransaction(txn2a);
209  var fldrId = observer._itemAddedId;
210  do_check_eq(bmsvc.getItemTitle(fldrId), "Folder");
211 
212  var txn2b = ptSvc.createItem(uri("http://www.example2.com"), fldrId, bmStartIndex, "Testing1b");
213  ptSvc.doTransaction(txn2b);
214  var b2 = (bmsvc.getBookmarkIdsForURI(uri("http://www.example2.com"), {}))[0];
215  do_check_eq(observer._itemAddedId, b2);
216  do_check_eq(observer._itemAddedIndex, bmStartIndex);
217  do_check_true(bmsvc.isBookmarked(uri("http://www.example2.com")));
218  txn2b.undoTransaction();
219  do_check_eq(observer._itemRemovedId, b2);
220  do_check_eq(observer._itemRemovedIndex, bmStartIndex);
221  txn2b.redoTransaction();
222  newId = (bmsvc.getBookmarkIdsForURI(uri("http://www.example2.com"), {}))[0];
223  do_check_eq(observer._itemAddedIndex, bmStartIndex);
224  do_check_eq(observer._itemAddedParent, fldrId);
225  do_check_eq(observer._itemAddedId, newId);
226  txn2b.undoTransaction();
227  do_check_eq(observer._itemRemovedId, newId);
228  do_check_eq(observer._itemRemovedFolder, fldrId);
229  do_check_eq(observer._itemRemovedIndex, bmStartIndex);
230 
231  // Testing moving an item
232  ptSvc.doTransaction(ptSvc.createItem(uri("http://www.example3.com"), root, -1, "Testing2"));
233  ptSvc.doTransaction(ptSvc.createItem(uri("http://www.example3.com"), root, -1, "Testing3"));
234  ptSvc.doTransaction(ptSvc.createItem(uri("http://www.example3.com"), fldrId, -1, "Testing4"));
235  var bkmkIds = bmsvc.getBookmarkIdsForURI(uri("http://www.example3.com"), {});
236  bkmkIds.sort();
237  var bkmk1Id = bkmkIds[0];
238  var bkmk2Id = bkmkIds[1];
239  var bkmk3Id = bkmkIds[2];
240 
241  // Moving items between the same folder
242  var txn3 = ptSvc.moveItem(bkmk1Id, root, -1);
243  txn3.doTransaction();
244  do_check_eq(observer._itemMovedId, bkmk1Id);
245  do_check_eq(observer._itemMovedOldParent, root);
246  do_check_eq(observer._itemMovedOldIndex, 1);
247  do_check_eq(observer._itemMovedNewParent, root);
248  do_check_eq(observer._itemMovedNewIndex, 2);
249  txn3.undoTransaction();
250  do_check_eq(observer._itemMovedId, bkmk1Id);
251  do_check_eq(observer._itemMovedOldParent, root);
252  do_check_eq(observer._itemMovedOldIndex, 2);
253  do_check_eq(observer._itemMovedNewParent, root);
254  do_check_eq(observer._itemMovedNewIndex, 1);
255  txn3.redoTransaction();
256  do_check_eq(observer._itemMovedId, bkmk1Id);
257  do_check_eq(observer._itemMovedOldParent, root);
258  do_check_eq(observer._itemMovedOldIndex, 1);
259  do_check_eq(observer._itemMovedNewParent, root);
260  do_check_eq(observer._itemMovedNewIndex, 2);
261  txn3.undoTransaction();
262  do_check_eq(observer._itemMovedId, bkmk1Id);
263  do_check_eq(observer._itemMovedOldParent, root);
264  do_check_eq(observer._itemMovedOldIndex, 2);
265  do_check_eq(observer._itemMovedNewParent, root);
266  do_check_eq(observer._itemMovedNewIndex, 1);
267 
268  // Moving items between different folders
269  var txn3b = ptSvc.moveItem(bkmk1Id, fldrId, -1);
270  txn3b.doTransaction();
271  do_check_eq(observer._itemMovedId, bkmk1Id);
272  do_check_eq(observer._itemMovedOldParent, root);
273  do_check_eq(observer._itemMovedOldIndex, 1);
274  do_check_eq(observer._itemMovedNewParent, fldrId);
275  do_check_eq(observer._itemMovedNewIndex, 1);
276  txn3.undoTransaction();
277  do_check_eq(observer._itemMovedId, bkmk1Id);
278  do_check_eq(observer._itemMovedOldParent, fldrId);
279  do_check_eq(observer._itemMovedOldIndex, 1);
280  do_check_eq(observer._itemMovedNewParent, root);
281  do_check_eq(observer._itemMovedNewIndex, 1);
282  txn3b.redoTransaction();
283  do_check_eq(observer._itemMovedId, bkmk1Id);
284  do_check_eq(observer._itemMovedOldParent, root);
285  do_check_eq(observer._itemMovedOldIndex, 1);
286  do_check_eq(observer._itemMovedNewParent, fldrId);
287  do_check_eq(observer._itemMovedNewIndex, 1);
288  txn3.undoTransaction();
289  do_check_eq(observer._itemMovedId, bkmk1Id);
290  do_check_eq(observer._itemMovedOldParent, fldrId);
291  do_check_eq(observer._itemMovedOldIndex, 1);
292  do_check_eq(observer._itemMovedNewParent, root);
293  do_check_eq(observer._itemMovedNewIndex, 1);
294 
295  // Test Removing a Folder
296  ptSvc.doTransaction(ptSvc.createFolder("Folder2", root, -1));
297  var fldrId2 = observer._itemAddedId;
298  do_check_eq(bmsvc.getItemTitle(fldrId2), "Folder2");
299 
300  var txn4 = ptSvc.removeItem(fldrId2);
301  txn4.doTransaction();
302  do_check_eq(observer._itemRemovedId, fldrId2);
303  do_check_eq(observer._itemRemovedFolder, root);
304  do_check_eq(observer._itemRemovedIndex, 3);
305  txn4.undoTransaction();
306  do_check_eq(observer._itemAddedId, fldrId2);
307  do_check_eq(observer._itemAddedParent, root);
308  do_check_eq(observer._itemAddedIndex, 3);
309  txn4.redoTransaction();
310  do_check_eq(observer._itemRemovedId, fldrId2);
311  do_check_eq(observer._itemRemovedFolder, root);
312  do_check_eq(observer._itemRemovedIndex, 3);
313  txn4.undoTransaction();
314  do_check_eq(observer._itemAddedId, fldrId2);
315  do_check_eq(observer._itemAddedParent, root);
316  do_check_eq(observer._itemAddedIndex, 3);
317 
318  // Test removing an item with a keyword
319  bmsvc.setKeywordForBookmark(bkmk2Id, "test_keyword");
320  var txn5 = ptSvc.removeItem(bkmk2Id);
321  txn5.doTransaction();
322  do_check_eq(observer._itemRemovedId, bkmk2Id);
323  do_check_eq(observer._itemRemovedFolder, root);
324  do_check_eq(observer._itemRemovedIndex, 2);
325  do_check_eq(bmsvc.getKeywordForBookmark(bkmk2Id), null);
326  txn5.undoTransaction();
327  var newbkmk2Id = observer._itemAddedId;
328  do_check_eq(observer._itemAddedParent, root);
329  do_check_eq(observer._itemAddedIndex, 2);
330  do_check_eq(bmsvc.getKeywordForBookmark(newbkmk2Id), "test_keyword");
331  txn5.redoTransaction();
332  do_check_eq(observer._itemRemovedId, newbkmk2Id);
333  do_check_eq(observer._itemRemovedFolder, root);
334  do_check_eq(observer._itemRemovedIndex, 2);
335  do_check_eq(bmsvc.getKeywordForBookmark(newbkmk2Id), null);
336  txn5.undoTransaction();
337  do_check_eq(observer._itemAddedParent, root);
338  do_check_eq(observer._itemAddedIndex, 2);
339 
340  // Test creating a separator
341  var txn6 = ptSvc.createSeparator(root, 1);
342  txn6.doTransaction();
343  var sepId = observer._itemAddedId;
344  do_check_eq(observer._itemAddedIndex, 1);
345  do_check_eq(observer._itemAddedParent, root);
346  txn6.undoTransaction();
347  do_check_eq(observer._itemRemovedId, sepId);
348  do_check_eq(observer._itemRemovedFolder, root);
349  do_check_eq(observer._itemRemovedIndex, 1);
350  txn6.redoTransaction();
351  var newSepId = observer._itemAddedId;
352  do_check_eq(observer._itemAddedIndex, 1);
353  do_check_eq(observer._itemAddedParent, root);
354  txn6.undoTransaction();
355  do_check_eq(observer._itemRemovedId, newSepId);
356  do_check_eq(observer._itemRemovedFolder, root);
357  do_check_eq(observer._itemRemovedIndex, 1);
358 
359  // Test removing a separator
360  ptSvc.doTransaction(ptSvc.createSeparator(root, 1));
361  var sepId2 = observer._itemAddedId;
362  var txn7 = ptSvc.removeItem(sepId2);
363  txn7.doTransaction();
364  do_check_eq(observer._itemRemovedId, sepId2);
365  do_check_eq(observer._itemRemovedFolder, root);
366  do_check_eq(observer._itemRemovedIndex, 1);
367  txn7.undoTransaction();
368  do_check_eq(observer._itemAddedId, sepId2); //New separator created
369  do_check_eq(observer._itemAddedParent, root);
370  do_check_eq(observer._itemAddedIndex, 1);
371  txn7.redoTransaction();
372  do_check_eq(observer._itemRemovedId, sepId2);
373  do_check_eq(observer._itemRemovedFolder, root);
374  do_check_eq(observer._itemRemovedIndex, 1);
375  txn7.undoTransaction();
376  do_check_eq(observer._itemAddedId, sepId2); //New separator created
377  do_check_eq(observer._itemAddedParent, root);
378  do_check_eq(observer._itemAddedIndex, 1);
379 
380  // Test editing item title
381  var txn8 = ptSvc.editItemTitle(bkmk1Id, "Testing2_mod");
382  txn8.doTransaction();
383  do_check_eq(observer._itemChangedId, bkmk1Id);
384  do_check_eq(observer._itemChangedProperty, "title");
385  do_check_eq(observer._itemChangedValue, "Testing2_mod");
386  txn8.undoTransaction();
387  do_check_eq(observer._itemChangedId, bkmk1Id);
388  do_check_eq(observer._itemChangedProperty, "title");
389  do_check_eq(observer._itemChangedValue, "Testing2");
390  txn8.redoTransaction();
391  do_check_eq(observer._itemChangedId, bkmk1Id);
392  do_check_eq(observer._itemChangedProperty, "title");
393  do_check_eq(observer._itemChangedValue, "Testing2_mod");
394  txn8.undoTransaction();
395  do_check_eq(observer._itemChangedId, bkmk1Id);
396  do_check_eq(observer._itemChangedProperty, "title");
397  do_check_eq(observer._itemChangedValue, "Testing2");
398 
399  // Test editing item uri
400  var txn9 = ptSvc.editBookmarkURI(bkmk1Id, uri("http://newuri.com"));
401  txn9.doTransaction();
402  do_check_eq(observer._itemChangedId, bkmk1Id);
403  do_check_eq(observer._itemChangedProperty, "uri");
404  do_check_eq(observer._itemChangedValue, "http://newuri.com/");
405  txn9.undoTransaction();
406  do_check_eq(observer._itemChangedId, bkmk1Id);
407  do_check_eq(observer._itemChangedProperty, "uri");
408  do_check_eq(observer._itemChangedValue, "http://www.example3.com/");
409  txn9.redoTransaction();
410  do_check_eq(observer._itemChangedId, bkmk1Id);
411  do_check_eq(observer._itemChangedProperty, "uri");
412  do_check_eq(observer._itemChangedValue, "http://newuri.com/");
413  txn9.undoTransaction();
414  do_check_eq(observer._itemChangedId, bkmk1Id);
415  do_check_eq(observer._itemChangedProperty, "uri");
416  do_check_eq(observer._itemChangedValue, "http://www.example3.com/");
417 
418  // Test edit item description
419  var txn10 = ptSvc.editItemDescription(bkmk1Id, "Description1");
420  txn10.doTransaction();
421  do_check_eq(observer._itemChangedId, bkmk1Id);
422  do_check_eq(observer._itemChangedProperty, "bookmarkProperties/description");
423 
424  // Testing edit keyword
425  var txn11 = ptSvc.editBookmarkKeyword(bkmk1Id, "kw1");
426  txn11.doTransaction();
427  do_check_eq(observer._itemChangedId, bkmk1Id);
428  do_check_eq(observer._itemChangedProperty, "keyword");
429  do_check_eq(observer._itemChangedValue, "kw1");
430  txn11.undoTransaction();
431  do_check_eq(observer._itemChangedId, bkmk1Id);
432  do_check_eq(observer._itemChangedProperty, "keyword");
433  do_check_eq(observer._itemChangedValue, "");
434 
435  // Testing create livemark
436  var txn12 = ptSvc.createLivemark(uri("http://feeduri.com"),
437  uri("http://siteuri.com"),
438  "Livemark1", root);
439  txn12.doTransaction();
440  var lvmkId = observer._itemAddedId;
441  do_check_true(lmsvc.isLivemark(lvmkId));
442  do_check_eq(lmsvc.getSiteURI(lvmkId).spec, "http://siteuri.com/");
443  do_check_eq(lmsvc.getFeedURI(lvmkId).spec, "http://feeduri.com/");
444  txn12.undoTransaction();
445  do_check_false(lmsvc.isLivemark(lvmkId));
446  txn12.redoTransaction();
447  lvmkId = observer._itemAddedId;
448  do_check_true(lmsvc.isLivemark(lvmkId));
449  do_check_eq(lmsvc.getSiteURI(lvmkId).spec, "http://siteuri.com/");
450  do_check_eq(lmsvc.getFeedURI(lvmkId).spec, "http://feeduri.com/");
451 
452  // editLivemarkSiteURI
453  var txn13 = ptSvc.editLivemarkSiteURI(lvmkId, uri("http://new-siteuri.com/"));
454  txn13.doTransaction();
455  do_check_eq(observer._itemChangedId, lvmkId);
456  do_check_eq(observer._itemChangedProperty, "livemark/siteURI");
457  do_check_eq(lmsvc.getSiteURI(lvmkId).spec, "http://new-siteuri.com/");
458  txn13.undoTransaction();
459  do_check_eq(observer._itemChangedId, lvmkId);
460  do_check_eq(observer._itemChangedProperty, "livemark/siteURI");
461  do_check_eq(observer._itemChangedValue, "");
462  do_check_eq(lmsvc.getSiteURI(lvmkId).spec, "http://siteuri.com/");
463  txn13.redoTransaction();
464  do_check_eq(observer._itemChangedId, lvmkId);
465  do_check_eq(observer._itemChangedProperty, "livemark/siteURI");
466  do_check_eq(lmsvc.getSiteURI(lvmkId).spec, "http://new-siteuri.com/");
467  txn13.undoTransaction();
468  do_check_eq(observer._itemChangedId, lvmkId);
469  do_check_eq(observer._itemChangedProperty, "livemark/siteURI");
470  do_check_eq(observer._itemChangedValue, "");
471  do_check_eq(lmsvc.getSiteURI(lvmkId).spec, "http://siteuri.com/");
472 
473  // editLivemarkFeedURI
474  var txn14 = ptSvc.editLivemarkFeedURI(lvmkId, uri("http://new-feeduri.com/"));
475  txn14.doTransaction();
476  do_check_eq(observer._itemChangedId, lvmkId);
477  do_check_eq(observer._itemChangedProperty, "livemark/feedURI");
478  do_check_eq(lmsvc.getFeedURI(lvmkId).spec, "http://new-feeduri.com/");
479  txn14.undoTransaction();
480  do_check_eq(observer._itemChangedId, lvmkId);
481  do_check_eq(observer._itemChangedProperty, "livemark/feedURI");
482  do_check_eq(observer._itemChangedValue, "");
483  do_check_eq(lmsvc.getFeedURI(lvmkId).spec, "http://feeduri.com/");
484  txn14.redoTransaction();
485  do_check_eq(observer._itemChangedId, lvmkId);
486  do_check_eq(observer._itemChangedProperty, "livemark/feedURI");
487  do_check_eq(observer._itemChangedValue, "");
488  do_check_eq(lmsvc.getFeedURI(lvmkId).spec, "http://new-feeduri.com/");
489  txn14.undoTransaction();
490  do_check_eq(observer._itemChangedId, lvmkId);
491  do_check_eq(observer._itemChangedProperty, "livemark/feedURI");
492  do_check_eq(observer._itemChangedValue, "");
493  do_check_eq(lmsvc.getFeedURI(lvmkId).spec, "http://feeduri.com/");
494 
495  // Testing remove livemark
496  // Set an annotation and check that we don't lose it on undo
497  annosvc.setItemAnnotation(lvmkId, "livemark/testAnno", "testAnno",
498  0, annosvc.EXPIRE_NEVER);
499  var txn15 = ptSvc.removeItem(lvmkId);
500  txn15.doTransaction();
501  do_check_false(lmsvc.isLivemark(lvmkId));
502  do_check_eq(observer._itemRemovedId, lvmkId);
503  txn15.undoTransaction();
504  lvmkId = observer._itemAddedId;
505  do_check_true(lmsvc.isLivemark(lvmkId));
506  do_check_eq(lmsvc.getSiteURI(lvmkId).spec, "http://siteuri.com/");
507  do_check_eq(lmsvc.getFeedURI(lvmkId).spec, "http://feeduri.com/");
508  do_check_eq(annosvc.getItemAnnotation(lvmkId, "livemark/testAnno"), "testAnno");
509  txn15.redoTransaction();
510  do_check_false(lmsvc.isLivemark(lvmkId));
511  do_check_eq(observer._itemRemovedId, lvmkId);
512 
513  // Test setLoadInSidebar
514  const LOAD_IN_SIDEBAR_ANNO = "bookmarkProperties/loadInSidebar";
515  var txn16 = ptSvc.setLoadInSidebar(bkmk1Id, true);
516  txn16.doTransaction();
517  do_check_eq(observer._itemChangedId, bkmk1Id);
518  do_check_eq(observer._itemChangedProperty, LOAD_IN_SIDEBAR_ANNO);
519  do_check_eq(observer._itemChanged_isAnnotationProperty, true);
520  txn16.undoTransaction();
521  do_check_eq(observer._itemChangedId, bkmk1Id);
522  do_check_eq(observer._itemChangedProperty, LOAD_IN_SIDEBAR_ANNO);
523  do_check_eq(observer._itemChanged_isAnnotationProperty, true);
524 
525  // Test generic item annotation
526  var itemAnnoObj = { name: "testAnno/testInt",
527  type: Ci.nsIAnnotationService.TYPE_INT32,
528  flags: 0,
529  value: 123,
530  expires: Ci.nsIAnnotationService.EXPIRE_NEVER };
531  var genItemAnnoTxn = ptSvc.setItemAnnotation(bkmk1Id, itemAnnoObj);
532  genItemAnnoTxn.doTransaction();
533  do_check_eq(observer._itemChangedId, bkmk1Id);
534  do_check_eq(observer._itemChangedProperty, "testAnno/testInt");
535  do_check_eq(observer._itemChanged_isAnnotationProperty, true);
536  genItemAnnoTxn.undoTransaction();
537  do_check_eq(observer._itemChangedId, bkmk1Id);
538  do_check_eq(observer._itemChangedProperty, "testAnno/testInt");
539  do_check_eq(observer._itemChanged_isAnnotationProperty, true);
540  genItemAnnoTxn.redoTransaction();
541  do_check_eq(observer._itemChangedId, bkmk1Id);
542  do_check_eq(observer._itemChangedProperty, "testAnno/testInt");
543  do_check_eq(observer._itemChanged_isAnnotationProperty, true);
544 
545  // Test generic page annotation
546  var pageAnnoObj = { name: "testAnno/testInt",
547  type: Ci.nsIAnnotationService.TYPE_INT32,
548  flags: 0,
549  value: 123,
550  expires: Ci.nsIAnnotationService.EXPIRE_NEVER };
551  var genPageAnnoTxn = ptSvc.setPageAnnotation(uri("http://www.mozilla.org/"), pageAnnoObj);
552  genPageAnnoTxn.doTransaction();
553  do_check_true(annosvc.pageHasAnnotation(uri("http://www.mozilla.org/"), "testAnno/testInt"));
554  genPageAnnoTxn.undoTransaction();
555  do_check_false(annosvc.pageHasAnnotation(uri("http://www.mozilla.org/"), "testAnno/testInt"));
556  genPageAnnoTxn.redoTransaction();
557  do_check_true(annosvc.pageHasAnnotation(uri("http://www.mozilla.org/"), "testAnno/testInt"));
558 
559  // sortFolderByName
560  ptSvc.doTransaction(ptSvc.createFolder("Sorting folder", root, bmStartIndex, [], null));
561  var srtFldId = observer._itemAddedId;
562  do_check_eq(bmsvc.getItemTitle(srtFldId), "Sorting folder");
563  ptSvc.doTransaction(ptSvc.createItem(uri("http://www.sortingtest.com"), srtFldId, -1, "c"));
564  ptSvc.doTransaction(ptSvc.createItem(uri("http://www.sortingtest.com"), srtFldId, -1, "b"));
565  ptSvc.doTransaction(ptSvc.createItem(uri("http://www.sortingtest.com"), srtFldId, -1, "a"));
566  var b = bmsvc.getBookmarkIdsForURI(uri("http://www.sortingtest.com"), {});
567  b.sort();
568  var b1 = b[0];
569  var b2 = b[1];
570  var b3 = b[2];
571  do_check_eq(0, bmsvc.getItemIndex(b1));
572  do_check_eq(1, bmsvc.getItemIndex(b2));
573  do_check_eq(2, bmsvc.getItemIndex(b3));
574  var txn17 = ptSvc.sortFolderByName(srtFldId);
575  txn17.doTransaction();
576  do_check_eq(2, bmsvc.getItemIndex(b1));
577  do_check_eq(1, bmsvc.getItemIndex(b2));
578  do_check_eq(0, bmsvc.getItemIndex(b3));
579  txn17.undoTransaction();
580  do_check_eq(0, bmsvc.getItemIndex(b1));
581  do_check_eq(1, bmsvc.getItemIndex(b2));
582  do_check_eq(2, bmsvc.getItemIndex(b3));
583  txn17.redoTransaction();
584  do_check_eq(2, bmsvc.getItemIndex(b1));
585  do_check_eq(1, bmsvc.getItemIndex(b2));
586  do_check_eq(0, bmsvc.getItemIndex(b3));
587  txn17.undoTransaction();
588  do_check_eq(0, bmsvc.getItemIndex(b1));
589  do_check_eq(1, bmsvc.getItemIndex(b2));
590  do_check_eq(2, bmsvc.getItemIndex(b3));
591 
592  // editBookmarkMicrosummary
593  var tmpMs = mss.createMicrosummary(uri("http://testmicro.com"),
594  uri("http://dietrich.ganx4.com/mozilla/test-microsummary.xml"));
595  ptSvc.doTransaction(
596  ptSvc.createItem(uri("http://dietrich.ganx4.com/mozilla/test-microsummary-content.php"),
597  root, -1, "micro test", null, null, null));
598  var bId = (bmsvc.getBookmarkIdsForURI(uri("http://dietrich.ganx4.com/mozilla/test-microsummary-content.php"),{}))[0];
599  do_check_true(!mss.hasMicrosummary(bId));
600  var txn18 = ptSvc.editBookmarkMicrosummary(bId, tmpMs);
601  txn18.doTransaction();
602  do_check_eq(observer._itemChangedId, bId);
603  do_check_true(mss.hasMicrosummary(bId));
604  txn18.undoTransaction();
605  do_check_eq(observer._itemChangedId, bId);
606  do_check_true(!mss.hasMicrosummary(bId));
607 
608  // Testing edit Post Data
609  const POST_DATA_ANNO = "bookmarkProperties/POSTData";
610  var postData = "foo";
611  var postDataURI = uri("http://foo.com");
612  ptSvc.doTransaction(
613  ptSvc.createItem(postDataURI, root, -1, "postdata test", null, null, null));
614  var postDataId = (bmsvc.getBookmarkIdsForURI(postDataURI,{}))[0];
615  var postDataTxn = ptSvc.editBookmarkPostData(postDataId, postData);
616  postDataTxn.doTransaction();
617  do_check_true(annosvc.itemHasAnnotation(postDataId, POST_DATA_ANNO))
618  do_check_eq(annosvc.getItemAnnotation(postDataId, POST_DATA_ANNO), postData);
619  postDataTxn.undoTransaction();
620  do_check_false(annosvc.itemHasAnnotation(postDataId, POST_DATA_ANNO))
621 
622  // Test editing item date added
623  var oldAdded = bmsvc.getItemDateAdded(bkmk1Id);
624  var newAdded = Date.now();
625  var eidaTxn = ptSvc.editItemDateAdded(bkmk1Id, newAdded);
626  eidaTxn.doTransaction();
627  do_check_eq(newAdded, bmsvc.getItemDateAdded(bkmk1Id));
628  eidaTxn.undoTransaction();
629  do_check_eq(oldAdded, bmsvc.getItemDateAdded(bkmk1Id));
630 
631  // Test editing item last modified
632  var oldModified = bmsvc.getItemLastModified(bkmk1Id);
633  var newModified = Date.now();
634  var eilmTxn = ptSvc.editItemLastModified(bkmk1Id, newModified);
635  eilmTxn.doTransaction();
636  do_check_eq(newModified, bmsvc.getItemLastModified(bkmk1Id));
637  eilmTxn.undoTransaction();
638  do_check_eq(oldModified, bmsvc.getItemLastModified(bkmk1Id));
639 
640  // Test tagURI/untagURI
641  var tagURI = uri("http://foo.tld");
642  var tagTxn = ptSvc.tagURI(tagURI, ["foo", "bar"]);
643  tagTxn.doTransaction();
644  do_check_eq(uneval(tagssvc.getTagsForURI(tagURI, { })), uneval(["bar","foo"]));
645  tagTxn.undoTransaction();
646  do_check_true(tagssvc.getTagsForURI(tagURI, { }).length == 0);
647  tagTxn.redoTransaction();
648  do_check_eq(uneval(tagssvc.getTagsForURI(tagURI, { })), uneval(["bar","foo"]));
649  var untagTxn = ptSvc.untagURI(tagURI, ["bar"]);
650  untagTxn.doTransaction();
651  do_check_eq(uneval(tagssvc.getTagsForURI(tagURI, { })), uneval(["foo"]));
652  untagTxn.undoTransaction();
653  do_check_eq(uneval(tagssvc.getTagsForURI(tagURI, { })), uneval(["bar","foo"]));
654  untagTxn.redoTransaction();
655  do_check_eq(uneval(tagssvc.getTagsForURI(tagURI, { })), uneval(["foo"]));
656 
657  // Test aggregate removeItem transaction
658  var bkmk1Id = bmsvc.insertBookmark(root, uri("http://www.mozilla.org/"), 0, "Mozilla");
659  var bkmk2Id = bmsvc.insertSeparator(root, 1);
660  var bkmk3Id = bmsvc.createFolder(root, "folder", 2);
661  var bkmk3_1Id = bmsvc.insertBookmark(bkmk3Id, uri("http://www.mozilla.org/"), 0, "Mozilla");
662  var bkmk3_2Id = bmsvc.insertSeparator(bkmk3Id, 1);
663  var bkmk3_3Id = bmsvc.createFolder(bkmk3Id, "folder", 2);
664 
665  var transactions = [];
666  transactions.push(ptSvc.removeItem(bkmk1Id));
667  transactions.push(ptSvc.removeItem(bkmk2Id));
668  transactions.push(ptSvc.removeItem(bkmk3Id));
669  var txn = ptSvc.aggregateTransactions("RemoveItems", transactions);
670 
671  txn.doTransaction();
672  do_check_eq(bmsvc.getItemIndex(bkmk1Id), -1);
673  do_check_eq(bmsvc.getItemIndex(bkmk2Id), -1);
674  do_check_eq(bmsvc.getItemIndex(bkmk3Id), -1);
675  do_check_eq(bmsvc.getItemIndex(bkmk3_1Id), -1);
676  do_check_eq(bmsvc.getItemIndex(bkmk3_2Id), -1);
677  do_check_eq(bmsvc.getItemIndex(bkmk3_3Id), -1);
678  // Check last removed item id.
679  do_check_eq(observer._itemRemovedId, bkmk3Id);
680 
681  txn.undoTransaction();
682  var newBkmk1Id = bmsvc.getIdForItemAt(root, 0);
683  var newBkmk2Id = bmsvc.getIdForItemAt(root, 1);
684  var newBkmk3Id = bmsvc.getIdForItemAt(root, 2);
685  var newBkmk3_1Id = bmsvc.getIdForItemAt(newBkmk3Id, 0);
686  var newBkmk3_2Id = bmsvc.getIdForItemAt(newBkmk3Id, 1);
687  var newBkmk3_3Id = bmsvc.getIdForItemAt(newBkmk3Id, 2);
688  do_check_eq(bmsvc.getItemType(newBkmk1Id), bmsvc.TYPE_BOOKMARK);
689  do_check_eq(bmsvc.getBookmarkURI(newBkmk1Id).spec, "http://www.mozilla.org/");
690  do_check_eq(bmsvc.getItemType(newBkmk2Id), bmsvc.TYPE_SEPARATOR);
691  do_check_eq(bmsvc.getItemType(newBkmk3Id), bmsvc.TYPE_FOLDER);
692  do_check_eq(bmsvc.getItemTitle(newBkmk3Id), "folder");
693  do_check_eq(bmsvc.getFolderIdForItem(newBkmk3_1Id), newBkmk3Id);
694  do_check_eq(bmsvc.getItemType(newBkmk3_1Id), bmsvc.TYPE_BOOKMARK);
695  do_check_eq(bmsvc.getBookmarkURI(newBkmk3_1Id).spec, "http://www.mozilla.org/");
696  do_check_eq(bmsvc.getFolderIdForItem(newBkmk3_2Id), newBkmk3Id);
697  do_check_eq(bmsvc.getItemType(newBkmk3_2Id), bmsvc.TYPE_SEPARATOR);
698  do_check_eq(bmsvc.getFolderIdForItem(newBkmk3_3Id), newBkmk3Id);
699  do_check_eq(bmsvc.getItemType(newBkmk3_3Id), bmsvc.TYPE_FOLDER);
700  do_check_eq(bmsvc.getItemTitle(newBkmk3_3Id), "folder");
701  // Check last added back item id.
702  // Notice items are restored in reverse order.
703  do_check_eq(observer._itemAddedId, newBkmk1Id);
704 
705  txn.redoTransaction();
706  do_check_eq(bmsvc.getItemIndex(newBkmk1Id), -1);
707  do_check_eq(bmsvc.getItemIndex(newBkmk2Id), -1);
708  do_check_eq(bmsvc.getItemIndex(newBkmk3Id), -1);
709  do_check_eq(bmsvc.getItemIndex(newBkmk3_1Id), -1);
710  do_check_eq(bmsvc.getItemIndex(newBkmk3_2Id), -1);
711  do_check_eq(bmsvc.getItemIndex(newBkmk3_3Id), -1);
712  // Check last removed item id.
713  do_check_eq(observer._itemRemovedId, newBkmk3Id);
714 
715  txn.undoTransaction();
716  newBkmk1Id = bmsvc.getIdForItemAt(root, 0);
717  newBkmk2Id = bmsvc.getIdForItemAt(root, 1);
718  newBkmk3Id = bmsvc.getIdForItemAt(root, 2);
719  newBkmk3_1Id = bmsvc.getIdForItemAt(newBkmk3Id, 0);
720  newBkmk3_2Id = bmsvc.getIdForItemAt(newBkmk3Id, 1);
721  newBkmk3_3Id = bmsvc.getIdForItemAt(newBkmk3Id, 2);
722  do_check_eq(bmsvc.getItemType(newBkmk1Id), bmsvc.TYPE_BOOKMARK);
723  do_check_eq(bmsvc.getBookmarkURI(newBkmk1Id).spec, "http://www.mozilla.org/");
724  do_check_eq(bmsvc.getItemType(newBkmk2Id), bmsvc.TYPE_SEPARATOR);
725  do_check_eq(bmsvc.getItemType(newBkmk3Id), bmsvc.TYPE_FOLDER);
726  do_check_eq(bmsvc.getItemTitle(newBkmk3Id), "folder");
727  do_check_eq(bmsvc.getFolderIdForItem(newBkmk3_1Id), newBkmk3Id);
728  do_check_eq(bmsvc.getItemType(newBkmk3_1Id), bmsvc.TYPE_BOOKMARK);
729  do_check_eq(bmsvc.getBookmarkURI(newBkmk3_1Id).spec, "http://www.mozilla.org/");
730  do_check_eq(bmsvc.getFolderIdForItem(newBkmk3_2Id), newBkmk3Id);
731  do_check_eq(bmsvc.getItemType(newBkmk3_2Id), bmsvc.TYPE_SEPARATOR);
732  do_check_eq(bmsvc.getFolderIdForItem(newBkmk3_3Id), newBkmk3Id);
733  do_check_eq(bmsvc.getItemType(newBkmk3_3Id), bmsvc.TYPE_FOLDER);
734  do_check_eq(bmsvc.getItemTitle(newBkmk3_3Id), "folder");
735  // Check last added back item id.
736  // Notice items are restored in reverse order.
737  do_check_eq(observer._itemAddedId, newBkmk1Id);
738 
739  // Test creating an item with child transactions.
740  var childTxns = [];
741  var newDateAdded = Date.now() - 20000;
742  childTxns.push(ptSvc.editItemDateAdded(null, newDateAdded));
743  var itemChildAnnoObj = { name: "testAnno/testInt",
744  type: Ci.nsIAnnotationService.TYPE_INT32,
745  flags: 0,
746  value: 123,
747  expires: Ci.nsIAnnotationService.EXPIRE_NEVER };
748  childTxns.push(ptSvc.setItemAnnotation(null, itemChildAnnoObj));
749  var itemWChildTxn = ptSvc.createItem(uri("http://www.example.com"), root,
750  bmStartIndex, "Testing1", null, null,
751  childTxns);
752  try {
753  ptSvc.doTransaction(itemWChildTxn); // Also testing doTransaction
754  var itemId = (bmsvc.getBookmarkIdsForURI(uri("http://www.example.com"), {}))[0];
755  do_check_eq(observer._itemAddedId, itemId);
756  do_check_eq(newDateAdded, bmsvc.getItemDateAdded(itemId));
757  do_check_eq(observer._itemChangedProperty, "testAnno/testInt");
758  do_check_eq(observer._itemChanged_isAnnotationProperty, true);
759  do_check_true(annosvc.itemHasAnnotation(itemId, itemChildAnnoObj.name))
760  do_check_eq(annosvc.getItemAnnotation(itemId, itemChildAnnoObj.name),
761  itemChildAnnoObj.value);
762  itemWChildTxn.undoTransaction();
763  do_check_eq(observer._itemRemovedId, itemId);
764  itemWChildTxn.redoTransaction();
765  do_check_true(bmsvc.isBookmarked(uri("http://www.example.com")));
766  var newId = (bmsvc.getBookmarkIdsForURI(uri("http://www.example.com"), {}))[0];
767  do_check_eq(newDateAdded, bmsvc.getItemDateAdded(newId));
768  do_check_eq(observer._itemAddedId, newId);
769  do_check_eq(observer._itemChangedProperty, "testAnno/testInt");
770  do_check_eq(observer._itemChanged_isAnnotationProperty, true);
771  do_check_true(annosvc.itemHasAnnotation(newId, itemChildAnnoObj.name))
772  do_check_eq(annosvc.getItemAnnotation(newId, itemChildAnnoObj.name),
773  itemChildAnnoObj.value);
774  itemWChildTxn.undoTransaction();
775  do_check_eq(observer._itemRemovedId, newId);
776  } catch (ex) {
777  do_throw("Setting a child transaction in a createItem transaction did throw: " + ex);
778  }
779 
780  // Create a folder with child item transactions.
781  var childItemTxn = ptSvc.createItem(uri("http://www.childItem.com"), root, bmStartIndex, "childItem");
782  var folderWChildItemTxn = ptSvc.createFolder("Folder", root, bmStartIndex, null, [childItemTxn]);
783  try {
784  ptSvc.doTransaction(folderWChildItemTxn);
785  var childItemId = (bmsvc.getBookmarkIdsForURI(uri("http://www.childItem.com"), {}))[0];
786  do_check_eq(observer._itemAddedId, childItemId);
787  do_check_eq(observer._itemAddedIndex, 0);
788  do_check_true(bmsvc.isBookmarked(uri("http://www.childItem.com")));
789  folderWChildItemTxn.undoTransaction();
790  do_check_false(bmsvc.isBookmarked(uri("http://www.childItem.com")));
791  folderWChildItemTxn.redoTransaction();
792  newchildItemId = (bmsvc.getBookmarkIdsForURI(uri("http://www.childItem.com"), {}))[0];
793  do_check_eq(observer._itemAddedIndex, 0);
794  do_check_eq(observer._itemAddedId, newchildItemId);
795  do_check_true(bmsvc.isBookmarked(uri("http://www.childItem.com")));
796  folderWChildItemTxn.undoTransaction();
797  do_check_false(bmsvc.isBookmarked(uri("http://www.childItem.com")));
798  } catch (ex) {
799  do_throw("Setting a child item transaction in a createFolder transaction did throw: " + ex);
800  }
801 
802  bmsvc.removeObserver(observer, false);
803 }
var observer
const Cc
do_check_eq(typeof PlacesUtils,"object")
#define DESCRIPTION_ANNO
menuItem id
Definition: FeedWriter.js:971
onPageChanged onEndUpdateBatch
Definition: FeedWriter.js:1395
sbDeviceFirmwareAutoCheckForUpdate prototype flags
sbOSDControlService prototype QueryInterface
#define POST_DATA_ANNO
getService(Ci.sbIFaceplateManager)
const bmsvc
function run_test()
var bmStartIndex
onPageChanged onBeginUpdateBatch
Definition: FeedWriter.js:1395
return null
Definition: FeedWriter.js:1143
var uri
Definition: FeedWriter.js:1135
countRef value
Definition: FeedWriter.js:1423
const Cr
const Ci
#define LOAD_IN_SIDEBAR_ANNO