Bug 545892 - Always pass WM_NCPAINT events to the default event procedure. r=bent...
[mozilla-central.git] / xpcom / tests / TestRegistrationOrder.cpp
blob59c5bfd990a35ad3c077c89f2142d4de1ee3b4df
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim:set ts=2 sw=2 sts=2 et: */
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
14 * License.
16 * The Original Code is mozilla.org code.
18 * The Initial Developer of the Original Code is
19 * Kathleen Brade <brade@pearlcrescent.com>
20 * Mark Smith <mcs@pearlcrescent.com>
21 * Portions created by the Initial Developer are Copyright (C) 2008
22 * the Initial Developer. All Rights Reserved.
24 * Contributor(s):
26 * Alternatively, the contents of this file may be used under the terms of
27 * either of the GNU General Public License Version 2 or later (the "GPL"),
28 * or 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 ***** */
40 #include "TestHarness.h"
41 #include "nsILocalFile.h"
42 #include "nsIDirectoryService.h"
43 #include "nsDirectoryServiceDefs.h"
44 #include "nsCOMArray.h"
45 #include "nsArrayEnumerator.h"
46 #include "nsXULAppAPI.h"
47 #include "nsIComponentRegistrar.h"
49 #define SERVICE_A_CONTRACT_ID "@mozilla.org/RegTestServiceA;1"
50 #define SERVICE_B_CONTRACT_ID "@mozilla.org/RegTestServiceB;1"
52 // {56ab1cd4-ac44-4f86-8104-171f8b8f2fc7}
53 #define CORE_SERVICE_A_CID \
54 { 0x56ab1cd4, 0xac44, 0x4f86, \
55 { 0x81, 0x04, 0x17, 0x1f, 0x8b, 0x8f, 0x2f, 0xc7} }
56 NS_DEFINE_CID(kCoreServiceA_CID, CORE_SERVICE_A_CID);
58 // {fe64efb7-c5ab-41a6-b639-e6c0f483181e}
59 #define EXT_SERVICE_A_CID \
60 { 0xfe64efb7, 0xc5ab, 0x41a6, \
61 { 0xb6, 0x39, 0xe6, 0xc0, 0xf4, 0x83, 0x18, 0x1e} }
62 NS_DEFINE_CID(kExtServiceA_CID, EXT_SERVICE_A_CID);
64 // {d04d1298-6dac-459b-a13b-bcab235730a0}
65 #define CORE_SERVICE_B_CID \
66 { 0xd04d1298, 0x6dac, 0x459b, \
67 { 0xa1, 0x3b, 0xbc, 0xab, 0x23, 0x57, 0x30, 0xa0 } }
68 NS_DEFINE_CID(kCoreServiceB_CID, CORE_SERVICE_B_CID);
70 // {e93dadeb-a6f6-4667-bbbc-ac8c6d440b20}
71 #define EXT_SERVICE_B_CID \
72 { 0xe93dadeb, 0xa6f6, 0x4667, \
73 { 0xbb, 0xbc, 0xac, 0x8c, 0x6d, 0x44, 0x0b, 0x20 } }
74 NS_DEFINE_CID(kExtServiceB_CID, EXT_SERVICE_B_CID);
76 nsresult execRegOrderTest(const char *aTestName, const char *aContractID,
77 const nsCID &aCoreCID, const nsCID &aExtCID)
79 // Make sure the core service loaded (it won't be found using contract ID).
80 nsresult rv = NS_ERROR_FAILURE;
81 nsCOMPtr<nsISupports> coreService = do_CreateInstance(aCoreCID, &rv);
82 #ifdef DEBUG_brade
83 if (rv) fprintf(stderr, "rv: %d (%x)\n", rv, rv);
84 fprintf(stderr, "coreService: %p\n", coreService.get());
85 #endif
86 if (NS_FAILED(rv))
88 fail("%s FAILED - cannot create core service\n", aTestName);
89 return rv;
92 // Get the extension service.
93 nsCOMPtr<nsISupports> extService = do_CreateInstance(aExtCID, &rv);
94 #ifdef DEBUG_brade
95 if (rv) fprintf(stderr, "rv: %d (%x)\n", rv, rv);
96 fprintf(stderr, "extService: %p\n", extService.get());
97 #endif
98 if (NS_FAILED(rv))
100 fail("%s FAILED - cannot create extension service\n", aTestName);
101 return rv;
105 * Get the service by contract ID and make sure it is the extension
106 * service (it should be, since the extension directory was registered
107 * after the core one).
109 nsCOMPtr<nsISupports> service = do_CreateInstance(aContractID, &rv);
110 #ifdef DEBUG_brade
111 if (rv) fprintf(stderr, "rv: %d (%x)\n", rv, rv);
112 fprintf(stderr, "service: %p\n", service.get());
113 #endif
114 if (NS_FAILED(rv))
116 fail("%s FAILED - cannot create service\n", aTestName);
117 return rv;
120 if (service != extService)
122 fail("%s FAILED - wrong service registered\n", aTestName);
123 return NS_ERROR_FAILURE;
126 passed(aTestName);
127 return NS_OK;
130 nsresult TestRegular()
132 return execRegOrderTest("TestRegular", SERVICE_A_CONTRACT_ID,
133 kCoreServiceA_CID, kExtServiceA_CID);
136 nsresult TestJar()
138 return execRegOrderTest("TestJar", SERVICE_B_CONTRACT_ID,
139 kCoreServiceB_CID, kExtServiceB_CID);
142 bool TestContractFirst()
144 nsCOMPtr<nsIComponentRegistrar> r;
145 NS_GetComponentRegistrar(getter_AddRefs(r));
147 nsCID* cid = NULL;
148 nsresult rv = r->ContractIDToCID("@mozilla.org/RegTestOrderC;1", &cid);
149 if (NS_FAILED(rv)) {
150 fail("RegTestOrderC: contract not registered");
151 return false;
154 nsCID goodcid;
155 goodcid.Parse("{ada15884-bb89-473c-8b50-dcfbb8447ff4}");
157 if (!goodcid.Equals(*cid)) {
158 fail("RegTestOrderC: CID doesn't match");
159 return false;
162 passed("RegTestOrderC");
163 return true;
166 static already_AddRefed<nsILocalFile>
167 GetRegDirectory(const char* basename, const char* dirname, const char* leafname)
169 nsCOMPtr<nsILocalFile> f;
170 nsresult rv = NS_NewNativeLocalFile(nsDependentCString(basename), PR_TRUE,
171 getter_AddRefs(f));
172 if (NS_FAILED(rv))
173 return NULL;
175 f->AppendNative(nsDependentCString(dirname));
176 if (leafname)
177 f->AppendNative(nsDependentCString(leafname));
178 return f.forget();
183 int main(int argc, char** argv)
185 if (argc < 2)
187 fprintf(stderr, "not enough arguments -- need registration dir path\n");
188 return 1;
191 ScopedLogging logging;
193 const char *regPath = argv[1];
194 XRE_AddManifestLocation(NS_COMPONENT_LOCATION,
195 nsCOMPtr<nsILocalFile>(GetRegDirectory(regPath, "core", "component.manifest")));
196 XRE_AddManifestLocation(NS_COMPONENT_LOCATION,
197 nsCOMPtr<nsILocalFile>(GetRegDirectory(regPath, "extension", "extComponent.manifest")));
198 XRE_AddJarManifestLocation(NS_COMPONENT_LOCATION,
199 nsCOMPtr<nsILocalFile>(GetRegDirectory(regPath, "extension2.jar", NULL)));
200 ScopedXPCOM xpcom("RegistrationOrder");
201 if (xpcom.failed())
202 return 1;
204 int rv = 0;
205 if (NS_FAILED(TestRegular()))
206 rv = 1;
208 if (NS_FAILED(TestJar()))
209 rv = 1;
211 if (!TestContractFirst())
212 rv = 1;
214 return rv;