phonebookengines/contactsmodel/tsrc/Integration/TestContactSuite/src/TestAddMultipleContactsStep.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 "TestAddMultipleContactsStep.h"
       
    17 #include "TestContactSuiteServer.h"
       
    18 #include <cntitem.h>
       
    19 #include <cntfield.h>
       
    20 #include <cntfldst.h>
       
    21 #include <cntdef.h>
       
    22 #include <cntdb.h>
       
    23 #include <commdb.h>
       
    24 
       
    25 
       
    26 TInt CTestAddMultipleContactsStep::iStorage[]=
       
    27 {
       
    28 KStorageTypeText, KStorageTypeText, KStorageTypeDateTime, KStorageTypeText,KStorageTypeText,KStorageTypeText,KStorageTypeText
       
    29 };
       
    30 
       
    31 TUid CTestAddMultipleContactsStep::iFieldUid[]=
       
    32 {
       
    33 KUidContactFieldAssistant, KUidContactFieldPhoneNumber, KUidContactFieldAnniversary,KUidContactFieldSpouse,KUidContactFieldChildren,KUidContactFieldClass,KUidContactFieldDepartmentName
       
    34 };
       
    35 
       
    36 TUid CTestAddMultipleContactsStep::iVcardUid[]=
       
    37 {
       
    38 KUidContactFieldVCardMapAssistant, KUidContactFieldVCardMapAssistantTel, KUidContactFieldVCardMapAnniversary, KUidContactFieldVCardMapSpouse,KUidContactFieldVCardMapChildren,KUidContactFieldVCardMapClass,KUidContactFieldVCardMapDepartment
       
    39 };
       
    40 
       
    41 CContactDatabase *database = NULL;
       
    42 TContactItemId id = 0;
       
    43 
       
    44 CTestAddMultipleContactsStep::~CTestAddMultipleContactsStep()
       
    45 /**
       
    46  * Destructor
       
    47  */
       
    48 	{
       
    49 
       
    50 	}
       
    51 
       
    52 CTestAddMultipleContactsStep::CTestAddMultipleContactsStep()
       
    53 /**
       
    54  * Constructor
       
    55  */
       
    56 	{
       
    57 	// **MUST** call SetTestStepName in the constructor as the controlling
       
    58 	// framework uses the test step name immediately following construction to set
       
    59 	// up the step's unique logging ID.
       
    60 	SetTestStepName(KTestAddMultipleContactsStep);
       
    61 	}
       
    62 
       
    63 TVerdict CTestAddMultipleContactsStep::doTestStepPreambleL()
       
    64 /**
       
    65  * @return - TVerdict code
       
    66  * Override of base class virtual
       
    67  */
       
    68 	{
       
    69 	iScheduler = new (ELeave) CActiveScheduler;
       
    70 	CActiveScheduler::Install(iScheduler);
       
    71 	SetTestStepResult(EPass);
       
    72 	return TestStepResult();
       
    73 	}
       
    74 
       
    75 TVerdict CTestAddMultipleContactsStep::doTestStepL()
       
    76 /**
       
    77  * @return - TVerdict code
       
    78  * Override of base class pure virtual
       
    79  */
       
    80 	{
       
    81 	TPtrC fieldName,fieldVal, fieldMultiple;
       
    82 	_LIT(KMultiple,	"multiple");
       
    83  	if (!GetStringFromConfig(ConfigSection(), KIniFieldName, fieldName))
       
    84 		{
       
    85 		ERR_PRINTF1(_L("Unable to read fieldname from ini file"));
       
    86 		SetTestStepResult(EFail);
       
    87 		}
       
    88 	else
       
    89 		{
       
    90 		if(KErrNone == fieldName.Compare(KAssistance))
       
    91 			{
       
    92 			// Get the values from ini file
       
    93 			GetStringFromConfig(ConfigSection(), KIniFieldVal, fieldVal);
       
    94 			GetStringFromConfig(ConfigSection(), KIniFlagvalue, fieldMultiple);
       
    95 			if(fieldMultiple.Compare(KMultiple)==0)
       
    96 				{
       
    97 				AddMultipleContactsL(fieldVal, iStorage[0],iFieldUid[0], iVcardUid[0]);
       
    98 				}
       
    99 			else
       
   100 				{		
       
   101 				OpenExistingContactL(fieldVal, iStorage[0],iFieldUid[0], iVcardUid[0]);
       
   102 				}
       
   103 			}
       
   104 		else if(KErrNone == fieldName.Compare(KAssistanceTel))
       
   105 			{
       
   106 			// Get the values from ini file
       
   107 			GetStringFromConfig(ConfigSection(), KIniFieldVal, fieldVal);
       
   108 			GetStringFromConfig(ConfigSection(), KIniFlagvalue, fieldMultiple);
       
   109 			if(fieldMultiple.Compare(KMultiple)==0)
       
   110 				{
       
   111 				AddMultipleContactsL(fieldVal, iStorage[1],iFieldUid[1], iVcardUid[1]);
       
   112 				}
       
   113 			else
       
   114 				{
       
   115 				OpenExistingContactL(fieldVal, iStorage[1],iFieldUid[1], iVcardUid[1]);
       
   116 				}
       
   117 			}
       
   118 		else if(KErrNone == fieldName.Compare(KAnniversary))
       
   119 			{
       
   120 			// Get the values from ini file
       
   121 			GetStringFromConfig(ConfigSection(), KIniFieldVal, fieldVal);
       
   122 			GetStringFromConfig(ConfigSection(), KIniFlagvalue, fieldMultiple);
       
   123 			if(fieldMultiple.Compare(KMultiple)==0)
       
   124 				{
       
   125 				AddMultipleContactsL(fieldVal, iStorage[2],iFieldUid[2], iVcardUid[2]);
       
   126 				}
       
   127 			else
       
   128 				{
       
   129 				OpenExistingContactL(fieldVal, iStorage[2],iFieldUid[2], iVcardUid[2]);
       
   130 				}
       
   131 			}
       
   132 		else if(KErrNone == fieldName.Compare(KSpouse))
       
   133 			{
       
   134 			// Get the values from ini file
       
   135 			GetStringFromConfig(ConfigSection(), KIniFieldVal, fieldVal);
       
   136 			GetStringFromConfig(ConfigSection(), KIniFlagvalue, fieldMultiple);
       
   137 			if(fieldMultiple.Compare(KMultiple)==0)
       
   138 				{
       
   139 				AddMultipleContactsL(fieldVal, iStorage[3],iFieldUid[3], iVcardUid[3]);
       
   140 				}
       
   141 			else
       
   142 				{
       
   143 				OpenExistingContactL(fieldVal, iStorage[3],iFieldUid[3], iVcardUid[3]);
       
   144 				}
       
   145 			}
       
   146 		else if(KErrNone == fieldName.Compare(KChildren))
       
   147 			{
       
   148 			// Get the values from ini file
       
   149 			GetStringFromConfig(ConfigSection(), KIniFieldVal, fieldVal);
       
   150 			GetStringFromConfig(ConfigSection(), KIniFlagvalue, fieldMultiple);
       
   151 			if(fieldMultiple.Compare(KMultiple)==0)
       
   152 				{
       
   153 				AddMultipleContactsL(fieldVal, iStorage[4],iFieldUid[4], iVcardUid[4]);
       
   154 				}
       
   155 			else
       
   156 				{
       
   157 				OpenExistingContactL(fieldVal, iStorage[4],iFieldUid[4], iVcardUid[4]);
       
   158 				}
       
   159 			}
       
   160 		else if(KErrNone == fieldName.Compare(KClass))
       
   161 			{
       
   162 			// Get the values from ini file
       
   163 			GetStringFromConfig(ConfigSection(), KIniFieldVal, fieldVal);
       
   164 			GetStringFromConfig(ConfigSection(), KIniFlagvalue, fieldMultiple);
       
   165 			if(fieldMultiple.Compare(KMultiple)==0)
       
   166 				{
       
   167 				AddMultipleContactsL(fieldVal, iStorage[5],iFieldUid[5], iVcardUid[5]);
       
   168 				}
       
   169 			else
       
   170 				{
       
   171 				OpenExistingContactL(fieldVal, iStorage[5],iFieldUid[5], iVcardUid[5]);
       
   172 				}
       
   173 			}
       
   174 		else if(KErrNone == fieldName.Compare(KDepartment))
       
   175 			{
       
   176 			// Get the values from ini file
       
   177 			GetStringFromConfig(ConfigSection(), KIniFieldVal, fieldVal);
       
   178 			GetStringFromConfig(ConfigSection(), KIniFlagvalue, fieldMultiple);
       
   179 			if(fieldMultiple.Compare(KMultiple)==0)
       
   180 				{
       
   181 				AddMultipleContactsL(fieldVal, iStorage[6],iFieldUid[6], iVcardUid[6]);
       
   182 				}
       
   183 			else
       
   184 				{
       
   185 				OpenExistingContactL(fieldVal, iStorage[6],iFieldUid[6], iVcardUid[6]);
       
   186 				}
       
   187 			}
       
   188 		else
       
   189 			{
       
   190 			ERR_PRINTF1(_L("Invalid fieldname"));
       
   191 			SetTestStepResult(EFail);
       
   192 			}
       
   193 		}
       
   194 		return TestStepResult();
       
   195 
       
   196 	}
       
   197 /** Create Contact Item with field and add contact Item
       
   198  *	Contact database.Reopen the contact item and add field to it. 
       
   199  *	aValue - field value
       
   200  *  @param aStorageType - Storage type of each field
       
   201  *	@param aContFieldUid - Uid of Contact field
       
   202  *	@param avCardMapUid - Uid of vCard 
       
   203  *	@return - void
       
   204 */		
       
   205 
       
   206 void CTestAddMultipleContactsStep::OpenExistingContactL(TPtrC aValue, TInt aStorageType,TUid aContFieldUid, TUid avCardMap)
       
   207 	{
       
   208 	TPtrC value,fielddel;
       
   209    _LIT(KTestDbName, "c:contactexist.cdb");
       
   210    _LIT(KExistDelete, "exist");
       
   211    	TInt year, month, day;	
       
   212    	
       
   213     // Create contact item and add field to it
       
   214 	CContactItem* item = CContactCard::NewLC();
       
   215     CContactItemField* field = CContactItemField::NewL(aStorageType, aContFieldUid);
       
   216     CleanupStack::PushL(field); 
       
   217     field->SetMapping(avCardMap);
       
   218     if(aStorageType==KStorageTypeDateTime)
       
   219     	{
       
   220     	GetIntFromConfig(ConfigSection(), KIniYear, year);
       
   221     	GetIntFromConfig(ConfigSection(), KIniMonth, month);
       
   222     	GetIntFromConfig(ConfigSection(), KIniDay, day);
       
   223 		TDateTime date(year,(TMonth)month,day,0,0,0,0);
       
   224    		field->DateTimeStorage()->SetTime(date);
       
   225     	}
       
   226     else
       
   227     	{
       
   228     	field->TextStorage()->SetTextL(aValue);	
       
   229     	}	
       
   230    	item->AddFieldL(*field);  
       
   231    	CleanupStack::Pop(field);
       
   232    
       
   233    	//replace existing database name
       
   234    	database = CContactDatabase::ReplaceL(KTestDbName);  
       
   235    	
       
   236    	// add contact item to database  
       
   237   	id = database->AddNewContactL(*item);
       
   238   	CleanupStack::PopAndDestroy(item);
       
   239   
       
   240   	//reopen the contact
       
   241   	CContactItem *openItem = database->OpenContactL(id);
       
   242   	CleanupStack::PushL(openItem);
       
   243   	
       
   244   	//create contactitem field and add to it.
       
   245   	CContactItemField* newField = CContactItemField::NewL(aStorageType, aContFieldUid);
       
   246     CleanupStack::PushL(newField); 
       
   247     newField->SetMapping(avCardMap);
       
   248     if(aStorageType==KStorageTypeDateTime)
       
   249     	{
       
   250     	GetIntFromConfig(ConfigSection(), KIniYear, year);
       
   251     	GetIntFromConfig(ConfigSection(), KIniMonth, month);
       
   252     	GetIntFromConfig(ConfigSection(), KIniDay, day);
       
   253 		TDateTime date(year,(TMonth)month,day,0,0,0,0);
       
   254    		newField->DateTimeStorage()->SetTime(date);
       
   255     	}
       
   256     else
       
   257     	{
       
   258     	newField->TextStorage()->SetTextL(aValue);	
       
   259     	}	
       
   260    	openItem->AddFieldL(*newField);  
       
   261    	CleanupStack::Pop(newField);
       
   262   
       
   263    	// verify the field is being added   
       
   264    	CContactItemFieldSet& ContactFieldSetOpen = openItem->CardFields();
       
   265    	TInt startPos=0;
       
   266    	for(TInt check=0; check<2; check++)
       
   267    		{
       
   268 		TInt pos = ContactFieldSetOpen.FindNext(aContFieldUid, avCardMap, startPos);
       
   269 		TInt noOfFields = ContactFieldSetOpen.Count();	
       
   270 		if(pos != KErrNotFound)	
       
   271 			{
       
   272 			if(aStorageType==KStorageTypeDateTime)
       
   273 				{
       
   274 				TDateTime result=ContactFieldSetOpen[pos].DateTimeStorage()->Time().DateTime();
       
   275 				startPos=startPos+1;
       
   276 				if((result.Year() == year) && (result.Month() == (TMonth)month) && (result.Day() == day)) 
       
   277 					{
       
   278 					SetTestStepResult(EPass);	
       
   279 					}
       
   280 				else
       
   281 					{
       
   282 					SetTestStepResult(EFail);	
       
   283 					}
       
   284 				}
       
   285 			else
       
   286 				{	
       
   287 				value.Set(ContactFieldSetOpen[pos].TextStorage()->Text());
       
   288 				startPos=startPos+1;
       
   289 				//compare the results
       
   290 				if(aValue.Compare(value)==0 )
       
   291 					{
       
   292 					SetTestStepResult(EPass);
       
   293 					}
       
   294 				else
       
   295 					{
       
   296 					SetTestStepResult(EFail);
       
   297 					}
       
   298 				}
       
   299 			}
       
   300    		}
       
   301 	GetStringFromConfig(ConfigSection(), KIniFlagDelete, fielddel);
       
   302   	if(KErrNone == fielddel.Compare(KExistDelete))
       
   303 		{
       
   304 		TRAPD(ret, DeleteExistContactL(aContFieldUid, avCardMap));
       
   305 		if(ret!=KErrNone)
       
   306 			{
       
   307 			database->CloseContactL(id);
       
   308   			delete database;
       
   309   			return;
       
   310 			}
       
   311 		}
       
   312 	database->CloseContactL(id);
       
   313 	CleanupStack::PopAndDestroy(openItem);
       
   314   	delete database;		
       
   315 	}
       
   316 /** Delete existing contact item field.
       
   317  *	Contact database.Reopen the contact item and add field to it. 
       
   318  *	@param aContFieldUid - Uid of Contact field
       
   319  *	@param avCardMapUid - Uid of vCard 
       
   320  *	@return - void
       
   321 */	
       
   322 void CTestAddMultipleContactsStep::DeleteExistContactL(TUid aContFieldUid, TUid avCardMap)	
       
   323 	{
       
   324 	CContactItem *readItem = NULL;
       
   325     readItem = database->ReadContactLC(id);
       
   326     CContactItemFieldSet& ContactFieldSet = readItem->CardFields();
       
   327 	TInt pos = ContactFieldSet.Find(aContFieldUid, avCardMap);
       
   328 	if(pos != KErrNotFound)
       
   329 		{
       
   330 		ContactFieldSet.Remove(pos);
       
   331 		SetTestStepResult(EPass);
       
   332 		INFO_PRINTF1(_L("field deleted"));
       
   333 		}
       
   334 	else
       
   335 		{
       
   336 		INFO_PRINTF1(_L("field not deleted"));
       
   337 		SetTestStepResult(EFail);
       
   338 		}
       
   339 	CleanupStack::PopAndDestroy(readItem);	
       
   340 	}	
       
   341 /** Create Multiple Contact Item with field and Add all contact
       
   342  *	Item to Contact database. verify field is 
       
   343  *	being added 
       
   344  *	aValue - field value
       
   345  *  @param aStorageType - Storage type of each field
       
   346  *	@param aContFieldUid - Uid of Contact field
       
   347  *	@param avCardMapUid - Uid of vCard 
       
   348  *	@return - void
       
   349 */		
       
   350 void CTestAddMultipleContactsStep::AddMultipleContactsL(TPtrC aValue, TInt aStorageType,TUid aContFieldUid, TUid avCardMap)	
       
   351 	{
       
   352 	TPtrC value;
       
   353    _LIT(KTestDbName, "c:contactmultiple.cdb");
       
   354    	TInt year, month, day,noOfCI;	
       
   355    	RArray <TInt> idArray;
       
   356 	CleanupClosePushL(idArray); 
       
   357 	
       
   358 	//replace existing database name
       
   359 	database = CContactDatabase::ReplaceL(KTestDbName); 
       
   360 	 
       
   361 	//Get the number of contact item from ini file
       
   362 	GetIntFromConfig(ConfigSection(), KNoOfContactItem, noOfCI);
       
   363 	
       
   364 	//Get the int value
       
   365 	GetIntFromConfig(ConfigSection(), KIniYear, year);
       
   366 	GetIntFromConfig(ConfigSection(), KIniMonth, month);
       
   367 	GetIntFromConfig(ConfigSection(), KIniDay, day);
       
   368 	
       
   369     // Create contact item and add field to it
       
   370     for(TInt range=0;range<noOfCI;range++)
       
   371     	{
       
   372 		CContactItem* item = CContactCard::NewLC();
       
   373 		CContactItemField* field = CContactItemField::NewL(aStorageType, aContFieldUid);
       
   374 		CleanupStack::PushL(field); 
       
   375 		field->SetMapping(avCardMap);
       
   376 		if(aStorageType==KStorageTypeDateTime)
       
   377 			{
       
   378 			TDateTime date(year,(TMonth)month,day,0,0,0,0);
       
   379 			field->DateTimeStorage()->SetTime(date);
       
   380 			}
       
   381 			else
       
   382 			{
       
   383 			field->TextStorage()->SetTextL(aValue);	
       
   384 			}	
       
   385 			item->AddFieldL(*field);  
       
   386 			CleanupStack::Pop(field);
       
   387 			
       
   388 		// add contact item to database  
       
   389 		id = database->AddNewContactL(*item);
       
   390 		
       
   391 		//append the id in array
       
   392 		idArray.Append(id);
       
   393 		
       
   394 		//Cleanup
       
   395 		CleanupStack::PopAndDestroy(item);	
       
   396     	}
       
   397     database->CloseContactL(id);
       
   398     
       
   399     // verify for multiple contact item with field is being created
       
   400     CContactItem *readItem = NULL;
       
   401     for(TInt readContact=0;readContact<noOfCI;readContact++)
       
   402     	{
       
   403     	readItem = database->ReadContactL(idArray[readContact]);
       
   404     	CleanupStack::PushL(readItem);
       
   405     	CContactItemFieldSet& ContactFieldSet = readItem->CardFields();
       
   406 		TInt pos = ContactFieldSet.Find(aContFieldUid, avCardMap);	
       
   407 		if(pos != KErrNotFound)	
       
   408 			{
       
   409 			if(aStorageType==KStorageTypeDateTime)
       
   410 				{
       
   411 				TDateTime result=ContactFieldSet[pos].DateTimeStorage()->Time().DateTime();
       
   412 				if((result.Year() == year) && (result.Month() == (TMonth)month) && (result.Day() == day)) 
       
   413 					{
       
   414 					SetTestStepResult(EPass);	
       
   415 					}
       
   416 				else
       
   417 					{
       
   418 					SetTestStepResult(EFail);	
       
   419 					}
       
   420 				}
       
   421 			else
       
   422 				{	
       
   423 				value.Set(ContactFieldSet[pos].TextStorage()->Text());
       
   424 				//compare the results
       
   425 				if(aValue.Compare(value)==0 )
       
   426 					{
       
   427 					SetTestStepResult(EPass);
       
   428 					}
       
   429 				else
       
   430 					{
       
   431 					SetTestStepResult(EFail);
       
   432 					}
       
   433 				}
       
   434 			}
       
   435 		CleanupStack::PopAndDestroy(readItem);		
       
   436     	}
       
   437     // Cleanup
       
   438     CleanupStack::PopAndDestroy(&idArray);
       
   439   	delete database;	
       
   440 	}
       
   441 
       
   442 TVerdict CTestAddMultipleContactsStep::doTestStepPostambleL()
       
   443 /**
       
   444  * @return - TVerdict code
       
   445  * Override of base class virtual
       
   446  */
       
   447 	{
       
   448 	CActiveScheduler::Install(NULL);
       
   449 	delete iScheduler;
       
   450 	return TestStepResult();
       
   451 	}
       
   452 
       
   453