|
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 // Get history service |
|
8 var histsvc = PlacesUtils.history; |
|
9 var bhist = PlacesUtils.bhistory; |
|
10 var bmsvc = PlacesUtils.bookmarks; |
|
11 |
|
12 var resultObserver = { |
|
13 insertedNode: null, |
|
14 nodeInserted: function(parent, node, newIndex) { |
|
15 this.insertedNode = node; |
|
16 }, |
|
17 removedNode: null, |
|
18 nodeRemoved: function(parent, node, oldIndex) { |
|
19 this.removedNode = node; |
|
20 }, |
|
21 |
|
22 nodeAnnotationChanged: function() {}, |
|
23 |
|
24 newTitle: "", |
|
25 nodeChangedByTitle: null, |
|
26 nodeTitleChanged: function(node, newTitle) { |
|
27 this.nodeChangedByTitle = node; |
|
28 this.newTitle = newTitle; |
|
29 }, |
|
30 |
|
31 newAccessCount: 0, |
|
32 newTime: 0, |
|
33 nodeChangedByHistoryDetails: null, |
|
34 nodeHistoryDetailsChanged: function(node, |
|
35 updatedVisitDate, |
|
36 updatedVisitCount) { |
|
37 this.nodeChangedByHistoryDetails = node |
|
38 this.newTime = updatedVisitDate; |
|
39 this.newAccessCount = updatedVisitCount; |
|
40 }, |
|
41 |
|
42 movedNode: null, |
|
43 nodeMoved: function(node, oldParent, oldIndex, newParent, newIndex) { |
|
44 this.movedNode = node; |
|
45 }, |
|
46 openedContainer: null, |
|
47 closedContainer: null, |
|
48 containerStateChanged: function (aNode, aOldState, aNewState) { |
|
49 if (aNewState == Ci.nsINavHistoryContainerResultNode.STATE_OPENED) { |
|
50 this.openedContainer = aNode; |
|
51 } |
|
52 else if (aNewState == Ci.nsINavHistoryContainerResultNode.STATE_CLOSED) { |
|
53 this.closedContainer = aNode; |
|
54 } |
|
55 }, |
|
56 invalidatedContainer: null, |
|
57 invalidateContainer: function(node) { |
|
58 this.invalidatedContainer = node; |
|
59 }, |
|
60 sortingMode: null, |
|
61 sortingChanged: function(sortingMode) { |
|
62 this.sortingMode = sortingMode; |
|
63 }, |
|
64 inBatchMode: false, |
|
65 batching: function(aToggleMode) { |
|
66 do_check_neq(this.inBatchMode, aToggleMode); |
|
67 this.inBatchMode = aToggleMode; |
|
68 }, |
|
69 result: null, |
|
70 reset: function() { |
|
71 this.insertedNode = null; |
|
72 this.removedNode = null; |
|
73 this.nodeChangedByTitle = null; |
|
74 this.nodeChangedByHistoryDetails = null; |
|
75 this.replacedNode = null; |
|
76 this.movedNode = null; |
|
77 this.openedContainer = null; |
|
78 this.closedContainer = null; |
|
79 this.invalidatedContainer = null; |
|
80 this.sortingMode = null; |
|
81 } |
|
82 }; |
|
83 |
|
84 var testURI = uri("http://mozilla.com"); |
|
85 |
|
86 function run_test() { |
|
87 run_next_test(); |
|
88 } |
|
89 |
|
90 add_test(function check_history_query() { |
|
91 var options = histsvc.getNewQueryOptions(); |
|
92 options.sortingMode = options.SORT_BY_DATE_DESCENDING; |
|
93 options.resultType = options.RESULTS_AS_VISIT; |
|
94 var query = histsvc.getNewQuery(); |
|
95 var result = histsvc.executeQuery(query, options); |
|
96 result.addObserver(resultObserver, false); |
|
97 var root = result.root; |
|
98 root.containerOpen = true; |
|
99 |
|
100 do_check_neq(resultObserver.openedContainer, null); |
|
101 |
|
102 // nsINavHistoryResultObserver.nodeInserted |
|
103 // add a visit |
|
104 promiseAddVisits(testURI).then(function() { |
|
105 do_check_eq(testURI.spec, resultObserver.insertedNode.uri); |
|
106 |
|
107 // nsINavHistoryResultObserver.nodeHistoryDetailsChanged |
|
108 // adding a visit causes nodeHistoryDetailsChanged for the folder |
|
109 do_check_eq(root.uri, resultObserver.nodeChangedByHistoryDetails.uri); |
|
110 |
|
111 // nsINavHistoryResultObserver.itemTitleChanged for a leaf node |
|
112 promiseAddVisits({ uri: testURI, title: "baz" }).then(function () { |
|
113 do_check_eq(resultObserver.nodeChangedByTitle.title, "baz"); |
|
114 |
|
115 // nsINavHistoryResultObserver.nodeRemoved |
|
116 var removedURI = uri("http://google.com"); |
|
117 promiseAddVisits(removedURI).then(function() { |
|
118 bhist.removePage(removedURI); |
|
119 do_check_eq(removedURI.spec, resultObserver.removedNode.uri); |
|
120 |
|
121 // nsINavHistoryResultObserver.invalidateContainer |
|
122 bhist.removePagesFromHost("mozilla.com", false); |
|
123 do_check_eq(root.uri, resultObserver.invalidatedContainer.uri); |
|
124 |
|
125 // nsINavHistoryResultObserver.sortingChanged |
|
126 resultObserver.invalidatedContainer = null; |
|
127 result.sortingMode = options.SORT_BY_TITLE_ASCENDING; |
|
128 do_check_eq(resultObserver.sortingMode, options.SORT_BY_TITLE_ASCENDING); |
|
129 do_check_eq(resultObserver.invalidatedContainer, result.root); |
|
130 |
|
131 // nsINavHistoryResultObserver.invalidateContainer |
|
132 bhist.removeAllPages(); |
|
133 do_check_eq(root.uri, resultObserver.invalidatedContainer.uri); |
|
134 |
|
135 // nsINavHistoryResultObserver.batching |
|
136 do_check_false(resultObserver.inBatchMode); |
|
137 histsvc.runInBatchMode({ |
|
138 runBatched: function (aUserData) { |
|
139 do_check_true(resultObserver.inBatchMode); |
|
140 } |
|
141 }, null); |
|
142 do_check_false(resultObserver.inBatchMode); |
|
143 bmsvc.runInBatchMode({ |
|
144 runBatched: function (aUserData) { |
|
145 do_check_true(resultObserver.inBatchMode); |
|
146 } |
|
147 }, null); |
|
148 do_check_false(resultObserver.inBatchMode); |
|
149 |
|
150 root.containerOpen = false; |
|
151 do_check_eq(resultObserver.closedContainer, resultObserver.openedContainer); |
|
152 result.removeObserver(resultObserver); |
|
153 resultObserver.reset(); |
|
154 promiseAsyncUpdates().then(run_next_test); |
|
155 }); |
|
156 }); |
|
157 }); |
|
158 }); |
|
159 |
|
160 add_test(function check_bookmarks_query() { |
|
161 var options = histsvc.getNewQueryOptions(); |
|
162 var query = histsvc.getNewQuery(); |
|
163 query.setFolders([bmsvc.bookmarksMenuFolder], 1); |
|
164 var result = histsvc.executeQuery(query, options); |
|
165 result.addObserver(resultObserver, false); |
|
166 var root = result.root; |
|
167 root.containerOpen = true; |
|
168 |
|
169 do_check_neq(resultObserver.openedContainer, null); |
|
170 |
|
171 // nsINavHistoryResultObserver.nodeInserted |
|
172 // add a bookmark |
|
173 var testBookmark = bmsvc.insertBookmark(bmsvc.bookmarksMenuFolder, testURI, bmsvc.DEFAULT_INDEX, "foo"); |
|
174 do_check_eq("foo", resultObserver.insertedNode.title); |
|
175 do_check_eq(testURI.spec, resultObserver.insertedNode.uri); |
|
176 |
|
177 // nsINavHistoryResultObserver.nodeHistoryDetailsChanged |
|
178 // adding a visit causes nodeHistoryDetailsChanged for the folder |
|
179 do_check_eq(root.uri, resultObserver.nodeChangedByHistoryDetails.uri); |
|
180 |
|
181 // nsINavHistoryResultObserver.nodeTitleChanged for a leaf node |
|
182 bmsvc.setItemTitle(testBookmark, "baz"); |
|
183 do_check_eq(resultObserver.nodeChangedByTitle.title, "baz"); |
|
184 do_check_eq(resultObserver.newTitle, "baz"); |
|
185 |
|
186 var testBookmark2 = bmsvc.insertBookmark(bmsvc.bookmarksMenuFolder, uri("http://google.com"), bmsvc.DEFAULT_INDEX, "foo"); |
|
187 bmsvc.moveItem(testBookmark2, bmsvc.bookmarksMenuFolder, 0); |
|
188 do_check_eq(resultObserver.movedNode.itemId, testBookmark2); |
|
189 |
|
190 // nsINavHistoryResultObserver.nodeRemoved |
|
191 bmsvc.removeItem(testBookmark2); |
|
192 do_check_eq(testBookmark2, resultObserver.removedNode.itemId); |
|
193 |
|
194 // XXX nsINavHistoryResultObserver.invalidateContainer |
|
195 |
|
196 // nsINavHistoryResultObserver.sortingChanged |
|
197 resultObserver.invalidatedContainer = null; |
|
198 result.sortingMode = options.SORT_BY_TITLE_ASCENDING; |
|
199 do_check_eq(resultObserver.sortingMode, options.SORT_BY_TITLE_ASCENDING); |
|
200 do_check_eq(resultObserver.invalidatedContainer, result.root); |
|
201 |
|
202 // nsINavHistoryResultObserver.batching |
|
203 do_check_false(resultObserver.inBatchMode); |
|
204 histsvc.runInBatchMode({ |
|
205 runBatched: function (aUserData) { |
|
206 do_check_true(resultObserver.inBatchMode); |
|
207 } |
|
208 }, null); |
|
209 do_check_false(resultObserver.inBatchMode); |
|
210 bmsvc.runInBatchMode({ |
|
211 runBatched: function (aUserData) { |
|
212 do_check_true(resultObserver.inBatchMode); |
|
213 } |
|
214 }, null); |
|
215 do_check_false(resultObserver.inBatchMode); |
|
216 |
|
217 root.containerOpen = false; |
|
218 do_check_eq(resultObserver.closedContainer, resultObserver.openedContainer); |
|
219 result.removeObserver(resultObserver); |
|
220 resultObserver.reset(); |
|
221 promiseAsyncUpdates().then(run_next_test); |
|
222 }); |
|
223 |
|
224 add_test(function check_mixed_query() { |
|
225 var options = histsvc.getNewQueryOptions(); |
|
226 var query = histsvc.getNewQuery(); |
|
227 query.onlyBookmarked = true; |
|
228 var result = histsvc.executeQuery(query, options); |
|
229 result.addObserver(resultObserver, false); |
|
230 var root = result.root; |
|
231 root.containerOpen = true; |
|
232 |
|
233 do_check_neq(resultObserver.openedContainer, null); |
|
234 |
|
235 // nsINavHistoryResultObserver.batching |
|
236 do_check_false(resultObserver.inBatchMode); |
|
237 histsvc.runInBatchMode({ |
|
238 runBatched: function (aUserData) { |
|
239 do_check_true(resultObserver.inBatchMode); |
|
240 } |
|
241 }, null); |
|
242 do_check_false(resultObserver.inBatchMode); |
|
243 bmsvc.runInBatchMode({ |
|
244 runBatched: function (aUserData) { |
|
245 do_check_true(resultObserver.inBatchMode); |
|
246 } |
|
247 }, null); |
|
248 do_check_false(resultObserver.inBatchMode); |
|
249 |
|
250 root.containerOpen = false; |
|
251 do_check_eq(resultObserver.closedContainer, resultObserver.openedContainer); |
|
252 result.removeObserver(resultObserver); |
|
253 resultObserver.reset(); |
|
254 promiseAsyncUpdates().then(run_next_test); |
|
255 }); |