Back to index

lightning-sunbird  0.9+nobinonly
nsIObserServ.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *   David Epstein <depstein@netscape.com>
00024  *   Ashish Bhatt <ashishbhatt@netscape.com>
00025  *
00026  * Alternatively, the contents of this file may be used under the terms of
00027  * either the GNU General Public License Version 2 or later (the "GPL"), or
00028  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00029  * in which case the provisions of the GPL or the LGPL are applicable instead
00030  * of those above. If you wish to allow use of your version of this file only
00031  * under the terms of either the GPL or the LGPL, and not to allow others to
00032  * use your version of this file under the terms of the MPL, indicate your
00033  * decision by deleting the provisions above and replace them with the notice
00034  * and other provisions required by the GPL or the LGPL. If you do not delete
00035  * the provisions above, a recipient may use your version of this file under
00036  * the terms of any one of the MPL, the GPL or the LGPL.
00037  *
00038  * ***** END LICENSE BLOCK ***** */
00039 
00040 // nsIObserServ.cpp: implementation of the CnsIObserServ class.
00041 //
00043 
00044 #include "stdafx.h"
00045 #include "nsIObserServ.h"
00046 #include "QaUtils.h"
00047 
00049 // Construction/Destruction
00051 CnsIObserServ::CnsIObserServ()
00052 
00053 {
00054        mRefCnt = 1 ;
00055 }
00056 
00057 
00058 CnsIObserServ::~CnsIObserServ()
00059 {
00060 
00061 }
00062 
00063 ObserverElement ObserverTable[] = {
00064        {"profile-before-change", PR_TRUE},
00065     {"profile-after-change", PR_FALSE},
00066        {"profile-do-change", PR_TRUE},
00067        {"text/xml", PR_FALSE},
00068        {"application/xml", PR_TRUE},
00069        {"htmlparser", PR_FALSE},
00070        {"xmlparser", PR_TRUE},     
00071        {"memory-pressure", PR_FALSE},
00072        {"quit-application", PR_TRUE},
00073        {"login-failed", PR_FALSE}  
00074 }; 
00075 
00076 
00077 void CnsIObserServ::OnStartTests(UINT nMenuID)
00078 
00079 {
00080        // Calls  all or indivdual test cases on the basis of the 
00081        // option selected from menu.
00082 
00083        switch(nMenuID)
00084        {
00085 
00086        case ID_INTERFACES_NSIOBSERVERSERVICE_RUNALLTESTS :
00087               RunAllTests();
00088               break;
00089 
00090        case ID_INTERFACES_NSIOBSERVERSERVICE_ADDOBSERVERS :
00091               AddObserversTest(2);
00092               break;
00093 
00094        case ID_INTERFACES_NSIOBSERVERSERVICE_ENUMERATEOBSERVERS :
00095               QAOutput("Adding observers first", 2);
00096               AddObserversTest(1);        
00097               EnumerateObserversTest(1);
00098               break;
00099 
00100        case ID_INTERFACES_NSIOBSERVERSERVICE_NOTIFYOBSERVERS :
00101               NotifyObserversTest(1);
00102               break;
00103 
00104        case ID_INTERFACES_NSIOBSERVERSERVICE_REMOVEOBSERVERS :
00105               QAOutput("Adding observers first.", 2);
00106               AddObserversTest(1);
00107               RemoveObserversTest(1);
00108               break;
00109 
00110        default :
00111               AfxMessageBox("Menu handler not added for this menu item");
00112               break;
00113        }
00114 }
00115 
00116 void CnsIObserServ::RunAllTests()
00117 {
00118        AddObserversTest(1);
00119        EnumerateObserversTest(1);
00120        NotifyObserversTest(1);
00121        RemoveObserversTest(1);
00122 }
00123 
00124 void CnsIObserServ::AddObserversTest(int displayType)
00125 {
00126        int i;
00127 
00128        nsCOMPtr<nsIObserverService>observerService(do_GetService("@mozilla.org/observer-service;1",&rv));
00129        RvTestResult(rv, "nsIObserverService object test", displayType);
00130        RvTestResultDlg(rv, "nsIObserverService object test", true);
00131 
00132        QAOutput("\n nsIObserverService::AddObserversTest().");
00133        if (!observerService) 
00134        {
00135               QAOutput("Can't get nsIObserverService object. Tests fail.");
00136               return;
00137        }
00138 
00139        for (i=0; i<10; i++)
00140        {
00141               rv = observerService->AddObserver(this, ObserverTable[i].theTopic, 
00142                                                                      ObserverTable[i].theOwnsWeak);
00143               FormatAndPrintOutput("The observer to be added = ", ObserverTable[i].theTopic, 1);  
00144               RvTestResult(rv, "AddObservers() test", displayType);
00145               RvTestResultDlg(rv, "AddObservers() test");
00146        }
00147 }
00148 
00149 void CnsIObserServ::RemoveObserversTest(int displayType)
00150 {
00151        int i;
00152 
00153        nsCOMPtr<nsIObserverService>observerService(do_GetService("@mozilla.org/observer-service;1",&rv));
00154 
00155        QAOutput("\n nsIObserverService::RemoveObserversTest().");
00156        if (!observerService) 
00157        {
00158               QAOutput("Can't get nsIObserverService object. Tests fail.");
00159               return;
00160        }
00161 
00162        for (i=0; i<10; i++)
00163        {
00164               rv = observerService->RemoveObserver(this, ObserverTable[i].theTopic);
00165               FormatAndPrintOutput("The observer to be removed = ", ObserverTable[i].theTopic, 1);       
00166               RvTestResult(rv, "RemoveObservers() test", displayType);
00167               RvTestResultDlg(rv, "RemoveObservers() test");
00168        }
00169 }
00170 
00171 
00172 void CnsIObserServ::NotifyObserversTest(int displayType)
00173 {
00174        PRInt32 i;
00175        nsCOMPtr<nsIObserverService>observerService(do_GetService("@mozilla.org/observer-service;1",&rv));
00176 
00177        QAOutput("\n nsIObserverService::NotifyObserversTest().");
00178 
00179        if (!observerService) 
00180        {
00181               QAOutput("Can't get nsIObserverService object. Tests fail.");
00182               return;
00183        }
00184 
00185        for (i=0; i<10; i++)
00186        {
00187               FormatAndPrintOutput("The notified observer = ", ObserverTable[i].theTopic, 1);
00188               rv = observerService->NotifyObservers(nsnull, ObserverTable[i].theTopic, 0);
00189               RvTestResult(rv, "NotifyObservers() test", displayType);
00190               RvTestResultDlg(rv, "NotifyObservers() test");
00191        }
00192 }
00193 
00194 
00195 void CnsIObserServ::EnumerateObserversTest(int displayType)
00196 {
00197        PRInt32 i=0;
00198        nsCOMPtr<nsIObserverService> observerService(do_GetService("@mozilla.org/observer-service;1",&rv));
00199        nsCOMPtr<nsISimpleEnumerator> simpleEnum;
00200 
00201        QAOutput("\n nsIObserverService::EnumerateObserversTest().");
00202        if (!observerService) 
00203        {
00204               QAOutput("Can't get nsIObserverService object. Tests fail.");
00205               return;
00206        }
00207 
00208        for (i=0; i<10; i++)
00209        {
00210               // need to handle Simple Enumerator
00211 
00212               rv = observerService->EnumerateObservers(ObserverTable[i].theTopic, 
00213                                                                                      getter_AddRefs(simpleEnum));
00214 
00215               RvTestResult(rv, "EnumerateObserversTest() test", displayType);
00216               RvTestResultDlg(rv, "EnumerateObserversTest() test");
00217               if (!simpleEnum)
00218               {
00219                      QAOutput("Didn't get SimpleEnumerator object. Tests fail.");
00220                      return;
00221               }
00222 
00223               nsCOMPtr<nsIObserver> observer;
00224               PRBool theLoop = PR_TRUE;
00225               PRBool bLoop = PR_TRUE;
00226 
00227               while( NS_SUCCEEDED(simpleEnum->HasMoreElements(&theLoop)) && bLoop) 
00228               {
00229                      simpleEnum->GetNext(getter_AddRefs(observer));
00230 
00231                      if (!observer)
00232                      {
00233                             QAOutput("Didn't get the observer object. Tests fail.");
00234                             return;
00235                      }
00236                      rv = observer->Observe(observer, ObserverTable[i].theTopic, 0);
00237                      RvTestResult(rv, "nsIObserver() test", 1);       
00238                      RvTestResultDlg(rv, "nsIObserver() test");
00239                      
00240                      // compare 'this' with observer object
00241                      if( this == NS_REINTERPRET_CAST(CnsIObserServ*,NS_REINTERPRET_CAST(void*, observer.get())))
00242                      {
00243                             QAOutput("observers match. Test passes.");
00244                             bLoop = PR_FALSE;
00245                      }
00246                      else
00247                             QAOutput("observers don't match. Test fails.");
00248               }
00249        }
00250 }
00251 
00252 
00253 NS_IMPL_THREADSAFE_ISUPPORTS2(CnsIObserServ,  nsIObserver,  nsISupportsWeakReference)
00254 
00255 
00256 NS_IMETHODIMP CnsIObserServ::Observe(nsISupports *aSubject, const char *aTopic, const PRUnichar *someData)
00257 {
00258     nsresult rv = NS_OK;
00259     
00260     if (nsCRT::strcmp(aTopic, "profile-before-change") == 0)
00261     {
00262               QAOutput("Observed 'profile-before-change'.");
00263     }
00264     else if (nsCRT::strcmp(aTopic, "profile-after-change") == 0)
00265     {
00266               QAOutput("Observed 'profile-after-change'.");
00267     }
00268        else if (nsCRT::strcmp(aTopic, "profile-do-change") == 0)
00269     {
00270               QAOutput("Observed 'profile-do-change'.");
00271     }
00272     else if (nsCRT::strcmp(aTopic, "text/xml") == 0)
00273     {
00274               QAOutput("Observed 'text/xml'.");
00275     }
00276        else if (nsCRT::strcmp(aTopic, "application/xml") == 0)
00277     {
00278               QAOutput("Observed 'application/xml'.");
00279     }
00280     else if (nsCRT::strcmp(aTopic, "htmlparser") == 0)
00281     {
00282               QAOutput("Observed 'htmlparser'.");
00283     }
00284        else if (nsCRT::strcmp(aTopic, "xmlparser") == 0)
00285     {
00286               QAOutput("Observed 'xmlparser'.");
00287     }
00288        else if (nsCRT::strcmp(aTopic, "memory-pressure") == 0)
00289     {
00290               QAOutput("Observed 'memory-pressure'.");
00291     }
00292        else if (nsCRT::strcmp(aTopic, "quit-application") == 0)
00293     {
00294               QAOutput("Observed 'quit-application'.");
00295     }
00296        else if (nsCRT::strcmp(aTopic, "login-failed") == 0)
00297     {
00298               QAOutput("Observed 'login-failed'.");
00299     }
00300 
00301     return rv;
00302 }