chrome/test/unit/test_no_remote_registration.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/chrome/test/unit/test_no_remote_registration.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,211 @@
     1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
     1.5 + * vim: sw=2 ts=2 sts=2 tw=78 expandtab :
     1.6 + * This Source Code Form is subject to the terms of the Mozilla Public
     1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.9 +
    1.10 +let manifests = [
    1.11 +  do_get_file("data/test_no_remote_registration.manifest"),
    1.12 +];
    1.13 +registerManifests(manifests);
    1.14 +
    1.15 +Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
    1.16 +
    1.17 +let XULAppInfo = {
    1.18 +  vendor: "Mozilla",
    1.19 +  name: "XPCShell",
    1.20 +  ID: "{39885e5f-f6b4-4e2a-87e5-6259ecf79011}",
    1.21 +  version: "5",
    1.22 +  appBuildID: "2007010101",
    1.23 +  platformVersion: "1.9",
    1.24 +  platformBuildID: "2007010101",
    1.25 +  inSafeMode: false,
    1.26 +  logConsoleErrors: true,
    1.27 +  OS: "XPCShell",
    1.28 +  XPCOMABI: "noarch-spidermonkey",
    1.29 +  QueryInterface: XPCOMUtils.generateQI([
    1.30 +    Ci.nsIXULAppInfo,
    1.31 +    Ci.nsIXULRuntime,
    1.32 +  ])
    1.33 +};
    1.34 +
    1.35 +let XULAppInfoFactory = {
    1.36 +  // These two are used when we register all our factories (and unregister)
    1.37 +  CID: XULAPPINFO_CID,
    1.38 +  scheme: "XULAppInfo",
    1.39 +  contractID: XULAPPINFO_CONTRACTID,
    1.40 +  createInstance: function (outer, iid) {
    1.41 +    if (outer != null)
    1.42 +      throw Cr.NS_ERROR_NO_AGGREGATION;
    1.43 +    return XULAppInfo.QueryInterface(iid);
    1.44 +  }
    1.45 +};
    1.46 +
    1.47 +function ProtocolHandler(aScheme, aFlags)
    1.48 +{
    1.49 +  this.scheme = aScheme;
    1.50 +  this.protocolFlags = aFlags;
    1.51 +  this.contractID = "@mozilla.org/network/protocol;1?name=" + aScheme;
    1.52 +}
    1.53 +
    1.54 +ProtocolHandler.prototype =
    1.55 +{
    1.56 +  defaultPort: -1,
    1.57 +  allowPort: function() false,
    1.58 +  newURI: function(aSpec, aCharset, aBaseURI)
    1.59 +  {
    1.60 +    let uri = Cc["@mozilla.org/network/standard-url;1"].
    1.61 +              createInstance(Ci.nsIURI);
    1.62 +    uri.spec = aSpec;
    1.63 +    if (!uri.scheme) {
    1.64 +      // We got a partial uri, so let's resolve it with the base one
    1.65 +      uri.spec = aBaseURI.resolve(aSpec);
    1.66 +    }
    1.67 +    return uri;
    1.68 +  },
    1.69 +  newChannel: function() { throw Cr.NS_ERROR_NOT_IMPLEMENTED },
    1.70 +  QueryInterface: XPCOMUtils.generateQI([
    1.71 +    Ci.nsIProtocolHandler
    1.72 +  ])
    1.73 +};
    1.74 +
    1.75 +let testProtocols = [
    1.76 +  // It doesn't matter if it has this flag - the only flag we accept is
    1.77 +  // URI_IS_LOCAL_RESOURCE.
    1.78 +  {scheme: "moz-protocol-ui-resource",
    1.79 +   flags: Ci.nsIProtocolHandler.URI_IS_UI_RESOURCE,
    1.80 +   CID: Components.ID("{d6dedc93-558f-44fe-90f4-3b4bba8a0b14}"),
    1.81 +   shouldRegister: false
    1.82 +  },
    1.83 +  // It doesn't matter if it has this flag - the only flag we accept is
    1.84 +  // URI_IS_LOCAL_RESOURCE.
    1.85 +  {scheme: "moz-protocol-local-file",
    1.86 +   flags: Ci.nsIProtocolHandler.URI_IS_LOCAL_FILE,
    1.87 +   CID: Components.ID("{ee30d594-0a2d-4f47-89cc-d4cde320ab69}"),
    1.88 +   shouldRegister: false
    1.89 +  },
    1.90 +  // This clearly is non-local
    1.91 +  {scheme: "moz-protocol-loadable-by-anyone",
    1.92 +   flags: Ci.nsIProtocolHandler.URI_LOADABLE_BY_ANYONE,
    1.93 +   CID: Components.ID("{c3735f23-3b0c-4a33-bfa0-79436dcd40b2}"),
    1.94 +   shouldRegister: false
    1.95 +  },
    1.96 +  // This should always be last (unless we add more flags that are OK)
    1.97 +  {scheme: "moz-protocol-local-resource",
    1.98 +   flags: Ci.nsIProtocolHandler.URI_IS_LOCAL_RESOURCE,
    1.99 +   CID: Components.ID("{b79e977c-f840-469a-b413-0125cc1b62a5}"),
   1.100 +   shouldRegister: true
   1.101 +  },
   1.102 +];
   1.103 +function run_test()
   1.104 +{
   1.105 +  // Create factories
   1.106 +  let factories = [];
   1.107 +  for (let i = 0; i < testProtocols.length; i++) {
   1.108 +    factories[i] = {
   1.109 +      scheme: testProtocols[i].scheme,
   1.110 +      flags: testProtocols[i].flags,
   1.111 +      CID: testProtocols[i].CID,
   1.112 +      contractID: "@mozilla.org/network/protocol;1?name=" + testProtocols[i].scheme,
   1.113 +      createInstance: function(aOuter, aIID)
   1.114 +      {
   1.115 +        if (aOuter != null)
   1.116 +          throw Cr.NS_ERROR_NO_AGGREGATION;
   1.117 +        let handler = new ProtocolHandler(this.scheme, this.flags, this.CID);
   1.118 +        return handler.QueryInterface(aIID);
   1.119 +      }
   1.120 +    };
   1.121 +  }
   1.122 +  // Add our XULAppInfo factory
   1.123 +  factories.push(XULAppInfoFactory);
   1.124 +
   1.125 +  // Register our factories
   1.126 +  for (let i = 0; i < factories.length; i++) {
   1.127 +    let factory = factories[i];
   1.128 +    Components.manager.QueryInterface(Ci.nsIComponentRegistrar)
   1.129 +              .registerFactory(factory.CID, "test-" + factory.scheme,
   1.130 +                               factory.contractID, factory);
   1.131 +  }
   1.132 +
   1.133 +  // Check for new chrome
   1.134 +  let cr = Cc["@mozilla.org/chrome/chrome-registry;1"].
   1.135 +           getService(Ci.nsIChromeRegistry);
   1.136 +  cr.checkForNewChrome();
   1.137 +
   1.138 +  // See if our various things were able to register
   1.139 +  let registrationTypes = [
   1.140 +    "content",
   1.141 +    "locale",
   1.142 +    "skin",
   1.143 +    "override",
   1.144 +    "resource",
   1.145 +  ];
   1.146 +  for (let i = 0; i < testProtocols.length; i++) {
   1.147 +    let protocol = testProtocols[i];
   1.148 +    for (let j = 0; j < registrationTypes.length; j++) {
   1.149 +      let type = registrationTypes[j];
   1.150 +      dump("Testing protocol '" + protocol.scheme + "' with type '" + type +
   1.151 +           "'\n");
   1.152 +      let expectedURI = protocol.scheme + "://foo/";
   1.153 +      let sourceURI = "chrome://" + protocol.scheme + "/" + type + "/";
   1.154 +      switch (type) {
   1.155 +        case "content":
   1.156 +          expectedURI += protocol.scheme + ".xul";
   1.157 +          break;
   1.158 +        case "locale":
   1.159 +          expectedURI += protocol.scheme + ".dtd";
   1.160 +          break;
   1.161 +        case "skin":
   1.162 +          expectedURI += protocol.scheme + ".css";
   1.163 +          break;
   1.164 +        case "override":
   1.165 +          sourceURI = "chrome://good-package/content/override-" +
   1.166 +                      protocol.scheme + ".xul";
   1.167 +          break;
   1.168 +        case "resource":
   1.169 +          sourceURI = "resource://" + protocol.scheme + "/";
   1.170 +          break;
   1.171 +      };
   1.172 +      try {
   1.173 +        let ios = Cc["@mozilla.org/network/io-service;1"].
   1.174 +                  getService(Ci.nsIIOService);
   1.175 +        sourceURI = ios.newURI(sourceURI, null, null);
   1.176 +        let uri;
   1.177 +        if (type == "resource") {
   1.178 +          // resources go about a slightly different way than everything else
   1.179 +          let rph = ios.getProtocolHandler("resource").
   1.180 +                    QueryInterface(Ci.nsIResProtocolHandler);
   1.181 +          // this throws for packages that are not registered
   1.182 +          uri = rph.resolveURI(sourceURI);
   1.183 +        }
   1.184 +        else {
   1.185 +          // this throws for packages that are not registered
   1.186 +          uri = cr.convertChromeURL(sourceURI).spec;
   1.187 +        }
   1.188 +
   1.189 +        if (protocol.shouldRegister) {
   1.190 +          do_check_eq(expectedURI, uri);
   1.191 +        }
   1.192 +        else {
   1.193 +          // Overrides will not throw, so we'll get to here.  We want to make
   1.194 +          // sure that the two strings are not the same in this situation.
   1.195 +          do_check_neq(expectedURI, uri);
   1.196 +        }
   1.197 +      }
   1.198 +      catch (e) {
   1.199 +        if (protocol.shouldRegister) {
   1.200 +          dump(e + "\n");
   1.201 +          do_throw("Should have registered our URI for protocol " +
   1.202 +                   protocol.scheme);
   1.203 +        }
   1.204 +      }
   1.205 +    }
   1.206 +  }
   1.207 +
   1.208 +  // Unregister our factories so we do not leak
   1.209 +  for (let i = 0; i < factories.length; i++) {
   1.210 +    let factory = factories[i];
   1.211 +    Components.manager.QueryInterface(Ci.nsIComponentRegistrar)
   1.212 +              .unregisterFactory(factory.CID, factory);
   1.213 +  }
   1.214 +}

mercurial