|
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 const bmsvc = PlacesUtils.bookmarks; |
|
8 const histsvc = PlacesUtils.history; |
|
9 |
|
10 const NOW = Date.now() * 1000; |
|
11 const TEST_URL = "http://example.com/"; |
|
12 const TEST_URI = uri(TEST_URL); |
|
13 const PLACE_URL = "place:queryType=0&sort=8&maxResults=10"; |
|
14 const PLACE_URI = uri(PLACE_URL); |
|
15 |
|
16 var tests = [ |
|
17 { |
|
18 desc: "Remove some visits outside valid timeframe from an unbookmarked URI", |
|
19 run: function () { |
|
20 print("Add 10 visits for the URI from way in the past."); |
|
21 let visits = []; |
|
22 for (let i = 0; i < 10; i++) { |
|
23 visits.push({ uri: TEST_URI, visitDate: NOW - 1000 - i }); |
|
24 } |
|
25 promiseAddVisits(visits).then(this.continue_run.bind(this)); |
|
26 }, |
|
27 continue_run: function () { |
|
28 print("Remove visits using timerange outside the URI's visits."); |
|
29 histsvc.QueryInterface(Ci.nsIBrowserHistory). |
|
30 removeVisitsByTimeframe(NOW - 10, NOW); |
|
31 |
|
32 print("URI should still exist in moz_places."); |
|
33 do_check_true(page_in_database(TEST_URL)); |
|
34 |
|
35 print("Run a history query and check that all visits still exist."); |
|
36 var query = histsvc.getNewQuery(); |
|
37 var opts = histsvc.getNewQueryOptions(); |
|
38 opts.resultType = opts.RESULTS_AS_VISIT; |
|
39 opts.sortingMode = opts.SORT_BY_DATE_DESCENDING; |
|
40 var resultRoot = histsvc.executeQuery(query, opts).root; |
|
41 resultRoot.containerOpen = true; |
|
42 do_check_eq(resultRoot.childCount, 10); |
|
43 for (let i = 0; i < resultRoot.childCount; i++) { |
|
44 var visitTime = resultRoot.getChild(i).time; |
|
45 do_check_eq(visitTime, NOW - 1000 - i); |
|
46 } |
|
47 resultRoot.containerOpen = false; |
|
48 |
|
49 print("asyncHistory.isURIVisited should return true."); |
|
50 PlacesUtils.asyncHistory.isURIVisited(TEST_URI, function(aURI, aIsVisited) { |
|
51 do_check_true(aIsVisited); |
|
52 |
|
53 promiseAsyncUpdates().then(function () { |
|
54 print("Frecency should be positive.") |
|
55 do_check_true(frecencyForUrl(TEST_URI) > 0); |
|
56 run_next_test(); |
|
57 }); |
|
58 }); |
|
59 } |
|
60 }, |
|
61 |
|
62 { |
|
63 desc: "Remove some visits outside valid timeframe from a bookmarked URI", |
|
64 run: function () { |
|
65 print("Add 10 visits for the URI from way in the past."); |
|
66 let visits = []; |
|
67 for (let i = 0; i < 10; i++) { |
|
68 visits.push({ uri: TEST_URI, visitDate: NOW - 1000 - i }); |
|
69 } |
|
70 promiseAddVisits(visits).then(function () { |
|
71 print("Bookmark the URI."); |
|
72 bmsvc.insertBookmark(bmsvc.unfiledBookmarksFolder, |
|
73 TEST_URI, |
|
74 bmsvc.DEFAULT_INDEX, |
|
75 "bookmark title"); |
|
76 |
|
77 promiseAsyncUpdates().then(this.continue_run.bind(this)); |
|
78 }.bind(this)); |
|
79 }, |
|
80 continue_run: function () { |
|
81 print("Remove visits using timerange outside the URI's visits."); |
|
82 histsvc.QueryInterface(Ci.nsIBrowserHistory). |
|
83 removeVisitsByTimeframe(NOW - 10, NOW); |
|
84 |
|
85 print("URI should still exist in moz_places."); |
|
86 do_check_true(page_in_database(TEST_URL)); |
|
87 |
|
88 print("Run a history query and check that all visits still exist."); |
|
89 var query = histsvc.getNewQuery(); |
|
90 var opts = histsvc.getNewQueryOptions(); |
|
91 opts.resultType = opts.RESULTS_AS_VISIT; |
|
92 opts.sortingMode = opts.SORT_BY_DATE_DESCENDING; |
|
93 var resultRoot = histsvc.executeQuery(query, opts).root; |
|
94 resultRoot.containerOpen = true; |
|
95 do_check_eq(resultRoot.childCount, 10); |
|
96 for (let i = 0; i < resultRoot.childCount; i++) { |
|
97 var visitTime = resultRoot.getChild(i).time; |
|
98 do_check_eq(visitTime, NOW - 1000 - i); |
|
99 } |
|
100 resultRoot.containerOpen = false; |
|
101 |
|
102 print("asyncHistory.isURIVisited should return true."); |
|
103 PlacesUtils.asyncHistory.isURIVisited(TEST_URI, function(aURI, aIsVisited) { |
|
104 do_check_true(aIsVisited); |
|
105 |
|
106 promiseAsyncUpdates().then(function () { |
|
107 print("Frecency should be positive.") |
|
108 do_check_true(frecencyForUrl(TEST_URI) > 0); |
|
109 run_next_test(); |
|
110 }); |
|
111 }); |
|
112 } |
|
113 }, |
|
114 |
|
115 { |
|
116 desc: "Remove some visits from an unbookmarked URI", |
|
117 run: function () { |
|
118 print("Add 10 visits for the URI from now to 9 usecs in the past."); |
|
119 let visits = []; |
|
120 for (let i = 0; i < 10; i++) { |
|
121 visits.push({ uri: TEST_URI, visitDate: NOW - i }); |
|
122 } |
|
123 promiseAddVisits(visits).then(this.continue_run.bind(this)); |
|
124 }, |
|
125 continue_run: function () { |
|
126 print("Remove the 5 most recent visits."); |
|
127 histsvc.QueryInterface(Ci.nsIBrowserHistory). |
|
128 removeVisitsByTimeframe(NOW - 4, NOW); |
|
129 |
|
130 print("URI should still exist in moz_places."); |
|
131 do_check_true(page_in_database(TEST_URL)); |
|
132 |
|
133 print("Run a history query and check that only the older 5 visits " + |
|
134 "still exist."); |
|
135 var query = histsvc.getNewQuery(); |
|
136 var opts = histsvc.getNewQueryOptions(); |
|
137 opts.resultType = opts.RESULTS_AS_VISIT; |
|
138 opts.sortingMode = opts.SORT_BY_DATE_DESCENDING; |
|
139 var resultRoot = histsvc.executeQuery(query, opts).root; |
|
140 resultRoot.containerOpen = true; |
|
141 do_check_eq(resultRoot.childCount, 5); |
|
142 for (let i = 0; i < resultRoot.childCount; i++) { |
|
143 var visitTime = resultRoot.getChild(i).time; |
|
144 do_check_eq(visitTime, NOW - i - 5); |
|
145 } |
|
146 resultRoot.containerOpen = false; |
|
147 |
|
148 print("asyncHistory.isURIVisited should return true."); |
|
149 PlacesUtils.asyncHistory.isURIVisited(TEST_URI, function(aURI, aIsVisited) { |
|
150 do_check_true(aIsVisited); |
|
151 |
|
152 promiseAsyncUpdates().then(function () { |
|
153 print("Frecency should be positive.") |
|
154 do_check_true(frecencyForUrl(TEST_URI) > 0); |
|
155 run_next_test(); |
|
156 }); |
|
157 }); |
|
158 } |
|
159 }, |
|
160 |
|
161 { |
|
162 desc: "Remove some visits from a bookmarked URI", |
|
163 run: function () { |
|
164 print("Add 10 visits for the URI from now to 9 usecs in the past."); |
|
165 let visits = []; |
|
166 for (let i = 0; i < 10; i++) { |
|
167 visits.push({ uri: TEST_URI, visitDate: NOW - i }); |
|
168 } |
|
169 promiseAddVisits(visits).then(function () { |
|
170 print("Bookmark the URI."); |
|
171 bmsvc.insertBookmark(bmsvc.unfiledBookmarksFolder, |
|
172 TEST_URI, |
|
173 bmsvc.DEFAULT_INDEX, |
|
174 "bookmark title"); |
|
175 promiseAsyncUpdates().then(this.continue_run.bind(this)); |
|
176 }.bind(this)); |
|
177 }, |
|
178 continue_run: function () { |
|
179 print("Remove the 5 most recent visits."); |
|
180 histsvc.QueryInterface(Ci.nsIBrowserHistory). |
|
181 removeVisitsByTimeframe(NOW - 4, NOW); |
|
182 |
|
183 print("URI should still exist in moz_places."); |
|
184 do_check_true(page_in_database(TEST_URL)); |
|
185 |
|
186 print("Run a history query and check that only the older 5 visits " + |
|
187 "still exist."); |
|
188 var query = histsvc.getNewQuery(); |
|
189 var opts = histsvc.getNewQueryOptions(); |
|
190 opts.resultType = opts.RESULTS_AS_VISIT; |
|
191 opts.sortingMode = opts.SORT_BY_DATE_DESCENDING; |
|
192 var resultRoot = histsvc.executeQuery(query, opts).root; |
|
193 resultRoot.containerOpen = true; |
|
194 do_check_eq(resultRoot.childCount, 5); |
|
195 for (let i = 0; i < resultRoot.childCount; i++) { |
|
196 var visitTime = resultRoot.getChild(i).time; |
|
197 do_check_eq(visitTime, NOW - i - 5); |
|
198 } |
|
199 resultRoot.containerOpen = false; |
|
200 |
|
201 print("asyncHistory.isURIVisited should return true."); |
|
202 PlacesUtils.asyncHistory.isURIVisited(TEST_URI, function(aURI, aIsVisited) { |
|
203 do_check_true(aIsVisited); |
|
204 |
|
205 promiseAsyncUpdates().then(function () { |
|
206 print("Frecency should be positive.") |
|
207 do_check_true(frecencyForUrl(TEST_URI) > 0); |
|
208 run_next_test(); |
|
209 }); |
|
210 }); |
|
211 } |
|
212 }, |
|
213 |
|
214 { |
|
215 desc: "Remove all visits from an unbookmarked URI", |
|
216 run: function () { |
|
217 print("Add some visits for the URI."); |
|
218 let visits = []; |
|
219 for (let i = 0; i < 10; i++) { |
|
220 visits.push({ uri: TEST_URI, visitDate: NOW - i }); |
|
221 } |
|
222 promiseAddVisits(visits).then(this.continue_run.bind(this)); |
|
223 }, |
|
224 continue_run: function () { |
|
225 print("Remove all visits."); |
|
226 histsvc.QueryInterface(Ci.nsIBrowserHistory). |
|
227 removeVisitsByTimeframe(NOW - 10, NOW); |
|
228 |
|
229 print("URI should no longer exist in moz_places."); |
|
230 do_check_false(page_in_database(TEST_URL)); |
|
231 |
|
232 print("Run a history query and check that no visits exist."); |
|
233 var query = histsvc.getNewQuery(); |
|
234 var opts = histsvc.getNewQueryOptions(); |
|
235 opts.resultType = opts.RESULTS_AS_VISIT; |
|
236 opts.sortingMode = opts.SORT_BY_DATE_DESCENDING; |
|
237 var resultRoot = histsvc.executeQuery(query, opts).root; |
|
238 resultRoot.containerOpen = true; |
|
239 do_check_eq(resultRoot.childCount, 0); |
|
240 resultRoot.containerOpen = false; |
|
241 |
|
242 print("asyncHistory.isURIVisited should return false."); |
|
243 PlacesUtils.asyncHistory.isURIVisited(TEST_URI, function(aURI, aIsVisited) { |
|
244 do_check_false(aIsVisited); |
|
245 run_next_test(); |
|
246 }); |
|
247 } |
|
248 }, |
|
249 |
|
250 { |
|
251 desc: "Remove all visits from an unbookmarked place: URI", |
|
252 run: function () { |
|
253 print("Add some visits for the URI."); |
|
254 let visits = []; |
|
255 for (let i = 0; i < 10; i++) { |
|
256 visits.push({ uri: PLACE_URI, visitDate: NOW - i }); |
|
257 } |
|
258 promiseAddVisits(visits).then(this.continue_run.bind(this)); |
|
259 }, |
|
260 continue_run: function () { |
|
261 print("Remove all visits."); |
|
262 histsvc.QueryInterface(Ci.nsIBrowserHistory). |
|
263 removeVisitsByTimeframe(NOW - 10, NOW); |
|
264 |
|
265 print("URI should still exist in moz_places."); |
|
266 do_check_true(page_in_database(PLACE_URL)); |
|
267 |
|
268 print("Run a history query and check that no visits exist."); |
|
269 var query = histsvc.getNewQuery(); |
|
270 var opts = histsvc.getNewQueryOptions(); |
|
271 opts.resultType = opts.RESULTS_AS_VISIT; |
|
272 opts.sortingMode = opts.SORT_BY_DATE_DESCENDING; |
|
273 var resultRoot = histsvc.executeQuery(query, opts).root; |
|
274 resultRoot.containerOpen = true; |
|
275 do_check_eq(resultRoot.childCount, 0); |
|
276 resultRoot.containerOpen = false; |
|
277 |
|
278 print("asyncHistory.isURIVisited should return false."); |
|
279 PlacesUtils.asyncHistory.isURIVisited(PLACE_URI, function(aURI, aIsVisited) { |
|
280 do_check_false(aIsVisited); |
|
281 |
|
282 promiseAsyncUpdates().then(function () { |
|
283 print("Frecency should be zero.") |
|
284 do_check_eq(frecencyForUrl(PLACE_URL), 0); |
|
285 run_next_test(); |
|
286 }); |
|
287 }); |
|
288 } |
|
289 }, |
|
290 |
|
291 { |
|
292 desc: "Remove all visits from a bookmarked URI", |
|
293 run: function () { |
|
294 print("Add some visits for the URI."); |
|
295 let visits = []; |
|
296 for (let i = 0; i < 10; i++) { |
|
297 visits.push({ uri: TEST_URI, visitDate: NOW - i }); |
|
298 } |
|
299 promiseAddVisits(visits).then(function () { |
|
300 print("Bookmark the URI."); |
|
301 bmsvc.insertBookmark(bmsvc.unfiledBookmarksFolder, |
|
302 TEST_URI, |
|
303 bmsvc.DEFAULT_INDEX, |
|
304 "bookmark title"); |
|
305 promiseAsyncUpdates().then(this.continue_run.bind(this)); |
|
306 }.bind(this)); |
|
307 }, |
|
308 continue_run: function () { |
|
309 print("Remove all visits."); |
|
310 histsvc.QueryInterface(Ci.nsIBrowserHistory). |
|
311 removeVisitsByTimeframe(NOW - 10, NOW); |
|
312 |
|
313 print("URI should still exist in moz_places."); |
|
314 do_check_true(page_in_database(TEST_URL)); |
|
315 |
|
316 print("Run a history query and check that no visits exist."); |
|
317 var query = histsvc.getNewQuery(); |
|
318 var opts = histsvc.getNewQueryOptions(); |
|
319 opts.resultType = opts.RESULTS_AS_VISIT; |
|
320 opts.sortingMode = opts.SORT_BY_DATE_DESCENDING; |
|
321 var resultRoot = histsvc.executeQuery(query, opts).root; |
|
322 resultRoot.containerOpen = true; |
|
323 do_check_eq(resultRoot.childCount, 0); |
|
324 resultRoot.containerOpen = false; |
|
325 |
|
326 print("asyncHistory.isURIVisited should return false."); |
|
327 PlacesUtils.asyncHistory.isURIVisited(TEST_URI, function(aURI, aIsVisited) { |
|
328 do_check_false(aIsVisited); |
|
329 |
|
330 print("nsINavBookmarksService.isBookmarked should return true."); |
|
331 do_check_true(bmsvc.isBookmarked(TEST_URI)); |
|
332 |
|
333 promiseAsyncUpdates().then(function () { |
|
334 print("Frecency should be negative.") |
|
335 do_check_true(frecencyForUrl(TEST_URI) < 0); |
|
336 run_next_test(); |
|
337 }); |
|
338 }); |
|
339 } |
|
340 }, |
|
341 |
|
342 { |
|
343 desc: "Remove some visits from a zero frecency URI retains zero frecency", |
|
344 run: function () { |
|
345 do_log_info("Add some visits for the URI."); |
|
346 promiseAddVisits([{ uri: TEST_URI, transition: TRANSITION_FRAMED_LINK, |
|
347 visitDate: (NOW - 86400000000) }, |
|
348 { uri: TEST_URI, transition: TRANSITION_FRAMED_LINK, |
|
349 visitDate: NOW }]).then( |
|
350 this.continue_run.bind(this)); |
|
351 }, |
|
352 continue_run: function () { |
|
353 do_log_info("Remove newer visit."); |
|
354 histsvc.QueryInterface(Ci.nsIBrowserHistory). |
|
355 removeVisitsByTimeframe(NOW - 10, NOW); |
|
356 |
|
357 promiseAsyncUpdates().then(function() { |
|
358 do_log_info("URI should still exist in moz_places."); |
|
359 do_check_true(page_in_database(TEST_URL)); |
|
360 do_log_info("Frecency should be zero.") |
|
361 do_check_eq(frecencyForUrl(TEST_URI), 0); |
|
362 run_next_test(); |
|
363 }); |
|
364 } |
|
365 } |
|
366 ]; |
|
367 |
|
368 /////////////////////////////////////////////////////////////////////////////// |
|
369 |
|
370 function run_test() |
|
371 { |
|
372 do_test_pending(); |
|
373 run_next_test(); |
|
374 } |
|
375 |
|
376 function run_next_test() { |
|
377 if (tests.length) { |
|
378 let test = tests.shift(); |
|
379 print("\n ***Test: " + test.desc); |
|
380 promiseClearHistory().then(function() { |
|
381 remove_all_bookmarks(); |
|
382 DBConn().executeSimpleSQL("DELETE FROM moz_places"); |
|
383 test.run.call(test); |
|
384 }); |
|
385 } |
|
386 else { |
|
387 do_test_finished(); |
|
388 } |
|
389 } |