summaryrefslogtreecommitdiffstats
path: root/browser/components/downloads/test/unit/test_DownloadsViewableInternally.js
blob: 16aee6e209c87c61d5510de2ada39c7c46652ae9 (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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

const PREF_SVG_DISABLED = "svg.disabled";
const PREF_WEBP_ENABLED = "image.webp.enabled";
const PDF_MIME = "application/pdf";
const OCTET_MIME = "application/octet-stream";
const XML_MIME = "text/xml";
const SVG_MIME = "image/svg+xml";
const WEBP_MIME = "image/webp";

const { Integration } = ChromeUtils.import(
  "resource://gre/modules/Integration.jsm"
);
const {
  DownloadsViewableInternally,
  PREF_ENABLED_TYPES,
  PREF_BRANCH_WAS_REGISTERED,
  PREF_BRANCH_PREVIOUS_ACTION,
  PREF_BRANCH_PREVIOUS_ASK,
} = ChromeUtils.import("resource:///modules/DownloadsViewableInternally.jsm");

/* global DownloadIntegration */
Integration.downloads.defineModuleGetter(
  this,
  "DownloadIntegration",
  "resource://gre/modules/DownloadIntegration.jsm"
);

const HandlerService = Cc[
  "@mozilla.org/uriloader/handler-service;1"
].getService(Ci.nsIHandlerService);
const MIMEService = Cc["@mozilla.org/mime;1"].getService(Ci.nsIMIMEService);

function checkPreferInternal(mime, ext, expectedPreferInternal) {
  const handler = MIMEService.getFromTypeAndExtension(mime, ext);
  if (expectedPreferInternal) {
    Assert.equal(
      handler?.preferredAction,
      Ci.nsIHandlerInfo.handleInternally,
      `checking ${mime} preferredAction == handleInternally`
    );
  } else {
    Assert.notEqual(
      handler?.preferredAction,
      Ci.nsIHandlerInfo.handleInternally,
      `checking ${mime} preferredAction != handleInternally`
    );
  }
}

function shouldView(mime, ext) {
  return DownloadIntegration.shouldViewDownloadInternally(mime, ext);
}

function checkShouldView(mime, ext, expectedShouldView) {
  Assert.equal(
    shouldView(mime, ext),
    expectedShouldView,
    `checking ${mime} shouldViewDownloadInternally`
  );
}

function checkWasRegistered(ext, expectedWasRegistered) {
  Assert.equal(
    Services.prefs.getBoolPref(PREF_BRANCH_WAS_REGISTERED + ext, false),
    expectedWasRegistered,
    `checking ${ext} was registered pref`
  );
}

function checkAll(mime, ext, expected) {
  checkPreferInternal(mime, ext, expected);
  checkShouldView(mime, ext, expected);
  checkWasRegistered(ext, expected);
}

add_task(async function test_viewable_internally() {
  Services.prefs.setCharPref(PREF_ENABLED_TYPES, "xml , svg,webp");
  Services.prefs.setBoolPref(PREF_SVG_DISABLED, false);
  Services.prefs.setBoolPref(PREF_WEBP_ENABLED, true);

  checkAll(XML_MIME, "xml", false);
  checkAll(SVG_MIME, "svg", false);
  checkAll(WEBP_MIME, "webp", false);

  DownloadsViewableInternally.register();

  checkAll(XML_MIME, "xml", true);
  checkAll(SVG_MIME, "svg", true);
  checkAll(WEBP_MIME, "webp", true);

  // Remove SVG so it won't be cleared
  Services.prefs.clearUserPref(PREF_BRANCH_WAS_REGISTERED + "svg");

  // Disable xml and svg, check that xml becomes disabled
  Services.prefs.setCharPref(PREF_ENABLED_TYPES, "webp");

  checkAll(XML_MIME, "xml", false);
  checkAll(WEBP_MIME, "webp", true);

  // SVG shouldn't be cleared
  checkPreferInternal(SVG_MIME, "svg", true);

  Assert.ok(
    shouldView(PDF_MIME),
    "application/pdf should be unaffected by pref"
  );
  Assert.ok(
    shouldView(OCTET_MIME, "pdf"),
    ".pdf should be accepted by extension"
  );
  Assert.ok(
    shouldView(OCTET_MIME, "PDF"),
    ".pdf should be detected case-insensitively"
  );
  Assert.ok(!shouldView(OCTET_MIME, "exe"), ".exe shouldn't be accepted");

  Assert.ok(!shouldView(XML_MIME), "text/xml should be disabled by pref");
  Assert.ok(!shouldView(SVG_MIME), "image/xml+svg should be disabled by pref");

  // Enable, check that everything is enabled again
  Services.prefs.setCharPref(PREF_ENABLED_TYPES, "xml,svg,webp");

  checkPreferInternal(XML_MIME, "xml", true);
  checkPreferInternal(SVG_MIME, "svg", true);

  Assert.ok(
    shouldView(PDF_MIME),
    "application/pdf should be unaffected by pref"
  );
  Assert.ok(shouldView(XML_MIME), "text/xml should be enabled by pref");
  Assert.ok(
    shouldView("application/xml"),
    "alternate MIME type application/xml should be accepted"
  );
  Assert.ok(
    shouldView(OCTET_MIME, "xml"),
    ".xml should be accepted by extension"
  );

  // Disable viewable internally, pre-set handlers.
  Services.prefs.setCharPref(PREF_ENABLED_TYPES, "");

  for (const [mime, ext, action, ask] of [
    [XML_MIME, "xml", Ci.nsIHandlerInfo.useSystemDefault, true],
    [SVG_MIME, "svg", Ci.nsIHandlerInfo.saveToDisk, true],
    [WEBP_MIME, "webp", Ci.nsIHandlerInfo.saveToDisk, false],
  ]) {
    let handler = MIMEService.getFromTypeAndExtension(mime, ext);
    handler.preferredAction = action;
    handler.alwaysAskBeforeHandling = ask;

    HandlerService.store(handler);
    checkPreferInternal(mime, ext, false);

    // Expect to read back the same values
    handler = MIMEService.getFromTypeAndExtension(mime, ext);
    Assert.equal(handler.preferredAction, action);
    Assert.equal(handler.alwaysAskBeforeHandling, ask);
  }

  // Enable viewable internally, XML should not be replaced, SVG and WebP should be saved.
  Services.prefs.setCharPref(PREF_ENABLED_TYPES, "svg,webp,xml");

  Assert.equal(
    Services.prefs.getIntPref(PREF_BRANCH_PREVIOUS_ACTION + "svg"),
    Ci.nsIHandlerInfo.saveToDisk,
    "svg action should be saved"
  );
  Assert.equal(
    Services.prefs.getBoolPref(PREF_BRANCH_PREVIOUS_ASK + "svg"),
    true,
    "svg ask should be saved"
  );
  Assert.equal(
    Services.prefs.getIntPref(PREF_BRANCH_PREVIOUS_ACTION + "webp"),
    Ci.nsIHandlerInfo.saveToDisk,
    "webp action should be saved"
  );
  Assert.equal(
    Services.prefs.getBoolPref(PREF_BRANCH_PREVIOUS_ASK + "webp"),
    false,
    "webp ask should be saved"
  );

  {
    let handler = MIMEService.getFromTypeAndExtension(XML_MIME, "xml");
    Assert.equal(
      handler.preferredAction,
      Ci.nsIHandlerInfo.useSystemDefault,
      "svg action should be preserved"
    );
    Assert.equal(
      !!handler.alwaysAskBeforeHandling,
      true,
      "svg ask should be preserved"
    );
    // Clean up
    HandlerService.remove(handler);
  }
  // It should still be possible to view XML internally
  checkShouldView(XML_MIME, "xml", true);
  checkWasRegistered("xml", true);

  checkAll(SVG_MIME, "svg", true);
  checkAll(WEBP_MIME, "webp", true);

  // Disable SVG to test SVG enabled check (depends on the pref)
  Services.prefs.setBoolPref(PREF_SVG_DISABLED, true);
  // Should have restored the settings from above
  {
    let handler = MIMEService.getFromTypeAndExtension(SVG_MIME, "svg");
    Assert.equal(handler.preferredAction, Ci.nsIHandlerInfo.saveToDisk);
    Assert.equal(!!handler.alwaysAskBeforeHandling, true);
    // Clean up
    HandlerService.remove(handler);
  }
  checkAll(SVG_MIME, "svg", false);

  Services.prefs.setBoolPref(PREF_SVG_DISABLED, false);
  checkAll(SVG_MIME, "svg", true);

  // Test WebP enabled check (depends on the pref)
  Services.prefs.setBoolPref(PREF_WEBP_ENABLED, false);
  // Should have restored the settings from above
  {
    let handler = MIMEService.getFromTypeAndExtension(WEBP_MIME, "webp");
    Assert.equal(handler.preferredAction, Ci.nsIHandlerInfo.saveToDisk);
    Assert.equal(!!handler.alwaysAskBeforeHandling, false);
    // Clean up
    HandlerService.remove(handler);
  }
  checkAll(WEBP_MIME, "webp", false);

  Services.prefs.setBoolPref(PREF_WEBP_ENABLED, true);
  checkAll(WEBP_MIME, "webp", true);

  Assert.ok(!shouldView(null, "pdf"), "missing MIME shouldn't be accepted");
  Assert.ok(!shouldView(null, "xml"), "missing MIME shouldn't be accepted");
  Assert.ok(!shouldView(OCTET_MIME), "unsupported MIME shouldn't be accepted");
  Assert.ok(!shouldView(OCTET_MIME, "exe"), ".exe shouldn't be accepted");
});

registerCleanupFunction(() => {
  // Clear all types to remove any saved values
  Services.prefs.setCharPref(PREF_ENABLED_TYPES, "");
  // Reset to the defaults
  Services.prefs.clearUserPref(PREF_ENABLED_TYPES);
  Services.prefs.clearUserPref(PREF_SVG_DISABLED);
  Services.prefs.clearUserPref(PREF_WEBP_ENABLED);
});