Revision control
Copy as Markdown
Other Tools
/* Any copyright is dedicated to the Public Domain.
"use strict";
const gMIMEService = Cc["@mozilla.org/mime;1"].getService(Ci.nsIMIMEService);
const gExternalProtocolService = Cc[
"@mozilla.org/uriloader/external-protocol-service;1"
].getService(Ci.nsIExternalProtocolService);
const gHandlerService = Cc[
"@mozilla.org/uriloader/handler-service;1"
].getService(Ci.nsIHandlerService);
// This seems odd, but for test purposes, this just has to be a file that we know exists,
// and by using this file, we don't have to worry about different platforms.
const exeFile = Services.dirsvc.get("XREExeF", Ci.nsIFile);
add_task(async function test_valid_handlers() {
await setupPolicyEngineWithJson({
policies: {
Handlers: {
mimeTypes: {
"application/marimba": {
action: "useHelperApp",
ask: true,
handlers: [
{
name: "Launch",
path: exeFile.path,
},
],
},
},
schemes: {
fake_scheme: {
action: "useHelperApp",
ask: false,
handlers: [
{
name: "Name",
},
],
},
},
extensions: {
txt: {
action: "saveToDisk",
ask: false,
},
},
},
},
});
let handlerInfo = gMIMEService.getFromTypeAndExtension(
"application/marimba",
""
);
is(handlerInfo.preferredAction, handlerInfo.useHelperApp);
is(handlerInfo.alwaysAskBeforeHandling, true);
is(handlerInfo.preferredApplicationHandler.name, "Launch");
is(handlerInfo.preferredApplicationHandler.executable.path, exeFile.path);
handlerInfo.preferredApplicationHandler = null;
gHandlerService.store(handlerInfo);
handlerInfo = handlerInfo = gMIMEService.getFromTypeAndExtension(
"application/marimba",
""
);
is(handlerInfo.preferredApplicationHandler, null);
gHandlerService.remove(handlerInfo);
handlerInfo = gExternalProtocolService.getProtocolHandlerInfo("fake_scheme");
is(handlerInfo.preferredAction, handlerInfo.useHelperApp);
is(handlerInfo.alwaysAskBeforeHandling, false);
is(handlerInfo.preferredApplicationHandler.name, "Name");
is(
handlerInfo.preferredApplicationHandler.uriTemplate,
);
handlerInfo.preferredApplicationHandler = null;
gHandlerService.store(handlerInfo);
handlerInfo = gExternalProtocolService.getProtocolHandlerInfo("fake_scheme");
is(handlerInfo.preferredApplicationHandler, null);
gHandlerService.remove(handlerInfo);
handlerInfo = gMIMEService.getFromTypeAndExtension("", "txt");
is(handlerInfo.preferredAction, handlerInfo.saveToDisk);
is(handlerInfo.alwaysAskBeforeHandling, false);
handlerInfo.preferredApplicationHandler = null;
gHandlerService.store(handlerInfo);
handlerInfo = gMIMEService.getFromTypeAndExtension("", "txt");
is(handlerInfo.preferredApplicationHandler, null);
gHandlerService.remove(handlerInfo);
});
add_task(async function test_no_handler() {
await setupPolicyEngineWithJson({
policies: {
Handlers: {
schemes: {
no_handler: {
action: "useHelperApp",
},
},
},
},
});
const handlerInfo =
gExternalProtocolService.getProtocolHandlerInfo("no_handler");
is(handlerInfo.preferredAction, handlerInfo.alwaysAsk);
is(handlerInfo.alwaysAskBeforeHandling, true);
is(handlerInfo.preferredApplicationHandler, null);
gHandlerService.remove(handlerInfo);
});
add_task(async function test_bad_web_handler1() {
await setupPolicyEngineWithJson({
policies: {
Handlers: {
schemes: {
bas_web_handler1: {
action: "useHelperApp",
handlers: [
{
name: "Name",
},
],
},
},
},
},
});
const handlerInfo =
gExternalProtocolService.getProtocolHandlerInfo("bad_web_handler1");
is(handlerInfo.preferredAction, handlerInfo.alwaysAsk);
is(handlerInfo.alwaysAskBeforeHandling, true);
is(handlerInfo.preferredApplicationHandler, null);
gHandlerService.remove(handlerInfo);
});
add_task(async function test_bad_web_handler2() {
await setupPolicyEngineWithJson({
policies: {
Handlers: {
schemes: {
bas_web_handler1: {
action: "useHelperApp",
handlers: [
{
name: "Name",
},
],
},
},
},
},
});
const handlerInfo =
gExternalProtocolService.getProtocolHandlerInfo("bad_web_handler1");
is(handlerInfo.preferredAction, handlerInfo.alwaysAsk);
is(handlerInfo.alwaysAskBeforeHandling, true);
is(handlerInfo.preferredApplicationHandler, null);
gHandlerService.remove(handlerInfo);
});