phonebookengines_old/contactsmodel/tsrc/t_agent.cpp
changeset 40 b46a585f6909
equal deleted inserted replaced
37:fd64c38c277d 40:b46a585f6909
       
     1 // Copyright (c) 2006-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 <e32test.h>
       
    17 #include "T_rndutils.h"
       
    18 #include "t_agent.h"
       
    19 #include <cntitem.h>
       
    20 #include <cntfldst.h>
       
    21 #include <cntdef.h>
       
    22 _LIT(KAgentExported, "C:\\Agentcontact.vcf");
       
    23 
       
    24 _LIT(KTestName,"T_AGENT");
       
    25 
       
    26 
       
    27 _LIT(KDatabaseFileName,"C:T_AGENT");
       
    28 _LIT(KMyfile,"Z:\\ImportContacts.vcf"); 
       
    29 _LIT(KAgentTemp,"C:\\ExportedContact.vcf");
       
    30 _LIT(KAgentexpected,"Z:\\Expectedcontact.vcf");
       
    31 
       
    32 
       
    33 CCntTest* CntTest=NULL;
       
    34 LOCAL_D RTest test(KTestName);
       
    35 
       
    36 CAgentTest* CAgentTest::NewL(CCntTest& aTestLibrary)
       
    37 	{
       
    38 	CAgentTest* self = new(ELeave) CAgentTest(aTestLibrary);
       
    39 	CleanupStack::PushL(self);
       
    40 	self->ConstructL();
       
    41 	CleanupStack::Pop(self);
       
    42 	return self;
       
    43 	}
       
    44 
       
    45 CAgentTest::~CAgentTest()
       
    46 	{
       
    47 	iTest.CloseDatabase();
       
    48 	delete iIdArray;
       
    49 	}
       
    50 
       
    51 
       
    52 void CAgentTest::RunTestsL()
       
    53 	{
       
    54     AgentExportL();
       
    55 	
       
    56 	test.Next(_L("Check if Agent cards are properly imported,single and multiple agents"));
       
    57 
       
    58 	ImportAgentToEmptyDatabaseL();
       
    59 	
       
    60 	test.Next(_L("Check if Agent cards are merged with main contacts in database."));
       
    61 
       
    62 	ImportAgentForMergeL();
       
    63 	
       
    64 	test.Next(_L("MainItems should be updated by incoming vCard having Agents."));
       
    65 
       
    66 	ImportAgentMiscL();
       
    67 
       
    68 	test.Next(_L("MainItems should be exported even if Agents are deleted."));
       
    69 
       
    70 	ExportWithDeletedAgentL();
       
    71 	
       
    72 	test.Next(_L("Multiple  agents should be exported."));
       
    73 
       
    74 	MultipleAgentExportL();
       
    75 
       
    76  	}
       
    77 
       
    78 CAgentTest::CAgentTest(CCntTest& aTestLibrary) : iTest(aTestLibrary)
       
    79 	{
       
    80 	}
       
    81 
       
    82 void CAgentTest::ConstructL()
       
    83 	{
       
    84 	iDb=iTest.CreateDatabaseL();
       
    85 	iIdArray = CContactIdArray::NewL();
       
    86 	}
       
    87 
       
    88 /** Import contacts from the vCard file specified by aFileName */
       
    89 void CAgentTest::ImportContactsL(const TDesC& aFileName)
       
    90 	{
       
    91 	RFileReadStream vcard;
       
    92 	User::LeaveIfError(vcard.Open(iTest.Fs(), aFileName, EFileRead));
       
    93 	CleanupClosePushL(vcard);
       
    94 
       
    95 	TBool success=EFalse;
       
    96 	CArrayPtr<CContactItem>* contactItems=iDb->ImportContactsL(TUid::Uid(KUidVCardConvDefaultImpl), vcard, success, CContactDatabase::ETTFormat);
       
    97 	CleanupStack::PopAndDestroy(&vcard);
       
    98 	       
       
    99 	CleanupStack::PushL(TCleanupItem(CleanUpResetAndDestroy,contactItems));
       
   100 	iIdArray->Reset();
       
   101 	TContactItemId contactId;
       
   102  	for(TInt loop = 0;loop < contactItems->Count();++loop)
       
   103 		{
       
   104 		contactId = (*contactItems)[loop]->Id();
       
   105 		iIdArray->AddL(contactId);		
       
   106 		}
       
   107 	CleanupStack::PopAndDestroy(contactItems);	
       
   108 	}
       
   109 
       
   110 /** Export contact items specified by aIds to aFileName */
       
   111 void CAgentTest::ExportContactsL(const TDesC& aFileName)
       
   112 	{
       
   113 	RFile outfile;
       
   114 	outfile.Replace(iTest.Fs(),aFileName,EFileWrite);
       
   115 	CleanupClosePushL(outfile);
       
   116 	RFileWriteStream writeStream(outfile);
       
   117 	CleanupClosePushL(writeStream);
       
   118 	
       
   119 	TUid uid;
       
   120 	uid.iUid=KUidVCardConvDefaultImpl;
       
   121 	iDb->ExportSelectedContactsL(uid,*iIdArray,writeStream,CContactDatabase::EDefault);
       
   122 
       
   123 	writeStream.CommitL();
       
   124 	CleanupStack::PopAndDestroy(2); //writeStream.Close(), outfile.Close()
       
   125 	}
       
   126 
       
   127 /** Imports contacts from a vCard present in descriptor*/
       
   128 void CAgentTest::ImportContactsFromDescriptorL(const TDesC8& aVCard)
       
   129 	{
       
   130 	RDesReadStream vcard(aVCard);
       
   131 	CleanupClosePushL(vcard);
       
   132 	CArrayPtr<CContactItem>* contactItems;
       
   133 	TBool success = EFalse;
       
   134 	contactItems = iDb->ImportContactsL(TUid::Uid(KUidVCardConvDefaultImpl), vcard, success, CContactDatabase::ETTFormat);
       
   135 	CleanupStack::PushL(TCleanupItem(CleanUpResetAndDestroy, contactItems));
       
   136 	iIdArray->Reset();
       
   137 	TContactItemId contactId;
       
   138 	for(TInt loop = 0;loop < contactItems->Count();++loop)
       
   139 		{
       
   140 		contactId = (*contactItems)[loop]->Id();
       
   141 		iIdArray->AddL(contactId);		
       
   142 		}
       
   143 	CleanupStack::PopAndDestroy(contactItems);
       
   144 	CleanupStack::PopAndDestroy(&vcard);
       
   145 	}
       
   146 
       
   147 	
       
   148 void CAgentTest::CompareFilesL(const TDesC& aOutputFile,const TDesC& aCompareFile)
       
   149 	{
       
   150 	RFs fsSession;
       
   151 	User::LeaveIfError(fsSession.Connect());
       
   152 	RFile file1,file2;
       
   153 	TInt err=file1.Open(fsSession, aOutputFile, EFileRead);
       
   154 	if (err!=KErrNone)
       
   155 		{
       
   156 		User::Leave(err);	
       
   157 		}
       
   158 	
       
   159 	CleanupClosePushL(file1);
       
   160 	err=file2.Open(fsSession, aCompareFile, EFileRead);
       
   161 	
       
   162 	if (err!=KErrNone)
       
   163 		{
       
   164 		User::Leave(err);	
       
   165 		}
       
   166 
       
   167 	
       
   168 	CleanupClosePushL(file2);
       
   169 	RFileReadStream stream1(file1);
       
   170 	RFileReadStream stream2(file2);
       
   171 	CleanupClosePushL(stream1);
       
   172 	CleanupClosePushL(stream2);
       
   173 	TBuf8<0x80> bufO,bufC;
       
   174 	TInt line=1;
       
   175 	do {
       
   176 		TRAP(err,stream1.ReadL(bufO,TChar(0xa)));
       
   177 		if (err==KErrNone || err==KErrEof)
       
   178 		{
       
   179 		TRAP(err,stream2.ReadL(bufC,TChar(0xa)));	
       
   180 		}
       
   181 		if (err!=KErrNone && err!=KErrEof)
       
   182 		{
       
   183 		User::Leave(err);	
       
   184 		}
       
   185 		
       
   186 		CompareLines(bufO, bufC);
       
   187 		++line;
       
   188 		} while (err!=KErrEof);
       
   189 	CleanupStack::PopAndDestroy(4,&file1);
       
   190 	}
       
   191 	
       
   192 TBool CAgentTest::CompareLines(const TDesC8& aLine1, const TDesC8& aLine2)
       
   193 	{
       
   194 	_LIT8(Krev,"REV");
       
   195 	
       
   196 	TBool ret=!aLine1.Compare(aLine2);
       
   197 	if (!ret)
       
   198 		{
       
   199 		TBufC8<2> firstchar(aLine1.Left(2));
       
   200 		if(!firstchar.Compare(Krev()))
       
   201 			{
       
   202 			return ETrue;
       
   203 			}
       
   204 		}
       
   205 	return ret;
       
   206 	}	
       
   207 	
       
   208 void CleanUpResetAndDestroy(TAny *aArray)
       
   209 	{
       
   210 	if (aArray)
       
   211 		{
       
   212 		CArrayPtr<CContactItem>* array=(CArrayPtr<CContactItem>*)aArray;
       
   213 		array->ResetAndDestroy();
       
   214 		delete array;
       
   215 		}
       
   216 	}
       
   217 
       
   218 
       
   219 void CAgentTest::AgentExportL()	
       
   220 	{  
       
   221      test.Next(_L("First it Imports the contacts from the vcard"));
       
   222 
       
   223     
       
   224     ImportContactsL(KMyfile);
       
   225     
       
   226     test.Next(_L("It Exports the main contact which conatains the agent contact"));
       
   227 
       
   228     
       
   229     ExportContactsL(KAgentTemp);
       
   230     
       
   231     test.Next(_L("It compares the exported conatcts with expected vcf file"));
       
   232 
       
   233  	 		   
       
   234     CompareFilesL(KAgentTemp,KAgentexpected);
       
   235  
       
   236 	}
       
   237 
       
   238 /**
       
   239 @SYMTestCaseID PIM-AGENT-0001
       
   240 @SYMDEF DEF098709
       
   241 @SYMTestType UT
       
   242 @SYMTestPriority High
       
   243 @SYMTestCaseDesc Check if Agent cards are properly imported.Single and Multiple.
       
   244 
       
   245 @SYMTestActions
       
   246 1.Import vCards having Main Contacts and Agent cards
       
   247 2.Import vCards having main contact having multiple agent cards.
       
   248 
       
   249 @SYMTestExpectedResults For the above tests:
       
   250 1.AgentCard should merge with the main contact item in database.
       
   251 2.All Agents should be properly imported, creating new agents and adding them to Main contact items.
       
   252 3.No leaves or panics should occur
       
   253 */	
       
   254 void CAgentTest::ImportAgentToEmptyDatabaseL()
       
   255 	{
       
   256 	_LIT(KAgentFirstName,"Example1");	
       
   257 	iDb = iTest.CreateDatabaseL();
       
   258 	//vCard has contact A, contact B. contact A included in contact B as Agent.
       
   259 	//Agent Contact in B has different Name property. so after merge, contact A should
       
   260 	//have the name present in Agent A. (look at the vCard in header file).
       
   261 	ImportContactsFromDescriptorL(KAgentvCard());
       
   262 	//Only 2 contacts should have been imported, as Agent will be merged.
       
   263 	test(iIdArray->Count() == 2);
       
   264 	
       
   265 	//check if name has changed for contact A
       
   266 	CContactItem* mainItem = iDb->ReadContactLC((*iIdArray)[0]);
       
   267 	test(FieldText(*mainItem, KUidContactFieldFamilyName) == KAgentFirstName);
       
   268 	CleanupStack::PopAndDestroy();
       
   269 
       
   270 	//Import vCard has 2 Main Contacts and 3 Agents. No contact is occurring twice,
       
   271 	//so all should be added as new contacts, 5 in total.
       
   272 	iDb = iTest.CreateDatabaseL();
       
   273 	ImportContactsFromDescriptorL(KMultipleAgentvCard());
       
   274 	test(iIdArray->Count() == 5);
       
   275 
       
   276 	//check that 1st Main contact has 1 agent.
       
   277 	//extracting 2nd one in array as agents would be added prior to the main contact.
       
   278 	test(GetAgentCountL((*iIdArray)[1]) == 1);
       
   279 
       
   280 	//2nd Main contact should have 2 agents.
       
   281 	//extracting 5th one in array as agents would be added prior to the main contact.
       
   282 	test(GetAgentCountL((*iIdArray)[4]) == 2);
       
   283 	}
       
   284 
       
   285 /**
       
   286 @SYMTestCaseID PIM-AGENT-0002 
       
   287 @SYMDEF DEF098709
       
   288 @SYMTestType UT
       
   289 @SYMTestPriority High
       
   290 @SYMTestCaseDesc Check if Agent cards are merged with main contacts in database.
       
   291 
       
   292 @SYMTestActions
       
   293 1.Create contact items and add contact agents to them.
       
   294 2.Export Main contact items.
       
   295 3.Change properties of Agent contact items in database.
       
   296 4.Import the exported vCard.
       
   297 
       
   298 @SYMTestExpectedResults For the above tests:
       
   299 1.vCard being imported should properly update the existing contact/Agent items
       
   300 2.Updated contacts should be restored with old values.
       
   301 3.No leaves or panics should occur
       
   302 */	
       
   303 void CAgentTest::ImportAgentForMergeL()
       
   304 	{
       
   305 	iDb = iTest.CreateDatabaseL();
       
   306 	//Adding 4 contacts (A,B,C,D)to database
       
   307 	AddContactItemsL(4);
       
   308 	//Make A an agent of B 
       
   309 	MakeAgentL((*iIdArray)[1], (*iIdArray)[0]);
       
   310 
       
   311 	//make C an Agent of D
       
   312 	MakeAgentL((*iIdArray)[3], (*iIdArray)[2]);
       
   313 	
       
   314 	//storing Id's of agents A and C and removing them from array of Ids to be exported.
       
   315 	TContactItemId idAgentA = (*iIdArray)[0];
       
   316 	iIdArray->Remove(0);
       
   317 	TContactItemId idAgentC = (*iIdArray)[1];
       
   318 	iIdArray->Remove(1);
       
   319 
       
   320 	//export Maincontacts B & D.
       
   321 	ExportContactsL(KAgentTemp);
       
   322 	
       
   323 	_LIT(Kfn,"NewName");
       
   324 	CContactItem* agentItem = NULL;
       
   325 
       
   326 	//change familyname property of Agent contact A
       
   327 	TBuf<20> agentAText;
       
   328 	agentItem = iDb->OpenContactL(idAgentA);
       
   329 	CleanupStack::PushL(agentItem);
       
   330 	//storing name to cross check after import
       
   331 	agentAText.Append(FieldText(*agentItem, KUidContactFieldFamilyName));
       
   332 	TUid aid = {0};
       
   333 	SetNameL(*agentItem, KUidContactFieldFamilyName, aid, Kfn, 0);
       
   334 	iDb->CommitContactL(*agentItem);
       
   335 	CleanupStack::PopAndDestroy();
       
   336 	
       
   337 	//change familyname property of Agent contact C
       
   338 	TBuf<20> agentCText;
       
   339 	agentItem = iDb->OpenContactL(idAgentC);
       
   340 	CleanupStack::PushL(agentItem);
       
   341 	//storing name to cross check after import
       
   342 	agentCText.Append(FieldText(*agentItem, KUidContactFieldFamilyName));
       
   343 	SetNameL(*agentItem, KUidContactFieldFamilyName, aid, Kfn, 0);
       
   344 	iDb->CommitContactL(*agentItem);
       
   345 	CleanupStack::PopAndDestroy();
       
   346 
       
   347 	//All four contacts exist in database, 2 main and 2 agents so iIdArray will have only 2 Ids.
       
   348 	ImportContactsL(KAgentTemp);
       
   349 	
       
   350 	//now check that names should have been restored to what it was before export
       
   351 	agentItem = iDb->ReadContactLC(idAgentA);
       
   352 	test(FieldText(*agentItem, KUidContactFieldFamilyName) == agentAText);
       
   353 	CleanupStack::PopAndDestroy();
       
   354 
       
   355 	agentItem = iDb->ReadContactLC(idAgentC);
       
   356 	test(FieldText(*agentItem, KUidContactFieldFamilyName) == agentCText);
       
   357 	CleanupStack::PopAndDestroy();	
       
   358 	}
       
   359 
       
   360 /**
       
   361 @SYMTestCaseID PIM-AGENT-0003
       
   362 @SYMDEF DEF098709
       
   363 @SYMTestType UT
       
   364 @SYMTestPriority High
       
   365 @SYMTestCaseDesc MainItems should be updated by incoming vCard having Agents.
       
   366 
       
   367 @SYMTestActions
       
   368 1.Create contact items and add contact agents to them.
       
   369 2.Export Main contact items.
       
   370 3.Delete agent fields from Main contact items.
       
   371 4.Import the exported vCard.
       
   372 
       
   373 @SYMTestExpectedResults For the above tests:
       
   374 1.vCard being imported should properly update the existing contact items
       
   375 2.Updated contacts should be restored with old values.
       
   376 3.Agents fields should be added to main contact items.
       
   377 4.No leaves or panics should occur
       
   378 */	
       
   379 
       
   380 void CAgentTest::ImportAgentMiscL()
       
   381 	{
       
   382 	iDb = iTest.CreateDatabaseL();
       
   383 	iIdArray->Reset();
       
   384 	
       
   385 	//Adding 4 contacts (A,B,C,D)to database
       
   386 	AddContactItemsL(4);
       
   387 	//Make A an agent of B 
       
   388 	MakeAgentL((*iIdArray)[1], (*iIdArray)[0]);
       
   389 	//make C an Agent of D
       
   390 	MakeAgentL((*iIdArray)[3], (*iIdArray)[2]);
       
   391 
       
   392 	//storing Id's of agents A and C and removing them from array of Ids to be exported.
       
   393 	TContactItemId idAgentA = (*iIdArray)[0];
       
   394 	iIdArray->Remove(0);
       
   395 	TContactItemId idAgentC = (*iIdArray)[1];
       
   396 	iIdArray->Remove(1);
       
   397 
       
   398 	//export Maincontacts B & D.
       
   399 	ExportContactsL(KAgentTemp);
       
   400 
       
   401 	//remove Agent fields from main contact items B and D.
       
   402 	RemoveFieldFromContactL((*iIdArray)[0], KUidContactFieldVCardMapAGENT);
       
   403 	test(GetAgentCountL((*iIdArray)[0]) == 0);
       
   404 	
       
   405 	RemoveFieldFromContactL((*iIdArray)[1], KUidContactFieldVCardMapAGENT);	
       
   406 	test(GetAgentCountL((*iIdArray)[1]) == 0);
       
   407 
       
   408 	//All four contacts exist in database,Merge will be done for all of them.
       
   409 	//2 main and 2 agents so iIdArray will have only 2 Ids.
       
   410 	//and main contacts should have Agent fields.
       
   411 	ImportContactsL(KAgentTemp);
       
   412 	
       
   413 	test(GetAgentCountL((*iIdArray)[0]) == 1);	
       
   414 	test(GetAgentCountL((*iIdArray)[1]) == 1);	
       
   415 			
       
   416 	}
       
   417 
       
   418 /**
       
   419 @SYMTestCaseID PIM-AGENT-0004
       
   420 @SYMDEF DEF102255
       
   421 @SYMTestType UT
       
   422 @SYMTestPriority High
       
   423 @SYMTestCaseDesc MainItems should be exported even if Agents are deleted.
       
   424 
       
   425 @SYMTestActions
       
   426 1.Create contact items and add contact agents to them.
       
   427 2.Delete agent contacts.
       
   428 3.Export Main contact items.
       
   429 
       
   430 @SYMTestExpectedResults For the above tests:
       
   431 1.Contact items and agents should be created.
       
   432 2.Agent deletion should be successful.
       
   433 3.Main contact items without agents should be exported.
       
   434 4.No leaves or panics should occur.
       
   435 */	
       
   436 void CAgentTest::ExportWithDeletedAgentL()
       
   437 {
       
   438 	iDb = iTest.CreateDatabaseL();
       
   439 	//Adding 4 contacts (A,B,C,D)to database
       
   440 	AddContactItemsL(4);
       
   441 	
       
   442 	//Make A an agent of B 
       
   443 	MakeAgentL((*iIdArray)[1], (*iIdArray)[0]);
       
   444 	
       
   445 	//Make C an Agent of D
       
   446 	MakeAgentL((*iIdArray)[3], (*iIdArray)[2]);
       
   447 	
       
   448 	//Delete agents A & C
       
   449 	iDb->DeleteContactL((*iIdArray)[0]);
       
   450 	iDb->DeleteContactL((*iIdArray)[2]);
       
   451 	
       
   452 	//Removing Id's of agents A & C from array of Ids to be exported
       
   453 	iIdArray->Remove(0);
       
   454 	iIdArray->Remove(1);	
       
   455 	
       
   456 	//Export main contacts B & D
       
   457 	ExportContactsL(KAgentTemp);
       
   458 }
       
   459 
       
   460 /**
       
   461 @SYMTestCaseID PIM-AGENT-0005
       
   462 @SYMDEF PDEF102693
       
   463 @SYMTestType UT
       
   464 @SYMTestPriority High
       
   465 @SYMTestCaseDesc Multiple agents must be Exported properly.
       
   466 
       
   467 @SYMTestActions
       
   468 1.Create contact item with multiple agents.
       
   469 2.Export contact items..
       
   470 3.Import  contact items.
       
   471 4.Check the agents.
       
   472 
       
   473 @SYMTestExpectedResults For the above tests:
       
   474 1. agents should be exported properly.
       
   475 */	
       
   476 void CAgentTest::MultipleAgentExportL()
       
   477 {
       
   478 	CTestRegister* TempFiles = CTestRegister::NewLC();
       
   479 	TempFiles->RegisterL(KAgentExported);
       
   480 
       
   481 	iDb = iTest.CreateDatabaseL();
       
   482 	ImportContactsFromDescriptorL(KMultipleAgentvCard());
       
   483 	_LIT(KAgent1FirstName,"SymbianFoundation");
       
   484 	_LIT(KAgent2FirstName,"Architect");
       
   485 
       
   486 	//remove the other contact item as the imported vacrd contains two contact items.
       
   487 	iIdArray->Remove(0);
       
   488 	iIdArray->Remove(0);
       
   489 	iIdArray->Remove(0);
       
   490 	iIdArray->Remove(0);
       
   491 	
       
   492 	ExportContactsL(KAgentExported);
       
   493 
       
   494 	//clean the database 
       
   495 	iDb = iTest.CreateDatabaseL();
       
   496 	//import contacts from the exported file.	
       
   497 	ImportContactsL(KAgentExported);
       
   498 	//check  the first names of the 
       
   499 	CContactItem* mainItem = iDb->ReadContactLC((*iIdArray)[0]);
       
   500 	test(FieldText(*mainItem, KUidContactFieldFamilyName) == KAgent1FirstName);
       
   501 
       
   502 
       
   503 	mainItem = iDb->ReadContactLC((*iIdArray)[1]);
       
   504 	test(FieldText(*mainItem, KUidContactFieldFamilyName) == KAgent2FirstName);
       
   505 	CleanupStack::PopAndDestroy(3);
       
   506 
       
   507 }
       
   508 
       
   509 /**Utility function to remove any specified contact field from a specified contact item*/
       
   510 TBool CAgentTest::RemoveFieldFromContactL(TContactItemId aContactId, TUid aContactField)
       
   511 	{
       
   512 	CContactItem* mainItem = NULL;
       
   513 	CContactItemFieldSet* fields = NULL;
       
   514 	mainItem = iDb->OpenContactL(aContactId);
       
   515 	CleanupStack::PushL(mainItem);
       
   516 	fields = &mainItem->CardFields();
       
   517 	TInt pos = 0;
       
   518 	pos = fields->Find(aContactField);
       
   519 	if (pos != KErrNotFound)
       
   520 		{
       
   521 		fields->Remove(pos);	
       
   522 		}
       
   523 	iDb->CommitContactL(*mainItem);
       
   524 	CleanupStack::PopAndDestroy();	
       
   525 	return pos;	
       
   526 	}
       
   527 
       
   528 /**Utility function for adding agent to a contact item*/
       
   529 void CAgentTest::MakeAgentL(TContactItemId aMainContactId, TContactItemId aAgentId)
       
   530 	{
       
   531 	CContactItem* mainItem = NULL;
       
   532 	CContactItemField* field = NULL;
       
   533 
       
   534 	mainItem = iDb->OpenContactL(aMainContactId);
       
   535 	CleanupStack::PushL(mainItem);
       
   536 	field = CContactItemField::NewLC(KStorageTypeContactItemId, KUidContactFieldVCardMapAGENT );
       
   537 	field->SetMapping(KUidContactFieldVCardMapAGENT);
       
   538 	field->AgentStorage()->SetAgentId(aAgentId);
       
   539 	mainItem->AddFieldL(*field);
       
   540 	CleanupStack::Pop();
       
   541 
       
   542 	iDb->CommitContactL(*mainItem);
       
   543 	CleanupStack::PopAndDestroy();
       
   544 	}
       
   545 
       
   546 /**Utility function for finding number of agents in a contact item*/
       
   547 TInt CAgentTest::GetAgentCountL(TContactItemId aContactId)
       
   548 	{
       
   549 	CContactItem* mainItem = NULL;
       
   550 	mainItem = iDb->ReadContactL(aContactId);
       
   551 	CleanupStack::PushL(mainItem);
       
   552 	CContactItemFieldSet* fields = &mainItem->CardFields();
       
   553 	TInt count = fields->Count();	
       
   554 	TInt agentCount = 0;
       
   555 	TInt i = 0;
       
   556 	for(; i < count; ++i)
       
   557 		{
       
   558 		if( (*fields)[i].StorageType() == KStorageTypeContactItemId )
       
   559 			{
       
   560 			++agentCount;
       
   561 			}
       
   562 		}
       
   563 	CleanupStack::PopAndDestroy();
       
   564 	return agentCount;
       
   565 	}
       
   566 
       
   567 /**Utility function for Adding any number of contact items
       
   568    with name and telephone fields.
       
   569 */
       
   570 void CAgentTest::AddContactItemsL(TInt aCount)
       
   571 	{
       
   572 	iIdArray->Reset();
       
   573 	CContactItem* templateCard = NULL;
       
   574 	CContactCard* card = NULL;
       
   575 	CContactItemFieldSet* fieldSet = NULL;
       
   576 	
       
   577 	for(TInt loop = 0; loop < aCount; ++loop)
       
   578 		{
       
   579 		TInt pos = KErrNotFound;
       
   580 		TBuf<16> name;
       
   581 		name.Format(_L("NAME%d"),loop);
       
   582 		TBuf<16> tel;
       
   583 		tel.Format(_L("123456%d"),loop);
       
   584 		
       
   585 		templateCard = iDb->ReadContactLC(iDb->TemplateId());
       
   586 		card = CContactCard::NewLC(templateCard); 
       
   587 	
       
   588 		fieldSet = &card->CardFields();
       
   589 		pos = fieldSet->Find(KUidContactFieldFamilyName);
       
   590 		if (pos != KErrNotFound)
       
   591 			{
       
   592 			(*fieldSet)[pos].TextStorage()->SetTextL(name);		
       
   593 			}
       
   594 		pos = fieldSet->Find(KUidContactFieldPhoneNumber);
       
   595 		
       
   596 		if (pos != KErrNotFound)
       
   597 			{
       
   598 			(*fieldSet)[pos].TextStorage()->SetTextL(tel);	
       
   599 			}
       
   600 		
       
   601     	TContactItemId contactId = iDb->AddNewContactL(*card);
       
   602 		iIdArray->AddL(contactId);
       
   603 		CleanupStack::PopAndDestroy(2);
       
   604 		}
       
   605 	}
       
   606 
       
   607 /**
       
   608 
       
   609 @SYMTestCaseID     PIM-T-AGENT-0001
       
   610 
       
   611 */
       
   612 
       
   613 LOCAL_C void DoTestsL()
       
   614 	{
       
   615 	test.Start(_L("@SYMTESTCaseID:PIM-T-AGENT-0001 Preparing tests"));
       
   616 
       
   617 	CAgentTest* agentTest = CAgentTest::NewL(*CntTest);
       
   618 	CleanupStack::PushL(agentTest);
       
   619 	agentTest->RunTestsL();	
       
   620 	CleanupStack::PopAndDestroy(agentTest);
       
   621 	}
       
   622 
       
   623 GLDEF_C TInt E32Main()
       
   624 	{
       
   625     __UHEAP_MARK; 
       
   626     CntTest = new CCntTest();
       
   627 	CntTest->ConstructL(test,KDatabaseFileName);
       
   628     TRAPD(err,DoTestsL());
       
   629 	CntTest->EndTestLib(err);
       
   630 	__UHEAP_MARKEND;	
       
   631 	return KErrNone;
       
   632     }