Bug 548621 - Create suite of tests for JS parsing speed. r=jorendorff.
[mozilla-central.git] / xpcom / tests / TestRegistrationOrder.cpp
blobc42b9a35e42e9c6d626f32adaecb8897b8b3a6da
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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"
47 #define SERVICE_A_CONTRACT_ID "@mozilla.org/RegTestServiceA;1"
48 #define SERVICE_B_CONTRACT_ID "@mozilla.org/RegTestServiceB;1"
50 // {56ab1cd4-ac44-4f86-8104-171f8b8f2fc7}
51 #define CORE_SERVICE_A_CID \
52 { 0x56ab1cd4, 0xac44, 0x4f86, \
53 { 0x81, 0x04, 0x17, 0x1f, 0x8b, 0x8f, 0x2f, 0xc7} }
54 NS_DEFINE_CID(kCoreServiceA_CID, CORE_SERVICE_A_CID);
56 // {fe64efb7-c5ab-41a6-b639-e6c0f483181e}
57 #define EXT_SERVICE_A_CID \
58 { 0xfe64efb7, 0xc5ab, 0x41a6, \
59 { 0xb6, 0x39, 0xe6, 0xc0, 0xf4, 0x83, 0x18, 0x1e} }
60 NS_DEFINE_CID(kExtServiceA_CID, EXT_SERVICE_A_CID);
62 // {d04d1298-6dac-459b-a13b-bcab235730a0}
63 #define CORE_SERVICE_B_CID \
64 { 0xd04d1298, 0x6dac, 0x459b, \
65 { 0xa1, 0x3b, 0xbc, 0xab, 0x23, 0x57, 0x30, 0xa0 } }
66 NS_DEFINE_CID(kCoreServiceB_CID, CORE_SERVICE_B_CID);
68 // {e93dadeb-a6f6-4667-bbbc-ac8c6d440b20}
69 #define EXT_SERVICE_B_CID \
70 { 0xe93dadeb, 0xa6f6, 0x4667, \
71 { 0xbb, 0xbc, 0xac, 0x8c, 0x6d, 0x44, 0x0b, 0x20 } }
72 NS_DEFINE_CID(kExtServiceB_CID, EXT_SERVICE_B_CID);
75 #ifdef DEBUG_brade
76 inline void
77 debugPrintPath(const char *aPrefix, nsIFile *aFile)
79 if (!aPrefix || !aFile)
80 return;
82 nsCAutoString path;
83 nsresult rv = aFile->GetNativePath(path);
84 if (NS_FAILED(rv))
85 fprintf(stderr, "%s: GetNativePath failed 0x%x\n", aPrefix, rv);
86 else
87 fprintf(stderr, "%s: %s\n", aPrefix, path.get());
89 #endif /* DEBUG_brade */
92 * nsIDirectoryServiceProvider class.
94 class RegOrderDirSvcProvider: public nsIDirectoryServiceProvider2 {
95 public:
96 RegOrderDirSvcProvider(const char *aRegPath)
97 : mRegPath(aRegPath)
101 NS_DECL_ISUPPORTS
102 NS_DECL_NSIDIRECTORYSERVICEPROVIDER
103 NS_DECL_NSIDIRECTORYSERVICEPROVIDER2
105 private:
106 nsresult getRegDirectory(const char *aDirName, nsIFile **_retval);
107 const char *mRegPath;
110 NS_IMPL_ISUPPORTS2(RegOrderDirSvcProvider,
111 nsIDirectoryServiceProvider,
112 nsIDirectoryServiceProvider2)
114 NS_IMETHODIMP
115 RegOrderDirSvcProvider::GetFiles(const char *aKey, nsISimpleEnumerator **aEnum)
117 nsresult rv = NS_ERROR_FAILURE;
118 *aEnum = nsnull;
120 #ifdef DEBUG_brade
121 fprintf(stderr, "GetFiles(%s)\n", aKey);
122 #endif
124 if (0 == strcmp(aKey, NS_XPCOM_COMPONENT_DIR_LIST))
126 nsCOMPtr<nsIFile> coreDir;
127 rv = getRegDirectory("core", getter_AddRefs(coreDir));
128 if (NS_SUCCEEDED(rv))
130 nsCOMPtr<nsIFile> extDir;
131 rv = getRegDirectory("extension", getter_AddRefs(extDir));
132 if (NS_SUCCEEDED(rv))
134 nsCOMArray<nsIFile> dirArray;
135 dirArray.AppendObject(coreDir);
136 dirArray.AppendObject(extDir);
137 rv = NS_NewArrayEnumerator(aEnum, dirArray);
142 #ifdef DEBUG_brade
143 if (rv) fprintf(stderr, "rv: %d (%x)\n", rv, rv);
144 #endif
145 return rv;
148 NS_IMETHODIMP
149 RegOrderDirSvcProvider::GetFile(const char *aProp, PRBool *aPersistent,
150 nsIFile **_retval)
152 *_retval = nsnull;
153 return NS_ERROR_FAILURE;
156 nsresult
157 RegOrderDirSvcProvider::getRegDirectory(const char *aDirName, nsIFile **_retval)
159 nsCOMPtr<nsILocalFile> compDir;
160 nsresult rv = NS_NewLocalFile(EmptyString(), PR_TRUE,
161 getter_AddRefs(compDir));
162 if (NS_FAILED(rv))
163 return rv;
165 rv = compDir->InitWithNativePath(nsDependentCString(mRegPath));
166 if (NS_FAILED(rv))
167 return rv;
169 rv = compDir->AppendRelativeNativePath(nsDependentCString(aDirName));
170 if (NS_FAILED(rv))
171 return rv;
173 *_retval = compDir;
174 NS_ADDREF(*_retval);
175 return NS_OK;
178 nsresult execRegOrderTest(const char *aTestName, const char *aContractID,
179 const nsCID &aCoreCID, const nsCID &aExtCID)
181 // Make sure the core service loaded (it won't be found using contract ID).
182 nsresult rv = NS_ERROR_FAILURE;
183 nsCOMPtr<nsISupports> coreService = do_CreateInstance(aCoreCID, &rv);
184 #ifdef DEBUG_brade
185 if (rv) fprintf(stderr, "rv: %d (%x)\n", rv, rv);
186 fprintf(stderr, "coreService: %p\n", coreService.get());
187 #endif
188 if (NS_FAILED(rv))
190 fail("%s FAILED - cannot create core service\n", aTestName);
191 return rv;
194 // Get the extension service.
195 nsCOMPtr<nsISupports> extService = do_CreateInstance(aExtCID, &rv);
196 #ifdef DEBUG_brade
197 if (rv) fprintf(stderr, "rv: %d (%x)\n", rv, rv);
198 fprintf(stderr, "extService: %p\n", extService.get());
199 #endif
200 if (NS_FAILED(rv))
202 fail("%s FAILED - cannot create extension service\n", aTestName);
203 return rv;
207 * Get the service by contract ID and make sure it is the extension
208 * service (it should be, since the extension directory was registered
209 * after the core one).
211 nsCOMPtr<nsISupports> service = do_CreateInstance(aContractID, &rv);
212 #ifdef DEBUG_brade
213 if (rv) fprintf(stderr, "rv: %d (%x)\n", rv, rv);
214 fprintf(stderr, "service: %p\n", service.get());
215 #endif
216 if (NS_FAILED(rv))
218 fail("%s FAILED - cannot create service\n", aTestName);
219 return rv;
222 if (service != extService)
224 fail("%s FAILED - wrong service registered\n", aTestName);
225 return NS_ERROR_FAILURE;
228 passed(aTestName);
229 return NS_OK;
232 nsresult TestRegular()
234 return execRegOrderTest("TestRegular", SERVICE_A_CONTRACT_ID,
235 kCoreServiceA_CID, kExtServiceA_CID);
238 nsresult TestDeferred()
240 return execRegOrderTest("TestDeferred", SERVICE_B_CONTRACT_ID,
241 kCoreServiceB_CID, kExtServiceB_CID);
244 int main(int argc, char** argv)
246 if (argc < 2)
248 fprintf(stderr, "not enough arguments -- need registration dir path\n");
249 return 1;
252 const char *regPath = argv[1];
253 RegOrderDirSvcProvider *dirSvcProvider = new RegOrderDirSvcProvider(regPath);
254 if (NULL == dirSvcProvider)
256 fprintf(stderr, "could not create dirSvcProvider\n");
257 return 1;
259 // no addref needed, ScopedXPCOM will do that if it doesn't fail
261 ScopedXPCOM xpcom("RegistrationOrder", dirSvcProvider);
262 if (xpcom.failed())
263 return 1;
265 int rv = 0;
266 if (NS_FAILED(TestRegular()))
267 rv = 1;
268 if (NS_FAILED(TestDeferred()))
269 rv = 1;
271 return rv;