browser_library_middleclick.js
Go to the documentation of this file.
1 /* vim:set ts=2 sw=2 sts=2 et: */
2 /* ***** BEGIN LICENSE BLOCK *****
3  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4  *
5  * The contents of this file are subject to the Mozilla Public License Version
6  * 1.1 (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  * http://www.mozilla.org/MPL/
9  *
10  * Software distributed under the License is distributed on an "AS IS" basis,
11  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12  * for the specific language governing rights and limitations under the
13  * License.
14  *
15  * The Original Code is Places test code.
16  *
17  * The Initial Developer of the Original Code is Mozilla Corp.
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 gPrefs = Cc["@mozilla.org/preferences-service;1"].
46  getService(Ci.nsIPrefBranch);
47 const DISABLE_HISTORY_PREF = "browser.history_expire_days";
48 
49 var gLibrary = null;
50 var gTests = [];
52 
53 // Listener for TabOpen and tabs progress.
55  _loadedURIs: [],
56  _openTabsCount: 0,
57 
58  handleEvent: function(aEvent) {
59  if (aEvent.type != "TabOpen")
60  return;
61 
62  if (++this._openTabsCount == gCurrentTest.URIs.length) {
63  is(gBrowser.mTabs.length, gCurrentTest.URIs.length + 1,
64  "We have opened " + gCurrentTest.URIs.length + " new tab(s)");
65  }
66 
67  var tab = aEvent.target;
68  is(tab.ownerDocument.defaultView.getBrowser(), gBrowser,
69  "Tab has been opened in current browser window");
70  },
71 
72  onLocationChange: function(aBrowser, aWebProgress, aRequest, aLocationURI) {
73  var spec = aLocationURI.spec;
74  ok(true, spec);
75  // When a new tab is opened, location is first set to "about:blank", so
76  // we can ignore those calls.
77  // Ignore multiple notifications for the same URI too.
78  if (spec == "about:blank" || this._loadedURIs.indexOf(spec) != -1)
79  return;
80 
81  ok(gCurrentTest.URIs.indexOf(spec) != -1,
82  "Opened URI found in list: " + spec);
83 
84  if (gCurrentTest.URIs.indexOf(spec) != -1 )
85  this._loadedURIs.push(spec);
86 
87  var fm = Components.classes["@mozilla.org/focus-manager;1"].
88  getService(Components.interfaces.nsIFocusManager);
89  is(fm.activeWindow, gBrowser.ownerDocument.defaultView, "window made active");
90 
91  if (this._loadedURIs.length == gCurrentTest.URIs.length) {
92  // We have correctly opened all URIs.
93 
94  // Reset arrays.
95  this._loadedURIs.length = 0;
96  // Close all tabs.
97  while (gBrowser.mTabs.length > 1)
98  gBrowser.removeCurrentTab();
99  this._openTabsCount = 0;
100 
101  // Test finished. This will move to the next one.
102  gCurrentTest.finish();
103  }
104  },
105 
106  onProgressChange: function(aBrowser, aWebProgress, aRequest,
107  aCurSelfProgress, aMaxSelfProgress,
108  aCurTotalProgress, aMaxTotalProgress) {
109  },
110  onStateChange: function(aBrowser, aWebProgress, aRequest,
111  aStateFlags, aStatus) {
112  },
113  onStatusChange: function(aBrowser, aWebProgress, aRequest,
114  aStatus, aMessage) {
115  },
116  onSecurityChange: function(aBrowser, aWebProgress, aRequest, aState) {
117  },
118  noLinkIconAvailable: function(aBrowser) {
119  }
120 }
121 
122 //------------------------------------------------------------------------------
123 // Open bookmark in a new tab.
124 
125 gTests.push({
126  desc: "Open bookmark in a new tab.",
127  URIs: ["about:buildconfig"],
128  _itemId: -1,
129 
130  setup: function() {
131  var bs = PlacesUtils.bookmarks;
132  // Add a new unsorted bookmark.
133  this._itemId = bs.insertBookmark(bs.unfiledBookmarksFolder,
134  PlacesUtils._uri(this.URIs[0]),
135  bs.DEFAULT_INDEX,
136  "Title");
137  // Select unsorted bookmarks root in the left pane.
138  gLibrary.PlacesOrganizer.selectLeftPaneQuery("UnfiledBookmarks");
139  isnot(gLibrary.PlacesOrganizer._places.selectedNode, null,
140  "We correctly have selection in the Library left pane");
141  // Get our bookmark in the right pane.
142  var bookmarkNode = gLibrary.PlacesOrganizer._content.view.nodeForTreeIndex(0);
143  is(bookmarkNode.uri, this.URIs[0], "Found bookmark in the right pane");
144  },
145 
146  finish: function() {
148  },
149 
150  cleanup: function() {
151  PlacesUtils.bookmarks.removeItem(this._itemId);
152  }
153 });
154 
155 //------------------------------------------------------------------------------
156 // Open a folder in tabs.
157 
158 gTests.push({
159  desc: "Open a folder in tabs.",
160  URIs: ["about:buildconfig", "about:"],
161  _folderId: -1,
162 
163  setup: function() {
164  var bs = PlacesUtils.bookmarks;
165  // Create a new folder.
166  var folderId = bs.createFolder(bs.unfiledBookmarksFolder,
167  "Folder",
168  bs.DEFAULT_INDEX);
169  this._folderId = folderId;
170 
171  // Add bookmarks in folder.
172  this.URIs.forEach(function(aURI) {
173  bs.insertBookmark(folderId,
174  PlacesUtils._uri(aURI),
175  bs.DEFAULT_INDEX,
176  "Title");
177  });
178 
179  // Select unsorted bookmarks root in the left pane.
180  gLibrary.PlacesOrganizer.selectLeftPaneQuery("UnfiledBookmarks");
181  isnot(gLibrary.PlacesOrganizer._places.selectedNode, null,
182  "We correctly have selection in the Library left pane");
183  // Get our bookmark in the right pane.
184  var folderNode = gLibrary.PlacesOrganizer._content.view.nodeForTreeIndex(0);
185  is(folderNode.title, "Folder", "Found folder in the right pane");
186  },
187 
188  finish: function() {
190  },
191 
192  cleanup: function() {
193  PlacesUtils.bookmarks.removeItem(this._folderId);
194  }
195 });
196 
197 //------------------------------------------------------------------------------
198 // Open a query in tabs.
199 
200 gTests.push({
201  desc: "Open a query in tabs.",
202  URIs: ["about:buildconfig", "about:"],
203  _folderId: -1,
204  _queryId: -1,
205 
206  setup: function() {
207  var bs = PlacesUtils.bookmarks;
208  // Create a new folder.
209  var folderId = bs.createFolder(bs.unfiledBookmarksFolder,
210  "Folder",
211  bs.DEFAULT_INDEX);
212  this._folderId = folderId;
213 
214  // Add bookmarks in folder.
215  this.URIs.forEach(function(aURI) {
216  bs.insertBookmark(folderId,
217  PlacesUtils._uri(aURI),
218  bs.DEFAULT_INDEX,
219  "Title");
220  });
221 
222  // Create a bookmarks query containing our bookmarks.
223  var hs = PlacesUtils.history;
224  var options = hs.getNewQueryOptions();
225  options.queryType = Ci.nsINavHistoryQueryOptions.QUERY_TYPE_BOOKMARKS;
226  var query = hs.getNewQuery();
227  query.searchTerms = "about";
228  var queryString = hs.queriesToQueryString([query], 1, options);
229  this._queryId = bs.insertBookmark(bs.unfiledBookmarksFolder,
230  PlacesUtils._uri(queryString),
231  0, // It must be the first.
232  "Query");
233 
234  // Select unsorted bookmarks root in the left pane.
235  gLibrary.PlacesOrganizer.selectLeftPaneQuery("UnfiledBookmarks");
236  isnot(gLibrary.PlacesOrganizer._places.selectedNode, null,
237  "We correctly have selection in the Library left pane");
238  // Get our bookmark in the right pane.
239  var folderNode = gLibrary.PlacesOrganizer._content.view.nodeForTreeIndex(0);
240  is(folderNode.title, "Query", "Found query in the right pane");
241  },
242 
243  finish: function() {
245  },
246 
247  cleanup: function() {
248  PlacesUtils.bookmarks.removeItem(this._folderId);
249  PlacesUtils.bookmarks.removeItem(this._queryId);
250  }
251 });
252 
253 //------------------------------------------------------------------------------
254 
255 function test() {
257 
258  // Sanity checks.
259  ok(PlacesUtils, "PlacesUtils in context");
260  ok(PlacesUIUtils, "PlacesUIUtils in context");
261 
262  // Add tabs listeners.
263  gBrowser.tabContainer.addEventListener("TabOpen", gTabsListener, false);
264  gBrowser.addTabsProgressListener(gTabsListener);
265 
266  // Temporary disable history, so we won't record pages navigation.
267  gPrefs.setIntPref(DISABLE_HISTORY_PREF, 0);
268 
269  // Window watcher for Library window.
270  var ww = Cc["@mozilla.org/embedcomp/window-watcher;1"].
271  getService(Ci.nsIWindowWatcher);
272  var windowObserver = {
273  observe: function(aSubject, aTopic, aData) {
274  if (aTopic === "domwindowopened") {
275  ww.unregisterNotification(this);
276  gLibrary = aSubject.QueryInterface(Ci.nsIDOMWindow);
277  gLibrary.addEventListener("load", function onLoad(event) {
278  gLibrary.removeEventListener("load", onLoad, false);
279  // Kick off tests.
281  }, false);
282  }
283  }
284  };
285 
286  // Open Library window.
287  ww.registerNotification(windowObserver);
288  ww.openWindow(null,
289  "chrome://browser/content/places/places.xul",
290  "",
291  "chrome,toolbar=yes,dialog=no,resizable",
292  null);
293 }
294 
295 function runNextTest() {
296  // Cleanup from previous test.
297  if (gCurrentTest)
298  gCurrentTest.cleanup();
299 
300  if (gTests.length > 0) {
301  // Goto next test.
302  gCurrentTest = gTests.shift();
303  info("Start of test: " + gCurrentTest.desc);
304  // Test setup will set Library so that the bookmark to be opened is the
305  // first node in the content (right pane) tree.
306  gCurrentTest.setup();
307 
308  // Middle click on first node in the content tree of the Library.
309  gLibrary.PlacesOrganizer._content.focus();
310  mouseEventOnCell(gLibrary.PlacesOrganizer._content, 0, 0, { button: 1 });
311  }
312  else {
313  // No more tests.
314 
315  // Close Library window.
316  gLibrary.close();
317 
318  // Remove tabs listeners.
319  gBrowser.tabContainer.removeEventListener("TabOpen", gTabsListener, false);
320  gBrowser.removeTabsProgressListener(gTabsListener);
321 
322  // Restore history.
323  gPrefs.setIntPref(DISABLE_HISTORY_PREF, 180);
324 
325  finish();
326  }
327 }
328 
329 function mouseEventOnCell(aTree, aRowIndex, aColumnIndex, aEventDetails) {
330  var selection = aTree.view.selection;
331  selection.select(aRowIndex);
332  aTree.treeBoxObject.ensureRowIsVisible(aRowIndex);
333  var column = aTree.columns[aColumnIndex];
334 
335  // get cell coordinates
336  var x = {}, y = {}, width = {}, height = {};
337  aTree.treeBoxObject.getCoordsForCellItem(aRowIndex, column, "text",
338  x, y, width, height);
339 
340  EventUtils.synthesizeMouse(aTree.body, x.value, y.value,
341  aEventDetails, gLibrary);
342 }
var PlacesUIUtils
Definition: utils.js:85
function setup()
var event
const DISABLE_HISTORY_PREF
var tab
getService(Ci.sbIFaceplateManager)
function width(ele) rect(ele).width
function test()
Lastfm onLoad
Definition: mini.js:36
waitForExplicitFinish()
aWindow setTimeout(function(){_this.restoreHistory(aWindow, aTabs, aTabData, aIdMap);}, 0)
function runNextTest()
return null
Definition: FeedWriter.js:1143
_updateDatepicker height
function cleanup()
_updateTextAndScrollDataForTab aBrowser
foldersync options
Definition: options.js:13
ContinuingWebProgressListener prototype onStateChange
function mouseEventOnCell(aTree, aRowIndex, aColumnIndex, aEventDetails)
restoreWindow aState
_updateTextAndScrollDataForFrame aData
sbDeviceFirmwareAutoCheckForUpdate prototype observe