summaryrefslogtreecommitdiffstats
path: root/toolkit/content/tests/browser/browser_saveImageURL.js
blob: c936b8ef84199d2c1fd7b0bad3783f362e79c247 (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
"use strict";

const IMAGE_PAGE =
  "https://example.com/browser/toolkit/content/tests/browser/image_page.html";

var MockFilePicker = SpecialPowers.MockFilePicker;

MockFilePicker.init(window);
MockFilePicker.returnValue = MockFilePicker.returnCancel;

registerCleanupFunction(function () {
  MockFilePicker.cleanup();
});

function waitForFilePicker() {
  return new Promise(resolve => {
    MockFilePicker.showCallback = () => {
      MockFilePicker.showCallback = null;
      ok(true, "Saw the file picker");
      resolve();
    };
  });
}

/**
 * Test that internalSave works when saving an image like the context menu does.
 */
add_task(async function preferred_API() {
  await BrowserTestUtils.withNewTab(
    {
      gBrowser,
      url: IMAGE_PAGE,
    },
    async function (browser) {
      let url = await SpecialPowers.spawn(browser, [], async function () {
        let image = content.document.getElementById("image");
        return image.href;
      });

      let filePickerPromise = waitForFilePicker();
      internalSave(
        url,
        null, // originalURL
        null, // document
        "image.jpg",
        null, // content disposition
        "image/jpeg",
        true, // bypass cache
        null, // dialog title key
        null, // chosen data
        null, // no referrer info
        null, // no document
        false, // don't skip the filename prompt
        null, // cache key
        false, // not private.
        gBrowser.contentPrincipal
      );
      await SpecialPowers.spawn(gBrowser.selectedBrowser, [], async () => {
        let channel = docShell.currentDocumentChannel;
        if (channel) {
          todo(
            channel.QueryInterface(Ci.nsIHttpChannelInternal)
              .channelIsForDownload
          );

          // Throttleable is the only class flag assigned to downloads.
          todo(
            channel.QueryInterface(Ci.nsIClassOfService).classFlags ==
              Ci.nsIClassOfService.Throttleable
          );
        }
      });
      await filePickerPromise;
    }
  );
});