VirtualBox

source: vbox/trunk/src/libs/xpcom18a4/xpcom/tests/TestObserverService.cpp@ 102348

Last change on this file since 102348 was 102348, checked in by vboxsync, 14 months ago

libs/xpcom: Get rid of prprf.h and prprf.c, bugref:10545 [build fix]

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 8.0 KB
Line 
1/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2/* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 *
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/
9 *
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.
14 *
15 * The Original Code is mozilla.org code.
16 *
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.
21 *
22 * Contributor(s):
23 * Pierre Phaneuf <pp@ludusdesign.com>
24 *
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.
36 *
37 * ***** END LICENSE BLOCK ***** */
38
39#include "nsISupports.h"
40#include "nsIComponentManager.h"
41#include "nsIObserverService.h"
42#include "nsIObserver.h"
43#include "nsIEnumerator.h"
44#include "nsString.h"
45#include "nsReadableUtils.h"
46#include "nsWeakReference.h"
47
48static nsIObserverService *anObserverService = NULL;
49
50#ifdef VBOX
51static bool testResult( nsresult rv ) {
52 if ( NS_SUCCEEDED( rv ) ) {
53 printf("...ok\n");
54 return true;
55 }
56 printf("...failed, rv=0x%x\n", (int)rv);
57 return false;
58}
59#else
60static void testResult( nsresult rv ) {
61 if ( NS_SUCCEEDED( rv ) ) {
62 printf("...ok\n");
63 } else {
64 printf("...failed, rv=0x%x\n", (int)rv);
65 }
66 return;
67}
68#endif
69
70void printString(nsString &str) {
71#ifdef VBOX /* asan complains about mixing different allocators */
72 char *cstr = ToNewCString(str);
73 printf("%s", cstr);
74 nsMemory::Free(cstr);
75#else
76 const char *cstr = ToNewCString(str);
77 printf("%s", cstr);
78 delete [] (char*)cstr;
79#endif
80}
81
82class TestObserver : public nsIObserver, public nsSupportsWeakReference {
83public:
84 TestObserver( const nsAString &name )
85 : mName( name ) {
86 }
87 NS_DECL_ISUPPORTS
88 NS_DECL_NSIOBSERVER
89
90 nsString mName;
91
92private:
93 ~TestObserver() {}
94};
95
96NS_IMPL_ISUPPORTS2( TestObserver, nsIObserver, nsISupportsWeakReference )
97
98NS_IMETHODIMP
99TestObserver::Observe( nsISupports *aSubject,
100 const char *aTopic,
101 const PRUnichar *someData ) {
102 nsCString topic( aTopic );
103 nsString data( someData );
104 /*
105 The annoying double-cast below is to work around an annoying bug in
106 the compiler currently used on wensleydale. This is a test.
107 */
108 printString(mName);
109 printf(" has observed something: subject@%p", (void*)aSubject);
110 printf(" name=");
111 printString(NS_REINTERPRET_CAST(TestObserver*, NS_REINTERPRET_CAST(void*, aSubject))->mName);
112 printf(" aTopic=%s", topic.get());
113 printf(" someData=");
114 printString(data);
115 printf("\n");
116 return NS_OK;
117}
118
119int main(int argc, char *argv[])
120{
121 nsCString topicA; topicA.Assign( "topic-A" );
122 nsCString topicB; topicB.Assign( "topic-B" );
123 nsresult rv;
124
125 nsresult res = nsComponentManager::CreateInstance("@mozilla.org/observer-service;1",
126 NULL,
127 NS_GET_IID(nsIObserverService),
128 (void **) &anObserverService);
129#ifdef VBOX
130 bool fSuccess = res == NS_OK;
131#endif
132
133 if (res == NS_OK) {
134
135 nsIObserver *aObserver = new TestObserver(NS_LITERAL_STRING("Observer-A"));
136 aObserver->AddRef();
137 nsIObserver *bObserver = new TestObserver(NS_LITERAL_STRING("Observer-B"));
138 bObserver->AddRef();
139
140 printf("Adding Observer-A as observer of topic-A...\n");
141 rv = anObserverService->AddObserver(aObserver, topicA.get(), PR_FALSE);
142#ifdef VBOX
143 fSuccess = fSuccess &&
144#endif
145 testResult(rv);
146
147 printf("Adding Observer-B as observer of topic-A...\n");
148 rv = anObserverService->AddObserver(bObserver, topicA.get(), PR_FALSE);
149#ifdef VBOX
150 fSuccess = fSuccess &&
151#endif
152 testResult(rv);
153
154 printf("Adding Observer-B as observer of topic-B...\n");
155 rv = anObserverService->AddObserver(bObserver, topicB.get(), PR_FALSE);
156#ifdef VBOX
157 fSuccess = fSuccess &&
158#endif
159 testResult(rv);
160
161 printf("Testing Notify(observer-A, topic-A)...\n");
162 rv = anObserverService->NotifyObservers( aObserver,
163 topicA.get(),
164 NS_LITERAL_STRING("Testing Notify(observer-A, topic-A)").get() );
165#ifdef VBOX
166 fSuccess = fSuccess &&
167#endif
168 testResult(rv);
169
170 printf("Testing Notify(observer-B, topic-B)...\n");
171 rv = anObserverService->NotifyObservers( bObserver,
172 topicB.get(),
173 NS_LITERAL_STRING("Testing Notify(observer-B, topic-B)").get() );
174#ifdef VBOX
175 fSuccess = fSuccess &&
176#endif
177 testResult(rv);
178
179 printf("Testing EnumerateObserverList (for topic-A)...\n");
180 nsCOMPtr<nsISimpleEnumerator> e;
181 rv = anObserverService->EnumerateObservers(topicA.get(), getter_AddRefs(e));
182
183#ifdef VBOX
184 fSuccess = fSuccess &&
185#endif
186 testResult(rv);
187
188 printf("Enumerating observers of topic-A...\n");
189 if ( e ) {
190 nsCOMPtr<nsIObserver> observer;
191 PRBool loop = PR_TRUE;
192 while( NS_SUCCEEDED(e->HasMoreElements(&loop)) && loop)
193 {
194 e->GetNext(getter_AddRefs(observer));
195 printf("Calling observe on enumerated observer ");
196 printString(NS_REINTERPRET_CAST(TestObserver*,
197 NS_REINTERPRET_CAST(void*, observer.get()))->mName);
198 printf("...\n");
199 rv = observer->Observe( observer,
200 topicA.get(),
201 NS_LITERAL_STRING("during enumeration").get() );
202#ifdef VBOX
203 fSuccess = fSuccess &&
204#endif
205 testResult(rv);
206 }
207 }
208 printf("...done enumerating observers of topic-A\n");
209
210 printf("Removing Observer-A...\n");
211 rv = anObserverService->RemoveObserver(aObserver, topicA.get());
212#ifdef VBOX
213 fSuccess = fSuccess &&
214#endif
215 testResult(rv);
216
217
218 printf("Removing Observer-B (topic-A)...\n");
219 rv = anObserverService->RemoveObserver(bObserver, topicB.get());
220#ifdef VBOX
221 fSuccess = fSuccess &&
222#endif
223 testResult(rv);
224 printf("Removing Observer-B (topic-B)...\n");
225 rv = anObserverService->RemoveObserver(bObserver, topicA.get());
226#ifdef VBOX
227 fSuccess = fSuccess &&
228#endif
229 testResult(rv);
230
231#ifdef VBOX
232 /* Cleanup: */
233 nsrefcnt refs = bObserver->Release();
234 fSuccess = fSuccess && refs == 0;
235 if (refs != 0)
236 printf("bObserver->Release() -> %d, expected 0\n", (int)refs);
237
238 refs = aObserver->Release();
239 fSuccess = fSuccess && refs == 0;
240 if (refs != 0)
241 printf("aObserver->Release() -> %d, expected 0\n", (int)refs);
242#endif
243 }
244#ifdef VBOX
245 return fSuccess ? 0 : 1;
246#else
247 return NS_OK;
248#endif
249}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette