summaryrefslogtreecommitdiffstats
path: root/toolkit/components/downloads/test/unit/test_DownloadHistory_initialization2.js
blob: 8b714a8faedcfe9306c4330a67bcef4ad61a6c39 (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
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

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

/**
 * This test is designed to ensure the cache of download history is correctly
 * loaded and initialized via adding downloads. We do this by having the test as
 * the only test in this file.
 */
add_task(async function test_initialization_via_addDownload() {
  // Clean up at the beginning and at the end of the test.
  async function cleanup() {
    await PlacesUtils.history.clear();
  }
  registerCleanupFunction(cleanup);
  await cleanup();

  const download1FileLocation = getTempFile(`${TEST_TARGET_FILE_NAME}1`).path;
  const download2FileLocation = getTempFile(`${TEST_TARGET_FILE_NAME}2`).path;
  const download = {
    source: {
      url: httpUrl(`source1`),
      isPrivate: false,
    },
    target: { path: download1FileLocation },
  };

  await DownloadHistory.addDownloadToHistory(download);

  // Initialize DownloadHistoryList only after having added the history and
  // session downloads.
  let historyList = await DownloadHistory.getList();
  let downloads = await historyList.getAll();
  Assert.equal(downloads.length, 1, "Should have only one entry");

  Assert.equal(
    downloads[0].target.path,
    download1FileLocation,
    "Should have the correct target path"
  );

  // Now re-add the download but with a different target.
  download.target.path = download2FileLocation;

  await DownloadHistory.addDownloadToHistory(download);

  historyList = await DownloadHistory.getList();
  downloads = await historyList.getAll();
  Assert.equal(downloads.length, 1, "Should still have only one entry");

  Assert.equal(
    downloads[0].target.path,
    download2FileLocation,
    "Should have the correct revised target path"
  );
});