summaryrefslogtreecommitdiffstats
path: root/toolkit/components/places/tests/unit/test_frecency_decay.js
blob: a9762209c12310837e9bd124ff92bcff1f5347a8 (plain)
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
72
73
74
75
76
77
78
79
80
81
82
/* Any copyright is dedicated to the Public Domain.
 * https://creativecommons.org/publicdomain/zero/1.0/ */

const PREF_FREC_DECAY_RATE_DEF = 0.975;

/**
 * Promises that the pages-rank-changed event has been seen.
 *
 * @returns {Promise} A promise which is resolved when the notification is seen.
 */
function promiseRankingChanged() {
  return PlacesTestUtils.waitForNotification("pages-rank-changed");
}

add_task(async function setup() {
  Services.prefs.setCharPref(
    "places.frecency.decayRate",
    PREF_FREC_DECAY_RATE_DEF
  );
});

add_task(async function test_isFrecencyDecaying() {
  let db = await PlacesUtils.promiseDBConnection();
  async function queryFrecencyDecaying() {
    return (
      await db.executeCached(`SELECT is_frecency_decaying()`)
    )[0].getResultByIndex(0);
  }
  PlacesUtils.history.isFrecencyDecaying = true;
  Assert.equal(PlacesUtils.history.isFrecencyDecaying, true);
  Assert.equal(await queryFrecencyDecaying(), true);
  PlacesUtils.history.isFrecencyDecaying = false;
  Assert.equal(PlacesUtils.history.isFrecencyDecaying, false);
  Assert.equal(await queryFrecencyDecaying(), false);
});

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 = promiseRankingChanged();
  await PlacesUtils.bookmarks.insert({
    url,
    parentGuid: PlacesUtils.bookmarks.unfiledGuid,
  });
  await PlacesFrecencyRecalculator.recalculateAnyOutdatedFrecencies();
  await promiseOne;

  let histogram = TelemetryTestUtils.getAndClearHistogram(
    "PLACES_IDLE_FRECENCY_DECAY_TIME_MS"
  );
  info("Trigger frecency decay.");
  Assert.equal(PlacesUtils.history.isFrecencyDecaying, false);
  let promiseRanking = promiseRankingChanged();

  PlacesFrecencyRecalculator.observe(null, "idle-daily", "");
  Assert.equal(PlacesUtils.history.isFrecencyDecaying, true);
  info("Wait for completion.");
  await PlacesFrecencyRecalculator.pendingFrecencyDecayPromise;

  await promiseRanking;
  Assert.equal(PlacesUtils.history.isFrecencyDecaying, false);

  // Now check the new frecency is correct.
  let newFrecency = await PlacesTestUtils.getDatabaseValue(
    "moz_places",
    "frecency",
    { url }
  );

  Assert.equal(
    newFrecency,
    Math.round(unvisitedBookmarkFrecency * PREF_FREC_DECAY_RATE_DEF),
    "Frecencies should match"
  );

  let snapshot = histogram.snapshot();
  Assert.greater(Object.values(snapshot.values).length, 0);
});