summaryrefslogtreecommitdiffstats
path: root/plugins/contacts/symbian/contactsmodel/tsrc/t_viewsortplugin.cpp
blob: 21bea63d253969beb791c2e636d9c49f3bb31f6e (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
/*
* 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 <s32file.h>
#include <e32math.h>
#include <cntdb.h>
#include <cntitem.h>
#include <cntfldst.h>
#include <cntview.h>
#include <coreappstest/testserver.h>

_LIT(KTestName, "T_ViewSortPlugin");
#include "testhelpers.h" //defines rtest test;
#include "t_viewsortplugin.h"

LOCAL_D	RFs theFs;

_LIT8(KReverseSortPlugin, "application.vnd.symbian.com/contacts-reverse-sort"); //SimpleSortPlugin DLL Unique name

LOCAL_C void CopyFileL(const TDesC& aFile1, const TDesC& aFile2)
	{
    RPIMTestServer serv;
    User::LeaveIfError(serv.Connect());
    serv.DeleteFileL(aFile2);
    serv.CopyFileL(aFile1, aFile2);
    serv.Close();
	}

// This test harness uses special framework for asynchronous function testing
// The test contains of steps. Every step is presented by a function with a name
// starting with "Step". Predefined step "StepDone" shut the framework down and
// must always be in the end of the Step list

// Step list is the function pointers array:
// All of the steps will be called consiquenlty from RunL() function of framework
void (CTestMachine::*StepFunc[])(void)= 
	{
	&CTestMachine::StepRestoreDatabaseL,
	&CTestMachine::StepNamedRemoteViewsL,
	&CTestMachine::StepFindViewsL,
	&CTestMachine::StepDone
	}; 

_LIT(KTestFile,"c:T_ViewSortPlugin.test");
_LIT(KTestFileBase,"c:T_ViewSortPlugin.base");

#ifndef __SYMBIAN_CNTMODEL_USE_SQLITE__
_LIT(KTestFileBaseFullPath,"C:\\private\\10003A73\\T_ViewSortPlugin.base");
_LIT(KTestFileFullPath,"C:\\private\\10003A73\\T_ViewSortPlugin.test");
#else
_LIT(KTestFileBaseFullPath,"C:\\private\\10003A73\\SQLite__T_ViewSortPlugin.base");
_LIT(KTestFileFullPath,"C:\\private\\10003A73\\SQLite__T_ViewSortPlugin.test");
#endif

const TInt64 KRandomSeed=648;


CTestMachine::CTestMachine() 
	: CTimer(EPriorityStandard), iSeed(KRandomSeed), iStep(-1), iSubStep(0), 
	 iNumOfEvents(0)
	{
	};

void CTestMachine::RunL()
	{
	(this->*StepFunc[iStep])();
	}

TInt CTestMachine::RunError(TInt aErrNum)
	{
	test.Printf(_L("RunError called with %d...\n"), aErrNum);
	return KErrNone;
	}

void CTestMachine::StepRestoreDatabaseL()
	{
	test.Printf(_L("Restoring Database..."));
	delete iDb; iDb = NULL; //Close

	TESTTRAPL(CopyFileL(KTestFileBaseFullPath, KTestFileFullPath));
	//Open
	test.Printf(_L("Opening cntmodel..."));
	iDb=CContactDatabase::OpenL(KTestFile);
	
	NextStep();
	//User::After(1000000);
	test.Printf(_L("done!\n"));
	}


//// Macros for asynchronous operations ////
// These macros is the main part of the framework
// Every step consists of several substeps. Between the steps active object
// is idle and waiting for request to be completed, allowing other object to do their work
// To provide this functionality framework puts hidden switch statement in the RunL 
// fuction of its active object. The switch is actually hidden by these macros:

// Don't be afraid of the macros, use them like this:
// void CTestMachine::StepMy()
//		{
//		BEGIN_ASYNC_TEST
//		WAIT_EVENT(1)
//		WAIT_EVENT(2)
//		...
//		WAIT_EVENT(n)
//		END_ASYNC_TEST
//		}
// and you gonna be alright!

#define BEGIN_ASYNC_TEST	\
	switch(iSubStep)		\
	{						\
	case 0:					\
		{					

#define WAIT_EVENT(num)		\
		iNumOfEvents = 1;	\
		}					\
	break;					\
	case num:				\
		{	

#define WAIT_EVENTS(num,numOfEv)\
		iNumOfEvents = numOfEv;	\
		}					\
	break;					\
	case num:				\
		{	



#define WAIT_TIMER(num,time)\
		iNumOfEvents = 0;   \
		this->After(time);	\
		++iSubStep;			\
		}					\
	break;					\
	case num:				\
		{	

#define END_ASYNC_TEST	\
		NextStep();		\
		}				\
	break;				\
	default:			\
		test.Printf(_L("Test step %d doesn't contain substep %d"), iStep, iSubStep);	\
		User::Leave(KErrAbort);	}

///////////////////////////////////////
/////////////// STEPS /////////////////

void CTestMachine::StepFindViewsL()
	{
	const TContactViewPreferences viewPrefs = TContactViewPreferences(EUnSortedAtEnd | EContactsOnly);
	_LIT(KMansell, 	"Mansell");	

	BEGIN_ASYNC_TEST
		
		/////////////////////////////////////////
		//Actions:	Create a local view with reverse sort plugin
		TESTTRAP(iLocalView = CContactLocalView::NewL(*this, *iDb, iSurnNameSortOrder, viewPrefs, KReverseSortPlugin));
	WAIT_EVENT(1)

		//Expected Result:	Check the order of entries in the view - it must be reverse.
		test.Printf(_L("Local View created:\n"));
		CheckViewOrderL(iLocalView);
		test.Printf(_L("\n\n"));

		/////////////////////////////////////////
		//Actions:	Create a remote view with reverse sort plugin
		TESTTRAP(iRemoteViewReverse = CContactRemoteView::NewL(*this, *iDb, iSurnNameSortOrder, viewPrefs, KReverseSortPlugin));
	WAIT_EVENT(2)

		//Expected Result:	Check the order of entries in the view - it must be reverse.
		test.Printf(_L("Remote View created:\n"));
		CheckViewOrderL(iRemoteViewReverse);
		test.Printf(_L("\n\n"));

		/////////////////////////////////////////
		//Actions:	Create a remote view with default sorting behaviour
		TESTTRAP(iRemoteViewForward = CContactRemoteView::NewL(*this, *iDb, iSurnNameSortOrder, viewPrefs));
	WAIT_EVENT(3)

		//Expected Result:	Check the order of entries in the view - it must be forward
		test.Printf(_L("Remote View created:\n"));
		CheckViewOrderL(iRemoteViewForward, EFalse, EFalse); //do not Print, forward order
		test.Printf(_L("\n\n"));

		/////////////////////////////////////////
		//Actions:	Create a Find view on top of the local view, using "Ma" search string 
		iFindDesArray =new(ELeave)CPtrC16Array(1);
		_LIT(KMa,"Ma");
		iFindDesArray->AppendL(TPtrC(KMa));

		iLocalFindView= CContactFindView::NewL(*iDb,*iLocalView,*this,iFindDesArray);
	WAIT_EVENT(4)

		//Expected Result:	Check the order of entries in the find view - it must be reverse
		test.Printf(_L("LocalFind View created:\n"));
		CheckViewOrderL(iLocalFindView);
		test.Printf(_L("\n\n"));

		/////////////////////////////////////////
		//Actions:	Create a Find view on top of the reverse remote view, using "el" search string 
		iFindDesArray->Reset();
		_LIT(Kel,"el");
		iFindDesArray->AppendL(TPtrC(Kel));
		iRemoteFindView = CContactFindView::NewL(*iDb,*iRemoteViewReverse,*this,iFindDesArray);
	WAIT_EVENT(5)
		
		//Expected Result:	Check the order of entries in the find view - it must be reverse
		test.Printf(_L("RemoteFind View created:\n"));
		CheckViewOrderL(iRemoteFindView);
		test.Printf(_L("\n\n"));

		/////////////////
		//Actions:	Add a new contact to the database, it must have "ma" and "el" substrings in it 
		//          to appear in either find view. Check the sorting in every view.		
		_LIT8(KIrvVCard, "BEGIN:VCARD\r\nVERSION:2.1\r\nN:Mansell;Nigel;11\r\nPensioner\r\nEND:VCARD");
		RDesReadStream stream(KIrvVCard);
		stream.PushL();
		  TBool success(EFalse);
		  CArrayPtr<CContactItem>* contactItems=iDb->ImportContactsL(TUid::Uid(KUidVCardConvDefaultImpl), stream, success, NULL);
		CleanupStack::PopAndDestroy(); // stream
		
		PUSH(contactItems);
		  TESTTRUE(success);
		  test.Printf(_L("%d entry was added created\n"), contactItems->Count());
		  contactItems->ResetAndDestroy(); //Potential memory leak - must be pushed onto the stack with ResetAndDestroy
		POPD(contactItems);

	WAIT_EVENTS(6,5) //Waiting for 5 events to arrive (one from each view) 
		//Note, that if all 5 events won't arrive the code will stuck here
		//It can be solved with another "timeout" active object, but not yet implemented
		
		//Expected Result:	Every view must send an event (5 events in total). 
		//Sort order for all the views except for RemoteForward view must be reverse. 
		//New entry must be inserted in every view.
		test.Printf(_L("Local View:\n"));
		CheckViewOrderL(iLocalView);
		test.Printf(_L("\n\n"));

		test.Printf(_L("Remote View Reverse:\n"));
		CheckViewOrderL(iRemoteViewReverse);
		test.Printf(_L("\n\n"));

		test.Printf(_L("Remote View Forward:\n"));
		CheckViewOrderL(iRemoteViewForward, EFalse, EFalse); //do not Print, forward order
		test.Printf(_L("\n\n"));

		test.Printf(_L("Local Find View:\n"));
		CheckViewOrderL(iLocalFindView);
		test.Printf(_L("\n\n"));

		test.Printf(_L("Remote Find View:\n"));
		CheckViewOrderL(iRemoteFindView);
		test.Printf(_L("\n\n"));
		
		//Searching for Mansell in the DB.
		TContactItemId manselId = SearchInViewL(iLocalFindView, KMansell);
		TESTTRUEL(manselId != 0); //Have we found it?

		//Actions:	Change a newly added contact from "Nigel Mansell" to "Ayrton Senna". 
		CContactItem* mansContact = iDb->OpenContactLX(manselId);
		PUSH(mansContact);
		  CContactItemFieldSet& fieldSet      = mansContact->CardFields();
		  
		  CContactItemField&    givenNameField = fieldSet[fieldSet.Find(KUidContactFieldGivenName)];
		  givenNameField.TextStorage()->SetTextL(_L("Ayrton"));

		  CContactItemField&    familyNameField = fieldSet[fieldSet.Find(KUidContactFieldFamilyName)];
		  familyNameField.TextStorage()->SetTextL(_L("Senna"));

		  CContactItemField&    ordinalNumField = fieldSet[fieldSet.Find(KUidContactFieldAdditionalName)];
		  ordinalNumField.TextStorage()->SetTextL(_L("20")); //Number is used for sorting checking

		iDb->CommitContactL(*mansContact);
		POPD(mansContact);
		CleanupStack::PopAndDestroy(); //record lock

	WAIT_EVENTS(7,10) //Waiting for 10 events to arrive (delele and insert event from each view)
		//Actions:	Check the sorting in every view.
	
		//Expected Result:	10 events should arrive - 5 deletes and 5 inserts. 
		//Sort order for all the views except for RemoteForward view must be reverse. 
		//The entry must be updated in local and remote views and should disappear 
		//from Find views because it doesn't contain substrings "Ma" or "el" anymore.
		test.Printf(_L("Local View:\n"));
		CheckViewOrderL(iLocalView, ETrue);
		test.Printf(_L("\n\n"));

		test.Printf(_L("Remote View Reverse:\n"));
		CheckViewOrderL(iRemoteViewReverse, ETrue);
		test.Printf(_L("\n\n"));

		test.Printf(_L("Remote View Forward:\n"));
		CheckViewOrderL(iRemoteViewForward, EFalse, EFalse); //do not Print, forward order
		test.Printf(_L("\n\n"));
		
		test.Printf(_L("Local Find View:\n"));
		CheckViewOrderL(iLocalFindView, ETrue);
		test.Printf(_L("\n\n"));

		test.Printf(_L("Remote Find View:\n"));
		CheckViewOrderL(iRemoteFindView, ETrue);
		test.Printf(_L("\n\n"));

		//We changed Mansell to Senna - the entry shouldn't appear in either find views
		TContactItemId manselId = SearchInViewL(iLocalFindView, KMansell);
		TESTTRUE(manselId == 0);
		manselId = SearchInViewL(iRemoteFindView, KMansell);
		TESTTRUE(manselId == 0);

	CloseView(iLocalFindView);
	CloseView(iLocalView);
	CloseView(iRemoteFindView);
	CloseView(iRemoteViewReverse);
	CloseView(iRemoteViewForward);

	iFindDesArray->Reset();
	delete iFindDesArray; iFindDesArray = NULL;

	END_ASYNC_TEST
	}


void CTestMachine::StepNamedRemoteViewsL()
	{
	const TContactViewPreferences viewPrefs = TContactViewPreferences(EUnSortedAtEnd | EContactsOnly);
	_LIT(KNamedRemoteView, "NamedRemoteView");

	RContactViewSortOrder theNameSurnSortOrder;
	theNameSurnSortOrder.AppendL(KUidContactFieldGivenName);
	theNameSurnSortOrder.AppendL(KUidContactFieldFamilyName);
	theNameSurnSortOrder.AppendL(KUidContactFieldPrefixName); //Contains order numbers for given name

	BEGIN_ASYNC_TEST
		//Actions:	Create named remote view a reverse sorting plugin. Check sorting order.
		iNamedRemoteView = CContactNamedRemoteView::NewL(*this, KNamedRemoteView, *iDb,iSurnNameSortOrder,viewPrefs, KReverseSortPlugin);

	WAIT_EVENT(1)
		//Expected Result:	Named remote view must be created - sorting order must be reverse.
		test.Printf(_L("Named Remote View:\n"));
		CheckViewOrderL(iNamedRemoteView, ETrue);
		test.Printf(_L("\n\n"));

		//Actions:	Create another instance of the named remote view with the same name, 
		//			but without a sorting plugin this time. Check sorting order.
		TESTTRAP(iNamedRemoteView2 = CContactNamedRemoteView::NewL(*this, KNamedRemoteView, *iDb, iSurnNameSortOrder,viewPrefs));
	WAIT_EVENT(2)

		//Expected Result:	When we create another named remote view with the name of existing view 
		// - another copy of the view will not be created, instead will receive the handle 
		//to existing view. That is why sort order should be as first time - reverse.
		test.Printf(_L("Another Copy of Named Remote View:\n"));
		CheckViewOrderL(iNamedRemoteView2, ETrue);
		test.Printf(_L("\n\n"));

		//Actions:	Change the sort order of the named remote view to sort on 
		//	Name and then Surname of the contacts. Check sorting for both handles of the view.
		iNamedRemoteView2->ChangeSortOrderL(theNameSurnSortOrder);
	WAIT_EVENTS(3,4) //2 EUnavailable and 2 EOrderChanged

		//Expected Result:	Both instances of the view must be sorted in reverse order on 
		//Names and Surnames of the contacts.
		test.Printf(_L("First Copy of Named Remote View:\n"));
		CheckViewOrderL(iNamedRemoteView, ETrue);
		test.Printf(_L("\n\n"));

		test.Printf(_L("Another Copy of Named Remote View:\n"));
		CheckViewOrderL(iNamedRemoteView2, ETrue);
		test.Printf(_L("\n\n"));

		/////////////////////////////////////////
		//Actions:	Create a Find view on top of the first instance of named remote view, 
		//using "Ma" search string 
		iFindDesArray =new(ELeave)CPtrC16Array(1);
		_LIT(KMa,"Ma");
		iFindDesArray->AppendL(TPtrC(KMa));

		iNamedRemoteFindView= CContactFindView::NewL(*iDb,*iNamedRemoteView,*this,iFindDesArray);
	WAIT_EVENT(4)

		//Expected Result:	Check the order of entries in the find view - it must be reverse,
		// sorting on Names and Surnames
		test.Printf(_L("NamedRemoteFind View created:\n"));
		CheckViewOrderL(iNamedRemoteFindView, ETrue);
		test.Printf(_L("\n\n"));

		/////////////////////////////////////////
		//Actions:	Create a Find view on top of the second instance of named remote view, 
		// using "el" search string 
		iFindDesArray->Reset();
		_LIT(Kel,"el");
		iFindDesArray->AppendL(TPtrC(Kel));
		iNamedRemoteFindView2 = CContactFindView::NewL(*iDb,*iNamedRemoteView2,*this,iFindDesArray);
	WAIT_EVENT(5)

		//Expected Result:	Check the order of entries in the find view - it must be reverse, 
		//sorting on Names and Surnames
		test.Printf(_L("NamedRemoteFind View 2 created:\n"));
		CheckViewOrderL(iNamedRemoteFindView2, ETrue);
		test.Printf(_L("\n\n"));
		
		CloseView(iNamedRemoteFindView);
		CloseView(iNamedRemoteFindView2);
		CloseView(iNamedRemoteView);
		CloseView(iNamedRemoteView2);

		iFindDesArray->Reset();
		delete iFindDesArray; iFindDesArray = NULL;

	END_ASYNC_TEST

	theNameSurnSortOrder.Close();
	}

/** Searches the view for a contact with particular surname */
TContactItemId CTestMachine::SearchInViewL(CContactViewBase* aView, const TDesC& aSurname)
	{		
	//Searching for aSurname in the DB.
	for (TInt i=aView->CountL() - 1; i >= 0; --i)
		{
		const CViewContact& theContact = aView->ContactAtL(i);
		const TPtrC& surn = theContact.Field(0);
		if (surn == aSurname)
			return theContact.Id();
		}
	return NULL;
	}


void CTestMachine::CheckViewOrderL(const CContactViewBase* aView, TBool aPrint, TBool aReverseOrder)
	{
	ASSERT(aView);
	const TInt numOfCont = aView->CountL();
	TInt prevOrder = aReverseOrder ? 99 : -1; //used for first comparison
	//99 - More than items in database, -1 less than an index of first item

	for (TInt i=0; i < numOfCont; ++i)
		{
		const CViewContact& theContact = aView->ContactAtL(i);
		const TPtrC& theNum = theContact.Field(2);
		
		const TPtrC& theFamilyName = theContact.Field(0);
		if (aPrint)
			{ //Print only if we asked to
			const TPtrC& theGivenName = theContact.Field(1);
			test.Printf(_L("%S %S, %S\n"), &theNum, &theFamilyName, &theGivenName);
			}

		//Order checking 
		//The number of contact in alphabetically sorted list is given in MiddleName field
		TLex16 strOrder(theNum);
		TInt   currOrder; 
		TESTNOERRL(strOrder.Val(currOrder)) //Convert string to integer

		if (aReverseOrder)
			TESTTRUE(prevOrder > currOrder) //Descending order for reverse sorting
		else
			TESTTRUE(prevOrder < currOrder) //Ascending order for forward sorting

		prevOrder = currOrder;
		}
	}


void CTestMachine::StepOOML()
	{
	//TInt allocCells(0);

	BEGIN_ASYNC_TEST
		if (iOOMCounter % 100 == 0)
			test.Printf(_L("Step: %d...\n"), iOOMCounter);
		++iOOMCounter;

		//__SRVHEAP_SETFAIL(iOOMCounter);
		//allocCells = iAgnServ->_DebugRequestAllocatedCellsL();				//__UHEAP_MARK;
		
		int ret=0;
		//TRAP(ret, DoSomething); 
		
		if (ret == KErrNoMemory)
			{
			//iAgnServ->_DebugSetHeapFail(RHeap::ENone,1);					  //__UHEAP_RESET; 
			//TESTVALUEL(iAgnServ->_DebugRequestAllocatedCellsL(), allocCells); //__UHEAP_MARKEND; 

			--iSubStep; //Stay in the same step
			//Completed(KErrNone);
			}
		else if (ret != KErrNone)
			{
			//__SRVHEAP_RESET;
			TESTNOERRL(ret); //Bad leaving code
			}
		
		iError = NULL;

	WAIT_EVENT(1);
		if (iError == KErrNoMemory)
			{//__UHEAP_MARKEND; //Cannot test, because agenda stores Category in long-living object
			iSubStep -=2; //One steps back
			//__SRVHEAP_RESET;
			}
		else if (iError != KErrNone)
			{
			TESTNOERRL(iError); //Bad leaving code
			}
		
		__UHEAP_RESET;
		//Completed(KErrNone);

	WAIT_EVENT(2);

		test.Printf(_L("OOM test finished successfully on step: %d...\n"), iOOMCounter);

	END_ASYNC_TEST
	}


void CTestMachine::StepDone()
	{
	test.Printf(_L("Shutting down\n"));
	User::After(1000000); // Wait for any server side processes to release the file
    TRAP_IGNORE(CContactDatabase::DeleteDatabaseL(KTestFileBase));
    TRAP_IGNORE(CContactDatabase::DeleteDatabaseL(KTestFile));
	CActiveScheduler::Stop();
	}


void CTestMachine::HandleContactViewEvent(const CContactViewBase& aView,
										  const TContactViewEvent& aEvent)
	{
	//Indentifying the view
	TPtrC viewName(KNullDesC);
	_LIT(KLocalView, "LocalView");
	_LIT(KRemoteViewReverse, "RemoteViewReverse");
	_LIT(KRemoteViewForward, "RemoteViewForward");
	_LIT(KNamedRemoteView, "NamedRemoteView");
	_LIT(KNamedRemoteView2, "NamedRemoteView2");
	_LIT(KLocalFindView, "LocalFindView");
	_LIT(KRemoteFindView, "RemoteFindView");
	_LIT(KNamedRemoteFindView, "NamedRemoteFindView");
	_LIT(KNamedRemoteFindView2, "NamedRemoteFindView2");

	if (&aView == iLocalView)
		viewName.Set(KLocalView);
	else if (&aView == iRemoteViewReverse)
		viewName.Set(KRemoteViewReverse);
	else if (&aView == iRemoteViewForward)
		viewName.Set(KRemoteViewForward);
	else if (&aView == iLocalFindView)
		viewName.Set(KLocalFindView);
	else if (&aView == iRemoteFindView)
		viewName.Set(KRemoteFindView);
	else if (&aView == iNamedRemoteView)
		viewName.Set(KNamedRemoteView);
	else if (&aView == iNamedRemoteView2)
		viewName.Set(KNamedRemoteView2);
	else if (&aView == iNamedRemoteFindView)
		viewName.Set(KNamedRemoteFindView);
	else if (&aView == iNamedRemoteFindView2)
		viewName.Set(KNamedRemoteFindView2);


	TPtrC eventName(KNullDesC);
	_LIT(KEventUnavailable, "Unavailable");
	_LIT(KEventReady, "Ready");
	_LIT(KEventSortOrderChanged, "Sort Order Changed");
	_LIT(KEventSortError, "Sort Error");
	_LIT(KEventServerError, "Server Error");
	_LIT(KEventIndexingError, "Indexing Error");
	_LIT(KEventItemAdded, "Item Added");
	_LIT(KEventItemRemoved, "Item Removed");
	_LIT(KEventGroupChanged, "Group Changed");
	_LIT(KEventUnknown, "unknown ??");

	
	switch (aEvent.iEventType)
		{
	case TContactViewEvent::EUnavailable:
		eventName.Set(KEventUnavailable);
		break;
	case TContactViewEvent::EReady:
		eventName.Set(KEventReady);
		break;
	case TContactViewEvent::ESortOrderChanged:
		eventName.Set(KEventSortOrderChanged);
		break;
	case TContactViewEvent::ESortError:
		eventName.Set(KEventSortError);
		break;
	case TContactViewEvent::EServerError:
		eventName.Set(KEventServerError);
		break;
	case TContactViewEvent::EIndexingError:
		eventName.Set(KEventIndexingError);
		break;
	case TContactViewEvent::EItemAdded:
		eventName.Set(KEventItemAdded);
		break;
	case TContactViewEvent::EItemRemoved:
		eventName.Set(KEventItemRemoved);
		break;
	case TContactViewEvent::EGroupChanged:
		eventName.Set(KEventGroupChanged);
		break;
	default:
		eventName.Set(KEventUnknown);
		break;
		}

	test.Printf(_L("=> %S has sent an event: %S\n"), &viewName, &eventName);

//	iError = aError;

	
	if (--iNumOfEvents == 0) 
		{ //We have received all planned events
		++iSubStep;

		TRequestStatus *pS=&iStatus;
		User::RequestComplete(pS,KErrNone);
		SetActive();
		}
	else if (iNumOfEvents < 0)
		{
		TESTTRUE(EFalse);  //Too many events
		}
	}


CTestMachine* CTestMachine::NewLC()
	{
	CTestMachine* self = new (ELeave) CTestMachine;
	PUSH(self);
	self->ConstructL();
	CActiveScheduler::Add(self);
	self->NextStep();
	return self;
	}

void CTestMachine::ConstructL()
	{
	CTimer::ConstructL();

	TESTTRAP(iDb=CContactDatabase::ReplaceL(KTestFileBase));

	RFileReadStream vcard;
	CleanupClosePushL(vcard);
	TESTNOERR(vcard.Open(theFs, _L("z:\\cntvcard\\T_ViewSortPlugin.vcf"), EFileRead));

	TBool success=EFalse;
	CArrayPtr<CContactItem>* contactItems=iDb->ImportContactsL(TUid::Uid(KUidVCardConvDefaultImpl), vcard, success, NULL);
	CleanupStack::PopAndDestroy(); //read stream vcard

	TESTTRUEL(success);

	test.Printf(_L("Sample Database with %d entries created\n"), contactItems->Count());

	contactItems->ResetAndDestroy();
	delete contactItems;

	//Prepare sort order for the views
	iSurnNameSortOrder.AppendL(KUidContactFieldFamilyName);
	iSurnNameSortOrder.AppendL(KUidContactFieldGivenName);
	iSurnNameSortOrder.AppendL(KUidContactFieldAdditionalName); //Contains order number for family name
	}

CTestMachine::~CTestMachine()
	{
	iSurnNameSortOrder.Close();
	//iRemoteSortOrder.Close();

	delete iDb;
	}

void CTestMachine::CloseView(CContactViewBase* aView)
	{
	if (aView)
		aView->Close(*this);
	aView = NULL;
	}


void CTestMachine::NextStep()
	{
	iError = KErrNone; //reset callback error variable
	++iStep;
	iSubStep = 0;

	TRequestStatus *pS=&iStatus;
	User::RequestComplete(pS,KErrNone);
	SetActive();
	}

LOCAL_C void doMainL()
	{
	TESTNOERR(theFs.Connect());
	CleanupClosePushL(theFs);
	CTestMachine* tm = CTestMachine::NewLC();
	CActiveScheduler::Start();
	CleanupStack::PopAndDestroy(tm);
	CleanupStack::PopAndDestroy(); // Close FS
	}


/**

@SYMTestCaseID     PIM-T-VIEWSORTPLUGIN-0001

*/

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

	CActiveScheduler* scheduler=new CActiveScheduler;
	test(scheduler != NULL);
	CActiveScheduler::Install(scheduler);
	CTrapCleanup* theCleanup=CTrapCleanup::New();
	TRAPD(ret,doMainL());	
	test(ret==KErrNone);
	test(__NB_failures == 0); //There were failures before.
	delete theCleanup;	
	delete scheduler;
	test.End();
	test.Close();
	__UHEAP_MARKEND;
	return(KErrNone);
	}




///// Helpers //////
void RandomText(TDes& aText,TInt64& aSeed)
{
	TInt numChar=Math::Rand(aSeed)%aText.MaxLength();
	if(Math::Rand(aSeed)%2)
		{
		for (TInt i=0;i<numChar;++i)
			{
			TInt c=Math::Rand(aSeed)%6;
			c= (c==0)? ' ' : ((Math::Rand(aSeed)%26)+    ((c==2)? 'A' : 'a'));
			aText.Append(c);
			}
		}
	else
		aText=KNullDesC;
}


//Export routine
//static void ExportContactAsVCardL(CContactDatabase& aDatabase, TContactItemId aId, RFs& aFsSession, const TDesC& aVCardFileName)
/*	CContactIdArray* idArray = CContactIdArray::NewLC();
	idArray->AddL(id);
	//
	RFile outfile;
	User::LeaveIfError(outfile.Replace(theFs, theFamilyName, EFileWrite));
	CleanupClosePushL(outfile);
	//
	RFileWriteStream writeStream(outfile);
	CleanupClosePushL(writeStream);
	//	
	const TUid KVersitVCardUid = { KUidVCardConvDefaultImpl };
	iDb->ExportSelectedContactsL(KVersitVCardUid, *idArray, writeStream, CContactDatabase::EDefault);
	writeStream.CommitL();
	CleanupStack::PopAndDestroy(3, idArray); // writeStream, outfile, idArray*/