contacts_plat/phonebook_2_contact_related_icon_customization_api/tsrc/src/CIconCustomApiTestModuleBlocks.cpp
branchRCL_3
changeset 6 e8e3147d53eb
equal deleted inserted replaced
5:81f8547efd4f 6:e8e3147d53eb
       
     1 /*
       
     2 * Copyright (c) 2008-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 // INCLUDE FILES
       
    18 
       
    19 #include <ecom\ecom.h>
       
    20 #include <e32property.h>
       
    21 #include <e32cmn.h>
       
    22 
       
    23 #include <StifParser.h>
       
    24 #include <StifTestInterface.h>
       
    25 
       
    26 #include <CContactCustomIconPluginBase.h>
       
    27 #include <CCustomIconIdMap.h>
       
    28 #include <CCustomIconDecisionData.h>
       
    29 
       
    30 #include "CIconCustomApiTestModule.h"
       
    31 
       
    32 _LIT( KTestPluginDisplayName, "Custom Icon test plugin" );
       
    33 
       
    34 
       
    35 const TInt KExecutableUidIndex = 0;
       
    36 
       
    37 // -----------------------------------------------------------------------------
       
    38 // CleanUpEComInfoArray
       
    39 // CleanupStack support for RImplInfoPtrArray.
       
    40 // See LoadTestPluginL how it is used
       
    41 // -----------------------------------------------------------------------------
       
    42 //
       
    43 LOCAL_C void CleanUpEComInfoArray( TAny* aInfoArray )
       
    44     {
       
    45     RImplInfoPtrArray* infoArray = (static_cast<RImplInfoPtrArray*>(aInfoArray));
       
    46     infoArray->ResetAndDestroy();
       
    47     infoArray->Close();
       
    48     }
       
    49 
       
    50 // ----------------------------------------------------------------------------
       
    51 // ProcessSID
       
    52 // Figure out current process SID (this should be STIFF SID)
       
    53 // ----------------------------------------------------------------------------
       
    54 //
       
    55 LOCAL_C TUid ProcessSID()
       
    56     {
       
    57     RProcess process;
       
    58     TUidType type = process.Type();
       
    59     // current process SID (STIFF executable SID = 0x1000007A)
       
    60     TUid uid = type[KExecutableUidIndex];
       
    61     return uid;
       
    62     }
       
    63 
       
    64 // -----------------------------------------------------------------------------
       
    65 // BuildDecisionDataLC
       
    66 // Utility to build decision data object.
       
    67 // See LoadTestPluginL how it is used
       
    68 // -----------------------------------------------------------------------------
       
    69 //
       
    70 LOCAL_C CCustomIconDecisionData* BuildDecisionDataLC(  CStifItemParser& aItem )
       
    71     {
       
    72     CCustomIconDecisionData* decisionData =
       
    73         new ( ELeave ) CCustomIconDecisionData();
       
    74     CleanupStack::PushL( decisionData );
       
    75 
       
    76     TUid contextParam = ProcessSID();
       
    77 
       
    78     TTime* time = new (ELeave) TTime;
       
    79     decisionData->SetTime( time ); // ownership taken
       
    80     time->UniversalTime();
       
    81 
       
    82     TPtrC phoneNumber;
       
    83     aItem.GetNextString( phoneNumber );
       
    84     decisionData->SetPhoneNumberL( phoneNumber );
       
    85 
       
    86     decisionData->SetContactLinkRef( NULL );
       
    87     decisionData->SetContext( contextParam );
       
    88 
       
    89     return decisionData;
       
    90     }
       
    91 
       
    92 
       
    93 // ----------------------------------------------------------------------------
       
    94 // ReplaceIconL
       
    95 // Find an icon and replace it
       
    96 // ----------------------------------------------------------------------------
       
    97 //
       
    98 LOCAL_C void ReplaceIconL( CCustomIconIdMap* aIcons,
       
    99     CGulIcon* aIcon, TCustomIconId aIconId )
       
   100     {
       
   101     TInt i;
       
   102     TInt count = aIcons->Count();
       
   103     TInt err( KErrNotFound );
       
   104     for( i=0; i<count; i++ )
       
   105         {
       
   106         TCustomIconIdMapItem& item( (*aIcons)[i] );
       
   107         if( item.iId == aIconId )
       
   108             {
       
   109             // Change the icon
       
   110             delete item.iIconPtr;
       
   111             item.iIconPtr = aIcon;
       
   112             err = KErrNone;
       
   113             break;
       
   114             }
       
   115         }
       
   116     User::LeaveIfError( err );
       
   117     }
       
   118 
       
   119 
       
   120 // ============================ MEMBER FUNCTIONS ==============================
       
   121 
       
   122 // ----------------------------------------------------------------------------
       
   123 // CIconCustomApiTestModule::CleanupPsKeys
       
   124 // Remove all key definitions and/or values, which have been
       
   125 // defined/set by this test module.
       
   126 // ----------------------------------------------------------------------------
       
   127 //
       
   128 void CIconCustomApiTestModule::CleanupPsKeys()
       
   129     {
       
   130     TInt count = iPublishedPsKeys.Count();
       
   131     TInt i;
       
   132     TUid sid = ProcessSID();
       
   133     for( i=0; i<count; i++ )
       
   134         {
       
   135         // Ignore return value, because key may not exist
       
   136         RProperty::Delete( sid, iPublishedPsKeys[i] );
       
   137         }
       
   138     }
       
   139 
       
   140 // ----------------------------------------------------------------------------
       
   141 // CIconCustomApiTestModule::RunMethodL
       
   142 // Run specified method. Contains also table of test methods and their names.
       
   143 // ----------------------------------------------------------------------------
       
   144 //
       
   145 TInt CIconCustomApiTestModule::RunMethodL(
       
   146     CStifItemParser& aItem )
       
   147     {
       
   148 
       
   149     static TStifFunctionInfo const KFunctions[] =
       
   150         {
       
   151         ENTRY( "LoadTestPluginL", CIconCustomApiTestModule::LoadTestPluginL ),
       
   152         ENTRY( "LoadIconsL", CIconCustomApiTestModule::LoadIconsL ),
       
   153         ENTRY( "LoadIconL", CIconCustomApiTestModule::LoadIconL ),
       
   154         ENTRY( "UpdateIconsWithLastIconIdsL", CIconCustomApiTestModule::UpdateIconsWithLastIconIdsL ),
       
   155         ENTRY( "DeleteIconsL", CIconCustomApiTestModule::DeleteIconsL ),
       
   156         ENTRY( "DeleteIconL", CIconCustomApiTestModule::DeleteIconL ),
       
   157         ENTRY( "IconChoiceL", CIconCustomApiTestModule::IconChoiceL ),
       
   158         ENTRY( "SetPsProperty", CIconCustomApiTestModule::SetPsProperty ),
       
   159         ENTRY( "DeletePsProperty", CIconCustomApiTestModule::DeletePsProperty ),
       
   160         ENTRY( "SetTestCaseAsObserverL", CIconCustomApiTestModule::SetTestCaseAsObserverL ),
       
   161         ENTRY( "CheckIfHasExtensionL", CIconCustomApiTestModule::CheckIfHasExtensionL ),
       
   162         };
       
   163 
       
   164     const TInt count = sizeof( KFunctions ) /
       
   165                         sizeof( TStifFunctionInfo );
       
   166 
       
   167     return RunInternalL( KFunctions, count, aItem );
       
   168     }
       
   169 
       
   170 // ----------------------------------------------------------------------------
       
   171 // CIconCustomApiTestModule::CustomIconsChangedL
       
   172 // Notification handler, called by plugin.
       
   173 // ----------------------------------------------------------------------------
       
   174 //
       
   175 void CIconCustomApiTestModule::CustomIconEvent(
       
   176             CContactCustomIconPluginBase& /*aPlugin*/,
       
   177             TEventType aEventType,
       
   178             const TArray<TCustomIconId>& aIconIds)
       
   179     {
       
   180     iLastEventType = aEventType;
       
   181     TInt signalCode( aEventType );
       
   182 
       
   183     TRAPD( err,
       
   184         iLastIconIds.Reset();
       
   185         TInt i;
       
   186         TInt count = aIconIds.Count();
       
   187         for( i=0; i<count; i++ )
       
   188             {
       
   189             iLastIconIds.AppendL( aIconIds[i] );
       
   190             }
       
   191         );
       
   192     if( err )
       
   193         {
       
   194         signalCode = err;
       
   195         }
       
   196     Signal( signalCode );
       
   197     }
       
   198 
       
   199 
       
   200 // ----------------------------------------------------------------------------
       
   201 // CIconCustomApiTestModule::LoadTestPluginL
       
   202 // ----------------------------------------------------------------------------
       
   203 //
       
   204 TInt CIconCustomApiTestModule::LoadTestPluginL( CStifItemParser& /*aItem*/ )
       
   205     {
       
   206     if( iTestPlugin )
       
   207         {
       
   208         return KErrAlreadyExists;
       
   209         }
       
   210 
       
   211     CContactCustomIconPluginBase* result = NULL;
       
   212 
       
   213     RImplInfoPtrArray implementationInfoArray;
       
   214     TCleanupItem cleanup( CleanUpEComInfoArray, &implementationInfoArray );
       
   215     CleanupStack::PushL( cleanup );
       
   216 
       
   217     TUid uid( KContactIconCustomizationInterfaceUid );
       
   218 
       
   219     REComSession::ListImplementationsL( uid,
       
   220         implementationInfoArray );
       
   221 
       
   222     TInt count = implementationInfoArray.Count();
       
   223 
       
   224     for( TInt i=0; i<count; i++)
       
   225         {
       
   226         CImplementationInformation* info = implementationInfoArray[i];
       
   227         if( !(info->DisplayName().Compare( KTestPluginDisplayName ) == 0) )
       
   228             {
       
   229             continue;
       
   230             }
       
   231         // Test plugin was found. Try to load it.
       
   232         TUid pluginUid = info->ImplementationUid();
       
   233         TRAPD( err, result = static_cast<CContactCustomIconPluginBase*>
       
   234             (REComSession::CreateImplementationL(pluginUid,
       
   235                                             _FOFF(CContactCustomIconPluginBase,
       
   236                                             iDtorKey)));
       
   237             );
       
   238         if( err )
       
   239             {
       
   240             result = NULL;
       
   241             }
       
   242         break;
       
   243         }
       
   244 
       
   245     CleanupStack::PopAndDestroy(&implementationInfoArray);
       
   246 
       
   247     iTestPlugin = result;
       
   248 
       
   249     if( !iTestPlugin )
       
   250         {
       
   251         return KErrNotFound;
       
   252         }
       
   253     return KErrNone;
       
   254     }
       
   255 
       
   256 // ----------------------------------------------------------------------------
       
   257 // CIconCustomApiTestModule::LoadIconsL
       
   258 // Load icon array
       
   259 // ----------------------------------------------------------------------------
       
   260 //
       
   261 TInt CIconCustomApiTestModule::LoadIconsL( CStifItemParser& aItem )
       
   262     {
       
   263     User::LeaveIfNull( iTestPlugin );
       
   264     if( iIcons )
       
   265         {
       
   266         User::Leave( KErrAlreadyExists );
       
   267         }
       
   268 
       
   269     TInt sizeHint( 0 );
       
   270     aItem.GetNextInt( sizeHint );
       
   271     iIcons = iTestPlugin->IconsL(
       
   272         ProcessSID(), // use SID as context parameter
       
   273         (CContactCustomIconPluginBase::TIconSizeHint)sizeHint );
       
   274     return KErrNone;
       
   275     }
       
   276 
       
   277 // ----------------------------------------------------------------------------
       
   278 // CIconCustomApiTestModule::LoadIconL
       
   279 // Load individual icon
       
   280 // ----------------------------------------------------------------------------
       
   281 //
       
   282 TInt CIconCustomApiTestModule::LoadIconL( CStifItemParser& aItem )
       
   283     {
       
   284     User::LeaveIfNull( iTestPlugin );
       
   285     if( iIcon )
       
   286         {
       
   287         User::Leave( KErrAlreadyExists );
       
   288         }
       
   289     TInt sizeHint( 0 );
       
   290     aItem.GetNextInt( sizeHint );
       
   291 
       
   292     CCustomIconDecisionData* decisionData = BuildDecisionDataLC( aItem );
       
   293 
       
   294     iIcon = iTestPlugin->IconL( *decisionData,
       
   295         (CContactCustomIconPluginBase::TIconSizeHint)sizeHint );
       
   296 
       
   297     CleanupStack::PopAndDestroy( decisionData );
       
   298     return KErrNone;
       
   299 
       
   300     }
       
   301 
       
   302 // ----------------------------------------------------------------------------
       
   303 // CIconCustomApiTestModule::UpdateIconsWithLastIconIdsL
       
   304 // Replace current icons with new ones
       
   305 // ----------------------------------------------------------------------------
       
   306 //
       
   307 TInt CIconCustomApiTestModule::UpdateIconsWithLastIconIdsL(
       
   308     CStifItemParser& aItem )
       
   309     {
       
   310     User::LeaveIfNull( iTestPlugin );
       
   311     if( !iIcons )
       
   312         {
       
   313         User::Leave( KErrNotFound );
       
   314         }
       
   315     TInt sizeHint( 0 );
       
   316     aItem.GetNextInt( sizeHint );
       
   317 
       
   318     TInt i;
       
   319     TInt count = iLastIconIds.Count();
       
   320     CGulIcon* icon;
       
   321     TCustomIconId iconId;
       
   322     TUid contextParam = ProcessSID(); // use process SID as context parameter
       
   323     for( i=0; i<count; i++ )
       
   324         {
       
   325         iconId = iLastIconIds[i];
       
   326         icon = iTestPlugin->IconL( contextParam,
       
   327             iconId,
       
   328             (CContactCustomIconPluginBase::TIconSizeHint)sizeHint );
       
   329         CleanupStack::PushL( icon );
       
   330         ReplaceIconL( iIcons, icon, iconId ); // Takes ownership
       
   331         CleanupStack::Pop( icon );
       
   332         }
       
   333 
       
   334     return KErrNone;
       
   335     }
       
   336 
       
   337 // ----------------------------------------------------------------------------
       
   338 // CIconCustomApiTestModule::DeleteIconsL
       
   339 // Delete icon array
       
   340 // ----------------------------------------------------------------------------
       
   341 //
       
   342 TInt CIconCustomApiTestModule::DeleteIconsL( CStifItemParser& /*aItem*/ )
       
   343     {
       
   344     if( !iIcons )
       
   345         {
       
   346         User::Leave( KErrNotFound );
       
   347         }
       
   348     delete iIcons;
       
   349     iIcons = NULL;
       
   350     return KErrNone;
       
   351     }
       
   352 
       
   353 // ----------------------------------------------------------------------------
       
   354 // CIconCustomApiTestModule::DeleteIconL
       
   355 // Delete individual icon instance
       
   356 // ----------------------------------------------------------------------------
       
   357 //
       
   358 TInt CIconCustomApiTestModule::DeleteIconL( CStifItemParser& /*aItem*/ )
       
   359     {
       
   360     if( !iIcon )
       
   361         {
       
   362         User::Leave( KErrNotFound );
       
   363         }
       
   364     delete iIcon;
       
   365     iIcon = NULL;
       
   366     return KErrNone;
       
   367     }
       
   368 
       
   369 // ----------------------------------------------------------------------------
       
   370 // CIconCustomApiTestModule::IconChoiceL
       
   371 // Query plug-in for a icon choice to the previously given icon array
       
   372 // Expect plug-in to provide a icon or otherwise fail
       
   373 // ----------------------------------------------------------------------------
       
   374 //
       
   375 TInt CIconCustomApiTestModule::IconChoiceL( CStifItemParser& aItem )
       
   376     {
       
   377     User::LeaveIfNull( iTestPlugin );
       
   378     User::LeaveIfNull( iIcons );
       
   379 
       
   380     CCustomIconDecisionData* decisionData = BuildDecisionDataLC( aItem );
       
   381 
       
   382     TInt iconId( 0 );
       
   383     TBool iconFound = iTestPlugin->IconChoiceL( *decisionData, iconId );
       
   384     if( iconFound )
       
   385         {
       
   386         // Plug-in gave us an icon identifier. Now check if we have the
       
   387         // corresponding icon.
       
   388         CGulIcon* icon = NULL;
       
   389         TCustomIconIdMapItem item;
       
   390         for( TInt i=0; i<iIcons->Count(); i++ )
       
   391             {
       
   392             item = iIcons->At( i );
       
   393             if( item.iId == iconId )
       
   394                 {
       
   395                 icon = item.iIconPtr; // may return NULL
       
   396                 break;
       
   397                 }
       
   398             }
       
   399         if( !icon )
       
   400             {
       
   401             // icon in the icon array was NULL, or
       
   402             // icon was not found by given icon identifier
       
   403             //
       
   404             User::Leave( KErrNotFound );
       
   405             }
       
   406         }
       
   407     else
       
   408         {
       
   409         User::Leave( KErrNotFound );
       
   410         }
       
   411     CleanupStack::PopAndDestroy( decisionData );
       
   412     return KErrNone;
       
   413     }
       
   414 
       
   415 // ----------------------------------------------------------------------------
       
   416 // CIconCustomApiTestModule::SetPsProperty
       
   417 // Define and set publish/subscribe property. This is used for configuring
       
   418 // testing environment. For example the test plug-in reads the P/S values
       
   419 // on its initialization phase.
       
   420 // ----------------------------------------------------------------------------
       
   421 //
       
   422 TInt CIconCustomApiTestModule::SetPsProperty( CStifItemParser& aItem )
       
   423     {
       
   424     TInt key;
       
   425     TInt value;
       
   426     aItem.GetNextInt( key );
       
   427     aItem.GetNextInt( value );
       
   428     TSecurityPolicy readwritePolicy( TSecurityPolicy::EAlwaysPass );
       
   429 
       
   430     TUid sid = ProcessSID();
       
   431     TInt err = RProperty::Define(
       
   432         sid,
       
   433         key,
       
   434         RProperty::EInt,
       
   435         readwritePolicy,
       
   436         readwritePolicy );
       
   437 
       
   438     if(err == KErrNone || err == KErrAlreadyExists)
       
   439         {
       
   440         iPublishedPsKeys.Append( key );
       
   441         err = RProperty::Set( sid, key, value );
       
   442         }
       
   443     return err;
       
   444     }
       
   445 
       
   446 
       
   447 // -----------------------------------------------------------------------------
       
   448 // CIconCustomApiTestModule::DeletePsProperty
       
   449 //
       
   450 // -----------------------------------------------------------------------------
       
   451 //
       
   452 TInt CIconCustomApiTestModule::DeletePsProperty( CStifItemParser& aItem )
       
   453     {
       
   454     TInt key;
       
   455     aItem.GetNextInt( key );
       
   456     TInt err = RProperty::Delete( ProcessSID(), key );
       
   457     if( !err )
       
   458         {
       
   459         TInt index = iPublishedPsKeys.Find ( key );
       
   460         if( index >= 0 )
       
   461             {
       
   462             iPublishedPsKeys.Remove( index );
       
   463             }
       
   464         }
       
   465     return err;
       
   466     }
       
   467 
       
   468 // -----------------------------------------------------------------------------
       
   469 // CIconCustomApiTestModule::SetTestCaseAsObserver
       
   470 //
       
   471 // -----------------------------------------------------------------------------
       
   472 //
       
   473 TInt CIconCustomApiTestModule::SetTestCaseAsObserverL( CStifItemParser& aItem )
       
   474     {
       
   475     TBool setAsObserver;
       
   476     aItem.GetNextInt( setAsObserver );
       
   477     if( setAsObserver )
       
   478         {
       
   479         iTestPlugin->AddObserverL( this );
       
   480         }
       
   481     else
       
   482         {
       
   483         iTestPlugin->RemoveObserver( this );
       
   484         }
       
   485     return KErrNone;
       
   486     }
       
   487 
       
   488 // -----------------------------------------------------------------------------
       
   489 // CIconCustomApiTestModule::CheckIfHasExtensionL
       
   490 //
       
   491 // -----------------------------------------------------------------------------
       
   492 //
       
   493 TInt CIconCustomApiTestModule::CheckIfHasExtensionL( CStifItemParser& aItem )
       
   494     {
       
   495     TInt extensionUid;
       
   496     TBool expectedResult;
       
   497     User::LeaveIfError( aItem.GetNextInt( extensionUid ) );
       
   498     User::LeaveIfError( aItem.GetNextInt( expectedResult ) );
       
   499     TAny* extension =
       
   500         iTestPlugin->ContactCustomIconExtension( TUid::Uid( extensionUid ) );
       
   501     delete extension;
       
   502     if( expectedResult && extension ||
       
   503         !expectedResult && !extension )
       
   504         {
       
   505         return KErrNone;
       
   506         }
       
   507     else
       
   508         {
       
   509         return KErrGeneral;
       
   510         }
       
   511     }
       
   512 
       
   513 
       
   514 
       
   515 
       
   516 //  End of File