-
Notifications
You must be signed in to change notification settings - Fork 54
/
test_frecency_decay.js
71 lines (62 loc) · 2.46 KB
/
test_frecency_decay.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
const {PromiseUtils} = ChromeUtils.import("resource://gre/modules/PromiseUtils.jsm");
const PREF_FREC_DECAY_RATE_DEF = 0.975;
/**
* Promises that the frecency has been changed, and is the new value.
*
* @param {nsIURI} expectedURI The URI to check frecency for.
* @param {Number} expectedFrecency The expected frecency for the URI.
* @returns {Promise} A promise which is resolved when the URI is seen.
*/
function promiseFrecencyChanged(expectedURI, expectedFrecency) {
let deferred = PromiseUtils.defer();
let obs = new NavHistoryObserver();
obs.onFrecencyChanged =
(uri, newFrecency, guid, hidden, visitDate) => {
PlacesUtils.history.removeObserver(obs);
Assert.ok(!!uri, "uri should not be null");
Assert.ok(uri.equals(NetUtil.newURI(expectedURI)), "uri should be the expected one");
Assert.equal(newFrecency, expectedFrecency, "Frecency should be the expected one");
deferred.resolve();
};
PlacesUtils.history.addObserver(obs);
return deferred.promise;
}
/**
* Promises that the many frecencies changed notification has been seen.
*
* @returns {Promise} A promise which is resolved when the notification is seen.
*/
function promiseManyFrecenciesChanged() {
let deferred = PromiseUtils.defer();
let obs = new NavHistoryObserver();
obs.onManyFrecenciesChanged = () => {
PlacesUtils.history.removeObserver(obs);
Assert.ok(true);
deferred.resolve();
};
PlacesUtils.history.addObserver(obs);
return deferred.promise;
}
add_task(async function setup() {
Services.prefs.setCharPref("places.frecency.decayRate", PREF_FREC_DECAY_RATE_DEF);
});
add_task(async function test_frecency_decay() {
let unvisitedBookmarkFrecency = Services.prefs.getIntPref("places.frecency.unvisitedBookmarkBonus");
// Add a bookmark and check its frecency.
let url = "http://example.com/b";
let promiseOne = promiseFrecencyChanged(url, unvisitedBookmarkFrecency);
await PlacesUtils.bookmarks.insert({
url,
parentGuid: PlacesUtils.bookmarks.unfiledGuid,
});
await promiseOne;
// Trigger DecayFrecency.
let promiseMany = promiseManyFrecenciesChanged();
PlacesUtils.history.QueryInterface(Ci.nsIObserver)
.observe(null, "idle-daily", "");
await promiseMany;
// Now check the new frecency is correct.
let newFrecency = await PlacesTestUtils.fieldInDB(url, "frecency");
Assert.equal(newFrecency, Math.round(unvisitedBookmarkFrecency * PREF_FREC_DECAY_RATE_DEF),
"Frecencies should match");
});