uiacceltk/hitchcock/CommonInc/alfmoduletestmap.h
changeset 19 f5bac0badc7e
child 25 f7f1ae431f74
child 27 70e659bb284f
equal deleted inserted replaced
14:83d2d132aa58 19:f5bac0badc7e
       
     1 /*
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: 
       
    15 *
       
    16 */
       
    17 /**
       
    18  * @note This class is provided only if module test hooks are set on.
       
    19  */
       
    20 
       
    21 #include "alfmoduletestconf.h"
       
    22 #ifdef USE_MODULE_TEST_HOOKS_FOR_ALF
       
    23 
       
    24 #ifndef ALFMODULETESTMAP_H
       
    25 #define ALFMODULETESTMAP_H
       
    26 
       
    27 #include <e32def.h> 
       
    28 #include "alfmoduletestitem.h"
       
    29 #include "alfmoduletesttype.h"
       
    30 
       
    31 
       
    32 /**
       
    33  * Class CAlfModuleTestMap
       
    34  * 
       
    35  * Provides map functionality for the key-value-pairs.
       
    36  * In test cases, this should most likely be used so, that 
       
    37  * first test case classes create items with certain keys, for example with handle values.
       
    38  * Then, define hooks are used in the code to update values that corresond the correct handles.
       
    39  * In the end, test case classes can check that items have correct values set and if the test
       
    40  * is passed.
       
    41  * 
       
    42  * @note When this item is copied, also the contents of the map item array are copied
       
    43  *       into new map item array. In other words, copy can be thought as deep copy
       
    44  *       instead of just shallow copy of array pointer.
       
    45  *       
       
    46  * @see CAlfModuleTestItem
       
    47  */
       
    48 template< class T >
       
    49 NONSHARABLE_CLASS( TAlfModuleTestMap )
       
    50     {
       
    51 public:
       
    52 
       
    53     // Maximum item count in the map
       
    54     static const TInt KMaxArrayCount = 100;
       
    55 
       
    56     // Maximum accepted test type count in the map
       
    57     static const TInt KMaxAcceptArrayCount = 20;
       
    58     
       
    59     
       
    60     /**
       
    61      * Constructor to initialize variables.
       
    62      */
       
    63     TAlfModuleTestMap():
       
    64         iArrayCount( 0 )
       
    65         {
       
    66         }
       
    67 
       
    68     
       
    69     /**
       
    70      * @note This will return normal items and also items that are links.
       
    71      * 
       
    72      * @param aTestType Informs what type of test is accepted. Others are skipped.
       
    73      * @param aKey Key of the map item.
       
    74      * @return T* Ownership is not transferred.
       
    75      *            NULL if item is not found.
       
    76      */
       
    77     TAlfModuleTestItem< T >* Find( const TAlfModuleTestType& aTestType, TInt aKey )
       
    78         {
       
    79         // Try to find the item corresponding the given key.
       
    80         for ( TInt i = 0; i < iArrayCount; ++i )
       
    81             {
       
    82             TAlfModuleTestItem< T >& testItem( iArray[ i ] );
       
    83             if ( testItem.Key() == aKey
       
    84                  && testItem.TestTypeMatch( aTestType ) )
       
    85                 {
       
    86                 return &( testItem );
       
    87                 }
       
    88             }
       
    89         // Item corresponding the given key was not found.
       
    90         return NULL;
       
    91         }    
       
    92 
       
    93     
       
    94     /**
       
    95      * Finds the actual item.
       
    96      * 
       
    97      * @note If the given key identifies a link item, 
       
    98      *       then search is continued to the actual item
       
    99      *       through the link chain.
       
   100      *       Link item is not returned here.
       
   101      * 
       
   102      * @param aTestType Test type of the linked item and link item.
       
   103      * @param aKey Key of the item. This can also be a key to a link item.
       
   104      * @return TAlfModuleTestItem< T >* Actual item. 
       
   105      *                                  NULL if item was not found.
       
   106      *                                  Ownership is not transferred.
       
   107      */
       
   108     TAlfModuleTestItem< T >* FindActual( const TAlfModuleTestType& aTestType, TInt aKey )
       
   109         {
       
   110         TAlfModuleTestItem< T >* item( Find( aTestType, aKey ) );
       
   111         while ( item && item->LinkTargetKey() != KErrNotFound )
       
   112             {
       
   113             // Item is link.
       
   114             // So, find the actual item through link targets.
       
   115             item = Find( aTestType, item->LinkTargetKey() );
       
   116             }
       
   117         return item;
       
   118         }
       
   119     
       
   120     
       
   121     /**
       
   122      * Function to append new item into the map.
       
   123      * 
       
   124      * @param aTestType Describes for what case the appended test item is created for.
       
   125      * @param aKey Key of the map item.
       
   126      * @param aDefaultValue Default value for the map item.
       
   127      * @return TInt System wide error code.
       
   128      */    
       
   129     TInt Append( const TAlfModuleTestType& aTestType, TInt aKey, const T& aDefaultValue )
       
   130         {
       
   131         if ( !Accept( aTestType ) )
       
   132             {
       
   133             // Given test type has not been set as accepted.
       
   134             return KErrNotSupported;
       
   135             }
       
   136         else if ( Find( aTestType, aKey ) )
       
   137             {
       
   138             // Item already exists in the map.
       
   139             return KErrAlreadyExists;
       
   140             }        
       
   141         else if ( iArrayCount == KMaxArrayCount )
       
   142             {
       
   143             // Array already full.
       
   144             return KErrOverflow;
       
   145             }
       
   146         
       
   147         // Append new item into the array.
       
   148         iArray[ iArrayCount ] = TAlfModuleTestItem< T >( aTestType, aKey, aDefaultValue );
       
   149         ++iArrayCount;
       
   150         return KErrNone;        
       
   151         }
       
   152 
       
   153     
       
   154     /**
       
   155      * Creates and appends link item that links to another item.
       
   156      * 
       
   157      * @param aTestType Test type of the linked item and link item.
       
   158      * @param aLinkKey Link item key.
       
   159      * @param aTargetKey Target item key.
       
   160      * @return TInt System wide error code.
       
   161      */
       
   162     TInt AppendLink( const TAlfModuleTestType& aTestType, TInt aLinkKey, TInt aTargetKey )
       
   163         {
       
   164         if ( !Accept( aTestType ) )
       
   165             {
       
   166             // Given test type has not been set as accepted.
       
   167             return KErrNotSupported;
       
   168             }
       
   169         else if ( Find( aTestType, aLinkKey ) )
       
   170             {
       
   171             // Link item already exists in the map.
       
   172             return KErrAlreadyExists;
       
   173             }
       
   174         else if ( !FindActual( aTestType, aTargetKey ) )
       
   175             {
       
   176             // Link has to point to another existing item.
       
   177             // Also, link has to point to the chain that ends to actual item.
       
   178             // Then, links will not create forever loops. Loops could occur
       
   179             // if links in chain point to each other creating loops.
       
   180             return KErrNotFound;
       
   181             }
       
   182         else if ( iArrayCount == KMaxArrayCount )
       
   183             {
       
   184             // Array already full.
       
   185             return KErrOverflow;
       
   186             }
       
   187         
       
   188         // Create link item because original item exists and link chain is correct.
       
   189         // Append new link item into the array and link it to another link or to
       
   190         // actual item.
       
   191         TAlfModuleTestItem< T >* target( Find( aTestType, aTargetKey ) );
       
   192         // Set value of the link same as its target's value.
       
   193         iArray[ iArrayCount ] = TAlfModuleTestItem< T >( aTestType, aLinkKey, target->Value() );
       
   194         iArray[ iArrayCount ].SetLinkTargetKey( target->Key() );
       
   195         ++iArrayCount;
       
   196         return KErrNone;            
       
   197         }
       
   198 
       
   199     
       
   200     /**
       
   201      * Sets the value for the actual item.
       
   202      * Item itself should already appended into map and exist
       
   203      * in the array before setting its value here.
       
   204      * 
       
   205      * @note Some items may be links. But, only the value of the
       
   206      *       actual item in the end of the link chain is set here.
       
   207      * 
       
   208      * @param aTestType Describes for what case the item is for.
       
   209      * @param aKey Key of the map item.
       
   210      * @param aValue Value for the map item.
       
   211      * @return TInt System wide error code.
       
   212      */
       
   213     TInt SetActualValue( const TAlfModuleTestType& aTestType, TInt aKey, const T& aValue )
       
   214         {
       
   215         TAlfModuleTestItem< T >* item( FindActual( aTestType, aKey ) );
       
   216         if ( !item )
       
   217             {
       
   218             // Actual item was not found from the array.        
       
   219             return KErrNotFound;            
       
   220             }
       
   221         
       
   222         // Actual item exists and it is not link item. So, set its values.
       
   223         item->SetValue( aValue );        
       
   224         return KErrNone;
       
   225         }
       
   226 
       
   227     
       
   228     /**
       
   229      * Find an actual item if it exists and gets its value if item is found
       
   230      * and value has been set.
       
   231      * 
       
   232      * @param aTestType Describes for what case the item is for.
       
   233      * @param aKey Key of the map item.
       
   234      * @param aValue If map item is found, value of the item is set here.
       
   235      * @param aAcceptDefault ETrue if an existing object having its default value
       
   236      *                       is accepted. EFalse if value should have been updated
       
   237      *                       separately.
       
   238      * @return TBool ETrue if map item is found and value has been set. Else EFalse. 
       
   239      */    
       
   240     TBool GetActualValue( 
       
   241         const TAlfModuleTestType& aTestType, TInt aKey, T& aValue, TBool aAcceptDefault )
       
   242         {
       
   243         const TAlfModuleTestItem< T >* item( FindActual( aTestType, aKey ) );
       
   244         if ( item && ( aAcceptDefault || item->ValueSetCount() > 0 ) )
       
   245             {
       
   246             aValue = item->Value();
       
   247             return ETrue;
       
   248             }
       
   249         return EFalse;
       
   250         }
       
   251 
       
   252 
       
   253     /**
       
   254      * Finds an actual item if it exists and checks if it equals the given value.
       
   255      * 
       
   256      * @param aTestType Describes for what case the item is for.
       
   257      * @param aKey Key of the map item.
       
   258      * @param aValue Value of the map item.
       
   259      * @return TBool ETrue if item is found and its value equals given value. Else EFalse. 
       
   260      */
       
   261     TBool ActualEquals( const TAlfModuleTestType& aTestType, TInt aKey, const T& aValue )
       
   262         {
       
   263         T value( aValue );
       
   264         if ( GetActualValue( aTestType, aKey, value, ETrue ) && value == aValue )
       
   265             {
       
   266             return ETrue;
       
   267             }
       
   268         return EFalse;
       
   269         }
       
   270 
       
   271 
       
   272     /**
       
   273      * @return TInt Number of map items
       
   274      */
       
   275     TInt ItemCount() const
       
   276         {
       
   277         return iArrayCount;
       
   278         }
       
   279 
       
   280 
       
   281     /**
       
   282      * @return const TAlfModuleTestItem< T >& Reference to the map item
       
   283      */
       
   284     const TAlfModuleTestItem< T >& Item( TInt aIndex ) const
       
   285         {
       
   286         return iArray[ aIndex ];
       
   287         }
       
   288 
       
   289     
       
   290     /**
       
   291      * Appends an accepted test type into the accepted array.
       
   292      * 
       
   293      * @note Only items of accepted test type can be appended
       
   294      *       into this map.
       
   295      * 
       
   296      * @param aTestType Test item type that is accepted for this map.
       
   297      * @return TInt System wide error code.
       
   298      */
       
   299     TInt AppendAccept( const TAlfModuleTestType& aTestType )
       
   300         {
       
   301         if ( Accept( aTestType ) )
       
   302             {
       
   303             // Type already exists in the array.
       
   304             return KErrAlreadyExists;
       
   305             }
       
   306         else if ( iAcceptArrayCount == KMaxAcceptArrayCount )
       
   307             {
       
   308             // Array already full.
       
   309             return KErrOverflow;
       
   310             }
       
   311 
       
   312         iAcceptArray[ iAcceptArrayCount ] = aTestType;
       
   313         ++iAcceptArrayCount;
       
   314         return KErrNone;
       
   315         }
       
   316     
       
   317     
       
   318     /**
       
   319      * @param aTestType Test item type to be checked.
       
   320      * @return TBool ETrue if given test type is set as accepted for this map.
       
   321      *               Else EFalse.
       
   322      */
       
   323     TBool Accept( const TAlfModuleTestType& aTestType ) const
       
   324         {
       
   325         for ( TInt i = 0; i < iAcceptArrayCount; ++i )
       
   326             {
       
   327             if ( aTestType == iAcceptArray[ i ] )
       
   328                 {
       
   329                 return ETrue;
       
   330                 }
       
   331             }
       
   332         return EFalse;
       
   333         }
       
   334 
       
   335 
       
   336     /**
       
   337      * Resets all the array items that match the given test type
       
   338      * to given default value.
       
   339      * 
       
   340      * @param aTestType The test type of items that should be resetted.
       
   341      * @param aValue Reference to the value that items are resetted to.
       
   342      */
       
   343     void ResetItems( const TAlfModuleTestType& aTestType, const T& aDefaultValue )
       
   344         {
       
   345         for ( TInt i = 0; i < iArrayCount; ++i )
       
   346             {
       
   347             if ( iArray[ i ].TestTypeMatch( aTestType ) )
       
   348                 {
       
   349                 iArray[ i ].Reset( aDefaultValue );
       
   350                 }
       
   351             }
       
   352         }
       
   353 
       
   354 
       
   355     /**
       
   356      * Resets the map
       
   357      */
       
   358     void Reset()
       
   359         {
       
   360         // Just reset the counter.
       
   361         // We do not bother to reset map items, because when counter is reseted
       
   362         // already set items and their info is left out of the scope.
       
   363         iArrayCount = 0;
       
   364         iAcceptArrayCount = 0;
       
   365         }
       
   366 
       
   367     
       
   368 private: // data
       
   369     
       
   370     TAlfModuleTestItem< T > iArray[ KMaxArrayCount ];
       
   371     TInt iArrayCount;
       
   372     
       
   373     TAlfModuleTestType iAcceptArray[ KMaxAcceptArrayCount ];
       
   374     TInt iAcceptArrayCount;
       
   375     
       
   376     };
       
   377 
       
   378 #endif // ALFMODULETESTMAP_H
       
   379 
       
   380 #endif // USE_MODULE_TEST_HOOKS_FOR_ALF
       
   381 
       
   382 // End of File