1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * vim: sw=2 ts=2 sts=2 tw=78 expandtab :
3 * ***** BEGIN LICENSE BLOCK *****
4 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 1.1 (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
9 * http://www.mozilla.org/MPL/
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
16 * The Original Code is Chrome Registration Test Code.
18 * The Initial Developer of the Original Code is
19 * Mozilla Corporation.
20 * Portions created by the Initial Developer are Copyright (C) 2008
21 * the Initial Developer. All Rights Reserved.
24 * Shawn Wilsher <me@shawnwilsher.com> (Original Author)
26 * Alternatively, the contents of this file may be used under the terms of
27 * either the GNU General Public License Version 2 or later (the "GPL"), or
28 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29 * in which case the provisions of the GPL or the LGPL are applicable instead
30 * of those above. If you wish to allow use of your version of this file only
31 * under the terms of either the GPL or the LGPL, and not to allow others to
32 * use your version of this file under the terms of the MPL, indicate your
33 * decision by deleting the provisions above and replace them with the notice
34 * and other provisions required by the GPL or the LGPL. If you do not delete
35 * the provisions above, a recipient may use your version of this file under
36 * the terms of any one of the MPL, the GPL or the LGPL.
38 * ***** END LICENSE BLOCK ***** */
41 do_get_file("chrome/test/unit/data/test_no_remote_registration.manifest"),
43 registerManifests(manifests);
45 Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
50 ID: "{39885e5f-f6b4-4e2a-87e5-6259ecf79011}",
52 appBuildID: "2007010101",
53 platformVersion: "1.9",
54 platformBuildID: "2007010101",
56 logConsoleErrors: true,
58 XPCOMABI: "noarch-spidermonkey",
59 QueryInterface: XPCOMUtils.generateQI([
65 let XULAppInfoFactory = {
66 // These two are used when we register all our factories (and unregister)
69 contractID: XULAPPINFO_CONTRACTID,
70 createInstance: function (outer, iid) {
72 throw Cr.NS_ERROR_NO_AGGREGATION;
73 return XULAppInfo.QueryInterface(iid);
77 function ProtocolHandler(aScheme, aFlags)
79 this.scheme = aScheme;
80 this.protocolFlags = aFlags;
81 this.contractID = "@mozilla.org/network/protocol;1?name=" + aScheme;
84 ProtocolHandler.prototype =
87 allowPort: function() false,
88 newURI: function(aSpec, aCharset, aBaseURI)
90 let uri = Cc["@mozilla.org/network/standard-url;1"].
91 createInstance(Ci.nsIURI);
94 // We got a partial uri, so let's resolve it with the base one
95 uri.spec = aBaseURI.resolve(aSpec);
99 newChannel: function() { throw Cr.NS_ERROR_NOT_IMPLEMENTED },
100 QueryInterface: XPCOMUtils.generateQI([
101 Ci.nsIProtocolHandler
105 let testProtocols = [
106 // It doesn't matter if it has this flag - the only flag we accept is
107 // URI_IS_LOCAL_RESOURCE.
108 {scheme: "moz-protocol-ui-resource",
109 flags: Ci.nsIProtocolHandler.URI_IS_UI_RESOURCE,
110 CID: Components.ID("{d6dedc93-558f-44fe-90f4-3b4bba8a0b14}"),
111 shouldRegister: false
113 // It doesn't matter if it has this flag - the only flag we accept is
114 // URI_IS_LOCAL_RESOURCE.
115 {scheme: "moz-protocol-local-file",
116 flags: Ci.nsIProtocolHandler.URI_IS_LOCAL_FILE,
117 CID: Components.ID("{ee30d594-0a2d-4f47-89cc-d4cde320ab69}"),
118 shouldRegister: false
120 // This clearly is non-local
121 {scheme: "moz-protocol-loadable-by-anyone",
122 flags: Ci.nsIProtocolHandler.URI_LOADABLE_BY_ANYONE,
123 CID: Components.ID("{c3735f23-3b0c-4a33-bfa0-79436dcd40b2}"),
124 shouldRegister: false
126 // This should always be last (unless we add more flags that are OK)
127 {scheme: "moz-protocol-local-resource",
128 flags: Ci.nsIProtocolHandler.URI_IS_LOCAL_RESOURCE,
129 CID: Components.ID("{b79e977c-f840-469a-b413-0125cc1b62a5}"),
137 for (let i = 0; i < testProtocols.length; i++) {
139 scheme: testProtocols[i].scheme,
140 flags: testProtocols[i].flags,
141 CID: testProtocols[i].CID,
142 contractID: "@mozilla.org/network/protocol;1?name=" + testProtocols[i].scheme,
143 createInstance: function(aOuter, aIID)
146 throw Cr.NS_ERROR_NO_AGGREGATION;
147 let handler = new ProtocolHandler(this.scheme, this.flags, this.CID);
148 return handler.QueryInterface(aIID);
152 // Add our XULAppInfo factory
153 factories.push(XULAppInfoFactory);
155 // Register our factories
156 for (let i = 0; i < factories.length; i++) {
157 let factory = factories[i];
158 Components.manager.QueryInterface(Ci.nsIComponentRegistrar)
159 .registerFactory(factory.CID, "test-" + factory.scheme,
160 factory.contractID, factory);
163 // Check for new chrome
164 let cr = Cc["@mozilla.org/chrome/chrome-registry;1"].
165 getService(Ci.nsIChromeRegistry);
166 cr.checkForNewChrome();
168 // See if our various things were able to register
169 let registrationTypes = [
176 for (let i = 0; i < testProtocols.length; i++) {
177 let protocol = testProtocols[i];
178 for (let j = 0; j < registrationTypes.length; j++) {
179 let type = registrationTypes[j];
180 dump("Testing protocol '" + protocol.scheme + "' with type '" + type +
182 let expectedURI = protocol.scheme + "://foo/";
183 let sourceURI = "chrome://" + protocol.scheme + "/" + type + "/";
186 expectedURI += protocol.scheme + ".xul";
189 expectedURI += protocol.scheme + ".dtd";
192 expectedURI += protocol.scheme + ".css";
195 sourceURI = "chrome://good-package/content/override-" +
196 protocol.scheme + ".xul";
199 sourceURI = "resource://" + protocol.scheme + "/";
203 let ios = Cc["@mozilla.org/network/io-service;1"].
204 getService(Ci.nsIIOService);
205 sourceURI = ios.newURI(sourceURI, null, null);
207 if (type == "resource") {
208 // resources go about a slightly different way than everything else
209 let rph = ios.getProtocolHandler("resource").
210 QueryInterface(Ci.nsIResProtocolHandler);
211 // this throws for packages that are not registered
212 uri = rph.resolveURI(sourceURI);
215 // this throws for packages that are not registered
216 uri = cr.convertChromeURL(sourceURI).spec;
219 if (protocol.shouldRegister) {
220 do_check_eq(expectedURI, uri);
223 // Overrides will not throw, so we'll get to here. We want to make
224 // sure that the two strings are not the same in this situation.
225 do_check_neq(expectedURI, uri);
229 if (protocol.shouldRegister) {
231 do_throw("Should have registered our URI for protocol " +
238 // Unregister our factories so we do not leak
239 for (let i = 0; i < factories.length; i++) {
240 let factory = factories[i];
241 Components.manager.QueryInterface(Ci.nsIComponentRegistrar)
242 .unregisterFactory(factory.CID, factory);