summaryrefslogtreecommitdiffstats
path: root/toolkit/components/places/tests/expiration/test_interactions_expiration.js
blob: 67b4b466c32c91f502702513708763d49ea52a4f (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
100
101
102
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

/**
 * Tests expiration of Places interactions data.
 */
// Number of days in the past where interactions will be expired.
const EXPIRE_DAYS = 60;
// Should be more recent than EXPIRED_DAYS.
const RECENT_DATE = new Date() - (EXPIRE_DAYS - 1) * 86400000;

add_task(async function setup() {
  Services.prefs.setBoolPref("browser.places.interactions.enabled", true);
  Services.prefs.setIntPref(
    "browser.places.interactions.expireDays",
    EXPIRE_DAYS
  );
});

add_task(async function test_expire_interactions() {
  // Add visits and metadata to 2 pages and force expiration.
  await PlacesTestUtils.addVisits([
    "https://expired.mozilla.org/",
    "https://interactions-expired.mozilla.org/",
    "https://some-interaction-expired.mozilla.org/",
    "https://not-expired.mozilla.org/",
  ]);
  // Insert dummy interactions for all the pages.
  await addDummyInteractions("https://removed.mozilla.org/", [0]);
  await addDummyInteractions("https://interactions-expired.mozilla.org/", [
    EXPIRE_DAYS + 10,
  ]);
  await addDummyInteractions("https://some-interactions-expired.mozilla.org/", [
    0,
    EXPIRE_DAYS + 10,
  ]);
  await addDummyInteractions("https://not-expired.mozilla.org/", [
    0,
    EXPIRE_DAYS / 2,
  ]);

  info("Remove a page from history and check interactions are removed");
  await PlacesUtils.history.remove("https://removed.mozilla.org/");
  await checkDummyInteractions("https://removed.mozilla.org/", 0);

  // Expire now.
  await promiseForceExpirationStep(-1);

  info("Test interactions expiration result");
  await checkDummyInteractions("https://interactions-expired.mozilla.org/", 0);
  await checkDummyInteractions(
    "https://some-interactions-expired.mozilla.org/",
    1
  );
  await checkDummyInteractions("https://not-expired.mozilla.org/", 2);

  // Clean up.
  await PlacesUtils.history.clear();
});

async function addDummyInteractions(url, interactionDaysAgo) {
  await PlacesTestUtils.addVisits(url);
  await PlacesUtils.withConnectionWrapper(
    "test_interactions_expiration.js: addDummyInteraction",
    async db => {
      await db.execute(
        `INSERT INTO moz_places_metadata (place_id, created_at, updated_at) VALUES (
        (SELECT id FROM moz_places WHERE url_hash = hash(:url)),
        strftime('%s','now','localtime','-' || :days || ' day','start of day','utc') * 1000,
        strftime('%s','now','localtime','-' || :days || ' day','start of day','utc') * 1000
      )`,
        interactionDaysAgo.map(days => ({ url, days }))
      );
    }
  );
}

async function checkDummyInteractions(url, interactionsLen) {
  info("Check interactions for " + url);
  await PlacesUtils.withConnectionWrapper(
    "test_interactions_expiration.js: addDummyInteraction",
    async db => {
      let rows = await db.execute(
        `SELECT updated_at
         FROM moz_places_metadata
         WHERE place_id = (SELECT id FROM moz_places WHERE url_hash = hash(:url))
         ORDER BY updated_at DESC`,
        { url }
      );
      let dates = rows.map(r => new Date(r.getResultByName("updated_at")));
      Assert.equal(
        rows.length,
        interactionsLen,
        "Found expected number of interactions"
      );
      Assert.ok(
        dates.every(d => d > RECENT_DATE),
        "All interactions are recent"
      );
    }
  );
}