|
1 /* Any copyright is dedicated to the Public Domain. |
|
2 http://creativecommons.org/publicdomain/zero/1.0/ */ |
|
3 |
|
4 function run_test() { |
|
5 run_next_test(); |
|
6 } |
|
7 |
|
8 // Each of these tests a path that triggers a frecency update. Together they |
|
9 // hit all sites that update a frecency. |
|
10 |
|
11 // InsertVisitedURIs::UpdateFrecency and History::InsertPlace |
|
12 add_task(function test_InsertVisitedURIs_UpdateFrecency_and_History_InsertPlace() { |
|
13 // InsertPlace is at the end of a path that UpdateFrecency is also on, so kill |
|
14 // two birds with one stone and expect two notifications. Trigger the path by |
|
15 // adding a download. |
|
16 let uri = NetUtil.newURI("http://example.com/a"); |
|
17 Cc["@mozilla.org/browser/download-history;1"]. |
|
18 getService(Ci.nsIDownloadHistory). |
|
19 addDownload(uri); |
|
20 yield Promise.all([onFrecencyChanged(uri), onFrecencyChanged(uri)]); |
|
21 }); |
|
22 |
|
23 // nsNavHistory::UpdateFrecency |
|
24 add_task(function test_nsNavHistory_UpdateFrecency() { |
|
25 let bm = PlacesUtils.bookmarks; |
|
26 let uri = NetUtil.newURI("http://example.com/b"); |
|
27 bm.insertBookmark(bm.unfiledBookmarksFolder, uri, |
|
28 Ci.nsINavBookmarksService.DEFAULT_INDEX, "test"); |
|
29 yield onFrecencyChanged(uri); |
|
30 }); |
|
31 |
|
32 // nsNavHistory::invalidateFrecencies for particular pages |
|
33 add_task(function test_nsNavHistory_invalidateFrecencies_somePages() { |
|
34 let uri = NetUtil.newURI("http://test-nsNavHistory-invalidateFrecencies-somePages.com/"); |
|
35 // Bookmarking the URI is enough to add it to moz_places, and importantly, it |
|
36 // means that removePagesFromHost doesn't remove it from moz_places, so its |
|
37 // frecency is able to be changed. |
|
38 let bm = PlacesUtils.bookmarks; |
|
39 bm.insertBookmark(bm.unfiledBookmarksFolder, uri, |
|
40 Ci.nsINavBookmarksService.DEFAULT_INDEX, "test"); |
|
41 PlacesUtils.history.removePagesFromHost(uri.host, false); |
|
42 yield onFrecencyChanged(uri); |
|
43 }); |
|
44 |
|
45 // nsNavHistory::invalidateFrecencies for all pages |
|
46 add_task(function test_nsNavHistory_invalidateFrecencies_allPages() { |
|
47 PlacesUtils.history.removeAllPages(); |
|
48 yield onManyFrecenciesChanged(); |
|
49 }); |
|
50 |
|
51 // nsNavHistory::DecayFrecency and nsNavHistory::FixInvalidFrecencies |
|
52 add_task(function test_nsNavHistory_DecayFrecency_and_nsNavHistory_FixInvalidFrecencies() { |
|
53 // FixInvalidFrecencies is at the end of a path that DecayFrecency is also on, |
|
54 // so expect two notifications. Trigger the path by making nsNavHistory |
|
55 // observe the idle-daily notification. |
|
56 PlacesUtils.history.QueryInterface(Ci.nsIObserver). |
|
57 observe(null, "idle-daily", ""); |
|
58 yield Promise.all([onManyFrecenciesChanged(), onManyFrecenciesChanged()]); |
|
59 }); |
|
60 |
|
61 function onFrecencyChanged(expectedURI) { |
|
62 let deferred = Promise.defer(); |
|
63 let obs = new NavHistoryObserver(); |
|
64 obs.onFrecencyChanged = |
|
65 (uri, newFrecency, guid, hidden, visitDate) => { |
|
66 PlacesUtils.history.removeObserver(obs); |
|
67 do_check_true(!!uri); |
|
68 do_check_true(uri.equals(expectedURI)); |
|
69 deferred.resolve(); |
|
70 }; |
|
71 PlacesUtils.history.addObserver(obs, false); |
|
72 return deferred.promise; |
|
73 } |
|
74 |
|
75 function onManyFrecenciesChanged() { |
|
76 let deferred = Promise.defer(); |
|
77 let obs = new NavHistoryObserver(); |
|
78 obs.onManyFrecenciesChanged = () => { |
|
79 PlacesUtils.history.removeObserver(obs); |
|
80 do_check_true(true); |
|
81 deferred.resolve(); |
|
82 }; |
|
83 PlacesUtils.history.addObserver(obs, false); |
|
84 return deferred.promise; |
|
85 } |