summaryrefslogtreecommitdiffstats
path: root/plugins/contacts/symbian/contactsmodel/tsrc/t_iccentry.cpp
blob: b8a6cf44a48d18b4718cbfebce6b7a5a7195c19b (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
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
/*
* Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
* Contact: http://www.qt-project.org/legal
* 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: 
*
*/


#include <e32test.h>
#include <cntdb.h>
#include <cntitem.h>
#include <cntfield.h>
#include <cntfldst.h>
#include <phbksync.h>

#include "t_utils.h"
#include "./testsyncplugin/cnttestsynchroniser.h"

// include templates for CleanupResetAndDestroyPushL(T)
#include "cntviewprivate.h"


/** 
 * @file
 * Test harness for CContactICCEntry and MContactSynchroniser interface.
 * todo Find out why telephone field won't match template correctly 
 *       (Maybe the ICC slot field needs to be added to the "golden" template)
 *
 */

_LIT(KDatabaseFileName,"C:T_ICCENTRY");

_LIT(KTestName,"Basic tests for CContactICCEntry");
_LIT(KGivenName,"Given");
_LIT(KGivenEditName,"EditedName");
_LIT(KTelephoneNum,"020 11111111");
_LIT(KTelephoneNumModified,"020 22222222");
_LIT(KTemplateName,"SIM Card Contacts"); 

_LIT(KPluginName,"phone book synchronizer Implementation");
_LIT(KTestPluginName,"Test phone book synchronizer Implementation");

CCntTest* CntTest=NULL;
LOCAL_D RTest test(KTestName);

LOCAL_D CContactTestSynchroniser* synchroniser;
LOCAL_D CContactSyncChecker* syncChecker;

LOCAL_C TInt findWordSplitterL(TAny *aParams)
	{
	SFindInTextDefWordParser *parser=(SFindInTextDefWordParser *)aParams;
	parser->iWordArray->AppendL(*parser->iSearchString);
	return(KErrNone);
	}

CContactIdArray* TestFindL(CContactDatabase& aDatabase, const TDesC& aText, const CContactItemFieldDef *aFieldDef, RTest& aTest)
	{
	CContactIdArray* retval = NULL;
	
	retval = aDatabase.FindLC(aText, aFieldDef);
	aTest(retval == NULL);
	CleanupStack::Pop( retval );
	
	return retval;
	}
	
CContactIdArray* TestFindInTextDefL(CContactDatabase& aDatabase, const MDesCArray& aFindWords,CContactTextDef* aTextDef, const TCallBack &aWordParserCallback, RTest& aTest)
	{
	CContactIdArray* retval = NULL;
	retval = aDatabase.FindInTextDefLC(aFindWords, aTextDef, aWordParserCallback);
	aTest(retval==NULL);
	CleanupStack::Pop( retval );
	return retval;
	}
		
/** 
 * Test Finding ICC Contacts when the ICC is locked.
 * @param aDb Contact database
 */

void TestFindingL(CContactDatabase& aDb)
	{
	test.Next(_L("Test FindLC"));

	syncChecker->SetValidateResponseL(MContactSynchroniser::ERead,KErrLocked);
	syncChecker->SetValidateResponseL(MContactSynchroniser::ESearch,KErrLocked);
	
	CContactItemFieldDef* fieldDef=new(ELeave) CContactItemFieldDef;
	CleanupStack::PushL(fieldDef);
	fieldDef->AppendL(KUidContactFieldFamilyName); 
	
	
	TRAPD(err,TestFindL(aDb, KGivenEditName, fieldDef, test) );
	test(err==KErrLocked);
	CleanupStack::PopAndDestroy(fieldDef);

	CContactItemFieldDef* fieldDef2=new(ELeave) CContactItemFieldDef;
	CleanupStack::PushL(fieldDef2);
	fieldDef2->AppendL(KUidContactFieldPhoneNumber); 
	TRAP(err, TestFindL(aDb, KTelephoneNum, fieldDef2, test) );
	test(err==KErrLocked);
	CleanupStack::PopAndDestroy(fieldDef2);

	test.Next(_L("Test FindInTextDefL"));

	TCallBack callBack(findWordSplitterL);
	CContactTextDef* textDef=CContactTextDef::NewLC();
	textDef->AppendL(TContactTextDefItem(KUidContactFieldFamilyName));
	CDesCArray* desArray = new(ELeave) CDesCArrayFlat(5);
	desArray->AppendL(_L("e"));
	
	CContactIdArray* arr = NULL;
	TRAP(err,arr = TestFindInTextDefL(aDb,*desArray,textDef, callBack, test) );
	
//	test(err==KErrLocked);
	desArray->Delete(0);
	delete desArray;
//FIX
	delete arr;	
	CleanupStack::PopAndDestroy(textDef); 

	test.Next(_L("Test FindInTextDefL2"));

	TCallBack callBack2(findWordSplitterL);
	CContactTextDef* textDef2=CContactTextDef::NewLC();
	textDef2->AppendL(TContactTextDefItem(KUidContactFieldFamilyName));
	textDef2->AppendL(TContactTextDefItem(KUidContactFieldUrl));
	CDesCArray* desArray2 = new(ELeave) CDesCArrayFlat(5);
	desArray2->AppendL(_L("e"));
	
	TRAP(err,arr = TestFindInTextDefL(aDb,*desArray2,textDef2, callBack2, test) );
//	test(err==KErrLocked);
	desArray2->Delete(0);
	delete desArray2;
//FIX
	delete arr;	
	CleanupStack::PopAndDestroy(textDef2);
	}


/** 
 * Test ICC contacts can be matched using MatchPhoneNumberL API. The 
 * synchroniser is not called to validate the IDs at for this method because 
 * phone number resolution needs to be as quick as possible. 
 * Instead, validation is done when the client attempts to read the items.
 * @param aDb Contact database
 */
 

void TestPhoneMatchingL(CContactDatabase& aDb)
	{
	test.Next(_L("Test phone match"));

	CContactIdArray* array = aDb.MatchPhoneNumberL(KTelephoneNum,KMaxPhoneMatchLength);	
	test(array->Count() == 4);
	delete array;

	syncChecker->SetValidateResponseL(MContactSynchroniser::ERead,KErrNone);
	syncChecker->SetValidateResponseL(MContactSynchroniser::EEdit, KErrNone);
	syncChecker->SetValidateWriteResponseL(KErrNone);
	aDb.SetDbViewContactType(KUidContactICCEntry);
	TContactItemId id = (*aDb.SortedItemsL())[0];
	CContactItem* item = aDb.OpenContactLX(id);
	CleanupStack::PushL(item);
	CContactItemFieldSet& fieldset = item->CardFields();
	const TInt pos = fieldset.Find(KUidContactFieldPhoneNumber);	
	CContactItemField& field = fieldset[pos];
	CContactTextField* textfield = field.TextStorage();
	textfield->SetTextL(KTelephoneNumModified);
	aDb.CommitContactL(*item);
	CleanupStack::PopAndDestroy(2); //item, lock record

	CContactIdArray* array2 = aDb.MatchPhoneNumberL(KTelephoneNumModified,KMaxPhoneMatchLength);	
	test(array2->Count() == 1);
	delete array2;
	}


/** 
 * Test database iteration using TContactIter.
 * @param aDb Contact database
 */
 

void TestDatabaseIterationL(CContactDatabase& aDb)
	{
	test.Next(_L("Test database iteration"));

	aDb.SetDbViewContactType(KUidContactICCEntry);
	TContactIter iterator(aDb);
	TContactItemId id = iterator.FirstL();
	while (id!=KNullContactId)
		{
		CContactItem* item = aDb.ReadContactLC(id);
		test(item->Type() == KUidContactICCEntry);
		CleanupStack::PopAndDestroy(item);
		id = iterator.NextL();
		}
	}



CContactItem* ReadItemL( CContactDatabase& aDatabase, TContactItemId aId )
	{
	CContactItem* retval = NULL;
	retval = aDatabase.ReadMinimalContactLC( aId );
	CleanupStack::Pop( retval );
	return retval;
	}

/** 
 * Verify that the plug-in implementation is called when the 
 * ReadMinimalContactL API is used. 
 * 
 * Tests methods:
 * IMPORT_C CContactItem* ReadMinimalContactL(TContactItemId aContactId);
 * IMPORT_C CContactItem* ReadMinimalContactLC(TContactItemId aContactId);
 * 
 * @param aDb Contact database
 * @param aId Contact item ID to read
 */
void TestReadMinimalContactL(CContactDatabase& aDb, TContactItemId aId)
	{
	syncChecker->SetValidateResponseL(MContactSynchroniser::ERead,KErrNone);
	syncChecker->ResetMethodCallCountsL();
	test(syncChecker->ValidateMethodCallCountL() == 0);	
	CContactItem* item = aDb.ReadMinimalContactLC(aId);
	test(syncChecker->ValidateMethodCallCountL() == 1);	
	CleanupStack::PopAndDestroy(item);

	syncChecker->SetValidateResponseL(MContactSynchroniser::ERead,KErrAccessDenied);
	syncChecker->ResetMethodCallCountsL();
	test(syncChecker->ValidateMethodCallCountL() == 0);
	item=NULL;
	//TRAPD(err, item = aDb.ReadMinimalContactLC(aId));
	TRAPD( err, item = ReadItemL( aDb, aId ) );
	delete item;
	test(err==KErrAccessDenied);
	test(syncChecker->ValidateMethodCallCountL() == 1);	
	}

/** 
 * Verify that the plug-in implementation is called when the 
 * ReadContactL API is used. 
 * 
 * Tests code called by methods:
 * IMPORT_C CContactItem* ReadContactL(TContactItemId aContactId);
 * IMPORT_C CArrayPtr<CContactItem>* ReadContactAndAgentL(TContactItemId aContactId);
 * IMPORT_C CContactItem* ReadContactL(TContactItemId aContactId,const CContactItemViewDef& aViewDef);
 * IMPORT_C CContactItem* ReadContactLC(TContactItemId aContactId);
 * IMPORT_C CContactItem* ReadContactLC(TContactItemId aContactId,const CContactItemViewDef& aViewDef);
 * 
 * @param aDb Contact database
 * @param aId Contact item ID to read
 */
void TestReadContactL(CContactDatabase& aDb, TContactItemId aId)
	{
	syncChecker->SetValidateResponseL(MContactSynchroniser::ERead,KErrNone);
	syncChecker->ResetMethodCallCountsL();
	test(syncChecker->ValidateMethodCallCountL() == 0);	
	CContactItem* item = aDb.ReadContactLC(aId);
	test(syncChecker->ValidateMethodCallCountL() == 1);	
	CleanupStack::PopAndDestroy(item);

	syncChecker->SetValidateResponseL(MContactSynchroniser::ERead,KErrAccessDenied);
	syncChecker->ResetMethodCallCountsL();
	test(syncChecker->ValidateMethodCallCountL() == 0);
	item=NULL;
	TRAPD(err, item = aDb.ReadContactL(aId));
	delete item;
	test(err==KErrAccessDenied);
	test(syncChecker->ValidateMethodCallCountL() == 1);	
	}

/** 
 * Verify that read validation is done for ReadContactTextDefL
 * 
 * @param aDb Contact database
 * @param aId Contact item ID to read
 */
void TestReadTextDefinitionL(CContactDatabase& aDb, TContactItemId aId)
	{
	syncChecker->SetValidateResponseL(MContactSynchroniser::ERead,KErrNone);
	syncChecker->ResetMethodCallCountsL();
	test(syncChecker->ValidateMethodCallCountL() == 0);
	TBuf<64> textDef;
	aDb.ReadContactTextDefL(aId,textDef);
	test(syncChecker->ValidateMethodCallCountL() == 1);	

	syncChecker->SetValidateResponseL(MContactSynchroniser::ERead,KErrAccessDenied);
	syncChecker->ResetMethodCallCountsL();
	test(syncChecker->ValidateMethodCallCountL() == 0);
	TRAPD(err, aDb.ReadContactTextDefL(aId,textDef));
	test(err==KErrAccessDenied);
	test(syncChecker->ValidateMethodCallCountL() == 1);	
	}

/** 
 * Create example ICC Entry 
 * @param aDb Contact database
 */
TContactItemId CreateTestICCEntryL(CContactDatabase& aDb)
	{
	TContactItemId templateId = aDb.ICCTemplateIdL(KUidIccGlobalAdnPhonebook);
	CContactItem* iccTemplate = aDb.ReadContactLC(templateId);
	CContactICCEntry* entry = CContactICCEntry::NewL(*iccTemplate);
	CleanupStack::PopAndDestroy(iccTemplate);
	CleanupStack::PushL(entry);
	SetNameL(*entry,KUidContactFieldFamilyName,KUidContactFieldVCardMapUnusedN,KGivenName,EFalse);
	SetNameL(*entry,KUidContactFieldPhoneNumber,KUidContactFieldVCardMapTEL,KTelephoneNum,EFalse);

	//Add to database
	syncChecker->SetValidateWriteResponseL(KErrNone);
	syncChecker->SetValidateResponseL(MContactSynchroniser::ERead,KErrNone);
	syncChecker->SetDeleteContactResponseL(KErrNone);

	TContactItemId id = aDb.AddNewContactL(*entry); 

	CleanupStack::PopAndDestroy(entry);	
	test(id!=KNullContactId);
	return id;
	}

/**
 * Verify Read validation is being done for reading items.
 * @param aDb Contact database
 */

void TestReadL(CContactDatabase& aDb)
	{
	TContactItemId id = CreateTestICCEntryL(aDb);
	test.Next(_L("TestReadMinimalContactL"));

	TestReadMinimalContactL(aDb,id);
	test.Next(_L("TestReadContactL"));

	TestReadContactL(aDb,id);
	test.Next(_L("TestReadTextDefinitionL"));

	TestReadTextDefinitionL(aDb,id);
	}

/**
 * Verify that edit validation is being done for ICC entries
 * and write validation is done when Commit is called.
 * @param aDb Contact database
 * @param aId Item Id to edit
 */
void TestSuccessfulEditL(CContactDatabase& aDb, TContactItemId aId)
	{
	syncChecker->SetValidateResponseL(MContactSynchroniser::ERead,KErrNone);
	syncChecker->SetValidateResponseL(MContactSynchroniser::EEdit, KErrNone);
	syncChecker->SetValidateWriteResponseL(KErrNone);
	syncChecker->ResetMethodCallCountsL();
	test(syncChecker->ValidateMethodCallCountL() == 0);
	CContactItem* item = aDb.OpenContactLX(aId);
	CleanupStack::PushL(item);
	test(syncChecker->ValidateMethodCallCountL() == 2);	

	CContactItemFieldSet& fieldset = item->CardFields();
	const TInt pos = fieldset.Find(KUidContactFieldFamilyName);	
	CContactItemField& field = fieldset[pos];
	CContactTextField* textfield = field.TextStorage();
	textfield->SetTextL(KGivenEditName);

	aDb.CommitContactL(*item);
	test(syncChecker->ValidateMethodCallCountL() == 4);	
	CleanupStack::PopAndDestroy(2); //item, lock record
	}

/**
 * Unsuccessful edit. Fail the edit validation request
 * @param aDb Contact database
 * @param aId Item Id to edit
 */
void FailEditValidationL(CContactDatabase& aDb, TContactItemId aId)
	{
	syncChecker->SetValidateResponseL(MContactSynchroniser::ERead,KErrNone);
	syncChecker->SetValidateResponseL(MContactSynchroniser::EEdit, KErrAccessDenied);
	syncChecker->ResetMethodCallCountsL();
	test(syncChecker->ValidateMethodCallCountL() == 0);
	TRAPD(err, aDb.OpenContactL(aId));
	test(syncChecker->ValidateMethodCallCountL() == 2);	
	test(err==KErrAccessDenied);
	}

/**
 * Unsuccessful edit. Fail the write validation request
 * @param aDb Contact database
 * @param aId Item Id to edit
 */
void FailWriteValidationL(CContactDatabase& aDb, TContactItemId aId)
	{
	syncChecker->SetValidateResponseL(MContactSynchroniser::ERead,KErrNone);
	syncChecker->SetValidateResponseL(MContactSynchroniser::EEdit, KErrNone);
	syncChecker->SetValidateWriteResponseL(KErrAccessDenied);
	syncChecker->ResetMethodCallCountsL();

	CContactItem* item = aDb.OpenContactLX(aId);
	CleanupStack::PushL(item);
	CContactItemFieldSet& fieldset = item->CardFields();
	const TInt pos = fieldset.Find(KUidContactFieldFamilyName);	
	CContactItemField& field = fieldset[pos];
	CContactTextField* textfield = field.TextStorage();
	textfield->SetTextL(KGivenEditName);

	TRAPD(err, aDb.CommitContactL(*item));
	test(syncChecker->ValidateMethodCallCountL() == 4);	
	test(err==KErrAccessDenied);

	CleanupStack::PopAndDestroy(2); //item, lock record
	}

/**
 * Verify that edit validation is being done for ICC entries
 * and write validation is done when Commit is called.
 * @param aDb Contact database
 */

void TestEditL(CContactDatabase& aDb)
	{
	TContactItemId id = CreateTestICCEntryL(aDb);
	test.Next(_L("Test successful edit"));

	TestSuccessfulEditL(aDb,id);
	test.Next(_L("Test unsuccessful edit - fail edit"));

	FailEditValidationL(aDb,id);
	test.Next(_L("Test unsuccessful edit - fail write"));

	FailWriteValidationL(aDb,id);
	}

/** 
 * Test an unsuccessful delete
 * @param aDb Contact database
 */
void TestUnsuccessfulDeleteL(CContactDatabase& aDb)
	{
	syncChecker->SetValidateResponseL(MContactSynchroniser::ERead,KErrNone);
	syncChecker->SetValidateResponseL(MContactSynchroniser::EEdit, KErrNone);
	TContactItemId id = CreateTestICCEntryL(aDb);
	// Creating the contact sets the delete error to KErrNone, so must do this afterwards
	syncChecker->SetDeleteContactResponseL(KErrCorrupt);
	syncChecker->ResetMethodCallCountsL();
	TRAPD(err, aDb.DeleteContactL(id));
	test(syncChecker->ValidateMethodCallCountL() == 2);
	test(err==KErrCorrupt);
	//Read contact to check it wasn't deleted anyway
	CContactItem* item = NULL;
	TRAP(err,item = aDb.ReadContactL(id));
	test(err==KErrNone);
	delete item;
	}


/** 
 * Verify that delete validation is being done for ICC entries.
 * @param aDb Contact database
 */

void TestDeleteL(CContactDatabase& aDb)
	{
	test.Next(_L("Test successful delete"));

	TContactItemId id = CreateTestICCEntryL(aDb);
	syncChecker->ResetMethodCallCountsL();
	aDb.DeleteContactL(id);
	test(syncChecker->ValidateMethodCallCountL() == 2);
	test.Next(_L("Test unsuccessful delete"));

	TestUnsuccessfulDeleteL(aDb);
	}


/** 
 * Create template card for ICC contact items 
 * @param aDb Contact database
 */
TContactItemId CreateICCTemplateL(CContactDatabase& aDb)
	{
	CContactItem* temp = aDb.CreateContactCardTemplateLC(KTemplateName);
	TContactItemId templateId = temp->Id();
	CleanupStack::PopAndDestroy(temp);
	temp=NULL;

	//Remove all the unnecessary fields
	temp = aDb.OpenContactLX(templateId);
	CleanupStack::PushL(temp);	
	const TInt fieldCount = temp->CardFields().Count();
	for(TInt i=fieldCount-1;i>=0;i--)
		temp->RemoveField(i);
		
	CContactItemField* name = CContactItemField::NewLC(KStorageTypeText,KUidContactFieldFamilyName);
	name->SetMapping(KUidContactFieldVCardMapUnusedN);
	temp->AddFieldL(*name);
	CleanupStack::Pop(name);

	CContactItemField* number = CContactItemField::NewLC(KStorageTypeText,KUidContactFieldPhoneNumber);
	number->SetMapping(KUidContactFieldVCardMapTEL);
	temp->AddFieldL(*number);
	CleanupStack::Pop(number);
				
	CContactItemField* slotnum = CContactItemField::NewLC(KStorageTypeText,KUidContactFieldICCSlot);
	temp->AddFieldL(*slotnum);
	CleanupStack::Pop(slotnum);

	CContactItemField* phonebook = CContactItemField::NewLC(KStorageTypeText,KUidContactFieldICCPhonebook);
	temp->AddFieldL(*phonebook);
	CleanupStack::Pop(phonebook);

	aDb.CommitContactL(*temp);
	CleanupStack::PopAndDestroy(2);	// temp, close template	
	
	return templateId;
	}

/**
 * Test SetDbViewContactType can be used for KUidContactICCEntry items.
 * @param aDb Contact database
 */
void TestDatabaseViewL(CContactDatabase& /*aDb*/)
	{
	syncChecker->SetValidateResponseL(MContactSynchroniser::ERead,KErrNone);
	syncChecker->SetValidateWriteResponseL(KErrNone);
	CntTest->CloseDatabase();
	CntTest->DeleteDatabaseL();
	// re-initialise the TLS
	syncChecker->EnableVerboseLoggingL();
	
	CntTest->CreateDatabaseL();	
	CntTest->OpenDatabaseL();
	CContactDatabase& db= *CntTest->Db();   
	
	delete synchroniser;
	synchroniser = CContactTestSynchroniser::NewL();
	// test with GSM ADN phonebook only
	//test(KErrNone == syncChecker->SetGsmPhonebook());

	// Start from clean
	CContactItem* card = CContactCard::NewLC();
	db.AddNewContactL(*card);
	db.CreateContactGroupLC();
	db.CreateOwnCardLC();
	TContactItemId id=CreateICCTemplateL(db);
	test(KErrNone == syncChecker->UpdatePhonebookEntryL(KUidIccGlobalAdnPhonebook, id, KNullContactId));
	TContactItemId templateId = db.ICCTemplateIdL(KUidIccGlobalAdnPhonebook);
	CContactItem* iccTemplate = db.ReadContactLC(templateId);
	CContactICCEntry* iccentry = CContactICCEntry::NewL(*iccTemplate);
	CleanupStack::PopAndDestroy(iccTemplate);
	CleanupStack::PushL(iccentry);
	db.AddNewContactL(*iccentry);
	TContactItemId iccId = iccentry->Id();
	CleanupStack::PopAndDestroy(4); //card,group,owncard,iccentry
	
	test(db.SortedItemsL()->Count() == 5); //default view includes ALL contact types
	CntTest->CloseDatabase();
	delete synchroniser;
	synchroniser = NULL;
	CntTest->OpenDatabaseL();
	CContactDatabase& db1= *CntTest->Db();
	db1.SetDbViewContactType(KUidContactICCEntry);
    TInt some = db1.SortedItemsL()->Count();
	test(db1.SortedItemsL()->Count() == 1);
	test((*db1.SortedItemsL())[0]==iccId);
	}

/** 
 * Create phonebook group 
 * @param aDb Contact database
 */
TContactItemId CreatePhonebookGroupL(CContactDatabase& aDb)
	{
	_LIT(KPhbkGroup,"ADN Phonebook"); 
	CContactItem* group = aDb.CreateContactGroupLC(KPhbkGroup);
	TContactItemId groupId = group->Id();
	CleanupStack::PopAndDestroy(group);	
	return groupId;
	}

/** 
 * Verify that the template was correctly added 
 * @param aTemplate CContactCardTemplate template item
 */
void CheckTemplateL(CContactItem& aTemplate)
	{
	test(aTemplate.Type() == KUidContactCardTemplate);
	test(aTemplate.TemplateRefId() == KNullContactId);
	test(static_cast<CContactCardTemplate&>(aTemplate).GetTemplateLabelL() == KTemplateName);
	CContactItemFieldSet& fieldset = aTemplate.CardFields();
	TInt pos = fieldset.Find(KUidContactFieldTemplateLabel);
	test(pos!=KErrNotFound);
	TInt count = fieldset.Count();
	test(count==5); //label, name, number, ICC slot, phonebook
	}


/** 
 * Check that the field of type aFieldType has the same content as 
 * aExpectedContent
 * @param aFieldset Contact item fieldset
 * @param aFieldType Field type to test
 * @param aExpectedContent Expected data
 */
void CheckFieldContentL(CContactItemFieldSet& aFieldset, TFieldType aFieldType, const TDesC& aExpectedContent)
	{
	TInt pos = aFieldset.Find(aFieldType);
	CContactItemField& field = aFieldset[pos];
	CContactTextField* textField = field.TextStorage();
	test(textField != NULL);
	test(textField->Text() == aExpectedContent);
	}

/** 
 * Test creation of CContactICCEntry items.
 * 
 * Set-up appropriate template and group ready...
 * @param aDb Contact database
 */

/**

@SYMTestCaseID     PIM-T-ICCENTRY-0001

*/

void TestCreationL(CContactDatabase& aDb)
	{
	_LIT(KTestCreation,"Create CContactICCEntry item");
	test.Start(_L("@SYMTESTCaseID:PIM-T-ICCENTRY-0001 Create CContactICCEntry item"));

	//System Template
	TContactItemId systemTemplateId = aDb.TemplateId();
	CContactItem* systemTemplate = aDb.ReadContactLC(systemTemplateId);
	CContactICCEntry* entry = CContactICCEntry::NewL(*systemTemplate);
	CleanupStack::PopAndDestroy(systemTemplate);
	CleanupStack::PushL(entry);
	
	//Test CContactICCEntry items can be identified from normal contact cards
	test(entry->Type() == KUidContactICCEntry);
	test(entry->TemplateRefId() == systemTemplateId);
	CleanupStack::PopAndDestroy(entry);

	//Create user defined template for ICC contacts
	test.Next(_L("CContactDatabase::ICCTemplateL()"));

	TContactItemId templateId = CreateICCTemplateL(aDb);
	test(KErrNone == syncChecker->UpdatePhonebookEntryL(KUidIccGlobalAdnPhonebook, templateId, KNullContactId));

	//Check that contacts model calls the plug-in when asked for the
	//template ID.
	TInt callCount = syncChecker->ValidateMethodCallCountL();
	test(callCount==0);
	TContactItemId templateIdFromCntmodel;
	templateIdFromCntmodel = aDb.ICCTemplateIdL(KUidIccGlobalAdnPhonebook);

	test(syncChecker->ValidateMethodCallCountL() == 1);
	test(templateId == templateIdFromCntmodel);
	
	CContactItem* iccTemplate = aDb.ReadContactLC(templateId);
	CheckTemplateL(*iccTemplate);
	//create ICC contact
	CContactICCEntry* iccentry = CContactICCEntry::NewL(*iccTemplate);
	CleanupStack::PopAndDestroy(iccTemplate);
	CleanupStack::PushL(iccentry);
	test(iccentry->Type() == KUidContactICCEntry);
	test(iccentry->TemplateRefId() == templateId);
	CleanupStack::PopAndDestroy(iccentry);
	
	test.End();
	}



/** 
 * Test successful addition to database.
 * @param aDb Contact database
 */
void TestSuccessfulAddL(CContactDatabase& aDb)
	{
	//Create group 
	TContactItemId groupId = CreatePhonebookGroupL(aDb);
	test(KErrNone == syncChecker->UpdatePhonebookGroupIdL(KUidIccGlobalAdnPhonebook, groupId));
	//Create item and add fields
	TContactItemId templateId = aDb.ICCTemplateIdL(KUidIccGlobalAdnPhonebook);
	CContactItem* iccTemplate = aDb.ReadContactLC(templateId);
	CContactICCEntry* entry = CContactICCEntry::NewL(*iccTemplate);
	CleanupStack::PopAndDestroy(iccTemplate);
	CleanupStack::PushL(entry);
	SetNameL(*entry,KUidContactFieldFamilyName,KUidContactFieldVCardMapUnusedN,KGivenName,EFalse);
	SetNameL(*entry,KUidContactFieldPhoneNumber,KUidContactFieldVCardMapTEL,KTelephoneNum,EFalse);

	//Add to database
	syncChecker->ResetMethodCallCountsL();
	syncChecker->SetValidateWriteResponseL(KErrNone);
	syncChecker->SetValidateResponseL(MContactSynchroniser::ERead,KErrNone);

	test(syncChecker->ValidateMethodCallCountL() == 0);
	TContactItemId id = aDb.AddNewContactL(*entry); 
	CleanupStack::PopAndDestroy(entry);	
	test(syncChecker->ValidateMethodCallCountL() == 3);
	test(id!=KNullContactId);

	CContactICCEntry* fetchedItem = static_cast<CContactICCEntry*>(aDb.ReadContactL(id));
	CleanupStack::PushL(fetchedItem);
	//Check group membership	
	const CContactIdArray* owned = fetchedItem->GroupsJoined();
	test(owned!=NULL && owned->Count() == 1);
	test((*owned)[0]==groupId);
	
	//Check number of fields and content
	
	CContactItemFieldSet& fieldset = fetchedItem->CardFields();
	TInt pos = fieldset.Find(KUidContactFieldTemplateLabel);
	test(pos==KErrNotFound);
	
//	test(fieldset.Count() == 3); 
	
/*	for (TInt i=0; i<fieldset.Count(); i++)
		{
		CContactItemField& field = fieldset[i];
		TInt count = field.ContentType().FieldTypeCount();
		for (TInt j=0; j<count; j++)
			{
			TFieldType ft= field.ContentType().FieldType(j);
			}


		TUid mapping = field.ContentType().Mapping();
		}
*/	
	CheckFieldContentL(fieldset,KUidContactFieldFamilyName,KGivenName);
//	CheckFieldContentL(fieldset,KUidContactFieldPhoneNumber,KTelephoneNum);

	CleanupStack::PopAndDestroy(fetchedItem);
	}

/** 
 * Test an unsuccessful addition to the database
 * @param aDb Contact database
 */
void TestUnsuccessfulAddL(CContactDatabase& aDb)
	{
	TContactItemId templateId = aDb.ICCTemplateIdL(KUidIccGlobalAdnPhonebook);
	CContactItem* iccTemplate = aDb.ReadContactLC(templateId);
	CContactICCEntry* entry = CContactICCEntry::NewL(*iccTemplate);
	CleanupStack::PopAndDestroy(iccTemplate);
	CleanupStack::PushL(entry);
	SetNameL(*entry,KUidContactFieldFamilyName,KUidContactFieldVCardMapUnusedN,KGivenName,EFalse);
	SetNameL(*entry,KUidContactFieldPhoneNumber,KUidContactFieldVCardMapTEL,KTelephoneNum,EFalse);

	syncChecker->ResetMethodCallCountsL();
	syncChecker->SetValidateWriteResponseL(KErrAccessDenied);
	TRAPD(error, aDb.AddNewContactL(*entry)); 	
	test(error==KErrAccessDenied);
	test(syncChecker->ValidateMethodCallCountL() == 1);
	CleanupStack::PopAndDestroy(entry);
	}

/** 
 * Test addition to database without adding to a group
 * @param aDb Contact database
 */
void TestAddingWithoutGroupL(CContactDatabase& aDb)
	{
	TContactItemId templateId = aDb.ICCTemplateIdL(KUidIccGlobalAdnPhonebook);
	CContactItem* iccTemplate = aDb.ReadContactLC(templateId);
	CContactICCEntry* entry = CContactICCEntry::NewL(*iccTemplate);
	CleanupStack::PopAndDestroy(iccTemplate);
	CleanupStack::PushL(entry);
	SetNameL(*entry,KUidContactFieldFamilyName,KUidContactFieldVCardMapUnusedN,KGivenName,EFalse);
	SetNameL(*entry,KUidContactFieldPhoneNumber,KUidContactFieldVCardMapTEL,KTelephoneNum,EFalse);

	syncChecker->ResetMethodCallCountsL();
	syncChecker->SetValidateWriteResponseL(KErrNone);
	TInt oldGroupId = syncChecker->GroupIdL(KUidIccGlobalAdnPhonebook);
	test(KErrNone == syncChecker->UpdatePhonebookGroupIdL(KUidIccGlobalAdnPhonebook, KNullContactId));
	TContactItemId id = aDb.AddNewContactL(*entry); 	
	test(syncChecker->ValidateMethodCallCountL() == 3);
	CleanupStack::PopAndDestroy(entry);

	CContactICCEntry* fetchedItem = static_cast<CContactICCEntry*>(aDb.ReadContactL(id));
	CleanupStack::PushL(fetchedItem);
	//Check group membership	
	const CContactIdArray* owned = fetchedItem->GroupsJoined();
	test(owned==NULL || owned->Count() == 0);
	CleanupStack::PopAndDestroy(fetchedItem);
		
	test(KErrNone == syncChecker->UpdatePhonebookGroupIdL(KUidIccGlobalAdnPhonebook, oldGroupId));
	}

/** 
 * Verify that adding contact cards doesn't call the synchroniser interface
 * @param aDb Contact database
 */
void TestAddingContactCardL(CContactDatabase& aDb)
	{
	CContactCard* card = CContactCard::NewLC();
	syncChecker->ResetMethodCallCountsL();
	aDb.AddNewContactL(*card);
	test(syncChecker->ValidateMethodCallCountL() == 0);
	CleanupStack::PopAndDestroy(card);
	}

/** 
 * Test addition of CContactICCEntry items.
 * @param aDb Contact database
 */
void TestAddL(CContactDatabase& aDb)
	{
	TestCreationL(aDb);
	TestSuccessfulAddL(aDb);
	TestUnsuccessfulAddL(aDb);
	TestAddingWithoutGroupL(aDb);
	TestAddingContactCardL(aDb);
	}

/** 
 * Test PhonebookGroupIdL method.
 * @param aDb Contact database
 */
void TestPhonebookGroupIdL(CContactDatabase& aDb)
	{
	synchroniser = CContactTestSynchroniser::NewL();
	const TContactItemId expectedGroupId(KNullContactId);
	test(KErrNone == syncChecker->UpdatePhonebookGroupIdL(KUidIccGlobalAdnPhonebook, expectedGroupId));
	TContactItemId id = aDb.PhonebookGroupIdL();
	test(id == expectedGroupId);
	}


void CheckForPhbkSyncPluginL()
	{
	test.Next(_L("Check for PhbkSync test plug-in"));


	RImplInfoPtrArray	implInfoArray;
	CleanupResetAndDestroyPushL(implInfoArray);
	REComSession::ListImplementationsL(KUidEcomCntPhBkSyncInterface, implInfoArray);
	//Find implementations of KUidEcomCntPhBkSyncInterface
	TInt availCount = implInfoArray.Count(); 
	TInt count;
	for(count = 0; count < availCount; count++)
		{
		//const TUid firstImplementationFound = implInfoArray[count]->ImplementationUid();
		CImplementationInformation *info = implInfoArray[count];
		test.Printf(_L("\n"));
		test.Printf(_L("PhbkSync plugin #%i, Implementation UID 0x%08X version %i\n"),
			count + 1, info->ImplementationUid(), info->Version());
		test.Printf(_L("Plugin name = \"%S\"\n"), &(info->DisplayName()));
		}

	// is telephony's plug-in in the list?
	for(count = 0; count < availCount; count++)
		{
		//const TUid firstImplementationFound = implInfoArray[count]->ImplementationUid();
		CImplementationInformation *info = implInfoArray[count];
		if(info->DisplayName() == KTestPluginName)
			{
			test.Printf(_L("\n"));
			test.Printf(_L("This test has now loaded the test plugin"));
			test.Printf(_L("\n"));
			availCount = 1;
			break;
			}		
		
		if(info->DisplayName() == KPluginName)
			{
			test.Printf(_L("\n"));
			test.Printf(_L("This test only works with Contacts the test plugin and not the original phonebooksync plugin."));
			test.Printf(_L("Depending on the build to removed the plugin in different ways:"));
			test.Printf(_L("hardware - delete the line \"ECOM_PLUGIN(phbksyncplugin.dll,1020428c.rsc)\" from phbksync.iby"));
			test.Printf(_L("winscw - delete phbksyncplugin.dll from %epocroot%/epoc32/release/winscw/udeb or similarly named directory"));
			test.Printf(_L("\n"));
			test(0);  // stop
			break;
			}
		}

	// only continue test if there is exactly one plug-in present
	test(availCount == 1);	
	CleanupStack::PopAndDestroy(&implInfoArray);
	}

/** Lists tests to execute */

void DoTestsL()
    {
	test.Start(_L("Basic tests for CContactICCEntry"));


	CheckForPhbkSyncPluginL();

	CntTest->CreateDatabaseL();	
	CntTest->OpenDatabaseL();

	syncChecker = CContactSyncChecker::NewL();
	synchroniser = CContactTestSynchroniser::NewL();
	syncChecker->ResetL();
	syncChecker->EnableVerboseLoggingL();
	// test with GSM ADN phonebook only
	test(KErrNone == syncChecker->SetGsmPhonebookL());
	
	CContactDatabase& db= *CntTest->Db();
	TestAddL(db);
	TestReadL(db);
	TestEditL(db);
	TestPhoneMatchingL(db);
	TestFindingL(db);
	TestDeleteL(db);
	TestDatabaseIterationL(db);
	TestDatabaseViewL(db);
	CContactDatabase& db1= *CntTest->Db();
	TestPhonebookGroupIdL(db1);
	
	CntTest->CloseDatabase();
	CntTest->DeleteDatabaseL();

	delete synchroniser;
	synchroniser = NULL;
	delete syncChecker;
	syncChecker = NULL;
    }

/** Standard E32Main method */
GLDEF_C TInt E32Main()
	{
	__UHEAP_MARK;
    CntTest=new(ELeave) CCntTest;
	CntTest->ConstructL(test,KDatabaseFileName);
    TRAPD(err,DoTestsL());
	if (err)
		{
		// delete globals
		delete synchroniser;
		synchroniser = NULL;
		delete syncChecker;
		syncChecker = NULL;
		}
	CntTest->EndTestLib(err);
	__UHEAP_MARKEND;
	return KErrNone;
    }