LCOV - code coverage report
Current view: directory - xpcom/tests - TestObserverService.cpp (source / functions) Found Hit Coverage
Test: app.info Lines: 75 6 8.0 %
Date: 2012-06-02 Functions: 9 1 11.1 %

       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 <pp@ludusdesign.com>
      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 "nsStringGlue.h"
      45                 : #include "nsWeakReference.h"
      46                 : #include "nsComponentManagerUtils.h"
      47                 : 
      48                 : #include <stdio.h>
      49                 : 
      50                 : static nsIObserverService *anObserverService = NULL;
      51                 : 
      52               0 : static void testResult( nsresult rv ) {
      53               0 :     if ( NS_SUCCEEDED( rv ) ) {
      54               0 :         printf("...ok\n");
      55                 :     } else {
      56               0 :         printf("...failed, rv=0x%x\n", (int)rv);
      57                 :     }
      58                 :     return;
      59                 : }
      60                 : 
      61               0 : void printString(nsString &str) {
      62               0 :     printf("%s", NS_ConvertUTF16toUTF8(str).get());
      63               0 : }
      64                 : 
      65                 : class TestObserver : public nsIObserver, public nsSupportsWeakReference {
      66                 : public:
      67               0 :     TestObserver( const nsAString &name )
      68               0 :         : mName( name ) {
      69               0 :     }
      70                 :     NS_DECL_ISUPPORTS
      71                 :     NS_DECL_NSIOBSERVER
      72                 : 
      73                 :     nsString mName;
      74                 : 
      75                 : private:
      76               0 :     ~TestObserver() {}
      77                 : };
      78                 : 
      79               0 : NS_IMPL_ISUPPORTS2( TestObserver, nsIObserver, nsISupportsWeakReference )
      80                 : 
      81                 : NS_IMETHODIMP
      82               0 : TestObserver::Observe( nsISupports     *aSubject,
      83                 :                        const char *aTopic,
      84                 :                        const PRUnichar *someData ) {
      85               0 :     nsCString topic( aTopic );
      86               0 :     nsString data( someData );
      87                 :         /*
      88                 :                 The annoying double-cast below is to work around an annoying bug in
      89                 :                 the compiler currently used on wensleydale.  This is a test.
      90                 :         */
      91               0 :     printString(mName);
      92               0 :     printf(" has observed something: subject@%p", (void*)aSubject);
      93               0 :     printf(" name=");
      94               0 :     printString(reinterpret_cast<TestObserver*>(reinterpret_cast<void*>(aSubject))->mName);
      95               0 :     printf(" aTopic=%s", topic.get());
      96               0 :     printf(" someData=");
      97               0 :     printString(data);
      98               0 :     printf("\n");
      99               0 :     return NS_OK;
     100                 : }
     101                 : 
     102               1 : int main(int argc, char *argv[])
     103                 : {
     104               2 :     nsCString topicA; topicA.Assign( "topic-A" );
     105               2 :     nsCString topicB; topicB.Assign( "topic-B" );
     106                 :     nsresult rv;
     107                 : 
     108               1 :     nsresult res = CallCreateInstance("@mozilla.org/observer-service;1", &anObserverService);
     109                 :         
     110               1 :     if (res == NS_OK) {
     111                 : 
     112               0 :         nsIObserver *aObserver = new TestObserver(NS_LITERAL_STRING("Observer-A"));
     113               0 :         aObserver->AddRef();
     114               0 :         nsIObserver *bObserver = new TestObserver(NS_LITERAL_STRING("Observer-B"));
     115               0 :         bObserver->AddRef();
     116                 :             
     117               0 :         printf("Adding Observer-A as observer of topic-A...\n");
     118               0 :         rv = anObserverService->AddObserver(aObserver, topicA.get(), false);
     119               0 :         testResult(rv);
     120                 :  
     121               0 :         printf("Adding Observer-B as observer of topic-A...\n");
     122               0 :         rv = anObserverService->AddObserver(bObserver, topicA.get(), false);
     123               0 :         testResult(rv);
     124                 :  
     125               0 :         printf("Adding Observer-B as observer of topic-B...\n");
     126               0 :         rv = anObserverService->AddObserver(bObserver, topicB.get(), false);
     127               0 :         testResult(rv);
     128                 : 
     129               0 :         printf("Testing Notify(observer-A, topic-A)...\n");
     130                 :         rv = anObserverService->NotifyObservers( aObserver,
     131                 :                                    topicA.get(),
     132               0 :                                    NS_LITERAL_STRING("Testing Notify(observer-A, topic-A)").get() );
     133               0 :         testResult(rv);
     134                 : 
     135               0 :         printf("Testing Notify(observer-B, topic-B)...\n");
     136                 :         rv = anObserverService->NotifyObservers( bObserver,
     137                 :                                    topicB.get(),
     138               0 :                                    NS_LITERAL_STRING("Testing Notify(observer-B, topic-B)").get() );
     139               0 :         testResult(rv);
     140                 :  
     141               0 :         printf("Testing EnumerateObserverList (for topic-A)...\n");
     142               0 :         nsCOMPtr<nsISimpleEnumerator> e;
     143               0 :         rv = anObserverService->EnumerateObservers(topicA.get(), getter_AddRefs(e));
     144                 : 
     145               0 :         testResult(rv);
     146                 : 
     147               0 :         printf("Enumerating observers of topic-A...\n");
     148               0 :         if ( e ) {
     149               0 :           nsCOMPtr<nsIObserver> observer;
     150               0 :           bool loop = true;
     151               0 :           while( NS_SUCCEEDED(e->HasMoreElements(&loop)) && loop) 
     152                 :           {
     153               0 :               e->GetNext(getter_AddRefs(observer));
     154               0 :               printf("Calling observe on enumerated observer ");
     155                 :               printString(reinterpret_cast<TestObserver*>
     156               0 :                                           (reinterpret_cast<void*>(observer.get()))->mName);
     157               0 :               printf("...\n");
     158               0 :               rv = observer->Observe( observer, 
     159                 :                                       topicA.get(), 
     160               0 :                                       NS_LITERAL_STRING("during enumeration").get() );
     161               0 :               testResult(rv);
     162                 :           }
     163                 :         }
     164               0 :         printf("...done enumerating observers of topic-A\n");
     165                 : 
     166               0 :         printf("Removing Observer-A...\n");
     167               0 :         rv = anObserverService->RemoveObserver(aObserver, topicA.get());
     168               0 :         testResult(rv);
     169                 : 
     170                 : 
     171               0 :         printf("Removing Observer-B (topic-A)...\n");
     172               0 :         rv = anObserverService->RemoveObserver(bObserver, topicB.get());
     173               0 :         testResult(rv);
     174               0 :         printf("Removing Observer-B (topic-B)...\n");
     175               0 :         rv = anObserverService->RemoveObserver(bObserver, topicA.get());
     176               0 :         testResult(rv);
     177                 :        
     178                 :     }
     179               1 :     return NS_OK;
     180                 : }

Generated by: LCOV version 1.7