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 "nsWeakReference.h"
|
---|
47 |
|
---|
48 | static nsIObserverService *anObserverService = NULL;
|
---|
49 |
|
---|
50 | #ifdef VBOX
|
---|
51 | static 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
|
---|
60 | static 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 |
|
---|
70 | static void 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 |
|
---|
82 | class TestObserver : public nsIObserver, public nsSupportsWeakReference {
|
---|
83 | public:
|
---|
84 | TestObserver( const nsAString &name )
|
---|
85 | : mName( name ) {
|
---|
86 | }
|
---|
87 | NS_DECL_ISUPPORTS
|
---|
88 | NS_DECL_NSIOBSERVER
|
---|
89 |
|
---|
90 | nsString mName;
|
---|
91 |
|
---|
92 | private:
|
---|
93 | ~TestObserver() {}
|
---|
94 | };
|
---|
95 |
|
---|
96 | NS_IMPL_ISUPPORTS2( TestObserver, nsIObserver, nsISupportsWeakReference )
|
---|
97 |
|
---|
98 | NS_IMETHODIMP
|
---|
99 | TestObserver::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 |
|
---|
119 | int 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 |
|
---|
130 | bool fSuccess = res == NS_OK;
|
---|
131 | if (fSuccess) {
|
---|
132 |
|
---|
133 | nsIObserver *aObserver = new TestObserver(NS_LITERAL_STRING("Observer-A"));
|
---|
134 | aObserver->AddRef();
|
---|
135 | nsIObserver *bObserver = new TestObserver(NS_LITERAL_STRING("Observer-B"));
|
---|
136 | bObserver->AddRef();
|
---|
137 |
|
---|
138 | printf("Adding Observer-A as observer of topic-A...\n");
|
---|
139 | rv = anObserverService->AddObserver(aObserver, topicA.get(), PR_FALSE);
|
---|
140 | fSuccess = testResult(rv);
|
---|
141 |
|
---|
142 | printf("Adding Observer-B as observer of topic-A...\n");
|
---|
143 | rv = anObserverService->AddObserver(bObserver, topicA.get(), PR_FALSE);
|
---|
144 | fSuccess = fSuccess && testResult(rv);
|
---|
145 |
|
---|
146 | printf("Adding Observer-B as observer of topic-B...\n");
|
---|
147 | rv = anObserverService->AddObserver(bObserver, topicB.get(), PR_FALSE);
|
---|
148 | fSuccess = fSuccess && testResult(rv);
|
---|
149 |
|
---|
150 | printf("Testing Notify(observer-A, topic-A)...\n");
|
---|
151 | rv = anObserverService->NotifyObservers( aObserver,
|
---|
152 | topicA.get(),
|
---|
153 | NS_LITERAL_STRING("Testing Notify(observer-A, topic-A)").get() );
|
---|
154 | fSuccess = fSuccess && testResult(rv);
|
---|
155 |
|
---|
156 | printf("Testing Notify(observer-B, topic-B)...\n");
|
---|
157 | rv = anObserverService->NotifyObservers( bObserver,
|
---|
158 | topicB.get(),
|
---|
159 | NS_LITERAL_STRING("Testing Notify(observer-B, topic-B)").get() );
|
---|
160 | fSuccess = fSuccess && testResult(rv);
|
---|
161 |
|
---|
162 | printf("Testing EnumerateObserverList (for topic-A)...\n");
|
---|
163 | nsCOMPtr<nsISimpleEnumerator> e;
|
---|
164 | rv = anObserverService->EnumerateObservers(topicA.get(), getter_AddRefs(e));
|
---|
165 | fSuccess = fSuccess && testResult(rv);
|
---|
166 |
|
---|
167 | printf("Enumerating observers of topic-A...\n");
|
---|
168 | if ( e ) {
|
---|
169 | nsCOMPtr<nsIObserver> observer;
|
---|
170 | PRBool loop = PR_TRUE;
|
---|
171 | while( NS_SUCCEEDED(e->HasMoreElements(&loop)) && loop)
|
---|
172 | {
|
---|
173 | e->GetNext(getter_AddRefs(observer));
|
---|
174 | printf("Calling observe on enumerated observer ");
|
---|
175 | printString(NS_REINTERPRET_CAST(TestObserver*,
|
---|
176 | NS_REINTERPRET_CAST(void*, observer.get()))->mName);
|
---|
177 | printf("...\n");
|
---|
178 | rv = observer->Observe( observer,
|
---|
179 | topicA.get(),
|
---|
180 | NS_LITERAL_STRING("during enumeration").get() );
|
---|
181 | fSuccess = fSuccess && testResult(rv);
|
---|
182 | }
|
---|
183 | }
|
---|
184 | printf("...done enumerating observers of topic-A\n");
|
---|
185 |
|
---|
186 | printf("Removing Observer-A...\n");
|
---|
187 | rv = anObserverService->RemoveObserver(aObserver, topicA.get());
|
---|
188 | fSuccess = fSuccess && testResult(rv);
|
---|
189 |
|
---|
190 |
|
---|
191 | printf("Removing Observer-B (topic-A)...\n");
|
---|
192 | rv = anObserverService->RemoveObserver(bObserver, topicB.get());
|
---|
193 | fSuccess = fSuccess && testResult(rv);
|
---|
194 | printf("Removing Observer-B (topic-B)...\n");
|
---|
195 | rv = anObserverService->RemoveObserver(bObserver, topicA.get());
|
---|
196 | fSuccess = fSuccess && testResult(rv);
|
---|
197 |
|
---|
198 | /* Cleanup: */
|
---|
199 | nsrefcnt refs = bObserver->Release();
|
---|
200 | fSuccess = fSuccess && refs == 0;
|
---|
201 | if (refs != 0)
|
---|
202 | printf("bObserver->Release() -> %d, expected 0\n", (int)refs);
|
---|
203 |
|
---|
204 | refs = aObserver->Release();
|
---|
205 | fSuccess = fSuccess && refs == 0;
|
---|
206 | if (refs != 0)
|
---|
207 | printf("aObserver->Release() -> %d, expected 0\n", (int)refs);
|
---|
208 | }
|
---|
209 |
|
---|
210 | return fSuccess ? 0 : 1;
|
---|
211 | }
|
---|