xpcom/tests/TestRegistrationOrder.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/xpcom/tests/TestRegistrationOrder.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,194 @@
     1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* vim:set ts=2 sw=2 sts=2 et: */
     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 +#include "TestHarness.h"
    1.11 +#include "nsIFile.h"
    1.12 +#include "nsIDirectoryService.h"
    1.13 +#include "nsDirectoryServiceDefs.h"
    1.14 +#include "nsCOMArray.h"
    1.15 +#include "nsArrayEnumerator.h"
    1.16 +#include "nsXULAppAPI.h"
    1.17 +#include "nsIComponentRegistrar.h"
    1.18 +
    1.19 +#define SERVICE_A_CONTRACT_ID  "@mozilla.org/RegTestServiceA;1"
    1.20 +#define SERVICE_B_CONTRACT_ID  "@mozilla.org/RegTestServiceB;1"
    1.21 +
    1.22 +// {56ab1cd4-ac44-4f86-8104-171f8b8f2fc7}
    1.23 +#define CORE_SERVICE_A_CID             \
    1.24 +        { 0x56ab1cd4, 0xac44, 0x4f86, \
    1.25 +        { 0x81, 0x04, 0x17, 0x1f, 0x8b, 0x8f, 0x2f, 0xc7} }
    1.26 +NS_DEFINE_CID(kCoreServiceA_CID, CORE_SERVICE_A_CID);
    1.27 +
    1.28 +// {fe64efb7-c5ab-41a6-b639-e6c0f483181e}
    1.29 +#define EXT_SERVICE_A_CID             \
    1.30 +        { 0xfe64efb7, 0xc5ab, 0x41a6, \
    1.31 +        { 0xb6, 0x39, 0xe6, 0xc0, 0xf4, 0x83, 0x18, 0x1e} }
    1.32 +NS_DEFINE_CID(kExtServiceA_CID, EXT_SERVICE_A_CID);
    1.33 +
    1.34 +// {d04d1298-6dac-459b-a13b-bcab235730a0}
    1.35 +#define CORE_SERVICE_B_CID             \
    1.36 +        { 0xd04d1298, 0x6dac, 0x459b, \
    1.37 +        { 0xa1, 0x3b, 0xbc, 0xab, 0x23, 0x57, 0x30, 0xa0 } }
    1.38 +NS_DEFINE_CID(kCoreServiceB_CID, CORE_SERVICE_B_CID);
    1.39 +
    1.40 +// {e93dadeb-a6f6-4667-bbbc-ac8c6d440b20}
    1.41 +#define EXT_SERVICE_B_CID             \
    1.42 +        { 0xe93dadeb, 0xa6f6, 0x4667, \
    1.43 +        { 0xbb, 0xbc, 0xac, 0x8c, 0x6d, 0x44, 0x0b, 0x20 } }
    1.44 +NS_DEFINE_CID(kExtServiceB_CID, EXT_SERVICE_B_CID);
    1.45 +
    1.46 +nsresult execRegOrderTest(const char *aTestName, const char *aContractID,
    1.47 +                      const nsCID &aCoreCID, const nsCID &aExtCID)
    1.48 +{
    1.49 +  // Make sure the core service loaded (it won't be found using contract ID).
    1.50 +  nsresult rv = NS_ERROR_FAILURE;
    1.51 +  nsCOMPtr<nsISupports> coreService = do_CreateInstance(aCoreCID, &rv);
    1.52 +#ifdef DEBUG_brade
    1.53 +  if (rv) fprintf(stderr, "rv: %d (%x)\n", rv, rv);
    1.54 +  fprintf(stderr, "coreService: %p\n", coreService.get());
    1.55 +#endif
    1.56 +  if (NS_FAILED(rv))
    1.57 +  {
    1.58 +    fail("%s FAILED - cannot create core service\n", aTestName);
    1.59 +    return rv;
    1.60 +  }
    1.61 +
    1.62 +  // Get the extension service.
    1.63 +  nsCOMPtr<nsISupports> extService = do_CreateInstance(aExtCID, &rv);
    1.64 +#ifdef DEBUG_brade
    1.65 +  if (rv) fprintf(stderr, "rv: %d (%x)\n", rv, rv);
    1.66 +  fprintf(stderr, "extService: %p\n", extService.get());
    1.67 +#endif
    1.68 +  if (NS_FAILED(rv))
    1.69 +  {
    1.70 +    fail("%s FAILED - cannot create extension service\n", aTestName);
    1.71 +    return rv;
    1.72 +  }
    1.73 +
    1.74 +  /*
    1.75 +   * Get the service by contract ID and make sure it is the extension
    1.76 +   * service (it should be, since the extension directory was registered
    1.77 +   * after the core one).
    1.78 +   */
    1.79 +  nsCOMPtr<nsISupports> service = do_CreateInstance(aContractID, &rv);
    1.80 +#ifdef DEBUG_brade
    1.81 +  if (rv) fprintf(stderr, "rv: %d (%x)\n", rv, rv);
    1.82 +  fprintf(stderr, "service: %p\n", service.get());
    1.83 +#endif
    1.84 +  if (NS_FAILED(rv))
    1.85 +  {
    1.86 +    fail("%s FAILED - cannot create service\n", aTestName);
    1.87 +    return rv;
    1.88 +  }
    1.89 +
    1.90 +  if (service != extService)
    1.91 +  {
    1.92 +    fail("%s FAILED - wrong service registered\n", aTestName);
    1.93 +    return NS_ERROR_FAILURE;
    1.94 +  }
    1.95 +
    1.96 +  passed(aTestName);
    1.97 +  return NS_OK;
    1.98 +}
    1.99 +
   1.100 +nsresult TestRegular()
   1.101 +{
   1.102 +  return execRegOrderTest("TestRegular", SERVICE_A_CONTRACT_ID,
   1.103 +                          kCoreServiceA_CID, kExtServiceA_CID);
   1.104 +}
   1.105 +
   1.106 +nsresult TestJar()
   1.107 +{
   1.108 +  return execRegOrderTest("TestJar", SERVICE_B_CONTRACT_ID,
   1.109 +                          kCoreServiceB_CID, kExtServiceB_CID);
   1.110 +}
   1.111 +
   1.112 +bool TestContractFirst()
   1.113 +{
   1.114 +  nsCOMPtr<nsIComponentRegistrar> r;
   1.115 +  NS_GetComponentRegistrar(getter_AddRefs(r));
   1.116 +
   1.117 +  nsCID* cid = nullptr;
   1.118 +  nsresult rv = r->ContractIDToCID("@mozilla.org/RegTestOrderC;1", &cid);
   1.119 +  if (NS_FAILED(rv)) {
   1.120 +    fail("RegTestOrderC: contract not registered");
   1.121 +    return false;
   1.122 +  }
   1.123 +
   1.124 +  nsCID goodcid;
   1.125 +  goodcid.Parse("{ada15884-bb89-473c-8b50-dcfbb8447ff4}");
   1.126 +
   1.127 +  if (!goodcid.Equals(*cid)) {
   1.128 +    fail("RegTestOrderC: CID doesn't match");
   1.129 +    return false;
   1.130 +  }
   1.131 +
   1.132 +  passed("RegTestOrderC");
   1.133 +  return true;
   1.134 +}
   1.135 +
   1.136 +static already_AddRefed<nsIFile>
   1.137 +GetRegDirectory(const char* basename, const char* dirname, const char* leafname)
   1.138 +{
   1.139 +    nsCOMPtr<nsIFile> f;
   1.140 +    nsresult rv = NS_NewNativeLocalFile(nsDependentCString(basename), true,
   1.141 +                                        getter_AddRefs(f));
   1.142 +    if (NS_FAILED(rv))
   1.143 +        return nullptr;
   1.144 +
   1.145 +    f->AppendNative(nsDependentCString(dirname));
   1.146 +    if (leafname)
   1.147 +        f->AppendNative(nsDependentCString(leafname));
   1.148 +    return f.forget();
   1.149 +}
   1.150 +
   1.151 +
   1.152 +
   1.153 +int main(int argc, char** argv)
   1.154 +{
   1.155 +  if (argc < 2)
   1.156 +  {
   1.157 +    fprintf(stderr, "not enough arguments -- need registration dir path\n");
   1.158 +    return 1;
   1.159 +  }
   1.160 +
   1.161 +  ScopedLogging logging;
   1.162 +
   1.163 +#ifdef XP_WIN
   1.164 +  // On Windows, convert to backslashes
   1.165 +  size_t regPathLen = strlen(argv[1]);
   1.166 +  char* regPath = new char[regPathLen + 1];
   1.167 +  for (size_t i = 0; i < regPathLen; i++) {
   1.168 +    char curr = argv[1][i];
   1.169 +    regPath[i] = (curr == '/') ? '\\' : curr;
   1.170 +  }
   1.171 +  regPath[regPathLen] = '\0';
   1.172 +#else
   1.173 +  const char *regPath = argv[1];
   1.174 +#endif
   1.175 +
   1.176 +  XRE_AddManifestLocation(NS_COMPONENT_LOCATION,
   1.177 +                          nsCOMPtr<nsIFile>(GetRegDirectory(regPath, "core", "component.manifest")));
   1.178 +  XRE_AddManifestLocation(NS_COMPONENT_LOCATION,
   1.179 +                          nsCOMPtr<nsIFile>(GetRegDirectory(regPath, "extension", "extComponent.manifest")));
   1.180 +  XRE_AddJarManifestLocation(NS_COMPONENT_LOCATION,
   1.181 +                          nsCOMPtr<nsIFile>(GetRegDirectory(regPath, "extension2.jar", nullptr)));
   1.182 +  ScopedXPCOM xpcom("RegistrationOrder");
   1.183 +  if (xpcom.failed())
   1.184 +    return 1;
   1.185 +
   1.186 +  int rv = 0;
   1.187 +  if (NS_FAILED(TestRegular()))
   1.188 +    rv = 1;
   1.189 +
   1.190 +  if (NS_FAILED(TestJar()))
   1.191 +    rv = 1;
   1.192 +
   1.193 +  if (!TestContractFirst())
   1.194 +    rv = 1;
   1.195 +
   1.196 +  return rv;
   1.197 +}

mercurial