2001-09-29 00:14:13 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: NPL 1.1/GPL 2.0/LGPL 2.1
|
1999-05-27 12:50:52 +04:00
|
|
|
*
|
2001-09-29 00:14:13 +04:00
|
|
|
* The contents of this file are subject to the Netscape Public License
|
|
|
|
* Version 1.1 (the "License"); you may not use this file except in
|
|
|
|
* compliance with the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/NPL/
|
1999-05-27 12:50:52 +04:00
|
|
|
*
|
2001-09-29 00:14:13 +04:00
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
1999-05-27 12:50:52 +04:00
|
|
|
*
|
1999-11-06 06:43:54 +03:00
|
|
|
* The Original Code is mozilla.org code.
|
|
|
|
*
|
2001-09-29 00:14:13 +04:00
|
|
|
* The Initial Developer of the Original Code is
|
|
|
|
* Netscape Communications Corporation.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 1998
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
1999-11-06 06:43:54 +03:00
|
|
|
*
|
2001-09-29 00:14:13 +04:00
|
|
|
* Contributor(s):
|
1999-12-01 03:21:53 +03:00
|
|
|
* Pierre Phaneuf <pp@ludusdesign.com>
|
2001-09-29 00:14:13 +04:00
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
|
|
|
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
* use your version of this file under the terms of the NPL, indicate your
|
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
|
|
|
* the terms of any one of the NPL, the GPL or the LGPL.
|
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
1999-05-27 12:50:52 +04:00
|
|
|
|
1999-04-07 00:54:09 +04:00
|
|
|
#define NS_IMPL_IDS
|
|
|
|
#include "nsISupports.h"
|
2001-09-07 01:13:11 +04:00
|
|
|
#include "nsIComponentManager.h"
|
1999-04-07 00:54:09 +04:00
|
|
|
#include "nsIObserverService.h"
|
|
|
|
#include "nsIObserver.h"
|
2001-10-20 00:52:59 +04:00
|
|
|
#include "nsIEnumerator.h"
|
1999-04-07 00:54:09 +04:00
|
|
|
#include "nsString.h"
|
2001-09-29 12:28:41 +04:00
|
|
|
#include "nsReadableUtils.h"
|
1999-05-29 04:51:17 +04:00
|
|
|
#include "prprf.h"
|
1999-07-17 12:17:54 +04:00
|
|
|
#include <iostream.h>
|
2001-03-11 21:10:58 +03:00
|
|
|
#include <iomanip.h> // needed for libstdc++-v3
|
1999-12-07 17:06:36 +03:00
|
|
|
#include "nsWeakReference.h"
|
1999-04-07 00:54:09 +04:00
|
|
|
|
1999-05-29 04:51:17 +04:00
|
|
|
static nsIObserverService *anObserverService = NULL;
|
|
|
|
|
|
|
|
static void testResult( nsresult rv ) {
|
|
|
|
if ( NS_SUCCEEDED( rv ) ) {
|
|
|
|
cout << "...ok" << endl;
|
|
|
|
} else {
|
|
|
|
cout << "...failed, rv=0x" << hex << (int)rv << endl;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
extern ostream &operator<<( ostream &s, nsString &str ) {
|
2001-09-29 12:28:41 +04:00
|
|
|
const char *cstr = ToNewCString(str);
|
1999-05-29 04:51:17 +04:00
|
|
|
s << cstr;
|
|
|
|
delete [] (char*)cstr;
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
1999-12-07 17:06:36 +03:00
|
|
|
class TestObserver : public nsIObserver, public nsSupportsWeakReference {
|
1999-05-29 04:51:17 +04:00
|
|
|
public:
|
2000-04-26 04:55:37 +04:00
|
|
|
TestObserver( const nsString &name = NS_ConvertASCIItoUCS2("unknown") )
|
1999-05-29 04:51:17 +04:00
|
|
|
: mName( name ) {
|
|
|
|
NS_INIT_REFCNT();
|
|
|
|
}
|
1999-06-21 05:59:44 +04:00
|
|
|
virtual ~TestObserver() {}
|
1999-05-29 04:51:17 +04:00
|
|
|
NS_DECL_ISUPPORTS
|
1999-08-22 13:18:49 +04:00
|
|
|
NS_DECL_NSIOBSERVER
|
1999-05-29 04:51:17 +04:00
|
|
|
nsString mName;
|
|
|
|
};
|
|
|
|
|
1999-12-07 17:06:36 +03:00
|
|
|
NS_IMPL_ISUPPORTS2( TestObserver, nsIObserver, nsISupportsWeakReference );
|
1999-05-29 04:51:17 +04:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
TestObserver::Observe( nsISupports *aSubject,
|
2001-10-20 00:52:59 +04:00
|
|
|
const char *aTopic,
|
1999-05-29 04:51:17 +04:00
|
|
|
const PRUnichar *someData ) {
|
2001-10-20 00:52:59 +04:00
|
|
|
nsCString topic( aTopic );
|
1999-05-29 04:51:17 +04:00
|
|
|
nsString data( someData );
|
1999-12-07 17:46:35 +03:00
|
|
|
/*
|
|
|
|
The annoying double-cast below is to work around an annoying bug in
|
|
|
|
the compiler currently used on wensleydale. This is a test.
|
|
|
|
*/
|
1999-05-29 04:51:17 +04:00
|
|
|
cout << mName << " has observed something: subject@" << (void*)aSubject
|
1999-12-07 18:26:41 +03:00
|
|
|
<< " name=" << NS_REINTERPRET_CAST(TestObserver*, NS_REINTERPRET_CAST(void*, aSubject))->mName
|
2001-10-29 12:12:39 +03:00
|
|
|
<< " aTopic=" << topic.get()
|
1999-05-29 04:51:17 +04:00
|
|
|
<< " someData=" << data << endl;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
1999-04-07 00:54:09 +04:00
|
|
|
|
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
2001-10-20 00:52:59 +04:00
|
|
|
nsCString topicA; topicA.Assign( "topic-A" );
|
|
|
|
nsCString topicB; topicB.Assign( "topic-B" );
|
1999-04-07 00:54:09 +04:00
|
|
|
nsresult rv;
|
|
|
|
|
2001-10-23 02:01:27 +04:00
|
|
|
nsresult res = nsComponentManager::CreateInstance("@mozilla.org/observer-service;1",
|
1999-05-27 12:50:52 +04:00
|
|
|
NULL,
|
1999-12-01 03:21:53 +03:00
|
|
|
NS_GET_IID(nsIObserverService),
|
1999-05-27 12:50:52 +04:00
|
|
|
(void **) &anObserverService);
|
1999-04-07 00:54:09 +04:00
|
|
|
|
1999-05-27 12:50:52 +04:00
|
|
|
if (res == NS_OK) {
|
1999-04-07 00:54:09 +04:00
|
|
|
|
2000-04-26 04:55:37 +04:00
|
|
|
nsIObserver *aObserver = new TestObserver(NS_ConvertASCIItoUCS2("Observer-A"));
|
1999-05-29 04:51:17 +04:00
|
|
|
aObserver->AddRef();
|
2000-04-26 04:55:37 +04:00
|
|
|
nsIObserver *bObserver = new TestObserver(NS_ConvertASCIItoUCS2("Observer-B"));
|
1999-05-29 04:51:17 +04:00
|
|
|
bObserver->AddRef();
|
1999-04-07 00:54:09 +04:00
|
|
|
|
1999-05-29 04:51:17 +04:00
|
|
|
cout << "Adding Observer-A as observer of topic-A..." << endl;
|
2001-10-20 00:52:59 +04:00
|
|
|
rv = anObserverService->AddObserver(aObserver, topicA.get(), PR_FALSE);
|
1999-05-29 04:51:17 +04:00
|
|
|
testResult(rv);
|
1999-04-20 18:50:47 +04:00
|
|
|
|
1999-05-29 04:51:17 +04:00
|
|
|
cout << "Adding Observer-B as observer of topic-A..." << endl;
|
2001-10-20 00:52:59 +04:00
|
|
|
rv = anObserverService->AddObserver(bObserver, topicA.get(), PR_FALSE);
|
1999-05-29 04:51:17 +04:00
|
|
|
testResult(rv);
|
|
|
|
|
|
|
|
cout << "Adding Observer-B as observer of topic-B..." << endl;
|
2001-10-20 00:52:59 +04:00
|
|
|
rv = anObserverService->AddObserver(bObserver, topicB.get(), PR_FALSE);
|
1999-05-29 04:51:17 +04:00
|
|
|
testResult(rv);
|
|
|
|
|
|
|
|
cout << "Testing Notify(observer-A, topic-A)..." << endl;
|
2001-10-20 00:52:59 +04:00
|
|
|
rv = anObserverService->NotifyObservers( aObserver,
|
2001-06-30 15:02:25 +04:00
|
|
|
topicA.get(),
|
|
|
|
NS_ConvertASCIItoUCS2("Testing Notify(observer-A, topic-A)").get() );
|
1999-05-29 04:51:17 +04:00
|
|
|
testResult(rv);
|
|
|
|
|
|
|
|
cout << "Testing Notify(observer-B, topic-B)..." << endl;
|
2001-10-20 00:52:59 +04:00
|
|
|
rv = anObserverService->NotifyObservers( bObserver,
|
2001-06-30 15:02:25 +04:00
|
|
|
topicB.get(),
|
|
|
|
NS_ConvertASCIItoUCS2("Testing Notify(observer-B, topic-B)").get() );
|
1999-05-29 04:51:17 +04:00
|
|
|
testResult(rv);
|
|
|
|
|
|
|
|
cout << "Testing EnumerateObserverList (for topic-A)..." << endl;
|
2001-10-20 00:52:59 +04:00
|
|
|
nsCOMPtr<nsISimpleEnumerator> e;
|
|
|
|
rv = anObserverService->EnumerateObservers(topicA.get(), getter_AddRefs(e));
|
|
|
|
|
1999-05-29 04:51:17 +04:00
|
|
|
testResult(rv);
|
|
|
|
|
|
|
|
cout << "Enumerating observers of topic-A..." << endl;
|
2001-10-20 00:52:59 +04:00
|
|
|
if ( e ) {
|
|
|
|
nsCOMPtr<nsIObserver> observer;
|
|
|
|
PRBool loop = PR_TRUE;
|
|
|
|
while( NS_SUCCEEDED(e->HasMoreElements(&loop)) && loop)
|
|
|
|
{
|
|
|
|
e->GetNext(getter_AddRefs(observer));
|
|
|
|
cout << "Calling observe on enumerated observer "
|
|
|
|
<< NS_REINTERPRET_CAST(TestObserver*, NS_REINTERPRET_CAST(void*, observer.get()))->mName << "..." << endl;
|
|
|
|
rv = observer->Observe( observer,
|
|
|
|
topicA.get(),
|
|
|
|
NS_ConvertASCIItoUCS2("during enumeration").get() );
|
|
|
|
testResult(rv);
|
|
|
|
}
|
1999-05-27 12:50:52 +04:00
|
|
|
}
|
1999-05-29 04:51:17 +04:00
|
|
|
cout << "...done enumerating observers of topic-A" << endl;
|
1999-04-07 00:54:09 +04:00
|
|
|
|
1999-05-29 04:51:17 +04:00
|
|
|
cout << "Removing Observer-A..." << endl;
|
2001-06-30 15:02:25 +04:00
|
|
|
rv = anObserverService->RemoveObserver(aObserver, topicA.get());
|
1999-05-29 04:51:17 +04:00
|
|
|
testResult(rv);
|
1999-04-20 18:50:47 +04:00
|
|
|
|
|
|
|
|
1999-05-29 04:51:17 +04:00
|
|
|
cout << "Removing Observer-B (topic-A)..." << endl;
|
2001-06-30 15:02:25 +04:00
|
|
|
rv = anObserverService->RemoveObserver(bObserver, topicB.get());
|
1999-05-29 04:51:17 +04:00
|
|
|
testResult(rv);
|
|
|
|
cout << "Removing Observer-B (topic-B)..." << endl;
|
2001-06-30 15:02:25 +04:00
|
|
|
rv = anObserverService->RemoveObserver(bObserver, topicA.get());
|
1999-05-29 04:51:17 +04:00
|
|
|
testResult(rv);
|
1999-05-27 12:50:52 +04:00
|
|
|
|
|
|
|
}
|
|
|
|
return NS_OK;
|
1999-04-07 00:54:09 +04:00
|
|
|
}
|