phonebookengines/contactsmodel/tsrc/Integration/TestStartUp/src/TestContactsDbStep.cpp
changeset 0 e686773b3f54
child 24 0ba2181d7c28
equal deleted inserted replaced
-1:000000000000 0:e686773b3f54
       
     1 // Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 //
       
    15 
       
    16 #include "TestContactsDbStep.h"
       
    17 #include "cntviewstoreprivate.h"
       
    18 #include <e32panic.h>
       
    19 
       
    20 
       
    21 CTestContactsDbStep::~CTestContactsDbStep()
       
    22 /**
       
    23  * Destructor
       
    24  */
       
    25 	{
       
    26 	delete iDb;
       
    27 	iDb = NULL;
       
    28 	}
       
    29 
       
    30 CTestContactsDbStep::CTestContactsDbStep() 
       
    31 /**
       
    32  * Constructor
       
    33  */
       
    34 	{ 
       
    35 	// Call base class method to set up the human readable name for logging
       
    36 	SetTestStepName(KTestContactsDbStep);
       
    37 	}
       
    38 
       
    39 TVerdict CTestContactsDbStep::doTestStepPreambleL()
       
    40 /**Step
       
    41  * @return - TVerdict code
       
    42  * Override of base class virtual
       
    43  */
       
    44  	{
       
    45  	SetTestStepResult(EPass);
       
    46  	
       
    47  	GetStringFromConfig(ConfigSection(),KCntDbActionTag,iCntsDbAction); 
       
    48  	GetIntFromConfig(ConfigSection(),KNoOfCntsTag,iNumberOfContacts);
       
    49  	
       
    50  	CActiveScheduler* iSched = NULL;
       
    51     iSched = new(ELeave) CActiveScheduler;
       
    52 	CActiveScheduler::Install(iSched);
       
    53  		
       
    54  	return TestStepResult();
       
    55  	}
       
    56  	
       
    57 
       
    58 TVerdict CTestContactsDbStep::doTestStepL()
       
    59 /**
       
    60  * @return - TVerdict code
       
    61  * Override of base class virtual
       
    62  */
       
    63     {
       
    64     SetTestStepResult(EPass);
       
    65 	
       
    66     if (iCntsDbAction.Compare(KCreateDb) == KErrNone)
       
    67         {
       
    68     	CreateDefaultCntsDb();
       
    69         }
       
    70         
       
    71     else if (iCntsDbAction.Compare(KOpenDb) == KErrNone)
       
    72         {
       
    73     	OpenDefaultCntsDb();
       
    74         }
       
    75         
       
    76     else if (iCntsDbAction.Compare(KReplaceDb) == KErrNone)
       
    77         {
       
    78     	ReplaceDefaultCntsDb();
       
    79         }
       
    80         
       
    81     else if (iCntsDbAction.Compare(KAddItemsDb) == KErrNone)
       
    82         {
       
    83     	TRAPD(err,AddContactsL(iNumberOfContacts));
       
    84     	if (err != KErrNone)
       
    85     	    {
       
    86     		_LIT(KAddCntLeft,"AddContactsL Left, err = %d");
       
    87     		INFO_PRINTF2(KAddCntLeft, err);
       
    88     		SetTestStepResult(EFail);
       
    89     	    }
       
    90         }
       
    91     
       
    92     else if (iCntsDbAction.Compare(KDeleteDb) == KErrNone)
       
    93         {
       
    94     	DeleteDefaultCntsDb();
       
    95         }
       
    96         
       
    97     else if (iCntsDbAction.Compare(KCheckCntViewDefs) == KErrNone)
       
    98         {
       
    99         
       
   100 #ifdef _DEBUG
       
   101 
       
   102     	TBool res = CheckExistingViewDefsL();
       
   103     	if (!res)
       
   104     	    {
       
   105     		SetTestStepResult(EFail);
       
   106     	    }
       
   107 #endif
       
   108         }
       
   109         
       
   110     else
       
   111         {
       
   112     	//illegal contacts db action
       
   113     	SetTestStepResult(EFail);
       
   114         }
       
   115         
       
   116     return TestStepResult();
       
   117     }
       
   118     
       
   119 
       
   120 TVerdict CTestContactsDbStep::doTestStepPostambleL()
       
   121 /**
       
   122  * @return - TVerdict code
       
   123  * Override of base class virtual
       
   124  */
       
   125     {
       
   126     delete iSched;
       
   127 	iSched = NULL;
       
   128 	CActiveScheduler::Install(NULL);
       
   129 	
       
   130 	return TestStepResult();
       
   131     }
       
   132 
       
   133 
       
   134 /**
       
   135  * @return - err 
       
   136  * Implements a method to create a default contacts database.
       
   137  */    
       
   138 void CTestContactsDbStep::CreateDefaultCntsDb()   
       
   139     {
       
   140     TRAPD(err,iDb = CContactDatabase::CreateL());
       
   141     if ((err != KErrNone) && (err != KErrAlreadyExists))
       
   142         {
       
   143         _LIT(KNoDbCreate,"Could not create cnts default db, err = %d");
       
   144         ERR_PRINTF2(KNoDbCreate,err);  
       
   145         SetTestStepResult(EFail);
       
   146         }
       
   147     }
       
   148 
       
   149 
       
   150 /**
       
   151  * @return - err
       
   152  * Implements a method to open a default contacts database.
       
   153  */
       
   154 void CTestContactsDbStep::OpenDefaultCntsDb()   
       
   155     {
       
   156     TRAPD(err,iDb = CContactDatabase::OpenL());
       
   157     if (err != KErrNone)
       
   158         {
       
   159         _LIT(KNoDbOpen,"Could not open cnts default db, err = %d");
       
   160         ERR_PRINTF2(KNoDbOpen,err);  
       
   161         SetTestStepResult(EFail);
       
   162         }
       
   163     }
       
   164     
       
   165 
       
   166 /**
       
   167  * @return err
       
   168  * Implements a method to delete and a default contacts database.
       
   169  */
       
   170 void CTestContactsDbStep::DeleteDefaultCntsDb()   
       
   171     {
       
   172     delete iDb;
       
   173     iDb = NULL;
       
   174     
       
   175     TRAPD(err,CContactDatabase::DeleteDefaultFileL());
       
   176     if(err != KErrNone)
       
   177         {
       
   178     	_LIT(KNoDbDel,"Could not delete cnts default db, err = %d");
       
   179         ERR_PRINTF2(KNoDbDel,err);  
       
   180         SetTestStepResult(EFail);
       
   181         }
       
   182     }
       
   183     
       
   184     
       
   185 /**
       
   186  * Implements a method to replace the default contacts database
       
   187  */   
       
   188 void CTestContactsDbStep::ReplaceDefaultCntsDb()
       
   189     {
       
   190 	TRAPD(err,iDb = CContactDatabase::ReplaceL())
       
   191 	if (err != KErrNone)
       
   192 	    {
       
   193 		_LIT(KNoRep,"Could not replace default contacts db, err = %d");
       
   194 		ERR_PRINTF2(KNoRep,err);
       
   195 		SetTestStepResult(EFail);
       
   196 	    }  
       
   197     }
       
   198 
       
   199 
       
   200 /**
       
   201  * @return TBool
       
   202  * Implements a method to check the remote view definitions used
       
   203  * by the cnts server when it is running. They are not necessarily
       
   204  * the same as the view definitions stored in central repository.
       
   205  * The views are fetched using a debug mode only, test API, and compared
       
   206  * against the data in the ini file.
       
   207  */
       
   208 TBool CTestContactsDbStep::CheckExistingViewDefsL()
       
   209     {        
       
   210  	RPointerArray<CContactDefaultViewDefinition> existingViewDefs;
       
   211  	RContactViewSortOrder viewSortOrder;
       
   212  	 
       
   213  	TInt noOfViewsFromIni;
       
   214  	GetIntFromConfig(ConfigSection(),KNoOfViewsTag,noOfViewsFromIni);	 
       
   215 	
       
   216 	TRAPD(err,TContactViewStorePrivate::GetDefinitionsOfExistingViewsL(KNullDesC, existingViewDefs));
       
   217 	if ((err == KErrNone) && (existingViewDefs.Count() == noOfViewsFromIni))
       
   218 	     {
       
   219 	 	 for (TInt n = 0; n < noOfViewsFromIni; n++)
       
   220 	         {
       
   221 	         CContactDefaultViewDefinition* existingDefs = existingViewDefs[n];
       
   222 		     TBuf<KMaxTagLen>viewNameTag;
       
   223 		     TBuf<KMaxTagLen>pluginNameTag;
       
   224 		     TBuf<KMaxTagLen>viewPreferanceTag;
       
   225 		     TBuf<KMaxTagLen>viewTypeTag;
       
   226 		     TBuf<KMaxTagLen>sortOrderTag;
       
   227 		
       
   228 		     TPtrC viewNameFromIni;
       
   229 		     TPtrC pluginNameFromIni;
       
   230 		     TInt  viewPreferanceFromIni;
       
   231 		     TInt  viewTypeFromIni =0;
       
   232 		     TInt  sortOrderFromIni =0;
       
   233 		
       
   234 	         viewNameTag       = KViewNameTag;
       
   235 		     pluginNameTag     = KPluginNameTag;
       
   236 		     viewPreferanceTag = KViewPreferanceTag;
       
   237 		     viewTypeTag       = KViewTypeTag;
       
   238 		
       
   239 		     viewNameTag.AppendNum(n+1);
       
   240 		     pluginNameTag.AppendNum(n+1);
       
   241 		     viewPreferanceTag.AppendNum(n+1);
       
   242 		     viewTypeTag.AppendNum(n+1);
       
   243 		
       
   244 		     GetStringFromConfig(ConfigSection(),viewNameTag,viewNameFromIni);
       
   245 		     GetStringFromConfig(ConfigSection(),pluginNameTag,pluginNameFromIni);
       
   246 		     GetIntFromConfig(ConfigSection(),viewPreferanceTag,viewPreferanceFromIni);
       
   247 		     GetIntFromConfig(ConfigSection(),viewTypeTag,viewTypeFromIni);
       
   248              
       
   249              if ((existingDefs->ViewNameL().Compare(viewNameFromIni)) != KErrNone)
       
   250 		         {
       
   251 		         _LIT(KWrongName, "View Name does not match expected name in INI file");
       
   252 		         ERR_PRINTF1(KWrongName);
       
   253 		         return (EFalse);
       
   254 		         }
       
   255 		         
       
   256 		     //Truncate plugin name
       
   257 		     HBufC8 *buf = NULL;
       
   258 		     TRAPD(err1, buf = HBufC8::NewL(pluginNameFromIni.Size()));
       
   259 		     CleanupStack::PushL(buf);
       
   260 		
       
   261 		     if (err1 != KErrNone)
       
   262 		        {
       
   263 		        CleanupStack::PopAndDestroy(buf);
       
   264 			    return (EFalse);
       
   265 	    	    }
       
   266 	    	
       
   267              TPtr8 tptr8pluginNameFromIni = buf->Des();
       
   268              tptr8pluginNameFromIni.Copy(pluginNameFromIni);
       
   269              if ((existingDefs->SortPluginNameL().Compare(tptr8pluginNameFromIni)) != KErrNone)
       
   270                  {
       
   271              	 _LIT(KNoMatch,"Plugin names dont match");
       
   272              	 ERR_PRINTF1(KNoMatch);
       
   273              	 return (EFalse);
       
   274                  }
       
   275              CleanupStack::PopAndDestroy(buf);
       
   276 		         
       
   277 		     if (existingDefs->ViewPreferences() != viewPreferanceFromIni)
       
   278 		         {
       
   279 		         _LIT(KVprefNoMatch,"ViewPreferances do not match");
       
   280 		         ERR_PRINTF1(KVprefNoMatch);
       
   281 		         return (EFalse);
       
   282 		         }
       
   283 		     
       
   284 		     CContactDefaultViewDefinition::TViewType viewType;
       
   285 	         if (viewTypeFromIni == 0)
       
   286 		         {			
       
   287 			     viewType = CContactDefaultViewDefinition::ERemoteView;
       
   288 		         }
       
   289 		     else if (viewTypeFromIni == 1)
       
   290 		         {
       
   291 			     viewType = CContactDefaultViewDefinition::ENamedRemoteView;
       
   292 		         }
       
   293 		     else
       
   294 		         {
       
   295 		         viewType = (CContactDefaultViewDefinition::TViewType)viewTypeFromIni;
       
   296 		         }
       
   297 		       
       
   298 		     if (existingDefs->ViewType() != viewType)
       
   299 		         {
       
   300 			     _LIT(KViewTypeNoMatch,"View Types do not match");
       
   301 		         ERR_PRINTF1(KViewTypeNoMatch);
       
   302 		         return (EFalse);
       
   303 		         }
       
   304 		         
       
   305 	     	viewSortOrder = existingDefs->SortOrder();
       
   306 		    TUid sortOrderUid = TUid::Null();
       
   307 								    
       
   308 		    sortOrderTag = KFirstSortOrderTag;
       
   309 	        sortOrderTag.AppendNum(n+1);
       
   310 	    	    
       
   311 		    if (GetIntFromConfig(ConfigSection(),sortOrderTag,sortOrderFromIni))						
       
   312 		        {
       
   313 			    sortOrderUid = TUid::Uid(sortOrderFromIni);
       
   314 					
       
   315 		        if (viewSortOrder[0] != sortOrderUid)
       
   316 	                {
       
   317 			        _LIT(KFirstSOWrong,"Incorrect first sort order received = %d");
       
   318 			        ERR_PRINTF2(KFirstSOWrong,viewSortOrder[0]);
       
   319 			        return (EFalse);
       
   320 			        }
       
   321 		        }
       
   322 			
       
   323 		    sortOrderTag = KSecondSortOrderTag;
       
   324 		    sortOrderTag.AppendNum(n+1);
       
   325 	
       
   326 		    if (GetIntFromConfig(ConfigSection(),sortOrderTag,sortOrderFromIni))
       
   327 		        {
       
   328 			    sortOrderUid = TUid::Uid(sortOrderFromIni);
       
   329 			
       
   330 		        if (viewSortOrder[1] != sortOrderUid)
       
   331 	                {
       
   332 			        _LIT(KSecondSOWrong,"Incorrect second sort order received = %d");
       
   333 			        ERR_PRINTF2(KSecondSOWrong,viewSortOrder[1]);
       
   334 			        return (EFalse);
       
   335 			        }
       
   336 		        }
       
   337 				
       
   338 		    sortOrderTag = KThirdSortOrderTag;
       
   339 		    sortOrderTag.AppendNum(n+1);
       
   340 		
       
   341 		    if (GetIntFromConfig(ConfigSection(),sortOrderTag,sortOrderFromIni))
       
   342 		        {
       
   343 			    sortOrderUid = TUid::Uid(sortOrderFromIni);
       
   344 			
       
   345 		        if (viewSortOrder[2] != sortOrderUid)
       
   346 	                {
       
   347 		            _LIT(KThirdSOWrong,"Incorrect third sort order received = %d");
       
   348 			        ERR_PRINTF2(KThirdSOWrong,viewSortOrder[2]);
       
   349 			        return (EFalse);
       
   350 			        }
       
   351 		        }
       
   352 					    
       
   353 	         }
       
   354 	     viewSortOrder.Close();
       
   355 	     }
       
   356 	 else
       
   357 	     {
       
   358 	 	 _LIT(KErrExistingView,"Problem with GetDefinitionsOfExistingViewsL(existingViewDefs)");
       
   359 	 	 ERR_PRINTF1(KErrExistingView);
       
   360 	 	 return (EFalse);
       
   361 	     }
       
   362 	     
       
   363 	 return (ETrue);
       
   364     }
       
   365 
       
   366 
       
   367 /**
       
   368  * @param aNumber of contact items
       
   369  * Implements a simple method to a number of contact items to the
       
   370  * the default database.
       
   371 */    
       
   372 void  CTestContactsDbStep::AddContactsL(TInt aNumber)
       
   373     {
       
   374     OpenDefaultCntsDb();
       
   375     if (iDb == NULL)
       
   376         {
       
   377     	SetTestStepResult(EFail);
       
   378         }
       
   379     else
       
   380         {
       
   381     	CContactItemViewDef* viewAll = CContactItemViewDef::NewL(CContactItemViewDef::EIncludeFields,
       
   382                                                                  CContactItemViewDef::EIncludeHiddenFields);
       
   383                                        
       
   384         viewAll->AddL(KUidContactFieldMatchAll);
       
   385         CleanupStack::PushL(viewAll);
       
   386                                          
       
   387         CContactTemplate* cntTemplate = STATIC_CAST(CContactTemplate*,iDb->ReadContactL(iDb->TemplateId(),*viewAll));
       
   388         CleanupStack::PushL(cntTemplate);
       
   389        
       
   390 	    CContactCard* contact = CContactCard::NewLC(cntTemplate);
       
   391 	    for(TInt i = 0; i < aNumber; i++)
       
   392 	        {		
       
   393 	        iDb->AddNewContactL(*contact);
       
   394 	        }
       
   395 	    	
       
   396 	    TContactIter hh(*iDb);
       
   397 	    _LIT(KText,"someDummyTextInFields");	
       
   398 	    CContactItem *contactItem = iDb->OpenContactL( hh.NextL(), *viewAll );
       
   399 	    CleanupStack::PushL(contactItem);
       
   400 	    SetManyFieldsL(contactItem->CardFields(),KText,20);
       
   401 	    iDb->CommitContactL(*contactItem);
       
   402 	    
       
   403 	    CleanupStack::PopAndDestroy(contactItem);	    
       
   404 	    CleanupStack::PopAndDestroy(contact);    
       
   405         CleanupStack::PopAndDestroy(cntTemplate);
       
   406         CleanupStack::PopAndDestroy(viewAll);
       
   407         }
       
   408     
       
   409     }
       
   410     
       
   411 /*sets a specific contact field to a value as determined by its type.
       
   412 field at aPos in aFields is set to aText*/
       
   413 void CTestContactsDbStep::SetFieldL(CContactItemFieldSet &aFields, const TInt aPos, const TDesC& aText)
       
   414 	{
       
   415 	CContactItemField &field = aFields[aPos];
       
   416 	if( field.IsTemplateLabelField() )
       
   417 		{
       
   418 		return;
       
   419 		}
       
   420 		
       
   421 	if( 0 == aText.Size() )
       
   422 		{
       
   423 		return;
       
   424 		}
       
   425 	
       
   426 	switch(field.StorageType())
       
   427 		{
       
   428 		case KStorageTypeText:
       
   429 			field.TextStorage()->SetTextL(aText);
       
   430 		break;
       
   431 		case KStorageTypeStore:
       
   432 			{
       
   433 			HBufC8 *buf = HBufC8::NewLC(aText.Size());
       
   434 			TPtr8 tptr8 = buf->Des();
       
   435 			tptr8.Copy(aText);
       
   436 			field.StoreStorage()->SetThingL(*buf);
       
   437 			CleanupStack::PopAndDestroy(buf);
       
   438 			}
       
   439 		break;
       
   440 		case KStorageTypeContactItemId:
       
   441 			{
       
   442 			field.AgentStorage()->SetAgentId(0);
       
   443 			}
       
   444 		break;
       
   445 		case KStorageTypeDateTime:
       
   446 			{			
       
   447 			field.DateTimeStorage()->SetTime( TTime(0) );
       
   448 			}
       
   449 		break;
       
   450 		default:
       
   451 			User::Panic(aText,EInvariantFalse);
       
   452 		break;
       
   453 		}
       
   454 	}
       
   455 
       
   456 void CTestContactsDbStep::SetManyFieldsL(CContactItemFieldSet &aFields, const TDesC& aText, const TInt aMany)
       
   457 	{
       
   458 	TInt count = aFields.Count();
       
   459 	if( count > aMany )
       
   460 		{
       
   461 		count = aMany;
       
   462 		}
       
   463 		
       
   464 	TInt i = 0;
       
   465 	for(; i < count; ++i)
       
   466         {
       
   467 		SetFieldL(aFields, i,aText);
       
   468 		}
       
   469 	}