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

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

add_task(function* test_downloads_saveAs() {
  function background() {
    const url = URL.createObjectURL(new Blob(["file content"]));
    browser.test.onMessage.addListener(async () => {
      try {
        let id = await browser.downloads.download({url, saveAs: true});
        browser.downloads.onChanged.addListener(delta => {
          if (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 {MockFilePicker} = SpecialPowers;
  const manifest = {background, manifest: {permissions: ["downloads"]}};
  const extension = ExtensionTestUtils.loadExtension(manifest);

  MockFilePicker.init(window);
  MockFilePicker.useAnyFile();
  const [file] = MockFilePicker.returnFiles;

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

  extension.sendMessage("download");
  let result = yield extension.awaitMessage("done");

  ok(result.ok, "downloads.download() works with saveAs");
  is(file.fileSize, 12, "downloaded file is the correct size");
  file.remove(false);

  // Test the user canceling the save dialog.
  MockFilePicker.returnValue = MockFilePicker.returnCancel;

  extension.sendMessage("download");
  result = yield extension.awaitMessage("done");

  ok(!result.ok, "download rejected if the user cancels the dialog");
  is(result.message, "Download canceled by the user", "with the correct message");
  ok(!file.exists(), "file was not downloaded");

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

</script>

</body>
</html>