Added cairoFontface destroy
[mozilla-central.git] / xpcom / tests / TestServMgr.cpp
blob283da71c29f7c59617f0c8f93eaa9aa4f97963a4
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
15 * The Original Code is mozilla.org code.
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998
20 * the Initial Developer. All Rights Reserved.
22 * Contributor(s):
23 * Pierre Phaneuf <pp@ludusdesign.com>
25 * Alternatively, the contents of this file may be used under the terms of
26 * either of the GNU General Public License Version 2 or later (the "GPL"),
27 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
37 * ***** END LICENSE BLOCK ***** */
39 #include "MyService.h"
40 #include "nsXPCOM.h"
41 #include "nsIServiceManager.h"
42 #include "nsIComponentManager.h"
43 #include "nsServiceManagerUtils.h"
44 #include <stdio.h>
46 static NS_DEFINE_CID(kIMyServiceCID, NS_IMYSERVICE_CID);
48 ////////////////////////////////////////////////////////////////////////////////
50 IMyService* myServ = NULL;
52 nsresult
53 BeginTest(int testNumber)
55 nsresult err;
56 NS_ASSERTION(myServ == NULL, "myServ not reset");
57 err = CallGetService(kIMyServiceCID, &myServ);
58 return err;
61 nsresult
62 EndTest(int testNumber)
64 nsresult err = NS_OK;
66 if (myServ) {
67 err = myServ->Doit();
68 if (err != NS_OK) return err;
70 NS_RELEASE(myServ);
73 printf("test %d succeeded\n", testNumber);
74 return NS_OK;
77 nsresult
78 SimpleTest(int testNumber)
80 // This test just gets a service, uses it and releases it.
82 nsresult err;
83 err = BeginTest(testNumber);
84 if (err != NS_OK) return err;
85 err = EndTest(testNumber);
86 return err;
89 ////////////////////////////////////////////////////////////////////////////////
91 nsresult
92 AsyncShutdown(int testNumber)
94 nsresult err = NS_OK;
96 // If the AsyncShutdown was truly asynchronous and happened on another
97 // thread, we'd have to protect all accesses to myServ throughout this
98 // code with a monitor.
100 // XXX-darin: say what?!?
103 err = nsServiceManager::UnregisterService(kIMyServiceCID);
104 if (err == NS_ERROR_SERVICE_NOT_AVAILABLE) {
105 printf("async shutdown -- service not found\n");
106 return NS_OK;
110 return err;
113 nsresult
114 AsyncNoShutdownTest(int testNumber)
116 // This test gets a service, and also gets an async request for shutdown,
117 // but the service doesn't get shut down because some other client (who's
118 // not participating in the async shutdown game as he should) is
119 // continuing to hang onto the service. This causes myServ variable to
120 // get set to NULL, but the service doesn't get unloaded right away as
121 // it should.
123 nsresult err;
125 err = BeginTest(testNumber);
126 if (err != NS_OK) return err;
128 // Create some other user of kIMyServiceCID, preventing it from
129 // really going away:
130 IMyService* otherClient;
131 err = CallGetService(kIMyServiceCID, &otherClient);
132 if (err != NS_OK) return err;
134 err = AsyncShutdown(testNumber);
135 if (err != NS_OK) return err;
136 err = EndTest(testNumber);
138 // Finally, release the other client.
139 NS_RELEASE(otherClient);
141 return err;
144 ////////////////////////////////////////////////////////////////////////////////
146 main(void)
148 nsresult err;
149 int testNumber = 0;
151 err = NS_InitXPCOM2(nsnull, nsnull, nsnull);
152 if (NS_FAILED(err)) {
153 printf("NS_InitXPCOM2 failed\n");
154 return -1;
157 err = SimpleTest(++testNumber);
158 if (err != NS_OK)
159 goto error;
161 err = AsyncNoShutdownTest(++testNumber);
162 if (err != NS_OK)
163 goto error;
165 AsyncShutdown(++testNumber);
167 printf("there was great success\n");
168 return 0;
170 error:
171 printf("test %d failed\n", testNumber);
172 return -1;
175 ////////////////////////////////////////////////////////////////////////////////