summaryrefslogtreecommitdiffstats
path: root/plugins/contacts/symbian/contactsmodel/tsrc/integration/cntperftest/src/customtemplatetest.cpp
blob: 4c9a2357ca40fbcda892f93d9f2f742402c1d6f8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
/*
* Copyright (c) 2006-2009 Nokia Corporation and/or its subsidiary(-ies).
* All rights reserved.
* This component and the accompanying materials are made available
* under the terms of "Eclipse Public License v1.0"
* which accompanies this distribution, and is available
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
*
* Initial Contributors:
* Nokia Corporation - initial contribution.
*
* Contributors:
*
* Description: 
*
*/


/**
 @file
 @publishedAll
 @released
*/

#include <e32panic.h>
#include <test/testexecutelog.h>
#include <cntfldst.h>
#include "clientserver.h"
#include "customtemplatetest.h"

_LIT(KRun1,"Empty");
_LIT(KRun2,"Full");
_LIT(KRun3,"Modified");
_LIT(KRun4,"MinimalModified");
_LIT(KRun5,"CustomNew");
_LIT(KRun6,"OnlyNew");
_LIT(KRun7,"Dynamic");
_LIT(KRun8,"CustomLabelUid");
_LIT(KRun9,"DeleteCustom");
_LIT(KRun10,"DeleteSystem");

_LIT(KTest1,"Testing empty template...");
_LIT(KTest2,"Testing full template...");
_LIT(KTest3,"Testing modified template...");
_LIT(KTest4,"Testing modified template loaded minimally...");
_LIT(KTest5,"Testing template plus custom new fields...");
_LIT(KTest6,"Testing template containing custom new fields only...");
_LIT(KTest7,"Testing system and custom template dynamic loading...");
_LIT(KTest8,"Testing system and custom template with custom labels and uids...");
_LIT(KTest9,"Testing deletion of custom template...");
_LIT(KTest10,"Testing deletion of system template...");



CCustomTemplateTest::CCustomTemplateTest() 
			: CCntBaseStep(1) //Number of contacts to be used by this step
	{
	// Call base class method to set up the human readable name for logging
	SetTestStepName(SharedConstants::KCustomTemplateTest);
	}
	
CCustomTemplateTest::~CCustomTemplateTest()
	{
	_LIT(KDestructPrint,"CCustomTemplateTest destructor");
	INFO_PRINTF1(KDestructPrint);
	Clean();
	TRAPD( err, CommitL() );
	if( err )
		{
		_LIT(KCommitError,"Couldnt commit default contact. Error: %d");
		ERR_PRINTF2(KCommitError, err );
		}
	}

TVerdict CCustomTemplateTest::doTestStepL()
	{
	
	__UHEAP_MARK;
	InitializeL();
	iIterate->Reset();
	
	const TDesC &run = ConfigSection();
	
	if( run == KRun1 )
		{
		INFO_PRINTF1(KTest1);
		EmptyTemplateTestL(iIterate->NextL());
		}
	else if( run == KRun2 )
		{
		INFO_PRINTF1(KTest2);
		FullTemplateTestL(iIterate->NextL());
		}
	else if( run == KRun3 )
		{
		INFO_PRINTF1(KTest3);
		ModifiedTemplateTestL(iIterate->NextL(), EFalse);
		}
	else if( run == KRun4 )
		{
		INFO_PRINTF1(KTest4);
		ModifiedTemplateTestL(iIterate->NextL(), ETrue);
		}
	else if( run == KRun5 )
		{
		INFO_PRINTF1(KTest5);
		NewFieldsTemplateTestL(iIterate->NextL(), ETrue);
		}
	else if( run == KRun6 )
		{
		INFO_PRINTF1(KTest6);
		NewFieldsTemplateTestL(iIterate->NextL(), EFalse);
		}
	else if( run == KRun7 )
		{
		INFO_PRINTF1(KTest7);
		TestDynamicLoadingL(iIterate->NextL());
		}
	else if( run == KRun8 )
		{
		INFO_PRINTF1(KTest8);
		 CustomFieldsTemplateTestL(iIterate->NextL(), ETrue, ETrue );
		}
	else if( run == KRun9 )
		{
		INFO_PRINTF1(KTest9);
		DeleteCustomTemplateTestL( iIterate->NextL() );
		}
	else if( run == KRun10 )
		{
		INFO_PRINTF1(KTest10);
		iIterate->PreviousL();
		DeleteSystemTemplateTestL( iIterate->NextL() );
		}
	else
		{
		MissngTestPanic();
		}
	Cleanup();
	__UHEAP_MARKEND;
	
	return TestStepResult();
	}

/**
create template with no fields, create contact based on template, check that contact has no fields
*/	
void CCustomTemplateTest::EmptyTemplateTestL(const TContactItemId aCid)
	{
	LoadContactL(aCid);
	iFields->Reset();
	//create empty custom template based on empty contact(no fields)
	CreateCustomTemplateL();
	
	//create contact based on templated
	const TContactItemId &KTemplatedContactId = AddTemplatedContactL(*iCustomTemplate);
	
	Clean();
	
	//read templated contact
	LoadContactL(KTemplatedContactId);
	//check that contact is empty
	INTCOMPARE(iFields->Count(), ==, 0, 0, 0 );
	
	Clean();
	iContactsDatabase->DeleteContactL(iCustomTemplateId);
	iContactsDatabase->DeleteContactL(KTemplatedContactId);
	iCustomTemplateId = -1;
	}

/**
create template with all fields, create contact based on template, check that contact has all fields
*/		
void CCustomTemplateTest::FullTemplateTestL(const TContactItemId aCid)
	{
	LoadContactL(aCid);
	SetContactFieldsL(*iContactRead);
	const TInt KFullContactFieldCount = iFields->Count();
	//create full custom template based on full contact (fully populated list of fields)
	CreateCustomTemplateL();
	
	//create contact based on templated
	const TContactItemId &KTemplatedContactId = AddTemplatedContactL(*iCustomTemplate);
	
	Clean();
	
	//read templated contact
	LoadContactL(KTemplatedContactId);
	
	const TInt KTemplatedContactFieldCount = iFields->Count();
	
	//checks that all fields in contact are empty
	INTCOMPARE(KTemplatedContactFieldCount, ==, KFullContactFieldCount, 0 ,0); 
	//check that all fields in templated contact are empty
	INTCOMPARE(CountEmpty(*iFields), ==, KTemplatedContactFieldCount, 0, 0);
	
	Clean();
	iContactsDatabase->DeleteContactL(KTemplatedContactId);
	iContactsDatabase->DeleteContactL(iCustomTemplateId);
	iCustomTemplateId = -1;	
	}

/**
create template then set all its fields to preset values, create contact based on template
check that all field values, labels, uids match between template and contact
*/		
void CCustomTemplateTest::ModifiedTemplateTestL(const TContactItemId aCid, const TBool aReadMinimal)
	{
	LoadContactL(aCid);
	const TInt KFullContactFieldCount = iFields->Count();
	
	//create custom template based on full contact
	CreateCustomTemplateL();
	//create contact based on custom templated	
	const TContactItemId &KTemplatedContactId = AddTemplatedContactL(*iCustomTemplate);	
	Clean();
	
	//open custom template
	OpenL(iCustomTemplateId);
	//update template fields to contain preset values
	SetContactFieldsL(*iContactOpened);
	CommitL();
	
	//read contact based on custom tempalte, either fully or minimally
	LoadContactL(KTemplatedContactId, aReadMinimal);
	
	const TInt KTemplatedContactFieldCount = iFields->Count();
	if(aReadMinimal)
		{
		//minimal read doesn't merge contact with template, no contact field should be read
		INTCOMPARE( KTemplatedContactFieldCount, ==, 0, 0, 0 );
		INTCOMPARE(CountEmpty(*iFields), ==, 0, 0, 0);
		}
	else
		{
		//full read merges contact with template, all fields should be loaded
		INTCOMPARE(KTemplatedContactFieldCount, ==, KFullContactFieldCount, 0,0 );
		//all fields should be full
		INTCOMPARE(CountEmpty(*iFields), ==, 0, 0, 0);
		//templated contact should match template
		SINGLECOMPARE(CompareToTemplateL( iCustomTemplateId, *iFields), 0 ,0 );
		}
	Clean();
	iContactsDatabase->DeleteContactL(KTemplatedContactId);
	iContactsDatabase->DeleteContactL(iCustomTemplateId);
	iCustomTemplateId = -1;
	}

/**
create template then add 10 new custom fields set all fields to preset values, 
create contact based on template check that all field values, labels, uids 
match between template and contact
*/		
void CCustomTemplateTest::NewFieldsTemplateTestL(const TContactItemId aCid, const TBool aNewAndOldFields)
	{
	LoadContactL(aCid);
	if(!aNewAndOldFields)
		{
		//delete all contacts		
		iFields->Reset();
		}
	//add custom new fields to contact
	for(TInt i = 0, uids = TestConstants::KAltUID; i < 10; i++, uids++)
		{
		AddNewFieldL( *iFields, TestConstants::KShortString, TFieldType::Uid(uids) );
		}
		
	const TInt KBaseContactFieldCount = iFields->Count();	
	//create custom template based on base contact
	CreateCustomTemplateL();
	const TContactItemId &KTemplatedContactId = AddTemplatedContactL(*iCustomTemplate);	
	Clean();
	
	//populate custom template fields
	OpenL(iCustomTemplateId);
	SetContactFieldsL(*iContactOpened);		
	CommitL();
	
	//read templated contact
	LoadContactL(KTemplatedContactId);
	
	//templated contact should contain all fields
	const TInt KTemplatedContactFieldCount = iFields->Count();
	INTCOMPARE(KTemplatedContactFieldCount, ==, KBaseContactFieldCount, 0 ,0 );
	
	//all fields should be populated 
	INTCOMPARE(CountEmpty(*iFields), ==, 0, 0, 0);
	//fields should match template
	SINGLECOMPARE(CompareToTemplateL( iCustomTemplateId, *iFields), 0, 0);
	
	Clean();
	iContactsDatabase->DeleteContactL(KTemplatedContactId);
	iContactsDatabase->DeleteContactL(iCustomTemplateId);
	iCustomTemplateId = -1;
	}
	


/**
create custom template, then create contact based on template. perform dynmaic loading on custom template.
Repeate fro system template
*/	
void CCustomTemplateTest::TestDynamicLoadingL(const TContactItemId aCid)
	{
	_LIT(KStartSystem, "SystemTemplate Dynamic testing");
	_LIT(KStartCustom, "CustomTemplate Dynamic testing");
	
	INFO_PRINTF1(KStartSystem);
	LoadContactL(aCid);
	const TInt KBaseContactFieldCount = iFields->Count();
	//create custom template based on full contact
	CreateCustomTemplateL();	
	//create templated contact
	const TContactItemId &KTemplatedContactId = AddTemplatedContactL(*iCustomTemplate);	
	Clean();
	
	//populate custom template fields
	OpenL(iCustomTemplateId);
	SetContactFieldsL(*iContactOpened);//need to set all fields otherwise field order is not preserved		
	CommitL();
	
	//perform dyamic loading for custom template
	doDynamicLoadingL(KTemplatedContactId, iCustomTemplateId, KBaseContactFieldCount);
	iContactsDatabase->DeleteContactL(iCustomTemplateId);
	iContactsDatabase->DeleteContactL(KTemplatedContactId);
		
	iCustomTemplateId = -1;
	
	INFO_PRINTF1(KStartCustom);
	//open system template
	OpenL(iContactsDatabase->TemplateId());
	//backup template fields
	CContactItemFieldSet * SystemTemplateFieldsetCopy = CopyFieldSetLC(*iFields);
	const TInt KSystemTemplateFieldCount = iFields->Count();
	//add template contact
	const TContactItemId &KSystemTemplatedContactId = AddTemplatedContactL( iContactsDatabase->TemplateId() );
	//populate fields for system template
	SetContactFieldsL(*iContactOpened);//need to set all fields otherwise field order is not preserved	

	CContactItemFieldSet &contactFields = iContactOpened->CardFields();
	const TInt KFieldsCount = contactFields.Count();
	
	// The system template's fields must not contain any data (see CContactDatabase::CommitContactL)
	if (iContactOpened->Id() == iContactsDatabase->TemplateId())
		{	
		for( TInt j = 0; j < KFieldsCount; ++j )
			{
			contactFields[j].ResetStore();
			}
		}
		
	CommitL();
	
	//perfrom dynamic loading for system template
	doDynamicLoadingL(KSystemTemplatedContactId, iContactsDatabase->TemplateId(), KSystemTemplateFieldCount-1, ETrue);
	
	iContactsDatabase->DeleteContactL(KSystemTemplatedContactId);
	
	OpenL(iContactsDatabase->TemplateId());
	iContactOpened->UpdateFieldSet(SystemTemplateFieldsetCopy);//restore system template
	CleanupStack::Pop(SystemTemplateFieldsetCopy);
	CommitL();
	}

/**
delete 1 field from template (aTemplate) and set value for next field. 
reload contact (aCid) based on template, check to see that it matches the updated template.
repeat for all fields in template
*/
void CCustomTemplateTest::doDynamicLoadingL(	const TContactItemId aCid, 
												const TContactItemId aTemplate, 
												const TInt aCount,
												const TBool aSystemTemplate)
	{
	_LIT(KDynamic,"DynamicTestValues");
	CContactItemField* NextField = NULL;
	TInt ContactFieldsCount = 0;  // avoid compiler warning
	TInt PositionOfNextField = 0;  // avoid compiler warning
	const TInt KLastFieldIndex = aSystemTemplate ? 0 : 1;
	TInt initialFieldCount = 70;  // number of fields in the template

	for(TInt i = aCount ; i > KLastFieldIndex; --i)
		{
		//open template
		OpenL(aTemplate);
		
		//the template's name ("Group / Template Label") is not part of the Contact based on the template
		const CContentType& KContentType = (*iFields)[i].ContentType();	
		
		if (KContentType.ContainsFieldType(KUidContactFieldTemplateLabel) && aSystemTemplate)
			{
			CommitL();
			continue;
			}
		
		//remove 1 field
		iFields->Remove(i);
		
		// if this is not the system template
		if (iContactOpened->Id() != iContactsDatabase->TemplateId())
			{
			//set value of next field to dynamic value
			SetFieldL(*iFields, i-1, KDynamic);
			}
		
		//copy next field
		NextField = CContactItemField::NewLC( (*iFields)[i-1]);		
		
		CommitL();
		
		//read templated contact
		LoadContactL(aCid);
		ContactFieldsCount = iFields->Count();
		
		//search for next field in tempalted contact
		PositionOfNextField = FindCtype( NextField->ContentType(), *iFields );
		//check that field count equal initial fieldcount - number of fields removed	
		INTCOMPARE( ContactFieldsCount, ==, ( initialFieldCount - 1 ), i, 0 );
		initialFieldCount--;
		
		//if next field not found
		if(PositionOfNextField == KErrNotFound)
			{
			//if not system template 
			if( !aSystemTemplate) 
				{
				//fail test
				SINGLECOMPARE(EFalse, i, 0);
				}
			}
		//if next field found
		else
			{		 	
			if(!aSystemTemplate)  // the system template fields do not contain data
				{
				SINGLECOMPARE( CompareFieldsL(*iFields, PositionOfNextField, KDynamic), i, 0 );
				}
			}
		CleanupStack::PopAndDestroy(NextField);
		NextField = NULL;
		Clean();
		}
	}

/**
set custom labels and/or uids for template. then create contact based on template. 
check contact matches template. repeate for contact based on system template
*/	
void CCustomTemplateTest::CustomFieldsTemplateTestL(	const TContactItemId aCid, 
														const TBool aSetCustomLabels,
														const TBool aUids)
	{
	LoadContactL(aCid);
	//create custom template based on full contact
	CreateCustomTemplateL();	
	//create templated contact
	const TContactItemId &KTemplatedContactId = AddTemplatedContactL(*iCustomTemplate);	
	Clean();
	
	//open custom template
	OpenL(iCustomTemplateId);
	SetContactFieldsL(*iContactOpened);//need to set all fields otherwise field order is not preserved
	if( aSetCustomLabels )
		{
		//update template field labels
		SetContactLabelsL(*iContactOpened);
		}
	if( aUids )
		{
		//update template field uids
		AddUIDsL(*iFields,TestConstants::KInitialUID);
		}
	CommitL();
	
	//load templated contact
	LoadContactL(KTemplatedContactId);
	//compare fields from template contact to template
	SINGLECOMPARE(CompareToTemplateL( iCustomTemplateId, *iFields),0,0);
	Clean();
	
	iContactsDatabase->DeleteContactL(KTemplatedContactId);
	iContactsDatabase->DeleteContactL(iCustomTemplateId);
	iCustomTemplateId = -1;
	
	//create templated contact based on sytem template
	const TContactItemId &KSystemTemplatedContactId = AddTemplatedContactL( iContactsDatabase->TemplateId() );
	OpenL(iContactsDatabase->TemplateId());
	//backup system template fields
	CContactItemFieldSet *SystemTemplateFieldsetCopy = CopyFieldSetLC(*iFields);
	iFields->Remove( iFields->Find(KUidContactFieldTemplateLabel) );//remove group field
	
	const TInt KFieldsCount = iFields->Count();
	for( TInt j = 0; j < KFieldsCount; ++j )
		{
		(*iFields)[j].ResetStore();
		}
	
	
	if( aSetCustomLabels )
		{
		//update template field labels
		SetContactLabelsL(*iContactOpened);
		}
	if( aUids )
		{
		//update template field uids
		AddUIDsL(*iFields,TestConstants::KInitialUID);
		}
	CommitL();
	
	//load templated contact
	LoadContactL(KSystemTemplatedContactId);
	//compare fields from template contact to system template
	SINGLECOMPARE(CompareToTemplateL( iContactsDatabase->TemplateId(), *iFields, 0, ETrue ),0,0);
	Clean();
	
	iContactsDatabase->DeleteContactL(KSystemTemplatedContactId);
	
	OpenL(iContactsDatabase->TemplateId());
	iContactOpened->UpdateFieldSet(SystemTemplateFieldsetCopy);//restore system template
	CleanupStack::Pop(SystemTemplateFieldsetCopy);
	CommitL();
	}

/**
create custom template, create contact based on template, delete template. reload template,
check to see it is marked as deleted. attempt to read contact again. delete contact and check
whether template is still in the dbase, if so delete template
*/	
void CCustomTemplateTest::DeleteCustomTemplateTestL(const TContactItemId aCid)
	{
	TInt err = KErrNone;
	CContactItem *ContactItem = NULL;
	
	//read contact item		
	LoadContactL(aCid);
	const TInt KContactFieldCount = iFields->Count();
	const TInt KContactHiddenCount = CountHiddenFields(*iFields);
	
	//create custom template based on full contact
	CreateCustomTemplateL();
	//create templated contact	
	const TContactItemId &KTemplatedContactId = AddTemplatedContactL(*iCustomTemplate);
	Clean();
	
	//open custom template
	OpenL(iCustomTemplateId);
	//populate template
	SetContactFieldsL(*iContactOpened);//need to set all fields otherwise field order is not preserved
	
	const TInt KTemplateAccessCount = iContactOpened->AccessCount();
	//check that template is not deleteable as a conatct is based on it
	SINGLECOMPARE(!iContactOpened->IsDeletable(),0,0);
	//access count should equal number of contacts based on template, 1
	INTCOMPARE( KTemplateAccessCount, ==, 1, 0, 0  );		
	CommitL();
	
	//attempt to delete custom template
	iContactsDatabase->DeleteContactL(iCustomTemplateId);
	
	//read deleted template		
	TRAP( err, ContactItem = iContactsDatabase->ReadContactL(iCustomTemplateId,*iViewAll) );
	//template should not have been fully deleted
	INTCOMPARE( err, ==, KErrNone, 0, 0  );
	//template should be marked as deleted
	SINGLECOMPARE(ContactItem->IsDeleted(),0,0);
	CLEAR(ContactItem);
	
	//deleted template with access count > 0 should contain the same number of fields pre as post delete.
	SINGLECOMPARE(CheckDeleteItemL(KTemplatedContactId, KContactFieldCount, KContactHiddenCount),0,0);
	SINGLECOMPARE(CheckDeleteItemL(iCustomTemplateId, KContactFieldCount+1, KContactHiddenCount),0,0);
	
	//it should be possible to read templated contact
	TRAP( err, ContactItem = iContactsDatabase->ReadContactL(KTemplatedContactId,*iViewAll) );
	INTCOMPARE( err, ==, KErrNone,0,0);
	CLEAR(ContactItem);
	
	//it should be possible to delete templated contact
	iContactsDatabase->DeleteContactL(KTemplatedContactId);
	//templated contact should not exist / be deleted
	TRAP( err, ContactItem = iContactsDatabase->ReadContactL(iCustomTemplateId,*iViewAll) );
	INTCOMPARE( err, ==, KErrNotFound,0,0 );
	CLEAR(ContactItem);
	
	iCustomTemplateId = -1;
	}

/**
attempt to delete system template, check that it fails and doesnt affect any contacts based on it.
*/
void CCustomTemplateTest::DeleteSystemTemplateTestL(const TContactItemId aCid)
	{
	TInt err = KErrNone;
	CContactItem *ContactItem = NULL;
	const TContactItemId &KSystemTemplateId = iContactsDatabase->TemplateId();
	LoadContactL(aCid);
	const TInt KContactFieldCount = iFields->Count();
	Clean();
	//attempt to delete system template
	TRAP( err, iContactsDatabase->DeleteContactL( KSystemTemplateId ) );
	//it should not be possible
	INTCOMPARE(KErrNotSupported, ==, err,0,0);
	
	//attempt to read system template
	TRAP( err, ContactItem = iContactsDatabase->ReadContactL( KSystemTemplateId ,*iViewAll) );
	CLEAR(ContactItem);
	//template should not have been deleted
	INTCOMPARE( KErrNone, ==, err,0,0 );
	
	//read system template
	LoadContactL(aCid);
	const TInt KDeletedContactFieldCount = iFields->Count();
	Clean();
	//number of fields should be unchanged
	INTCOMPARE( KDeletedContactFieldCount, ==, KContactFieldCount,0,0 );
	}

//deleted template with access count > 0 should contain the same number of fields pre as post delete.
TBool CCustomTemplateTest::CheckDeleteItemL(const TContactItemId aCid,
											const TInt aFieldCountPreDelete, 
											const TInt aHiddenCountPreDelete )
	{
	TBool ret = EFalse;
	
	LoadContactL(aCid);
	const TInt KFieldCountPostDelete = iFields->Count();
	const TInt KHiddenFieldCountPostDelete = CountHiddenFields(*iFields);
	const TInt KEmptyFieldCountPostDelete = CountEmpty(*iFields);
	ret = (KFieldCountPostDelete == aFieldCountPreDelete);
	ret = ret && ( KEmptyFieldCountPostDelete == 0 ) && ( KHiddenFieldCountPostDelete == aHiddenCountPreDelete);
	
	Clean();
	
	return ret;
	}
	
void CCustomTemplateTest::CreateCustomTemplateL()
	{
	_LIT(KTLabel,"UserDefined template");
	iCustomTemplate = iContactsDatabase->CreateContactCardTemplateL(iContactRead,KTLabel);
	iCustomTemplateId = iCustomTemplate->Id();
	}
	
void CCustomTemplateTest::LoadContactL(const TContactItemId aCid, const TBool aReadMinimal)
	{
	if(aReadMinimal)
		{
		iContactRead = iContactsDatabase->ReadMinimalContactL(aCid);
		}
	else
		{
		iContactRead = iContactsDatabase->ReadContactL(aCid,*iViewAll);
		}
	iFields = &(iContactRead->CardFields());
	}
	
void CCustomTemplateTest::OpenL(const TContactItemId aCid)
	{
	iContactOpened = iContactsDatabase->OpenContactL(aCid,*iViewAll);
	iFields = &(iContactOpened->CardFields());
	}
	
void CCustomTemplateTest::CommitL()
	{
	if(NULL == iContactOpened)
		{
		return;
		}
	iContactsDatabase->CommitContactL(*iContactOpened);
	CLEAR(iContactOpened);
	iFields = NULL;
	}
	
TContactItemId CCustomTemplateTest::AddTemplatedContactL(const CContactItem &aTemplate)
	{
	CContactCard* contact = CContactCard::NewLC(&aTemplate);
	const TContactItemId &cid = iContactsDatabase->AddNewContactL(*contact);
	CleanupStack::PopAndDestroy(contact);
	return cid;
	}

TContactItemId CCustomTemplateTest::AddTemplatedContactL(const TContactItemId aTemplate)
	{
	CContactItem *ctemplate = iContactsDatabase->ReadContactLC(aTemplate,*iViewAll);
	const TContactItemId &cid = AddTemplatedContactL(*ctemplate);
	CleanupStack::PopAndDestroy(ctemplate);
	return cid;
	}
	
void CCustomTemplateTest::Clean()
	{
	delete iCustomTemplate;
	iCustomTemplate = NULL;
	CLEAR(iContactRead);
	iFields = NULL;
	}

TBool CCustomTemplateTest::CompareToTemplateL( 	const TContactItemId aCid, 
												const CContactItemFieldSet &aFields,
												const TInt aStart,
												const TBool aNogroup )
	{
	_LIT(KGroupField, "Group / Template Label");
	TBool ret = EFalse;
	const CContactItem *citem = iContactsDatabase->ReadContactLC(aCid,*iViewAll);
	const CContactItemFieldSet &cfields = citem->CardFields();
	const TInt count = cfields.Count();

	TInt fPos = -1;
	TInt i = aStart;//ignore first field in template as that is template 
					//related custom field (access count?)
		
	for(; i < count; ++i)
		{
		if( aNogroup && ( cfields[i].Label() ).Compare(KGroupField) == 0)//ignore group field
			{
			continue;
			}
		else if( ( fPos = FindCtype( cfields[i].ContentType(), aFields ) ) == KErrNotFound )
			{
			break;
			}
		else if( ( aFields[fPos].Label() ).Compare( cfields[i].Label() ) != 0 )
			{
			break;
			}
		// the system template fields should contain no data (i.e. don't test for this)	
		}
		
	if( count == i )
		{
		ret = ETrue;
		}
	CleanupStack::PopAndDestroy( CONST_CAST(CContactItem *, citem) );
	return ret;
	}

TInt CCustomTemplateTest::FindCtype( const CContentType &aCtype, const CContactItemFieldSet &aFields )
	{
	TInt ret = KErrNotFound;
	const TInt count = aFields.Count();
	for(TInt i = 0; i < count; ++i)
		{
		if( aCtype == aFields[i].ContentType() )
			{
			return i;
			}
		}
	return ret;
	}

CContactItemFieldSet *CCustomTemplateTest::CopyFieldSetLC(const CContactItemFieldSet &aFields)
	{
	const TInt count = aFields.Count();
	CContactItemFieldSet *FieldSetCopy = CContactItemFieldSet::NewLC();
	FieldSetCopy->Reset();//make sure its empty
	CContactItemField *field = NULL;
	
	for( TInt i = 0; i < count; ++i )
		{
		field = CContactItemField::NewLC(aFields[i]);
		FieldSetCopy->AddL(*field);
		CleanupStack::Pop(field);
		field = NULL;
		}
	return FieldSetCopy;
	}

TBool CCustomTemplateTest::CompareFields(const CContactItemField &aField1, const CContactItemField &aField2) const
	{
	TBool ret = EFalse;
	if(  aField1.IsTemplateLabelField() || aField2.IsTemplateLabelField() )
		{
		return ret;
		}
	switch( aField1.StorageType() )
		{
		case KStorageTypeText:
			ret = (  (aField1.TextStorage()->Text()).Compare(aField2.TextStorage()->Text()) ) == 0;
		break;
		case KStorageTypeStore:
			ret = ( ( aField1.StoreStorage()->Thing() )->Compare( *(aField2.StoreStorage()->Thing()) ) == 0 );
		break;
		case KStorageTypeContactItemId:
			ret = aField1.AgentStorage()->Value() == aField2.AgentStorage()->Value();
		break;
		case KStorageTypeDateTime:
			ret = aField1.DateTimeStorage()->Time() == aField2.DateTimeStorage()->Time();
		break;
		default:
			{
			_LIT(KCompareFields,"comparefields panic");
			User::Panic(KCompareFields,EInvariantFalse);
			}
		break;
		}
	return ret;
	
	}