summaryrefslogtreecommitdiffstats
path: root/toolkit/components/places/tests/history/test_removeVisitsByFilter.js
blob: be01fcb90101fc3d1fbef6a6f52f943e17df805a (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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
/* vim:set ts=2 sw=2 sts=2 et: */

// Tests for `History.removeVisitsByFilter`, as implemented in History.jsm

"use strict";

add_task(async function test_removeVisitsByFilter() {
  let referenceDate = new Date(1999, 9, 9, 9, 9);

  // Populate a database with 20 entries, remove a subset of entries,
  // ensure consistency.
  let remover = async function (options) {
    info("Remover with options " + JSON.stringify(options));
    let SAMPLE_SIZE = options.sampleSize;

    await PlacesUtils.history.clear();
    await PlacesUtils.bookmarks.eraseEverything();

    // Populate the database.
    // Create `SAMPLE_SIZE` visits, from the oldest to the newest.

    let bookmarkIndices = new Set(options.bookmarks);
    let visits = [];
    let rankingChangePromises = [];
    let uriDeletePromises = new Map();
    let getURL = options.url
      ? i =>
          "http://mozilla.com/test_browserhistory/test_removeVisitsByFilter/removeme/byurl/" +
          Math.floor(i / (SAMPLE_SIZE / 5)) +
          "/"
      : i =>
          "http://mozilla.com/test_browserhistory/test_removeVisitsByFilter/removeme/" +
          i +
          "/" +
          Math.random();
    for (let i = 0; i < SAMPLE_SIZE; ++i) {
      let spec = getURL(i);
      let uri = NetUtil.newURI(spec);
      let jsDate = new Date(Number(referenceDate) + 3600 * 1000 * i);
      let dbDate = jsDate * 1000;
      let hasBookmark = bookmarkIndices.has(i);
      let hasOwnBookmark = hasBookmark;
      if (!hasOwnBookmark && options.url) {
        // Also mark as bookmarked if one of the earlier bookmarked items has the same URL.
        hasBookmark = options.bookmarks
          .filter(n => n < i)
          .some(n => visits[n].uri.spec == spec && visits[n].test.hasBookmark);
      }
      info("Generating " + uri.spec + ", " + dbDate);
      let visit = {
        uri,
        title: "visit " + i,
        visitDate: dbDate,
        test: {
          // `visitDate`, as a Date
          jsDate,
          // `true` if we expect that the visit will be removed
          toRemove: false,
          // `true` if `onRow` informed of the removal of this visit
          announcedByOnRow: false,
          // `true` if there is a bookmark for this URI, i.e. of the page
          // should not be entirely removed.
          hasBookmark,
        },
      };
      visits.push(visit);
      if (hasOwnBookmark) {
        info("Adding a bookmark to visit " + i);
        await PlacesUtils.bookmarks.insert({
          url: uri,
          parentGuid: PlacesUtils.bookmarks.unfiledGuid,
          title: "test bookmark",
        });
        info("Bookmark added");
      }
    }

    info("Adding visits");
    await PlacesTestUtils.addVisits(visits);

    info("Preparing filters");
    let filter = {};
    let beginIndex = 0;
    let endIndex = visits.length - 1;
    if ("begin" in options) {
      let ms = Number(visits[options.begin].test.jsDate) - 1000;
      filter.beginDate = new Date(ms);
      beginIndex = options.begin;
    }
    if ("end" in options) {
      let ms = Number(visits[options.end].test.jsDate) + 1000;
      filter.endDate = new Date(ms);
      endIndex = options.end;
    }
    if ("limit" in options) {
      endIndex = beginIndex + options.limit - 1; // -1 because the start index is inclusive.
      filter.limit = options.limit;
    }
    let removedItems = visits.slice(beginIndex);
    endIndex -= beginIndex;
    if (options.url) {
      let rawURL = "";
      switch (options.url) {
        case 1:
          filter.url = new URL(removedItems[0].uri.spec);
          rawURL = filter.url.href;
          break;
        case 2:
          filter.url = removedItems[0].uri;
          rawURL = filter.url.spec;
          break;
        case 3:
          filter.url = removedItems[0].uri.spec;
          rawURL = filter.url;
          break;
      }
      endIndex = Math.min(
        endIndex,
        removedItems.findIndex(v => v.uri.spec != rawURL) - 1
      );
    }
    removedItems.splice(endIndex + 1);
    let remainingItems = visits.filter(v => !removedItems.includes(v));
    for (let i = 0; i < removedItems.length; i++) {
      let test = removedItems[i].test;
      info("Marking visit " + (beginIndex + i) + " as expecting removal");
      test.toRemove = true;
      if (
        test.hasBookmark ||
        (options.url &&
          remainingItems.some(v => v.uri.spec == removedItems[i].uri.spec))
      ) {
        rankingChangePromises.push(Promise.withResolvers());
      } else if (!options.url || i == 0) {
        uriDeletePromises.set(
          removedItems[i].uri.spec,
          Promise.withResolvers()
        );
      }
    }

    const placesEventListener = events => {
      for (const event of events) {
        switch (event.type) {
          case "page-title-changed": {
            this.deferred.reject(
              "Unexpected page-title-changed event happens on " + event.url
            );
            break;
          }
          case "history-cleared": {
            info("history-cleared");
            this.deferred.reject("Unexpected history-cleared event happens");
            break;
          }
          case "pages-rank-changed": {
            info("pages-rank-changed");
            for (const deferred of rankingChangePromises) {
              deferred.resolve();
            }
            break;
          }
        }
      }
    };
    PlacesObservers.addListener(
      ["page-title-changed", "history-cleared", "pages-rank-changed"],
      placesEventListener
    );

    let cbarg;
    if (options.useCallback) {
      info("Setting up callback");
      cbarg = [
        info => {
          for (let visit of visits) {
            info("Comparing " + info.date + " and " + visit.test.jsDate);
            if (Math.abs(visit.test.jsDate - info.date) < 100) {
              // Assume rounding errors
              Assert.ok(
                !visit.test.announcedByOnRow,
                "This is the first time we announce the removal of this visit"
              );
              Assert.ok(
                visit.test.toRemove,
                "This is a visit we intended to remove"
              );
              visit.test.announcedByOnRow = true;
              return;
            }
          }
          Assert.ok(false, "Could not find the visit we attempt to remove");
        },
      ];
    } else {
      info("No callback");
      cbarg = [];
    }
    let result = await PlacesUtils.history.removeVisitsByFilter(
      filter,
      ...cbarg
    );

    Assert.ok(result, "Removal succeeded");

    // Make sure that we have eliminated exactly the entries we expected
    // to eliminate.
    for (let i = 0; i < visits.length; ++i) {
      let visit = visits[i];
      info("Controlling the results on visit " + i);
      let remainingVisitsForURI = remainingItems.filter(
        v => visit.uri.spec == v.uri.spec
      ).length;
      Assert.equal(
        visits_in_database(visit.uri),
        remainingVisitsForURI,
        "Visit is still present iff expected"
      );
      if (options.useCallback) {
        Assert.equal(
          visit.test.toRemove,
          visit.test.announcedByOnRow,
          "Visit removal has been announced by onResult iff expected"
        );
      }
      if (visit.test.hasBookmark || remainingVisitsForURI) {
        Assert.notEqual(
          page_in_database(visit.uri),
          0,
          "The page should still appear in the db"
        );
      } else {
        Assert.equal(
          page_in_database(visit.uri),
          0,
          "The page should have been removed from the db"
        );
      }
    }

    // Make sure that the observer has been called wherever applicable.
    info("Checking URI delete promises.");
    await Promise.all(Array.from(uriDeletePromises.values()));
    info("Checking frecency change promises.");
    await Promise.all(rankingChangePromises);
    PlacesObservers.removeListener(
      ["page-title-changed", "history-cleared", "pages-rank-changed"],
      placesEventListener
    );
  };

  let size = 20;
  for (let range of [
    { begin: 0 },
    { end: 19 },
    { begin: 0, end: 10 },
    { begin: 3, end: 4 },
    { begin: 5, end: 8, limit: 2 },
    { begin: 10, end: 18, limit: 5 },
  ]) {
    for (let bookmarks of [[], [5, 6]]) {
      let options = {
        sampleSize: size,
        bookmarks,
      };
      if ("begin" in range) {
        options.begin = range.begin;
      }
      if ("end" in range) {
        options.end = range.end;
      }
      if ("limit" in range) {
        options.limit = range.limit;
      }
      await remover(options);
      options.url = 1;
      await remover(options);
      options.url = 2;
      await remover(options);
      options.url = 3;
      await remover(options);
    }
  }
  await PlacesUtils.history.clear();
});

// Test the various error cases
add_task(async function test_error_cases() {
  Assert.throws(
    () => PlacesUtils.history.removeVisitsByFilter(),
    /TypeError: Expected a filter/
  );
  Assert.throws(
    () => PlacesUtils.history.removeVisitsByFilter("obviously, not a filter"),
    /TypeError: Expected a filter/
  );
  Assert.throws(
    () => PlacesUtils.history.removeVisitsByFilter({}),
    /TypeError: Expected a non-empty filter/
  );
  Assert.throws(
    () => PlacesUtils.history.removeVisitsByFilter({ beginDate: "now" }),
    /TypeError: Expected a valid Date/
  );
  Assert.throws(
    () => PlacesUtils.history.removeVisitsByFilter({ beginDate: Date.now() }),
    /TypeError: Expected a valid Date/
  );
  Assert.throws(
    () =>
      PlacesUtils.history.removeVisitsByFilter({ beginDate: new Date(NaN) }),
    /TypeError: Expected a valid Date/
  );
  Assert.throws(
    () =>
      PlacesUtils.history.removeVisitsByFilter(
        { beginDate: new Date() },
        "obviously, not a callback"
      ),
    /TypeError: Invalid function/
  );
  Assert.throws(
    () =>
      PlacesUtils.history.removeVisitsByFilter({
        beginDate: new Date(1000),
        endDate: new Date(0),
      }),
    /TypeError: `beginDate` should be at least as old/
  );
  Assert.throws(
    () => PlacesUtils.history.removeVisitsByFilter({ limit: {} }),
    /Expected a non-zero positive integer as a limit/
  );
  Assert.throws(
    () => PlacesUtils.history.removeVisitsByFilter({ limit: -1 }),
    /Expected a non-zero positive integer as a limit/
  );
  Assert.throws(
    () => PlacesUtils.history.removeVisitsByFilter({ limit: 0.1 }),
    /Expected a non-zero positive integer as a limit/
  );
  Assert.throws(
    () => PlacesUtils.history.removeVisitsByFilter({ limit: Infinity }),
    /Expected a non-zero positive integer as a limit/
  );
  Assert.throws(
    () => PlacesUtils.history.removeVisitsByFilter({ url: {} }),
    /Expected a valid URL for `url`/
  );
  Assert.throws(
    () => PlacesUtils.history.removeVisitsByFilter({ url: 0 }),
    /Expected a valid URL for `url`/
  );
  Assert.throws(
    () =>
      PlacesUtils.history.removeVisitsByFilter({
        beginDate: new Date(1000),
        endDate: new Date(0),
      }),
    /TypeError: `beginDate` should be at least as old/
  );
  Assert.throws(
    () =>
      PlacesUtils.history.removeVisitsByFilter({
        beginDate: new Date(1000),
        endDate: new Date(0),
      }),
    /TypeError: `beginDate` should be at least as old/
  );
  Assert.throws(
    () => PlacesUtils.history.removeVisitsByFilter({ transition: -1 }),
    /TypeError: `transition` should be valid/
  );
});

add_task(async function test_orphans() {
  let uri = NetUtil.newURI("http://moz.org/");
  await PlacesTestUtils.addVisits({ uri });

  PlacesUtils.favicons.setAndFetchFaviconForPage(
    uri,
    SMALLPNG_DATA_URI,
    true,
    PlacesUtils.favicons.FAVICON_LOAD_NON_PRIVATE,
    null,
    Services.scriptSecurityManager.getSystemPrincipal()
  );
  await PlacesUtils.history.update({
    url: uri,
    annotations: new Map([["test", "restval"]]),
  });

  await PlacesUtils.history.removeVisitsByFilter({
    beginDate: new Date(1999, 9, 9, 9, 9),
    endDate: new Date(),
  });
  Assert.ok(
    !(await PlacesTestUtils.isPageInDB(uri)),
    "Page should have been removed"
  );
  let db = await PlacesUtils.promiseDBConnection();
  let rows = await db.execute(`SELECT (SELECT count(*) FROM moz_annos) +
                                      (SELECT count(*) FROM moz_icons) +
                                      (SELECT count(*) FROM moz_pages_w_icons) +
                                      (SELECT count(*) FROM moz_icons_to_pages) AS count`);
  Assert.equal(rows[0].getResultByName("count"), 0, "Should not find orphans");
});