browser_library_views_liveupdate.js
Go to the documentation of this file.
1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3  *
4  * The contents of this file are subject to the Mozilla Public License Version
5  * 1.1 (the "License"); you may not use this file except in compliance with
6  * the License. You may obtain a copy of the License at
7  * http://www.mozilla.org/MPL/
8  *
9  * Software distributed under the License is distributed on an "AS IS" basis,
10  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11  * for the specific language governing rights and limitations under the
12  * License.
13  *
14  * The Original Code is mozilla.org code.
15  *
16  * The Initial Developer of the Original Code is
17  * Mozilla Corporation.
18  * Portions created by the Initial Developer are Copyright (C) 2009
19  * the Initial Developer. All Rights Reserved.
20  *
21  * Contributor(s):
22  * Marco Bonardo <mak77@bonardo.net>
23  *
24  * Alternatively, the contents of this file may be used under the terms of
25  * either the GNU General Public License Version 2 or later (the "GPL"), or
26  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27  * in which case the provisions of the GPL or the LGPL are applicable instead
28  * of those above. If you wish to allow use of your version of this file only
29  * under the terms of either the GPL or the LGPL, and not to allow others to
30  * use your version of this file under the terms of the MPL, indicate your
31  * decision by deleting the provisions above and replace them with the notice
32  * and other provisions required by the GPL or the LGPL. If you do not delete
33  * the provisions above, a recipient may use your version of this file under
34  * the terms of any one of the MPL, the GPL or the LGPL.
35  *
36  * ***** END LICENSE BLOCK ***** */
37 
42 const Cc = Components.classes;
43 const Ci = Components.interfaces;
44 
45 var gLibrary = null;
46 
47 function test() {
49 
50  // Sanity checks.
51  ok(PlacesUtils, "PlacesUtils in context");
52  ok(PlacesUIUtils, "PlacesUIUtils in context");
53 
54  // Open Library, we will check the left pane.
55  var ww = Cc["@mozilla.org/embedcomp/window-watcher;1"].
56  getService(Ci.nsIWindowWatcher);
57  var windowObserver = {
58  observe: function(aSubject, aTopic, aData) {
59  if (aTopic === "domwindowopened") {
60  ww.unregisterNotification(this);
61  gLibrary = aSubject.QueryInterface(Ci.nsIDOMWindow);
62  gLibrary.addEventListener("load", function onLoad(event) {
63  gLibrary.removeEventListener("load", onLoad, false);
64  executeSoon(startTest);
65  }, false);
66  }
67  }
68  };
69  ww.registerNotification(windowObserver);
70  ww.openWindow(null,
71  "chrome://browser/content/places/places.xul",
72  "",
73  "chrome,toolbar=yes,dialog=no,resizable",
74  null);
75 }
76 
80 function startTest() {
81  var bs = PlacesUtils.bookmarks;
82  // Add bookmarks observer.
83  bs.addObserver(bookmarksObserver, false);
84  var addedBookmarks = [];
85 
86  // MENU
87  ok(true, "*** Acting on menu bookmarks");
88  var id = bs.insertBookmark(bs.bookmarksMenuFolder,
89  PlacesUtils._uri("http://bm1.mozilla.org/"),
90  bs.DEFAULT_INDEX,
91  "bm1");
92  addedBookmarks.push(id);
93  id = bs.insertBookmark(bs.bookmarksMenuFolder,
94  PlacesUtils._uri("place:"),
95  bs.DEFAULT_INDEX,
96  "bm2");
97  addedBookmarks.push(id);
98  id = bs.insertSeparator(bs.bookmarksMenuFolder, bs.DEFAULT_INDEX);
99  addedBookmarks.push(id);
100  id = bs.createFolder(bs.bookmarksMenuFolder,
101  "bmf",
102  bs.DEFAULT_INDEX);
103  addedBookmarks.push(id);
104  id = bs.insertBookmark(id,
105  PlacesUtils._uri("http://bmf1.mozilla.org/"),
106  bs.DEFAULT_INDEX,
107  "bmf1");
108  addedBookmarks.push(id);
109  bs.moveItem(id, bs.bookmarksMenuFolder, 0);
110 
111  // TOOLBAR
112  ok(true, "*** Acting on toolbar bookmarks");
113  bs.insertBookmark(bs.toolbarFolder,
114  PlacesUtils._uri("http://tb1.mozilla.org/"),
115  bs.DEFAULT_INDEX,
116  "tb1");
117  addedBookmarks.push(id);
118  id = bs.insertBookmark(bs.toolbarFolder,
119  PlacesUtils._uri("place:"),
120  bs.DEFAULT_INDEX,
121  "tb2");
122  addedBookmarks.push(id);
123  id = bs.insertSeparator(bs.toolbarFolder, bs.DEFAULT_INDEX);
124  addedBookmarks.push(id);
125  id = bs.createFolder(bs.toolbarFolder,
126  "tbf",
127  bs.DEFAULT_INDEX);
128  addedBookmarks.push(id);
129  id = bs.insertBookmark(id,
130  PlacesUtils._uri("http://tbf1.mozilla.org/"),
131  bs.DEFAULT_INDEX,
132  "bmf1");
133  addedBookmarks.push(id);
134  bs.moveItem(id, bs.toolbarFolder, 0);
135 
136  // UNSORTED
137  ok(true, "*** Acting on unsorted bookmarks");
138  id = bs.insertBookmark(bs.unfiledBookmarksFolder,
139  PlacesUtils._uri("http://ub1.mozilla.org/"),
140  bs.DEFAULT_INDEX,
141  "ub1");
142  addedBookmarks.push(id);
143  id = bs.insertBookmark(bs.unfiledBookmarksFolder,
144  PlacesUtils._uri("place:"),
145  bs.DEFAULT_INDEX,
146  "ub2");
147  addedBookmarks.push(id);
148  id = bs.insertSeparator(bs.unfiledBookmarksFolder, bs.DEFAULT_INDEX);
149  addedBookmarks.push(id);
150  id = bs.createFolder(bs.unfiledBookmarksFolder,
151  "ubf",
152  bs.DEFAULT_INDEX);
153  addedBookmarks.push(id);
154  id = bs.insertBookmark(id,
155  PlacesUtils._uri("http://ubf1.mozilla.org/"),
156  bs.DEFAULT_INDEX,
157  "ubf1");
158  addedBookmarks.push(id);
159  bs.moveItem(id, bs.unfiledBookmarksFolder, 0);
160 
161  // Remove all added bookmarks.
162  addedBookmarks.forEach(function (aItem) {
163  // If we remove an item after its containing folder has been removed,
164  // this will throw, but we can ignore that.
165  try {
166  bs.removeItem(aItem);
167  } catch (ex) {}
168  });
169 
170  // Remove bookmarks observer.
171  bs.removeObserver(bookmarksObserver);
172  finishTest();
173 }
174 
178 function finishTest() {
179  // Close Library window.
180  gLibrary.close();
181  finish();
182 }
183 
189  QueryInterface: function PSB_QueryInterface(aIID) {
190  if (aIID.equals(Ci.nsINavBookmarkObserver) ||
191  aIID.equals(Ci.nsISupports))
192  return this;
193  throw Cr.NS_NOINTERFACE;
194  },
195 
196  // nsINavBookmarkObserver
197  onItemAdded: function PSB_onItemAdded(aItemId, aFolderId, aIndex) {
198  var node = null;
199  var index = null;
200  [node, index] = getNodeForTreeItem(aItemId, gLibrary.PlacesOrganizer._places);
201  // Left pane should not be updated for normal bookmarks or separators.
202  var type = PlacesUtils.bookmarks.getItemType(aItemId);
203  switch (type) {
204  case PlacesUtils.bookmarks.TYPE_BOOKMARK:
205  var uriString = PlacesUtils.bookmarks.getBookmarkURI(aItemId).spec;
206  var isQuery = uriString.substr(0, 6) == "place:";
207  if (isQuery) {
208  isnot(node, null, "Found new Places node in left pane");
209  ok(index >= 0, "Node is at index " + index);
210  break;
211  }
212  // Fallback to separator case if this is not a query.
213  case PlacesUtils.bookmarks.TYPE_SEPARATOR:
214  is(node, null, "New Places node not added in left pane");
215  break;
216  default:
217  isnot(node, null, "Found new Places node in left pane");
218  ok(index >= 0, "Node is at index " + index);
219  }
220  },
221 
222  onItemRemoved: function PSB_onItemRemoved(aItemId, aFolder, aIndex) {
223  var node = null;
224  var index = null;
225  [node, index] = getNodeForTreeItem(aItemId, gLibrary.PlacesOrganizer._places);
226  is(node, null, "Places node not found in left pane");
227  },
228 
229  onItemMoved: function(aItemId,
230  aOldFolderId, aOldIndex,
231  aNewFolderId, aNewIndex) {
232  var node = null;
233  var index = null;
234  [node, index] = getNodeForTreeItem(aItemId, gLibrary.PlacesOrganizer._places);
235  // Left pane should not be updated for normal bookmarks or separators.
236  var type = PlacesUtils.bookmarks.getItemType(aItemId);
237  switch (type) {
238  case PlacesUtils.bookmarks.TYPE_BOOKMARK:
239  var uriString = PlacesUtils.bookmarks.getBookmarkURI(aItemId).spec;
240  var isQuery = uriString.substr(0, 6) == "place:";
241  if (isQuery) {
242  isnot(node, null, "Found new Places node in left pane");
243  ok(index >= 0, "Node is at index " + index);
244  break;
245  }
246  // Fallback to separator case if this is not a query.
247  case type == PlacesUtils.bookmarks.TYPE_SEPARATOR:
248  is(node, null, "New Places node not added in left pane");
249  break;
250  default:
251  isnot(node, null, "Found new Places node in left pane");
252  ok(index >= 0, "Node is at index " + index);
253  }
254  },
255 
256  onBeginUpdateBatch: function PSB_onBeginUpdateBatch() {},
257  onEndUpdateBatch: function PSB_onEndUpdateBatch() {},
258  onBeforeItemRemoved: function PSB_onBeforeItemRemoved(aItemId) {},
259  onItemVisited: function() {},
260  onItemChanged: function PSB_onItemChanged(aItemId, aProperty,
261  aIsAnnotationProperty, aValue) {}
262 };
263 
264 
272 function getNodeForTreeItem(aItemId, aTree) {
273 
274  function findNode(aContainerIndex) {
275  if (aTree.view.isContainerEmpty(aContainerIndex))
276  return [null, null];
277 
278  // The rowCount limit is just for sanity, but we will end looping when
279  // we have checked the last child of this container or we have found node.
280  for (var i = aContainerIndex + 1; i < aTree.view.rowCount; i++) {
281  var node = aTree.view.nodeForTreeIndex(i);
282 
283  if (node.itemId == aItemId) {
284  // Minus one because we want relative index inside the container.
285  return [node, i - aTree.view.getParentIndex(i) - 1];
286  }
287 
288  if (PlacesUtils.nodeIsFolder(node)) {
289  // Open container.
290  aTree.view.toggleOpenState(i);
291  // Search inside it.
292  var foundNode = findNode(i);
293  // Close container.
294  aTree.view.toggleOpenState(i);
295  // Return node if found.
296  if (foundNode[0] != null)
297  return foundNode;
298  }
299 
300  // We have finished walking this container.
301  if (!aTree.view.hasNextSibling(aContainerIndex + 1, i))
302  break;
303  }
304  return [null, null]
305  }
306 
307  // Root node is hidden, so we need to manually walk the first level.
308  for (var i = 0; i < aTree.view.rowCount; i++) {
309  // Open container.
310  aTree.view.toggleOpenState(i);
311  // Search inside it.
312  var foundNode = findNode(i);
313  // Close container.
314  aTree.view.toggleOpenState(i);
315  // Return node if found.
316  if (foundNode[0] != null)
317  return foundNode;
318  }
319  return [null, null];
320 }
var PlacesUIUtils
Definition: utils.js:85
onPageChanged aValue
Definition: FeedWriter.js:1395
onPageChanged onEndUpdateBatch
Definition: FeedWriter.js:1395
var event
sbOSDControlService prototype QueryInterface
function finishTest()
getService(Ci.sbIFaceplateManager)
Lastfm onLoad
Definition: mini.js:36
waitForExplicitFinish()
function startTest()
onPageChanged onBeginUpdateBatch
Definition: FeedWriter.js:1395
return null
Definition: FeedWriter.js:1143
let node
const Cr
function getNodeForTreeItem(aItemId, aTree)
_getSelectedPageStyle s i
_updateTextAndScrollDataForFrame aData
sbDeviceFirmwareAutoCheckForUpdate prototype observe