summaryrefslogtreecommitdiffstats
path: root/plugins/contacts/symbian/contactsmodel/tsrc/t_iccmultiplephonebook.cpp
blob: 01dc074755283711d442abf9e18d285c7196bc9c (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
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
/*
* 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>

#ifdef SYMBIAN_ENABLE_SPLIT_HEADERS
#include "cntsyncecom.h"
#endif

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

#include "t_utils.h"
#include "cntsyncchecker.h"

_LIT(KTestName,"CContactICCEntry tests with multiple phonebooks");
_LIT(KDatabaseFileName,"C:T_ICCMULTIPLEPHONEBOOK");
_LIT(KGivenEditName,"edited name");
_LIT(KIccName,"icc entry name");
_LIT(KNonIccName,"non-icc name");
_LIT(KIccNumber,"020 794 60123");
_LIT(KNonIccNumber,"020 794 60060");

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

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

LOCAL_D CContactSyncChecker* syncChecker;

//===============================================================================================
// SETUP & UTILITIES
//===============================================================================================
class TFieldEntry
	{
	public:
		TFieldEntry(TFieldType aField);
		TFieldEntry(TFieldType aField,TUid aFieldType);
	public:
		TFieldType iField;
		TUid iFieldType;
	};

TFieldEntry::TFieldEntry(TFieldType aField)
:iField(aField)
,iFieldType(KUidIccPhonebookNotFound)
	{}

TFieldEntry::TFieldEntry(TFieldType aField,TUid aFieldType)
:iField(aField)
,iFieldType(aFieldType)
	{}

/**
 * Create a new template and add it to the database
 * @param aDb Contact database
 * @param aTemplateName Name for the template
 * @param aFieldSet Array of fields which should are added to the template
 * @return TContactItemId Id of the newly created template
 */
TContactItemId CreatePhonebookTemplateL(CContactDatabase& aDb, const TDesC& aTemplateName, RArray<TFieldEntry>& aFieldSet)
	{
	CContactItem* temp = aDb.CreateContactCardTemplateLC(aTemplateName);
	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);

	// Add each of the required fields to the template
	for (TInt j=0; j<aFieldSet.Count(); ++j)
		{
		TFieldEntry fieldEntry=aFieldSet[j];

		CContactItemField* field = CContactItemField::NewLC(KStorageTypeText,fieldEntry.iField);
		if (fieldEntry.iFieldType!=KNullUid)
			field->AddFieldTypeL(fieldEntry.iFieldType);
		temp->AddFieldL(*field);
		CleanupStack::Pop(field);
		}

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

/**
 * Return an array of fields for the phonebook specified
 * @param aPhonebookUid The phonebook uid
 * @return RArray<TFieldEntry> Field set for this specific phonebook.  Any contact item
 * which belongs to this phonebook should contain these fields
 */
RArray<TFieldEntry> fieldSetForPhonebook(TUid aPhonebookUid)
	{
	RArray<TFieldEntry> entries;
	entries.Append(TFieldEntry(KUidContactFieldFamilyName));
	entries.Append(TFieldEntry(KUidContactFieldGivenName));
	entries.Append(TFieldEntry(KUidContactFieldPhoneNumber,KUidContactFieldVCardMapHOME));
	entries.Append(TFieldEntry(KUidContactFieldICCPhonebook));
	
	if (aPhonebookUid==KUidUsimAppAdnPhonebook)
		{
		entries.Append(TFieldEntry(KUidContactFieldEMail,KUidContactFieldVCardMapHOME));
		entries.Append(TFieldEntry(KUidContactFieldEMail,KUidContactFieldVCardMapWORK));
		entries.Append(TFieldEntry(KUidContactFieldPhoneNumber,KUidContactFieldVCardMapHOME));
		entries.Append(TFieldEntry(KUidContactFieldPhoneNumber,KUidContactFieldVCardMapWORK));
		entries.Append(TFieldEntry(KUidContactFieldPhoneNumber,KUidContactFieldVCardMapCELL));
		entries.Append(TFieldEntry(KUidContactFieldPhoneNumber,KUidContactFieldVCardMapPAGER));
		}

	return entries;
	}

void SetUpIccPhonebooksL(CContactDatabase& aDb)
	{
	test.Next(_L("Set up phonebooks"));
	RArray<TUid>	phbkList;
	CleanupClosePushL(phbkList);

	phbkList.AppendL(KUidIccGlobalAdnPhonebook);
	phbkList.AppendL(KUidIccGlobalSdnPhonebook);
	phbkList.AppendL(KUidIccGlobalLndPhonebook);
	phbkList.AppendL(KUidUsimAppAdnPhonebook);
	// tell phonebook sync simulator what phonebooks to use
	test(syncChecker->SetPhonebookListL(phbkList) == KErrNone);

	// simulate synchronisation - includes creating/finding template & group
	// also defaults all validate responses to KErrNone
	test(syncChecker->SynchroniseIccAllPhonebooksL(aDb, KErrNone, KErrNone) == KErrNone);
	
	CleanupStack::PopAndDestroy(); // phbkList
	}

/**
 * Check the content of the phonebook field in the contact item
 * @param aContactItem Contact item
 * @param aPhonebookUid The phonebook uid
 * @param aFieldIsSet EFalse if the phonebook field should not contain a value, else ETrue
 */
void CheckPhonebookField(CContactItem& aContactItem,TUid aPhonebookUid,TBool aFieldIsSet)
	{
	const CContactItemFieldSet& fieldset = aContactItem.CardFields();
	const TInt pos = fieldset.Find(KUidContactFieldICCPhonebook);
	test(pos!=KErrNotFound);
	TPtrC phonebookField=fieldset[pos].TextStorage()->Text();

	if(aFieldIsSet)
		{
		TLex input(phonebookField);
		TInt phonebookFieldVal;
		/*TInt ignore  = */ input.Val(phonebookFieldVal);
		TUid phonebookFieldUid;
		phonebookFieldUid.iUid=phonebookFieldVal;
		test(phonebookFieldUid==aPhonebookUid);
		}
	else
		test(phonebookField==KNullDesC);
	}

/**
 * Verify that the template id returned by CContactDatabase::ICCTemplateIdL is the
 * same as that returned directly from the plugin
 * @param aDb Contact database
 */
void VerifyPhonebookTemplateIdsL(CContactDatabase& aDb)
	{
	test.Next(_L("Verify template ids"));
	syncChecker->ResetMethodCallCountsL();
	test(aDb.ICCTemplateIdL() == syncChecker->ICCTemplateIdL(KUidIccGlobalAdnPhonebook));
	test(aDb.ICCTemplateIdL(KUidIccGlobalAdnPhonebook) == syncChecker->ICCTemplateIdL(KUidIccGlobalAdnPhonebook));
	test(aDb.ICCTemplateIdL(KUidIccGlobalSdnPhonebook) == syncChecker->ICCTemplateIdL(KUidIccGlobalSdnPhonebook));
	test(aDb.ICCTemplateIdL(KUidIccGlobalLndPhonebook) == syncChecker->ICCTemplateIdL(KUidIccGlobalLndPhonebook));
	test(aDb.ICCTemplateIdL(KUidUsimAppAdnPhonebook) == syncChecker->ICCTemplateIdL(KUidUsimAppAdnPhonebook));
	test(aDb.ICCTemplateIdL(KNullUid) == KNullContactId);
	test(syncChecker->ValidateMethodCallCountL() == 6);
	}

/**
 * Set the name field of the contact with id aId to contain the name aName
 * @param aDb Contact database
 * @param aId Contact item id
 * @param aName A name
 */
void SetNameField(CContactDatabase& aDb,TContactItemId aId,const TDesC& aName)
	{
	syncChecker->ResetMethodCallCountsL();
	CContactItem* item = aDb.OpenContactLX(aId);
	CleanupStack::PushL(item);
	if (item->Type() == KUidContactICCEntry)
		{
		test(syncChecker->ValidateMethodCallCountL() == 2);
		}
	else
		{
		test(syncChecker->ValidateMethodCallCountL() == 0);
		}
	CContactItemFieldSet& fieldset = item->CardFields();
	TInt pos = fieldset.Find(KUidContactFieldFamilyName);
	test(pos!=KErrNotFound);
	CContactItemField& field = fieldset[pos];
	CContactTextField* textfield = field.TextStorage();
	textfield->SetTextL(aName);

	syncChecker->ResetMethodCallCountsL();
	aDb.CommitContactL(*item);
	if (item->Type() == KUidContactICCEntry)
		{
		test(syncChecker->ValidateMethodCallCountL() == 2);
		}
	else
		{
		test(syncChecker->ValidateMethodCallCountL() == 0);
		}
	CleanupStack::PopAndDestroy(2); // item, lock record
	}

/**
 * Set the phone number field of the contact with id aId to contain the number aNumber
 * @param aDb Contact database
 * @param aId Contact item id
 * @param aNumber A telephone number
 */
void SetPhoneNumberField(CContactDatabase& aDb,TContactItemId aId,const TDesC& aNumber)
	{
	syncChecker->ResetMethodCallCountsL();
	CContactItem* item = aDb.OpenContactLX(aId);
	CleanupStack::PushL(item);
	if (item->Type() == KUidContactICCEntry)
		{
		test(syncChecker->ValidateMethodCallCountL() == 2);
		}
	else
		{
		test(syncChecker->ValidateMethodCallCountL() == 0);
		}
	CContactItemFieldSet& fieldset = item->CardFields();
	TInt pos = fieldset.Find(KUidContactFieldPhoneNumber);
	test(pos!=KErrNotFound);
	
	CContactItemField& field = fieldset[pos];
	CContactTextField* textfield = field.TextStorage();
	textfield->SetTextL(aNumber);

	syncChecker->ResetMethodCallCountsL();
	aDb.CommitContactL(*item);
	if (item->Type() == KUidContactICCEntry)
		{
		test(syncChecker->ValidateMethodCallCountL() == 2);
		}
	else
		{
		test(syncChecker->ValidateMethodCallCountL() == 0);
		}
	CleanupStack::PopAndDestroy(2); // item, lock record
	}

//===============================================================================================
// ADDING entries
//===============================================================================================
/**
 * Create an entry based on the template for this specific phonebook, and add
 * the entry to the contact database
 * @param aDb Contact database
 * @param aPhonebookUid The phonebook uid
 * @return TContactItemId Id of the newly created icc entry
 */
TContactItemId doAddIccEntryL(CContactDatabase& aDb,TUid aPhonebookUid)
	{
	syncChecker->ResetMethodCallCountsL();
	TContactItemId templateId = aDb.ICCTemplateIdL(aPhonebookUid);
	test(syncChecker->ValidateMethodCallCountL() == 1);
	CContactItem* iccTemplate = aDb.ReadContactLC(templateId);
	CContactICCEntry* entry = CContactICCEntry::NewL(*iccTemplate);
	CleanupStack::PopAndDestroy(iccTemplate);
	CleanupStack::PushL(entry);
	// Add to the database
	CheckPhonebookField(*entry,aPhonebookUid, EFalse);
	syncChecker->ResetMethodCallCountsL();
	TContactItemId id = aDb.AddNewContactL(*entry); 
	test(syncChecker->ValidateMethodCallCountL() == 3);
	CleanupStack::PopAndDestroy(entry);
	test(id!=KNullContactId);
	// Check group membership
	syncChecker->ResetMethodCallCountsL();
	CContactICCEntry* fetchedItem = static_cast<CContactICCEntry*>(aDb.ReadContactL(id));
	test(syncChecker->ValidateMethodCallCountL() == 1);
	CleanupStack::PushL(fetchedItem);	
	const CContactIdArray* owned = fetchedItem->GroupsJoined();
	test(owned!=NULL && owned->Count() == 1);
	test((*owned)[0]==syncChecker->GroupIdL(aPhonebookUid));
	test(fetchedItem->Type() == KUidContactICCEntry);
	// Verify that the phonebook field has been set
	CheckPhonebookField(*fetchedItem,aPhonebookUid, ETrue);
	CleanupStack::PopAndDestroy(fetchedItem);	

	return id;
	}

TContactItemId AddIccEntryL(CContactDatabase& aDb,TUid aPhonebookUid)
	{
	syncChecker->SetValidateWriteResponseL(KErrNone);
	return doAddIccEntryL(aDb, aPhonebookUid);
	}

void UnsuccessfulAddIccEntryL(CContactDatabase& aDb,TUid aPhonebookUid, TInt aReason)
	{
	syncChecker->SetValidateWriteResponseL(aReason);
	TRAPD(err,doAddIccEntryL(aDb, aPhonebookUid));
	test(err==aReason);
	}

void AddEntriesBasedOnEachOfTheTemplatesL(CContactDatabase& aDb)
	{
	test.Next(_L("Test successful adding"));
	TInt numberOfAdnEntries=3;
	TInt numberOfSdnEntries=2;
	TInt numberOfLdnEntries=1;
	TInt numberOfUsimEntries=4;
	TInt iter=0;
	for (iter=0; iter<numberOfAdnEntries; ++iter)
		{
		TContactItemId id=AddIccEntryL(aDb,KUidIccGlobalAdnPhonebook);
		CContactGroup* group=static_cast<CContactGroup*>(aDb.ReadContactLC(syncChecker->GroupIdL(KUidIccGlobalAdnPhonebook)));
		CContactIdArray* entries=group->ItemsContainedLC();
		test(entries->Count() == iter+1);
		CleanupStack::PopAndDestroy(entries);
		test(group->ContainsItem(id));
		CleanupStack::PopAndDestroy(group);
		}
	for (iter=0; iter<numberOfSdnEntries; ++iter)
		{
		TContactItemId id=AddIccEntryL(aDb,KUidIccGlobalSdnPhonebook);
		CContactGroup* group=static_cast<CContactGroup*>(aDb.ReadContactLC(syncChecker->GroupIdL(KUidIccGlobalSdnPhonebook)));
		CContactIdArray* entries=group->ItemsContainedLC();
		test(entries->Count() == iter+1);
		CleanupStack::PopAndDestroy(entries);
		test(group->ContainsItem(id));
		CleanupStack::PopAndDestroy(group);
		}
	for (iter=0; iter<numberOfLdnEntries; ++iter)
		{
		TContactItemId id=AddIccEntryL(aDb, KUidIccGlobalLndPhonebook);
		CContactGroup* group=static_cast<CContactGroup*>(aDb.ReadContactLC(syncChecker->GroupIdL(KUidIccGlobalLndPhonebook)));
		CContactIdArray* entries=group->ItemsContainedLC();
		test(entries->Count() == iter+1);
		CleanupStack::PopAndDestroy(entries);
		test(group->ContainsItem(id));
		CleanupStack::PopAndDestroy(group);
		}
	for (iter=0; iter<numberOfUsimEntries; ++iter)
		{
		TContactItemId id=AddIccEntryL(aDb,KUidUsimAppAdnPhonebook);
		CContactGroup* group=static_cast<CContactGroup*>(aDb.ReadContactLC(syncChecker->GroupIdL(KUidUsimAppAdnPhonebook)));
		CContactIdArray* entries=group->ItemsContainedLC();
		test(entries->Count() == iter+1);
		CleanupStack::PopAndDestroy(entries);
		test(group->ContainsItem(id));
		CleanupStack::PopAndDestroy(group);
		}
	// Add an ICC entry based on the system template, not one of the ICC templates
	TContactItemId templateId=aDb.TemplateId();
	CContactItem* systemTemplate = aDb.ReadContactLC(templateId);
	CContactICCEntry* entry = CContactICCEntry::NewL(*systemTemplate);
	CleanupStack::PopAndDestroy(systemTemplate);
	CleanupStack::PushL(entry);
	syncChecker->ResetMethodCallCountsL();
	TContactItemId id = aDb.AddNewContactL(*entry); 
	test(syncChecker->ValidateMethodCallCountL() == 3);

	syncChecker->ResetMethodCallCountsL();
	CContactItem* item=aDb.ReadContactLC(id);
	test(syncChecker->ValidateMethodCallCountL() == 1);
	// check the entry hasn't been added to any group
	CContactIdArray* array=static_cast<CContactICCEntry*>(item)->GroupsJoinedLC();
	test(array->Count() == 0);
	CleanupStack::PopAndDestroy(3); // array, close item, item
	}

void TestUnsuccessfulAddL(CContactDatabase& aDb)
	{
	// Test adding entries to each of the phonebooks when the ICC is locked, unaccessible or corrupt
	test.Next(_L("Test unsuccessful adding"));
	UnsuccessfulAddIccEntryL(aDb,KUidIccGlobalAdnPhonebook,KErrLocked);
	UnsuccessfulAddIccEntryL(aDb,KUidIccGlobalAdnPhonebook,KErrAccessDenied);
	UnsuccessfulAddIccEntryL(aDb,KUidIccGlobalAdnPhonebook,KErrCorrupt);
	UnsuccessfulAddIccEntryL(aDb,KUidIccGlobalSdnPhonebook,KErrLocked);
	UnsuccessfulAddIccEntryL(aDb,KUidIccGlobalSdnPhonebook,KErrAccessDenied);
	UnsuccessfulAddIccEntryL(aDb,KUidIccGlobalSdnPhonebook,KErrCorrupt);
	UnsuccessfulAddIccEntryL(aDb,KUidIccGlobalLndPhonebook,KErrLocked);
	UnsuccessfulAddIccEntryL(aDb,KUidIccGlobalLndPhonebook,KErrAccessDenied);
	UnsuccessfulAddIccEntryL(aDb,KUidIccGlobalLndPhonebook,KErrCorrupt);
	UnsuccessfulAddIccEntryL(aDb,KUidUsimAppAdnPhonebook,KErrLocked);
	UnsuccessfulAddIccEntryL(aDb,KUidUsimAppAdnPhonebook,KErrAccessDenied);
	UnsuccessfulAddIccEntryL(aDb,KUidUsimAppAdnPhonebook,KErrCorrupt);
	}

/** 
 * Verify that adding contact cards doesn't call the synchroniser interface
 * @param aDb Contact database
 */
TContactItemId AddContactCardL(CContactDatabase& aDb)
	{
	_LIT(KTemplateName,"contact card template");
	RArray<TFieldEntry> entries=fieldSetForPhonebook(KUidIccGlobalAdnPhonebook);
	TContactItemId templateId=CreatePhonebookTemplateL(aDb,KTemplateName,entries);
	entries.Close();
	CContactItem* iccTemplate = aDb.ReadContactLC(templateId);
	CContactCard* card = CContactCard::NewLC(iccTemplate);
	syncChecker->ResetMethodCallCountsL();
	TContactItemId id=aDb.AddNewContactL(*card);
	test(syncChecker->ValidateMethodCallCountL() == 0);
	CleanupStack::PopAndDestroy(2,iccTemplate);
	return id;
	}
//===============================================================================================
// DELETING entries
//===============================================================================================
/**
 * Verify that the synchroniser is called when deleting an icc entry, and that the 
 * deletion of an icc entry fails if the icc is locked.
 * @param aDb Contact database
 */
void TestDeleteL(CContactDatabase& aDb)
	{
	test.Next(_L("Test delete when ICC locked"));
	syncChecker->SetDeleteContactResponseL(KErrLocked);
	TContactItemId iccId=AddIccEntryL(aDb,KUidIccGlobalAdnPhonebook);
	syncChecker->ResetMethodCallCountsL();
	TRAPD(err,aDb.DeleteContactL(iccId));
	test(err==KErrLocked);
	test(syncChecker->ValidateMethodCallCountL() == 2);
	syncChecker->ResetMethodCallCountsL();
	CContactItem* item=aDb.ReadContactLC(iccId);
	test(syncChecker->ValidateMethodCallCountL() == 1);
	test(item!=NULL);
	CleanupStack::PopAndDestroy(item);
	item=NULL;

	test.Next(_L("Test successful delete of non-icc entry when ICC locked"));
	TContactItemId nonIccId=AddContactCardL(aDb);
	syncChecker->ResetMethodCallCountsL();
	TRAP(err,aDb.DeleteContactL(nonIccId));
	test(err==KErrNone);
	test(syncChecker->ValidateMethodCallCountL() == 0);

	test.Next(_L("Test successful delete"));
	syncChecker->SetDeleteContactResponseL(KErrNone);
	syncChecker->ResetMethodCallCountsL();
	TRAP(err,aDb.DeleteContactL(iccId));
	test(syncChecker->ValidateMethodCallCountL() == 2);
	test(err==KErrNone);
	syncChecker->ResetMethodCallCountsL();
	TRAP(err,aDb.ReadContactLC(iccId));
	test(syncChecker->ValidateMethodCallCountL() == 0);
	test(err==KErrNotFound);
	}

//===============================================================================================
// READING entries
//===============================================================================================
void TestReadMinimalContactL(CContactDatabase& aDb, TContactItemId aIccId, TContactItemId aNonIccId)
	{
	test.Next(_L("Test ReadMinimalContactL"));
	// Successful read of icc entry
	syncChecker->SetValidateResponseL(MContactSynchroniser::ERead,KErrNone);
	syncChecker->ResetMethodCallCountsL();
	CContactItem* item=aDb.ReadMinimalContactLC(aIccId);
	test(syncChecker->ValidateMethodCallCountL() == 1);
	test(item!=NULL);
	CleanupStack::PopAndDestroy(item);
	item=NULL;

	// Unsuccessful read of icc entry because icc locked
	syncChecker->SetValidateResponseL(MContactSynchroniser::ERead,KErrAccessDenied);
	syncChecker->ResetMethodCallCountsL();
	TRAPD(err,item=aDb.ReadMinimalContactLC(aIccId));
	test(syncChecker->ValidateMethodCallCountL() == 1);
	test(item==NULL);
	test(err==KErrAccessDenied);
	
	// successful read of non-icc entry, even though icc locked
	syncChecker->ResetMethodCallCountsL();
	item=aDb.ReadMinimalContactLC(aNonIccId);
	test(syncChecker->ValidateMethodCallCountL() == 0);
	test(item!=NULL);
	CleanupStack::PopAndDestroy(item);
	item=NULL;
	}

void TestReadContactL(CContactDatabase& aDb, TContactItemId aIccId, TContactItemId aNonIccId)
	{
	test.Next(_L("Test ReadContactL"));
	// Successful read of icc entry
	syncChecker->SetValidateResponseL(MContactSynchroniser::ERead,KErrNone);
	syncChecker->ResetMethodCallCountsL();
	test(syncChecker->ValidateMethodCallCountL() == 0);	
	CContactItem* item=aDb.ReadContactLC(aIccId);
	test(syncChecker->ValidateMethodCallCountL() == 1);
	test(item!=NULL);
	CleanupStack::PopAndDestroy(item);
	item=NULL;

	// Unsuccessful read of icc entry because icc locked
	syncChecker->SetValidateResponseL(MContactSynchroniser::ERead,KErrAccessDenied);
	syncChecker->ResetMethodCallCountsL();
	test(syncChecker->ValidateMethodCallCountL() == 0);
	TRAPD(err,item=aDb.ReadContactL(aIccId));
	test(item==NULL);
	test(err==KErrAccessDenied);
	test(syncChecker->ValidateMethodCallCountL() == 1);
	
	// successful read of non-icc entry, even though icc locked
	syncChecker->ResetMethodCallCountsL();
	test(syncChecker->ValidateMethodCallCountL() == 0);	
	item=aDb.ReadContactLC(aNonIccId);
	test(syncChecker->ValidateMethodCallCountL() == 0);
	test(item!=NULL);
	CleanupStack::PopAndDestroy(item);
	item=NULL;
	}

void TestReadTextDefinitionL(CContactDatabase& aDb, TContactItemId aIccId, TContactItemId aNonIccId)
	{
	test.Next(_L("Test ReadTextDefinitionL"));
	// Successful read of icc entry
	CContactTextDef* textDef=CContactTextDef::NewLC();
	textDef->AppendL(TContactTextDefItem(KUidContactFieldFamilyName));
	TBuf<64> result;
	syncChecker->SetValidateResponseL(MContactSynchroniser::ERead,KErrNone);
	syncChecker->ResetMethodCallCountsL();
	aDb.ReadContactTextDefL(aIccId,result,textDef);
	test(syncChecker->ValidateMethodCallCountL() == 1);
	test(result==KIccName);
	result.Zero();

	// Unsuccessful read of icc entry because icc locked
	syncChecker->SetValidateResponseL(MContactSynchroniser::ERead,KErrAccessDenied);
	syncChecker->ResetMethodCallCountsL();
	TRAPD(err, aDb.ReadContactTextDefL(aIccId,result,textDef));
	test(err==KErrAccessDenied);
	test(result.Length() == 0);
	test(syncChecker->ValidateMethodCallCountL() == 1);

	// successful read of non-icc entry, even though icc locked
	syncChecker->ResetMethodCallCountsL();
	TRAP(err, aDb.ReadContactTextDefL(aNonIccId,result,textDef));
	test(err==KErrNone);
	test(result==KNonIccName);
	test(syncChecker->ValidateMethodCallCountL() == 0);
	CleanupStack::PopAndDestroy(textDef);
	}

/**
 * Verify that the synchroniser is called when reading an icc entry
 * @param aDb Contact database
 */
void TestReadL(CContactDatabase& aDb)
	{
	TContactItemId iccId=AddIccEntryL(aDb,KUidIccGlobalAdnPhonebook);
	SetNameField(aDb,iccId,KIccName);
	TContactItemId nonIccId = AddContactCardL(aDb);
	SetNameField(aDb,nonIccId,KNonIccName);
	TestReadMinimalContactL(aDb,iccId,nonIccId);
	TestReadContactL(aDb,iccId,nonIccId);
	TestReadTextDefinitionL(aDb,iccId,nonIccId);
	syncChecker->SetValidateResponseL(MContactSynchroniser::ERead,KErrNone);
	syncChecker->ResetMethodCallCountsL();
	aDb.DeleteContactL(iccId);
	aDb.DeleteContactL(nonIccId);
	test(syncChecker->ValidateMethodCallCountL() == 2);
	}

//===============================================================================================
// EDITING entries
//===============================================================================================
/**
 * 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 TestSuccessfulIccEditL(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
	}

/**
 * Verify that edit of non-icc entries is successful when the icc is
 * locked, and that the synchroniser is not called
 * @param aDb Contact database
 * @param aId Item Id to edit
 */
void TestSuccessfulNonIccEditL(CContactDatabase& aDb, TContactItemId aId)
	{
	syncChecker->SetValidateResponseL(MContactSynchroniser::ERead,KErrLocked);
	syncChecker->SetValidateResponseL(MContactSynchroniser::EEdit, KErrLocked);
	syncChecker->SetValidateWriteResponseL(KErrLocked);
	syncChecker->ResetMethodCallCountsL();
	test(syncChecker->ValidateMethodCallCountL() == 0);
	CContactItem* item = aDb.OpenContactLX(aId);
	CleanupStack::PushL(item);
	test(syncChecker->ValidateMethodCallCountL() == 0);	

	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() == 0);	
	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)
	{
	syncChecker->SetValidateResponseL(MContactSynchroniser::ERead,KErrNone);
	TContactItemId iccId = AddIccEntryL(aDb,KUidIccGlobalAdnPhonebook);
	TContactItemId nonIccId = AddContactCardL(aDb);
	test.Next(_L("Test successful edit"));
	TestSuccessfulIccEditL(aDb,iccId);
	TestSuccessfulNonIccEditL(aDb,nonIccId);
	test.Next(_L("Test unsuccessful edit - fail edit"));
	FailEditValidationL(aDb,iccId);
	test.Next(_L("Test unsuccessful edit - fail write"));
	FailWriteValidationL(aDb,iccId);
	}
//===============================================================================================
// SEARCHING entries
//===============================================================================================
void TestFindLC(CContactDatabase& aDb, TContactItemId aIccId, TContactItemId aNonIccId)
	{
	test.Next(_L("Test FindLC"));
	// Successful find of icc entry
	CContactItemFieldDef* fieldDef=new(ELeave) CContactItemFieldDef;
	CleanupStack::PushL(fieldDef);
	fieldDef->AppendL(KUidContactFieldFamilyName);
	syncChecker->ResetMethodCallCountsL();
	CContactIdArray* array = aDb.FindLC(KIccName,fieldDef);
	test(syncChecker->ValidateMethodCallCountL() == 1);
	test(array!=NULL);
	test(array->Count() == 1);
	test((*array)[0]==aIccId);
	CleanupStack::PopAndDestroy(array);
	array=NULL;

	test.Next(_L("Test searching when ICC locked"));
	// Unsuccessful find of icc entry because icc locked
	syncChecker->SetValidateResponseL(MContactSynchroniser::ESearch,KErrLocked);
	syncChecker->ResetMethodCallCountsL();
	TRAPD(err,array = aDb.FindLC(KIccName,fieldDef));
	test(syncChecker->ValidateMethodCallCountL() == 1);
	test(err==KErrLocked);
	test(array==NULL);

	// successful find of non-icc entry, even though icc locked
	syncChecker->ResetMethodCallCountsL();
	array = aDb.FindLC(KNonIccName,fieldDef);
	test(syncChecker->ValidateMethodCallCountL() == 0);
	test(array!=NULL);
	test(array->Count() == 1);
	test((*array)[0]==aNonIccId);
	CleanupStack::PopAndDestroy(2,fieldDef); // array, fieldDef
	}

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

void TestFindInTextDefLC(CContactDatabase& aDb, TContactItemId aIccId, TContactItemId aNonIccId)
	{
	test.Next(_L("Test FindInTextDefLC"));
	// Successful find of icc entry
	syncChecker->SetValidateResponseL(MContactSynchroniser::ESearch,KErrNone);
	syncChecker->SetValidateResponseL(MContactSynchroniser::ERead,KErrNone);

	TCallBack callBack(findWordSplitterL);
	CContactTextDef* textDef=CContactTextDef::NewLC();
	textDef->AppendL(TContactTextDefItem(KUidContactFieldFamilyName));
	CDesCArray* desArray = new(ELeave) CDesCArrayFlat(5);
	desArray->AppendL(_L("icc"));
	syncChecker->ResetMethodCallCountsL();
	CContactIdArray* array = aDb.FindInTextDefLC(*desArray,textDef,callBack);
	//test(syncChecker->ValidateMethodCallCountL() == 3);
	test(array!=NULL);
	test(array->Count() == 1);
	test((*array)[0]==aIccId);
	CleanupStack::PopAndDestroy(array);
	array=NULL;

	test.Next(_L("Test searching when ICC locked"));
	// Unsuccessful find of icc entry because icc locked
	syncChecker->SetValidateResponseL(MContactSynchroniser::ESearch,KErrLocked);
	syncChecker->ResetMethodCallCountsL();
	TRAPD(err,array = aDb.FindInTextDefLC(*desArray,textDef,callBack));
	test(syncChecker->ValidateMethodCallCountL() == 1);
	test(err==KErrLocked);
	test(array==NULL);
	desArray->Delete(0);
	delete desArray;

	// successful find of non-icc entry, even though icc locked
	CDesCArray* desArray2 = new(ELeave) CDesCArrayFlat(5);
	desArray2->AppendL(_L("non-icc"));
	syncChecker->ResetMethodCallCountsL();
	array = aDb.FindInTextDefLC(*desArray2,textDef,callBack);
	test(syncChecker->ValidateMethodCallCountL() == 0);
	test(array!=NULL);
	test(array->Count() == 1);
	test((*array)[0]==aNonIccId);

	// both the icc and non-icc entry should match the search criteria, but only the
	// non-icc entry should be returned since icc is locked
	SetNameField(aDb,aIccId,KNonIccName);
	syncChecker->ResetMethodCallCountsL();
	TRAP(err,array = aDb.FindInTextDefLC(*desArray2,textDef,callBack));
	test(syncChecker->ValidateMethodCallCountL() == 1);
	test(err==KErrLocked);
	test(array!=NULL);
	test(array->Count() == 1);
	test((*array)[0]==aNonIccId);
	desArray2->Delete(0);
	delete desArray2;
	SetNameField(aDb,aIccId,KIccName);
	CleanupStack::PopAndDestroy(2,textDef); // array, textDef
	}

void TestMatchPhoneNumberL(CContactDatabase& aDb, TContactItemId aIccId, TContactItemId aNonIccId)
	{
	// 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.
	test.Next(_L("Test MatchPhoneNumberL"));
	// Successful find of icc entry phone number
	syncChecker->ResetMethodCallCountsL();
	CContactIdArray* array = aDb.MatchPhoneNumberL(KIccNumber,KMaxPhoneMatchLength);
	test(syncChecker->ValidateMethodCallCountL() == 0);
	test(array!=NULL);
	test(array->Count() == 1);
	test((*array)[0]==aIccId);
	delete array;
	array=NULL;

	test.Next(_L("Test searching when ICC locked"));
	// successful find when icc locked - validation done when entry read
	syncChecker->SetValidateResponseL(MContactSynchroniser::ESearch,KErrLocked);
	syncChecker->ResetMethodCallCountsL();
	TRAPD(err,array = aDb.MatchPhoneNumberL(KIccNumber,KMaxPhoneMatchLength));
	test(syncChecker->ValidateMethodCallCountL() == 0);
	test(array!=NULL);
	test(err==KErrNone);
	test((*array)[0]==aIccId);
	delete array;
	array=NULL;
	syncChecker->ResetMethodCallCountsL();
	array = aDb.MatchPhoneNumberL(KNonIccNumber,KMaxPhoneMatchLength);
	test(syncChecker->ValidateMethodCallCountL() == 0);
	test(array!=NULL);
	test(array->Count() == 1);
	test((*array)[0]==aNonIccId);
	delete array;
	array=NULL;
	}

void TestSearchL(CContactDatabase& aDb)
	{
	test.Next(_L("Test searching"));
	syncChecker->SetValidateResponseL(MContactSynchroniser::ERead,KErrNone);
	syncChecker->SetValidateResponseL(MContactSynchroniser::ESearch,KErrNone);
	TContactItemId iccId = AddIccEntryL(aDb,KUidIccGlobalAdnPhonebook);
	SetNameField(aDb,iccId,KIccName);
	SetPhoneNumberField(aDb,iccId,KIccNumber);
	TContactItemId nonIccId = AddContactCardL(aDb);
	SetNameField(aDb,nonIccId,KNonIccName);
	SetPhoneNumberField(aDb,nonIccId,KNonIccNumber);

	TestFindLC(aDb,iccId,nonIccId);
	TestFindInTextDefLC(aDb,iccId,nonIccId);
	TestMatchPhoneNumberL(aDb,iccId,nonIccId);
	}


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 */

/**

@SYMTestCaseID     PIM-T-ICCMULTIPLEPHONEBOOK-0001

*/
void DoTestsL()
    {
	test.Start(_L("@SYMTestCaseID:PIM-T-ICCMULTIPLEPHONEBOOK-0001 CContactICCEntry tests with multiple phonebooks"));

	CheckForPhbkSyncPluginL();

	CntTest->CreateDatabaseL();	
	CntTest->OpenDatabaseL();
	CContactDatabase& db= *CntTest->Db();

	syncChecker = CContactSyncChecker::NewL();
	syncChecker->ResetEverythingL();

	SetUpIccPhonebooksL(db);
	VerifyPhonebookTemplateIdsL(db);
	AddEntriesBasedOnEachOfTheTemplatesL(db);
	TestUnsuccessfulAddL(db);
	TestDeleteL(db);
	TestReadL(db);
	TestEditL(db);
	TestSearchL(db);
	syncChecker->ResetMethodCallCountsL();

	CntTest->CloseDatabase();
	CntTest->DeleteDatabaseL();


	delete syncChecker;
	syncChecker = NULL;
    }


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