summaryrefslogtreecommitdiffstats
path: root/browser/components/extensions/test/browser/browser_ext_browsingData_history.js
blob: 2f696f315428b400deeacc1785526934fe1b9e71 (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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
/* vim: set sts=2 sw=2 et tw=80: */
"use strict";

ChromeUtils.defineESModuleGetters(this, {
  PlacesTestUtils: "resource://testing-common/PlacesTestUtils.sys.mjs",
  PlacesUtils: "resource://gre/modules/PlacesUtils.sys.mjs",
});

const OLD_URL = "http://example.com/";
const RECENT_URL = "http://example.com/2/";
const REFERENCE_DATE = new Date();

// Visits to add via addVisits
const PLACEINFO = [
  {
    uri: RECENT_URL,
    title: `test visit for ${RECENT_URL}`,
    visitDate: REFERENCE_DATE,
  },
  {
    uri: OLD_URL,
    title: `test visit for ${OLD_URL}`,
    visitDate: new Date(Number(REFERENCE_DATE) - 1000),
  },
  {
    uri: OLD_URL,
    title: `test visit for ${OLD_URL}`,
    visitDate: new Date(Number(REFERENCE_DATE) - 2000),
  },
];

async function setupHistory() {
  await PlacesUtils.history.clear();
  await PlacesTestUtils.addVisits(PLACEINFO);
  is(
    await PlacesTestUtils.visitsInDB(RECENT_URL),
    1,
    "Expected number of visits found in history database."
  );
  is(
    await PlacesTestUtils.visitsInDB(OLD_URL),
    2,
    "Expected number of visits found in history database."
  );
}

add_task(async function testHistory() {
  function background() {
    browser.test.onMessage.addListener(async (msg, options) => {
      if (msg == "removeHistory") {
        await browser.browsingData.removeHistory(options);
      } else {
        await browser.browsingData.remove(options, { history: true });
      }
      browser.test.sendMessage("historyRemoved");
    });
  }

  let extension = ExtensionTestUtils.loadExtension({
    background,
    manifest: {
      permissions: ["browsingData"],
    },
  });

  async function testRemovalMethod(method) {
    // Clear history with no since value.
    await setupHistory();
    extension.sendMessage(method, {});
    await extension.awaitMessage("historyRemoved");

    is(
      await PlacesTestUtils.visitsInDB(RECENT_URL),
      0,
      "Expected number of visits removed from history database."
    );
    is(
      await PlacesTestUtils.visitsInDB(OLD_URL),
      0,
      "Expected number of visits removed from history database."
    );

    // Clear history with recent since value.
    await setupHistory();
    extension.sendMessage(method, { since: REFERENCE_DATE - 1000 });
    await extension.awaitMessage("historyRemoved");

    is(
      await PlacesTestUtils.visitsInDB(RECENT_URL),
      0,
      "Expected number of visits removed from history database."
    );
    is(
      await PlacesTestUtils.visitsInDB(OLD_URL),
      1,
      "Expected number of visits removed from history database."
    );

    // Clear history with old since value.
    await setupHistory();
    extension.sendMessage(method, { since: REFERENCE_DATE - 100000 });
    await extension.awaitMessage("historyRemoved");

    is(
      await PlacesTestUtils.visitsInDB(RECENT_URL),
      0,
      "Expected number of visits removed from history database."
    );
    is(
      await PlacesTestUtils.visitsInDB(OLD_URL),
      0,
      "Expected number of visits removed from history database."
    );
  }

  await extension.startup();

  await testRemovalMethod("removeHistory");
  await testRemovalMethod("remove");

  await extension.unload();
});