summaryrefslogtreecommitdiffstats
path: root/toolkit/components/places/tests/unit/test_PlacesQuery_history.js
blob: 9546e03730d6d8d4fd4e25e35bdd5d987726903b (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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/* Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

const { PlacesQuery } = ChromeUtils.importESModule(
  "resource://gre/modules/PlacesQuery.sys.mjs"
);

const placesQuery = new PlacesQuery();

registerCleanupFunction(() => placesQuery.close());

async function waitForUpdateHistoryTask(updateTask) {
  const promise = new Promise(resolve =>
    placesQuery.observeHistory(newHistory => resolve(newHistory))
  );
  await updateTask();
  return promise;
}

add_task(async function test_visits_cache_is_updated() {
  const now = new Date();
  info("Insert the first visit.");
  await PlacesUtils.history.insert({
    url: "https://www.example.com/",
    title: "Example Domain",
    visits: [{ date: now }],
  });
  let history = await placesQuery.getHistory();
  const mapKey = placesQuery.getStartOfDayTimestamp(now);
  history = history.get(mapKey);
  Assert.equal(history.length, 1);
  Assert.equal(history[0].url, "https://www.example.com/");
  Assert.equal(history[0].date.getTime(), now.getTime());
  Assert.equal(history[0].title, "Example Domain");

  info("Insert the next visit.");
  history = await waitForUpdateHistoryTask(() =>
    PlacesUtils.history.insert({
      url: "https://example.net/",
      visits: [{ date: now }],
    })
  );
  history = history.get(mapKey);
  Assert.equal(history.length, 2);
  Assert.equal(
    history[0].url,
    "https://example.net/",
    "The most recent visit should come first."
  );
  Assert.equal(history[0].date.getTime(), now.getTime());

  info("Remove the first visit.");
  history = await waitForUpdateHistoryTask(() =>
    PlacesUtils.history.remove("https://www.example.com/")
  );
  history = history.get(mapKey);
  Assert.equal(history.length, 1);
  Assert.equal(history[0].url, "https://example.net/");

  info("Remove all visits.");
  history = await waitForUpdateHistoryTask(() => PlacesUtils.history.clear());
  Assert.equal(history.size, 0);
});

add_task(async function test_filter_visits_by_age() {
  const now = new Date();
  await PlacesUtils.history.insertMany([
    {
      url: "https://www.example.com/",
      visits: [{ date: new Date("2000-01-01T12:00:00") }],
    },
    {
      url: "https://example.net/",
      visits: [{ date: now }],
    },
  ]);
  let history = await placesQuery.getHistory({ daysOld: 1 });
  Assert.equal(history.size, 1, "The older visit should be excluded.");
  Assert.equal(
    history.get(placesQuery.getStartOfDayTimestamp(now))[0].url,
    "https://example.net/"
  );
  await PlacesUtils.history.clear();
});

add_task(async function test_visits_sort_option() {
  const now = new Date();
  info("Get history sorted by site.");
  await PlacesUtils.history.insertMany([
    { url: "https://www.reddit.com/", visits: [{ date: now }] },
    { url: "https://twitter.com/", visits: [{ date: now }] },
  ]);
  let history = await placesQuery.getHistory({ sortBy: "site" });
  ["reddit.com", "twitter.com"].forEach(domain =>
    Assert.ok(history.has(domain))
  );

  info("Insert the next visit.");
  history = await waitForUpdateHistoryTask(() =>
    PlacesUtils.history.insert({
      url: "https://www.wikipedia.org/",
      visits: [{ date: now }],
    })
  );
  ["reddit.com", "twitter.com", "wikipedia.org"].forEach(domain =>
    Assert.ok(history.has(domain))
  );

  info("Update the sort order.");
  history = await placesQuery.getHistory({ sortBy: "date" });
  const mapKey = placesQuery.getStartOfDayTimestamp(now);
  Assert.equal(history.get(mapKey).length, 3);

  info("Insert the next visit.");
  history = await waitForUpdateHistoryTask(() =>
    PlacesUtils.history.insert({
      url: "https://www.youtube.com/",
      visits: [{ date: now }],
    })
  );
  Assert.equal(history.get(mapKey).length, 4);
  await PlacesUtils.history.clear();
});

add_task(async function test_visits_limit_option() {
  await PlacesUtils.history.insertMany([
    {
      url: "https://www.example.com/",
      visits: [{ date: new Date() }],
    },
    {
      url: "https://example.net/",
      visits: [{ date: new Date() }],
    },
  ]);
  let history = await placesQuery.getHistory({ limit: 1 });
  Assert.equal(
    [...history.values()].reduce((acc, { length }) => acc + length, 0),
    1,
    "Number of visits should be limited to 1."
  );
  await PlacesUtils.history.clear();
});

add_task(async function test_dedupe_visits_by_url() {
  const today = new Date();
  const yesterday = new Date(
    today.getFullYear(),
    today.getMonth(),
    today.getDate() - 1
  );
  await PlacesUtils.history.insertMany([
    {
      url: "https://www.example.com/",
      visits: [{ date: yesterday }],
    },
    {
      url: "https://www.example.com/",
      visits: [{ date: today }],
    },
    {
      url: "https://www.example.com/",
      visits: [{ date: today }],
    },
  ]);
  info("Get history sorted by date.");
  let history = await placesQuery.getHistory({ sortBy: "date" });
  Assert.equal(
    history.get(placesQuery.getStartOfDayTimestamp(yesterday)).length,
    1,
    "There was only one visit from yesterday."
  );
  Assert.equal(
    history.get(placesQuery.getStartOfDayTimestamp(today)).length,
    1,
    "The duplicate visit from today should be removed."
  );
  history = await waitForUpdateHistoryTask(() =>
    PlacesUtils.history.insert({
      url: "https://www.example.com/",
      visits: [{ date: today }],
    })
  );
  Assert.equal(
    history.get(placesQuery.getStartOfDayTimestamp(today)).length,
    1,
    "Visits inserted from `page-visited` events should be deduped."
  );

  info("Get history sorted by site.");
  history = await placesQuery.getHistory({ sortBy: "site" });
  Assert.equal(
    history.get("example.com").length,
    1,
    "The duplicate visits for this site should be removed."
  );
  history = await waitForUpdateHistoryTask(() =>
    PlacesUtils.history.insert({
      url: "https://www.example.com/",
      visits: [{ date: yesterday }],
    })
  );
  const visits = history.get("example.com");
  Assert.equal(
    visits.length,
    1,
    "Visits inserted from `page-visited` events should be deduped."
  );
  Assert.equal(
    visits[0].date.getTime(),
    today.getTime(),
    "Deduping keeps the most recent visit."
  );

  await PlacesUtils.history.clear();
});

add_task(async function test_search_visits() {
  const now = new Date();
  await PlacesUtils.history.insertMany([
    {
      url: "https://www.example.com/",
      title: "First Visit",
      visits: [{ date: now }],
    },
    {
      url: "https://example.net/",
      title: "Second Visit",
      visits: [{ date: now }],
    },
  ]);

  let results = await placesQuery.searchHistory("Visit");
  Assert.equal(results.length, 2, "Both visits match the search query.");

  results = await placesQuery.searchHistory("First Visit");
  Assert.equal(results.length, 1, "One visit matches the search query.");

  results = await placesQuery.searchHistory("Bogus");
  Assert.equal(results.length, 0, "Neither visit matches the search query.");

  await PlacesUtils.history.clear();
});