summaryrefslogtreecommitdiffstats
path: root/uriloader/exthandler/tests/unit/test_handlerService.js
blob: 610eb5b749cc1994abcf910c419c5f08f3651240 (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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
/* 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/. */

function run_test() {
  //* *************************************************************************//
  // Constants

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

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

  const protoSvc = Cc[
    "@mozilla.org/uriloader/external-protocol-service;1"
  ].getService(Ci.nsIExternalProtocolService);

  const prefSvc = Services.prefs;

  const env = Cc["@mozilla.org/process/environment;1"].getService(
    Ci.nsIEnvironment
  );

  const rootPrefBranch = prefSvc.getBranch("");

  let noMailto = false;
  if (mozinfo.os == "win") {
    // Check mailto handler from registry.
    // If registry entry is nothing, no mailto handler
    let regSvc = Cc["@mozilla.org/windows-registry-key;1"].createInstance(
      Ci.nsIWindowsRegKey
    );
    try {
      regSvc.open(regSvc.ROOT_KEY_CLASSES_ROOT, "mailto", regSvc.ACCESS_READ);
      noMailto = false;
    } catch (ex) {
      noMailto = true;
    }
    regSvc.close();
  }

  if (mozinfo.os == "linux") {
    // Check mailto handler from GIO
    // If there isn't one, then we have no mailto handler
    let gIOSvc = Cc["@mozilla.org/gio-service;1"].createInstance(
      Ci.nsIGIOService
    );
    try {
      gIOSvc.getAppForURIScheme("mailto");
      noMailto = false;
    } catch (ex) {
      noMailto = true;
    }
  }

  //* *************************************************************************//
  // Sample Data

  // It doesn't matter whether or not this nsIFile is actually executable,
  // only that it has a path and exists.  Since we don't know any executable
  // that exists on all platforms (except possibly the application being
  // tested, but there doesn't seem to be a way to get a reference to that
  // from the directory service), we use the temporary directory itself.
  var executable = Services.dirsvc.get("TmpD", Ci.nsIFile);
  // XXX We could, of course, create an actual executable in the directory:
  // executable.append("localhandler");
  // if (!executable.exists())
  //  executable.create(Ci.nsIFile.NORMAL_FILE_TYPE, 0o755);

  var localHandler = Cc[
    "@mozilla.org/uriloader/local-handler-app;1"
  ].createInstance(Ci.nsILocalHandlerApp);
  localHandler.name = "Local Handler";
  localHandler.executable = executable;

  var webHandler = Cc[
    "@mozilla.org/uriloader/web-handler-app;1"
  ].createInstance(Ci.nsIWebHandlerApp);
  webHandler.name = "Web Handler";
  webHandler.uriTemplate = "http://www.example.com/?%s";

  // FIXME: these tests create and manipulate enough variables that it would
  // make sense to move each test into its own scope so we don't run the risk
  // of one test stomping on another's data.

  //* *************************************************************************//
  // Test Default Properties

  // Get a handler info for a MIME type that neither the application nor
  // the OS knows about and make sure its properties are set to the proper
  // default values.

  var handlerInfo = mimeSvc.getFromTypeAndExtension("nonexistent/type", null);

  // Make sure it's also an nsIHandlerInfo.
  Assert.ok(handlerInfo instanceof Ci.nsIHandlerInfo);

  Assert.equal(handlerInfo.type, "nonexistent/type");

  // Deprecated property, but we should still make sure it's set correctly.
  Assert.equal(handlerInfo.MIMEType, "nonexistent/type");

  // These properties are the ones the handler service knows how to store.
  Assert.equal(handlerInfo.preferredAction, Ci.nsIHandlerInfo.saveToDisk);
  Assert.equal(handlerInfo.preferredApplicationHandler, null);
  Assert.equal(handlerInfo.possibleApplicationHandlers.length, 0);
  Assert.ok(handlerInfo.alwaysAskBeforeHandling);

  // These properties are initialized to default values by the service,
  // so we might as well make sure they're initialized to the right defaults.
  Assert.equal(handlerInfo.description, "");
  Assert.equal(handlerInfo.hasDefaultHandler, false);
  Assert.equal(handlerInfo.defaultDescription, "");

  // test some default protocol info properties
  var haveDefaultHandlersVersion = false;
  try {
    // If we have a defaultHandlersVersion pref, then assume that we're in the
    // firefox tree and that we'll also have default handlers.
    // Bug 395131 has been filed to make this test work more generically
    // by providing our own prefs for this test rather than this icky
    // special casing.
    rootPrefBranch.getCharPref("gecko.handlerService.defaultHandlersVersion");
    haveDefaultHandlersVersion = true;
  } catch (ex) {}

  const kExternalWarningDefault =
    "network.protocol-handler.warn-external-default";
  prefSvc.setBoolPref(kExternalWarningDefault, true);

  // XXX add more thorough protocol info property checking

  // no OS default handler exists
  var protoInfo = protoSvc.getProtocolHandlerInfo("x-moz-rheet");
  Assert.equal(protoInfo.preferredAction, protoInfo.alwaysAsk);
  Assert.ok(protoInfo.alwaysAskBeforeHandling);

  // OS default exists, injected default does not exist,
  // explicit warning pref: false
  const kExternalWarningPrefPrefix = "network.protocol-handler.warn-external.";
  prefSvc.setBoolPref(kExternalWarningPrefPrefix + "http", false);
  protoInfo = protoSvc.getProtocolHandlerInfo("http");
  Assert.equal(0, protoInfo.possibleApplicationHandlers.length);
  Assert.ok(!protoInfo.alwaysAskBeforeHandling);

  // OS default exists, injected default does not exist,
  // explicit warning pref: true
  prefSvc.setBoolPref(kExternalWarningPrefPrefix + "http", true);
  protoInfo = protoSvc.getProtocolHandlerInfo("http");
  // OS handler isn't included in possibleApplicationHandlers, so length is 0
  // Once they become instances of nsILocalHandlerApp, this number will need
  // to change.
  Assert.equal(0, protoInfo.possibleApplicationHandlers.length);
  Assert.ok(protoInfo.alwaysAskBeforeHandling);

  // OS default exists, injected default exists, explicit warning pref: false
  prefSvc.setBoolPref(kExternalWarningPrefPrefix + "mailto", false);
  protoInfo = protoSvc.getProtocolHandlerInfo("mailto");
  if (haveDefaultHandlersVersion) {
    Assert.equal(2, protoInfo.possibleApplicationHandlers.length);
  } else {
    Assert.equal(0, protoInfo.possibleApplicationHandlers.length);
  }

  // Win7+ or Linux's GIO might not have a default mailto: handler
  if (noMailto) {
    Assert.ok(protoInfo.alwaysAskBeforeHandling);
  } else {
    Assert.ok(!protoInfo.alwaysAskBeforeHandling);
  }

  // OS default exists, injected default exists, explicit warning pref: true
  prefSvc.setBoolPref(kExternalWarningPrefPrefix + "mailto", true);
  protoInfo = protoSvc.getProtocolHandlerInfo("mailto");
  if (haveDefaultHandlersVersion) {
    Assert.equal(2, protoInfo.possibleApplicationHandlers.length);
    // Win7+ or Linux's GIO may have no default mailto: handler, so we'd ask
    // anyway. Otherwise, the default handlers will not have stored preferred
    // actions etc., so re-requesting them after the warning pref has changed
    // will use the updated pref value. So both when we have and do not have
    // a default mailto: handler, we'll ask:
    Assert.ok(protoInfo.alwaysAskBeforeHandling);
    // As soon as anyone actually stores updated defaults into the profile
    // database, that default will stop tracking the warning pref.
  } else {
    Assert.equal(0, protoInfo.possibleApplicationHandlers.length);
    Assert.ok(protoInfo.alwaysAskBeforeHandling);
  }

  if (haveDefaultHandlersVersion) {
    // Now set the value stored in RDF to true, and the pref to false, to make
    // sure we still get the right value. (Basically, same thing as above but
    // with the values reversed.)
    prefSvc.setBoolPref(kExternalWarningPrefPrefix + "mailto", false);
    protoInfo.alwaysAskBeforeHandling = true;
    handlerSvc.store(protoInfo);
    protoInfo = protoSvc.getProtocolHandlerInfo("mailto");
    Assert.equal(2, protoInfo.possibleApplicationHandlers.length);
    Assert.ok(protoInfo.alwaysAskBeforeHandling);
  }

  //* *************************************************************************//
  // Test Round-Trip Data Integrity

  // Test round-trip data integrity by setting the properties of the handler
  // info object to different values, telling the handler service to store the
  // object, and then retrieving a new info object for the same type and making
  // sure its properties are identical.

  handlerInfo.preferredAction = Ci.nsIHandlerInfo.useHelperApp;
  handlerInfo.preferredApplicationHandler = localHandler;
  handlerInfo.alwaysAskBeforeHandling = false;

  handlerSvc.store(handlerInfo);

  handlerInfo = mimeSvc.getFromTypeAndExtension("nonexistent/type", null);

  Assert.equal(handlerInfo.preferredAction, Ci.nsIHandlerInfo.useHelperApp);

  Assert.notEqual(handlerInfo.preferredApplicationHandler, null);
  var preferredHandler = handlerInfo.preferredApplicationHandler;
  Assert.equal(typeof preferredHandler, "object");
  Assert.equal(preferredHandler.name, "Local Handler");
  Assert.ok(preferredHandler instanceof Ci.nsILocalHandlerApp);
  preferredHandler.QueryInterface(Ci.nsILocalHandlerApp);
  Assert.equal(preferredHandler.executable.path, localHandler.executable.path);

  Assert.ok(!handlerInfo.alwaysAskBeforeHandling);

  // Make sure the handler service's enumerate method lists all known handlers.
  var handlerInfo2 = mimeSvc.getFromTypeAndExtension("nonexistent/type2", null);
  handlerSvc.store(handlerInfo2);
  var handlerTypes = ["nonexistent/type", "nonexistent/type2"];
  if (haveDefaultHandlersVersion) {
    handlerTypes.push("mailto");
    handlerTypes.push("irc");
    handlerTypes.push("ircs");
  }
  for (let handler of handlerSvc.enumerate()) {
    Assert.notEqual(handlerTypes.indexOf(handler.type), -1);
    handlerTypes.splice(handlerTypes.indexOf(handler.type), 1);
  }
  Assert.equal(handlerTypes.length, 0);
  // Make sure the handler service's remove method removes a handler record.
  handlerSvc.remove(handlerInfo2);
  let handlers = handlerSvc.enumerate();
  while (handlers.hasMoreElements()) {
    Assert.notEqual(
      handlers.getNext().QueryInterface(Ci.nsIHandlerInfo).type,
      handlerInfo2.type
    );
  }

  // Make sure we can store and retrieve a handler info object with no preferred
  // handler.
  var noPreferredHandlerInfo = mimeSvc.getFromTypeAndExtension(
    "nonexistent/no-preferred-handler",
    null
  );
  handlerSvc.store(noPreferredHandlerInfo);
  noPreferredHandlerInfo = mimeSvc.getFromTypeAndExtension(
    "nonexistent/no-preferred-handler",
    null
  );
  Assert.equal(noPreferredHandlerInfo.preferredApplicationHandler, null);

  // Make sure that the handler service removes an existing handler record
  // if we store a handler info object with no preferred handler.
  var removePreferredHandlerInfo = mimeSvc.getFromTypeAndExtension(
    "nonexistent/rem-preferred-handler",
    null
  );
  removePreferredHandlerInfo.preferredApplicationHandler = localHandler;
  handlerSvc.store(removePreferredHandlerInfo);
  removePreferredHandlerInfo = mimeSvc.getFromTypeAndExtension(
    "nonexistent/rem-preferred-handler",
    null
  );
  removePreferredHandlerInfo.preferredApplicationHandler = null;
  handlerSvc.store(removePreferredHandlerInfo);
  removePreferredHandlerInfo = mimeSvc.getFromTypeAndExtension(
    "nonexistent/rem-preferred-handler",
    null
  );
  Assert.equal(removePreferredHandlerInfo.preferredApplicationHandler, null);

  // Make sure we can store and retrieve a handler info object with possible
  // handlers.  We test both adding and removing handlers.

  // Get a handler info and make sure it has no possible handlers.
  var possibleHandlersInfo = mimeSvc.getFromTypeAndExtension(
    "nonexistent/possible-handlers",
    null
  );
  Assert.equal(possibleHandlersInfo.possibleApplicationHandlers.length, 0);

  // Store and re-retrieve the handler and make sure it still has no possible
  // handlers.
  handlerSvc.store(possibleHandlersInfo);
  possibleHandlersInfo = mimeSvc.getFromTypeAndExtension(
    "nonexistent/possible-handlers",
    null
  );
  Assert.equal(possibleHandlersInfo.possibleApplicationHandlers.length, 0);

  // Add two handlers, store the object, re-retrieve it, and make sure it has
  // two handlers.
  possibleHandlersInfo.possibleApplicationHandlers.appendElement(localHandler);
  possibleHandlersInfo.possibleApplicationHandlers.appendElement(webHandler);
  handlerSvc.store(possibleHandlersInfo);
  possibleHandlersInfo = mimeSvc.getFromTypeAndExtension(
    "nonexistent/possible-handlers",
    null
  );
  Assert.equal(possibleHandlersInfo.possibleApplicationHandlers.length, 2);

  // Figure out which is the local and which is the web handler and the index
  // in the array of the local handler, which is the one we're going to remove
  // to test removal of a handler.
  var handler1 = possibleHandlersInfo.possibleApplicationHandlers.queryElementAt(
    0,
    Ci.nsIHandlerApp
  );
  var handler2 = possibleHandlersInfo.possibleApplicationHandlers.queryElementAt(
    1,
    Ci.nsIHandlerApp
  );
  var localPossibleHandler, webPossibleHandler, localIndex;
  if (handler1 instanceof Ci.nsILocalHandlerApp) {
    [localPossibleHandler, webPossibleHandler, localIndex] = [
      handler1,
      handler2,
      0,
    ];
  } else {
    [localPossibleHandler, webPossibleHandler, localIndex] = [
      handler2,
      handler1,
      1,
    ];
  }
  localPossibleHandler.QueryInterface(Ci.nsILocalHandlerApp);
  webPossibleHandler.QueryInterface(Ci.nsIWebHandlerApp);

  // Make sure the two handlers are the ones we stored.
  Assert.equal(localPossibleHandler.name, localHandler.name);
  Assert.ok(localPossibleHandler.equals(localHandler));
  Assert.equal(webPossibleHandler.name, webHandler.name);
  Assert.ok(webPossibleHandler.equals(webHandler));

  // Remove a handler, store the object, re-retrieve it, and make sure
  // it only has one handler.
  possibleHandlersInfo.possibleApplicationHandlers.removeElementAt(localIndex);
  handlerSvc.store(possibleHandlersInfo);
  possibleHandlersInfo = mimeSvc.getFromTypeAndExtension(
    "nonexistent/possible-handlers",
    null
  );
  Assert.equal(possibleHandlersInfo.possibleApplicationHandlers.length, 1);

  // Make sure the handler is the one we didn't remove.
  webPossibleHandler = possibleHandlersInfo.possibleApplicationHandlers.queryElementAt(
    0,
    Ci.nsIWebHandlerApp
  );
  Assert.equal(webPossibleHandler.name, webHandler.name);
  Assert.ok(webPossibleHandler.equals(webHandler));

  // ////////////////////////////////////////////////////
  // handler info command line parameters and equality
  var localApp = Cc[
    "@mozilla.org/uriloader/local-handler-app;1"
  ].createInstance(Ci.nsILocalHandlerApp);
  var handlerApp = localApp.QueryInterface(Ci.nsIHandlerApp);

  Assert.ok(handlerApp.equals(localApp));

  localApp.executable = executable;

  Assert.equal(0, localApp.parameterCount);
  localApp.appendParameter("-test1");
  Assert.equal(1, localApp.parameterCount);
  localApp.appendParameter("-test2");
  Assert.equal(2, localApp.parameterCount);
  Assert.ok(localApp.parameterExists("-test1"));
  Assert.ok(localApp.parameterExists("-test2"));
  Assert.ok(!localApp.parameterExists("-false"));
  localApp.clearParameters();
  Assert.equal(0, localApp.parameterCount);

  var localApp2 = Cc[
    "@mozilla.org/uriloader/local-handler-app;1"
  ].createInstance(Ci.nsILocalHandlerApp);

  localApp2.executable = executable;

  localApp.clearParameters();
  Assert.ok(localApp.equals(localApp2));

  // equal:
  // cut -d 1 -f 2
  // cut -d 1 -f 2

  localApp.appendParameter("-test1");
  localApp.appendParameter("-test2");
  localApp.appendParameter("-test3");
  localApp2.appendParameter("-test1");
  localApp2.appendParameter("-test2");
  localApp2.appendParameter("-test3");
  Assert.ok(localApp.equals(localApp2));

  // not equal:
  // cut -d 1 -f 2
  // cut -f 1 -d 2

  localApp.clearParameters();
  localApp2.clearParameters();

  localApp.appendParameter("-test1");
  localApp.appendParameter("-test2");
  localApp.appendParameter("-test3");
  localApp2.appendParameter("-test2");
  localApp2.appendParameter("-test1");
  localApp2.appendParameter("-test3");
  Assert.ok(!localApp2.equals(localApp));

  var str;
  str = localApp.getParameter(0);
  Assert.equal(str, "-test1");
  str = localApp.getParameter(1);
  Assert.equal(str, "-test2");
  str = localApp.getParameter(2);
  Assert.equal(str, "-test3");

  // FIXME: test round trip integrity for a protocol.
  // FIXME: test round trip integrity for a handler info with a web handler.

  //* *************************************************************************//
  // getTypeFromExtension tests

  // test nonexistent extension
  var lolType = handlerSvc.getTypeFromExtension("lolcat");
  Assert.equal(lolType, "");

  // add a handler for the extension
  var lolHandler = mimeSvc.getFromTypeAndExtension("application/lolcat", null);

  Assert.ok(!lolHandler.extensionExists("lolcat"));
  lolHandler.preferredAction = Ci.nsIHandlerInfo.useHelperApp;
  lolHandler.preferredApplicationHandler = localHandler;
  lolHandler.alwaysAskBeforeHandling = false;
  lolHandler.appendExtension("lolcat");

  // store the handler
  Assert.ok(!handlerSvc.exists(lolHandler));
  handlerSvc.store(lolHandler);
  Assert.ok(handlerSvc.exists(lolHandler));

  // test now-existent extension
  lolType = handlerSvc.getTypeFromExtension("lolcat");
  Assert.equal(lolType, "application/lolcat");

  // test mailcap entries with needsterminal are ignored on non-Windows non-Mac.
  if (mozinfo.os != "win" && mozinfo.os != "mac") {
    env.set("PERSONAL_MAILCAP", do_get_file("mailcap").path);
    handlerInfo = mimeSvc.getFromTypeAndExtension("text/plain", null);
    Assert.equal(
      handlerInfo.preferredAction,
      Ci.nsIHandlerInfo.useSystemDefault
    );
    Assert.equal(handlerInfo.defaultDescription, "sed");
  }
}