contacts_plat/virtual_phonebook_store_api/tsrc/T_MVPbkContactStore/src/T_MVPbkContactStoreComposite.cpp
changeset 0 e686773b3f54
child 21 9da50d567e3c
equal deleted inserted replaced
-1:000000000000 0:e686773b3f54
       
     1 /*
       
     2 * Copyright (c) 2004 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 
       
    19 //  CLASS HEADER
       
    20 #include "T_MVPbkContactStoreComposite.h"
       
    21 
       
    22 #include <CVPbkContactManager.h>
       
    23 #include <CVPbkContactViewDefinition.h>
       
    24 #include <CVPbkSortOrder.h>
       
    25 #include <CVPbkContactLinkArray.h>
       
    26 #include <CVPbkContactStoreUriArray.h>
       
    27 #include <MVPbkContactStoreList.h>
       
    28 #include <MVPbkContactStore.h>
       
    29 #include <MVPbkContactViewBase.h>
       
    30 #include <MVPbkViewContact.h>
       
    31 #include <MVPbkContactOperationBase.h>
       
    32 #include <MVPbkStoreContact.h>
       
    33 #include <MVPbkStoreContactFieldCollection.h>
       
    34 #include <MVPbkContactFieldTextData.h>
       
    35 #include <MVPbkContactViewFiltering.h>
       
    36 #include <TVPbkFieldTypeMapping.h>
       
    37 #include <TVPbkContactStoreUriPtr.h>
       
    38 
       
    39 #include <T_MVPbkContactStore.rsg>
       
    40 
       
    41 //  EXTERNAL INCLUDES
       
    42 #include <EUnitMacros.h>
       
    43 #include <barsc.h>
       
    44 #include <barsread.h>
       
    45 #include <s32mem.h>
       
    46 #include <bautils.h>
       
    47 
       
    48 //  INTERNAL INCLUDES
       
    49 
       
    50 // LOCAL
       
    51 namespace 
       
    52     {
       
    53 #if defined(__WINS__)
       
    54     _LIT( KResFile, "z:\\resource\\T_MVPbkContactStore.rsc" );
       
    55 #else
       
    56     _LIT( KResFile, "c:\\resource\\T_MVPbkContactStore.rsc" );
       
    57 #endif // defined(__WINS__)    
       
    58     _LIT( KFNameFieldValue, "Bbbfirstname" );
       
    59     _LIT( KLNameFieldValue, "Ccclastname" ); 
       
    60     _LIT( KTelNo, "555-1239876543" );
       
    61 
       
    62     _LIT( KFNameFieldValue2, "Aaafirstname" );
       
    63     _LIT( KLNameFieldValue2, "Bbblastname" );
       
    64     _LIT( KTelNo2, "+358959876543" );
       
    65 
       
    66     _LIT( KFNameFieldValue3, "Cccfirstname" );
       
    67     _LIT( KLNameFieldValue3, "Aaalastname" );
       
    68     _LIT( KTelNo3, "04051245652" );
       
    69 
       
    70     void StopScheduler( CActiveSchedulerWait& aScheduler )
       
    71         {
       
    72         if ( aScheduler.IsStarted() )
       
    73             {
       
    74             aScheduler.AsyncStop();
       
    75             }
       
    76         }
       
    77         
       
    78     _LIT( KFindString, "a" );
       
    79     _LIT( KFindStrings, "aa" );
       
    80 
       
    81     } // namespace
       
    82 
       
    83 // CALLBACK
       
    84 
       
    85 void T_MVPbkContactStoreComposite::StoreReady(MVPbkContactStore& /*aContactStore*/)
       
    86     {
       
    87     if (iStoreScheduler.IsStarted())
       
    88     	{    	
       
    89 	    iLastError = KErrNone;
       
    90 	    iLastEvent = EStoreReady;
       
    91 	    // Event coming straight from the store, so no OpenComplete() expected
       
    92 	    if ( iFlag ) 
       
    93 	        {
       
    94 	        iFlag = EFalse;
       
    95 	        if (iExpectedStoreEvent == iLastEvent)
       
    96 	        	{
       
    97 		        iExpectedStoreResponseFound = ETrue;
       
    98 	        	}
       
    99 	        StopScheduler(iStoreScheduler);
       
   100 	        }
       
   101     	}
       
   102     }
       
   103 
       
   104 void T_MVPbkContactStoreComposite::StoreUnavailable
       
   105                             (MVPbkContactStore& /*aContactStore*/, TInt aReason)
       
   106     {
       
   107     if (iStoreScheduler.IsStarted())
       
   108     	{    	
       
   109 	    iLastError = aReason;
       
   110 	    iLastEvent = EOpenError;
       
   111 	    
       
   112 	    // Event coming straight from the store, so no OpenComplete() expected
       
   113 	    if ( iFlag ) 
       
   114 	        {
       
   115 	        iFlag = EFalse;
       
   116 	        iExpectedStoreResponseFound = ETrue;
       
   117  	       StopScheduler(iStoreScheduler);
       
   118 	        }
       
   119     	}
       
   120     }
       
   121     
       
   122 void T_MVPbkContactStoreComposite::HandleStoreEventL(
       
   123                 MVPbkContactStore& /*aContactStore*/, 
       
   124                 TVPbkContactStoreEvent /*aStoreEvent*/)
       
   125 	{
       
   126 	}
       
   127 
       
   128 void T_MVPbkContactStoreComposite::OpenComplete()
       
   129     {
       
   130     if (iStoreScheduler.IsStarted())
       
   131     	{    	
       
   132 	    iLastError = KErrNone;
       
   133 	    iLastEvent = EOpenComplete;
       
   134         if (iExpectedStoreEvent == iLastEvent)
       
   135         	{
       
   136 	        iExpectedStoreResponseFound = ETrue;
       
   137         	}
       
   138 	    StopScheduler(iStoreScheduler);
       
   139     	}
       
   140     }
       
   141 
       
   142 void T_MVPbkContactStoreComposite::ContactViewReady(MVPbkContactViewBase& /*aView*/)
       
   143     {
       
   144     if ( iViewScheduler.IsStarted() )
       
   145         {
       
   146         iLastError = KErrNone;
       
   147         iLastEvent = EContactViewReady;
       
   148         if (iExpectedViewEvent == iLastEvent)
       
   149         	{
       
   150 	        iExpectedViewResponseFound = ETrue;
       
   151         	}
       
   152         StopScheduler(iViewScheduler);
       
   153         }
       
   154     }
       
   155 
       
   156 void T_MVPbkContactStoreComposite::ContactViewUnavailable(MVPbkContactViewBase& /*aView*/)
       
   157     {
       
   158     if ( iViewScheduler.IsStarted() )
       
   159         {
       
   160         iLastError = KErrNone;
       
   161         iLastEvent = EContactViewUnavailable;
       
   162         if (iExpectedViewEvent == iLastEvent)
       
   163         	{
       
   164 	        iExpectedViewResponseFound = ETrue;
       
   165         	}
       
   166         StopScheduler(iViewScheduler);
       
   167         }
       
   168     }
       
   169 
       
   170 void T_MVPbkContactStoreComposite::ContactAddedToView
       
   171         (MVPbkContactViewBase& /*aView*/, TInt /*aIndex*/, const MVPbkContactLink& /*aLink*/)
       
   172     {
       
   173     if ( iViewScheduler.IsStarted() )
       
   174         {
       
   175         iLastError = KErrNone;
       
   176         iLastEvent = EContactAddedToView;
       
   177         if (iExpectedViewEvent == iLastEvent)
       
   178         	{
       
   179 	        iExpectedViewResponseFound = ETrue;
       
   180         	}
       
   181         StopScheduler(iViewScheduler);
       
   182         }
       
   183     }
       
   184 
       
   185 void T_MVPbkContactStoreComposite::ContactRemovedFromView
       
   186         (MVPbkContactViewBase& /*aView*/, TInt /*aIndex*/, const MVPbkContactLink& /*aLink*/)
       
   187     {
       
   188     if ( iViewScheduler.IsStarted() )
       
   189         {
       
   190         iLastError = KErrNone;
       
   191         iLastEvent = EContactRemovedFromView;
       
   192         if (iExpectedViewEvent == iLastEvent)
       
   193         	{
       
   194 	        iExpectedViewResponseFound = ETrue;
       
   195         	}
       
   196         StopScheduler(iViewScheduler);
       
   197         }
       
   198     }
       
   199 
       
   200 void T_MVPbkContactStoreComposite::ContactViewError
       
   201         (MVPbkContactViewBase& /*aView*/, TInt aError, TBool /*aErrorNotified*/)
       
   202     {
       
   203     if ( iViewScheduler.IsStarted() )
       
   204         {
       
   205         iLastError = aError;
       
   206         iLastEvent = EContactViewError;
       
   207         iExpectedViewResponseFound = ETrue;
       
   208         StopScheduler(iViewScheduler);
       
   209         }
       
   210     }
       
   211 
       
   212 void T_MVPbkContactStoreComposite::StepComplete(MVPbkContactOperationBase& /*aOperation*/,
       
   213                                   TInt /*aStepSize*/)
       
   214     {
       
   215     iLastError = KErrNone;
       
   216     iLastEvent = EStepComplete;
       
   217     }
       
   218 
       
   219 TBool T_MVPbkContactStoreComposite::StepFailed(
       
   220         MVPbkContactOperationBase& /*aOperation*/,
       
   221         TInt /*aStepSize*/,
       
   222         TInt aError)
       
   223     {
       
   224     iLastError = aError;
       
   225     iLastEvent = EStepFailed;
       
   226     CActiveScheduler::Stop();
       
   227     return EFalse;
       
   228     }
       
   229 
       
   230 void T_MVPbkContactStoreComposite::OperationComplete(MVPbkContactOperationBase& /*aOperation*/)
       
   231     {
       
   232     iLastError = KErrNone;
       
   233     iLastEvent = EOperationComplete;
       
   234     CActiveScheduler::Stop();
       
   235     }
       
   236 
       
   237 void T_MVPbkContactStoreComposite::ContactOperationCompleted(TContactOpResult aResult)
       
   238     {
       
   239     if (iContactScheduler.IsStarted())
       
   240     	{
       
   241 		iLastError = KErrNone;
       
   242 		iLastEvent = EContactOperationCompleted;
       
   243 		if ( aResult.iOpCode == EContactReadAndLock || 
       
   244 		     aResult.iOpCode == EContactRead )
       
   245 		    {
       
   246 		    iStoreContact = aResult.iStoreContact;
       
   247 		    }
       
   248 		    
       
   249         if (iExpectedContactEvent == iLastEvent)
       
   250         	{
       
   251 	        iExpectedContactResponseFound = ETrue;
       
   252         	}
       
   253 		StopScheduler(iContactScheduler);
       
   254     	}
       
   255     }
       
   256 
       
   257 void T_MVPbkContactStoreComposite::ContactOperationFailed
       
   258     (TContactOp /*aOpCode*/, TInt aErrorCode, TBool /*aErrorNotified*/)
       
   259     {
       
   260     iLastError = aErrorCode;
       
   261     iLastEvent = EContactOperationFailed;
       
   262     iExpectedContactResponseFound = ETrue;
       
   263 	StopScheduler(iContactScheduler);
       
   264     }
       
   265 
       
   266 // CONSTRUCTION
       
   267 T_MVPbkContactStoreComposite* T_MVPbkContactStoreComposite::NewL(const RArray<TVPbkContactStoreUriPtr>& aUriList)
       
   268     {
       
   269     T_MVPbkContactStoreComposite* self = T_MVPbkContactStoreComposite::NewLC(aUriList); 
       
   270     CleanupStack::Pop();
       
   271 
       
   272     return self;
       
   273     }
       
   274 
       
   275 T_MVPbkContactStoreComposite* 
       
   276 T_MVPbkContactStoreComposite::NewLC(const RArray<TVPbkContactStoreUriPtr>& aUriList)
       
   277     {
       
   278     T_MVPbkContactStoreComposite* self = 
       
   279         new( ELeave ) T_MVPbkContactStoreComposite();
       
   280     CleanupStack::PushL( self );
       
   281 
       
   282 	self->ConstructL(aUriList); 
       
   283 
       
   284     return self;
       
   285     }
       
   286 
       
   287 // Destructor (virtual by CBase)
       
   288 T_MVPbkContactStoreComposite::~T_MVPbkContactStoreComposite()
       
   289     {
       
   290     delete iFindStrings;
       
   291     delete iUriArray;
       
   292     delete iStorePaths;
       
   293     iFs.Close();
       
   294     }
       
   295 
       
   296 // Default constructor
       
   297 T_MVPbkContactStoreComposite::T_MVPbkContactStoreComposite()
       
   298 : iFlag(EFalse)
       
   299     {
       
   300     }
       
   301 
       
   302 // Second phase construct
       
   303 void T_MVPbkContactStoreComposite::ConstructL(
       
   304         const RArray<TVPbkContactStoreUriPtr>& aUriList)
       
   305     {
       
   306     // The ConstructL from the base class CEUnitTestSuiteClass must be called.
       
   307     // It generates the test case table.
       
   308     CEUnitTestSuiteClass::ConstructL();
       
   309     User::LeaveIfError( iFs.Connect() );
       
   310     TInt count( aUriList.Count() );
       
   311     iStorePaths = new (ELeave) CDesCArrayFlat( count );
       
   312     iUriArray = CVPbkContactStoreUriArray::NewL();
       
   313     for ( TInt i = 0; i < count; ++i )
       
   314         {
       
   315         iStorePaths->AppendL( aUriList[ i ].UriDes() );
       
   316         iUriArray->AppendL( TVPbkContactStoreUriPtr(iStorePaths->MdcaPoint( i ) ));
       
   317         }
       
   318     }
       
   319 
       
   320 
       
   321 //  METHODS
       
   322 
       
   323 void T_MVPbkContactStoreComposite::SetupL(  )   
       
   324     {
       
   325     iFlag = EFalse;
       
   326     iContactManager = CVPbkContactManager::NewL( *iUriArray, &iFs );
       
   327     iExpectedStoreResponseFound = EFalse;
       
   328     iExpectedContactResponseFound = EFalse;    
       
   329     }
       
   330 
       
   331 void T_MVPbkContactStoreComposite::SetupEmptyL()
       
   332     {
       
   333     iFlag = EFalse;
       
   334     CVPbkContactStoreUriArray* uriArray = CVPbkContactStoreUriArray::NewLC();
       
   335     iContactManager = CVPbkContactManager::NewL( *uriArray, &iFs );
       
   336     CleanupStack::PopAndDestroy(uriArray);
       
   337     }
       
   338 
       
   339 void T_MVPbkContactStoreComposite::SetupStoreL()
       
   340     {
       
   341     SetupL();
       
   342 
       
   343     ReplaceAllStoresL();
       
   344 
       
   345     PopulateStoresL();
       
   346     }
       
   347 
       
   348 void T_MVPbkContactStoreComposite::SetupViewL(TInt aViewDefResId, TInt aSortOrderResId)
       
   349     {
       
   350     TFileName path;        
       
   351     // NearestLanguageFile takes only TFileName
       
   352     path.Zero();
       
   353     path.Append( KResFile() );
       
   354 
       
   355     if ( path.Length() != 0 )
       
   356         {
       
   357         BaflUtils::NearestLanguageFile( iFs, path );
       
   358         }
       
   359 	
       
   360     RResourceFile resFile;
       
   361     CleanupClosePushL(resFile);
       
   362     resFile.OpenL( iFs, path );
       
   363     TResourceReader reader;
       
   364     HBufC8* buffer = resFile.AllocReadLC(aViewDefResId);
       
   365     reader.SetBuffer(buffer);
       
   366 
       
   367     CVPbkContactViewDefinition* def = CVPbkContactViewDefinition::NewL(reader);
       
   368     CleanupStack::PopAndDestroy(buffer);
       
   369     CleanupStack::PushL(def);
       
   370 
       
   371     CVPbkSortOrder* sortOrder = CreateSortOrderLC( aSortOrderResId );
       
   372 
       
   373     iView = iContactManager->CreateContactViewLC( *this, *def, *sortOrder );
       
   374     CleanupStack::Pop(); // iView
       
   375     EUNIT_ASSERT( iView );
       
   376     CleanupStack::PopAndDestroy(3); // sortOrder, def, resFile
       
   377 
       
   378     StartViewSchedulerAndDecideToLeaveL(EContactViewReady);
       
   379     EUNIT_ASSERT( iLastError == KErrNone );
       
   380     EUNIT_ASSERT( iLastEvent == iExpectedViewEvent );
       
   381     }
       
   382 
       
   383 void T_MVPbkContactStoreComposite::SetupOrderedFlatCompositeViewL()
       
   384     {
       
   385     SetupStoreL();
       
   386     
       
   387     SetupCreateFindStringsL();
       
   388 
       
   389     SetupViewL(R_ORDERED_FLAT_COMPOSITE_CONTACT_VIEW, R_SORTORDER_LASTNAME_FIRSTNAME);
       
   390     }
       
   391 
       
   392 void T_MVPbkContactStoreComposite::SetupOrderedComplexCompositeViewL()
       
   393     {
       
   394     SetupStoreL();
       
   395     
       
   396     SetupCreateFindStringsL();
       
   397 
       
   398     SetupViewL(R_ORDERED_COMPLEX_COMPOSITE_CONTACT_VIEW, R_SORTORDER_LASTNAME_FIRSTNAME);
       
   399     }
       
   400 
       
   401 void T_MVPbkContactStoreComposite::SetupSortedFlatCompositeViewL()
       
   402     {
       
   403     SetupStoreL();
       
   404     
       
   405     SetupCreateFindStringsL();
       
   406 
       
   407     SetupViewL(R_SORTED_FLAT_COMPOSITE_CONTACT_VIEW, R_SORTORDER_LASTNAME_FIRSTNAME);
       
   408     }
       
   409 
       
   410 void T_MVPbkContactStoreComposite::SetupSortedComplexCompositeViewL()
       
   411     {
       
   412     SetupStoreL();
       
   413     
       
   414     SetupCreateFindStringsL();
       
   415 
       
   416     SetupViewL(R_SORTED_COMPLEX_COMPOSITE_CONTACT_VIEW, R_SORTORDER_LASTNAME_FIRSTNAME);
       
   417     }
       
   418 
       
   419 void T_MVPbkContactStoreComposite::SetupStoreWithContactsL()
       
   420     {
       
   421     SetupStoreL();
       
   422 
       
   423     PopulateStoresL();
       
   424     }
       
   425 
       
   426 void T_MVPbkContactStoreComposite::SetupCreateFindStringsL()
       
   427     {
       
   428     iFindStrings = new(ELeave)CDesCArrayFlat( 2 );
       
   429     }
       
   430 
       
   431 void T_MVPbkContactStoreComposite::Teardown(  )
       
   432     {
       
   433     delete iFindStrings;
       
   434     iFindStrings = NULL;
       
   435     
       
   436     delete iStoreContact; iStoreContact = NULL;
       
   437     delete iView; iView = NULL;
       
   438     if ( iStoreList )
       
   439         {
       
   440         iStoreList->CloseAll( *this );
       
   441         }
       
   442     iStoreList = NULL;
       
   443     delete iContactManager; iContactManager = NULL;
       
   444     }
       
   445 
       
   446 
       
   447 // TESTS
       
   448 
       
   449 void T_MVPbkContactStoreComposite::TestGetStoreListL()
       
   450     {
       
   451     iStoreList = &iContactManager->ContactStoresL();
       
   452     EUNIT_ASSERT( iStoreList->Count() == iUriArray->Count() );
       
   453     }
       
   454 
       
   455 void T_MVPbkContactStoreComposite::TestLoadContactStoreL()
       
   456     {
       
   457     const TInt count = iUriArray->Count();
       
   458     for ( TInt i = 0; i < count; ++i )
       
   459         {
       
   460         iContactManager->LoadContactStoreL( (*iUriArray)[i] );
       
   461         }
       
   462     iStoreList = &iContactManager->ContactStoresL();
       
   463     EUNIT_ASSERT( iStoreList->Count() == count );
       
   464     }
       
   465 
       
   466 void T_MVPbkContactStoreComposite::TestOpeningAllStoresL(  )
       
   467     {
       
   468     iStoreList = &iContactManager->ContactStoresL();
       
   469     TInt storeCount ( iStoreList->Count() );
       
   470     EUNIT_ASSERT_EQUALS_DESC( storeCount, 4, "Store count check failed." );
       
   471 
       
   472     // Try to open all stores.
       
   473     iStoreList->OpenAllL( *this );
       
   474 
       
   475     // We have to get EOpenComplete event.
       
   476     StartStoreSchedulerAndDecideToLeaveL(EOpenComplete);
       
   477     EUNIT_ASSERT( iLastError == KErrNone );
       
   478     EUNIT_ASSERT( iLastEvent == iExpectedStoreEvent );
       
   479     }
       
   480 
       
   481 void T_MVPbkContactStoreComposite::TestViewContactCountL()
       
   482     {
       
   483     // There is at least 3 * storecount contacts available
       
   484     EUNIT_ASSERT( iView->ContactCountL() >= ( iUriArray->Count() * 3 ) );
       
   485     }
       
   486 
       
   487 void T_MVPbkContactStoreComposite::TestCommitMultipleContactsL()
       
   488     {
       
   489     TInt cntCount( iView->ContactCountL() );
       
   490     RArray<MVPbkStoreContact*> storeCnts;
       
   491     CleanupClosePushL( storeCnts );
       
   492     
       
   493     iView->ContactAtL( 0 ).ReadAndLockL( *this );
       
   494 
       
   495     StartContactSchedulerAndDecideToLeaveL(EContactOperationCompleted);
       
   496     EUNIT_ASSERT( iLastError == KErrNone );
       
   497     EUNIT_ASSERT( iLastEvent == iExpectedContactEvent );
       
   498     iExpectedContactResponseFound = EFalse;
       
   499 
       
   500     MVPbkStoreContact* cnt1 = iStoreContact;
       
   501     iStoreContact = NULL;
       
   502     CleanupDeletePushL( cnt1 );
       
   503     storeCnts.Append( cnt1 );
       
   504 
       
   505     // get last contact from view
       
   506     iView->ContactAtL( cntCount - 1 ).ReadAndLockL( *this ); 
       
   507 
       
   508     StartContactSchedulerAndDecideToLeaveL(EContactOperationCompleted);
       
   509     EUNIT_ASSERT( iLastError == KErrNone );
       
   510     EUNIT_ASSERT( iLastEvent == iExpectedContactEvent );
       
   511     iExpectedContactResponseFound = EFalse;
       
   512 
       
   513     MVPbkStoreContact* cnt2 = iStoreContact;
       
   514     iStoreContact = NULL;
       
   515     CleanupDeletePushL( cnt2 );
       
   516     storeCnts.Append( cnt2 );
       
   517         
       
   518     MVPbkContactOperationBase* op = 
       
   519         iContactManager->CommitContactsL( storeCnts.Array(), *this );
       
   520     CleanupDeletePushL( op );
       
   521 
       
   522     StartSchedulerAndDecideToLeaveL();
       
   523     EUNIT_ASSERT( iLastError == KErrNone );
       
   524     EUNIT_ASSERT( iLastEvent == EOperationComplete );
       
   525     EUNIT_ASSERT( iView->ContactCountL() == cntCount );
       
   526 
       
   527     CleanupStack::PopAndDestroy( 4 ); // op, cnt2, cnt1, storeCnts
       
   528     }
       
   529 
       
   530 void T_MVPbkContactStoreComposite::TestDeleteMultipleContactsL()
       
   531     {
       
   532     TInt deletecount( 2 );
       
   533     TInt cntCount( iView->ContactCountL() );
       
   534     CVPbkContactLinkArray* contactLinks = CVPbkContactLinkArray::NewLC();
       
   535 
       
   536     for ( TInt i = 0; i < deletecount; ++i )
       
   537         {
       
   538         MVPbkContactLink* lnk = iView->ContactAtL( i ).CreateLinkLC();
       
   539         contactLinks->AppendL( lnk );
       
   540         CleanupStack::Pop(); // lnk
       
   541         }
       
   542 
       
   543     MVPbkContactOperationBase* op = 
       
   544         iContactManager->DeleteContactsL( *contactLinks, *this);
       
   545     CleanupDeletePushL( op );
       
   546     
       
   547     // TODO: This part of VPBkEng under construction...
       
   548     StartSchedulerAndDecideToLeaveL();
       
   549     EUNIT_ASSERT( iLastError == KErrNone );
       
   550     EUNIT_ASSERT( iLastEvent == EOperationComplete );
       
   551     /*
       
   552     StartSchedulerAndDecideToLeaveL();
       
   553     EUNIT_ASSERT( iLastError == KErrNone );
       
   554     EUNIT_ASSERT( iLastEvent == EContactViewUnavailable );
       
   555     StartSchedulerAndDecideToLeaveL();
       
   556     EUNIT_ASSERT( iLastError == KErrNone );
       
   557     EUNIT_ASSERT( iLastEvent == EContactViewReady );
       
   558     */
       
   559     
       
   560     EUNIT_ASSERT( iView->ContactCountL() == ( cntCount - deletecount ) );
       
   561 
       
   562     CleanupStack::PopAndDestroy(); // op
       
   563     CleanupStack::PopAndDestroy(); // contactLnks
       
   564     }
       
   565     
       
   566 void T_MVPbkContactStoreComposite::TestCreateFilteredViewL()
       
   567     {
       
   568     // First check that the view supports filtering
       
   569     EUNIT_ASSERT( iView->ViewFiltering() );
       
   570     
       
   571     iFindStrings->AppendL( KFindString );
       
   572        
       
   573     MVPbkContactViewBase* findView = 
       
   574         iView->ViewFiltering()->CreateFilteredViewLC( *this, *iFindStrings, 
       
   575             NULL );
       
   576     StartViewSchedulerAndDecideToLeaveL(EContactViewReady);
       
   577     
       
   578     EUNIT_ASSERT( iLastError == KErrNone );
       
   579     EUNIT_ASSERT( iLastEvent == EContactViewReady );    
       
   580     
       
   581     CleanupStack::PopAndDestroy( 1 ); // findView
       
   582     }
       
   583     
       
   584 void T_MVPbkContactStoreComposite::TestCreateFilteredView2L()
       
   585     {
       
   586     // First check that the view supports filtering
       
   587     EUNIT_ASSERT( iView->ViewFiltering() );
       
   588     
       
   589     iFindStrings->AppendL( KFindString );
       
   590     
       
   591     // First character
       
   592     MVPbkContactViewBase* findView = 
       
   593         iView->ViewFiltering()->CreateFilteredViewLC( *this, 
       
   594             *iFindStrings, NULL ); 
       
   595     StartViewSchedulerAndDecideToLeaveL(EContactViewReady);
       
   596     
       
   597     iFindStrings->Reset();
       
   598     iFindStrings->AppendL( KFindStrings );
       
   599     
       
   600     // Second character
       
   601     MVPbkContactViewBase* refineView = 
       
   602         findView->ViewFiltering()->CreateFilteredViewLC( *this, 
       
   603             *iFindStrings, NULL );
       
   604     StartViewSchedulerAndDecideToLeaveL(EContactViewReady);    
       
   605     
       
   606     EUNIT_ASSERT( iLastError == KErrNone );
       
   607     EUNIT_ASSERT( iLastEvent == EContactViewReady );
       
   608     
       
   609     CleanupStack::PopAndDestroy( 2 ); // findView, refineView
       
   610     }
       
   611         
       
   612 void T_MVPbkContactStoreComposite::TestDeleteContactsFromFilteredViewL()
       
   613     {
       
   614     // First check that the view supports filtering
       
   615     EUNIT_ASSERT( iView->ViewFiltering() );
       
   616     
       
   617     iFindStrings->AppendL( KFindString );
       
   618     
       
   619     // First character
       
   620     MVPbkContactViewBase* findView = 
       
   621         iView->ViewFiltering()->CreateFilteredViewLC( *this, 
       
   622             *iFindStrings, NULL );
       
   623     StartViewSchedulerAndDecideToLeaveL(EContactViewReady);
       
   624     
       
   625     iFindStrings->Reset();
       
   626     iFindStrings->AppendL( KFindStrings );
       
   627     
       
   628     // Second character
       
   629     MVPbkContactViewBase* refineView = 
       
   630         findView->ViewFiltering()->CreateFilteredViewLC( *this, 
       
   631             *iFindStrings, NULL );
       
   632     StartViewSchedulerAndDecideToLeaveL(EContactViewReady);    
       
   633         
       
   634     TestDeleteMultipleContactsL();
       
   635             
       
   636     CleanupStack::PopAndDestroy( 2 ); // findView, refineView        
       
   637     }
       
   638 
       
   639 // HELP FUNCTIONS
       
   640 
       
   641 void T_MVPbkContactStoreComposite::ReplaceAllStoresL()
       
   642     {
       
   643     iStoreList = &iContactManager->ContactStoresL();
       
   644     TInt count ( iStoreList->Count() );
       
   645     
       
   646     for ( TInt i = 0; i < count; ++i )
       
   647         {
       
   648         MVPbkContactStore& contactStore = iStoreList->At( i );
       
   649         contactStore.ReplaceL( *this );
       
   650 	    
       
   651 	    // Flag to notice that we dont wait OpenComplete event
       
   652 	    // but StoreReady or StoreError event
       
   653 	    iFlag = ETrue;
       
   654 	    
       
   655         StartStoreSchedulerAndDecideToLeaveL(EStoreReady);
       
   656         EUNIT_ASSERT( iLastError == KErrNone );
       
   657         EUNIT_ASSERT( iLastEvent == iExpectedStoreEvent );
       
   658         }
       
   659     }
       
   660 
       
   661 CVPbkSortOrder* T_MVPbkContactStoreComposite::CreateSortOrderLC( TInt aResId )
       
   662     {
       
   663     TFileName path;        
       
   664     // NearestLanguageFile takes only TFileName
       
   665     path.Zero();
       
   666     path.Append( KResFile() );
       
   667 
       
   668     if ( path.Length() != 0 )
       
   669         {
       
   670         BaflUtils::NearestLanguageFile( iFs, path );
       
   671         }
       
   672 	
       
   673     RResourceFile resFile;	
       
   674     CleanupClosePushL( resFile );
       
   675     resFile.OpenL( iFs, path );
       
   676     TResourceReader reader;
       
   677     HBufC8* buffer = resFile.AllocReadLC( aResId );
       
   678     reader.SetBuffer(buffer);
       
   679 
       
   680     CVPbkSortOrder* sortOrder = CVPbkSortOrder::NewL( reader, iContactManager->FieldTypes() );
       
   681     CleanupStack::PopAndDestroy( 2 ); // buffer, resFile
       
   682     CleanupStack::PushL( sortOrder );
       
   683     return sortOrder;
       
   684     }
       
   685 
       
   686 void T_MVPbkContactStoreComposite::PopulateStoresL()
       
   687     {
       
   688     TInt storeCount( iStoreList->Count() );
       
   689     for ( TInt i = 0; i < storeCount; ++i )
       
   690         {
       
   691         MVPbkContactStore* store = &iStoreList->At( i );
       
   692         MVPbkStoreContact* storeContact = store->CreateNewContactLC();
       
   693         AddContactFieldsAndSaveL( *storeContact, KFNameFieldValue, KLNameFieldValue, KTelNo );
       
   694         CleanupStack::PopAndDestroy(); // storeContact
       
   695         storeContact = store->CreateNewContactLC();
       
   696         AddContactFieldsAndSaveL( *storeContact, KFNameFieldValue2, KLNameFieldValue2, KTelNo2 );
       
   697         CleanupStack::PopAndDestroy(); // storeContact
       
   698         storeContact = store->CreateNewContactLC();
       
   699         AddContactFieldsAndSaveL( *storeContact, KFNameFieldValue3, KLNameFieldValue3, KTelNo3 );
       
   700         CleanupStack::PopAndDestroy(); // storeContact
       
   701         }
       
   702     }
       
   703 
       
   704 void T_MVPbkContactStoreComposite::AddContactFieldsAndSaveL( 
       
   705     MVPbkStoreContact& aStoreContact, const TDesC& aFName, const TDesC& aLName, const TDesC& aTelNo )
       
   706     {
       
   707     AddFieldToContact( aStoreContact, R_FIRST_NAME_TYPE, aFName );
       
   708     AddFieldToContact( aStoreContact, R_LAST_NAME_TYPE, aLName );
       
   709     AddFieldToContact( aStoreContact, R_TEL_NO_TYPE, aTelNo );
       
   710    
       
   711     // Save modifications to the storecontact
       
   712     aStoreContact.CommitL( *this );
       
   713     StartContactSchedulerAndDecideToLeaveL(EContactOperationCompleted);
       
   714     EUNIT_ASSERT( iLastError == KErrNone );
       
   715     EUNIT_ASSERT( iLastEvent == iExpectedContactEvent );
       
   716     iExpectedContactResponseFound = EFalse;
       
   717     if ( iView ) // If no view, there will be no events to it
       
   718         {
       
   719         StartViewSchedulerAndDecideToLeaveL(EContactAddedToView);
       
   720         EUNIT_ASSERT( iLastError == KErrNone );
       
   721         EUNIT_ASSERT( iLastEvent == iExpectedViewEvent );
       
   722         }
       
   723     }
       
   724 
       
   725 void T_MVPbkContactStoreComposite::AddFieldToContact( MVPbkStoreContact& aStoreContact, TInt aResId, const TDesC& aValue )
       
   726     {
       
   727     TInt fldCount( aStoreContact.Fields().FieldCount() );
       
   728     // Create fieldtype
       
   729     TVPbkFieldTypeMapping fieldTypeMapping = CreateFieldTypeMappingL( aResId );
       
   730     const MVPbkFieldType* fieldType = 
       
   731         fieldTypeMapping.FindMatch( iContactManager->FieldTypes() );
       
   732     EUNIT_ASSERT( fieldType );
       
   733     // Add value to the field
       
   734     AddValueToContactFieldL( aStoreContact, *fieldType, aValue );
       
   735     EUNIT_ASSERT( aStoreContact.Fields().FieldCount() == ++fldCount );
       
   736     }
       
   737 
       
   738 void T_MVPbkContactStoreComposite::AddValueToContactFieldL( MVPbkStoreContact& aStoreContact, 
       
   739     const MVPbkFieldType& aFieldType, const TDesC& aValue )
       
   740     {
       
   741     MVPbkStoreContactField* field = aStoreContact.CreateFieldLC( aFieldType );
       
   742     EUNIT_ASSERT( field->FieldData().DataType() == EVPbkFieldStorageTypeText );
       
   743     MVPbkContactFieldTextData::Cast( field->FieldData() ).SetTextL( aValue );
       
   744     aStoreContact.AddFieldL( field );
       
   745     CleanupStack::Pop();    
       
   746     }
       
   747 
       
   748 TVPbkFieldTypeMapping T_MVPbkContactStoreComposite::CreateFieldTypeMappingL( const TInt aResId )
       
   749     {
       
   750     TFileName path;        
       
   751     // NearestLanguageFile takes only TFileName
       
   752     path.Zero();
       
   753     path.Append( KResFile() );
       
   754 
       
   755     if ( path.Length() != 0 )
       
   756         {
       
   757         BaflUtils::NearestLanguageFile( iFs, path );
       
   758         }
       
   759 	
       
   760     RResourceFile resFile;	
       
   761     CleanupClosePushL( resFile );
       
   762     resFile.OpenL( iFs, path );
       
   763     TResourceReader reader;
       
   764     HBufC8* buffer = resFile.AllocReadLC( aResId );
       
   765     reader.SetBuffer(buffer);
       
   766 
       
   767     TVPbkFieldTypeMapping result(reader);
       
   768     CleanupStack::PopAndDestroy( 2 ); // buffer, resFile
       
   769 
       
   770     return result;
       
   771     }
       
   772 
       
   773 void T_MVPbkContactStoreComposite::StartSchedulerAndDecideToLeaveL()
       
   774     {
       
   775     CActiveScheduler::Start();
       
   776     if ( iLastError == KErrNoMemory )
       
   777         {
       
   778         User::Leave( iLastError );
       
   779         }
       
   780     }
       
   781     
       
   782 void T_MVPbkContactStoreComposite::StartStoreSchedulerAndDecideToLeaveL(TInt aExpectedEvent)
       
   783 	{
       
   784 	iExpectedStoreEvent = aExpectedEvent;
       
   785     while (!iExpectedStoreResponseFound)
       
   786     	{	
       
   787 	    iStoreScheduler.Start();
       
   788 	    if ( iLastError == KErrNoMemory )
       
   789 	        {
       
   790 	        User::Leave( iLastError );
       
   791 	        }
       
   792     	}	
       
   793     iExpectedStoreResponseFound = EFalse;
       
   794 	}
       
   795     
       
   796 void T_MVPbkContactStoreComposite::StartContactSchedulerAndDecideToLeaveL(TInt aExpectedEvent)
       
   797 	{
       
   798 	iExpectedContactEvent = aExpectedEvent;
       
   799     while (!iExpectedContactResponseFound)
       
   800     	{	
       
   801 	    iContactScheduler.Start();
       
   802 	    if ( iLastError == KErrNoMemory )
       
   803 	        {
       
   804 	        User::Leave( iLastError );
       
   805 	        }
       
   806     	}	
       
   807     iExpectedContactResponseFound = EFalse;
       
   808 	}
       
   809 
       
   810 
       
   811 void T_MVPbkContactStoreComposite::StartViewSchedulerAndDecideToLeaveL(TInt aExpectedEvent)
       
   812     {
       
   813     iExpectedViewEvent = aExpectedEvent;
       
   814     while (!iExpectedViewResponseFound)
       
   815     	{	
       
   816 	    iViewScheduler.Start();
       
   817 	    if ( iLastError == KErrNoMemory )
       
   818 	        {
       
   819 	        User::Leave( iLastError );
       
   820 	        }
       
   821     	}
       
   822     iExpectedViewResponseFound = EFalse;
       
   823     }	
       
   824 	
       
   825 //  TEST TABLE
       
   826 
       
   827 EUNIT_BEGIN_TEST_TABLE( 
       
   828     T_MVPbkContactStoreComposite,
       
   829     "VPbkApiTester::MVPbkContactStoreComposite",
       
   830     "MODULE" )
       
   831 
       
   832 EUNIT_TEST(
       
   833     "Retrieving stores from contact manager",
       
   834     "MVPbkContactStoreList",
       
   835     "ContactStoresL",
       
   836     "FUNCTIONALITY",
       
   837     SetupL, TestGetStoreListL, Teardown )
       
   838 
       
   839 EUNIT_TEST(
       
   840     "Loading stores dynamically",
       
   841     "CVPbkContactManager",
       
   842     "LoadContactStoreL",
       
   843     "FUNCTIONALITY",
       
   844     SetupL, TestLoadContactStoreL, Teardown )
       
   845 
       
   846 EUNIT_TEST(
       
   847     "Opening all stores",
       
   848     "MVPbkContactStoreList",
       
   849     "OpenAllL",
       
   850     "FUNCTIONALITY",
       
   851     SetupStoreL, TestOpeningAllStoresL, Teardown )
       
   852 
       
   853 EUNIT_TEST(
       
   854     "View's contact count",
       
   855     "MVPbkContactViewBase",
       
   856     "ContactCountL",
       
   857     "FUNCTIONALITY",
       
   858     SetupOrderedFlatCompositeViewL, TestViewContactCountL, Teardown )
       
   859 
       
   860 EUNIT_TEST(
       
   861     "View's contact count",
       
   862     "MVPbkContactViewBase",
       
   863     "ContactCountL",
       
   864     "FUNCTIONALITY",
       
   865     SetupOrderedComplexCompositeViewL, TestViewContactCountL, Teardown )
       
   866 
       
   867 EUNIT_TEST(
       
   868     "View's contact count",
       
   869     "MVPbkContactViewBase",
       
   870     "ContactCountL",
       
   871     "FUNCTIONALITY",
       
   872     SetupSortedFlatCompositeViewL, TestViewContactCountL, Teardown )
       
   873 
       
   874 EUNIT_TEST(
       
   875     "View's contact count",
       
   876     "MVPbkContactViewBase",
       
   877     "ContactCountL",
       
   878     "FUNCTIONALITY",
       
   879     SetupSortedComplexCompositeViewL, TestViewContactCountL, Teardown )
       
   880 
       
   881 EUNIT_TEST(
       
   882     "Commit of multiple contacts",
       
   883     "CVPbkContactManager",
       
   884     "CommitContactsL",
       
   885     "FUNCTIONALITY",
       
   886     SetupOrderedFlatCompositeViewL, TestCommitMultipleContactsL, Teardown )
       
   887 
       
   888 EUNIT_TEST(
       
   889     "Deletion of multiple contacts",
       
   890     "CVPbkContactManager",
       
   891     "DeleteContactsL",
       
   892     "FUNCTIONALITY",
       
   893     SetupOrderedFlatCompositeViewL, TestDeleteMultipleContactsL, Teardown )
       
   894     
       
   895 EUNIT_TEST(
       
   896     "Create filtered view. One character.",
       
   897     "MVPbkContactViewBase",
       
   898     "CreateFilteredViewLC",
       
   899     "FUNCTIONALITY",
       
   900     SetupSortedFlatCompositeViewL, TestCreateFilteredViewL, Teardown )    
       
   901 
       
   902 EUNIT_TEST(
       
   903     "Create filtered view. One character.",
       
   904     "MVPbkContactViewBase",
       
   905     "CreateFilteredViewLC",
       
   906     "FUNCTIONALITY",
       
   907     SetupOrderedFlatCompositeViewL, TestCreateFilteredViewL, Teardown )        
       
   908     
       
   909 EUNIT_TEST(
       
   910     "Create filtered view. One character.",
       
   911     "MVPbkContactViewBase",
       
   912     "CreateFilteredViewLC",
       
   913     "FUNCTIONALITY",
       
   914     SetupSortedComplexCompositeViewL, TestCreateFilteredViewL, Teardown )    
       
   915 
       
   916 EUNIT_TEST(
       
   917     "Create filtered view. One character.",
       
   918     "MVPbkContactViewBase",
       
   919     "CreateFilteredViewLC",
       
   920     "FUNCTIONALITY",
       
   921     SetupOrderedComplexCompositeViewL, TestCreateFilteredViewL, Teardown )   
       
   922 
       
   923 EUNIT_TEST(
       
   924     "Create filtered view. Two character.",
       
   925     "MVPbkContactViewBase",
       
   926     "CreateFilteredViewLC",
       
   927     "FUNCTIONALITY",
       
   928     SetupSortedFlatCompositeViewL, TestCreateFilteredView2L, Teardown )    
       
   929 
       
   930 EUNIT_TEST(
       
   931     "Create filtered view. Two character.",
       
   932     "MVPbkContactViewBase",
       
   933     "CreateFilteredViewLC",
       
   934     "FUNCTIONALITY",
       
   935     SetupOrderedFlatCompositeViewL, TestCreateFilteredView2L, Teardown )        
       
   936     
       
   937 EUNIT_TEST(
       
   938     "Create filtered view. Two character.",
       
   939     "MVPbkContactViewBase",
       
   940     "CreateFilteredViewLC",
       
   941     "FUNCTIONALITY",
       
   942     SetupSortedComplexCompositeViewL, TestCreateFilteredView2L, Teardown )    
       
   943 
       
   944 EUNIT_TEST(
       
   945     "Create filtered view. Two character.",
       
   946     "MVPbkContactViewBase",
       
   947     "CreateFilteredViewLC",
       
   948     "FUNCTIONALITY",
       
   949     SetupOrderedComplexCompositeViewL, TestCreateFilteredView2L, Teardown ) 
       
   950     
       
   951 EUNIT_TEST(
       
   952     "Delete contacts from filtered view.",
       
   953     "CVPbkContactManager",
       
   954     "DeleteContactsL",
       
   955     "FUNCTIONALITY",
       
   956     SetupOrderedFlatCompositeViewL, TestDeleteContactsFromFilteredViewL, Teardown )                 
       
   957     
       
   958 EUNIT_END_TEST_TABLE
       
   959 
       
   960 //  END OF FILE