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
5 : *
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/
10 : *
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.
15 : *
16 : * The Original Code is mozilla.org code.
17 : *
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.
23 : *
24 : * Contributor(s):
25 : *
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.
37 : *
38 : * ***** END LICENSE BLOCK ***** */
39 :
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"
48 :
49 : #define SERVICE_A_CONTRACT_ID "@mozilla.org/RegTestServiceA;1"
50 : #define SERVICE_B_CONTRACT_ID "@mozilla.org/RegTestServiceB;1"
51 :
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);
57 :
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);
63 :
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);
69 :
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);
75 :
76 2 : nsresult execRegOrderTest(const char *aTestName, const char *aContractID,
77 : const nsCID &aCoreCID, const nsCID &aExtCID)
78 : {
79 : // Make sure the core service loaded (it won't be found using contract ID).
80 2 : nsresult rv = NS_ERROR_FAILURE;
81 4 : 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 2 : if (NS_FAILED(rv))
87 : {
88 0 : fail("%s FAILED - cannot create core service\n", aTestName);
89 0 : return rv;
90 : }
91 :
92 : // Get the extension service.
93 4 : 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 2 : if (NS_FAILED(rv))
99 : {
100 0 : fail("%s FAILED - cannot create extension service\n", aTestName);
101 0 : return rv;
102 : }
103 :
104 : /*
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).
108 : */
109 4 : 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 2 : if (NS_FAILED(rv))
115 : {
116 0 : fail("%s FAILED - cannot create service\n", aTestName);
117 0 : return rv;
118 : }
119 :
120 2 : if (service != extService)
121 : {
122 0 : fail("%s FAILED - wrong service registered\n", aTestName);
123 0 : return NS_ERROR_FAILURE;
124 : }
125 :
126 2 : passed(aTestName);
127 2 : return NS_OK;
128 : }
129 :
130 1 : nsresult TestRegular()
131 : {
132 : return execRegOrderTest("TestRegular", SERVICE_A_CONTRACT_ID,
133 1 : kCoreServiceA_CID, kExtServiceA_CID);
134 : }
135 :
136 1 : nsresult TestJar()
137 : {
138 : return execRegOrderTest("TestJar", SERVICE_B_CONTRACT_ID,
139 1 : kCoreServiceB_CID, kExtServiceB_CID);
140 : }
141 :
142 1 : bool TestContractFirst()
143 : {
144 2 : nsCOMPtr<nsIComponentRegistrar> r;
145 1 : NS_GetComponentRegistrar(getter_AddRefs(r));
146 :
147 1 : nsCID* cid = NULL;
148 1 : nsresult rv = r->ContractIDToCID("@mozilla.org/RegTestOrderC;1", &cid);
149 1 : if (NS_FAILED(rv)) {
150 0 : fail("RegTestOrderC: contract not registered");
151 0 : return false;
152 : }
153 :
154 : nsCID goodcid;
155 1 : goodcid.Parse("{ada15884-bb89-473c-8b50-dcfbb8447ff4}");
156 :
157 1 : if (!goodcid.Equals(*cid)) {
158 0 : fail("RegTestOrderC: CID doesn't match");
159 0 : return false;
160 : }
161 :
162 1 : passed("RegTestOrderC");
163 1 : return true;
164 : }
165 :
166 : static already_AddRefed<nsILocalFile>
167 3 : GetRegDirectory(const char* basename, const char* dirname, const char* leafname)
168 : {
169 6 : nsCOMPtr<nsILocalFile> f;
170 3 : nsresult rv = NS_NewNativeLocalFile(nsDependentCString(basename), true,
171 6 : getter_AddRefs(f));
172 3 : if (NS_FAILED(rv))
173 0 : return NULL;
174 :
175 3 : f->AppendNative(nsDependentCString(dirname));
176 3 : if (leafname)
177 2 : f->AppendNative(nsDependentCString(leafname));
178 3 : return f.forget();
179 : }
180 :
181 :
182 :
183 1 : int main(int argc, char** argv)
184 : {
185 1 : if (argc < 2)
186 : {
187 0 : fprintf(stderr, "not enough arguments -- need registration dir path\n");
188 0 : return 1;
189 : }
190 :
191 2 : ScopedLogging logging;
192 :
193 1 : const char *regPath = argv[1];
194 : XRE_AddManifestLocation(NS_COMPONENT_LOCATION,
195 1 : nsCOMPtr<nsILocalFile>(GetRegDirectory(regPath, "core", "component.manifest")));
196 : XRE_AddManifestLocation(NS_COMPONENT_LOCATION,
197 1 : nsCOMPtr<nsILocalFile>(GetRegDirectory(regPath, "extension", "extComponent.manifest")));
198 : XRE_AddJarManifestLocation(NS_COMPONENT_LOCATION,
199 1 : nsCOMPtr<nsILocalFile>(GetRegDirectory(regPath, "extension2.jar", NULL)));
200 2 : ScopedXPCOM xpcom("RegistrationOrder");
201 1 : if (xpcom.failed())
202 0 : return 1;
203 :
204 1 : int rv = 0;
205 1 : if (NS_FAILED(TestRegular()))
206 0 : rv = 1;
207 :
208 1 : if (NS_FAILED(TestJar()))
209 0 : rv = 1;
210 :
211 1 : if (!TestContractFirst())
212 0 : rv = 1;
213 :
214 1 : return rv;
215 : }
|