summaryrefslogtreecommitdiffstats
path: root/plugins/contacts/symbian/contactsmodel/tsrc/t_filterview.cpp
blob: 45d2a113a7ee6311004d5fd2176acc1a1e72f360 (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
/*
* Copyright (c) 2000-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: 
*
*/


#include <e32std.h>
#include <e32test.h>
#include <cntdb.h>
#include <cntitem.h>
#include <cntfldst.h>
#include "t_utils2.h"
#include "t_filterview.h"
#include "t_utils.h"

//
// Constants.
//

_LIT(KTestName,"t_filterview");
_LIT(KLogFileName,"t_filterview.log");

_LIT(KDbFileName,"c:t_filterview.cdb");

_LIT(KRemoteViewName,"RemoteView");
_LIT(KTextDefSeparator,"");

LOCAL_D RTest test(KTestName);

const TInt KNumSmsContacts = 111;
const TInt KNumWorkEmailContacts = 121;
const TInt KNumHomeEmailContacts = 131;
const TInt KNumLandlineContacts = 141;
const TInt KNumFaxContacts = 151;
const TInt KNumRingTone = 161;
const TInt KNumVoiceDial = 171;

const TInt KNumIMProtocolWV = 53; 
const TInt KNumIMAddress= KNumIMProtocolWV;

const TInt KNumPhonable = KNumSmsContacts + KNumLandlineContacts + KNumFaxContacts;
const TInt KNumUnfiltered =	KNumSmsContacts
							+ KNumWorkEmailContacts
							+ KNumHomeEmailContacts
							+ KNumLandlineContacts
							+ KNumFaxContacts
							+ KNumRingTone
							+ KNumVoiceDial
							+ KNumIMAddress;


//
// CTestConductor.
//

CTestConductor* CTestConductor::NewL()
	{
	CTestConductor* self=new(ELeave) CTestConductor();
	CleanupStack::PushL(self);
	self->ConstructL();
	self->RunTestsL();
	CleanupStack::Pop();
	return self;
	}

CTestConductor::~CTestConductor()
	{
	delete iLog;
	delete iDb;
	delete iRandomGenerator;
    TRAP_IGNORE(CContactDatabase::DeleteDatabaseL(KDbFileName));
    
	iFs.Close();
	CCntTest::ProfileReset(0,1);	// ensure the profiling 'TLS' used in EKA2 is freed
	}

CTestConductor::CTestConductor() {}

void CTestConductor::ConstructL()
	{
	User::LeaveIfError(iFs.Connect());
	iLog=CLog::NewL(test,KLogFileName);
	iDb=CContactDatabase::ReplaceL(KDbFileName);
	iRandomGenerator=CRandomContactGenerator::NewL();
	iRandomGenerator->SetDbL(*iDb);
	AddContactsL();
	}

void CTestConductor::AddContactL(TInt aBitwiseFilterType)
	{
	test.Printf(_L("Adding Contact"));
	iRandomGenerator->AddTypicalContactForFilterL(aBitwiseFilterType);
	iTotalContacts++;
	}

void CTestConductor::AddContactsThatMatchFilterL(TInt aNumberOfContacts, TInt aFilter)
	{
	test.Printf(_L("Adding %d contacts which match filter 0x%X : "), aNumberOfContacts, aFilter);
	TInt counter = 0;
	for (counter = 0; counter < aNumberOfContacts; counter++)
		{
		if(aFilter & CContactDatabase::EWirelessVillage)
			{
			iRandomGenerator->AddTypicalContactForFilterL(aFilter, KUidContactFieldVCardMapWV);
			}
		else
			{
			iRandomGenerator->AddTypicalContactForFilterL(aFilter);
			}
		iTotalContacts++;
		}
	test.Printf(_L("Done\n"));
	}

void CTestConductor::AddContactsL()
	{
	iTotalContacts=0;
	test.Printf(_L("Adding \n"));

	AddContactsThatMatchFilterL(KNumSmsContacts, CContactDatabase::ESmsable);
	AddContactsThatMatchFilterL(KNumWorkEmailContacts, CContactDatabase::EMailable);
	AddContactsThatMatchFilterL(KNumHomeEmailContacts, CContactDatabase::EMailable | CContactDatabase::EHome);
	AddContactsThatMatchFilterL(KNumLandlineContacts, CContactDatabase::ELandLine);
	AddContactsThatMatchFilterL(KNumFaxContacts, CContactDatabase::EFaxable);
	AddContactsThatMatchFilterL(KNumRingTone, CContactDatabase::ERingTone);
	AddContactsThatMatchFilterL(KNumVoiceDial, CContactDatabase::EVoiceDial);
	AddContactsThatMatchFilterL(KNumIMProtocolWV, /*CContactDatabase::EIMAddress |*/ CContactDatabase::EWirelessVillage);	
	}

void CTestConductor::RunTestsL()
	{
	CViewTester* tester=CViewTester::NewL(*iLog,*iDb,this);
	CleanupStack::PushL(tester);
	CActiveScheduler::Start();
	CleanupStack::Pop(tester);

	User::LeaveIfError(iTestError);
	}


void CTestConductor::SetTestError(TInt aTestError)
	{
	iTestError = aTestError;
	}


//
// CViewTester.
//

CViewTester* CViewTester::NewL(CLog& aLog,CContactDatabase& aDb,CTestConductor* aTestConductor)
	{
	CViewTester* self=new(ELeave) CViewTester(aLog,aDb,aTestConductor);
	CleanupStack::PushL(self);
	self->ConstructL();
	CleanupStack::Pop();
	return self;
	}

CViewTester::~CViewTester()
	{
	iVoiceDialView->Close(*this);
	iRingToneView->Close(*this);
	iLandlineFilterView->Close(*this);
	iLandlineFilterView2Fields->Close(*this);
	iSmsFilterView->Close(*this);
	iEmailFilterView->Close(*this);
	iFaxFilterView->Close(*this);
	iPhonableFilterView->Close(*this);
	iWorkFilterView->Close(*this);
	iHomeFilterView->Close(*this);
	iUnfilteredView->Close(*this);
	iWirelessVillageView->Close(*this);
	iIMAddressView->Close(*this);
	iOOMIMAddressView->Close(*this);
	iNamedRemoteView->Close(*this);
	iSortOrder_1.Close();
	iSortOrder_2.Close();
	delete iTextDef;
	}

CViewTester::CViewTester(CLog& aLog,CContactDatabase& aDb,CTestConductor* aTestConductor)
	: CActive(EPriorityStandard),iLog(aLog),iDb(aDb),iCurrentTest(-1),iTestConductor(aTestConductor)
	{
	CActiveScheduler::Add(this);
	}

void CViewTester::ConstructL()
	{
	iSortOrder_1.AppendL(KUidContactFieldGivenName);
	iSortOrder_1.AppendL(KUidContactFieldFamilyName);
	iSortOrder_1.AppendL(KUidContactFieldCompanyName);

	iTextDef=CContactTextDef::NewL();
	iTextDef->AppendL(TContactTextDefItem(KUidContactFieldGivenName,KTextDefSeparator));
	iTextDef->AppendL(TContactTextDefItem(KUidContactFieldFamilyName,KTextDefSeparator));
	iTextDef->AppendL(TContactTextDefItem(KUidContactFieldCompanyName,KTextDefSeparator));

	iSortOrder_2.AppendL(KUidContactFieldCompanyName);
	iSortOrder_2.AppendL(KUidContactFieldFamilyName);
	iSortOrder_2.AppendL(KUidContactFieldGivenName);

	NextTest();
	}

void CViewTester::PrintTimeTaken(TInt aTime)
	{
	iScratchBuf.Format(_L("**  Test Took [%ds%dus]  **\n"), aTime/1000000, aTime%1000000);
	iLog.LogLine(iScratchBuf);
	//TBuf<100> buffer;
	//buffer.Format(_L("  Test Took [%ds%dus]\n"), aProfile.iTime/1000000, aProfile.iTime%1000000);
	//iLog.LogLine(buffer);
	}


void CViewTester::PrintTotalTimeTaken()
	{
	TInt counter;
	TInt totalTime = 0;

	for (counter = 0; counter < ENumTests; counter++)
		{
		totalTime += iTimeForTestsToComplete[counter];
		}

	iScratchBuf.Format(_L("**  Total amount of time taken [%ds%dus]  **"), totalTime/1000000, totalTime%1000000);
	iLog.LogLine(iScratchBuf);

	}

CContactFilteredView* CViewTester::CreateFilterViewL()
	{
	CCntTest::ProfileReset(0,1);
	CCntTest::ProfileStart(0);
	return CContactFilteredView::NewL(*this,iDb,*iNamedRemoteView, iFilter);
	}

void CViewTester::RunL()
	{
	switch (iCurrentTest)
		{
		case ECreateLocalView:
			iLog.LogLine(_L("=== Create Name Remote local view"));
			CCntTest::ProfileReset(0,1);
			CCntTest::ProfileStart(0);
			iNamedRemoteView=CContactNamedRemoteView::NewL(*this,KRemoteViewName,iDb,iSortOrder_1,EContactsOnly);
			break;
		case EExerciseLocalView:
			iLog.LogLine(_L("=== Exercise local view"));
			ExceriseViewL(*iNamedRemoteView);
			NextTest();
			break;
		case ECreateLandlineFilterView:
			{
			iLog.LogLine(_L("=== Create Land line Filter View {CContactFilteredView}"));
			iFilter = CContactDatabase::ELandLine;
			iLandlineFilterView = CreateFilterViewL();
			}
			break;
		case ETestLandlineFilterView:
			{
			iLog.LogLine(_L("==== Exercise ETestLandlineFilterView"));
			test(iLandlineFilterView->CountL()==KNumLandlineContacts);
			TestViewIndiciesL(*iLandlineFilterView);
			NextTest();
			}
			break;
		case ECreateSmsFilterView:
			{
			iLog.LogLine(_L("==== Create SMS Filter View"));
			iFilter=CContactDatabase::ESmsable;
			iSmsFilterView=CreateFilterViewL();
			}
			break;
		case ETestSmsFilterView:
			{
			iLog.LogLine(_L("==== Exercise ETestSmsFilterView"));
			test(iSmsFilterView->CountL()==KNumSmsContacts);
			TestViewIndiciesL(*iSmsFilterView);
			NextTest();
			}
			break;
		case ECreateEmailFilterView:
			{
			iLog.LogLine(_L("==== Create Email Filter View"));
			iFilter=CContactDatabase::EMailable;
			iEmailFilterView=CreateFilterViewL();
			}
			break;
		case ETestEmailFilterView:
			{
			iLog.LogLine(_L("==== Exercise ETestEmailFilterView"));
			CCntTest::ProfileReset(0,1);
			CCntTest::ProfileStart(0);
			test(iEmailFilterView->CountL()==KNumWorkEmailContacts+KNumHomeEmailContacts);
			TestViewIndiciesL(*iEmailFilterView);
			NextTest();
			}
			break;
		case ECreateFaxFilterView:
			{
			iLog.LogLine(_L("==== Create Fax Filter View"));
			iFilter=CContactDatabase::EFaxable;
			iFaxFilterView=CreateFilterViewL();
			}
			break;
		case ETestFaxFilterView:
			{
			iLog.LogLine(_L("==== Exercise ETestFaxFilterView"));
			test(iFaxFilterView->CountL()==KNumFaxContacts);
			TestViewIndiciesL(*iFaxFilterView);
			NextTest();
			}
			break;
		case ECreatePhonableFilterView:
			{
			iLog.LogLine(_L("==== Create Phonable Filter View"));
			iFilter=CContactDatabase::EPhonable;
			iPhonableFilterView=CreateFilterViewL();
			}
			break;
		case ECreateWorkFilterView:
			{
			iLog.LogLine(_L("==== Create Work Filter View"));
			iFilter=CContactDatabase::EWork;
			iFilter|=CContactDatabase::EMailable;
			iWorkFilterView=CreateFilterViewL();
			}
			break;
		case ECreateHomeFilterView:
			{
			iLog.LogLine(_L("==== Create Home Filter View"));
			iFilter=CContactDatabase::EHome;
			iFilter|=CContactDatabase::EMailable;
			iHomeFilterView=CreateFilterViewL();
			}
			break;
		case ECreateUnfilteredView:
			{
			iLog.LogLine(_L("==== Create Unfiltered Filter View"));
			iFilter=0;
			iUnfilteredView=CreateFilterViewL();
			}
			break;

		case ECreateRingToneView:
			iLog.LogLine(_L("==== Create Unfiltered Filter View"));
			iFilter = CContactDatabase::ERingTone;
			CCntTest::ProfileReset(0,1);
			CCntTest::ProfileStart(0);
			iRingToneView = CreateFilterViewL();
			break;

		case ECreateVoiceDialView:
			iLog.LogLine(_L("==== Create Unfiltered Filter View"));
			iFilter = CContactDatabase::EVoiceDial;
			iVoiceDialView = CreateFilterViewL();
			break;

		case ECreateWirelessVillageAddressView:
			{
			iLog.LogLine(_L("==== Create WirelessVillage address view"));
			iFilter = CContactDatabase::EWirelessVillage;
			iWirelessVillageView = CreateFilterViewL();
			}
			break;

		case ECreateIMAddressView:
			{
			iLog.LogLine(_L("==== Create IM address view"));
			iFilter = CContactDatabase::EIMAddress;
			iIMAddressView = CreateFilterViewL();
			}
			break;

		case ETestPhonableFilterView:
			{
			iLog.LogLine(_L("==== Exercise ETestPhonableFilterView"));
			test(iPhonableFilterView->CountL()==KNumPhonable);
			TestViewIndiciesL(*iPhonableFilterView);
			NextTest();
			}
			break;
		case ETestWorkFilterView:
			{
			iLog.LogLine(_L("==== Exercise ETestWorkFilterView"));
			test(iWorkFilterView->CountL()==KNumWorkEmailContacts);
			TestViewIndiciesL(*iWorkFilterView);
			NextTest();
			}
			break;
		case ETestHomeFilterView:
			{
			iLog.LogLine(_L("==== Exercise ETestHomeFilterView"));
			test(iHomeFilterView->CountL()==KNumHomeEmailContacts);
			TestViewIndiciesL(*iHomeFilterView);
			NextTest();
			}
			break;
		case ETestUnfilteredView:
			{
			iLog.LogLine(_L("==== Exercise EUnfilteredView"));
			test(iUnfilteredView->CountL()==KNumUnfiltered);
			TestViewIndiciesL(*iUnfilteredView);
			NextTest();
			}
			break;

		case ETestRingToneView:
			iLog.LogLine(_L("==== Exercise ETestRingToneView"));
			test(iRingToneView->CountL()==KNumRingTone);
			TestViewIndiciesL(*iRingToneView);
			NextTest();

			break;

		case ETestVoiceDialView:
			iLog.LogLine(_L("==== Exercise ETestVoiceDialView"));
			test( iVoiceDialView->CountL() == KNumVoiceDial );
			TestViewIndiciesL(*iVoiceDialView);
			NextTest();

			break;

		case ETestWirelessVillageAddressView:
		// this should also test the different protocols assigned to the IMAddress for each contact
			{
			iLog.LogLine(_L("==== Exercise WirelessVillageAddressView"));
			TInt numberContacts = iWirelessVillageView->CountL();
			test(numberContacts == KNumIMAddress);
			
			TInt numberWV = 0;

			for(TInt a = 0; a < numberContacts; a++)
				{
				TContactItemId contactID = iWirelessVillageView->AtL(a);
				CContactItem* contact = iDb.ReadContactLC(contactID);	// PUSH
				CContactItemFieldSet& fieldSet = contact->CardFields();
				CContactItemField& field = fieldSet[contact->CardFields().Find(KUidContactFieldIMAddress)];
				TFieldType imProtocol = field.ContentType().FieldType(1);

				if(imProtocol == KUidContactFieldVCardMapWV)
					numberWV++;
				CleanupStack::PopAndDestroy();							// contact
				}
			test(numberWV == KNumIMProtocolWV);

			TestViewIndiciesL(*iWirelessVillageView);
			NextTest();
			}
			break;

		case ETestIMAddressView:
			{
			iLog.LogLine(_L("==== Exercise IMAddressView"));
			test(iIMAddressView->CountL()==KNumIMAddress);
			TestViewIndiciesL(*iIMAddressView);
			NextTest();
			}
			break;

		case ETestRemoveContacts:
			{
			iLog.LogLine(_L("==== Remove Some Contacts"));
			TContactItemId deletedId = iPhonableFilterView->AtL(10);
			iDb.DeleteContactL(deletedId);
			 RDebug::Print(_L("DELETED ID : %d"),deletedId);
			deletedId = iEmailFilterView->AtL(3);
			iDb.DeleteContactL(deletedId);
			RDebug::Print(_L("DELETED ID : %d"),deletedId);
			deletedId = iFaxFilterView->AtL(6);
			iDb.DeleteContactL(deletedId);
			RDebug::Print(_L("DELETED ID : %d"),deletedId);
			iItemEventsExpected=3;
			iItemsEventsRecieved=0;
			}
			break;
		case ETestViewIndiciesAfterRemoval:
			{
			iLog.LogLine(_L("==== Exercise ETestPhonableFilterView after removal"));
			TestViewIndiciesL(*iPhonableFilterView);
			TestViewIndiciesL(*iLandlineFilterView);
			TestViewIndiciesL(*iSmsFilterView);
			TestViewIndiciesL(*iFaxFilterView);
			TestViewIndiciesL(*iEmailFilterView);
			NextTest();
			}
			break;
		case ETestAddContacts:
			{
			iLog.LogLine(_L("==== Add some Contacts"));
			iTestConductor->AddContactL(CContactDatabase::ELandLine);
			iTestConductor->AddContactL(CContactDatabase::EMailable);
			iTestConductor->AddContactL(CContactDatabase::ESmsable);
			iTestConductor->AddContactL(CContactDatabase::EFaxable);
			iItemEventsExpected=4;
			iItemsEventsRecieved=0;
	//		NextTest(); //wait until notitifications has gone through the system
			}
			break;
		case ETestViewIndiciesAfterAddition:
			{
			iLog.LogLine(_L("==== Exercise ETestPhonableFilterView after addition"));
			TestViewIndiciesL(*iPhonableFilterView);
			TestViewIndiciesL(*iLandlineFilterView);
			TestViewIndiciesL(*iSmsFilterView);
			TestViewIndiciesL(*iFaxFilterView);
			TestViewIndiciesL(*iEmailFilterView);
			NextTest();
			}
			break;
		case EAllViewsOutOfBoundsAccess:
			{
			//Views depend on their underlying views being in a good state, however
			//as some base views are potentially in other processes they must be resistant
			//to out of date views accessesing out of bound members, views, should not
			//panic but should leave with KErrNotFound;
			//local view
			TInt err=0;
			iLog.LogLine(_L("=== Test views for out of bounds access"));
			TInt outCount = iPhonableFilterView->CountL();
			TRAP(err,iPhonableFilterView->AtL(outCount));
			test(err==KErrNotFound);
			TRAP(err,iPhonableFilterView->ContactAtL(outCount));
			test(err==KErrNotFound);
			NextTest();
			}
			break;
		case EOOMIMAddressView:
			{
			++iOOMCounter += iOOMCounter/25; //Increasing step as we go further
			iFilter = CContactDatabase::EIMAddress;

			__UHEAP_SETFAIL(RHeap::EDeterministic,iOOMCounter);
			TRAPD(ret, iOOMIMAddressView = CContactFilteredView::NewL(*this,iDb,*iNamedRemoteView, iFilter));

			if (ret==KErrNone) 
				{
				break; //Wait for asynchronous notification in HandleContactViewEvent
				}

			if (ret != KErrNoMemory)
				{
				_LIT(KOMMFail, "OOM Test failed: Leave reason is not KErrNoMemory: %d");
				test.Printf(KOMMFail, ret);
				User::Leave(KErrAbort);
				}
			__UHEAP_RESET;
		
			test.Printf(_L("KErrNoMemory at step %5d"), iOOMCounter);
			TRequestStatus *pS=&iStatus;
			User::RequestComplete(pS,KErrNone);
			SetActive();
			}
			break;

		case EOOMDeleteIMAddressView:
			{
			iOOMIMAddressView->Close(*this);
			iCurrentTest = EOOMIMAddressView; //Back to OOM 
			TRequestStatus *pS=&iStatus;
			User::RequestComplete(pS,KErrNone);
			SetActive();
			}
			break;
			
 		case ECreateLandLineFilterView2Fields:
 			{
 			// For DEF077467. Propagated from DEF074316.
 			// Ensure hint field is set to landline when there's < 3 fields but one of them is voice
 			// One contact should appear in the filtered view 
 			// Clear the database first
 			while(iDb.CountL() !=0)
 				{
 				iDb.DeleteContactL((*iDb.SortedItemsL())[0]);
 				}
 			iFilterViewSavedId = KErrNotFound;
 			// Loop adds 2 contacts with 2 fields, Hint field should be set with ELandLine
 			// first time through loop and hence should appear in the filtered view
 			// Second time through the loop still 2 fields but not voice and hence will not
 			// appear in the filtered view
 			// Verify the correct one is in the view with the contact id
 			for(TInt i=0;i<2;++i)
 				{
 				CContactCard* card=CContactCard::NewLC();
 				CContactItemField* field=CContactItemField::NewLC(KStorageTypeText);
 				field->AddFieldTypeL(KUidContactFieldPhoneNumber);
 				if(i==0)
 					{
 					field->AddFieldTypeL(KUidContactFieldVCardMapVOICE);
 					}
 				else
 					{
 					field->AddFieldTypeL(KUidContactFieldVCardMapPAGER);
 					}
 				field->TextStorage()->SetTextL(_L("02071541222"));
 				card->AddFieldL(*field);
 				CleanupStack::Pop(field);
 				if(i==0)
 					{
 					iFilterViewSavedId = iDb.AddNewContactL(*card);
 					}
 				else
 					{
 					(void)iDb.AddNewContactL(*card);
 					}
 				CleanupStack::PopAndDestroy(card); // card
 				}
 			// Create the filtered view
 			iFilter = CContactDatabase::ESmsable|CContactDatabase::ELandLine;
 			iLandlineFilterView2Fields = CreateFilterViewL();
 			// Event handler kicks to next state where we check result
 			}
 			break;
 			
 		case ETestLandLineFilterView2Fields:
 			{
 			// Should only be one contact in the view even though there's 2 in the database
 			test(iLandlineFilterView2Fields->CountL()==1);
 			TContactItemId id = iLandlineFilterView2Fields->AtL(0);
 			// Make sure it's the one we're expecting
 			test(iFilterViewSavedId == id && iFilterViewSavedId != KErrNotFound);
 			NextTest();
 			}
 			break;
		
		case ENumTests:
			iLog.LogLine(_L("==== Filter View Finished"));
			PrintTotalTimeTaken();
			CActiveScheduler::Stop();
			delete this;
			break;
		default:
			ASSERT(EFalse);
			break;
		}
	}


TInt CViewTester::RunError(TInt aError)
	{
	iTestConductor->SetTestError(aError);

	switch (iCurrentTest)
		{
		case ECreateLocalView: test.Printf(_L("Test failed at step CreateLocalView (%i) with error %i"), iCurrentTest, aError); break;
		case EExerciseLocalView: test.Printf(_L("Test failed at step ExerciseLocalView (%i) with error %i"), iCurrentTest, aError); break;
		case ECreateLandlineFilterView: test.Printf(_L("Test failed at step CreateLandlineFilterView (%i) with error %i"), iCurrentTest, aError); break;
		case ETestLandlineFilterView: test.Printf(_L("Test failed at step TestLandlineFilterView (%i) with error %i"), iCurrentTest, aError); break;
		case ECreateSmsFilterView: test.Printf(_L("Test failed at step CreateSmsFilterView (%i) with error %i"), iCurrentTest, aError); break;
		case ETestSmsFilterView: test.Printf(_L("Test failed at step TestSmsFilterView (%i) with error %i"), iCurrentTest, aError); break;
		case ECreateEmailFilterView: test.Printf(_L("Test failed at step CreateEmailFilterView (%i) with error %i"), iCurrentTest, aError); break;
		case ETestEmailFilterView: test.Printf(_L("Test failed at step TestEmailFilterView (%i) with error %i"), iCurrentTest, aError); break;
		case ECreateFaxFilterView: test.Printf(_L("Test failed at step CreateFaxFilterView (%i) with error %i"), iCurrentTest, aError); break;
		case ETestFaxFilterView: test.Printf(_L("Test failed at step TestFaxFilterView (%i) with error %i"), iCurrentTest, aError); break;
		case ECreatePhonableFilterView: test.Printf(_L("Test failed at step CreatePhonableFilterView (%i) with error %i"), iCurrentTest, aError); break;
		case ECreateWorkFilterView: test.Printf(_L("Test failed at step CreateWorkFilterView (%i) with error %i"), iCurrentTest, aError); break;
		case ECreateHomeFilterView: test.Printf(_L("Test failed at step CreateHomeFilterView (%i) with error %i"), iCurrentTest, aError); break;
		case ECreateUnfilteredView: test.Printf(_L("Test failed at step CreateUnfilteredView (%i) with error %i"), iCurrentTest, aError); break;
		case ECreateRingToneView: test.Printf(_L("Test failed at step CreateRingToneView (%i) with error %i"), iCurrentTest, aError); break;
		case ECreateVoiceDialView: test.Printf(_L("Test failed at step CreateVoiceDialView (%i) with error %i"), iCurrentTest, aError); break;
		case ECreateWirelessVillageAddressView: test.Printf(_L("Test failed at step CreateWirelessVillageAddressView (%i) with error %i"), iCurrentTest, aError); break;
		case ECreateIMAddressView: test.Printf(_L("Test failed at step CreateIMAddressView (%i) with error %i"), iCurrentTest, aError); break;
		case ETestPhonableFilterView: test.Printf(_L("Test failed at step TestPhonableFilterView (%i) with error %i"), iCurrentTest, aError); break;
		case ETestWorkFilterView: test.Printf(_L("Test failed at step TestWorkFilterView (%i) with error %i"), iCurrentTest, aError); break;
		case ETestHomeFilterView: test.Printf(_L("Test failed at step TestHomeFilterView (%i) with error %i"), iCurrentTest, aError); break;
		case ETestUnfilteredView: test.Printf(_L("Test failed at step TestUnfilteredView (%i) with error %i"), iCurrentTest, aError); break;
		case ETestRingToneView: test.Printf(_L("Test failed at step TestRingToneView (%i) with error %i"), iCurrentTest, aError); break;
		case ETestVoiceDialView: test.Printf(_L("Test failed at step TestVoiceDialView (%i) with error %i"), iCurrentTest, aError); break;
		case ETestWirelessVillageAddressView: test.Printf(_L("Test failed at step TestWirelessVillageAddressView (%i) with error %i"), iCurrentTest, aError); break;
		case ETestIMAddressView: test.Printf(_L("Test failed at step TestIMAddressView (%i) with error %i"), iCurrentTest, aError); break;
		case ETestRemoveContacts: test.Printf(_L("Test failed at step TestRemoveContacts (%i) with error %i"), iCurrentTest, aError); break;
		case ETestViewIndiciesAfterRemoval: test.Printf(_L("Test failed at step TestViewIndiciesAfterRemoval (%i) with error %i"), iCurrentTest, aError); break;
		case ETestAddContacts: test.Printf(_L("Test failed at step TestAddContacts (%i) with error %i"), iCurrentTest, aError); break;
		case ETestViewIndiciesAfterAddition: test.Printf(_L("Test failed at step TestViewIndiciesAfterAddition (%i) with error %i"), iCurrentTest, aError); break;
		case EAllViewsOutOfBoundsAccess: test.Printf(_L("Test failed at step AllViewsOutOfBoundsAccess (%i) with error %i"), iCurrentTest, aError); break;
		case EOOMIMAddressView: test.Printf(_L("Test failed at step OOMIMAddressView (%i) with error %i"), iCurrentTest, aError); break;
		case EOOMDeleteIMAddressView: test.Printf(_L("Test failed at step OOMDeleteIMAddressView (%i) with error %i"), iCurrentTest, aError); break;
 		case ECreateLandLineFilterView2Fields: test.Printf(_L("Test failed at step CreateLandlineFilterView (%i) with error %i"), iCurrentTest, aError); break;
 		case ETestLandLineFilterView2Fields: test.Printf(_L("Test failed at step TestLandlineFilterView (%i) with error %i"), iCurrentTest, aError); break;

		case ENumTests: test.Printf(_L("Test failed at step NumTests (%i) with error %i"), iCurrentTest, aError); break;

		default: test.Printf(_L("Test failed at step %i with error %i"), iCurrentTest, aError); break;
		}

	CActiveScheduler::Stop();
	return KErrNone;
	}


// Should always return true for a correct implementation of
// CContactViewBase
TBool CViewTester::ContactIdsEqualL(const CContactViewBase& aView, TInt aIndex)
  {
  TContactItemId viewId = aView.AtL(aIndex);
  //RDebug::Print(_L("UnderLyingView viewId : %d"),viewId);
  TContactItemId contactId = aView.ContactAtL(aIndex).Id();
  //RDebug::Print(_L("Sub View Id contactId : %d"),contactId);
  return (contactId == viewId);
  }

void CViewTester::TestViewIndiciesL(const CContactViewBase& aView)
	{
	for (TInt i=0; i < aView.CountL(); ++i)
	  {
	  test(ContactIdsEqualL(aView,i)!=EFalse);
	  }
	}
/*
CContactFilteredView* filteredView = ...;
for (TInt i=0; i < filteredView->CountL(); ++i)
  {
  ASSERT(ContactIdsEqual(*filteredView, i);
  }*/

void CViewTester::HandleViewCreation(const CContactViewBase& aView, const TContactViewEvent& aEvent, CContactViewBase* aPtrView)
	{
	test(aPtrView==&aView);
	test(aEvent.iEventType==TContactViewEvent::EReady);
	CCntTest::ProfileEnd(0);
	CCntTest::ProfileResult(&iProfile,0,1);
	iTimeForTestsToComplete[iCurrentTest] = iProfile.iTime;
	PrintTimeTaken(iProfile.iTime);
	}


void CViewTester::HandleContactViewEvent(const CContactViewBase& aView,const TContactViewEvent& aEvent)
	{
	switch (iCurrentTest)
		{
		case ECreateLocalView:
			HandleViewCreation( aView, aEvent, iNamedRemoteView );
			break;
		case EExerciseLocalView:
			test(ETrue);
			break;
		case ECreateLandlineFilterView:
			HandleViewCreation( aView, aEvent, iLandlineFilterView );
			break;
		case ETestLandlineFilterView:
			test(EFalse);
			break;
		case ECreateSmsFilterView:
			HandleViewCreation( aView, aEvent, iSmsFilterView );
			break;
		case ETestSmsFilterView:
			test(EFalse);
			break;
		case ECreateEmailFilterView:
			HandleViewCreation( aView, aEvent, iEmailFilterView );
			break;
		case ETestEmailFilterView:
			test(EFalse);
			break;
		case ECreateFaxFilterView:
			HandleViewCreation( aView, aEvent, iFaxFilterView );
			break;
		case ETestFaxFilterView:
			test(EFalse);
			break;
		case ECreatePhonableFilterView:
			HandleViewCreation( aView, aEvent, iPhonableFilterView );
			break;
		case ECreateWorkFilterView:
			HandleViewCreation( aView, aEvent, iWorkFilterView );
			break;
		case ECreateHomeFilterView:
			HandleViewCreation( aView, aEvent, iHomeFilterView );
			break;
		case ECreateUnfilteredView:
			HandleViewCreation( aView, aEvent, iUnfilteredView );
			break;

		case ECreateRingToneView:
			HandleViewCreation( aView, aEvent, iRingToneView );
			break;

		case ECreateVoiceDialView:
			HandleViewCreation( aView, aEvent, iVoiceDialView );
			break;

		case ECreateWirelessVillageAddressView:
			HandleViewCreation( aView, aEvent, iWirelessVillageView );
			break;

		case ECreateIMAddressView:
			HandleViewCreation( aView, aEvent, iIMAddressView );
			break;

		case ETestPhonableFilterView:
			test(EFalse);
			break;
		case ETestRemoveContacts:
			{
			if(aEvent.iEventType==TContactViewEvent::EItemRemoved)
				{
				iItemsEventsRecieved++;
				if(iItemEventsExpected==iItemsEventsRecieved)
					{
					NextTest();
					}
				}
			return;
			}
		case ETestViewIndiciesAfterRemoval:
			test(aEvent.iEventType==TContactViewEvent::EItemRemoved);
			return;
		case ETestAddContacts:
			{
			if(aEvent.iEventType==TContactViewEvent::EItemAdded)
				{
				iItemsEventsRecieved++;
				if(iItemEventsExpected==iItemsEventsRecieved)
					{
					NextTest();
					}
				}
			return;
			}
		case ETestViewIndiciesAfterAddition:
			test(aEvent.iEventType==TContactViewEvent::EItemAdded);
			return;
		case EAllViewsOutOfBoundsAccess:
			{
			test(EFalse);
			break;
			}
		case EOOMIMAddressView:
			{
			__UHEAP_RESET;
			test(iOOMIMAddressView==&aView);
			switch (aEvent.iEventType)
				{
				case TContactViewEvent::EReady:
					test.Printf(_L("OOM: EReady at step %5d\n"), iOOMCounter);
					++iCurrentTest; //Exit OOM loop - goto next test
					break;
				case TContactViewEvent::ESortError:
					test.Printf(_L("OOM: ESortError at step %5d\n"), iOOMCounter);
					break; //Go to EOOMDeleteIMAddressView - we cannot delete view in its callback.
				default:
					test.Printf(_L("OOM: Unexpected notification at step %5d\n"), iOOMCounter);
					test(EFalse); //Unsupported event - fail
				}
			}
			break;

 		case ECreateLandLineFilterView2Fields:
 			{
 			HandleViewCreation( aView, aEvent, iLandlineFilterView2Fields);
 			}
 			break;
			
		case EOOMDeleteIMAddressView: //there should not be any call backs after deletion
		case ENumTests:
		default:
			test(EFalse);
			break;
		}
	if (--iNumNotificationExpected<=0)
		{
		NextTest();
		iNumNotificationExpected=0;
		}
	}

void CViewTester::TestViewNotificationsL(CContactViewBase& /*aView*/)
	{
	//Test just add some contacts for now...
/*	CRandomContactGenerator* generator=CRandomContactGenerator::NewL();
	CleanupStack::PushL(generator);
	generator->SetDbL(iDb);
	for (TInt ii=0;ii<KInitialNumContacts;++ii)
		{
		generator->AddTypicalRandomContactL();
		}
	CleanupStack::PopAndDestroy(); // generator.
*/	}

void CViewTester::NextTest()
	{
	++iCurrentTest;
	TRequestStatus *pS=&iStatus;
	User::RequestComplete(pS,KErrNone);
	SetActive();
	}

void CViewTester::ExceriseViewL(CContactViewBase& aView)
	{
	TContactItemId lastId=0;
	const TInt numItems=aView.CountL();
	for (TInt ii=0;ii<numItems;++ii)
		{
		if (ii==numItems-1)
			{
			lastId=aView.AtL(ii);
			}
		iDb.ReadContactTextDefL(aView.AtL(ii),iScratchBuf,iTextDef);
		iLog.LogLineNoEcho(iScratchBuf);
		iScratchBuf.SetLength(0);
		}

	test(aView.FindL(lastId)==numItems-1);
	}

void CViewTester::DoCancel()
	{
	}

void doMainL()
	{
	__UHEAP_MARK;
	CTestConductor* testConductor = NULL;
	testConductor = CTestConductor::NewL();
	CleanupStack::PushL( testConductor );
	CleanupStack::PopAndDestroy( testConductor );
	__UHEAP_MARKEND;
	}
//
// Main.
//


/**

@SYMTestCaseID     PIM-T-FILTERVIEW-0001

*/

GLDEF_C TInt E32Main()
	{
	__UHEAP_MARK;
	test.Start(_L("@SYMTESTCaseID:PIM-T-FILTERVIEW-0001 "));

	CActiveScheduler* scheduler=new CActiveScheduler;
	if (scheduler)
		{
		CActiveScheduler::Install(scheduler);
		CTrapCleanup* cleanup=CTrapCleanup::New();
		if (cleanup)
			{
			TRAPD(err,doMainL());
			test(err == KErrNone);
			delete cleanup;
			}
		delete scheduler;
		}
	test.End();
	test.Close();
	__UHEAP_MARKEND;
	return KErrNone;
    }