summaryrefslogtreecommitdiffstats
path: root/browser/components/extensions/test/xpcshell/test_ext_browsingData_downloads.js
blob: 1257f2360020ce0c5648f05ae86e8e45f416ea1c (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
/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
/* vim: set sts=2 sw=2 et tw=80: */
"use strict";

ChromeUtils.defineESModuleGetters(this, {
  Downloads: "resource://gre/modules/Downloads.sys.mjs",
});

const OLD_NAMES = {
  [Downloads.PUBLIC]: "old-public",
  [Downloads.PRIVATE]: "old-private",
};
const RECENT_NAMES = {
  [Downloads.PUBLIC]: "recent-public",
  [Downloads.PRIVATE]: "recent-private",
};
const REFERENCE_DATE = new Date();
const OLD_DATE = new Date(Number(REFERENCE_DATE) - 10000);

async function downloadExists(list, path) {
  let listArray = await list.getAll();
  return listArray.some(i => i.target.path == path);
}

async function checkDownloads(
  expectOldExists = true,
  expectRecentExists = true
) {
  for (let listType of [Downloads.PUBLIC, Downloads.PRIVATE]) {
    let downloadsList = await Downloads.getList(listType);
    equal(
      await downloadExists(downloadsList, OLD_NAMES[listType]),
      expectOldExists,
      `Fake old download ${expectOldExists ? "was found" : "was removed"}.`
    );
    equal(
      await downloadExists(downloadsList, RECENT_NAMES[listType]),
      expectRecentExists,
      `Fake recent download ${
        expectRecentExists ? "was found" : "was removed"
      }.`
    );
  }
}

async function setupDownloads() {
  let downloadsList = await Downloads.getList(Downloads.ALL);
  await downloadsList.removeFinished();

  for (let listType of [Downloads.PUBLIC, Downloads.PRIVATE]) {
    downloadsList = await Downloads.getList(listType);
    let download = await Downloads.createDownload({
      source: {
        url: "https://bugzilla.mozilla.org/show_bug.cgi?id=1321303",
        isPrivate: listType == Downloads.PRIVATE,
      },
      target: OLD_NAMES[listType],
    });
    download.startTime = OLD_DATE;
    download.canceled = true;
    await downloadsList.add(download);

    download = await Downloads.createDownload({
      source: {
        url: "https://bugzilla.mozilla.org/show_bug.cgi?id=1321303",
        isPrivate: listType == Downloads.PRIVATE,
      },
      target: RECENT_NAMES[listType],
    });
    download.startTime = REFERENCE_DATE;
    download.canceled = true;
    await downloadsList.add(download);
  }

  // Confirm everything worked.
  downloadsList = await Downloads.getList(Downloads.ALL);
  equal((await downloadsList.getAll()).length, 4, "4 fake downloads added.");
  checkDownloads();
}

add_task(async function testDownloads() {
  function background() {
    browser.test.onMessage.addListener(async (msg, options) => {
      if (msg == "removeDownloads") {
        await browser.browsingData.removeDownloads(options);
      } else {
        await browser.browsingData.remove(options, { downloads: true });
      }
      browser.test.sendMessage("downloadsRemoved");
    });
  }

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

  async function testRemovalMethod(method) {
    // Clear downloads with no since value.
    await setupDownloads();
    extension.sendMessage(method, {});
    await extension.awaitMessage("downloadsRemoved");
    await checkDownloads(false, false);

    // Clear downloads with recent since value.
    await setupDownloads();
    extension.sendMessage(method, { since: REFERENCE_DATE });
    await extension.awaitMessage("downloadsRemoved");
    await checkDownloads(true, false);

    // Clear downloads with old since value.
    await setupDownloads();
    extension.sendMessage(method, { since: REFERENCE_DATE - 100000 });
    await extension.awaitMessage("downloadsRemoved");
    await checkDownloads(false, false);
  }

  await extension.startup();

  await testRemovalMethod("removeDownloads");
  await testRemovalMethod("remove");

  await extension.unload();
});