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