1 /* -*- indent-tabs-mode: nil; js-indent-level: 2 -*-
2 * vim: sw=2 ts=2 sts=2 tw=78 expandtab :
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 var manifests = [do_get_file("data/test_no_remote_registration.manifest")];
8 registerManifests(manifests);
10 function ProtocolHandler(aScheme) {
11 this.scheme = aScheme;
14 ProtocolHandler.prototype = {
15 allowPort: () => false,
17 throw Components.Exception("", Cr.NS_ERROR_NOT_IMPLEMENTED);
19 QueryInterface: ChromeUtils.generateQI(["nsIProtocolHandler"]),
23 // It doesn't matter if it has this flag - the only flag we accept is
24 // URI_IS_LOCAL_RESOURCE.
26 scheme: "moz-protocol-ui-resource",
27 flags: Ci.nsIProtocolHandler.URI_IS_UI_RESOURCE,
28 shouldRegister: false,
30 // It doesn't matter if it has this flag - the only flag we accept is
31 // URI_IS_LOCAL_RESOURCE.
33 scheme: "moz-protocol-local-file",
34 flags: Ci.nsIProtocolHandler.URI_IS_LOCAL_FILE,
35 shouldRegister: false,
37 // This clearly is non-local
39 scheme: "moz-protocol-loadable-by-anyone",
40 flags: Ci.nsIProtocolHandler.URI_LOADABLE_BY_ANYONE,
41 shouldRegister: false,
43 // This should always be last (unless we add more flags that are OK)
45 scheme: "moz-protocol-local-resource",
46 flags: Ci.nsIProtocolHandler.URI_IS_LOCAL_RESOURCE,
51 const { newAppInfo } = ChromeUtils.importESModule(
52 "resource://testing-common/AppInfo.sys.mjs"
54 let XULAppInfo = newAppInfo({
56 ID: "{39885e5f-f6b4-4e2a-87e5-6259ecf79011}",
58 platformVersion: "1.9",
61 const uuidGenerator = Services.uuid;
63 let XULAppInfoFactory = {
64 // These two are used when we register all our factories (and unregister)
65 CID: uuidGenerator.generateUUID(),
67 contractID: "@mozilla.org/xre/app-info;1",
69 return XULAppInfo.QueryInterface(iid);
73 for (let protocol of testProtocols) {
74 Services.io.registerProtocolHandler(
76 new ProtocolHandler(protocol.scheme),
82 let registrar = Components.manager.QueryInterface(Ci.nsIComponentRegistrar);
84 // Register the XULAppInfoFactory
85 // Make sure the class ID has not already been registered
86 let old_factory = { CID: "", factory: null };
87 if (!registrar.isCIDRegistered(XULAppInfoFactory.CID)) {
88 // Check to see if a contract was already registered and
89 // register it if it is not. Otherwise, store the previous one
90 // to be restored later and register the new one.
91 if (registrar.isContractIDRegistered(XULAppInfoFactory.contractID)) {
93 XULAppInfoFactory.scheme +
94 " is already registered. Storing currently registered object for restoration later."
96 old_factory.CID = registrar.contractIDToCID(XULAppInfoFactory.contractID);
97 old_factory.factory = Components.manager.getClassObject(
98 Cc[XULAppInfoFactory.contractID],
103 XULAppInfoFactory.scheme + " has never been registered. Registering..."
107 registrar.registerFactory(
108 XULAppInfoFactory.CID,
109 "test-" + XULAppInfoFactory.scheme,
110 XULAppInfoFactory.contractID,
114 do_throw("CID " + XULAppInfoFactory.CID + " has already been registered!");
117 // Check for new chrome
118 let cr = Cc["@mozilla.org/chrome/chrome-registry;1"].getService(
121 cr.checkForNewChrome();
123 // See if our various things were able to register
124 let registrationTypes = ["content", "locale", "skin", "override", "resource"];
125 for (let i = 0; i < testProtocols.length; i++) {
126 let protocol = testProtocols[i];
127 for (let j = 0; j < registrationTypes.length; j++) {
128 let type = registrationTypes[j];
130 "Testing protocol '" + protocol.scheme + "' with type '" + type + "'\n"
132 let expectedURI = protocol.scheme + "://foo/";
133 let sourceURI = "chrome://" + protocol.scheme + "/" + type + "/";
136 expectedURI += protocol.scheme + ".xul";
139 expectedURI += protocol.scheme + ".dtd";
142 expectedURI += protocol.scheme + ".css";
146 "chrome://good-package/content/override-" +
151 sourceURI = "resource://" + protocol.scheme + "/";
155 sourceURI = Services.io.newURI(sourceURI);
157 if (type == "resource") {
158 // resources go about a slightly different way than everything else
159 let rph = Services.io
160 .getProtocolHandler("resource")
161 .QueryInterface(Ci.nsIResProtocolHandler);
162 // this throws for packages that are not registered
163 uri = rph.resolveURI(sourceURI);
165 // this throws for packages that are not registered
166 uri = cr.convertChromeURL(sourceURI).spec;
169 if (protocol.shouldRegister) {
170 Assert.equal(expectedURI, uri);
172 // Overrides will not throw, so we'll get to here. We want to make
173 // sure that the two strings are not the same in this situation.
174 Assert.notEqual(expectedURI, uri);
177 if (protocol.shouldRegister) {
180 "Should have registered our URI for protocol " + protocol.scheme
187 // Unregister our protocol handlers so we do not leak
188 for (let protocol of testProtocols) {
189 Services.io.unregisterProtocolHandler(protocol.scheme);
192 // Unregister XULAppInfoFactory
193 registrar.unregisterFactory(XULAppInfoFactory.CID, XULAppInfoFactory);
194 if (old_factory.factory != null) {
195 registrar.registerFactory(
198 XULAppInfoFactory.contractID,