browser_library_search.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 Places test code.
17  *
18  * The Initial Developer of the Original Code is Mozilla Corp.
19  * Portions created by the Initial Developer are Copyright (C) 2009
20  * the Initial Developer. All Rights Reserved.
21  *
22  * Contributor(s):
23  * Drew Willcoxon <adw@mozilla.com> (Original Author)
24  *
25  * Alternatively, the contents of this file may be used under the terms of
26  * either the GNU General Public License Version 2 or later (the "GPL"), or
27  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28  * in which case the provisions of the GPL or the LGPL are applicable instead
29  * of those above. If you wish to allow use of your version of this file only
30  * under the terms of either the GPL or the LGPL, and not to allow others to
31  * use your version of this file under the terms of the MPL, indicate your
32  * decision by deleting the provisions above and replace them with the notice
33  * and other provisions required by the GPL or the LGPL. If you do not delete
34  * the provisions above, a recipient may use your version of this file under
35  * the terms of any one of the MPL, the GPL or the LGPL.
36  *
37  * ***** END LICENSE BLOCK ***** */
38 
63 const TEST_URL = "http://dummy.mozilla.org/";
64 
65 // Add your tests here. Each is a function that's called by testHelper().
66 var testCases = [
67 
68  // All Bookmarks
69  function () {
70  var defScope = getDefaultScope(PlacesUIUtils.allBookmarksFolderId);
71  search(PlacesUIUtils.allBookmarksFolderId, "dummy", defScope);
72  is(selectScope("scopeBarFolder"), false,
73  "Folder scope should be disabled for All Bookmarks");
74  resetSearch(defScope);
75  },
76 
77  // History
78  function () {
79  var defScope = getDefaultScope(PlacesUIUtils.leftPaneQueries["History"]);
80  search(PlacesUIUtils.leftPaneQueries["History"], "dummy", defScope);
81  is(selectScope("scopeBarFolder"), false,
82  "Folder scope should be disabled for History");
83  resetSearch(defScope);
84  },
85 
86  // Toolbar folder
87  function () {
88  var defScope = getDefaultScope(bmsvc.toolbarFolder);
89  search(bmsvc.toolbarFolder, "dummy", defScope);
90  is(selectScope("scopeBarFolder"), true,
91  "Folder scope should be enabled for toolbar folder");
92  // Ensure that folder scope is still selected after resetting and searching
93  // again.
94  resetSearch("scopeBarFolder");
95  search(bmsvc.toolbarFolder, "dummy", "scopeBarFolder");
96  },
97 
98  // A regular non-root subfolder
99  function () {
100  var folderId = bmsvc.createFolder(bmsvc.toolbarFolder,
101  "dummy folder",
102  bmsvc.DEFAULT_INDEX);
103  var defScope = getDefaultScope(folderId);
104  search(folderId, "dummy", defScope);
105  is(selectScope("scopeBarFolder"), true,
106  "Folder scope should be enabled for regular subfolder");
107  // Ensure that folder scope is still selected after resetting and searching
108  // again.
109  resetSearch("scopeBarFolder");
110  search(folderId, "dummy", "scopeBarFolder");
111  bmsvc.removeItem(folderId);
112  },
113 ];
114 
116 
117 const bmsvc = Cc["@mozilla.org/browser/nav-bookmarks-service;1"].
118  getService(Ci.nsINavBookmarksService);
119 const histsvc = Cc["@mozilla.org/browser/nav-history-service;1"].
120  getService(Ci.nsINavHistoryService);
122 
124 
132 function getDefaultScope(aFolderId) {
133  return aFolderId === PlacesUIUtils.leftPaneQueries["History"] ?
134  "scopeBarHistory" :
135  "scopeBarAll";
136 }
137 
144  var doc = libraryWin.document;
145  var scopeButtons = doc.getElementById("organizerScopeBar").childNodes;
146  for (let i = 0; i < scopeButtons.length; i++) {
147  if (scopeButtons[i].checked)
148  return scopeButtons[i].id;
149  }
150  return null;
151 }
152 
160 function queryStringToQuery(aPlaceURI) {
161  var queries = {};
162  histsvc.queryStringToQueries(aPlaceURI, queries, {}, {});
163  return queries.value[0];
164 }
165 
173 function resetSearch(aExpectedScopeButtonId) {
174  search(null, "", aExpectedScopeButtonId);
175 }
176 
190 function search(aFolderId, aSearchStr, aExpectedScopeButtonId) {
191  var doc = libraryWin.document;
192  var folderTree = doc.getElementById("placesList");
193  var contentTree = doc.getElementById("placeContent");
194 
195  // First, ensure that selecting the folder in the left pane updates the
196  // content tree properly.
197  if (aFolderId) {
198  folderTree.selectItems([aFolderId]);
199  isnot(folderTree.selectedNode, null,
200  "Sanity check: left pane tree should have selection after selecting!");
201 
202  // getFolders() on a History query returns an empty array, so no use
203  // comparing against aFolderId in that case.
204  if (aFolderId !== PlacesUIUtils.leftPaneQueries["History"]) {
205  // contentTree.place should be equal to contentTree.getResult().root.uri,
206  // but it's not until bug 476952 is fixed.
207  var query = queryStringToQuery(contentTree.getResult().root.uri);
208  is(query.getFolders({}, {})[0], aFolderId,
209  "Content tree's folder should be what was selected in the left pane");
210  }
211  }
212 
213  // Second, ensure that searching updates the content tree and search UI
214  // properly.
215  var searchBox = doc.getElementById("searchFilter");
216  searchBox.value = aSearchStr;
217  libraryWin.PlacesSearchBox.search(searchBox.value);
218  query = queryStringToQuery(contentTree.getResult().root.uri);
219  if (aSearchStr) {
220  is(query.searchTerms, aSearchStr,
221  "Content tree's searchTerms should be text in search box");
222  is(doc.getElementById("searchModifiers").hidden, false,
223  "Scope bar should not be hidden after searching");
224  if (getSelectedScopeButtonId() == "scopeBarHistory" ||
225  getSelectedScopeButtonId() == "scopeBarAll" ||
226  aFolderId == PlacesUtils.bookmarks.unfiledBookmarksFolder) {
227  // Check that search has returned a valid result.
228  contentTree.view.selection.select(0);
229  var foundNode = contentTree.selectedNode;
230  isnot(foundNode, null, "Found a valid node");
231  is(foundNode.uri, TEST_URL);
232  }
233  }
234  else {
235  is(query.hasSearchTerms, false,
236  "Content tree's searchTerms should not exist after search reset");
237  ok(doc.getElementById("searchModifiers").hidden,
238  "Scope bar should be hidden after search reset");
239  }
240  is(getSelectedScopeButtonId(), aExpectedScopeButtonId,
241  "Proper scope button should be selected after searching or resetting");
242 }
243 
251 function selectScope(aScopeButtonId) {
252  var doc = libraryWin.document;
253  var button = doc.getElementById(aScopeButtonId);
254  isnot(button, null,
255  "Sanity check: scope button with ID " + aScopeButtonId + "should exist");
256  // Bug 469436 may hide an inappropriate scope button instead of disabling it.
257  if (button.disabled || button.hidden)
258  return false;
259  button.click();
260  return true;
261 }
262 
270 function testHelper(aLibraryWin) {
271  libraryWin = aLibraryWin;
272  testCases.forEach(function (aTest) aTest());
273  aLibraryWin.close();
274 
275  // Cleanup.
276  PlacesUtils.tagging.untagURI(PlacesUtils._uri(TEST_URL), ["dummyTag"]);
277  PlacesUtils.bookmarks.removeFolderChildren(PlacesUtils.bookmarks.unfiledBookmarksFolder);
278  PlacesUtils.history.QueryInterface(Ci.nsIBrowserHistory).removeAllPages();
279 
280  finish();
281 }
282 
284 
285 function test() {
287 
288  // Sanity:
289  ok(PlacesUtils, "PlacesUtils in context");
290  // Add a visit, a bookmark and a tag.
291  PlacesUtils.history.addVisit(PlacesUtils._uri(TEST_URL),
292  Date.now() * 1000, null,
293  PlacesUtils.history.TRANSITION_TYPED, false, 0);
294  PlacesUtils.bookmarks.insertBookmark(PlacesUtils.bookmarks.unfiledBookmarksFolder,
295  PlacesUtils._uri(TEST_URL),
296  PlacesUtils.bookmarks.DEFAULT_INDEX,
297  "dummy");
298  PlacesUtils.tagging.tagURI(PlacesUtils._uri(TEST_URL), ["dummyTag"]);
299 
300  var ww = Cc["@mozilla.org/embedcomp/window-watcher;1"].
301  getService(Ci.nsIWindowWatcher);
302 
303  var windowObserver = {
304  observe: function(aSubject, aTopic, aData) {
305  if (aTopic === "domwindowopened") {
306  ww.unregisterNotification(this);
307  var win = aSubject.QueryInterface(Ci.nsIDOMWindow);
308  win.addEventListener("load", function onLoad(event) {
309  win.removeEventListener("load", onLoad, false);
310  executeSoon(function () testHelper(win));
311  }, false);
312  }
313  }
314  };
315 
316  ww.registerNotification(windowObserver);
317  ww.openWindow(null,
318  "chrome://browser/content/places/places.xul",
319  "",
320  "chrome,toolbar=yes,dialog=no,resizable",
321  null);
322 }
var PlacesUIUtils
Definition: utils.js:85
function getSelectedScopeButtonId()
const Cc
var libraryWin
function doc() browser.contentDocument
var event
function resetSearch(aExpectedScopeButtonId)
const TEST_URL
const histsvc
function selectScope(aScopeButtonId)
getService(Ci.sbIFaceplateManager)
function testHelper(aLibraryWin)
var testCases
function test()
function search(aFolderId, aSearchStr, aExpectedScopeButtonId)
const bmsvc
Lastfm onLoad
Definition: mini.js:36
waitForExplicitFinish()
function queryStringToQuery(aPlaceURI)
function getDefaultScope(aFolderId)
return null
Definition: FeedWriter.js:1143
const Ci
_getSelectedPageStyle s i
_updateTextAndScrollDataForFrame aData
sbDeviceFirmwareAutoCheckForUpdate prototype observe