phonebookengines/contactsmodel/tsrc/Integration/CntPerfTest/src/CustomTemplateTest.cpp
changeset 0 e686773b3f54
child 24 0ba2181d7c28
equal deleted inserted replaced
-1:000000000000 0:e686773b3f54
       
     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 /**
       
    17  @file
       
    18  @publishedAll
       
    19  @released
       
    20 */
       
    21 
       
    22 #include <e32panic.h>
       
    23 #include <test/testexecutelog.h>
       
    24 #include <cntfldst.h>
       
    25 #include "ClientServer.h"
       
    26 #include "CustomTemplateTest.h"
       
    27 
       
    28 _LIT(KRun1,"Empty");
       
    29 _LIT(KRun2,"Full");
       
    30 _LIT(KRun3,"Modified");
       
    31 _LIT(KRun4,"MinimalModified");
       
    32 _LIT(KRun5,"CustomNew");
       
    33 _LIT(KRun6,"OnlyNew");
       
    34 _LIT(KRun7,"Dynamic");
       
    35 _LIT(KRun8,"CustomLabelUid");
       
    36 _LIT(KRun9,"DeleteCustom");
       
    37 _LIT(KRun10,"DeleteSystem");
       
    38 
       
    39 _LIT(KTest1,"Testing empty template...");
       
    40 _LIT(KTest2,"Testing full template...");
       
    41 _LIT(KTest3,"Testing modified template...");
       
    42 _LIT(KTest4,"Testing modified template loaded minimally...");
       
    43 _LIT(KTest5,"Testing template plus custom new fields...");
       
    44 _LIT(KTest6,"Testing template containing custom new fields only...");
       
    45 _LIT(KTest7,"Testing system and custom template dynamic loading...");
       
    46 _LIT(KTest8,"Testing system and custom template with custom labels and uids...");
       
    47 _LIT(KTest9,"Testing deletion of custom template...");
       
    48 _LIT(KTest10,"Testing deletion of system template...");
       
    49 
       
    50 
       
    51 
       
    52 CCustomTemplateTest::CCustomTemplateTest() 
       
    53 			: CCntBaseStep(1) //Number of contacts to be used by this step
       
    54 	{
       
    55 	// Call base class method to set up the human readable name for logging
       
    56 	SetTestStepName(SharedConstants::KCustomTemplateTest);
       
    57 	}
       
    58 	
       
    59 CCustomTemplateTest::~CCustomTemplateTest()
       
    60 	{
       
    61 	_LIT(KDestructPrint,"CCustomTemplateTest destructor");
       
    62 	INFO_PRINTF1(KDestructPrint);
       
    63 	Clean();
       
    64 	TRAPD( err, CommitL() );
       
    65 	if( err )
       
    66 		{
       
    67 		_LIT(KCommitError,"Couldnt commit default contact. Error: %d");
       
    68 		ERR_PRINTF2(KCommitError, err );
       
    69 		}
       
    70 	}
       
    71 
       
    72 TVerdict CCustomTemplateTest::doTestStepL()
       
    73 	{
       
    74 	
       
    75 	__UHEAP_MARK;
       
    76 	InitializeL();
       
    77 	iIterate->Reset();
       
    78 	
       
    79 	const TDesC &run = ConfigSection();
       
    80 	
       
    81 	if( run == KRun1 )
       
    82 		{
       
    83 		INFO_PRINTF1(KTest1);
       
    84 		EmptyTemplateTestL(iIterate->NextL());
       
    85 		}
       
    86 	else if( run == KRun2 )
       
    87 		{
       
    88 		INFO_PRINTF1(KTest2);
       
    89 		FullTemplateTestL(iIterate->NextL());
       
    90 		}
       
    91 	else if( run == KRun3 )
       
    92 		{
       
    93 		INFO_PRINTF1(KTest3);
       
    94 		ModifiedTemplateTestL(iIterate->NextL(), EFalse);
       
    95 		}
       
    96 	else if( run == KRun4 )
       
    97 		{
       
    98 		INFO_PRINTF1(KTest4);
       
    99 		ModifiedTemplateTestL(iIterate->NextL(), ETrue);
       
   100 		}
       
   101 	else if( run == KRun5 )
       
   102 		{
       
   103 		INFO_PRINTF1(KTest5);
       
   104 		NewFieldsTemplateTestL(iIterate->NextL(), ETrue);
       
   105 		}
       
   106 	else if( run == KRun6 )
       
   107 		{
       
   108 		INFO_PRINTF1(KTest6);
       
   109 		NewFieldsTemplateTestL(iIterate->NextL(), EFalse);
       
   110 		}
       
   111 	else if( run == KRun7 )
       
   112 		{
       
   113 		INFO_PRINTF1(KTest7);
       
   114 		TestDynamicLoadingL(iIterate->NextL());
       
   115 		}
       
   116 	else if( run == KRun8 )
       
   117 		{
       
   118 		INFO_PRINTF1(KTest8);
       
   119 		 CustomFieldsTemplateTestL(iIterate->NextL(), ETrue, ETrue );
       
   120 		}
       
   121 	else if( run == KRun9 )
       
   122 		{
       
   123 		INFO_PRINTF1(KTest9);
       
   124 		DeleteCustomTemplateTestL( iIterate->NextL() );
       
   125 		}
       
   126 	else if( run == KRun10 )
       
   127 		{
       
   128 		INFO_PRINTF1(KTest10);
       
   129 		iIterate->PreviousL();
       
   130 		DeleteSystemTemplateTestL( iIterate->NextL() );
       
   131 		}
       
   132 	else
       
   133 		{
       
   134 		MissngTestPanic();
       
   135 		}
       
   136 	Cleanup();
       
   137 	__UHEAP_MARKEND;
       
   138 	
       
   139 	return TestStepResult();
       
   140 	}
       
   141 
       
   142 /**
       
   143 create template with no fields, create contact based on template, check that contact has no fields
       
   144 */	
       
   145 void CCustomTemplateTest::EmptyTemplateTestL(const TContactItemId aCid)
       
   146 	{
       
   147 	LoadContactL(aCid);
       
   148 	iFields->Reset();
       
   149 	//create empty custom template based on empty contact(no fields)
       
   150 	CreateCustomTemplateL();
       
   151 	
       
   152 	//create contact based on templated
       
   153 	const TContactItemId &KTemplatedContactId = AddTemplatedContactL(*iCustomTemplate);
       
   154 	
       
   155 	Clean();
       
   156 	
       
   157 	//read templated contact
       
   158 	LoadContactL(KTemplatedContactId);
       
   159 	//check that contact is empty
       
   160 	INTCOMPARE(iFields->Count(), ==, 0, 0, 0 );
       
   161 	
       
   162 	Clean();
       
   163 	iContactsDatabase->DeleteContactL(iCustomTemplateId);
       
   164 	iContactsDatabase->DeleteContactL(KTemplatedContactId);
       
   165 	iCustomTemplateId = -1;
       
   166 	}
       
   167 
       
   168 /**
       
   169 create template with all fields, create contact based on template, check that contact has all fields
       
   170 */		
       
   171 void CCustomTemplateTest::FullTemplateTestL(const TContactItemId aCid)
       
   172 	{
       
   173 	LoadContactL(aCid);
       
   174 	SetContactFieldsL(*iContactRead);
       
   175 	const TInt KFullContactFieldCount = iFields->Count();
       
   176 	//create full custom template based on full contact (fully populated list of fields)
       
   177 	CreateCustomTemplateL();
       
   178 	
       
   179 	//create contact based on templated
       
   180 	const TContactItemId &KTemplatedContactId = AddTemplatedContactL(*iCustomTemplate);
       
   181 	
       
   182 	Clean();
       
   183 	
       
   184 	//read templated contact
       
   185 	LoadContactL(KTemplatedContactId);
       
   186 	
       
   187 	const TInt KTemplatedContactFieldCount = iFields->Count();
       
   188 	
       
   189 	//checks that all fields in contact are empty
       
   190 	INTCOMPARE(KTemplatedContactFieldCount, ==, KFullContactFieldCount, 0 ,0); 
       
   191 	//check that all fields in templated contact are empty
       
   192 	INTCOMPARE(CountEmpty(*iFields), ==, KTemplatedContactFieldCount, 0, 0);
       
   193 	
       
   194 	Clean();
       
   195 	iContactsDatabase->DeleteContactL(KTemplatedContactId);
       
   196 	iContactsDatabase->DeleteContactL(iCustomTemplateId);
       
   197 	iCustomTemplateId = -1;	
       
   198 	}
       
   199 
       
   200 /**
       
   201 create template then set all its fields to preset values, create contact based on template
       
   202 check that all field values, labels, uids match between template and contact
       
   203 */		
       
   204 void CCustomTemplateTest::ModifiedTemplateTestL(const TContactItemId aCid, const TBool aReadMinimal)
       
   205 	{
       
   206 	LoadContactL(aCid);
       
   207 	const TInt KFullContactFieldCount = iFields->Count();
       
   208 	
       
   209 	//create custom template based on full contact
       
   210 	CreateCustomTemplateL();
       
   211 	//create contact based on custom templated	
       
   212 	const TContactItemId &KTemplatedContactId = AddTemplatedContactL(*iCustomTemplate);	
       
   213 	Clean();
       
   214 	
       
   215 	//open custom template
       
   216 	OpenL(iCustomTemplateId);
       
   217 	//update template fields to contain preset values
       
   218 	SetContactFieldsL(*iContactOpened);
       
   219 	CommitL();
       
   220 	
       
   221 	//read contact based on custom tempalte, either fully or minimally
       
   222 	LoadContactL(KTemplatedContactId, aReadMinimal);
       
   223 	
       
   224 	const TInt KTemplatedContactFieldCount = iFields->Count();
       
   225 	if(aReadMinimal)
       
   226 		{
       
   227 		//minimal read doesn't merge contact with template, no contact field should be read
       
   228 		INTCOMPARE( KTemplatedContactFieldCount, ==, 0, 0, 0 );
       
   229 		INTCOMPARE(CountEmpty(*iFields), ==, 0, 0, 0);
       
   230 		}
       
   231 	else
       
   232 		{
       
   233 		//full read merges contact with template, all fields should be loaded
       
   234 		INTCOMPARE(KTemplatedContactFieldCount, ==, KFullContactFieldCount, 0,0 );
       
   235 		//all fields should be full
       
   236 		INTCOMPARE(CountEmpty(*iFields), ==, 0, 0, 0);
       
   237 		//templated contact should match template
       
   238 		SINGLECOMPARE(CompareToTemplateL( iCustomTemplateId, *iFields), 0 ,0 );
       
   239 		}
       
   240 	Clean();
       
   241 	iContactsDatabase->DeleteContactL(KTemplatedContactId);
       
   242 	iContactsDatabase->DeleteContactL(iCustomTemplateId);
       
   243 	iCustomTemplateId = -1;
       
   244 	}
       
   245 
       
   246 /**
       
   247 create template then add 10 new custom fields set all fields to preset values, 
       
   248 create contact based on template check that all field values, labels, uids 
       
   249 match between template and contact
       
   250 */		
       
   251 void CCustomTemplateTest::NewFieldsTemplateTestL(const TContactItemId aCid, const TBool aNewAndOldFields)
       
   252 	{
       
   253 	LoadContactL(aCid);
       
   254 	if(!aNewAndOldFields)
       
   255 		{
       
   256 		//delete all contacts		
       
   257 		iFields->Reset();
       
   258 		}
       
   259 	//add custom new fields to contact
       
   260 	for(TInt i = 0, uids = TestConstants::KAltUID; i < 10; i++, uids++)
       
   261 		{
       
   262 		AddNewFieldL( *iFields, TestConstants::KShortString, TFieldType::Uid(uids) );
       
   263 		}
       
   264 		
       
   265 	const TInt KBaseContactFieldCount = iFields->Count();	
       
   266 	//create custom template based on base contact
       
   267 	CreateCustomTemplateL();
       
   268 	const TContactItemId &KTemplatedContactId = AddTemplatedContactL(*iCustomTemplate);	
       
   269 	Clean();
       
   270 	
       
   271 	//populate custom template fields
       
   272 	OpenL(iCustomTemplateId);
       
   273 	SetContactFieldsL(*iContactOpened);		
       
   274 	CommitL();
       
   275 	
       
   276 	//read templated contact
       
   277 	LoadContactL(KTemplatedContactId);
       
   278 	
       
   279 	//templated contact should contain all fields
       
   280 	const TInt KTemplatedContactFieldCount = iFields->Count();
       
   281 	INTCOMPARE(KTemplatedContactFieldCount, ==, KBaseContactFieldCount, 0 ,0 );
       
   282 	
       
   283 	//all fields should be populated 
       
   284 	INTCOMPARE(CountEmpty(*iFields), ==, 0, 0, 0);
       
   285 	//fields should match template
       
   286 	SINGLECOMPARE(CompareToTemplateL( iCustomTemplateId, *iFields), 0, 0);
       
   287 	
       
   288 	Clean();
       
   289 	iContactsDatabase->DeleteContactL(KTemplatedContactId);
       
   290 	iContactsDatabase->DeleteContactL(iCustomTemplateId);
       
   291 	iCustomTemplateId = -1;
       
   292 	}
       
   293 	
       
   294 
       
   295 
       
   296 /**
       
   297 create custom template, then create contact based on template. perform dynmaic loading on custom template.
       
   298 Repeate fro system template
       
   299 */	
       
   300 void CCustomTemplateTest::TestDynamicLoadingL(const TContactItemId aCid)
       
   301 	{
       
   302 	_LIT(KStartSystem, "SystemTemplate Dynamic testing");
       
   303 	_LIT(KStartCustom, "CustomTemplate Dynamic testing");
       
   304 	
       
   305 	INFO_PRINTF1(KStartSystem);
       
   306 	LoadContactL(aCid);
       
   307 	const TInt KBaseContactFieldCount = iFields->Count();
       
   308 	//create custom template based on full contact
       
   309 	CreateCustomTemplateL();	
       
   310 	//create templated contact
       
   311 	const TContactItemId &KTemplatedContactId = AddTemplatedContactL(*iCustomTemplate);	
       
   312 	Clean();
       
   313 	
       
   314 	//populate custom template fields
       
   315 	OpenL(iCustomTemplateId);
       
   316 	SetContactFieldsL(*iContactOpened);//need to set all fields otherwise field order is not preserved		
       
   317 	CommitL();
       
   318 	
       
   319 	//perform dyamic loading for custom template
       
   320 	doDynamicLoadingL(KTemplatedContactId, iCustomTemplateId, KBaseContactFieldCount);
       
   321 	iContactsDatabase->DeleteContactL(iCustomTemplateId);
       
   322 	iContactsDatabase->DeleteContactL(KTemplatedContactId);
       
   323 		
       
   324 	iCustomTemplateId = -1;
       
   325 	
       
   326 	INFO_PRINTF1(KStartCustom);
       
   327 	//open system template
       
   328 	OpenL(iContactsDatabase->TemplateId());
       
   329 	//backup template fields
       
   330 	CContactItemFieldSet * SystemTemplateFieldsetCopy = CopyFieldSetLC(*iFields);
       
   331 	const TInt KSystemTemplateFieldCount = iFields->Count();
       
   332 	//add template contact
       
   333 	const TContactItemId &KSystemTemplatedContactId = AddTemplatedContactL( iContactsDatabase->TemplateId() );
       
   334 	//populate fields for system template
       
   335 	SetContactFieldsL(*iContactOpened);//need to set all fields otherwise field order is not preserved	
       
   336 
       
   337 	CContactItemFieldSet &contactFields = iContactOpened->CardFields();
       
   338 	const TInt KFieldsCount = contactFields.Count();
       
   339 	
       
   340 	// The system template's fields must not contain any data (see CContactDatabase::CommitContactL)
       
   341 	if (iContactOpened->Id() == iContactsDatabase->TemplateId())
       
   342 		{	
       
   343 		for( TInt j = 0; j < KFieldsCount; ++j )
       
   344 			{
       
   345 			contactFields[j].ResetStore();
       
   346 			}
       
   347 		}
       
   348 		
       
   349 	CommitL();
       
   350 	
       
   351 	//perfrom dynamic loading for system template
       
   352 	doDynamicLoadingL(KSystemTemplatedContactId, iContactsDatabase->TemplateId(), KSystemTemplateFieldCount-1, ETrue);
       
   353 	
       
   354 	iContactsDatabase->DeleteContactL(KSystemTemplatedContactId);
       
   355 	
       
   356 	OpenL(iContactsDatabase->TemplateId());
       
   357 	iContactOpened->UpdateFieldSet(SystemTemplateFieldsetCopy);//restore system template
       
   358 	CleanupStack::Pop(SystemTemplateFieldsetCopy);
       
   359 	CommitL();
       
   360 	}
       
   361 
       
   362 /**
       
   363 delete 1 field from template (aTemplate) and set value for next field. 
       
   364 reload contact (aCid) based on template, check to see that it matches the updated template.
       
   365 repeat for all fields in template
       
   366 */
       
   367 void CCustomTemplateTest::doDynamicLoadingL(	const TContactItemId aCid, 
       
   368 												const TContactItemId aTemplate, 
       
   369 												const TInt aCount,
       
   370 												const TBool aSystemTemplate)
       
   371 	{
       
   372 	_LIT(KDynamic,"DynamicTestValues");
       
   373 	CContactItemField* NextField = NULL;
       
   374 	TInt ContactFieldsCount = 0;  // avoid compiler warning
       
   375 	TInt PositionOfNextField = 0;  // avoid compiler warning
       
   376 	const TInt KLastFieldIndex = aSystemTemplate ? 0 : 1;
       
   377 	TInt initialFieldCount = 70;  // number of fields in the template
       
   378 
       
   379 	for(TInt i = aCount ; i > KLastFieldIndex; --i)
       
   380 		{
       
   381 		//open template
       
   382 		OpenL(aTemplate);
       
   383 		
       
   384 		//the template's name ("Group / Template Label") is not part of the Contact based on the template
       
   385 		const CContentType& KContentType = (*iFields)[i].ContentType();	
       
   386 		
       
   387 		if (KContentType.ContainsFieldType(KUidContactFieldTemplateLabel) && aSystemTemplate)
       
   388 			{
       
   389 			CommitL();
       
   390 			continue;
       
   391 			}
       
   392 		
       
   393 		//remove 1 field
       
   394 		iFields->Remove(i);
       
   395 		
       
   396 		// if this is not the system template
       
   397 		if (iContactOpened->Id() != iContactsDatabase->TemplateId())
       
   398 			{
       
   399 			//set value of next field to dynamic value
       
   400 			SetFieldL(*iFields, i-1, KDynamic);
       
   401 			}
       
   402 		
       
   403 		//copy next field
       
   404 		NextField = CContactItemField::NewLC( (*iFields)[i-1]);		
       
   405 		
       
   406 		CommitL();
       
   407 		
       
   408 		//read templated contact
       
   409 		LoadContactL(aCid);
       
   410 		ContactFieldsCount = iFields->Count();
       
   411 		
       
   412 		//search for next field in tempalted contact
       
   413 		PositionOfNextField = FindCtype( NextField->ContentType(), *iFields );
       
   414 		//check that field count equal initial fieldcount - number of fields removed	
       
   415 		INTCOMPARE( ContactFieldsCount, ==, ( initialFieldCount - 1 ), i, 0 );
       
   416 		initialFieldCount--;
       
   417 		
       
   418 		//if next field not found
       
   419 		if(PositionOfNextField == KErrNotFound)
       
   420 			{
       
   421 			//if not system template 
       
   422 			if( !aSystemTemplate) 
       
   423 				{
       
   424 				//fail test
       
   425 				SINGLECOMPARE(EFalse, i, 0);
       
   426 				}
       
   427 			}
       
   428 		//if next field found
       
   429 		else
       
   430 			{		 	
       
   431 			if(!aSystemTemplate)  // the system template fields do not contain data
       
   432 				{
       
   433 				SINGLECOMPARE( CompareFieldsL(*iFields, PositionOfNextField, KDynamic), i, 0 );
       
   434 				}
       
   435 			}
       
   436 		CleanupStack::PopAndDestroy(NextField);
       
   437 		NextField = NULL;
       
   438 		Clean();
       
   439 		}
       
   440 	}
       
   441 
       
   442 /**
       
   443 set custom labels and/or uids for template. then create contact based on template. 
       
   444 check contact matches template. repeate for contact based on system template
       
   445 */	
       
   446 void CCustomTemplateTest::CustomFieldsTemplateTestL(	const TContactItemId aCid, 
       
   447 														const TBool aSetCustomLabels,
       
   448 														const TBool aUids)
       
   449 	{
       
   450 	LoadContactL(aCid);
       
   451 	//create custom template based on full contact
       
   452 	CreateCustomTemplateL();	
       
   453 	//create templated contact
       
   454 	const TContactItemId &KTemplatedContactId = AddTemplatedContactL(*iCustomTemplate);	
       
   455 	Clean();
       
   456 	
       
   457 	//open custom template
       
   458 	OpenL(iCustomTemplateId);
       
   459 	SetContactFieldsL(*iContactOpened);//need to set all fields otherwise field order is not preserved
       
   460 	if( aSetCustomLabels )
       
   461 		{
       
   462 		//update template field labels
       
   463 		SetContactLabelsL(*iContactOpened);
       
   464 		}
       
   465 	if( aUids )
       
   466 		{
       
   467 		//update template field uids
       
   468 		AddUIDsL(*iFields,TestConstants::KInitialUID);
       
   469 		}
       
   470 	CommitL();
       
   471 	
       
   472 	//load templated contact
       
   473 	LoadContactL(KTemplatedContactId);
       
   474 	//compare fields from template contact to template
       
   475 	SINGLECOMPARE(CompareToTemplateL( iCustomTemplateId, *iFields),0,0);
       
   476 	Clean();
       
   477 	
       
   478 	iContactsDatabase->DeleteContactL(KTemplatedContactId);
       
   479 	iContactsDatabase->DeleteContactL(iCustomTemplateId);
       
   480 	iCustomTemplateId = -1;
       
   481 	
       
   482 	//create templated contact based on sytem template
       
   483 	const TContactItemId &KSystemTemplatedContactId = AddTemplatedContactL( iContactsDatabase->TemplateId() );
       
   484 	OpenL(iContactsDatabase->TemplateId());
       
   485 	//backup system template fields
       
   486 	CContactItemFieldSet *SystemTemplateFieldsetCopy = CopyFieldSetLC(*iFields);
       
   487 	iFields->Remove( iFields->Find(KUidContactFieldTemplateLabel) );//remove group field
       
   488 	
       
   489 	const TInt KFieldsCount = iFields->Count();
       
   490 	for( TInt j = 0; j < KFieldsCount; ++j )
       
   491 		{
       
   492 		(*iFields)[j].ResetStore();
       
   493 		}
       
   494 	
       
   495 	
       
   496 	if( aSetCustomLabels )
       
   497 		{
       
   498 		//update template field labels
       
   499 		SetContactLabelsL(*iContactOpened);
       
   500 		}
       
   501 	if( aUids )
       
   502 		{
       
   503 		//update template field uids
       
   504 		AddUIDsL(*iFields,TestConstants::KInitialUID);
       
   505 		}
       
   506 	CommitL();
       
   507 	
       
   508 	//load templated contact
       
   509 	LoadContactL(KSystemTemplatedContactId);
       
   510 	//compare fields from template contact to system template
       
   511 	SINGLECOMPARE(CompareToTemplateL( iContactsDatabase->TemplateId(), *iFields, 0, ETrue ),0,0);
       
   512 	Clean();
       
   513 	
       
   514 	iContactsDatabase->DeleteContactL(KSystemTemplatedContactId);
       
   515 	
       
   516 	OpenL(iContactsDatabase->TemplateId());
       
   517 	iContactOpened->UpdateFieldSet(SystemTemplateFieldsetCopy);//restore system template
       
   518 	CleanupStack::Pop(SystemTemplateFieldsetCopy);
       
   519 	CommitL();
       
   520 	}
       
   521 
       
   522 /**
       
   523 create custom template, create contact based on template, delete template. reload template,
       
   524 check to see it is marked as deleted. attempt to read contact again. delete contact and check
       
   525 whether template is still in the dbase, if so delete template
       
   526 */	
       
   527 void CCustomTemplateTest::DeleteCustomTemplateTestL(const TContactItemId aCid)
       
   528 	{
       
   529 	TInt err = KErrNone;
       
   530 	CContactItem *ContactItem = NULL;
       
   531 	
       
   532 	//read contact item		
       
   533 	LoadContactL(aCid);
       
   534 	const TInt KContactFieldCount = iFields->Count();
       
   535 	const TInt KContactHiddenCount = CountHiddenFields(*iFields);
       
   536 	
       
   537 	//create custom template based on full contact
       
   538 	CreateCustomTemplateL();
       
   539 	//create templated contact	
       
   540 	const TContactItemId &KTemplatedContactId = AddTemplatedContactL(*iCustomTemplate);
       
   541 	Clean();
       
   542 	
       
   543 	//open custom template
       
   544 	OpenL(iCustomTemplateId);
       
   545 	//populate template
       
   546 	SetContactFieldsL(*iContactOpened);//need to set all fields otherwise field order is not preserved
       
   547 	
       
   548 	const TInt KTemplateAccessCount = iContactOpened->AccessCount();
       
   549 	//check that template is not deleteable as a conatct is based on it
       
   550 	SINGLECOMPARE(!iContactOpened->IsDeletable(),0,0);
       
   551 	//access count should equal number of contacts based on template, 1
       
   552 	INTCOMPARE( KTemplateAccessCount, ==, 1, 0, 0  );		
       
   553 	CommitL();
       
   554 	
       
   555 	//attempt to delete custom template
       
   556 	iContactsDatabase->DeleteContactL(iCustomTemplateId);
       
   557 	
       
   558 	//read deleted template		
       
   559 	TRAP( err, ContactItem = iContactsDatabase->ReadContactL(iCustomTemplateId,*iViewAll) );
       
   560 	//template should not have been fully deleted
       
   561 	INTCOMPARE( err, ==, KErrNone, 0, 0  );
       
   562 	//template should be marked as deleted
       
   563 	SINGLECOMPARE(ContactItem->IsDeleted(),0,0);
       
   564 	CLEAR(ContactItem);
       
   565 	
       
   566 	//deleted template with access count > 0 should contain the same number of fields pre as post delete.
       
   567 	SINGLECOMPARE(CheckDeleteItemL(KTemplatedContactId, KContactFieldCount, KContactHiddenCount),0,0);
       
   568 	SINGLECOMPARE(CheckDeleteItemL(iCustomTemplateId, KContactFieldCount+1, KContactHiddenCount),0,0);
       
   569 	
       
   570 	//it should be possible to read templated contact
       
   571 	TRAP( err, ContactItem = iContactsDatabase->ReadContactL(KTemplatedContactId,*iViewAll) );
       
   572 	INTCOMPARE( err, ==, KErrNone,0,0);
       
   573 	CLEAR(ContactItem);
       
   574 	
       
   575 	//it should be possible to delete templated contact
       
   576 	iContactsDatabase->DeleteContactL(KTemplatedContactId);
       
   577 	//templated contact should not exist / be deleted
       
   578 	TRAP( err, ContactItem = iContactsDatabase->ReadContactL(iCustomTemplateId,*iViewAll) );
       
   579 	INTCOMPARE( err, ==, KErrNotFound,0,0 );
       
   580 	CLEAR(ContactItem);
       
   581 	
       
   582 	iCustomTemplateId = -1;
       
   583 	}
       
   584 
       
   585 /**
       
   586 attempt to delete system template, check that it fails and doesnt affect any contacts based on it.
       
   587 */
       
   588 void CCustomTemplateTest::DeleteSystemTemplateTestL(const TContactItemId aCid)
       
   589 	{
       
   590 	TInt err = KErrNone;
       
   591 	CContactItem *ContactItem = NULL;
       
   592 	const TContactItemId &KSystemTemplateId = iContactsDatabase->TemplateId();
       
   593 	LoadContactL(aCid);
       
   594 	const TInt KContactFieldCount = iFields->Count();
       
   595 	Clean();
       
   596 	//attempt to delete system template
       
   597 	TRAP( err, iContactsDatabase->DeleteContactL( KSystemTemplateId ) );
       
   598 	//it should not be possible
       
   599 	INTCOMPARE(KErrNotSupported, ==, err,0,0);
       
   600 	
       
   601 	//attempt to read system template
       
   602 	TRAP( err, ContactItem = iContactsDatabase->ReadContactL( KSystemTemplateId ,*iViewAll) );
       
   603 	CLEAR(ContactItem);
       
   604 	//template should not have been deleted
       
   605 	INTCOMPARE( KErrNone, ==, err,0,0 );
       
   606 	
       
   607 	//read system template
       
   608 	LoadContactL(aCid);
       
   609 	const TInt KDeletedContactFieldCount = iFields->Count();
       
   610 	Clean();
       
   611 	//number of fields should be unchanged
       
   612 	INTCOMPARE( KDeletedContactFieldCount, ==, KContactFieldCount,0,0 );
       
   613 	}
       
   614 
       
   615 //deleted template with access count > 0 should contain the same number of fields pre as post delete.
       
   616 TBool CCustomTemplateTest::CheckDeleteItemL(const TContactItemId aCid,
       
   617 											const TInt aFieldCountPreDelete, 
       
   618 											const TInt aHiddenCountPreDelete )
       
   619 	{
       
   620 	TBool ret = EFalse;
       
   621 	
       
   622 	LoadContactL(aCid);
       
   623 	const TInt KFieldCountPostDelete = iFields->Count();
       
   624 	const TInt KHiddenFieldCountPostDelete = CountHiddenFields(*iFields);
       
   625 	const TInt KEmptyFieldCountPostDelete = CountEmpty(*iFields);
       
   626 	ret = (KFieldCountPostDelete == aFieldCountPreDelete);
       
   627 	ret = ret && ( KEmptyFieldCountPostDelete == 0 ) && ( KHiddenFieldCountPostDelete == aHiddenCountPreDelete);
       
   628 	
       
   629 	Clean();
       
   630 	
       
   631 	return ret;
       
   632 	}
       
   633 	
       
   634 void CCustomTemplateTest::CreateCustomTemplateL()
       
   635 	{
       
   636 	_LIT(KTLabel,"UserDefined template");
       
   637 	iCustomTemplate = iContactsDatabase->CreateContactCardTemplateL(iContactRead,KTLabel);
       
   638 	iCustomTemplateId = iCustomTemplate->Id();
       
   639 	}
       
   640 	
       
   641 void CCustomTemplateTest::LoadContactL(const TContactItemId aCid, const TBool aReadMinimal)
       
   642 	{
       
   643 	if(aReadMinimal)
       
   644 		{
       
   645 		iContactRead = iContactsDatabase->ReadMinimalContactL(aCid);
       
   646 		}
       
   647 	else
       
   648 		{
       
   649 		iContactRead = iContactsDatabase->ReadContactL(aCid,*iViewAll);
       
   650 		}
       
   651 	iFields = &(iContactRead->CardFields());
       
   652 	}
       
   653 	
       
   654 void CCustomTemplateTest::OpenL(const TContactItemId aCid)
       
   655 	{
       
   656 	iContactOpened = iContactsDatabase->OpenContactL(aCid,*iViewAll);
       
   657 	iFields = &(iContactOpened->CardFields());
       
   658 	}
       
   659 	
       
   660 void CCustomTemplateTest::CommitL()
       
   661 	{
       
   662 	if(NULL == iContactOpened)
       
   663 		{
       
   664 		return;
       
   665 		}
       
   666 	iContactsDatabase->CommitContactL(*iContactOpened);
       
   667 	CLEAR(iContactOpened);
       
   668 	iFields = NULL;
       
   669 	}
       
   670 	
       
   671 TContactItemId CCustomTemplateTest::AddTemplatedContactL(const CContactItem &aTemplate)
       
   672 	{
       
   673 	CContactCard* contact = CContactCard::NewLC(&aTemplate);
       
   674 	const TContactItemId &cid = iContactsDatabase->AddNewContactL(*contact);
       
   675 	CleanupStack::PopAndDestroy(contact);
       
   676 	return cid;
       
   677 	}
       
   678 
       
   679 TContactItemId CCustomTemplateTest::AddTemplatedContactL(const TContactItemId aTemplate)
       
   680 	{
       
   681 	CContactItem *ctemplate = iContactsDatabase->ReadContactLC(aTemplate,*iViewAll);
       
   682 	const TContactItemId &cid = AddTemplatedContactL(*ctemplate);
       
   683 	CleanupStack::PopAndDestroy(ctemplate);
       
   684 	return cid;
       
   685 	}
       
   686 	
       
   687 void CCustomTemplateTest::Clean()
       
   688 	{
       
   689 	delete iCustomTemplate;
       
   690 	iCustomTemplate = NULL;
       
   691 	CLEAR(iContactRead);
       
   692 	iFields = NULL;
       
   693 	}
       
   694 
       
   695 TBool CCustomTemplateTest::CompareToTemplateL( 	const TContactItemId aCid, 
       
   696 												const CContactItemFieldSet &aFields,
       
   697 												const TInt aStart,
       
   698 												const TBool aNogroup )
       
   699 	{
       
   700 	_LIT(KGroupField, "Group / Template Label");
       
   701 	TBool ret = EFalse;
       
   702 	const CContactItem *citem = iContactsDatabase->ReadContactLC(aCid,*iViewAll);
       
   703 	const CContactItemFieldSet &cfields = citem->CardFields();
       
   704 	const TInt count = cfields.Count();
       
   705 
       
   706 	TInt fPos = -1;
       
   707 	TInt i = aStart;//ignore first field in template as that is template 
       
   708 					//related custom field (access count?)
       
   709 		
       
   710 	for(; i < count; ++i)
       
   711 		{
       
   712 		if( aNogroup && ( cfields[i].Label() ).Compare(KGroupField) == 0)//ignore group field
       
   713 			{
       
   714 			continue;
       
   715 			}
       
   716 		else if( ( fPos = FindCtype( cfields[i].ContentType(), aFields ) ) == KErrNotFound )
       
   717 			{
       
   718 			break;
       
   719 			}
       
   720 		else if( ( aFields[fPos].Label() ).Compare( cfields[i].Label() ) != 0 )
       
   721 			{
       
   722 			break;
       
   723 			}
       
   724 		// the system template fields should contain no data (i.e. don't test for this)	
       
   725 		}
       
   726 		
       
   727 	if( count == i )
       
   728 		{
       
   729 		ret = ETrue;
       
   730 		}
       
   731 	CleanupStack::PopAndDestroy( CONST_CAST(CContactItem *, citem) );
       
   732 	return ret;
       
   733 	}
       
   734 
       
   735 TInt CCustomTemplateTest::FindCtype( const CContentType &aCtype, const CContactItemFieldSet &aFields )
       
   736 	{
       
   737 	TInt ret = KErrNotFound;
       
   738 	const TInt count = aFields.Count();
       
   739 	for(TInt i = 0; i < count; ++i)
       
   740 		{
       
   741 		if( aCtype == aFields[i].ContentType() )
       
   742 			{
       
   743 			return i;
       
   744 			}
       
   745 		}
       
   746 	return ret;
       
   747 	}
       
   748 
       
   749 CContactItemFieldSet *CCustomTemplateTest::CopyFieldSetLC(const CContactItemFieldSet &aFields)
       
   750 	{
       
   751 	const TInt count = aFields.Count();
       
   752 	CContactItemFieldSet *FieldSetCopy = CContactItemFieldSet::NewLC();
       
   753 	FieldSetCopy->Reset();//make sure its empty
       
   754 	CContactItemField *field = NULL;
       
   755 	
       
   756 	for( TInt i = 0; i < count; ++i )
       
   757 		{
       
   758 		field = CContactItemField::NewLC(aFields[i]);
       
   759 		FieldSetCopy->AddL(*field);
       
   760 		CleanupStack::Pop(field);
       
   761 		field = NULL;
       
   762 		}
       
   763 	return FieldSetCopy;
       
   764 	}
       
   765 
       
   766 TBool CCustomTemplateTest::CompareFields(const CContactItemField &aField1, const CContactItemField &aField2) const
       
   767 	{
       
   768 	TBool ret = EFalse;
       
   769 	if(  aField1.IsTemplateLabelField() || aField2.IsTemplateLabelField() )
       
   770 		{
       
   771 		return ret;
       
   772 		}
       
   773 	switch( aField1.StorageType() )
       
   774 		{
       
   775 		case KStorageTypeText:
       
   776 			ret = (  (aField1.TextStorage()->Text()).Compare(aField2.TextStorage()->Text()) ) == 0;
       
   777 		break;
       
   778 		case KStorageTypeStore:
       
   779 			ret = ( ( aField1.StoreStorage()->Thing() )->Compare( *(aField2.StoreStorage()->Thing()) ) == 0 );
       
   780 		break;
       
   781 		case KStorageTypeContactItemId:
       
   782 			ret = aField1.AgentStorage()->Value() == aField2.AgentStorage()->Value();
       
   783 		break;
       
   784 		case KStorageTypeDateTime:
       
   785 			ret = aField1.DateTimeStorage()->Time() == aField2.DateTimeStorage()->Time();
       
   786 		break;
       
   787 		default:
       
   788 			{
       
   789 			_LIT(KCompareFields,"comparefields panic");
       
   790 			User::Panic(KCompareFields,EInvariantFalse);
       
   791 			}
       
   792 		break;
       
   793 		}
       
   794 	return ret;
       
   795 	
       
   796 	}
       
   797 
       
   798 
       
   799