|
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 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
4 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
6 |
|
7 /** |
|
8 * Bug 451151 |
|
9 * https://bugzilla.mozilla.org/show_bug.cgi?id=451151 |
|
10 * |
|
11 * Summary: |
|
12 * Tests frontend Places Library searching -- search, search reset, search scope |
|
13 * consistency. |
|
14 * |
|
15 * Details: |
|
16 * Each test below |
|
17 * 1. selects a folder in the left pane and ensures that the content tree is |
|
18 * appropriately updated, |
|
19 * 2. performs a search and ensures that the content tree is correct for the |
|
20 * folder and search and that the search UI is visible and appropriate to |
|
21 * folder, |
|
22 * 5. resets the search and ensures that the content tree is correct and that |
|
23 * the search UI is hidden, and |
|
24 * 6. if folder scope was clicked, searches again and ensures folder scope |
|
25 * remains selected. |
|
26 */ |
|
27 |
|
28 const TEST_URL = "http://dummy.mozilla.org/"; |
|
29 const TEST_DOWNLOAD_URL = "http://dummy.mozilla.org/dummy.pdf"; |
|
30 |
|
31 let gLibrary; |
|
32 |
|
33 let testCases = [ |
|
34 function allBookmarksScope() { |
|
35 let defScope = getDefaultScope(PlacesUIUtils.allBookmarksFolderId); |
|
36 search(PlacesUIUtils.allBookmarksFolderId, "dummy", defScope); |
|
37 }, |
|
38 |
|
39 function historyScope() { |
|
40 let defScope = getDefaultScope(PlacesUIUtils.leftPaneQueries["History"]); |
|
41 search(PlacesUIUtils.leftPaneQueries["History"], "dummy", defScope); |
|
42 }, |
|
43 |
|
44 function downloadsScope() { |
|
45 let defScope = getDefaultScope(PlacesUIUtils.leftPaneQueries["Downloads"]); |
|
46 search(PlacesUIUtils.leftPaneQueries["Downloads"], "dummy", defScope); |
|
47 }, |
|
48 ]; |
|
49 |
|
50 /////////////////////////////////////////////////////////////////////////////// |
|
51 |
|
52 /** |
|
53 * Returns the default search scope for a given folder. |
|
54 * |
|
55 * @param aFolderId |
|
56 * the item ID of a node in the left pane's tree |
|
57 * @return the default scope when the folder is newly selected |
|
58 */ |
|
59 function getDefaultScope(aFolderId) { |
|
60 switch (aFolderId) { |
|
61 case PlacesUIUtils.leftPaneQueries["History"]: |
|
62 return "scopeBarHistory" |
|
63 case PlacesUIUtils.leftPaneQueries["Downloads"]: |
|
64 return "scopeBarDownloads"; |
|
65 default: |
|
66 return "scopeBarAll"; |
|
67 } |
|
68 } |
|
69 |
|
70 /** |
|
71 * Returns the single nsINavHistoryQuery represented by a given place URI. |
|
72 * |
|
73 * @param aPlaceURI |
|
74 * a URI that represents a single query |
|
75 * @return an nsINavHistoryQuery object |
|
76 */ |
|
77 function queryStringToQuery(aPlaceURI) { |
|
78 let queries = {}; |
|
79 PlacesUtils.history.queryStringToQueries(aPlaceURI, queries, {}, {}); |
|
80 return queries.value[0]; |
|
81 } |
|
82 |
|
83 /** |
|
84 * Resets the search by clearing the search box's text and ensures that the |
|
85 * search scope remains as expected. |
|
86 * |
|
87 * @param aExpectedScopeButtonId |
|
88 * this button should be selected after the reset |
|
89 */ |
|
90 function resetSearch(aExpectedScopeButtonId) { |
|
91 search(null, "", aExpectedScopeButtonId); |
|
92 } |
|
93 |
|
94 /** |
|
95 * Performs a search for a given folder and search string and ensures that the |
|
96 * URI of the right pane's content tree is as expected for the folder and search |
|
97 * string. Also ensures that the search scope button is as expected after the |
|
98 * search. |
|
99 * |
|
100 * @param aFolderId |
|
101 * the item ID of a node in the left pane's tree |
|
102 * @param aSearchStr |
|
103 * the search text; may be empty to reset the search |
|
104 * @param aExpectedScopeButtonId |
|
105 * after searching the selected scope button should be this |
|
106 */ |
|
107 function search(aFolderId, aSearchStr, aExpectedScopeButtonId) { |
|
108 let doc = gLibrary.document; |
|
109 let folderTree = doc.getElementById("placesList"); |
|
110 let contentTree = doc.getElementById("placeContent"); |
|
111 |
|
112 // First, ensure that selecting the folder in the left pane updates the |
|
113 // content tree properly. |
|
114 if (aFolderId) { |
|
115 folderTree.selectItems([aFolderId]); |
|
116 isnot(folderTree.selectedNode, null, |
|
117 "Sanity check: left pane tree should have selection after selecting!"); |
|
118 |
|
119 // getFolders() on a History query returns an empty array, so no use |
|
120 // comparing against aFolderId in that case. |
|
121 if (aFolderId !== PlacesUIUtils.leftPaneQueries["History"] && |
|
122 aFolderId !== PlacesUIUtils.leftPaneQueries["Downloads"]) { |
|
123 // contentTree.place should be equal to contentTree.result.root.uri, |
|
124 // but it's not until bug 476952 is fixed. |
|
125 let query = queryStringToQuery(contentTree.result.root.uri); |
|
126 is(query.getFolders()[0], aFolderId, |
|
127 "Content tree's folder should be what was selected in the left pane"); |
|
128 } |
|
129 } |
|
130 |
|
131 // Second, ensure that searching updates the content tree and search UI |
|
132 // properly. |
|
133 let searchBox = doc.getElementById("searchFilter"); |
|
134 searchBox.value = aSearchStr; |
|
135 gLibrary.PlacesSearchBox.search(searchBox.value); |
|
136 let query = queryStringToQuery(contentTree.result.root.uri); |
|
137 if (aSearchStr) { |
|
138 is(query.searchTerms, aSearchStr, |
|
139 "Content tree's searchTerms should be text in search box"); |
|
140 } |
|
141 else { |
|
142 is(query.hasSearchTerms, false, |
|
143 "Content tree's searchTerms should not exist after search reset"); |
|
144 } |
|
145 } |
|
146 |
|
147 /** |
|
148 * test() contains window-launching boilerplate that calls this to really kick |
|
149 * things off. Add functions to the testCases array, and this will call them. |
|
150 */ |
|
151 function onLibraryAvailable() { |
|
152 testCases.forEach(function (aTest) aTest()); |
|
153 |
|
154 gLibrary.close(); |
|
155 gLibrary = null; |
|
156 |
|
157 // Cleanup. |
|
158 PlacesUtils.tagging.untagURI(PlacesUtils._uri(TEST_URL), ["dummyTag"]); |
|
159 PlacesUtils.bookmarks.removeFolderChildren(PlacesUtils.unfiledBookmarksFolderId); |
|
160 waitForClearHistory(finish); |
|
161 } |
|
162 |
|
163 /////////////////////////////////////////////////////////////////////////////// |
|
164 |
|
165 function test() { |
|
166 waitForExplicitFinish(); |
|
167 |
|
168 // Sanity: |
|
169 ok(PlacesUtils, "PlacesUtils in context"); |
|
170 |
|
171 // Add visits, a bookmark and a tag. |
|
172 addVisits( |
|
173 [{ uri: PlacesUtils._uri(TEST_URL), visitDate: Date.now() * 1000, |
|
174 transition: PlacesUtils.history.TRANSITION_TYPED }, |
|
175 { uri: PlacesUtils._uri(TEST_DOWNLOAD_URL), visitDate: Date.now() * 1000, |
|
176 transition: PlacesUtils.history.TRANSITION_DOWNLOAD }], |
|
177 window, |
|
178 function() { |
|
179 PlacesUtils.bookmarks.insertBookmark(PlacesUtils.unfiledBookmarksFolderId, |
|
180 PlacesUtils._uri(TEST_URL), |
|
181 PlacesUtils.bookmarks.DEFAULT_INDEX, |
|
182 "dummy"); |
|
183 PlacesUtils.tagging.tagURI(PlacesUtils._uri(TEST_URL), ["dummyTag"]); |
|
184 |
|
185 gLibrary = openLibrary(onLibraryAvailable); |
|
186 }); |
|
187 } |