Back to index

lightning-sunbird  0.9+nobinonly
TestObserverService.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *   Pierre Phaneuf <pp@ludusdesign.com>
00024  *
00025  * Alternatively, the contents of this file may be used under the terms of
00026  * either of the GNU General Public License Version 2 or later (the "GPL"),
00027  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00028  * in which case the provisions of the GPL or the LGPL are applicable instead
00029  * of those above. If you wish to allow use of your version of this file only
00030  * under the terms of either the GPL or the LGPL, and not to allow others to
00031  * use your version of this file under the terms of the MPL, indicate your
00032  * decision by deleting the provisions above and replace them with the notice
00033  * and other provisions required by the GPL or the LGPL. If you do not delete
00034  * the provisions above, a recipient may use your version of this file under
00035  * the terms of any one of the MPL, the GPL or the LGPL.
00036  *
00037  * ***** END LICENSE BLOCK ***** */
00038 
00039 #include "nsISupports.h"
00040 #include "nsIComponentManager.h"
00041 #include "nsIObserverService.h"
00042 #include "nsIObserver.h"
00043 #include "nsIEnumerator.h"
00044 #include "nsString.h"
00045 #include "nsReadableUtils.h"
00046 #include "prprf.h"
00047 #include "nsWeakReference.h"
00048 
00049 static nsIObserverService *anObserverService = NULL;
00050 
00051 static void testResult( nsresult rv ) {
00052     if ( NS_SUCCEEDED( rv ) ) {
00053         printf("...ok\n");
00054     } else {
00055         printf("...failed, rv=0x%x\n", (int)rv);
00056     }
00057     return;
00058 }
00059 
00060 void printString(nsString &str) {
00061     const char *cstr = ToNewCString(str);
00062     printf("%s", cstr);
00063     delete [] (char*)cstr;
00064 }
00065 
00066 class TestObserver : public nsIObserver, public nsSupportsWeakReference {
00067 public:
00068     TestObserver( const nsAString &name )
00069         : mName( name ) {
00070     }
00071     NS_DECL_ISUPPORTS
00072     NS_DECL_NSIOBSERVER
00073 
00074     nsString mName;
00075 
00076 private:
00077     ~TestObserver() {}
00078 };
00079 
00080 NS_IMPL_ISUPPORTS2( TestObserver, nsIObserver, nsISupportsWeakReference )
00081 
00082 NS_IMETHODIMP
00083 TestObserver::Observe( nsISupports     *aSubject,
00084                        const char *aTopic,
00085                        const PRUnichar *someData ) {
00086     nsCString topic( aTopic );
00087     nsString data( someData );
00088        /*
00089               The annoying double-cast below is to work around an annoying bug in
00090               the compiler currently used on wensleydale.  This is a test.
00091        */
00092     printString(mName);
00093     printf(" has observed something: subject@%p", (void*)aSubject);
00094     printf(" name=");
00095     printString(NS_REINTERPRET_CAST(TestObserver*, NS_REINTERPRET_CAST(void*, aSubject))->mName);
00096     printf(" aTopic=%s", topic.get());
00097     printf(" someData=");
00098     printString(data);
00099     printf("\n");
00100     return NS_OK;
00101 }
00102 
00103 int main(int argc, char *argv[])
00104 {
00105     nsCString topicA; topicA.Assign( "topic-A" );
00106     nsCString topicB; topicB.Assign( "topic-B" );
00107     nsresult rv;
00108 
00109     nsresult res = CallCreateInstance("@mozilla.org/observer-service;1", &anObserverService);
00110        
00111     if (res == NS_OK) {
00112 
00113         nsIObserver *aObserver = new TestObserver(NS_LITERAL_STRING("Observer-A"));
00114         aObserver->AddRef();
00115         nsIObserver *bObserver = new TestObserver(NS_LITERAL_STRING("Observer-B"));
00116         bObserver->AddRef();
00117             
00118         printf("Adding Observer-A as observer of topic-A...\n");
00119         rv = anObserverService->AddObserver(aObserver, topicA.get(), PR_FALSE);
00120         testResult(rv);
00121  
00122         printf("Adding Observer-B as observer of topic-A...\n");
00123         rv = anObserverService->AddObserver(bObserver, topicA.get(), PR_FALSE);
00124         testResult(rv);
00125  
00126         printf("Adding Observer-B as observer of topic-B...\n");
00127         rv = anObserverService->AddObserver(bObserver, topicB.get(), PR_FALSE);
00128         testResult(rv);
00129 
00130         printf("Testing Notify(observer-A, topic-A)...\n");
00131         rv = anObserverService->NotifyObservers( aObserver,
00132                                    topicA.get(),
00133                                    NS_LITERAL_STRING("Testing Notify(observer-A, topic-A)").get() );
00134         testResult(rv);
00135 
00136         printf("Testing Notify(observer-B, topic-B)...\n");
00137         rv = anObserverService->NotifyObservers( bObserver,
00138                                    topicB.get(),
00139                                    NS_LITERAL_STRING("Testing Notify(observer-B, topic-B)").get() );
00140         testResult(rv);
00141  
00142         printf("Testing EnumerateObserverList (for topic-A)...\n");
00143         nsCOMPtr<nsISimpleEnumerator> e;
00144         rv = anObserverService->EnumerateObservers(topicA.get(), getter_AddRefs(e));
00145 
00146         testResult(rv);
00147 
00148         printf("Enumerating observers of topic-A...\n");
00149         if ( e ) {
00150           nsCOMPtr<nsIObserver> observer;
00151           PRBool loop = PR_TRUE;
00152           while( NS_SUCCEEDED(e->HasMoreElements(&loop)) && loop) 
00153           {
00154               e->GetNext(getter_AddRefs(observer));
00155               printf("Calling observe on enumerated observer ");
00156               printString(NS_REINTERPRET_CAST(TestObserver*,
00157                                               NS_REINTERPRET_CAST(void*, observer.get()))->mName);
00158               printf("...\n");
00159               rv = observer->Observe( observer, 
00160                                       topicA.get(), 
00161                                       NS_LITERAL_STRING("during enumeration").get() );
00162               testResult(rv);
00163           }
00164         }
00165         printf("...done enumerating observers of topic-A\n");
00166 
00167         printf("Removing Observer-A...\n");
00168         rv = anObserverService->RemoveObserver(aObserver, topicA.get());
00169         testResult(rv);
00170 
00171 
00172         printf("Removing Observer-B (topic-A)...\n");
00173         rv = anObserverService->RemoveObserver(bObserver, topicB.get());
00174         testResult(rv);
00175         printf("Removing Observer-B (topic-B)...\n");
00176         rv = anObserverService->RemoveObserver(bObserver, topicA.get());
00177         testResult(rv);
00178        
00179     }
00180     return NS_OK;
00181 }