CLOSED TREE: TraceMonkey merge head. (a=blockers)
[mozilla-central.git] / xpcom / tests / TestObserverService.cpp
blob32f07fbad7db8ddf76190738a5fbfad1fb935e37
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
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 "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"
48 #include <stdio.h>
50 static nsIObserverService *anObserverService = NULL;
52 static void testResult( nsresult rv ) {
53 if ( NS_SUCCEEDED( rv ) ) {
54 printf("...ok\n");
55 } else {
56 printf("...failed, rv=0x%x\n", (int)rv);
58 return;
61 void printString(nsString &str) {
62 printf("%s", NS_ConvertUTF16toUTF8(str).get());
65 class TestObserver : public nsIObserver, public nsSupportsWeakReference {
66 public:
67 TestObserver( const nsAString &name )
68 : mName( name ) {
70 NS_DECL_ISUPPORTS
71 NS_DECL_NSIOBSERVER
73 nsString mName;
75 private:
76 ~TestObserver() {}
79 NS_IMPL_ISUPPORTS2( TestObserver, nsIObserver, nsISupportsWeakReference )
81 NS_IMETHODIMP
82 TestObserver::Observe( nsISupports *aSubject,
83 const char *aTopic,
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.
91 printString(mName);
92 printf(" has observed something: subject@%p", (void*)aSubject);
93 printf(" name=");
94 printString(reinterpret_cast<TestObserver*>(reinterpret_cast<void*>(aSubject))->mName);
95 printf(" aTopic=%s", topic.get());
96 printf(" someData=");
97 printString(data);
98 printf("\n");
99 return NS_OK;
102 int main(int argc, char *argv[])
104 nsCString topicA; topicA.Assign( "topic-A" );
105 nsCString topicB; topicB.Assign( "topic-B" );
106 nsresult rv;
108 nsresult res = CallCreateInstance("@mozilla.org/observer-service;1", &anObserverService);
110 if (res == NS_OK) {
112 nsIObserver *aObserver = new TestObserver(NS_LITERAL_STRING("Observer-A"));
113 aObserver->AddRef();
114 nsIObserver *bObserver = new TestObserver(NS_LITERAL_STRING("Observer-B"));
115 bObserver->AddRef();
117 printf("Adding Observer-A as observer of topic-A...\n");
118 rv = anObserverService->AddObserver(aObserver, topicA.get(), PR_FALSE);
119 testResult(rv);
121 printf("Adding Observer-B as observer of topic-A...\n");
122 rv = anObserverService->AddObserver(bObserver, topicA.get(), PR_FALSE);
123 testResult(rv);
125 printf("Adding Observer-B as observer of topic-B...\n");
126 rv = anObserverService->AddObserver(bObserver, topicB.get(), PR_FALSE);
127 testResult(rv);
129 printf("Testing Notify(observer-A, topic-A)...\n");
130 rv = anObserverService->NotifyObservers( aObserver,
131 topicA.get(),
132 NS_LITERAL_STRING("Testing Notify(observer-A, topic-A)").get() );
133 testResult(rv);
135 printf("Testing Notify(observer-B, topic-B)...\n");
136 rv = anObserverService->NotifyObservers( bObserver,
137 topicB.get(),
138 NS_LITERAL_STRING("Testing Notify(observer-B, topic-B)").get() );
139 testResult(rv);
141 printf("Testing EnumerateObserverList (for topic-A)...\n");
142 nsCOMPtr<nsISimpleEnumerator> e;
143 rv = anObserverService->EnumerateObservers(topicA.get(), getter_AddRefs(e));
145 testResult(rv);
147 printf("Enumerating observers of topic-A...\n");
148 if ( e ) {
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);
157 printf("...\n");
158 rv = observer->Observe( observer,
159 topicA.get(),
160 NS_LITERAL_STRING("during enumeration").get() );
161 testResult(rv);
164 printf("...done enumerating observers of topic-A\n");
166 printf("Removing Observer-A...\n");
167 rv = anObserverService->RemoveObserver(aObserver, topicA.get());
168 testResult(rv);
171 printf("Removing Observer-B (topic-A)...\n");
172 rv = anObserverService->RemoveObserver(bObserver, topicB.get());
173 testResult(rv);
174 printf("Removing Observer-B (topic-B)...\n");
175 rv = anObserverService->RemoveObserver(bObserver, topicA.get());
176 testResult(rv);
179 return NS_OK;