VirtualBox

source: vbox/trunk/src/libs/xpcom18a4/xpcom/tests/TestObserverService.cpp@ 65902

最後變更 在這個檔案從65902是 1,由 vboxsync 提交於 55 年 前

import

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Author Date Id Revision
檔案大小: 6.7 KB
 
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
4 *
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/
9 *
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.
14 *
15 * The Original Code is mozilla.org code.
16 *
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.
21 *
22 * Contributor(s):
23 * Pierre Phaneuf <[email protected]>
24 *
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.
36 *
37 * ***** END LICENSE BLOCK ***** */
38
39#include "nsISupports.h"
40#include "nsIComponentManager.h"
41#include "nsIObserverService.h"
42#include "nsIObserver.h"
43#include "nsIEnumerator.h"
44#include "nsString.h"
45#include "nsReadableUtils.h"
46#include "prprf.h"
47#include "nsWeakReference.h"
48
49static nsIObserverService *anObserverService = NULL;
50
51static void testResult( nsresult rv ) {
52 if ( NS_SUCCEEDED( rv ) ) {
53 printf("...ok\n");
54 } else {
55 printf("...failed, rv=0x%x\n", (int)rv);
56 }
57 return;
58}
59
60void printString(nsString &str) {
61 const char *cstr = ToNewCString(str);
62 printf("%s", cstr);
63 delete [] (char*)cstr;
64}
65
66class TestObserver : public nsIObserver, public nsSupportsWeakReference {
67public:
68 TestObserver( const nsAString &name )
69 : mName( name ) {
70 }
71 NS_DECL_ISUPPORTS
72 NS_DECL_NSIOBSERVER
73
74 nsString mName;
75
76private:
77 ~TestObserver() {}
78};
79
80NS_IMPL_ISUPPORTS2( TestObserver, nsIObserver, nsISupportsWeakReference )
81
82NS_IMETHODIMP
83TestObserver::Observe( nsISupports *aSubject,
84 const char *aTopic,
85 const PRUnichar *someData ) {
86 nsCString topic( aTopic );
87 nsString data( someData );
88 /*
89 The annoying double-cast below is to work around an annoying bug in
90 the compiler currently used on wensleydale. This is a test.
91 */
92 printString(mName);
93 printf(" has observed something: subject@%p", (void*)aSubject);
94 printf(" name=");
95 printString(NS_REINTERPRET_CAST(TestObserver*, NS_REINTERPRET_CAST(void*, aSubject))->mName);
96 printf(" aTopic=%s", topic.get());
97 printf(" someData=");
98 printString(data);
99 printf("\n");
100 return NS_OK;
101}
102
103int main(int argc, char *argv[])
104{
105 nsCString topicA; topicA.Assign( "topic-A" );
106 nsCString topicB; topicB.Assign( "topic-B" );
107 nsresult rv;
108
109 nsresult res = nsComponentManager::CreateInstance("@mozilla.org/observer-service;1",
110 NULL,
111 NS_GET_IID(nsIObserverService),
112 (void **) &anObserverService);
113
114 if (res == NS_OK) {
115
116 nsIObserver *aObserver = new TestObserver(NS_LITERAL_STRING("Observer-A"));
117 aObserver->AddRef();
118 nsIObserver *bObserver = new TestObserver(NS_LITERAL_STRING("Observer-B"));
119 bObserver->AddRef();
120
121 printf("Adding Observer-A as observer of topic-A...\n");
122 rv = anObserverService->AddObserver(aObserver, topicA.get(), PR_FALSE);
123 testResult(rv);
124
125 printf("Adding Observer-B as observer of topic-A...\n");
126 rv = anObserverService->AddObserver(bObserver, topicA.get(), PR_FALSE);
127 testResult(rv);
128
129 printf("Adding Observer-B as observer of topic-B...\n");
130 rv = anObserverService->AddObserver(bObserver, topicB.get(), PR_FALSE);
131 testResult(rv);
132
133 printf("Testing Notify(observer-A, topic-A)...\n");
134 rv = anObserverService->NotifyObservers( aObserver,
135 topicA.get(),
136 NS_LITERAL_STRING("Testing Notify(observer-A, topic-A)").get() );
137 testResult(rv);
138
139 printf("Testing Notify(observer-B, topic-B)...\n");
140 rv = anObserverService->NotifyObservers( bObserver,
141 topicB.get(),
142 NS_LITERAL_STRING("Testing Notify(observer-B, topic-B)").get() );
143 testResult(rv);
144
145 printf("Testing EnumerateObserverList (for topic-A)...\n");
146 nsCOMPtr<nsISimpleEnumerator> e;
147 rv = anObserverService->EnumerateObservers(topicA.get(), getter_AddRefs(e));
148
149 testResult(rv);
150
151 printf("Enumerating observers of topic-A...\n");
152 if ( e ) {
153 nsCOMPtr<nsIObserver> observer;
154 PRBool loop = PR_TRUE;
155 while( NS_SUCCEEDED(e->HasMoreElements(&loop)) && loop)
156 {
157 e->GetNext(getter_AddRefs(observer));
158 printf("Calling observe on enumerated observer ");
159 printString(NS_REINTERPRET_CAST(TestObserver*,
160 NS_REINTERPRET_CAST(void*, observer.get()))->mName);
161 printf("...\n");
162 rv = observer->Observe( observer,
163 topicA.get(),
164 NS_LITERAL_STRING("during enumeration").get() );
165 testResult(rv);
166 }
167 }
168 printf("...done enumerating observers of topic-A\n");
169
170 printf("Removing Observer-A...\n");
171 rv = anObserverService->RemoveObserver(aObserver, topicA.get());
172 testResult(rv);
173
174
175 printf("Removing Observer-B (topic-A)...\n");
176 rv = anObserverService->RemoveObserver(bObserver, topicB.get());
177 testResult(rv);
178 printf("Removing Observer-B (topic-B)...\n");
179 rv = anObserverService->RemoveObserver(bObserver, topicA.get());
180 testResult(rv);
181
182 }
183 return NS_OK;
184}
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette