1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/xpcom/tests/TestObserverService.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,152 @@ 1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +#include "nsISupports.h" 1.10 +#include "nsIComponentManager.h" 1.11 +#include "nsIObserverService.h" 1.12 +#include "nsIObserver.h" 1.13 +#include "nsISimpleEnumerator.h" 1.14 +#include "nsStringGlue.h" 1.15 +#include "nsWeakReference.h" 1.16 +#include "nsComponentManagerUtils.h" 1.17 +#include "mozilla/Attributes.h" 1.18 + 1.19 +#include <stdio.h> 1.20 + 1.21 +static nsIObserverService *anObserverService = nullptr; 1.22 + 1.23 +static void testResult( nsresult rv ) { 1.24 + if ( NS_SUCCEEDED( rv ) ) { 1.25 + printf("...ok\n"); 1.26 + } else { 1.27 + printf("...failed, rv=0x%x\n", (int)rv); 1.28 + } 1.29 + return; 1.30 +} 1.31 + 1.32 +void printString(nsString &str) { 1.33 + printf("%s", NS_ConvertUTF16toUTF8(str).get()); 1.34 +} 1.35 + 1.36 +class TestObserver MOZ_FINAL : public nsIObserver, 1.37 + public nsSupportsWeakReference 1.38 +{ 1.39 +public: 1.40 + TestObserver( const nsAString &name ) 1.41 + : mName( name ) { 1.42 + } 1.43 + NS_DECL_ISUPPORTS 1.44 + NS_DECL_NSIOBSERVER 1.45 + 1.46 + nsString mName; 1.47 + 1.48 +private: 1.49 + ~TestObserver() {} 1.50 +}; 1.51 + 1.52 +NS_IMPL_ISUPPORTS( TestObserver, nsIObserver, nsISupportsWeakReference ) 1.53 + 1.54 +NS_IMETHODIMP 1.55 +TestObserver::Observe( nsISupports *aSubject, 1.56 + const char *aTopic, 1.57 + const char16_t *someData ) { 1.58 + nsCString topic( aTopic ); 1.59 + nsString data( someData ); 1.60 + /* 1.61 + The annoying double-cast below is to work around an annoying bug in 1.62 + the compiler currently used on wensleydale. This is a test. 1.63 + */ 1.64 + printString(mName); 1.65 + printf(" has observed something: subject@%p", (void*)aSubject); 1.66 + printf(" name="); 1.67 + printString(reinterpret_cast<TestObserver*>(reinterpret_cast<void*>(aSubject))->mName); 1.68 + printf(" aTopic=%s", topic.get()); 1.69 + printf(" someData="); 1.70 + printString(data); 1.71 + printf("\n"); 1.72 + return NS_OK; 1.73 +} 1.74 + 1.75 +int main(int argc, char *argv[]) 1.76 +{ 1.77 + nsCString topicA; topicA.Assign( "topic-A" ); 1.78 + nsCString topicB; topicB.Assign( "topic-B" ); 1.79 + nsresult rv; 1.80 + 1.81 + nsresult res = CallCreateInstance("@mozilla.org/observer-service;1", &anObserverService); 1.82 + 1.83 + if (res == NS_OK) { 1.84 + 1.85 + nsIObserver *aObserver = new TestObserver(NS_LITERAL_STRING("Observer-A")); 1.86 + aObserver->AddRef(); 1.87 + nsIObserver *bObserver = new TestObserver(NS_LITERAL_STRING("Observer-B")); 1.88 + bObserver->AddRef(); 1.89 + 1.90 + printf("Adding Observer-A as observer of topic-A...\n"); 1.91 + rv = anObserverService->AddObserver(aObserver, topicA.get(), false); 1.92 + testResult(rv); 1.93 + 1.94 + printf("Adding Observer-B as observer of topic-A...\n"); 1.95 + rv = anObserverService->AddObserver(bObserver, topicA.get(), false); 1.96 + testResult(rv); 1.97 + 1.98 + printf("Adding Observer-B as observer of topic-B...\n"); 1.99 + rv = anObserverService->AddObserver(bObserver, topicB.get(), false); 1.100 + testResult(rv); 1.101 + 1.102 + printf("Testing Notify(observer-A, topic-A)...\n"); 1.103 + rv = anObserverService->NotifyObservers( aObserver, 1.104 + topicA.get(), 1.105 + MOZ_UTF16("Testing Notify(observer-A, topic-A)") ); 1.106 + testResult(rv); 1.107 + 1.108 + printf("Testing Notify(observer-B, topic-B)...\n"); 1.109 + rv = anObserverService->NotifyObservers( bObserver, 1.110 + topicB.get(), 1.111 + MOZ_UTF16("Testing Notify(observer-B, topic-B)") ); 1.112 + testResult(rv); 1.113 + 1.114 + printf("Testing EnumerateObserverList (for topic-A)...\n"); 1.115 + nsCOMPtr<nsISimpleEnumerator> e; 1.116 + rv = anObserverService->EnumerateObservers(topicA.get(), getter_AddRefs(e)); 1.117 + 1.118 + testResult(rv); 1.119 + 1.120 + printf("Enumerating observers of topic-A...\n"); 1.121 + if ( e ) { 1.122 + nsCOMPtr<nsIObserver> observer; 1.123 + bool loop = true; 1.124 + while( NS_SUCCEEDED(e->HasMoreElements(&loop)) && loop) 1.125 + { 1.126 + nsCOMPtr<nsISupports> supports; 1.127 + e->GetNext(getter_AddRefs(supports)); 1.128 + observer = do_QueryInterface(supports); 1.129 + printf("Calling observe on enumerated observer "); 1.130 + printString(reinterpret_cast<TestObserver*> 1.131 + (reinterpret_cast<void*>(observer.get()))->mName); 1.132 + printf("...\n"); 1.133 + rv = observer->Observe( observer, 1.134 + topicA.get(), 1.135 + MOZ_UTF16("during enumeration") ); 1.136 + testResult(rv); 1.137 + } 1.138 + } 1.139 + printf("...done enumerating observers of topic-A\n"); 1.140 + 1.141 + printf("Removing Observer-A...\n"); 1.142 + rv = anObserverService->RemoveObserver(aObserver, topicA.get()); 1.143 + testResult(rv); 1.144 + 1.145 + 1.146 + printf("Removing Observer-B (topic-A)...\n"); 1.147 + rv = anObserverService->RemoveObserver(bObserver, topicB.get()); 1.148 + testResult(rv); 1.149 + printf("Removing Observer-B (topic-B)...\n"); 1.150 + rv = anObserverService->RemoveObserver(bObserver, topicA.get()); 1.151 + testResult(rv); 1.152 + 1.153 + } 1.154 + return 0; 1.155 +}