summaryrefslogtreecommitdiffstats
path: root/toolkit/components/places/tests/unit/test_frecency_observers.js
blob: bc7a7ca0993a4a5e6145800623148f0380118846 (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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

// Each of these tests a path that triggers a frecency update.  Together they
// hit all sites that update a frecency.

// InsertVisitedURIs::UpdateFrecency and History::InsertPlace
add_task(
  async function test_InsertVisitedURIs_UpdateFrecency_and_History_InsertPlace() {
    // InsertPlace is at the end of a path that UpdateFrecency is also on, so kill
    // two birds with one stone and expect two notifications.  Trigger the path by
    // adding a download.
    let url = Services.io.newURI("http://example.com/a");
    let promise = onRankingChanged();
    await PlacesUtils.history.insert({
      url,
      visits: [
        {
          transition: PlacesUtils.history.TRANSITIONS.DOWNLOAD,
        },
      ],
    });
    await promise;
  }
);

// nsNavHistory::UpdateFrecency
add_task(async function test_nsNavHistory_UpdateFrecency() {
  let url = Services.io.newURI("http://example.com/b");
  let promise = onRankingChanged();
  await PlacesUtils.bookmarks.insert({
    parentGuid: PlacesUtils.bookmarks.unfiledGuid,
    url,
    title: "test",
  });
  await PlacesFrecencyRecalculator.recalculateAnyOutdatedFrecencies();
  await promise;
});

// History.sys.mjs invalidateFrecencies()
add_task(async function test_invalidateFrecencies() {
  let url = Services.io.newURI("http://test-invalidateFrecencies.com/");
  // Bookmarking the URI is enough to add it to moz_places, and importantly, it
  // means that removeByFilter doesn't remove it from moz_places, so its
  // frecency is able to be changed.
  await PlacesUtils.bookmarks.insert({
    parentGuid: PlacesUtils.bookmarks.unfiledGuid,
    url,
    title: "test",
  });
  let promise = onRankingChanged();
  await PlacesUtils.history.removeByFilter({ host: url.host });
  await PlacesFrecencyRecalculator.recalculateAnyOutdatedFrecencies();
  await promise;
});

// History.sys.mjs clear() should not cause a frecency recalculation since pages
// are removed.
add_task(async function test_clear() {
  let received = [];
  let listener = events =>
    (received = received.concat(events.map(e => e.type)));
  PlacesObservers.addListener(
    ["history-cleared", "pages-rank-changed"],
    listener
  );
  await PlacesUtils.history.clear();
  PlacesObservers.removeListener(
    ["history-cleared", "pages-rank-changed"],
    listener
  );
  Assert.deepEqual(received, ["history-cleared"]);
});

add_task(async function test_nsNavHistory_idleDaily() {
  await PlacesUtils.bookmarks.insert({
    parentGuid: PlacesUtils.bookmarks.unfiledGuid,
    url: "https://test-site1.org",
    title: "test",
  });
  PlacesFrecencyRecalculator.observe(null, "idle-daily", "");
  await Promise.all([onRankingChanged()]);
});

add_task(async function test_nsNavHistory_recalculate() {
  await PlacesUtils.bookmarks.insert({
    parentGuid: PlacesUtils.bookmarks.unfiledGuid,
    url: "https://test-site1.org",
    title: "test",
  });
  await Promise.all([
    onRankingChanged(),
    PlacesFrecencyRecalculator.recalculateAnyOutdatedFrecencies(),
  ]);
});

function onRankingChanged() {
  return PlacesTestUtils.waitForNotification("pages-rank-changed");
}