summaryrefslogtreecommitdiffstats
path: root/toolkit/components/extensions/test/browser/browser_ext_downloads_filters.js
blob: a3ee11aa6620918d1bb4bf5dabd2275cb03ba741 (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
/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
/* vim: set sts=2 sw=2 et tw=80: */

"use strict";

async function testAppliedFilters(ext, expectedFilter, expectedFilterCount) {
  let tempDir = FileUtils.getDir("TmpD", [`testDownloadDir-${Math.random()}`]);
  tempDir.create(Ci.nsIFile.DIRECTORY_TYPE, FileUtils.PERMS_DIRECTORY);

  let filterCount = 0;

  let MockFilePicker = SpecialPowers.MockFilePicker;
  MockFilePicker.init(window.browsingContext);
  MockFilePicker.displayDirectory = tempDir;
  MockFilePicker.returnValue = MockFilePicker.returnCancel;
  MockFilePicker.appendFiltersCallback = function (fp, val) {
    const hexstr = "0x" + ("000" + val.toString(16)).substr(-3);
    filterCount++;
    if (filterCount < expectedFilterCount) {
      is(val, expectedFilter, "Got expected filter: " + hexstr);
    } else if (filterCount == expectedFilterCount) {
      is(val, MockFilePicker.filterAll, "Got all files filter: " + hexstr);
    } else {
      is(val, null, "Got unexpected filter: " + hexstr);
    }
  };
  MockFilePicker.showCallback = function (fp) {
    const filename = fp.defaultString;
    info("MockFilePicker - save as: " + filename);
  };

  let manifest = {
    description: ext,
    permissions: ["downloads"],
  };

  const extension = ExtensionTestUtils.loadExtension({
    manifest: manifest,

    background: async function () {
      let ext = chrome.runtime.getManifest().description;
      await browser.test.assertRejects(
        browser.downloads.download({
          url: "http://any-origin/any-path/any-resource",
          filename: "any-file" + ext,
          saveAs: true,
        }),
        "Download canceled by the user",
        "expected request to be canceled"
      );
      browser.test.sendMessage("canceled");
    },
  });

  await extension.startup();
  await extension.awaitMessage("canceled");
  await extension.unload();

  is(
    filterCount,
    expectedFilterCount,
    "Got correct number of filters: " + filterCount
  );

  MockFilePicker.cleanup();

  tempDir.remove(true);
}

// Missing extension
add_task(async function testDownload_missing_All() {
  await testAppliedFilters("", null, 1);
});

// Unrecognized extension
add_task(async function testDownload_unrecognized_All() {
  await testAppliedFilters(".xxx", null, 1);
});

// Recognized extensions
add_task(async function testDownload_html_HTML() {
  await testAppliedFilters(".html", Ci.nsIFilePicker.filterHTML, 2);
});

add_task(async function testDownload_xhtml_HTML() {
  await testAppliedFilters(".xhtml", Ci.nsIFilePicker.filterHTML, 2);
});

add_task(async function testDownload_txt_Text() {
  await testAppliedFilters(".txt", Ci.nsIFilePicker.filterText, 2);
});

add_task(async function testDownload_text_Text() {
  await testAppliedFilters(".text", Ci.nsIFilePicker.filterText, 2);
});

add_task(async function testDownload_jpe_Images() {
  await testAppliedFilters(".jpe", Ci.nsIFilePicker.filterImages, 2);
});

add_task(async function testDownload_tif_Images() {
  await testAppliedFilters(".tif", Ci.nsIFilePicker.filterImages, 2);
});

add_task(async function testDownload_webp_Images() {
  await testAppliedFilters(".webp", Ci.nsIFilePicker.filterImages, 2);
});

add_task(async function testDownload_heic_Images() {
  await testAppliedFilters(".heic", Ci.nsIFilePicker.filterImages, 2);
});

add_task(async function testDownload_xml_XML() {
  await testAppliedFilters(".xml", Ci.nsIFilePicker.filterXML, 2);
});

add_task(async function testDownload_aac_Audio() {
  await testAppliedFilters(".aac", Ci.nsIFilePicker.filterAudio, 2);
});

add_task(async function testDownload_mp3_Audio() {
  await testAppliedFilters(".mp3", Ci.nsIFilePicker.filterAudio, 2);
});

add_task(async function testDownload_wma_Audio() {
  await testAppliedFilters(".wma", Ci.nsIFilePicker.filterAudio, 2);
});

add_task(async function testDownload_avi_Video() {
  await testAppliedFilters(".avi", Ci.nsIFilePicker.filterVideo, 2);
});

add_task(async function testDownload_mp4_Video() {
  await testAppliedFilters(".mp4", Ci.nsIFilePicker.filterVideo, 2);
});

add_task(async function testDownload_xvid_Video() {
  await testAppliedFilters(".xvid", Ci.nsIFilePicker.filterVideo, 2);
});