summaryrefslogtreecommitdiffstats
path: root/uriloader/exthandler/tests/unit/test_downloads_improvements_migration.js
blob: 49e4493b23a521ff25a6cf7a369cd6a9c2641d3c (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
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

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

const mimeSvc = Cc["@mozilla.org/mime;1"].getService(Ci.nsIMIMEService);

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

/* global DownloadIntegration */
Integration.downloads.defineESModuleGetter(
  this,
  "DownloadIntegration",
  "resource://gre/modules/DownloadIntegration.sys.mjs"
);

/**
 * Tests that the migration runs and that only
 * files with preferredAction alwaysAsk are updated.
 */
add_task(async function test_migration() {
  // Create mock implementation of shouldDownloadInternally for test case
  let oldShouldViewDownloadInternally =
    DownloadIntegration.shouldViewDownloadInternally;
  DownloadIntegration.shouldViewDownloadInternally = (mimeType, extension) => {
    let downloadTypesViewableInternally = [
      {
        extension: "pdf",
        mimeTypes: ["application/pdf"],
      },
      {
        extension: "webp",
        mimeTypes: ["image/webp"],
      },
    ];

    for (const mockHandler of downloadTypesViewableInternally) {
      if (mockHandler.mimeTypes.includes(mimeType)) {
        return true;
      }
    }

    return false;
  };

  registerCleanupFunction(async function () {
    Services.prefs.clearUserPref(
      "browser.download.improvements_to_download_panel"
    );
    DownloadIntegration.shouldViewDownloadInternally =
      oldShouldViewDownloadInternally;
  });

  // For setup, set pref to false. Will be enabled later.
  Services.prefs.setBoolPref(
    "browser.download.improvements_to_download_panel",
    false
  );

  // Plain text file
  let txtHandlerInfo = mimeSvc.getFromTypeAndExtension("text/plain", "txt");
  txtHandlerInfo.preferredAction = Ci.nsIHandlerInfo.alwaysAsk;
  txtHandlerInfo.alwaysAskBeforeHandling = true;
  // PDF file
  let pdfHandlerInfo = mimeSvc.getFromTypeAndExtension(
    "application/pdf",
    "pdf"
  );
  pdfHandlerInfo.preferredAction = Ci.nsIHandlerInfo.alwaysAsk;
  pdfHandlerInfo.alwaysAskBeforeHandling = true;
  // WebP file
  let webpHandlerInfo = mimeSvc.getFromTypeAndExtension("image/webp", "webp");
  webpHandlerInfo.preferredAction = Ci.nsIHandlerInfo.useSystemDefault;
  webpHandlerInfo.alwaysAskBeforeHandling = false;

  handlerSvc.store(txtHandlerInfo);
  handlerSvc.store(pdfHandlerInfo);
  handlerSvc.store(webpHandlerInfo);

  Services.prefs.setBoolPref(
    "browser.download.improvements_to_download_panel",
    true
  );
  gHandlerService.wrappedJSObject._migrateDownloadsImprovementsIfNeeded();

  txtHandlerInfo = mimeSvc.getFromTypeAndExtension("text/plain", "txt");
  pdfHandlerInfo = mimeSvc.getFromTypeAndExtension("application/pdf", "pdf");
  webpHandlerInfo = mimeSvc.getFromTypeAndExtension("image/webp", "webp");
  let data = gHandlerService.wrappedJSObject._store.data;
  Assert.equal(
    data.isDownloadsImprovementsAlreadyMigrated,
    true,
    "isDownloadsImprovementsAlreadyMigrated should be set to true"
  );
  Assert.equal(
    pdfHandlerInfo.preferredAction,
    Ci.nsIHandlerInfo.handleInternally,
    "application/pdf - preferredAction should be handleInternally"
  );
  Assert.equal(
    pdfHandlerInfo.alwaysAskBeforeHandling,
    false,
    "application/pdf - alwaysAskBeforeHandling should be false"
  );
  Assert.equal(
    webpHandlerInfo.preferredAction,
    Ci.nsIHandlerInfo.useSystemDefault,
    "image/webp - preferredAction should be useSystemDefault"
  );
  Assert.equal(
    webpHandlerInfo.alwaysAskBeforeHandling,
    false,
    "image/webp - alwaysAskBeforeHandling should be false"
  );
  Assert.equal(
    txtHandlerInfo.preferredAction,
    Ci.nsIHandlerInfo.saveToDisk,
    "text/plain - preferredAction should be saveToDisk"
  );
  Assert.equal(
    txtHandlerInfo.alwaysAskBeforeHandling,
    false,
    "text/plain - alwaysAskBeforeHandling should be false"
  );
});

/**
 * Tests that the migration does not run if the migration was already run.
 */
add_task(async function test_migration_already_run() {
  let data = gHandlerService.wrappedJSObject._store.data;
  data.isDownloadsImprovementsAlreadyMigrated = true;

  // Plain text file
  let txtHandlerInfo = mimeSvc.getFromTypeAndExtension("text/plain", "txt");
  txtHandlerInfo.preferredAction = Ci.nsIHandlerInfo.alwaysAsk;
  txtHandlerInfo.alwaysAskBeforeHandling = true;
  // PDF file
  let pdfHandlerInfo = mimeSvc.getFromTypeAndExtension(
    "application/pdf",
    "pdf"
  );
  pdfHandlerInfo.preferredAction = Ci.nsIHandlerInfo.alwaysAsk;
  pdfHandlerInfo.alwaysAskBeforeHandling = true;

  handlerSvc.store(txtHandlerInfo);
  handlerSvc.store(pdfHandlerInfo);

  gHandlerService.wrappedJSObject._migrateDownloadsImprovementsIfNeeded();

  txtHandlerInfo = mimeSvc.getFromTypeAndExtension("text/plain", "txt");
  pdfHandlerInfo = mimeSvc.getFromTypeAndExtension("application/pdf", "pdf");
  data = gHandlerService.wrappedJSObject._store.data;
  Assert.equal(
    pdfHandlerInfo.preferredAction,
    Ci.nsIHandlerInfo.alwaysAsk,
    "application/pdf - preferredAction should be alwaysAsk"
  );
  Assert.equal(
    pdfHandlerInfo.alwaysAskBeforeHandling,
    true,
    "application/pdf - alwaysAskBeforeHandling should be true"
  );
  Assert.equal(
    txtHandlerInfo.preferredAction,
    Ci.nsIHandlerInfo.alwaysAsk,
    "text/plain - preferredAction should be alwaysAsk"
  );
  Assert.equal(
    txtHandlerInfo.alwaysAskBeforeHandling,
    true,
    "text/plain - alwaysAskBeforeHandling should be true"
  );
});

/**
 * Test migration of SVG and XML info.
 */
add_task(async function test_migration_xml_svg() {
  let data = gHandlerService.wrappedJSObject._store.data;
  // Plain text file
  let txtHandlerInfo = mimeSvc.getFromTypeAndExtension("text/plain", "txt");
  txtHandlerInfo.preferredAction = Ci.nsIHandlerInfo.alwaysAsk;
  txtHandlerInfo.alwaysAskBeforeHandling = true;
  // SVG file
  let svgHandlerInfo = mimeSvc.getFromTypeAndExtension("image/svg+xml", "svg");
  svgHandlerInfo.preferredAction = Ci.nsIHandlerInfo.handleInternally;
  svgHandlerInfo.alwaysAskBeforeHandling = false;
  // XML file
  let xmlHandlerInfo = mimeSvc.getFromTypeAndExtension("text/xml", "xml");
  xmlHandlerInfo.preferredAction = Ci.nsIHandlerInfo.handleInternally;
  xmlHandlerInfo.alwaysAskBeforeHandling = false;

  handlerSvc.store(txtHandlerInfo);
  handlerSvc.store(svgHandlerInfo);
  handlerSvc.store(xmlHandlerInfo);

  gHandlerService.wrappedJSObject._migrateSVGXMLIfNeeded();

  txtHandlerInfo = mimeSvc.getFromTypeAndExtension("text/plain", "txt");
  svgHandlerInfo = mimeSvc.getFromTypeAndExtension("image/svg+xml", "svg");
  xmlHandlerInfo = mimeSvc.getFromTypeAndExtension("text/xml", "xml");
  data = gHandlerService.wrappedJSObject._store.data;
  Assert.equal(
    svgHandlerInfo.preferredAction,
    Ci.nsIHandlerInfo.saveToDisk,
    "image/svg+xml - preferredAction should be saveToDisk"
  );
  Assert.equal(
    svgHandlerInfo.alwaysAskBeforeHandling,
    false,
    "image/svg+xml - alwaysAskBeforeHandling should be false"
  );
  Assert.equal(
    xmlHandlerInfo.preferredAction,
    Ci.nsIHandlerInfo.saveToDisk,
    "text/xml - preferredAction should be saveToDisk"
  );
  Assert.equal(
    xmlHandlerInfo.alwaysAskBeforeHandling,
    false,
    "text/xml - alwaysAskBeforeHandling should be false"
  );
  Assert.equal(
    txtHandlerInfo.preferredAction,
    Ci.nsIHandlerInfo.alwaysAsk,
    "text/plain - preferredAction should be alwaysAsk"
  );
  Assert.equal(
    txtHandlerInfo.alwaysAskBeforeHandling,
    true,
    "text/plain - alwaysAskBeforeHandling should be true"
  );

  ok(
    data.isSVGXMLAlreadyMigrated,
    "Should have stored migration state on the data object."
  );
});