summaryrefslogtreecommitdiffstats
path: root/plugins/contacts/symbian/contactsmodel/tsrc/t_persistencelayer.cpp
blob: aec98296e294f36a4a7ea8f6fa391c8001c7e491 (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
/*
* 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: 
* Persistence Layer Test module
*
*/



#include <e32test.h>
#include <coreappstest/testserver.h>
#include "persistencelayer.h"
#include "cnttestsynchroniser.h"

_LIT(KTestName, "T_PersistenceLayer"); // Used by Testhelper

#include "testhelpers.h"

#include "nbcnttestlib/nbcnttestlib.h" 

#include "t_persistencelayer.h"
#include "t_cnttestimpldefs.h"


#define RUN_PL
#define RUN_CNT

#define KUidContactFieldCustom1Value		0x101FD209
#define KUidContactFieldCustom2Value		0x101FD20A
const TUid KUidContactFieldCustom1={KUidContactFieldCustom1Value};
const TUid KUidContactFieldCustom2={KUidContactFieldCustom2Value};
_LIT(KCrudTestDbFile, "C:CRUD_TEST");

////////////////////////////////////////////////////////////////////
// This test used to spit out a lot of html files which display
// the contents of contact items. There are a lot of files
// generated in the process which, as this is normally run on the
// overnight build, are not read and just take up disk space.
// Therefore, the call to the contact dumping method has been
// configured out by #ifdefs and needs to be manually re-activated.
//
// So, uncomment the following line to turn on contact dumping.
//#define __T_PERSISTENCELAYER_DUMP_CONTACT__
////////////////////////////////////////////////////////////////////


void CPersistenceTests::AllTestsL()
	{
	FileCreationL();

	IccL();
	DuplicatedFieldUidsL();

	BasicCRUDL();
	ViewDefsInclL();
	ViewDefsMaskL();

	TDriveUnit drv(EDriveC);
	CDesCArray* arr = iCntTestImpl.ListDatabasesL(drv);
	delete arr;

	OomTestL();
	AccessCountL();
	TemplateAccessCountL();
	GroupsL();
	iCntTestImpl.CloseDatabase();
	}
	

void CPersistenceTests::IccL()
	{
	CContactSyncChecker::ResetL();
	CContactItem* iccEntry = CContactICCEntry::NewL(iCntTestImpl.GetSysTemplate());
	PUSH(iccEntry);
	
	TCnt theCnt(iccEntry);
	theCnt[KUidContactFieldGivenName] = _L("Johny");
	theCnt[KUidContactFieldFamilyName] = _L("Herbert");

	iCntTestImpl.CreateL(*iccEntry);

	POPD(iccEntry);
	}


void CPersistenceTests::OomTestL()
	{
	test.Next(_L("ReadLC OOM test"));
	CContactItem* cnt = iCntTestImpl.ReadLC(1, *iMatchAll);
	POPD(cnt);

	TInt ret = 0;
	for(TInt i=1; i < 2000; ++i)
		{
		__UHEAP_SETFAIL(RHeap::EDeterministic, i);
		__UHEAP_MARK;
		TRAP(ret, CContactItem* cnt = iCntTestImpl.ReadLC(1, *iMatchAll);	POPD(cnt); );
		__UHEAP_MARKEND;
		__UHEAP_RESET;

		if (ret == KErrNone)
			{
			break;
			}
		else if (ret != KErrNoMemory)
			{
			TESTVALUE(ret, KErrNoMemory);
			break;
			}
		if (!(i % 100))
			{
			test.Printf(_L("."), i/100);
			}
		}
	test.Printf(_L("\r\n"));
	}



void CPersistenceTests::TemplateAccessCountL()
	{
	test.Next(_L("Template Access Count test"));

	//Create a custom template
	CContactItem* templ = iCntTestImpl.CreateContactCardTemplateLC(_L("UserTemplate"));
	TContactItemId templId = templ->Id();
	POPD(templ); templ = NULL;

	//Read the template from the database
	templ = iCntTestImpl.ReadLC(templId, *iMatchAll);
	TESTVALUE(templ->IsDeleted(), EFalse);


	//Create a contact based on the template
	CContactItem* card = CContactCard::NewLC(templ);
	TCnt theCnt(card);
	theCnt[KUidContactFieldGivenName] += _L("Contact");
	theCnt[KUidContactFieldFamilyName] += _L("Based on");
	theCnt[KUidContactFieldEMail] & KHome += _L("User Template");
	iCntTestImpl.CreateL(*card);

	const TInt createdEntryId =  card->Id();
	POPD(card); card = NULL;
	POPD(templ); templ = NULL;

	//Try to delete the template (it should only be marked and deleted)
	TESTTRAP(iCntTestImpl.DeleteL(templId));

	//Read the template from the database
	//It should work and the template must be marked as deleted
	templ = iCntTestImpl.ReadLC(templId, *iMatchAll);
	TESTTRUE(templ->IsDeleted());
	POPD(templ); templ = NULL;

	//Delete the only contact, based on the user template
	TESTTRAP(iCntTestImpl.DeleteL(createdEntryId));

	//Because the contact was the only thing dependant on the template
	//Template was deleted also
	TRAPD(err, iCntTestImpl.ReadLC(templId, *iMatchAll); POPD(templ););
	TESTVALUE(err, KErrNotFound);
	}

void CPersistenceTests::AccessCountL()
	{
	test.Next(_L("Access Count test"));
	static const TInt KAccessReps = 10;
	TInt i = 0;
	const CContactTemplate& goldenTemplate = iCntTestImpl.GetSysTemplate();

	//Create a contact and set Acces Count to 10.
	CContactItem* card = CContactCard::NewLC(&goldenTemplate);
	TCnt theCnt(card);

	theCnt[KUidContactFieldGivenName] = _L("Giancarlo");
	theCnt[KUidContactFieldFamilyName] = _L("Fisichella");
	theCnt[KUidContactFieldPhoneNumber] & KHome & KVoice & KCell = _L("mo1 39849342");
	theCnt[KUidContactFieldEMail] & KHome = _L("home@email");
	theCnt[KUidContactFieldAddress] & KHome = _L("Home addresssssss");
//	theCnt[KUidContactFieldLocality] & KHome = _L("Rome");

	for(i = 0; i < KAccessReps; ++i )
		{
		card->IncAccessCount();
		}
	TESTTRUE(card->AccessCount() == KAccessReps);;
	iCntTestImpl.CreateL(*card);
	const TInt createdEntryId =  card->Id();
	POPD(card);

	//Check is Access cound was saved
	card = iCntTestImpl.ReadLC(createdEntryId, *iMatchAll);
	TESTTRUE(card->AccessCount() == KAccessReps);;
	POPD(card);	

	//Try to delete entry 10 times
	for(i = 0; i < KAccessReps; ++i )
		{
		iCntTestImpl.DeleteL(createdEntryId);
		}
	//Delete doesn't physically delete an entry, is simply marks is as deleted.

	//Check is Access count was saved, set it back to 0 and update
	card = iCntTestImpl.OpenLC(createdEntryId, *iMatchAll);
	TESTTRUE(card->IsDeleted());
	TESTTRUE(card->AccessCount() == KAccessReps);;
	for(i = 0; i < KAccessReps; ++i )
		{
		card->DecAccessCount();
		}

	iCntTestImpl.UpdateL(*card); //The contact is actually deleted here
	POPD(card);					 //because the access count is now 0

	//The contact should not be there anymore
	TRAPD(err, card = iCntTestImpl.ReadLC(createdEntryId, *iMatchAll));
	TESTTRUE(err == KErrNotFound);
	}


void CPersistenceTests::DuplicatedFieldUidsL()
	{
	CContactItem* cntItem = iCntTestImpl.OpenLC(1, *iMatchAll);
	TCnt theCnt(cntItem);
#ifdef __T_PERSISTENCELAYER_DUMP_CONTACT__
	TESTTRAP(iCntTestImpl.DumpContactL(*cntItem, _L("Before UID field")));
#endif

	theCnt[KUidContactFieldPhoneNumber] & KWork & KPager = _L("New work pager");
	
	iCntTestImpl.UpdateL(*cntItem);

#ifdef __T_PERSISTENCELAYER_DUMP_CONTACT__
	TESTTRAP(iCntTestImpl.DumpContactL(*cntItem, _L("After UID field")));
#endif
	
	TPtrC phone = theCnt[KUidContactFieldPhoneNumber] & KWork & KPager;

	POPD(cntItem);
	}


// Implementation of CPersistenceTests member functions

void CPersistenceTests::FileCreationL()
	{
	test.Next(_L("File creation test"));

#ifndef __SYMBIAN_CNTMODEL_USE_SQLITE__	
	TRAPD(err,iCntTestImpl.CreateDatabaseL(_L("JustAFilename.ext"), ETrue));
	TESTVALUE(err, KErrBadName); //The filename MUST contain a drive name
#endif

	iCntTestImpl.CreateDatabaseL(KCrudTestDbFile, ETrue);
	const CContactTemplate& goldenTemplate = iCntTestImpl.GetSysTemplate();

#ifdef __T_PERSISTENCELAYER_DUMP_CONTACT__
	TESTTRAP(iCntTestImpl.DumpContactL(goldenTemplate, _L("Template")));
#endif

	CContactItem* card = CContactCard::NewLC(&goldenTemplate);
	TBuf<10> uidStr(_L("UidString"));
	card->SetUidStringL(uidStr);
	TCnt theCnt(card);

	theCnt[KUidContactFieldGivenName] = _L("David");
	theCnt[KUidContactFieldFamilyName] = _L("Coulthard");

	theCnt[KUidContactFieldPhoneNumber] & KHome & KVoice & KCell = _L("mo1 39849342");
	theCnt[KUidContactFieldPhoneNumber] & KHome & KVoice & KCell += _L("mo2 34530903495803948503984545");

	theCnt[KUidContactFieldEMail] & KHome = _L("home@email");
	theCnt[KUidContactFieldAddress] & KHome = _L("Home addresssssss");
	theCnt[KUidContactFieldLocality] & KHome = _L("Glazgo");
	theCnt[KUidContactFieldIMAddress] & KHome & KWv = _L("wireless@village");

	//If the code fails on this line, you most likely haven't built the cnttutil
	//test library that create the test version of the system template cntmodel.rsc
	theCnt[KUidContactFieldSecondName] = _L("Second name");

	theCnt[KUidContactFieldCompanyName] = _L("McLaren");
	theCnt[KUidContactFieldJobTitle]	= _L("Driver");
	theCnt[KUidContactFieldPhoneNumber] & KWork & KVoice = _L("work Tel 2342");

	theCnt[KUidContactFieldEMail] & KWork = _L("work@email");
	theCnt[KUidContactFieldUrl] & KWork = _L("sdfsd");
	theCnt[KUidContactFieldAddress] & KWork = _L("afas df asd");
	theCnt[KUidContactFieldLocality] & KWork = _L("sdfsd");

	theCnt[KUidContactFieldCustom1] = _L("Filterable 1");
	theCnt[KUidContactFieldCustom2] = _L("Filterable 2");

	theCnt[KUidContactFieldGivenNamePronunciation] = _L("first name re");
	theCnt[KUidContactFieldFamilyNamePronunciation] = _L("Last name re");
	
	theCnt[KUidContactFieldSIPID] & KPoc  = _L("SIP POC");
	theCnt[KUidContactFieldSIPID] & KSwis = _L("SIP SWIS");
	theCnt[KUidContactFieldSIPID] & KVoip = _L("SIP VOIP");

#ifdef __T_PERSISTENCELAYER_DUMP_CONTACT__
	TESTTRAP(iCntTestImpl.DumpContactL(*card, _L("Before Creation")));
#endif
	iCntTestImpl.CreateL(*card);
	POPD(card);

	card = CContactCard::NewLC(&goldenTemplate);
	theCnt[KUidContactFieldGivenName] = _L("Elton");
	theCnt[KUidContactFieldFamilyName] = _L("John");
	theCnt[KUidContactFieldCompanyName] = _L("EMI");
	theCnt[KUidContactFieldJobTitle]	= _L("Singer");
	theCnt[KUidContactFieldPhoneNumber] & KHome & KVoice & KCell = _L("tel. 39849342");
	iCntTestImpl.CreateL(*card);
	POPD(card);
	
	//Create a group
	CContactGroup* group1 = iCntTestImpl.CreateGroupLC(_L("MyGroup1"));
	POPD(group1);
	}

void CPersistenceTests::ViewDefsMaskL()
	{
	test.Next(_L("View Definition with Masks"));

#ifdef __T_PERSISTENCELAYER_DUMP_CONTACT__
	TESTTRAP(iCntTestImpl.PrintCntForViewL(_L("Full"), *iMatchAll));
#endif

	//ViewDefinition with masking of normal fields
	CContactItemViewDef* viewDef0 = CContactItemViewDef::NewLC( CContactItemViewDef::EMaskFields,
																CContactItemViewDef::EIncludeHiddenFields);	

#ifdef __T_PERSISTENCELAYER_DUMP_CONTACT__
	TESTTRAP(iCntTestImpl.PrintCntForViewL(_L("MaskFields_nothing"), *viewDef0));
#endif

	viewDef0->AddL(KUidContactFieldGivenName);
#ifdef __T_PERSISTENCELAYER_DUMP_CONTACT__
	TESTTRAP(iCntTestImpl.PrintCntForViewL(_L("Mask_GivenName"), *viewDef0));
#endif

	  viewDef0->AddL(KUidContactFieldPhoneNumber);
#ifdef __T_PERSISTENCELAYER_DUMP_CONTACT__
	  TESTTRAP(iCntTestImpl.PrintCntForViewL(_L("Mask_GNameAndPhones"), *viewDef0));
#endif

		viewDef0->AddL(KUidContactFieldEMail);
#ifdef __T_PERSISTENCELAYER_DUMP_CONTACT__
		TESTTRAP(iCntTestImpl.PrintCntForViewL(_L("Mask_GNamePhonesAndEmails"), *viewDef0));
#endif

		viewDef0->Remove(2);
	  viewDef0->Remove(1);
	viewDef0->Remove(0);

	viewDef0->AddL(KHome);
#ifdef __T_PERSISTENCELAYER_DUMP_CONTACT__
	TESTTRAP(iCntTestImpl.PrintCntForViewL(_L("Mask_Home"), *viewDef0));
#endif
	viewDef0->Remove(0);

	POPD(viewDef0);

		
	//ViewDefinition with masking of hidden fields
	CContactItemViewDef* viewDef1 = CContactItemViewDef::NewLC( CContactItemViewDef::EIncludeFields,
																CContactItemViewDef::EMaskHiddenFields);

	viewDef1->AddL(KUidContactFieldGivenName);
#ifdef __T_PERSISTENCELAYER_DUMP_CONTACT__
	TESTTRAP(iCntTestImpl.PrintCntForViewL(_L("OnlyGivenName_noH"), *viewDef1));
#endif
	viewDef1->Remove(0);

	viewDef1->AddL(KUidContactFieldPicture);
#ifdef __T_PERSISTENCELAYER_DUMP_CONTACT__
	TESTTRAP(iCntTestImpl.PrintCntForViewL(_L("noH_Photo"), *viewDef1));
#endif
	viewDef1->Remove(0);
	POPD(viewDef1);

	//todo: add more testing for hidden fields.
	}



void CPersistenceTests::ViewDefsInclL()
	{
	test.Next(_L("View Definition with Inclusions"));

	CContactItem* cntItem = NULL;

	//ViewDefinition with inclusion
	CContactItemViewDef* viewDef1 = CContactItemViewDef::NewLC(CContactItemViewDef::EIncludeFields,CContactItemViewDef::EIncludeHiddenFields);

	cntItem = iCntTestImpl.ReadMinimalLC(1);
	
#ifdef __T_PERSISTENCELAYER_DUMP_CONTACT__
	TESTTRAP(iCntTestImpl.DumpContactL(*cntItem, _L("Minimal")));
#endif

	POPD(cntItem);

#ifdef __T_PERSISTENCELAYER_DUMP_CONTACT__
	TESTTRAP(iCntTestImpl.PrintCntForViewL(_L("Full"), *iMatchAll));
#endif

	viewDef1->AddL(KUidContactFieldPhoneNumber);
#ifdef __T_PERSISTENCELAYER_DUMP_CONTACT__
	TESTTRAP(iCntTestImpl.PrintCntForViewL(_L("OnlyPhones"), *viewDef1));
#endif
	viewDef1->Remove(0);

	viewDef1->AddL(KUidContactFieldAddress);
#ifdef __T_PERSISTENCELAYER_DUMP_CONTACT__
	TESTTRAP(iCntTestImpl.PrintCntForViewL(_L("OnlyAddress"), *viewDef1));
#endif
	viewDef1->Remove(0);

	viewDef1->AddL(KUidContactFieldGivenName);
#ifdef __T_PERSISTENCELAYER_DUMP_CONTACT__
	TESTTRAP(iCntTestImpl.PrintCntForViewL(_L("OnlyGivenName"), *viewDef1));
#endif
	viewDef1->Remove(0);

	viewDef1->AddL(KUidContactFieldEMail);
#ifdef __T_PERSISTENCELAYER_DUMP_CONTACT__
	TESTTRAP(iCntTestImpl.PrintCntForViewL(_L("OnlyEmails"), *viewDef1));
#endif
	  viewDef1->AddL(KUidContactFieldPhoneNumber);
#ifdef __T_PERSISTENCELAYER_DUMP_CONTACT__
	  TESTTRAP(iCntTestImpl.PrintCntForViewL(_L("Emails_and_Phones"), *viewDef1));
#endif
		viewDef1->AddL(KUidContactFieldFamilyName);
#ifdef __T_PERSISTENCELAYER_DUMP_CONTACT__
		TESTTRAP(iCntTestImpl.PrintCntForViewL(_L("EmailsPhonesAndFamilyName"), *viewDef1));
#endif
		viewDef1->Remove(2);
	  viewDef1->Remove(1);
	viewDef1->Remove(0);

	viewDef1->AddL(KUidContactFieldBirthday);
#ifdef __T_PERSISTENCELAYER_DUMP_CONTACT__
	TESTTRAP(iCntTestImpl.PrintCntForViewL(_L("OnlyBirthday"), *viewDef1));
#endif
	viewDef1->Remove(0);

	viewDef1->AddL(KUidContactFieldPicture);
#ifdef __T_PERSISTENCELAYER_DUMP_CONTACT__
	TESTTRAP(iCntTestImpl.PrintCntForViewL(_L("OnlyPicture"), *viewDef1));
#endif
	viewDef1->Remove(0);


	POPD(viewDef1);
	}



void CPersistenceTests::GroupsL()
	{
	test.Next(_L("Groups testing"));
	
	//Create a new group
	CContactGroup* group1 = iCntTestImpl.CreateGroupLC(_L("NewGroupLabel"));				//>>>
	const TContactItemId newGroupID = group1->Id();
#ifdef __T_PERSISTENCELAYER_DUMP_CONTACT__
	TESTTRAP(iCntTestImpl.DumpContactL(*group1, _L("NewGroup_created")));
#endif
	
	//Add existing contact to the group
	iCntTestImpl.GroupAddAndRemove(*group1, 1, -1);
#ifdef __T_PERSISTENCELAYER_DUMP_CONTACT__
	TESTTRAP(iCntTestImpl.DumpContactL(*group1, _L("GroupWithContAdded")));
#endif
	
	//Add another existing contact to the groups
	iCntTestImpl.GroupAddAndRemove(*group1, 2, -1);
#ifdef __T_PERSISTENCELAYER_DUMP_CONTACT__
	TESTTRAP(iCntTestImpl.DumpContactL(*group1, _L("GroupWithCont2Added")));
#endif
	
	//Add the created group to an existing group
	CContactGroup* group3 = static_cast<CContactGroup*>(iCntTestImpl.ReadLC(3, *iMatchAll));	//>>>
#ifdef __T_PERSISTENCELAYER_DUMP_CONTACT__
	TESTTRAP(iCntTestImpl.DumpContactL(*group3, _L("ExitstingGroup3")));
#endif
	iCntTestImpl.GroupAddAndRemove(*group3, group1->Id(), -1);
	POPD(group3);																	//<<<
	POPD(group1);																	//<<<

	//Print the existing group contents
	group3 = static_cast<CContactGroup*>(iCntTestImpl.ReadLC(3, *iMatchAll));					//>>>
#ifdef __T_PERSISTENCELAYER_DUMP_CONTACT__
	TESTTRAP(iCntTestImpl.DumpContactL(*group3, _L("Group3AddedGroup1")));
#endif
	POPD(group3);																	//<<<

	//Delete one of the contacts, belonging to the newly created group
	iCntTestImpl.DeleteL(2);
	group1 = static_cast<CContactGroup*>(iCntTestImpl.ReadLC(newGroupID, *iMatchAll));			//>>>
#ifdef __T_PERSISTENCELAYER_DUMP_CONTACT__
	TESTTRAP(iCntTestImpl.DumpContactL(*group1, _L("GroupWithContRemoved")));
#endif
	POPD(group1);																	//<<<

	//Delete new group
	iCntTestImpl.DeleteL(newGroupID);
	group3 = static_cast<CContactGroup*>(iCntTestImpl.ReadLC(3, *iMatchAll));					//>>>
#ifdef __T_PERSISTENCELAYER_DUMP_CONTACT__
	TESTTRAP(iCntTestImpl.DumpContactL(*group3, _L("Group3-Group1WasDeleted")));
#endif
	///////////////
	
	TRAPD(err, iCntTestImpl.GroupAddAndRemove(*group3, 238, -1));
	TESTVALUE(err, KErrNotFound);
	TRAP(err, iCntTestImpl.GroupAddAndRemove(*group3, -1, 1257));
	TESTVALUE(err, KErrNotFound);

	POPD(group3);																	//<<<
	}


void CPersistenceTests::BasicCRUDL()
	{
	test.Next(_L("Basic CRUD test"));


	test.Printf(_L("Reading Template\n"));
	CContactItem* cntItem = iCntTestImpl.ReadMinimalLC(0);
#ifdef __T_PERSISTENCELAYER_DUMP_CONTACT__
	TESTTRAP(iCntTestImpl.DumpContactL(*cntItem, _L("GoldenTempl")));
#endif
	POPD(cntItem);

	test.Printf(_L("Reading First contact\n"));
	cntItem = iCntTestImpl.ReadMinimalLC(1);
#ifdef __T_PERSISTENCELAYER_DUMP_CONTACT__
	TESTTRAP(iCntTestImpl.DumpContactL(*cntItem, _L("Contact_1")));
#endif

	TCnt theCnt2(cntItem);

	theCnt2[KUidContactFieldPhoneNumber] & KHome & KVoice & KCell = _L("456456456");
	theCnt2[KUidContactFieldPhoneNumber] & KWork & KVoice = _L("456456456");
	
	//Note that we are trying to create a contact, based on another conact which
	//was read using ReadMinimal API. This API doest read template information
	//So the newly created contact will not be bases on any template 
	//(even not on Golden Template e.i.  TemplateRefId() == 0)
	test.Printf(_L("Create new contact\n"));
	TContactItemId newItemId = 0;
	TESTTRAP(newItemId = iCntTestImpl.CreateL(*cntItem)); 
#ifdef __T_PERSISTENCELAYER_DUMP_CONTACT__
	TESTTRAP(iCntTestImpl.DumpContactL(*cntItem, _L("InMemoryAfterCreation")));
#endif
	POPD(cntItem);

	test.Printf(_L("Reading Created contact\n"));
	cntItem = iCntTestImpl.OpenLC(newItemId, *iMatchAll);
#ifdef __T_PERSISTENCELAYER_DUMP_CONTACT__
	TESTTRAP(iCntTestImpl.DumpContactL(*cntItem, _L("Created_ReadFromDb")));
#endif
	
	TCnt theCnt3(cntItem);
	theCnt3[KUidContactFieldGivenName] = _L("new Given Name");
	theCnt3[KUidContactFieldPhoneNumber] & KWork & KVoice = _L("123123123");
	theCnt3[KUidContactFieldPhoneNumber] & KWork & KVoice & KCell += _L("123123123");
	theCnt3[KUidContactFieldEMail] & KHome = _L("new@home");
	theCnt3[KUidContactFieldAddress] & KHome = _L("new home address");

	test.Printf(_L("Update contact\n"));
#ifdef __T_PERSISTENCELAYER_DUMP_CONTACT__
	TESTTRAP(iCntTestImpl.DumpContactL(*cntItem, _L("BeforeUpdate")));
#endif
	TESTTRAP(iCntTestImpl.UpdateL(*cntItem));
#ifdef __T_PERSISTENCELAYER_DUMP_CONTACT__
	TESTTRAP(iCntTestImpl.DumpContactL(*cntItem, _L("AfterUpdate")));
#endif
	POPD(cntItem);

	test.Printf(_L("Reading Updated contact\n"));
	cntItem = iCntTestImpl.ReadMinimalLC(newItemId);
#ifdef __T_PERSISTENCELAYER_DUMP_CONTACT__
	TESTTRAP(iCntTestImpl.DumpContactL(*cntItem, _L("Updated_ReadFromDb")));
#endif
	
	iCntTestImpl.DeleteL(cntItem->Id()); 
	
	POPD(cntItem);
	}


//Persistence test construction and destruction
CPersistenceTests* CPersistenceTests::NewLC (CCntTestImplementor& aCntTestImpl)
	{
	CPersistenceTests* self = new (ELeave) CPersistenceTests(aCntTestImpl);
	CleanupStack::PushL(self);

	self->ConstructL();

	return self;
	}

void CPersistenceTests::ConstructL()
	{
	iMatchAll = CContactItemViewDef::NewLC(CContactItemViewDef::EIncludeFields,CContactItemViewDef::EIncludeHiddenFields);
	iMatchAll->AddL(KUidContactFieldMatchAll);
	CleanupStack::Pop(iMatchAll);
	}

CPersistenceTests::~CPersistenceTests()
	{
	delete iMatchAll;
	iMatchAll = NULL;
	}

/**

@SYMTestCaseID PIM-T-PERSISTENCELAYER-0001

*/

LOCAL_C void DoTestsL()
	{
    CleanupClosePushL(test);
	CPersistenceTests* cntTester = NULL;
	test.Start(_L("@SYMTestCaseID:PIM-T-PERSISTENCELAYER-0001 Persistency tests"));
	
	RPIMTestServer serv;
	User::LeaveIfError(serv.Connect());

#ifdef RUN_PL

	test.Start(_L("Persistence Layer Tests"));
    __UHEAP_MARK;
	CPlTestImpl* plCntTestImpl = CPlTestImpl::NewLC();
  	cntTester = CPersistenceTests::NewLC(*plCntTestImpl);
	cntTester->AllTestsL();
	test(__NB_failures == 0);

	POPD(cntTester);
	CleanupStack::PopAndDestroy(plCntTestImpl);
	plCntTestImpl = NULL;
	test.End();
    __UHEAP_MARKEND;

#endif 

#ifdef RUN_CNT

	test.Start(_L("Contact model tests"));
    __UHEAP_MARK;
	CCntTestImpl* oldCntTestImpl = CCntTestImpl::NewLC();

	cntTester = CPersistenceTests::NewLC(*oldCntTestImpl);
	cntTester->AllTestsL();
	POPD(cntTester);
	test(__NB_failures == 0);

	CleanupStack::PopAndDestroy(oldCntTestImpl);
	oldCntTestImpl = NULL;
	test.End();
    __UHEAP_MARKEND;
	User::After(1500000);
#endif	
	
	serv.Close();	
	test.End();
    CleanupStack::PopAndDestroy(1);  // test.Close

	}




GLDEF_C TInt E32Main()
	{
    // Init
    CTrapCleanup* cleanupStack = CTrapCleanup::New();
    if (!cleanupStack)
        {
        return KErrNoMemory;
        }

    CActiveScheduler* activeScheduler = new CActiveScheduler;
    if (!activeScheduler)
        {
        return KErrNoMemory;
        }
    CActiveScheduler::Install(activeScheduler);

    // Run the tests
    __UHEAP_MARK;
    TRAPD(err, DoTestsL());
	test(err == KErrNone);
	TRAP_IGNORE(CContactDatabase::DeleteDatabaseL(KCrudTestDbFile) );
	__UHEAP_MARKEND;

	
    // Cleanup
    delete activeScheduler;
    delete cleanupStack;
	activeScheduler = NULL;
	cleanupStack = NULL;
	
	return err;
    }