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
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
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.
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 "nsISupports.h"
40 #include "nsIComponentManager.h"
41 #include "nsIObserverService.h"
42 #include "nsIObserver.h"
43 #include "nsIEnumerator.h"
44 #include "nsStringGlue.h"
45 #include "nsWeakReference.h"
46 #include "nsComponentManagerUtils.h"
50 static nsIObserverService
*anObserverService
= NULL
;
52 static void testResult( nsresult rv
) {
53 if ( NS_SUCCEEDED( rv
) ) {
56 printf("...failed, rv=0x%x\n", (int)rv
);
61 void printString(nsString
&str
) {
62 printf("%s", NS_ConvertUTF16toUTF8(str
).get());
65 class TestObserver
: public nsIObserver
, public nsSupportsWeakReference
{
67 TestObserver( const nsAString
&name
)
79 NS_IMPL_ISUPPORTS2( TestObserver
, nsIObserver
, nsISupportsWeakReference
)
82 TestObserver::Observe( nsISupports
*aSubject
,
84 const PRUnichar
*someData
) {
85 nsCString
topic( aTopic
);
86 nsString
data( someData
);
88 The annoying double-cast below is to work around an annoying bug in
89 the compiler currently used on wensleydale. This is a test.
92 printf(" has observed something: subject@%p", (void*)aSubject
);
94 printString(reinterpret_cast<TestObserver
*>(reinterpret_cast<void*>(aSubject
))->mName
);
95 printf(" aTopic=%s", topic
.get());
102 int main(int argc
, char *argv
[])
104 nsCString topicA
; topicA
.Assign( "topic-A" );
105 nsCString topicB
; topicB
.Assign( "topic-B" );
108 nsresult res
= CallCreateInstance("@mozilla.org/observer-service;1", &anObserverService
);
112 nsIObserver
*aObserver
= new TestObserver(NS_LITERAL_STRING("Observer-A"));
114 nsIObserver
*bObserver
= new TestObserver(NS_LITERAL_STRING("Observer-B"));
117 printf("Adding Observer-A as observer of topic-A...\n");
118 rv
= anObserverService
->AddObserver(aObserver
, topicA
.get(), PR_FALSE
);
121 printf("Adding Observer-B as observer of topic-A...\n");
122 rv
= anObserverService
->AddObserver(bObserver
, topicA
.get(), PR_FALSE
);
125 printf("Adding Observer-B as observer of topic-B...\n");
126 rv
= anObserverService
->AddObserver(bObserver
, topicB
.get(), PR_FALSE
);
129 printf("Testing Notify(observer-A, topic-A)...\n");
130 rv
= anObserverService
->NotifyObservers( aObserver
,
132 NS_LITERAL_STRING("Testing Notify(observer-A, topic-A)").get() );
135 printf("Testing Notify(observer-B, topic-B)...\n");
136 rv
= anObserverService
->NotifyObservers( bObserver
,
138 NS_LITERAL_STRING("Testing Notify(observer-B, topic-B)").get() );
141 printf("Testing EnumerateObserverList (for topic-A)...\n");
142 nsCOMPtr
<nsISimpleEnumerator
> e
;
143 rv
= anObserverService
->EnumerateObservers(topicA
.get(), getter_AddRefs(e
));
147 printf("Enumerating observers of topic-A...\n");
149 nsCOMPtr
<nsIObserver
> observer
;
150 PRBool loop
= PR_TRUE
;
151 while( NS_SUCCEEDED(e
->HasMoreElements(&loop
)) && loop
)
153 e
->GetNext(getter_AddRefs(observer
));
154 printf("Calling observe on enumerated observer ");
155 printString(reinterpret_cast<TestObserver
*>
156 (reinterpret_cast<void*>(observer
.get()))->mName
);
158 rv
= observer
->Observe( observer
,
160 NS_LITERAL_STRING("during enumeration").get() );
164 printf("...done enumerating observers of topic-A\n");
166 printf("Removing Observer-A...\n");
167 rv
= anObserverService
->RemoveObserver(aObserver
, topicA
.get());
171 printf("Removing Observer-B (topic-A)...\n");
172 rv
= anObserverService
->RemoveObserver(bObserver
, topicB
.get());
174 printf("Removing Observer-B (topic-B)...\n");
175 rv
= anObserverService
->RemoveObserver(bObserver
, topicA
.get());