summaryrefslogtreecommitdiffstats
path: root/toolkit/components/extensions/test/mochitest/test_chrome_ext_downloads_uniquify.html
blob: 99a6c4850004309b8bcdcbd95a1b5ee676b32efc (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
<!doctype html>
<html>
<head>
  <title>Test downloads.download() uniquify option</title>
  <script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
  <script src="chrome://mochikit/content/tests/SimpleTest/ExtensionTestUtils.js"></script>
  <script src="head.js"></script>
  <link rel="stylesheet" href="chrome://mochikit/content/tests/SimpleTest/test.css"/>
</head>
<body>

<script type="text/javascript">
"use strict";

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

let directory;

add_task(async function setup() {
  directory = FileUtils.getDir("TmpD", ["downloads"]);
  directory.createUnique(Ci.nsIFile.DIRECTORY_TYPE, FileUtils.PERMS_DIRECTORY);
  info(`Using download directory ${directory.path}`);

  await SpecialPowers.pushPrefEnv({"set": [
    ["browser.download.folderList", 2],
    ["browser.download.dir", directory.path],
  ]});

  SimpleTest.registerCleanupFunction(async () => {
    await SpecialPowers.popPrefEnv();
    directory.remove(true);
  });
});

add_task(async function test_downloads_uniquify() {
  const file = directory.clone();
  file.append("file_download.txt");

  const unique = directory.clone();
  unique.append("file_download(1).txt");

  const {MockFilePicker} = SpecialPowers;
  MockFilePicker.init(window);
  MockFilePicker.returnValue = MockFilePicker.returnOK;

  MockFilePicker.showCallback = fp => {
    let file = directory.clone();
    file.append(fp.defaultString);
    MockFilePicker.setFiles([file]);
  };

  function background() {
    const url = URL.createObjectURL(new Blob(["file content"]));
    browser.test.onMessage.addListener(async (filename, saveAs) => {
      try {
        let id = await browser.downloads.download({
          url,
          filename,
          saveAs,
          conflictAction: "uniquify",
        });
        browser.downloads.onChanged.addListener(delta => {
          if (delta.id == id && delta.state.current === "complete") {
            browser.test.sendMessage("done", {ok: true, id});
          }
        });
      } catch ({message}) {
        browser.test.sendMessage("done", {ok: false, message});
      }
    });
    browser.test.sendMessage("ready");
  }

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

  await extension.startup();
  await extension.awaitMessage("ready");

  async function testUniquify(saveAs) {
    info(`Testing conflictAction:"uniquify" with saveAs=${saveAs}`);

    ok(!file.exists(), "downloaded file should have been cleaned up before test ran");
    ok(!unique.exists(), "uniquified file should have been cleaned up before test ran");

    // Test download without uniquify and create a conflicting file so we can
    // test with uniquify.
    extension.sendMessage("file_download.txt", saveAs);
    let result = await extension.awaitMessage("done");
    ok(result.ok, "downloads.download() works with saveAs");

    ok(file.exists(), "the file exists.");
    is(file.fileSize, 12, "downloaded file is the correct size");

    // Now that a conflicting file exists, test the uniquify behavior
    extension.sendMessage("file_download.txt", saveAs);
    result = await extension.awaitMessage("done");
    ok(result.ok, "downloads.download() works with saveAs and uniquify");

    ok(unique.exists(), "the file exists.");
    is(unique.fileSize, 12, "downloaded file is the correct size");

    file.remove(false);
    unique.remove(false);
  }
  await testUniquify(true);
  await testUniquify(false);

  await extension.unload();
  MockFilePicker.cleanup();
});

</script>

</body>
</html>