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