2001-09-29 00:14:13 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
2004-04-18 18:21:17 +04:00
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
1999-05-27 12:50:52 +04:00
|
|
|
*
|
2004-04-18 18:21:17 +04:00
|
|
|
* The contents of this file are subject to the Mozilla 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/MPL/
|
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.
|
|
|
|
*
|
2004-04-18 18:21:17 +04:00
|
|
|
* The Initial Developer of the Original Code is
|
2001-09-29 00:14:13 +04:00
|
|
|
* 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
|
2004-04-18 18:21:17 +04:00
|
|
|
* either of 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"),
|
2001-09-29 00:14:13 +04:00
|
|
|
* 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
|
2004-04-18 18:21:17 +04:00
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
2001-09-29 00:14:13 +04:00
|
|
|
* 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
|
2004-04-18 18:21:17 +04:00
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
2001-09-29 00:14:13 +04:00
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
1999-05-27 12:50:52 +04:00
|
|
|
|
1999-04-07 00:54:09 +04:00
|
|
|
#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-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 ) ) {
|
2004-02-07 05:41:10 +03:00
|
|
|
printf("...ok\n");
|
1999-05-29 04:51:17 +04:00
|
|
|
} else {
|
2004-02-07 05:41:10 +03:00
|
|
|
printf("...failed, rv=0x%x\n", (int)rv);
|
1999-05-29 04:51:17 +04:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2004-02-07 05:41:10 +03:00
|
|
|
void printString(nsString &str) {
|
2001-09-29 12:28:41 +04:00
|
|
|
const char *cstr = ToNewCString(str);
|
2004-02-07 05:41:10 +03:00
|
|
|
printf("%s", cstr);
|
1999-05-29 04:51:17 +04:00
|
|
|
delete [] (char*)cstr;
|
|
|
|
}
|
|
|
|
|
1999-12-07 17:06:36 +03:00
|
|
|
class TestObserver : public nsIObserver, public nsSupportsWeakReference {
|
1999-05-29 04:51:17 +04:00
|
|
|
public:
|
2001-11-14 20:27:11 +03:00
|
|
|
TestObserver( const nsAString &name )
|
1999-05-29 04:51:17 +04:00
|
|
|
: mName( name ) {
|
|
|
|
}
|
|
|
|
NS_DECL_ISUPPORTS
|
1999-08-22 13:18:49 +04:00
|
|
|
NS_DECL_NSIOBSERVER
|
2004-01-15 09:14:18 +03:00
|
|
|
|
1999-05-29 04:51:17 +04:00
|
|
|
nsString mName;
|
2004-01-15 09:14:18 +03:00
|
|
|
|
|
|
|
private:
|
|
|
|
~TestObserver() {}
|
1999-05-29 04:51:17 +04:00
|
|
|
};
|
|
|
|
|
2003-09-08 04:31:50 +04: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.
|
|
|
|
*/
|
2004-02-07 05:41:10 +03:00
|
|
|
printString(mName);
|
|
|
|
printf(" has observed something: subject@%p", (void*)aSubject);
|
|
|
|
printf(" name=");
|
|
|
|
printString(NS_REINTERPRET_CAST(TestObserver*, NS_REINTERPRET_CAST(void*, aSubject))->mName);
|
|
|
|
printf(" aTopic=%s", topic.get());
|
|
|
|
printf(" someData=");
|
|
|
|
printString(data);
|
|
|
|
printf("\n");
|
1999-05-29 04:51:17 +04:00
|
|
|
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;
|
|
|
|
|
2004-11-01 21:50:36 +03:00
|
|
|
nsresult res = CallCreateInstance("@mozilla.org/observer-service;1", &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
|
|
|
|
2001-11-14 04:33:42 +03:00
|
|
|
nsIObserver *aObserver = new TestObserver(NS_LITERAL_STRING("Observer-A"));
|
1999-05-29 04:51:17 +04:00
|
|
|
aObserver->AddRef();
|
2001-11-14 04:33:42 +03:00
|
|
|
nsIObserver *bObserver = new TestObserver(NS_LITERAL_STRING("Observer-B"));
|
1999-05-29 04:51:17 +04:00
|
|
|
bObserver->AddRef();
|
1999-04-07 00:54:09 +04:00
|
|
|
|
2004-02-07 05:41:10 +03:00
|
|
|
printf("Adding Observer-A as observer of topic-A...\n");
|
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
|
|
|
|
2004-02-07 05:41:10 +03:00
|
|
|
printf("Adding Observer-B as observer of topic-A...\n");
|
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);
|
|
|
|
|
2004-02-07 05:41:10 +03:00
|
|
|
printf("Adding Observer-B as observer of topic-B...\n");
|
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);
|
|
|
|
|
2004-02-07 05:41:10 +03:00
|
|
|
printf("Testing Notify(observer-A, topic-A)...\n");
|
2001-10-20 00:52:59 +04:00
|
|
|
rv = anObserverService->NotifyObservers( aObserver,
|
2001-06-30 15:02:25 +04:00
|
|
|
topicA.get(),
|
2001-11-14 04:33:42 +03:00
|
|
|
NS_LITERAL_STRING("Testing Notify(observer-A, topic-A)").get() );
|
1999-05-29 04:51:17 +04:00
|
|
|
testResult(rv);
|
|
|
|
|
2004-02-07 05:41:10 +03:00
|
|
|
printf("Testing Notify(observer-B, topic-B)...\n");
|
2001-10-20 00:52:59 +04:00
|
|
|
rv = anObserverService->NotifyObservers( bObserver,
|
2001-06-30 15:02:25 +04:00
|
|
|
topicB.get(),
|
2001-11-14 04:33:42 +03:00
|
|
|
NS_LITERAL_STRING("Testing Notify(observer-B, topic-B)").get() );
|
1999-05-29 04:51:17 +04:00
|
|
|
testResult(rv);
|
|
|
|
|
2004-02-07 05:41:10 +03:00
|
|
|
printf("Testing EnumerateObserverList (for topic-A)...\n");
|
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);
|
|
|
|
|
2004-02-07 05:41:10 +03:00
|
|
|
printf("Enumerating observers of topic-A...\n");
|
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));
|
2004-02-07 05:41:10 +03:00
|
|
|
printf("Calling observe on enumerated observer ");
|
|
|
|
printString(NS_REINTERPRET_CAST(TestObserver*,
|
|
|
|
NS_REINTERPRET_CAST(void*, observer.get()))->mName);
|
|
|
|
printf("...\n");
|
2001-10-20 00:52:59 +04:00
|
|
|
rv = observer->Observe( observer,
|
|
|
|
topicA.get(),
|
2001-11-14 04:33:42 +03:00
|
|
|
NS_LITERAL_STRING("during enumeration").get() );
|
2001-10-20 00:52:59 +04:00
|
|
|
testResult(rv);
|
|
|
|
}
|
1999-05-27 12:50:52 +04:00
|
|
|
}
|
2004-02-07 05:41:10 +03:00
|
|
|
printf("...done enumerating observers of topic-A\n");
|
1999-04-07 00:54:09 +04:00
|
|
|
|
2004-02-07 05:41:10 +03:00
|
|
|
printf("Removing Observer-A...\n");
|
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
|
|
|
|
|
|
|
|
2004-02-07 05:41:10 +03:00
|
|
|
printf("Removing Observer-B (topic-A)...\n");
|
2001-06-30 15:02:25 +04:00
|
|
|
rv = anObserverService->RemoveObserver(bObserver, topicB.get());
|
1999-05-29 04:51:17 +04:00
|
|
|
testResult(rv);
|
2004-02-07 05:41:10 +03:00
|
|
|
printf("Removing Observer-B (topic-B)...\n");
|
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
|
|
|
}
|