summaryrefslogtreecommitdiffstats
path: root/plugins/contacts/symbian/contactsmodel/tsrc/t_find.cpp
blob: d6523dce0404a1b35dcc076ad6de318cc2ea733f (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
/*
* 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: 
* Run as part T_DBASE2, tests find
*
*/


#include <e32test.h>
#include <f32file.h>
#include <s32file.h>
#include <s32mem.h>
#include <cntdb.h>
#include <cntitem.h>
#include <cntfield.h>
#include <cntfldst.h>
#include "t_utils.h"
#include "t_dbase2.h"

class CAsyncFinder : public CBase, public MIdleFindObserver
	{
public:
	~CAsyncFinder();
	void ConstructL(CContactDatabase *aContactDatabase, const TDesC& aText,const CContactItemFieldDef *aFieldDef);
	void ConstructL(CContactDatabase *aContactDatabase, const CDesCArray& aFindWords,const CContactTextDef *aTextDef,const TCallBack &aCallBack);
	inline CContactIdArray *TakeContactIds() {return(iFinder->TakeContactIds());};
	inline TInt Error() const {return(iFinder->Error());};
protected:
// from MIdleFindObserver
	void IdleFindCallback();
private:
	CIdleFinder *iFinder;
	};

CAsyncFinder::~CAsyncFinder()
	{
	delete iFinder;
	}

void CAsyncFinder::ConstructL(CContactDatabase *aContactDatabase, const TDesC& aText,const CContactItemFieldDef *aFieldDef)
	{
	iFinder=aContactDatabase->FindAsyncL(aText, aFieldDef, this);
	}

void CAsyncFinder::ConstructL(CContactDatabase *aContactDatabase, const CDesCArray& aFindWords,const CContactTextDef *aTextDef,const TCallBack &aCallBack)
	{
	if (aTextDef)
		iFinder=aContactDatabase->FindInTextDefAsyncL(aFindWords, aTextDef, this, aCallBack);
	else
		iFinder=aContactDatabase->FindInTextDefAsyncL(aFindWords, this, aCallBack);
	}

void CAsyncFinder::IdleFindCallback()
	{
	if (iFinder->IsComplete())
		CActiveScheduler::Stop();
	}

GLDEF_C TBool CompareIdArrays(CContactIdArray* aIds1,CContactIdArray* aIds2)
	{
	if (aIds1->Count()!=aIds2->Count())
		return(EFalse);
	for(TInt loop=0;loop<aIds1->Count();loop++)
		{
		if ((*aIds1)[loop]!=(*aIds2)[loop])
			return(EFalse);
		}
	return(ETrue);
	}

GLDEF_C CContactIdArray* CreateIdArrayLC(TContactItemId *aId1, TContactItemId *aId2=NULL, TContactItemId *aId3=NULL, TContactItemId *aId4=NULL, TContactItemId *aId5=NULL)
	{
	CContactIdArray* ids=CContactIdArray::NewLC();
	if (aId1)
		ids->AddL(*aId1);
	if (aId2)
		ids->AddL(*aId2);
	if (aId3)
		ids->AddL(*aId3);
	if (aId4)
		ids->AddL(*aId4);
	if (aId5)
		ids->AddL(*aId5);
	return(ids);
	}

LOCAL_C CDesCArray *CreateFindWordsL(const TDesC &aFind1,const TDesC &aFind2,const TDesC &aFind3)
	{
	CDesCArray *desCA=new(ELeave) CDesCArrayFlat(5);
	CleanupStack::PushL(desCA);
	if (aFind1.Length()>0)
		desCA->AppendL(aFind1);
	if (aFind2.Length()>0)
		desCA->AppendL(aFind2);
	if (aFind3.Length()>0)
		desCA->AppendL(aFind3);
	CleanupStack::Pop();	// desCA
	return(desCA);
	}

LOCAL_C void CheckResult(CContactIdArray* aIds,CContactIdArray* aResultIds)
	{
	if (aResultIds)
		test(CompareIdArrays(aIds,aResultIds));
	else
		test(aIds->Count()==0);
	}

LOCAL_C void runAsyncFindL(CAsyncFinder *aFinder, CContactIdArray* aResultIds)
	{
	CActiveScheduler::Start();
	CContactIdArray* ids=aFinder->TakeContactIds();
	CheckResult(ids,aResultIds);
	delete ids;
	}

LOCAL_C TInt findWordSplitterL(TAny *aParams)
	{
	SFindInTextDefWordParser *parser=(SFindInTextDefWordParser *)aParams;
	const TText *ptr=parser->iSearchString->Ptr();
	const TText *end=ptr+parser->iSearchString->Length();
	const TText *startOfWord=NULL;
	FOREVER
		{
		if (ptr==end || !TChar(*ptr).IsAlphaDigit())
			{
			if (startOfWord)
				{
				TPtrC addWord(startOfWord,ptr-startOfWord);
				parser->iWordArray->AppendL(addWord);
				startOfWord=NULL;
				}
			if (ptr==end)
				break;
			}
		else if (!startOfWord)
			startOfWord=ptr;
		ptr++;
		}
	return(KErrNone);
	}

GLDEF_C void doDoFindInTextDefTestL(CDesCArray *aFindWords, CContactTextDef *aTextDef, CContactIdArray* aResultIds)
	{
	TCallBack callBack(findWordSplitterL);
	CAsyncFinder *finder=new(ELeave) CAsyncFinder;
	CleanupStack::PushL(finder);
	finder->ConstructL(CntTest->Db(),*aFindWords,aTextDef,callBack);
	runAsyncFindL(finder, aResultIds);
	CleanupStack::PopAndDestroy(); // finder
//
	CContactIdArray* ids=NULL;
	if (aTextDef)
		ids=CntTest->Db()->FindInTextDefLC(*aFindWords,aTextDef,callBack);
	else
		ids=CntTest->Db()->FindInTextDefLC(*aFindWords,callBack);
	CheckResult(ids,aResultIds);
	CleanupStack::PopAndDestroy(); // ids
	}

GLDEF_C void DoFindInTextDefTestL(CDesCArray *aFindWords, CContactTextDef *aTextDef, CContactIdArray* aResultIds)
	{
	CleanupStack::PushL(aFindWords);
	doDoFindInTextDefTestL(aFindWords, aTextDef, aResultIds);
	CContactTextDef* textDef=CContactTextDef::NewLC();
	if (aTextDef)
		{
		for(TInt loop=0;loop<aTextDef->Count();loop++)
			textDef->AppendL((*aTextDef)[loop]);
		textDef->SetFallbackField(aTextDef->FallbackField());
		}
	CntTest->Db()->SetTextDefinitionL(textDef);
	CleanupStack::Pop();	// textDef
	doDoFindInTextDefTestL(aFindWords, NULL, aResultIds);
	CntTest->Db()->SetTextDefinitionL(NULL);
	CleanupStack::Pop(); // aFindWords
	}

GLDEF_C void DoFindInTextDefTestLD(CDesCArray *aFindWords, CContactTextDef *aTextDef, CContactIdArray* aResultIds)
	{
	DoFindInTextDefTestL(aFindWords, aTextDef, aResultIds);
	if (aFindWords->Count()>0)
		{
		CDesCArray *reverseArray=new(ELeave) CDesCArrayFlat(10);
		CleanupStack::PushL(reverseArray);
		for(TInt loop=aFindWords->Count()-1;loop>=0;loop--)
			reverseArray->AppendL((*aFindWords)[loop]);
		DoFindInTextDefTestL(reverseArray, aTextDef, aResultIds);
		CleanupStack::PopAndDestroy();
		}
	delete aFindWords;
	}

GLDEF_C void DoAsyncFindL(const TDesC &aText, CContactItemFieldDef *aFieldDef,CContactIdArray* aResultIds)
	{
	CAsyncFinder *finder=new(ELeave) CAsyncFinder;
	CleanupStack::PushL(finder);
	finder->ConstructL(CntTest->Db(),aText,aFieldDef);
	runAsyncFindL(finder, aResultIds);
	CleanupStack::PopAndDestroy(); // finder
	}

GLDEF_C void DoAsyncFindL(const TDesC &aText, CContactItemFieldDef *aFieldDef, TInt aCount)
	{
	CAsyncFinder *finder=new(ELeave) CAsyncFinder;
	CleanupStack::PushL(finder);
	finder->ConstructL(CntTest->Db(),aText,aFieldDef);
	CActiveScheduler::Start();
	CContactIdArray* ids=finder->TakeContactIds();
	test(ids->Count()==aCount);
	delete ids;
	CleanupStack::PopAndDestroy(); // finder
	}

/**

@SYMTestCaseID     PIM-T-FIND-0001

*/

GLDEF_C void FindAsyncL()
	{
	test.Next(_L("@SYMTestCaseID:PIM-T-FIND-0001 Async search 1"));
//
	TBuf<32> name;
	name.Format(KTestName,7);
	DoAsyncFindL(name,NULL,1);
//
	test.Next(_L("Async search 2"));
	CContactItemFieldDef *fieldDef=new(ELeave) CContactItemFieldDef;
	CleanupStack::PushL(fieldDef);
	fieldDef->AppendL(KUidContactFieldFamilyName);
	DoAsyncFindL(_L("Test Name No"),fieldDef,CntTest->Db()->CountL()-3); // ..empty groups & own card
	CleanupStack::PopAndDestroy(); // fieldDef
//
	test.Next(_L("Async search 3"));
	CContactItemFieldDef *fieldDef2=new(ELeave) CContactItemFieldDef;
	CleanupStack::PushL(fieldDef2);
	fieldDef2->AppendL(KUidContactFieldAddress);
	DoAsyncFindL(_L("Test Name No"),fieldDef2,0);
	CleanupStack::PopAndDestroy(); // fieldDef2
//
#if defined(_DEBUG)
	CAsyncFinder *finder=new(ELeave) CAsyncFinder;
	CleanupStack::PushL(finder);
	TBuf<32> name2;
	name2.Format(KTestName,7);
	finder->ConstructL(CntTest->Db(),name2,NULL);
	__UHEAP_SETFAIL(RHeap::EDeterministic,10);
	CActiveScheduler::Start();
	__UHEAP_RESET;
//
	CContactIdArray* ids=finder->TakeContactIds();
	test(finder->Error()==KErrNoMemory || finder->Error()==KErrNone);
	delete ids;
	CleanupStack::PopAndDestroy(); // finder
#endif
//
	CAsyncFinder *asyncFinder=new(ELeave) CAsyncFinder;
	asyncFinder->ConstructL(CntTest->Db(), _L("a"), NULL);
	delete asyncFinder;	// Checks ids get deleted if not claimed with TakeContactIds()
	}

LOCAL_C TContactItemId AddFindRecordL(const TDesC &aFirstName, const TDesC &aLastName, const TDesC &aCompanyName, const TDesC &aAddress)
	{
	CContactItem *item=CContactCard::NewLC();
	if (aFirstName.Length()>0)
		SetNameL(*item,KUidContactFieldGivenName,KUidContactFieldVCardMapUnusedN,aFirstName,ETrue);
	if (aLastName.Length()>0)
		SetNameL(*item,KUidContactFieldFamilyName,KUidContactFieldVCardMapUnusedN,aLastName,ETrue);
	if (aCompanyName.Length()>0)
		SetNameL(*item,KUidContactFieldCompanyName,KUidContactFieldVCardMapORG,aCompanyName,ETrue);
	if (aAddress.Length()>0)
		SetNameL(*item,KUidContactFieldAddress,KUidContactFieldVCardMapADR,aAddress,ETrue);
	TContactItemId id=CntTest->Db()->AddNewContactL(*item);
	CleanupStack::PopAndDestroy();	// item
	return(id);
	}

GLDEF_C void FindAsyncInTextDefL()
	{
	CContactTextDef* textDef1=CContactTextDef::NewLC();
	textDef1->AppendL(TContactTextDefItem(KUidContactFieldGivenName));
	textDef1->AppendL(TContactTextDefItem(KUidContactFieldFamilyName));
	textDef1->SetFallbackField(KUidContactFieldCompanyName);
	CContactTextDef* textDef2=CContactTextDef::NewLC();
	textDef2->AppendL(TContactTextDefItem(KUidContactFieldGivenName));
	textDef2->AppendL(TContactTextDefItem(KUidContactFieldFamilyName));
	textDef2->AppendL(TContactTextDefItem(KUidContactFieldCompanyName));
	textDef2->SetFallbackField(KUidContactFieldAddress);
//
	TContactItemId wally=AddFindRecordL(_L("Wally"),_L("Wombat"),_L("Wombat Soup Inc"),_L(""));
	TContactItemId vincent=AddFindRecordL(_L("Vincent"),_L("Jones"),_L(""),_L(""));
	TContactItemId wilbert=AddFindRecordL(_L("Wilbert"),_L("Wombat"),_L("Great Ape Software"),_L(""));
	TContactItemId vincent2=AddFindRecordL(_L("Vincent"),_L("Vincent"),_L(""),_L(""));
	TContactItemId death=AddFindRecordL(_L("Black"),_L("De'ath"),_L("Hell"),_L("South of Heaven"));
//
	CContactIdArray* wallyResults=CreateIdArrayLC(&wally);
	CContactIdArray* wombatResults=CreateIdArrayLC(&wally,&wilbert);
	CContactIdArray* vjResults=CreateIdArrayLC(&vincent);
	CContactIdArray* v2Results=CreateIdArrayLC(&vincent2);
	CContactIdArray* vjv2Results=CreateIdArrayLC(&vincent,&vincent2);
	CContactIdArray* deathResults=CreateIdArrayLC(&death);
//	
	CDesCArray *findWords=CreateFindWordsL(_L("Wally"),_L(""),_L(""));
//chrisd: the behaviour of the new code has changed because if you pass a null 
//chrisd: textdef to ER5/ER6 or 6.1 it doesn't find anything
	DoFindInTextDefTestL(findWords, NULL, NULL);	// No text def defined
//
	DoFindInTextDefTestLD(findWords, textDef1, wallyResults);
	findWords=CreateFindWordsL(_L("Wally"),_L("NotFound"),_L(""));
	DoFindInTextDefTestLD(findWords, textDef1, NULL);
	findWords=CreateFindWordsL(_L("Wally"),_L("Wombat"),_L(""));
	DoFindInTextDefTestLD(findWords, textDef1, wallyResults);
	findWords=CreateFindWordsL(_L("Wally"),_L("Wombat"),_L("xxx"));
	DoFindInTextDefTestLD(findWords, textDef1, NULL);
	findWords=CreateFindWordsL(_L("W"),_L("Wombat"),_L(""));
	DoFindInTextDefTestLD(findWords, textDef1, wombatResults);
	findWords=CreateFindWordsL(_L("Wally"),_L("Wally"),_L(""));
	DoFindInTextDefTestLD(findWords, textDef1, NULL);
	findWords=CreateFindWordsL(_L("Wombat"),_L("Wombat"),_L(""));
	DoFindInTextDefTestLD(findWords, textDef1, wallyResults);
	findWords=CreateFindWordsL(_L("Wombat"),_L("Wombat"),_L("Wombat"));
	DoFindInTextDefTestLD(findWords, textDef1, NULL);
	findWords=CreateFindWordsL(_L("Wombat"),_L("Soup"),_L("Inc"));
//chrisd: new code doesn't find anything here and should have - this is
//chrisd: all words are contained in the company field
DoFindInTextDefTestLD(findWords, textDef1, wallyResults);
//
	findWords=CreateFindWordsL(_L("ombat"),_L("oup"),_L("lly"));
	DoFindInTextDefTestLD(findWords, textDef1, NULL);
	findWords=CreateFindWordsL(_L("Wombat"),_L("Soup"),_L("Inc"));
//chrisd: new code doesn't find anything here and should have - this is
//chrisd: all words are contained in the company field
DoFindInTextDefTestLD(findWords, textDef1, wallyResults);
//
	findWords=CreateFindWordsL(_L("Vincent"),_L("J"),_L(""));
	DoFindInTextDefTestLD(findWords, textDef1, vjResults);
	findWords=CreateFindWordsL(_L("Vincent"),_L("V"),_L(""));
	DoFindInTextDefTestLD(findWords, textDef1, v2Results);
	findWords=CreateFindWordsL(_L("Vincent"),_L(""),_L(""));
	DoFindInTextDefTestLD(findWords, textDef1, vjv2Results);

	findWords=CreateFindWordsL(_L("De"),_L("Ath"),_L(""));
//chrisd: new code fails here
//chrisd: last name contains both first & last names (PC PIM syncs both to one field)
DoFindInTextDefTestLD(findWords, textDef1, deathResults);
	findWords=CreateFindWordsL(_L("De"),_L("Ath"),_L("Heaven"));
	DoFindInTextDefTestLD(findWords, textDef1, NULL);
//chrisd: new code fails here
//chrisd: last name contains first and last
findWords=CreateFindWordsL(_L("De"),_L("Ath"),_L("Heaven"));
DoFindInTextDefTestLD(findWords, textDef2, deathResults);
	findWords=CreateFindWordsL(_L("Heaven"),_L(""),_L(""));
//chrisd:one word at the end - partial match - so doesn't need to match
DoFindInTextDefTestLD(findWords, textDef1, NULL);
	findWords=CreateFindWordsL(_L("Heaven"),_L(""),_L(""));
//chrisd:one word at the end - partial match - so doesn't need to match
DoFindInTextDefTestLD(findWords, textDef2, deathResults);
findWords=CreateFindWordsL(_L("of"),_L("South"),_L("Heaven"));
//chrisd:all words are in one field - this doesn't need to match
DoFindInTextDefTestLD(findWords, textDef2, deathResults);
//
	CntTest->DeleteContact(wally);
	CntTest->DeleteContact(vincent);
	CntTest->DeleteContact(wilbert);
	CntTest->DeleteContact(vincent2);
	CntTest->DeleteContact(death);
	CleanupStack::PopAndDestroy(6);	// wallyResults,wombatResults,vjResults,v2Results,deathResults
	CleanupStack::PopAndDestroy(2);	// textDef1,textDef2
	}

GLDEF_C void FindL()
	{
	CContactItemFieldDef* fieldDef=new(ELeave) CContactItemFieldDef;
	CleanupStack::PushL(fieldDef);
	fieldDef->AppendL(KUidContactFieldAddress);
	fieldDef->AppendL(KUidContactFieldFamilyName);
	fieldDef->AppendL(KUidContactFieldGivenName);
	fieldDef->AppendL(KUidContactFieldCompanyName);
	fieldDef->AppendL(KUidContactFieldBirthday);
	fieldDef->AppendL(KUidContactFieldTemplateLabel);
//
	test.Next(_L("Search for full given name"));
	TBuf<16> name;
	name.Format(KTestName,7);
	CContactIdArray* ids=CntTest->Db()->FindLC(name,fieldDef);
	test(ids->Count()==1);
	CleanupStack::PopAndDestroy(); // ids

	test.Next(_L("Search for full given name in only contact groups"));
	// same test but only looking in groups so shouldn't find anything
	name.Format(KTestName,7);
	CntTest->Db()->SetDbViewContactType(KUidContactGroup);
	ids=CntTest->Db()->FindLC(name,fieldDef);
	test(ids->Count()==0); // nothing found.
	CleanupStack::PopAndDestroy(); // ids

	CntTest->Db()->SetDbViewContactType(KUidContactItem); //reset view
	test.Next(_L("Search for partial name"));
	TBuf<8> shortName(name.Left(8));
	ids=CntTest->Db()->FindLC(shortName,fieldDef);
	test(ids->Count()==KTotalNumRecords);
	CleanupStack::PopAndDestroy(); // ids

	shortName=name.Right(8);
	ids=CntTest->Db()->FindLC(shortName,fieldDef);
	test(ids->Count()==1);
	CleanupStack::PopAndDestroy(); // ids
	shortName=name.Mid(3,8);
	ids=CntTest->Db()->FindLC(shortName,fieldDef);
	test(ids->Count()>1);
	CleanupStack::PopAndDestroy(); // ids

	test.Next(_L("Search for full address"));
	TBuf<16> address;
	address.Format(KTestAddress,11);
	ids=CntTest->Db()->FindLC(address,fieldDef);
	test(ids->Count()==1);
	CleanupStack::PopAndDestroy(); // ids

	test.Next(_L("NULL search string"));
	ids=CntTest->Db()->FindLC(_L(""),fieldDef);
	test(ids->Count()==KTotalNumRecords + 3); // 2 groups + 1 owncard
	CleanupStack::PopAndDestroy(); // ids

	// look in just the groups
	test.Next(_L("NULL search string in GROUPS"));
	TInt groupCount = CntTest->Db()->GroupCount();
	CntTest->Db()->SetDbViewContactType(KUidContactGroup);
	ids=CntTest->Db()->FindLC(_L(""),fieldDef);
	test(ids->Count()==groupCount);
	CleanupStack::PopAndDestroy(); // ids
//
	ids=CntTest->Db()->FindLC(_L("New Group"),fieldDef);
	test(ids->Count()==groupCount);
	CleanupStack::PopAndDestroy(); // ids
//
	CntTest->Db()->SetDbViewContactType(KUidContactItem);

	// look in just the OWN CARD
	test.Next(_L("NULL search string in OWN CARD"));
	CntTest->Db()->SetDbViewContactType(KUidContactOwnCard);
	ids=CntTest->Db()->FindLC(_L(""),fieldDef);
	test(ids->Count()==1);
	CleanupStack::PopAndDestroy(); // ids
//
	CntTest->Db()->SetDbViewContactType(KUidContactItem);

	test.Next(_L("Search for partial address"));
	TBuf<8> shortAddress(address.Left(6));
	ids=CntTest->Db()->FindLC(shortAddress,fieldDef);
	test(ids->Count()==KTotalNumRecords/2);
	CleanupStack::PopAndDestroy(); // ids
	shortAddress=address.Right(6);
	ids=CntTest->Db()->FindLC(shortAddress,fieldDef);
	test(ids->Count()==1);
	CleanupStack::PopAndDestroy(); // ids
	shortAddress=address.Mid(3,6);
	ids=CntTest->Db()->FindLC(shortAddress,fieldDef);
	test(ids->Count()>1);
	TContactItemId deleteItem=(*ids)[0];
	CleanupStack::PopAndDestroy(); // ids
//
	CntTest->AdjustContactAccessCountL(deleteItem,1);
	CntTest->Db()->DeleteContactL(deleteItem);
//
	ids=CntTest->Db()->FindLC(shortAddress,NULL);
	test(ids->Count()>1);
	CleanupStack::PopAndDestroy(); // ids
//
	CntTest->AdjustContactAccessCountL(deleteItem,-1);
	ids=CntTest->Db()->FindLC(shortAddress,NULL);
	test(ids->Count()>1);
	CleanupStack::PopAndDestroy(); // ids
//
	ids=CntTest->Db()->FindLC(_L("*"),NULL);
	test(ids->Count()==0); // literal matching now and no asterisks in this file!
	CleanupStack::PopAndDestroy(); // ids
//
	ids=CntTest->Db()->FindLC(shortAddress,fieldDef);
	test(ids->Count()>1);
	CleanupStack::PopAndDestroy(); // ids
//
	CleanupStack::PopAndDestroy(); // fieldDef
	}

GLDEF_C TContactItemId AddContactForFindL(const TDesC& aName)
	{
	CContactItem* item=CContactCard::NewLC();
	SetNameL(*item,KUidContactFieldGivenName,KUidContactFieldVCardMapUnusedN,aName,ETrue);	
	item->SetTemplateRefId(KGoldenTemplateId);
	TContactItemId id=CntTest->Db()->AddNewContactL(*item);
	CleanupStack::PopAndDestroy(item); 
	return id;
	}

GLDEF_C TContactItemId AddContactWithAddressL(const TDesC& aName, const TDesC& anAddress)
	{
	CContactItem* item=CContactCard::NewLC();
	item->SetTemplateRefId(KGoldenTemplateId);
	SetNameL(*item,KUidContactFieldGivenName,KUidContactFieldVCardMapUnusedN,aName,ETrue);	
	SetNameL(*item,KUidContactFieldAddress,  KUidContactFieldVCardMapUnusedN,anAddress,ETrue);	
	TContactItemId id=CntTest->Db()->AddNewContactL(*item);
	CleanupStack::PopAndDestroy(item); 
	return id;
	}

//
// Testcode for find defect 
// EDNAPIN-4L4EN9 "Wildcards should be treated as normal charactersin search"
//
// Test case:
// a contacts database containing contacts with names
// *,?,chris,c?ris,c*s,??,????
// 
// '*' matches contacts with fields containing '*' 
// '?' matches contacts with fields containing '?'
// 'chris' matches contacts with fields containing 'chris' but not 'c*s' or 'c?ris'
// 'c?ris' matches contacts with fields containing 'c?ris' but not 'c*s' or 'chris'
// 'c*' matches contacts with fields containing 'c*' but not 'chris' or 'c?ris'
// '??' matches contacts with fields containing '??' but not '?', 'chris', 'c*s' etc.
//
GLDEF_C void FindDefectL()
	{
	test.Next(_L("Find defect"));
	CntTest->CreateDatabaseL();
	_LIT(KAsterisk,"*");
	_LIT(KQuestionMark,"?");
	_LIT(KChris,"chris");
	_LIT(KChrisQuestion,"c?ris");
	_LIT(KChrisAsterisk,"c*s");
	_LIT(KTwoQuestionMarks,"??");
	_LIT(KFourQuestionMarks,"????");
	TContactItemId asterisk = AddContactForFindL(KAsterisk());
	AddContactForFindL(KQuestionMark());
	TContactItemId chris = AddContactForFindL(KChris());
	TContactItemId chrisquestion = AddContactForFindL(KChrisQuestion());
	TContactItemId chrisasterisk = AddContactForFindL(KChrisAsterisk());
	AddContactForFindL(KTwoQuestionMarks());
	AddContactForFindL(KFourQuestionMarks());


	CContactItemFieldDef* fieldDef=new(ELeave) CContactItemFieldDef;
	CleanupStack::PushL(fieldDef);
	fieldDef->AppendL(KUidContactFieldGivenName);

	// *	
	CContactIdArray* ids=CntTest->Db()->FindLC(KAsterisk,fieldDef);
	test(ids->Count()==2);
	test((*ids)[0]==asterisk);
	CleanupStack::PopAndDestroy(); // ids

	// ?
	ids=CntTest->Db()->FindLC(KQuestionMark,fieldDef);
	test(ids->Count()==4); // should be 4
	CleanupStack::PopAndDestroy(); // ids

	// chris
	ids=CntTest->Db()->FindLC(KChris,fieldDef);
	test(ids->Count()==1); // should be 1
	test((*ids)[0]==chris);
	CleanupStack::PopAndDestroy(); // ids

	// c?ris
	ids=CntTest->Db()->FindLC(KChrisQuestion,fieldDef);
	test(ids->Count()==1); // should be 1
	test((*ids)[0]==chrisquestion);
	CleanupStack::PopAndDestroy(); // ids

	// c*
	ids=CntTest->Db()->FindLC(KChrisAsterisk,fieldDef);
	test(ids->Count()==1); // should be 1
	test((*ids)[0]==chrisasterisk);
	CleanupStack::PopAndDestroy(); // ids

	// ??
	ids=CntTest->Db()->FindLC(KTwoQuestionMarks,fieldDef);
	test(ids->Count()==2); // should be 2
	CleanupStack::PopAndDestroy(); // ids

	CleanupStack::PopAndDestroy(fieldDef);
	CntTest->CloseDatabase();
	CntTest->DeleteDatabaseL();
	};

GLDEF_C void FindAndCheckL(const TDesC& aStr, const TInt aNum, const CContactItemFieldDef* const aFieldDef)
	{
	CContactIdArray* const ids = CntTest->Db()->FindLC(aStr,aFieldDef);
	test(aNum == ids->Count());
	CleanupStack::PopAndDestroy(ids);
	}

GLDEF_C void FindScandinavianLettersL()
	{
	test.Next(_L("Scandinavian Letters defect"));
	CntTest->CreateDatabaseL();

	//Scandinavian words with their conterparts without accents.
	_LIT(KMalmoS,"Malm\xF6"); //Malmö
	_LIT(KMalmo,"Malmo");
	_LIT(KAraS,"\xC5ra");  //Åra
	_LIT(KAra,"Ara");
	_LIT(KAtaS,"\xC4ta");  //Äta
	_LIT(KAta,"Ata");

	//Adding some contacts with Scandinavian letter in them.
	//Note that Name is stored in separate field while address is in the BLOB
	AddContactWithAddressL(KAraS, KMalmoS);
	AddContactWithAddressL(KAtaS, KAtaS);
	AddContactWithAddressL(KAtaS, KAraS);
	AddContactWithAddressL(KMalmoS, KMalmoS);

	//Search only by name column
	CContactItemFieldDef* fieldDef=new(ELeave) CContactItemFieldDef;
	CleanupStack::PushL(fieldDef);
	fieldDef->AppendL(KUidContactFieldGivenName);
	
	FindAndCheckL(KAta,  2, fieldDef);
	FindAndCheckL(KAra,  1, fieldDef);
	FindAndCheckL(KMalmo,1, fieldDef);

	//Search only by address column
	fieldDef->Delete(0);
	fieldDef->AppendL(KUidContactFieldAddress);
	test(fieldDef->Count() == 1);

//  Uncomment when DBMS collation problem will be sorted out.
	FindAndCheckL(KMalmo,2, fieldDef);
	FindAndCheckL(KAra,  1, fieldDef);
	FindAndCheckL(KAta,1, fieldDef);

	CleanupStack::PopAndDestroy(fieldDef);
	CntTest->CloseDatabase();
	CntTest->DeleteDatabaseL();
	}

GLDEF_C void DoFindL(const TDesC &aTextToFind, CContactItemFieldDef *aFieldDef, TInt aCount)
	{
	CContactIdArray* matchList=CntTest->Db()->FindLC(aTextToFind,aFieldDef);

	const TInt numIds=matchList->Count();
	RDebug::Print(_L("Matched %d contact(s) of %d expected"),numIds,aCount);
	if(numIds!=aCount)
		{
		RDebug::Print(_L("FAILED!"));
		}
	test(numIds==aCount);
	CleanupStack::PopAndDestroy(); // matchList.
	}

GLDEF_C void FindNameInFirstNameFieldL(const TDesC& aTextToFind, const TInt aNumExpectedFinds,TBool aAsync=EFalse)
	{
	RDebug::Print(_L("Find %S in Given name field"),&aTextToFind);	
	CContactItemFieldDef* def=new(ELeave) CContactItemFieldDef();
	CleanupStack::PushL(def);
	def->AppendL(KUidContactFieldGivenName);
	if(!aAsync)
		{
		DoFindL(aTextToFind,def,aNumExpectedFinds);
		}
	else
		{
		DoAsyncFindL(aTextToFind,def,aNumExpectedFinds);
		}
	CleanupStack::PopAndDestroy(); // def.
	}

GLDEF_C void FindNameInLastNameFieldL(const TDesC& aTextToFind, const TInt aNumExpectedFinds,TBool aAsync=EFalse)
	{
	RDebug::Print(_L("Find %S in Family name field"),&aTextToFind);	
	CContactItemFieldDef* def=new(ELeave) CContactItemFieldDef();
	CleanupStack::PushL(def);
	def->AppendL(KUidContactFieldFamilyName);
	if(!aAsync)
		{
		DoFindL(aTextToFind,def,aNumExpectedFinds);
		}	
	else
		{
		DoAsyncFindL(aTextToFind,def,aNumExpectedFinds);
		}
	CleanupStack::PopAndDestroy(); // def.	
	}

GLDEF_C void FindInAllFieldsL(const TDesC& aTextToFind, const TInt aNumExpectedFinds,TBool aAsync=EFalse)
	{
	RDebug::Print(_L("Find %S in All fields"),&aTextToFind);	
	CContactItemFieldDef* def=new(ELeave) CContactItemFieldDef();
	CleanupStack::PushL(def);
	def->AppendL(KUidContactFieldMatchAll);
	if(!aAsync)
		{
		DoFindL(aTextToFind,def,aNumExpectedFinds);
		}
	else
		{
		DoAsyncFindL(aTextToFind,def,aNumExpectedFinds);
		}
	CleanupStack::PopAndDestroy(); // def.
	}

GLDEF_C void FindInCompanyFieldL(const TDesC& aTextToFind, const TInt aNumExpectedFinds,TBool aAsync=EFalse)
	{
	RDebug::Print(_L("Find %S in Company name field"),&aTextToFind);	
	CContactItemFieldDef* def=new(ELeave) CContactItemFieldDef();
	CleanupStack::PushL(def);
	def->AppendL(KUidContactFieldCompanyName);
	if(!aAsync)
		{
		DoFindL(aTextToFind,def,aNumExpectedFinds);
		}
	else
		{
		DoAsyncFindL(aTextToFind,def,aNumExpectedFinds);
		}
	CleanupStack::PopAndDestroy(); // def.
	}

/**
@SYMTestCaseID PIM-T-FIND-INC049017-0001
@SYMTestType CT
@SYMTestPriority High
@SYMDEF INC049017
@SYMTestCaseDependencies cnttutil
@SYMTestCaseDesc Tests that Identity fields are searched when FindAsyncL and FindLC are used with KUidContactFieldMatchAll.
@SYMTestActions Create a contact database and add four contacts.
Search the database by using identity fields: GivenName, FamilyName and CompanyName.
Search for contacts by providing a name and using KUidContactFieldMatchAll.
Perform the search synchronously and asynchronously.
@SYMTestExpectedResults Contacts are found by given name, family name or company name when using KUidContactFieldMatchAll.
*/

GLDEF_C void FindUsingKUidContactFieldMatchAllL()
	{
	//create the database
	CntTest->CreateDatabaseL();

	//add the contacts
	//AddFindRecordL(const TDesC &aFirstName, const TDesC &aLastName, const TDesC &aCompanyName, const TDesC &aAddress)
	AddFindRecordL(_L("Robert"),_L("Jordan"),_L("Software Solutions"),_L(" "));
	AddFindRecordL(_L("Amanda"),_L("Jordan"),_L("Software Solutions"),_L(" "));
	AddFindRecordL(_L("Ben"),_L("Turner"),_L("High Res Solutions"),_L(" "));
	AddFindRecordL(_L("Chris"),_L("Andrews"),_L("Soft Kicks"),_L(" "));

	//start of tests for defect where identity fields were not searched when using KUidMatchInAllFields
	test.Next(_L("@SYMTestCaseID:PIM-T-FIND-INC049017-0001 FINDING CONTACTS BY NAME FROM GIVEN NAME FIELD."));
		{
		FindNameInFirstNameFieldL(_L("Chris"),1);
		FindNameInFirstNameFieldL(_L("Turner"),0);
		FindNameInFirstNameFieldL(_L("e"),2);
		}

	test.Next(_L("FINDING CONTACTS BY NAME FROM FAMILY NAME FIELD"));
		{
		FindNameInLastNameFieldL(_L("Turner"),1);
		FindNameInLastNameFieldL(_L("Jordan"),2);
		FindNameInLastNameFieldL(_L("r"),4);
		FindNameInLastNameFieldL(_L("Chris"),0);
		}

	test.Next(_L("FINDING CONTACTS BY NAME FROM ALL FIELDS"));
		{
		FindInAllFieldsL(_L("Ben"),1);
		FindInAllFieldsL(_L("Jordan"),2);
		FindInAllFieldsL(_L("r"),4);
		FindInAllFieldsL(_L("xxxxxxxxx"),0);
		}

	test.Next(_L("FINDING CONTACTS BY COMPANY FROM COMPANY NAME FIELD"));
		{
		FindInCompanyFieldL(_L("Ben"),0);
		FindInCompanyFieldL(_L("High Res Solutions"),1);
		FindInCompanyFieldL(_L("Software Solutions"),2);
		FindInCompanyFieldL(_L("s"),4);
		}

	test.Next(_L("FINDING CONTACTS BY COMPANY FROM  ALL FIELDS"));
		{
		FindInAllFieldsL(_L("Kick"),1);
		FindInAllFieldsL(_L("High Res Solutions"),1);
		FindInAllFieldsL(_L("Software Solutions"),2);
		FindInAllFieldsL(_L("s"),4);
		}
	//end of tests for defect where identity fields were not searched when using KUidMatchInAllFields

	//start of Async tests for defect where identity fields were not searched when using KUidMatchInAllFields
	test.Next(_L("FINDING CONTACTS BY NAME FROM GIVEN NAME FIELD ASYNCHRONOUSLY"));
		{
		FindNameInFirstNameFieldL(_L("Chris"),1,ETrue);
		FindNameInFirstNameFieldL(_L("Turner"),0,ETrue);
		FindNameInFirstNameFieldL(_L("e"),2,ETrue);
		}

	test.Next(_L("FINDING CONTACTS BY NAME FROM FAMILY NAME FIELD ASYNCHRONOUSLY"));
		{
		FindNameInLastNameFieldL(_L("Turner"),1,ETrue);
		FindNameInLastNameFieldL(_L("Jordan"),2,ETrue);
		FindNameInLastNameFieldL(_L("r"),4,ETrue);
		FindNameInLastNameFieldL(_L("Chris"),0,ETrue);
		}

	test.Next(_L("FINDING CONTACTS BY NAME FROM ALL FIELDS ASYNCHRONOUSLY"));
		{
		FindInAllFieldsL(_L("Ben"),1,ETrue);
		FindInAllFieldsL(_L("Jordan"),2,ETrue);
		FindInAllFieldsL(_L("r"),4,ETrue);
		FindInAllFieldsL(_L("xxxxxxxxx"),0,ETrue);
		}

	test.Next(_L("FINDING CONTACTS BY COMPANY FROM COMPANY NAME FIELD ASYNCHRONOUSLY"));
		{
		FindInCompanyFieldL(_L("Ben"),0,ETrue);
		FindInCompanyFieldL(_L("High Res Solutions"),1,ETrue);
		FindInCompanyFieldL(_L("Software Solutions"),2,ETrue);
		FindInCompanyFieldL(_L("s"),4,ETrue);
		}

	test.Next(_L("FINDING CONTACTS BY COMPANY FROM  ALL FIELDS ASYNCHRONOUSLY"));
		{
		FindInAllFieldsL(_L("Kick"),1,ETrue);
		FindInAllFieldsL(_L("High Res Solutions"),1,ETrue);
		FindInAllFieldsL(_L("Software Solutions"),2,ETrue);
		FindInAllFieldsL(_L("s"),4,ETrue);
		}
	//end of Async tests for defect where identity fields were not searched when using KUidMatchInAllFields

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