summaryrefslogtreecommitdiffstats
path: root/browser/components/downloads/test/unit/test_DownloadLastDir_basics.js
blob: f1dfbe473307442e65fa06ac0125d7ba62a51e0e (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
/* Any copyright is dedicated to the Public Domain.
 * https://creativecommons.org/publicdomain/zero/1.0/ */

// Basic test for setting and retrieving a download last dir.
// More complex tests can be found in browser/components/privatebrowsing/.

const SAVE_PER_SITE_PREF_BRANCH = "browser.download.lastDir";
const SAVE_PER_SITE_PREF = SAVE_PER_SITE_PREF_BRANCH + ".savePerSite";

let { FileUtils } = ChromeUtils.importESModule(
  "resource://gre/modules/FileUtils.sys.mjs"
);
let { DownloadLastDir } = ChromeUtils.importESModule(
  "resource://gre/modules/DownloadLastDir.sys.mjs"
);

add_task(
  {
    pref_set: [[SAVE_PER_SITE_PREF, true]],
  },
  async function test() {
    let downloadLastDir = new DownloadLastDir(null);

    let unknownUri = Services.io.newURI("https://unknown.org/");
    Assert.deepEqual(
      await downloadLastDir.getFileAsync(unknownUri),
      null,
      "Untracked URI, no pref set"
    );

    let dir1 = FileUtils.getDir("TmpD", ["dir1"], true);
    let uri1 = Services.io.newURI("https://test1.moz.org");
    downloadLastDir.setFile(uri1, dir1);
    let dir2 = FileUtils.getDir("TmpD", ["dir2"], true);
    let uri2 = Services.io.newURI("https://test2.moz.org");
    downloadLastDir.setFile(uri2, dir2);
    let dir3 = FileUtils.getDir("TmpD", ["dir3"], true);
    downloadLastDir.setFile(null, dir3);
    Assert.equal(
      (await downloadLastDir.getFileAsync(uri1)).path,
      dir1.path,
      "Check common URI"
    );
    Assert.equal(
      (await downloadLastDir.getFileAsync(uri2)).path,
      dir2.path,
      "Check common URI"
    );
    Assert.equal(downloadLastDir.file.path, dir3.path, "No URI");
    Assert.equal(
      (await downloadLastDir.getFileAsync(unknownUri)).path,
      dir3.path,
      "Untracked URI, pref set"
    );

    info("Check clearHistory removes all data");
    let subject = {};
    Services.obs.notifyObservers(subject, "browser:purge-session-history");
    await subject.promise;
    Assert.deepEqual(
      await downloadLastDir.getFileAsync(uri1),
      null,
      "Check common URI after clear history returns null"
    );
    Assert.deepEqual(
      await downloadLastDir.getFileAsync(uri2),
      null,
      "Check common URI after clear history returns null"
    );
    Assert.deepEqual(
      await downloadLastDir.getFileAsync(unknownUri),
      null,
      "Check untracked URI after clear history returns null"
    );

    // file: URIs should all point to the same folder.
    let fileUri1 = Services.io.newURI("file:///c:/test.txt");
    downloadLastDir.setFile(uri1, dir3);
    let dir4 = FileUtils.getDir("TmpD", ["dir4"], true);
    let fileUri2 = Services.io.newURI("file:///d:/test.png");
    downloadLastDir.setFile(uri1, dir4);
    Assert.equal(
      (await downloadLastDir.getFileAsync(fileUri1)).path,
      dir4.path,
      "Check file URI"
    );
    Assert.equal(
      (await downloadLastDir.getFileAsync(fileUri2)).path,
      dir4.path,
      "Check file URI"
    );
    let unknownFileUri = Services.io.newURI("file:///e:/test.mkv");
    Assert.equal(
      (await downloadLastDir.getFileAsync(unknownFileUri)).path,
      dir4.path,
      "Untracked File URI, pref set"
    );

    // data: URIs should point to a folder per mime-type.
    // Unspecified mime-type is handled as text/plain.
    let dataUri1 = Services.io.newURI("data:text/plain;charset=UTF-8,1234");
    downloadLastDir.setFile(dataUri1, dir1);
    let dataUri2 = Services.io.newURI("data:image/png;base64,1234");
    Assert.equal(
      (await downloadLastDir.getFileAsync(dataUri2)).path,
      dir1.path,
      "Check data URI"
    );
    let dataUri3 = Services.io.newURI("data:image/png,5678");
    downloadLastDir.setFile(dataUri3, dir2);
    Assert.equal(
      (await downloadLastDir.getFileAsync(dataUri2)).path,
      dir2.path,
      "Data URI was changed, same mime-type"
    );
    Assert.equal(
      (await downloadLastDir.getFileAsync(dataUri1)).path,
      dir1.path,
      "Data URI was not changed, different mime-type"
    );
    let dataUri4 = Services.io.newURI("data:,");
    Assert.equal(
      (await downloadLastDir.getFileAsync(dataUri4)).path,
      dir1.path,
      "Data URI defaults to text/plain"
    );
    downloadLastDir.setFile(null, dir4);
    let unknownDataUri = Services.io.newURI("data:application/zip,");
    Assert.deepEqual(
      (await downloadLastDir.getFileAsync(unknownDataUri)).path,
      dir4.path,
      "Untracked data URI"
    );
    Assert.equal(
      (await downloadLastDir.getFileAsync(dataUri4)).path,
      dir1.path,
      "Data URI didn't change"
    );
  }
);