Bug 470455 - test_database_sync_embed_visits.js leaks, r=sdwilsh
[wine-gecko.git] / embedding / qa / testembed / nsIObserServ.cpp
blobe64be6869a8366b1db5a71d272b97494c6e907f3
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
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/
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.
15 * The Original Code is mozilla.org code.
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.
22 * Contributor(s):
23 * David Epstein <depstein@netscape.com>
24 * Ashish Bhatt <ashishbhatt@netscape.com>
26 * Alternatively, the contents of this file may be used under the terms of
27 * either the GNU General Public License Version 2 or later (the "GPL"), or
28 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29 * in which case the provisions of the GPL or the LGPL are applicable instead
30 * of those above. If you wish to allow use of your version of this file only
31 * under the terms of either the GPL or the LGPL, and not to allow others to
32 * use your version of this file under the terms of the MPL, indicate your
33 * decision by deleting the provisions above and replace them with the notice
34 * and other provisions required by the GPL or the LGPL. If you do not delete
35 * the provisions above, a recipient may use your version of this file under
36 * the terms of any one of the MPL, the GPL or the LGPL.
38 * ***** END LICENSE BLOCK ***** */
40 // nsIObserServ.cpp: implementation of the CnsIObserServ class.
42 //////////////////////////////////////////////////////////////////////
44 #include "stdafx.h"
45 #include "nsIObserServ.h"
46 #include "QaUtils.h"
48 //////////////////////////////////////////////////////////////////////
49 // Construction/Destruction
50 //////////////////////////////////////////////////////////////////////
51 CnsIObserServ::CnsIObserServ()
54 mRefCnt = 1 ;
58 CnsIObserServ::~CnsIObserServ()
63 ObserverElement ObserverTable[] = {
64 {"profile-before-change", PR_TRUE},
65 {"profile-after-change", PR_FALSE},
66 {"profile-do-change", PR_TRUE},
67 {"text/xml", PR_FALSE},
68 {"application/xml", PR_TRUE},
69 {"htmlparser", PR_FALSE},
70 {"xmlparser", PR_TRUE},
71 {"memory-pressure", PR_FALSE},
72 {"quit-application", PR_TRUE},
73 {"login-failed", PR_FALSE}
74 };
77 void CnsIObserServ::OnStartTests(UINT nMenuID)
80 // Calls all or indivdual test cases on the basis of the
81 // option selected from menu.
83 switch(nMenuID)
86 case ID_INTERFACES_NSIOBSERVERSERVICE_RUNALLTESTS :
87 RunAllTests();
88 break;
90 case ID_INTERFACES_NSIOBSERVERSERVICE_ADDOBSERVERS :
91 AddObserversTest(2);
92 break;
94 case ID_INTERFACES_NSIOBSERVERSERVICE_ENUMERATEOBSERVERS :
95 QAOutput("Adding observers first", 2);
96 AddObserversTest(1);
97 EnumerateObserversTest(1);
98 break;
100 case ID_INTERFACES_NSIOBSERVERSERVICE_NOTIFYOBSERVERS :
101 NotifyObserversTest(1);
102 break;
104 case ID_INTERFACES_NSIOBSERVERSERVICE_REMOVEOBSERVERS :
105 QAOutput("Adding observers first.", 2);
106 AddObserversTest(1);
107 RemoveObserversTest(1);
108 break;
110 default :
111 AfxMessageBox("Menu handler not added for this menu item");
112 break;
116 void CnsIObserServ::RunAllTests()
118 AddObserversTest(1);
119 EnumerateObserversTest(1);
120 NotifyObserversTest(1);
121 RemoveObserversTest(1);
124 void CnsIObserServ::AddObserversTest(int displayType)
126 int i;
128 nsCOMPtr<nsIObserverService>observerService(do_GetService("@mozilla.org/observer-service;1",&rv));
129 RvTestResult(rv, "nsIObserverService object test", displayType);
130 RvTestResultDlg(rv, "nsIObserverService object test", true);
132 QAOutput("\n nsIObserverService::AddObserversTest().");
133 if (!observerService)
135 QAOutput("Can't get nsIObserverService object. Tests fail.");
136 return;
139 for (i=0; i<10; i++)
141 rv = observerService->AddObserver(this, ObserverTable[i].theTopic,
142 ObserverTable[i].theOwnsWeak);
143 FormatAndPrintOutput("The observer to be added = ", ObserverTable[i].theTopic, 1);
144 RvTestResult(rv, "AddObservers() test", displayType);
145 RvTestResultDlg(rv, "AddObservers() test");
149 void CnsIObserServ::RemoveObserversTest(int displayType)
151 int i;
153 nsCOMPtr<nsIObserverService>observerService(do_GetService("@mozilla.org/observer-service;1",&rv));
155 QAOutput("\n nsIObserverService::RemoveObserversTest().");
156 if (!observerService)
158 QAOutput("Can't get nsIObserverService object. Tests fail.");
159 return;
162 for (i=0; i<10; i++)
164 rv = observerService->RemoveObserver(this, ObserverTable[i].theTopic);
165 FormatAndPrintOutput("The observer to be removed = ", ObserverTable[i].theTopic, 1);
166 RvTestResult(rv, "RemoveObservers() test", displayType);
167 RvTestResultDlg(rv, "RemoveObservers() test");
172 void CnsIObserServ::NotifyObserversTest(int displayType)
174 PRInt32 i;
175 nsCOMPtr<nsIObserverService>observerService(do_GetService("@mozilla.org/observer-service;1",&rv));
177 QAOutput("\n nsIObserverService::NotifyObserversTest().");
179 if (!observerService)
181 QAOutput("Can't get nsIObserverService object. Tests fail.");
182 return;
185 for (i=0; i<10; i++)
187 FormatAndPrintOutput("The notified observer = ", ObserverTable[i].theTopic, 1);
188 rv = observerService->NotifyObservers(nsnull, ObserverTable[i].theTopic, 0);
189 RvTestResult(rv, "NotifyObservers() test", displayType);
190 RvTestResultDlg(rv, "NotifyObservers() test");
195 void CnsIObserServ::EnumerateObserversTest(int displayType)
197 PRInt32 i=0;
198 nsCOMPtr<nsIObserverService> observerService(do_GetService("@mozilla.org/observer-service;1",&rv));
199 nsCOMPtr<nsISimpleEnumerator> simpleEnum;
201 QAOutput("\n nsIObserverService::EnumerateObserversTest().");
202 if (!observerService)
204 QAOutput("Can't get nsIObserverService object. Tests fail.");
205 return;
208 for (i=0; i<10; i++)
210 // need to handle Simple Enumerator
212 rv = observerService->EnumerateObservers(ObserverTable[i].theTopic,
213 getter_AddRefs(simpleEnum));
215 RvTestResult(rv, "EnumerateObserversTest() test", displayType);
216 RvTestResultDlg(rv, "EnumerateObserversTest() test");
217 if (!simpleEnum)
219 QAOutput("Didn't get SimpleEnumerator object. Tests fail.");
220 return;
223 nsCOMPtr<nsIObserver> observer;
224 PRBool theLoop = PR_TRUE;
225 PRBool bLoop = PR_TRUE;
227 while( NS_SUCCEEDED(simpleEnum->HasMoreElements(&theLoop)) && bLoop)
229 simpleEnum->GetNext(getter_AddRefs(observer));
231 if (!observer)
233 QAOutput("Didn't get the observer object. Tests fail.");
234 return;
236 rv = observer->Observe(observer, ObserverTable[i].theTopic, 0);
237 RvTestResult(rv, "nsIObserver() test", 1);
238 RvTestResultDlg(rv, "nsIObserver() test");
240 // compare 'this' with observer object
241 if( this == reinterpret_cast<CnsIObserServ*>(reinterpret_cast<void*>(observer.get())))
243 QAOutput("observers match. Test passes.");
244 bLoop = PR_FALSE;
246 else
247 QAOutput("observers don't match. Test fails.");
253 NS_IMPL_THREADSAFE_ISUPPORTS2(CnsIObserServ, nsIObserver, nsISupportsWeakReference)
256 NS_IMETHODIMP CnsIObserServ::Observe(nsISupports *aSubject, const char *aTopic, const PRUnichar *someData)
258 nsresult rv = NS_OK;
260 if (nsCRT::strcmp(aTopic, "profile-before-change") == 0)
262 QAOutput("Observed 'profile-before-change'.");
264 else if (nsCRT::strcmp(aTopic, "profile-after-change") == 0)
266 QAOutput("Observed 'profile-after-change'.");
268 else if (nsCRT::strcmp(aTopic, "profile-do-change") == 0)
270 QAOutput("Observed 'profile-do-change'.");
272 else if (nsCRT::strcmp(aTopic, "text/xml") == 0)
274 QAOutput("Observed 'text/xml'.");
276 else if (nsCRT::strcmp(aTopic, "application/xml") == 0)
278 QAOutput("Observed 'application/xml'.");
280 else if (nsCRT::strcmp(aTopic, "htmlparser") == 0)
282 QAOutput("Observed 'htmlparser'.");
284 else if (nsCRT::strcmp(aTopic, "xmlparser") == 0)
286 QAOutput("Observed 'xmlparser'.");
288 else if (nsCRT::strcmp(aTopic, "memory-pressure") == 0)
290 QAOutput("Observed 'memory-pressure'.");
292 else if (nsCRT::strcmp(aTopic, "quit-application") == 0)
294 QAOutput("Observed 'quit-application'.");
296 else if (nsCRT::strcmp(aTopic, "login-failed") == 0)
298 QAOutput("Observed 'login-failed'.");
301 return rv;